| 1 | ''' |
| 2 | Module of Android API for plyer.battery. |
| 3 | ''' |
| 4 | |
| 5 | from jnius import autoclass, cast |
| 6 | from plyer.platforms.android import activity |
| 7 | from plyer.facades import Battery |
| 8 | |
| 9 | Intent = autoclass('android.content.Intent') |
| 10 | BatteryManager = autoclass('android.os.BatteryManager') |
| 11 | IntentFilter = autoclass('android.content.IntentFilter') |
| 12 | |
| 13 | |
| 14 | class AndroidBattery(Battery): |
| 15 | ''' |
| 16 | Implementation of Android battery API. |
| 17 | ''' |
| 18 | |
| 19 | def _get_state(self): |
| 20 | status = {"isCharging": None, "percentage": None} |
| 21 | |
| 22 | ifilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) |
| 23 | |
| 24 | battery_status = cast( |
| 25 | 'android.content.Intent', |
| 26 | activity.registerReceiver(None, ifilter) |
| 27 | ) |
| 28 | |
| 29 | query = battery_status.getIntExtra(BatteryManager.EXTRA_STATUS, -1) |
| 30 | is_charging = query == BatteryManager.BATTERY_STATUS_CHARGING |
| 31 | is_full = query == BatteryManager.BATTERY_STATUS_FULL |
| 32 | |
| 33 | level = battery_status.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) |
| 34 | scale = battery_status.getIntExtra(BatteryManager.EXTRA_SCALE, -1) |
| 35 | percentage = (level / float(scale)) * 100 |
| 36 | |
| 37 | status['isCharging'] = is_charging or is_full |
| 38 | status['percentage'] = percentage |
| 39 | |
| 40 | return status |
| 41 | |
| 42 | |
| 43 | def instance(): |
| 44 | ''' |
| 45 | Instance for facade proxy. |
| 46 | ''' |
| 47 | return AndroidBattery() |