File: batterystats.py

package info (click to toggle)
python-pure-python-adb 0.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,500 kB
  • sloc: python: 2,597; makefile: 8; sh: 1
file content (43 lines) | stat: -rw-r--r-- 1,296 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from ppadb.plugins.device import batterystats_section as section_module
from ppadb.plugins import Plugin
from ppadb.utils.logger import AdbLogging

logger = AdbLogging.get_logger(__name__)


class BatteryStats(Plugin):
    def get_battery_level(self):
        battery = self.shell("dumpsys battery")

        for line in battery.split('\n'):
            tokens = line.split(":")
            if tokens[0].strip() == "level" and len(tokens) == 2:
                return int(tokens[1])

        return None


    def get_batterystats(self):
        result = self.shell("dumpsys batterystats -c")
        sections = {}
        for line in result.split("\n"):
            if not line.strip():
                continue

            tokens = line.split(",", 4)
            if len(tokens) < 5:
                continue

            dummy, uid, mode, id, remaining_fields = tokens
            print(dummy, uid, mode, id, remaining_fields)
            SectionClass = section_module.get_section(id)
            if not SectionClass:
                logger.error("Unknown section {} in batterystats".format(id))
                continue

            if id not in sections:
                sections[id] = []

            sections[id].append(SectionClass(*remaining_fields.split(",")))

        return sections