Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into rockgecko-development-power
  • Loading branch information
n8fr8 committed Dec 28, 2017
2 parents 4582e28 + 32f8977 commit 455b6f0
Showing 1 changed file with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;

import org.havenapp.main.R;
import org.havenapp.main.model.EventTrigger;
Expand All @@ -19,17 +20,30 @@ public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;

int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
// Can't use intent.getIntExtra(BatteryManager.EXTRA_STATUS), as the extra is not provided.
// The code example at
// https://developer.android.com/training/monitoring-device-state/battery-monitoring.html
// is wrong
// see https://stackoverflow.com/questions/10211609/problems-with-action-power-connected

// explicitly check the intent action
// avoids lint issue UnsafeProtectedBroadcastReceiver
boolean isCharging;
if(intent.getAction() == null) return;
switch(intent.getAction()){
case Intent.ACTION_POWER_CONNECTED:
isCharging = true;
break;
case Intent.ACTION_POWER_DISCONNECTED:
isCharging = false;
break;
default:
return;
}

if (MonitorService.getInstance() != null
&& MonitorService.getInstance().isRunning()) {
MonitorService.getInstance().alert(EventTrigger.POWER, context.getString(R.string.status_charging) + isCharging + " USB:" + usbCharge + " AC:" + acCharge);
MonitorService.getInstance().alert(EventTrigger.POWER, context.getString(R.string.status_charging) + isCharging );
}
}
}

0 comments on commit 455b6f0

Please sign in to comment.