From 08cd3d2f8850a793f52d130b27ade2c76de91577 Mon Sep 17 00:00:00 2001 From: sim Date: Thu, 28 Nov 2024 16:26:31 +0000 Subject: [PATCH] Sync only high and normal messages with low battery --- .../distributor/nextpush/AppCompanion.kt | 5 ++ .../distributor/nextpush/api/Api.kt | 5 ++ .../nextpush/callback/BatteryCallback.kt | 68 +++++++++++++++++++ .../nextpush/services/StartService.kt | 5 ++ 4 files changed, 83 insertions(+) create mode 100644 app/src/main/java/org/unifiedpush/distributor/nextpush/callback/BatteryCallback.kt diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/AppCompanion.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/AppCompanion.kt index 397cb79..5f7ad4e 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/AppCompanion.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/AppCompanion.kt @@ -18,6 +18,11 @@ object AppCompanion { */ val hasInternet = AtomicBoolean(true) + /** + * If we have low battery, we listen for normal and high events only + */ + val lowBattery = AtomicBoolean(false) + /** Have we received the start event ? To check the reverse proxy timeout is high enough */ val started = AtomicBoolean(false) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt index 30839ff..8f53ab3 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt @@ -14,6 +14,7 @@ import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.sse.EventSource import okhttp3.sse.EventSources +import org.unifiedpush.distributor.nextpush.AppCompanion import org.unifiedpush.distributor.nextpush.AppStore import org.unifiedpush.distributor.nextpush.LastEventId import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount @@ -99,6 +100,10 @@ class Api(context: Context) { LastEventId(store).get()?.let { header("Last-Event-ID", it) } + if (AppCompanion.lowBattery.get()) { + Log.d(TAG, "Battery is low, registering normal and high only") + header("Urgency", "normal") + } } .build() diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/callback/BatteryCallback.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/callback/BatteryCallback.kt new file mode 100644 index 0000000..7871f0b --- /dev/null +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/callback/BatteryCallback.kt @@ -0,0 +1,68 @@ +package org.unifiedpush.distributor.nextpush.callback + +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 org.unifiedpush.distributor.nextpush.AppCompanion +import org.unifiedpush.distributor.nextpush.services.FailureHandler +import org.unifiedpush.distributor.nextpush.services.RestartWorker +import org.unifiedpush.distributor.nextpush.services.StartService +import org.unifiedpush.distributor.nextpush.utils.TAG + +class BatteryCallback : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + when (intent.action) { + Intent.ACTION_BATTERY_LOW -> onBatteryLow(context) + Intent.ACTION_BATTERY_OKAY -> onBatteryOk(context) + } + } + + private fun onBatteryLow(context: Context) { + // If it was OK + if (!AppCompanion.lowBattery.getAndSet(true)) { + Log.d(TAG, "The battery is now low.") + restartServer(context) + } + } + + private fun onBatteryOk(context: Context) { + // If it was low + if (AppCompanion.lowBattery.getAndSet(false)) { + Log.d(TAG, "The battery is now OK.") + restartServer(context) + } + } + + private fun restartServer(context: Context) { + if (!FailureHandler.hasFailed()) { + StartService.stopService { + RestartWorker.run(context, delay = 0) + } + } + } + + companion object { + /** + * Register an instance of [BatteryCallback] and returns it + * + * Initiates value of AppCompanion.lowBattery + */ + fun register(context: Context): BatteryCallback { + // First we initiate the value + val bm = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager + val level = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) + AppCompanion.lowBattery.set(level <= 20) + // Then we register the receiver + val filter = IntentFilter().apply { + addAction(Intent.ACTION_BATTERY_LOW) + addAction(Intent.ACTION_BATTERY_OKAY) + } + return BatteryCallback().also { + context.registerReceiver(it, filter) + } + } + } +} diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt index 1411ea1..340cfad 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt @@ -11,6 +11,7 @@ import org.unifiedpush.distributor.nextpush.AppCompanion import org.unifiedpush.distributor.nextpush.WakeLock import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount import org.unifiedpush.distributor.nextpush.api.Api +import org.unifiedpush.distributor.nextpush.callback.BatteryCallback import org.unifiedpush.distributor.nextpush.callback.NetworkCallback import org.unifiedpush.distributor.nextpush.utils.ForegroundNotification import org.unifiedpush.distributor.nextpush.utils.NOTIFICATION_ID_FOREGROUND @@ -19,6 +20,7 @@ import org.unifiedpush.distributor.nextpush.utils.TAG class StartService : Service() { private val networkCallback = NetworkCallback(this) + private var batteryCallback: BatteryCallback? = null private var api: Api? = null override fun onBind(intent: Intent?): IBinder? { @@ -38,6 +40,7 @@ class StartService : Service() { Log.d(TAG, "onStartCommand: regular start") // That's a normal start networkCallback.register() + batteryCallback = BatteryCallback.register(this) startService() } else { Log.d(TAG, "onStartCommand: to destroy") @@ -62,6 +65,8 @@ class StartService : Service() { RestartWorker.run(this, delay = 0) } else { networkCallback.unregister() + batteryCallback?.let { unregisterReceiver(it) } + batteryCallback = null RestartWorker.stopPeriodic(this) } }