diff options
Diffstat (limited to 'external/plyer/platforms/android/battery.py')
-rw-r--r-- | external/plyer/platforms/android/battery.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/external/plyer/platforms/android/battery.py b/external/plyer/platforms/android/battery.py new file mode 100644 index 0000000..2ade1d2 --- /dev/null +++ b/external/plyer/platforms/android/battery.py @@ -0,0 +1,34 @@ +from jnius import autoclass, cast +from plyer.platforms.android import activity +from plyer.facades import Battery + +Intent = autoclass('android.content.Intent') +BatteryManager = autoclass('android.os.BatteryManager') +IntentFilter = autoclass('android.content.IntentFilter') + + +class AndroidBattery(Battery): + def _get_state(self): + status = {"isCharging": None, "percentage": None} + + ifilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) + + batteryStatus = cast('android.content.Intent', + activity.registerReceiver(None, ifilter)) + + query = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1) + isCharging = (query == BatteryManager.BATTERY_STATUS_CHARGING or + query == BatteryManager.BATTERY_STATUS_FULL) + + level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) + scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) + percentage = level / float(scale) + + status['isCharging'] = isCharging + status['percentage'] = percentage + + return status + + +def instance(): + return AndroidBattery() |