Skip to content

Commit e11ca5d

Browse files
authored
add check for battery status not availlable, return default value instead (#521)
1 parent 7208c48 commit e11ca5d

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
public class MapboxTelemetry implements Callback, LocationEngineListener {
5252

5353
private static final String LOG_TAG = MapboxTelemetry.class.getSimpleName();
54+
private static final int UNAVAILABLE_BATTERY_LEVEL = 100;
5455

5556
private static MapboxTelemetry instance;
5657

@@ -241,9 +242,13 @@ private void registerBatteryUpdates() {
241242
}
242243

243244
private int getBatteryLevel() {
244-
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
245-
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
246-
return Math.round((level / (float) scale) * 100);
245+
if (batteryStatus != null) {
246+
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
247+
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
248+
return Math.round((level / (float) scale) * 100);
249+
} else {
250+
return UNAVAILABLE_BATTERY_LEVEL;
251+
}
247252
}
248253

249254
/**

0 commit comments

Comments
 (0)