Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): use newer APIs for battery level/status properties #13996

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -85,7 +86,6 @@ public PlatformModule()
super();

batteryState = BATTERY_STATE_UNKNOWN;
batteryLevel = -1;

// Extract "<major>.<minor>" integers from OS version string.
String[] versionComponents = Build.VERSION.RELEASE.split("\\.");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
Loading