Skip to content

Commit

Permalink
Begin add power events
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeswitz committed Feb 12, 2019
1 parent 693db02 commit d8aa371
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

import org.havenapp.main.R;
import org.havenapp.main.model.EventTrigger;
Expand All @@ -26,7 +28,14 @@ public void onReceive(Context context, Intent intent) {

// explicitly check the intent action
// avoids lint issue UnsafeProtectedBroadcastReceiver
boolean isCharging;
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;

if(intent.getAction() == null) return;
switch(intent.getAction()){
case Intent.ACTION_POWER_CONNECTED:
Expand All @@ -44,4 +53,16 @@ public void onReceive(Context context, Intent intent) {
MonitorService.getInstance().alert(EventTrigger.POWER, context.getString(R.string.status_charging) + isCharging );
}
}

//Ref: https://developer.android.com/training/monitoring-device-state/battery-monitoring.html

private void getBatteryStatus(Context context) {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);

// How are we charging?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
}
}
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<string name="action_cancel">Deactivate</string>
<string name="status_on">ACTIVE</string>
<string name="status_charging">Charging:</string>
<string name="power_source">Power Source: </string>
<string name="you_will_receive_a_text_when_the_app_hears_or_sees_something">You will receive a text when the app hears or sees something</string>
<string name="know_immediately_when_haven_detects_something">Know immediately when Haven detects something</string>
<string name="phone_saved">Phone number saved!</string>
Expand Down

0 comments on commit d8aa371

Please sign in to comment.