| 1 | ''' |
| 2 | Module of iOS API for plyer.battery. |
| 3 | ''' |
| 4 | |
| 5 | from pyobjus import autoclass |
| 6 | from pyobjus.dylib_manager import load_framework |
| 7 | from plyer.facades import Battery |
| 8 | |
| 9 | load_framework('/System/Library/Frameworks/UIKit.framework') |
| 10 | UIDevice = autoclass('UIDevice') |
| 11 | |
| 12 | |
| 13 | class IOSBattery(Battery): |
| 14 | ''' |
| 15 | Implementation of iOS battery API. |
| 16 | ''' |
| 17 | |
| 18 | def __init__(self): |
| 19 | super().__init__() |
| 20 | self.device = UIDevice.currentDevice() |
| 21 | |
| 22 | def _get_state(self): |
| 23 | status = {"isCharging": None, "percentage": None} |
| 24 | |
| 25 | if not self.device.batteryMonitoringEnabled: |
| 26 | self.device.setBatteryMonitoringEnabled_(True) |
| 27 | |
| 28 | if self.device.batteryState == 0: |
| 29 | is_charging = None |
| 30 | elif self.device.batteryState == 2: |
| 31 | is_charging = True |
| 32 | else: |
| 33 | is_charging = False |
| 34 | |
| 35 | percentage = self.device.batteryLevel * 100. |
| 36 | |
| 37 | status['isCharging'] = is_charging |
| 38 | status['percentage'] = percentage |
| 39 | |
| 40 | return status |
| 41 | |
| 42 | |
| 43 | def instance(): |
| 44 | ''' |
| 45 | Instance for facade proxy. |
| 46 | ''' |
| 47 | return IOSBattery() |