2018-04-25 20:49:03 +02:00
|
|
|
package info.nightscout.androidaps.receivers;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.BatteryManager;
|
|
|
|
|
|
|
|
import info.nightscout.androidaps.events.EventChargingState;
|
2019-10-15 13:31:55 +02:00
|
|
|
import info.nightscout.androidaps.plugins.bus.RxBus;
|
2018-04-25 20:49:03 +02:00
|
|
|
|
|
|
|
public class ChargingStateReceiver extends BroadcastReceiver {
|
|
|
|
|
2019-06-01 18:40:33 +02:00
|
|
|
private static EventChargingState lastEvent;
|
|
|
|
|
2018-04-25 20:49:03 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2018-04-29 22:49:43 +02:00
|
|
|
EventChargingState event = grabChargingState(context);
|
2018-04-25 20:49:03 +02:00
|
|
|
|
|
|
|
if (event != null)
|
2019-10-15 13:31:55 +02:00
|
|
|
RxBus.INSTANCE.send(event);
|
2019-06-01 18:40:33 +02:00
|
|
|
lastEvent = event;
|
2018-04-25 20:49:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-29 22:49:43 +02:00
|
|
|
public EventChargingState grabChargingState(Context context) {
|
|
|
|
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
|
|
|
|
|
2018-08-13 23:57:16 +02:00
|
|
|
if (bm == null)
|
|
|
|
return new EventChargingState(false);
|
|
|
|
|
2018-04-29 22:49:43 +02:00
|
|
|
int status = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS);
|
|
|
|
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
|
|
|
|
|| status == BatteryManager.BATTERY_STATUS_FULL;
|
|
|
|
|
|
|
|
EventChargingState event = new EventChargingState(isCharging);
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2019-06-01 18:40:33 +02:00
|
|
|
static public boolean isCharging() {
|
2019-08-15 17:10:36 +02:00
|
|
|
return lastEvent != null && lastEvent.isCharging();
|
2019-06-01 18:40:33 +02:00
|
|
|
}
|
2019-07-18 23:49:59 +02:00
|
|
|
|
|
|
|
static public EventChargingState getLastEvent() {
|
|
|
|
return lastEvent;
|
|
|
|
}
|
2018-04-25 20:49:03 +02:00
|
|
|
}
|