Browse Source

feat: Acquire partial wake lock for all foreground tasks

All of our foreground tasks require the CPU to be at least awake to make
any progress. We could keep the screen on but we really only need the
partial wake lock to make sure progress is made.
Peter Cai 10 months ago
parent
commit
a6286ed097

+ 1 - 0
app-common/src/main/AndroidManifest.xml

@@ -7,6 +7,7 @@
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
+    <uses-permission android:name="android.permission.WAKE_LOCK" />
 
 
     <application
     <application
         android:enableOnBackInvokedCallback="true"
         android:enableOnBackInvokedCallback="true"

+ 10 - 0
app-common/src/main/java/im/angry/openeuicc/service/EuiccChannelManagerService.kt

@@ -4,6 +4,7 @@ import android.content.Intent
 import android.content.pm.PackageManager
 import android.content.pm.PackageManager
 import android.os.Binder
 import android.os.Binder
 import android.os.IBinder
 import android.os.IBinder
+import android.os.PowerManager
 import android.util.Log
 import android.util.Log
 import androidx.core.app.NotificationChannelCompat
 import androidx.core.app.NotificationChannelCompat
 import androidx.core.app.NotificationCompat
 import androidx.core.app.NotificationCompat
@@ -91,6 +92,12 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
     }
     }
     val euiccChannelManager: EuiccChannelManager by euiccChannelManagerDelegate
     val euiccChannelManager: EuiccChannelManager by euiccChannelManagerDelegate
 
 
+    val wakeLock: PowerManager.WakeLock by lazy {
+        (getSystemService(POWER_SERVICE) as PowerManager).run {
+            newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this::class.simpleName)
+        }
+    }
+
     /**
     /**
      * The state of a "foreground" task (named so due to the need to startForeground())
      * The state of a "foreground" task (named so due to the need to startForeground())
      */
      */
@@ -275,6 +282,8 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
 
 
             updateForegroundNotification(title, iconRes)
             updateForegroundNotification(title, iconRes)
 
 
+            wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
+
             try {
             try {
                 withContext(Dispatchers.IO + NonCancellable) { // Any LPA-related task must always complete
                 withContext(Dispatchers.IO + NonCancellable) { // Any LPA-related task must always complete
                     this@EuiccChannelManagerService.task()
                     this@EuiccChannelManagerService.task()
@@ -290,6 +299,7 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
                     postForegroundTaskFailureNotification(failureTitle)
                     postForegroundTaskFailureNotification(failureTitle)
                 }
                 }
             } finally {
             } finally {
+                wakeLock.release()
                 if (isActive) {
                 if (isActive) {
                     stopSelf()
                     stopSelf()
                 }
                 }