mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-09-03 23:55:08 +00:00
Fix crash on Android 5.1 when calling registerReceiver(null)
Plus small optimization by re-using the NetworkPackage.
This commit is contained in:
@@ -40,7 +40,7 @@ public class BatteryPlugin extends Plugin {
|
|||||||
private static final int THRESHOLD_EVENT_NONE= 0;
|
private static final int THRESHOLD_EVENT_NONE= 0;
|
||||||
private static final int THRESHOLD_EVENT_BATTERY_LOW = 1;
|
private static final int THRESHOLD_EVENT_BATTERY_LOW = 1;
|
||||||
|
|
||||||
private NetworkPackage lastInfo = null;
|
private NetworkPackage batteryInfo = new NetworkPackage(PACKAGE_TYPE_BATTERY);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getDisplayName() {
|
public String getDisplayName() {
|
||||||
@@ -56,18 +56,18 @@ public class BatteryPlugin extends Plugin {
|
|||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent batteryIntent) {
|
public void onReceive(Context context, Intent batteryIntent) {
|
||||||
|
|
||||||
Intent batteryChargeIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
||||||
int level = batteryChargeIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
|
||||||
int scale = batteryChargeIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);
|
int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
||||||
int currentCharge = level*100 / scale;
|
|
||||||
boolean isCharging = (0 != batteryChargeIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0));
|
int currentCharge = (level == -1)? batteryInfo.getInt("currentCharge") : level*100 / scale;
|
||||||
|
boolean isCharging = (plugged == -1)? batteryInfo.getBoolean("isCharging") : (0 != plugged);
|
||||||
boolean lowBattery = Intent.ACTION_BATTERY_LOW.equals(batteryIntent.getAction());
|
boolean lowBattery = Intent.ACTION_BATTERY_LOW.equals(batteryIntent.getAction());
|
||||||
int thresholdEvent = lowBattery? THRESHOLD_EVENT_BATTERY_LOW : THRESHOLD_EVENT_NONE;
|
int thresholdEvent = lowBattery? THRESHOLD_EVENT_BATTERY_LOW : THRESHOLD_EVENT_NONE;
|
||||||
|
|
||||||
if (lastInfo != null
|
if (isCharging == batteryInfo.getBoolean("isCharging")
|
||||||
&& isCharging == lastInfo.getBoolean("isCharging")
|
&& currentCharge == batteryInfo.getInt("currentCharge")
|
||||||
&& currentCharge == lastInfo.getInt("currentCharge")
|
&& thresholdEvent == batteryInfo.getInt("thresholdEvent")
|
||||||
&& thresholdEvent == lastInfo.getInt("thresholdEvent")
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
//Do not send again if nothing has changed
|
//Do not send again if nothing has changed
|
||||||
@@ -75,12 +75,10 @@ public class BatteryPlugin extends Plugin {
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_BATTERY);
|
batteryInfo.set("currentCharge", currentCharge);
|
||||||
np.set("currentCharge", currentCharge);
|
batteryInfo.set("isCharging", isCharging);
|
||||||
np.set("isCharging", isCharging);
|
batteryInfo.set("thresholdEvent", thresholdEvent);
|
||||||
np.set("thresholdEvent", thresholdEvent);
|
device.sendPackage(batteryInfo);
|
||||||
device.sendPackage(np);
|
|
||||||
lastInfo = np;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,8 +87,10 @@ public class BatteryPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreate() {
|
public boolean onCreate() {
|
||||||
context.registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
IntentFilter intentFilter = new IntentFilter();
|
||||||
context.registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));
|
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
|
||||||
|
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
|
||||||
|
context.registerReceiver(receiver, intentFilter);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,9 +104,7 @@ public class BatteryPlugin extends Plugin {
|
|||||||
public boolean onPackageReceived(NetworkPackage np) {
|
public boolean onPackageReceived(NetworkPackage np) {
|
||||||
|
|
||||||
if (np.getBoolean("request")) {
|
if (np.getBoolean("request")) {
|
||||||
if (lastInfo != null) {
|
device.sendPackage(batteryInfo);
|
||||||
device.sendPackage(lastInfo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
Reference in New Issue
Block a user