AndroidAPS/app/src/main/java/info/nightscout/androidaps/utils/BatteryLevel.java

36 lines
1.1 KiB
Java
Raw Normal View History

2019-02-26 20:38:27 +01:00
package info.nightscout.androidaps.utils;
2017-02-20 08:32:10 +01:00
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
2017-02-21 13:45:33 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2017-02-20 08:32:10 +01:00
import info.nightscout.androidaps.MainApp;
2020-01-13 19:22:28 +01:00
import info.nightscout.androidaps.logging.StacktraceLoggerWrapper;
2017-02-20 08:32:10 +01:00
/**
* Created by mike on 20.02.2017.
*/
public class BatteryLevel {
2020-01-13 19:22:28 +01:00
private static Logger log = StacktraceLoggerWrapper.getLogger(BatteryLevel.class);
2017-02-20 08:32:10 +01:00
static public int lastUploadedLevel = 0;
static public int getBatteryLevel() {
2017-02-21 13:45:33 +01:00
int batteryLevel = 0;
2017-02-20 08:32:10 +01:00
Intent batteryIntent = MainApp.instance().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
if (batteryIntent != null) {
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
2017-02-21 13:45:33 +01:00
if (level != -1 && scale != -1) {
batteryLevel = (int) (((float) level / (float) scale) * 100.0f);
2017-02-20 08:32:10 +01:00
}
2017-02-21 13:45:33 +01:00
}
log.debug("Battery level: " + batteryLevel);
return batteryLevel;
2017-02-20 08:32:10 +01:00
}
}