summaryrefslogtreecommitdiff
path: root/external/plyer/platforms/ios/battery.py
blob: 55aa2c61c4ecc6890339b197e17573c9a2b73f3e (plain) (blame)
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
from pyobjus import autoclass
from pyobjus.dylib_manager import load_framework
from plyer.facades import Battery

load_framework('/System/Library/Frameworks/UIKit.framework')
UIDevice = autoclass('UIDevice')


class iOSBattery(Battery):
    def __init__(self):
        super(iOSBattery, self).__init__()
        self.device = UIDevice.currentDevice()

    def _get_state(self):
        status = {"isCharging": None, "percentage": None}

        if(not self.device.batteryMonitoringEnabled):
            self.device.setBatteryMonitoringEnabled_(True)

        if self.device.batteryState == 0:
            isCharging = None
        elif self.device.batteryState == 2:
            isCharging = True
        else:
            isCharging = False

        percentage = self.device.batteryLevel * 100.

        status['isCharging'] = isCharging
        status['percentage'] = percentage

        return status


def instance():
    return iOSBattery()