Sync only high and normal messages with low battery
This commit is contained in:
parent
aeb5a776ce
commit
08cd3d2f88
|
@ -18,6 +18,11 @@ object AppCompanion {
|
||||||
*/
|
*/
|
||||||
val hasInternet = AtomicBoolean(true)
|
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 */
|
/** Have we received the start event ? To check the reverse proxy timeout is high enough */
|
||||||
val started = AtomicBoolean(false)
|
val started = AtomicBoolean(false)
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.sse.EventSource
|
import okhttp3.sse.EventSource
|
||||||
import okhttp3.sse.EventSources
|
import okhttp3.sse.EventSources
|
||||||
|
import org.unifiedpush.distributor.nextpush.AppCompanion
|
||||||
import org.unifiedpush.distributor.nextpush.AppStore
|
import org.unifiedpush.distributor.nextpush.AppStore
|
||||||
import org.unifiedpush.distributor.nextpush.LastEventId
|
import org.unifiedpush.distributor.nextpush.LastEventId
|
||||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
|
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
|
||||||
|
@ -99,6 +100,10 @@ class Api(context: Context) {
|
||||||
LastEventId(store).get()?.let {
|
LastEventId(store).get()?.let {
|
||||||
header("Last-Event-ID", it)
|
header("Last-Event-ID", it)
|
||||||
}
|
}
|
||||||
|
if (AppCompanion.lowBattery.get()) {
|
||||||
|
Log.d(TAG, "Battery is low, registering normal and high only")
|
||||||
|
header("Urgency", "normal")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ import org.unifiedpush.distributor.nextpush.AppCompanion
|
||||||
import org.unifiedpush.distributor.nextpush.WakeLock
|
import org.unifiedpush.distributor.nextpush.WakeLock
|
||||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
|
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
|
||||||
import org.unifiedpush.distributor.nextpush.api.Api
|
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.callback.NetworkCallback
|
||||||
import org.unifiedpush.distributor.nextpush.utils.ForegroundNotification
|
import org.unifiedpush.distributor.nextpush.utils.ForegroundNotification
|
||||||
import org.unifiedpush.distributor.nextpush.utils.NOTIFICATION_ID_FOREGROUND
|
import org.unifiedpush.distributor.nextpush.utils.NOTIFICATION_ID_FOREGROUND
|
||||||
|
@ -19,6 +20,7 @@ import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||||
class StartService : Service() {
|
class StartService : Service() {
|
||||||
|
|
||||||
private val networkCallback = NetworkCallback(this)
|
private val networkCallback = NetworkCallback(this)
|
||||||
|
private var batteryCallback: BatteryCallback? = null
|
||||||
private var api: Api? = null
|
private var api: Api? = null
|
||||||
|
|
||||||
override fun onBind(intent: Intent?): IBinder? {
|
override fun onBind(intent: Intent?): IBinder? {
|
||||||
|
@ -38,6 +40,7 @@ class StartService : Service() {
|
||||||
Log.d(TAG, "onStartCommand: regular start")
|
Log.d(TAG, "onStartCommand: regular start")
|
||||||
// That's a normal start
|
// That's a normal start
|
||||||
networkCallback.register()
|
networkCallback.register()
|
||||||
|
batteryCallback = BatteryCallback.register(this)
|
||||||
startService()
|
startService()
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "onStartCommand: to destroy")
|
Log.d(TAG, "onStartCommand: to destroy")
|
||||||
|
@ -62,6 +65,8 @@ class StartService : Service() {
|
||||||
RestartWorker.run(this, delay = 0)
|
RestartWorker.run(this, delay = 0)
|
||||||
} else {
|
} else {
|
||||||
networkCallback.unregister()
|
networkCallback.unregister()
|
||||||
|
batteryCallback?.let { unregisterReceiver(it) }
|
||||||
|
batteryCallback = null
|
||||||
RestartWorker.stopPeriodic(this)
|
RestartWorker.stopPeriodic(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue