Bugfix when checking for any battery charging type

This commit is contained in:
jens 2025-03-23 18:33:13 +01:00
parent abd346946a
commit 04d2e4b432
4 changed files with 34 additions and 29 deletions

View File

@ -10,12 +10,7 @@ import android.widget.RadioButton;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.jens.automation2.ActivityManageRule; import com.jens.automation2.receivers.BatteryReceiver;
import com.jens.automation2.Miscellaneous;
import com.jens.automation2.R;
import com.jens.automation2.Trigger;
import org.apache.commons.lang3.StringUtils;
public class ActivityManageTriggerCharging extends Activity public class ActivityManageTriggerCharging extends Activity
{ {
@ -69,13 +64,13 @@ public class ActivityManageTriggerCharging extends Activity
String param2 = ""; String param2 = "";
if(rbChargingTypeAny.isChecked()) if(rbChargingTypeAny.isChecked())
param2 = "0"; param2 = String.valueOf(BatteryReceiver.batteryChargingTypeAny);
else if(rbChargingTypeAc.isChecked()) else if(rbChargingTypeAc.isChecked())
param2 = String.valueOf(BatteryManager.BATTERY_PLUGGED_AC); param2 = String.valueOf(BatteryReceiver.batteryChargingTypeAc);
else if(rbChargingTypeUsb.isChecked()) else if(rbChargingTypeUsb.isChecked())
param2 = String.valueOf(BatteryManager.BATTERY_PLUGGED_USB); param2 = String.valueOf(BatteryReceiver.batteryChargingTypeUsb);
else if(rbChargingTypeWireless.isChecked()) else if(rbChargingTypeWireless.isChecked())
param2 = String.valueOf(BatteryManager.BATTERY_PLUGGED_WIRELESS); param2 = String.valueOf(BatteryReceiver.batteryChargingTypeWireless);
response.putExtra(ActivityManageRule.intentNameTriggerParameter2, param2); response.putExtra(ActivityManageRule.intentNameTriggerParameter2, param2);

View File

@ -1220,7 +1220,9 @@ public class Trigger
int desiredType; int desiredType;
String[] typeParams = getTriggerParameter2().split(triggerParameter2Split, -1); String[] typeParams = getTriggerParameter2().split(triggerParameter2Split, -1);
desiredType = Integer.parseInt(typeParams[0]); desiredType = Integer.parseInt(typeParams[0]);
if(desiredType == BatteryReceiver.getCurrentChargingType()) if(desiredType == BatteryReceiver.batteryChargingTypeAny)
return true;
else if(desiredType == BatteryReceiver.getCurrentChargingType())
return true; return true;
} }
} }

View File

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

View File

@ -1 +1,2 @@
* Fixed: Crash when triggering a URL without parameter pairs * Fixed: Crash when triggering a URL without parameter pairs
* Fixed: When checking for battery charging type "any" the trigger didn't fire.