diff --git a/android/modules/platform/src/java/ti/modules/titanium/platform/PlatformModule.java b/android/modules/platform/src/java/ti/modules/titanium/platform/PlatformModule.java index da68156e563..b8efddc3f5e 100644 --- a/android/modules/platform/src/java/ti/modules/titanium/platform/PlatformModule.java +++ b/android/modules/platform/src/java/ti/modules/titanium/platform/PlatformModule.java @@ -6,6 +6,8 @@ */ package ti.modules.titanium.platform; +import static android.content.Context.BATTERY_SERVICE; + import org.appcelerator.kroll.KrollDict; import org.appcelerator.kroll.KrollFunction; import org.appcelerator.kroll.KrollInvocation; @@ -75,7 +77,6 @@ public class PlatformModule extends KrollModule private int versionMinor; private int versionPatch; protected int batteryState; - protected double batteryLevel; protected boolean batteryStateReady; protected BroadcastReceiver batteryStateReceiver; @@ -85,7 +86,6 @@ public PlatformModule() super(); batteryState = BATTERY_STATE_UNKNOWN; - batteryLevel = -1; // Extract "." integers from OS version string. String[] versionComponents = Build.VERSION.RELEASE.split("\\."); @@ -399,13 +399,23 @@ public boolean getBatteryMonitoring() @Kroll.getProperty public int getBatteryState() { - return batteryState; + if (Build.VERSION.SDK_INT >= 26) { + Context context = TiApplication.getInstance().getApplicationContext(); + BatteryManager bm = (BatteryManager) context.getSystemService(BATTERY_SERVICE); + int batStatus = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS); + return batStatus; + } else { + return batteryState; + } } @Kroll.getProperty public double getBatteryLevel() { - return batteryLevel; + Context context = TiApplication.getInstance().getApplicationContext(); + BatteryManager bm = (BatteryManager) context.getSystemService(BATTERY_SERVICE); + int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); + return batLevel; } @Kroll.getProperty @@ -495,7 +505,7 @@ protected void registerBatteryStateReceiver() public void onReceive(Context context, Intent intent) { int scale = intent.getIntExtra(TiC.PROPERTY_SCALE, -1); - batteryLevel = convertBatteryLevel(intent.getIntExtra(TiC.PROPERTY_LEVEL, -1), scale); + double batteryLevel = convertBatteryLevel(intent.getIntExtra(TiC.PROPERTY_LEVEL, -1), scale); batteryState = convertBatteryStatus(intent.getIntExtra(TiC.PROPERTY_STATUS, -1)); KrollDict event = new KrollDict();