From f42589dd9cbc25113aa32bccf09642728dc9a8fa Mon Sep 17 00:00:00 2001 From: sim Date: Fri, 14 Jan 2022 01:16:19 +0100 Subject: [PATCH] Improve Wake Lock --- .../nextpush/services/SSEListener.kt | 16 ++++++++++-- .../nextpush/services/StartService.kt | 25 +++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt index 65e8898..2556f40 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt @@ -22,6 +22,11 @@ class SSEListener (val context: Context) : EventSourceListener() { override fun onOpen(eventSource: EventSource, response: Response) { deleteWarningNotification(context) nFails = 0 + wakeLock?.let { + while (it.isHeld) { + it.release() + } + } try { Log.d(TAG, "onOpen: " + response.code) } catch (e: Exception) { @@ -31,6 +36,9 @@ class SSEListener (val context: Context) : EventSourceListener() { override fun onEvent(eventSource: EventSource, id: String?, type: String?, data: String) { Log.d(TAG, "New SSE message event=$type message=$data") + wakeLock?.let { + it.acquire() + } when (type) { "warning" -> Log.d(TAG, "Warning event received.") "ping" -> Log.d(TAG, "SSE ping received.") @@ -53,18 +61,22 @@ class SSEListener (val context: Context) : EventSourceListener() { db.unregisterApp(connectorToken) } } + wakeLock?.let { + if (it.isHeld) { + it.release() + } + } } override fun onClosed(eventSource: EventSource) { Log.d(TAG, "onClosed: $eventSource") - isServiceStarted = false + nFails += 1 createWarningNotification(context) startListener(context) } override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) { Log.d(TAG, "onFailure") - isServiceStarted = false nFails += 1 if (nFails > 1) createWarningNotification(context) 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 1a96542..dabe9c5 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 @@ -24,8 +24,10 @@ import org.unifiedpush.distributor.nextpush.api.apiSync import java.lang.Exception private const val TAG = "StartService" +const val WAKE_LOCK_TAG = "NextPush:StartService:lock" var isServiceStarted = false var nFails = 0 +var wakeLock: PowerManager.WakeLock? = null fun startListener(context: Context){ Log.d(TAG, "Starting the Listener") @@ -39,7 +41,6 @@ fun startListener(context: Context){ class StartService: Service(){ - private var wakeLock: PowerManager.WakeLock? = null private var isCallbackRegistered = false override fun onBind(intent: Intent?): IBinder? { @@ -65,12 +66,26 @@ class StartService: Service(){ override fun onDestroy() { Log.d(TAG, "Destroyed") + if (isServiceStarted) { + startListener(this) + } else { + stopService() + } + } + + private fun stopService() { + Log.d(TAG, "Stopping Service") apiDestroy() - super.onDestroy() + wakeLock?.let { + while (it.isHeld) { + it.release() + } + } + stopSelf() } private fun startService() { - if (isServiceStarted) return + if (isServiceStarted && nFails == 0) return isServiceStarted = true try { @@ -83,7 +98,7 @@ class StartService: Service(){ // we need this lock so our service gets not affected by Doze Mode wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).run { - newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EndlessService::lock").apply { + newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG).apply { acquire() } } @@ -98,7 +113,7 @@ class StartService: Service(){ connectivityManager.registerDefaultNetworkCallback(object : NetworkCallback() { override fun onAvailable(network: Network) { Log.d(TAG, "Network is CONNECTED") - startService() + startListener(this@StartService) } override fun onLost(network: Network) {