|
|
|
@ -1,11 +1,13 @@
|
|
|
|
|
package com.jens.automation2.receivers;
|
|
|
|
|
|
|
|
|
|
import static android.os.BatteryManager.*;
|
|
|
|
|
|
|
|
|
|
import android.os.BatteryManager;
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
|
import android.os.BatteryManager;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import com.jens.automation2.ActivityPermissions;
|
|
|
|
@ -18,6 +20,11 @@ import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
public class BatteryReceiver extends BroadcastReceiver implements AutomationListenerInterface
|
|
|
|
|
{
|
|
|
|
|
public static final int batteryChargingTypeAny = 0;
|
|
|
|
|
public static final int batteryChargingTypeAc = BatteryManager.BATTERY_PLUGGED_AC;
|
|
|
|
|
public static final int batteryChargingTypeUsb = BatteryManager.BATTERY_PLUGGED_USB;
|
|
|
|
|
public static final int batteryChargingTypeWireless = BatteryManager.BATTERY_PLUGGED_WIRELESS;
|
|
|
|
|
|
|
|
|
|
public static AutomationService automationServiceRef = null;
|
|
|
|
|
static int batteryLevel = -1; // initialize with a better value than this
|
|
|
|
|
static boolean usbHostConnected = false;
|
|
|
|
@ -102,7 +109,7 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
|
|
|
|
batteryLevel = intent.getIntExtra(EXTRA_LEVEL, -1);
|
|
|
|
|
// int scale = -1;
|
|
|
|
|
// int voltage = -1;
|
|
|
|
|
// int temp = -1;
|
|
|
|
@ -112,36 +119,36 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
|
|
|
|
|
Log.i("Battery", "Level: " + String.valueOf(batteryLevel));
|
|
|
|
|
this.actionBatteryLevel(context);
|
|
|
|
|
|
|
|
|
|
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
|
|
|
|
|
int statusPlugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
|
|
|
|
int status = intent.getIntExtra(EXTRA_STATUS, -1);
|
|
|
|
|
int statusPlugged = intent.getIntExtra(EXTRA_PLUGGED, -1);
|
|
|
|
|
Miscellaneous.logEvent("i", "BatteryReceiver", "Status: " + String.valueOf(statusPlugged), 5);
|
|
|
|
|
|
|
|
|
|
switch(statusPlugged)
|
|
|
|
|
{
|
|
|
|
|
case BatteryManager.BATTERY_PLUGGED_AC:
|
|
|
|
|
case BATTERY_PLUGGED_AC:
|
|
|
|
|
// Toast.makeText(context, "Regular charging", Toast.LENGTH_LONG).show();
|
|
|
|
|
Miscellaneous.logEvent("i", "BatteryReceiver", "Regular charging.", 5);
|
|
|
|
|
this.actionCharging(context, statusPlugged);
|
|
|
|
|
break;
|
|
|
|
|
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
|
|
|
|
|
case BATTERY_PLUGGED_WIRELESS:
|
|
|
|
|
// Toast.makeText(context, "Regular charging", Toast.LENGTH_LONG).show();
|
|
|
|
|
Miscellaneous.logEvent("i", "BatteryReceiver", "Wireless charging.", 5);
|
|
|
|
|
this.actionCharging(context, statusPlugged);
|
|
|
|
|
break;
|
|
|
|
|
case BatteryManager.BATTERY_PLUGGED_USB:
|
|
|
|
|
case BATTERY_PLUGGED_USB:
|
|
|
|
|
this.actionUsbConnected(context);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(status)
|
|
|
|
|
{
|
|
|
|
|
case BatteryManager.BATTERY_STATUS_CHARGING:
|
|
|
|
|
case BatteryManager.BATTERY_STATUS_FULL:
|
|
|
|
|
case BATTERY_STATUS_CHARGING:
|
|
|
|
|
case BATTERY_STATUS_FULL:
|
|
|
|
|
// Miscellaneous.logEvent("i", "BatteryReceiver", "Device has been fully charged.", 5);
|
|
|
|
|
this.actionCharging(context, statusPlugged);
|
|
|
|
|
break;
|
|
|
|
|
case BatteryManager.BATTERY_STATUS_DISCHARGING:
|
|
|
|
|
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
|
|
|
|
|
case BATTERY_STATUS_DISCHARGING:
|
|
|
|
|
case BATTERY_STATUS_NOT_CHARGING:
|
|
|
|
|
this.actionDischarging(context);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
@ -163,7 +170,7 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
|
|
|
|
|
case 1:
|
|
|
|
|
Miscellaneous.logEvent("i", "ChargingInfo", "Device is discharging.", 3);
|
|
|
|
|
break;
|
|
|
|
|
case BatteryManager.BATTERY_STATUS_CHARGING:
|
|
|
|
|
case BATTERY_STATUS_CHARGING:
|
|
|
|
|
Miscellaneous.logEvent("i", "ChargingInfo", "Device is charging.", 3);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
@ -178,10 +185,10 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
|
|
|
|
|
|
|
|
|
|
private void actionCharging(Context context, int statusPlugged)
|
|
|
|
|
{
|
|
|
|
|
if(currentChargingState != BatteryManager.BATTERY_STATUS_CHARGING) // Avoid flooding the log. This event will occur on a regular basis even though charging state wasn't changed.
|
|
|
|
|
if(currentChargingState != BATTERY_STATUS_CHARGING) // Avoid flooding the log. This event will occur on a regular basis even though charging state wasn't changed.
|
|
|
|
|
{
|
|
|
|
|
Miscellaneous.logEvent("i", "BatteryReceiver", "Battery is charging or full.", 3);
|
|
|
|
|
currentChargingState = BatteryManager.BATTERY_STATUS_CHARGING;
|
|
|
|
|
currentChargingState = BATTERY_STATUS_CHARGING;
|
|
|
|
|
currentChargingType = statusPlugged;
|
|
|
|
|
|
|
|
|
|
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger_Enum.charging);
|
|
|
|
@ -207,10 +214,10 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
|
|
|
|
|
|
|
|
|
|
private void actionDischarging(Context context)
|
|
|
|
|
{
|
|
|
|
|
if(currentChargingState != BatteryManager.BATTERY_STATUS_UNKNOWN) // Avoid flooding the log. This event will occur on a regular basis even though charging state wasn't changed.
|
|
|
|
|
if(currentChargingState != BATTERY_STATUS_UNKNOWN) // Avoid flooding the log. This event will occur on a regular basis even though charging state wasn't changed.
|
|
|
|
|
{
|
|
|
|
|
Miscellaneous.logEvent("i", "BatteryReceiver", "Battery is discharging.", 3);
|
|
|
|
|
currentChargingState = BatteryManager.BATTERY_STATUS_UNKNOWN;
|
|
|
|
|
currentChargingState = BATTERY_STATUS_UNKNOWN;
|
|
|
|
|
//activate rule(s)
|
|
|
|
|
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger_Enum.charging);
|
|
|
|
|
// ArrayList<Rule> ruleCandidates = Rule.findRuleCandidatesByCharging(false);
|
|
|
|
@ -243,7 +250,7 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
|
|
|
|
|
oneRule.activate(automationServiceRef, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.actionCharging(context, BatteryManager.BATTERY_PLUGGED_USB);
|
|
|
|
|
this.actionCharging(context, BATTERY_PLUGGED_USB);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|