Listen for battery events

This commit is contained in:
M66B 2022-04-01 09:58:07 +02:00
parent a96171665a
commit 7f0bea899c
1 changed files with 19 additions and 0 deletions

View File

@ -35,6 +35,7 @@ import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.os.OperationCanceledException;
@ -206,6 +207,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
registerReceiver(suspendChanged, suspend);
}
registerReceiver(batteryChanged, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
DB db = DB.getInstance(this);
@ -866,6 +869,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
unregisterReceiver(batteryChanged);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
unregisterReceiver(suspendChanged);
@ -2652,6 +2657,20 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}
};
private final BroadcastReceiver batteryChanged = new BroadcastReceiver() {
private Integer lastLevel = null;
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
if (!Objects.equals(level, lastLevel)) {
lastLevel = level;
EntityLog.log(context, intent.getAction() + " " +
TextUtils.join(", ", Log.getExtras(intent.getExtras())));
}
}
};
private void updateNetworkState(final Network network, final String reason) {
getMainHandler().post(new Runnable() {
@Override