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.MainApp;
|
|
|
|
import info.nightscout.androidaps.events.EventChargingState;
|
|
|
|
|
|
|
|
public class ChargingStateReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
@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)
|
|
|
|
MainApp.bus().post(event);
|
|
|
|
}
|
|
|
|
|
2018-04-29 22:49:43 +02:00
|
|
|
public EventChargingState grabChargingState(Context context) {
|
|
|
|
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-25 20:49:03 +02:00
|
|
|
}
|