From e1991f97498565030fa5431f34ebff27db54c6be Mon Sep 17 00:00:00 2001
From: Torsten Grote <t@grobox.de>
Date: Wed, 20 Apr 2022 09:30:31 -0300
Subject: [PATCH] Backport Huawei power management fix

---
 build.gradle                                  |  2 +-
 gradle.properties                             |  2 +-
 .../android/dontkillmelib/HuaweiUtils.kt      | 24 +++++++++++++++++--
 lib/src/main/res/values/strings.xml           |  3 +++
 4 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/build.gradle b/build.gradle
index ee659d3..785ae44 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,5 +1,5 @@
 buildscript {
-    ext.kotlin_version = '1.6.10'
+    ext.kotlin_version = '1.6.20'
     repositories {
         mavenCentral()
         google()
diff --git a/gradle.properties b/gradle.properties
index 972becf..5d34c3a 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -1,6 +1,6 @@
 GROUP=org.briarproject
 POM_ARTIFACT_ID=dont-kill-me-lib
-VERSION_NAME=0.1
+VERSION_NAME=0.2
 
 POM_NAME=Do not kill me library
 POM_DESCRIPTION=An Android library helping to keep a foreground service with wake-locks running. No other use-cases considered.
diff --git a/lib/src/main/java/org/briarproject/android/dontkillmelib/HuaweiUtils.kt b/lib/src/main/java/org/briarproject/android/dontkillmelib/HuaweiUtils.kt
index 1f89167..979f4f8 100644
--- a/lib/src/main/java/org/briarproject/android/dontkillmelib/HuaweiUtils.kt
+++ b/lib/src/main/java/org/briarproject/android/dontkillmelib/HuaweiUtils.kt
@@ -8,6 +8,8 @@ import android.os.Build.VERSION.SDK_INT
 public object HuaweiUtils {
 
     private const val PACKAGE_NAME_HUAWEI = "com.huawei.systemmanager"
+    private const val CLASS_NAME_APP_LIST =
+        "$PACKAGE_NAME_HUAWEI.startupmgr.ui.StartupNormalAppListActivity"
     private const val CLASS_NAME_POWER_MANAGER =
         "$PACKAGE_NAME_HUAWEI.power.ui.HwPowerManagerActivity"
     private const val CLASS_NAME_PROTECTED_APPS =
@@ -22,8 +24,10 @@ public object HuaweiUtils {
         // "App launch" was introduced in EMUI 8 (Android 8.0)
         if (SDK_INT < 26) return false
         val pm = context.packageManager
-        val resolveInfos = pm.queryIntentActivities(huaweiProtectedAppsIntent, MATCH_DEFAULT_ONLY)
-        return resolveInfos.isNotEmpty()
+        for (i in huaweiAppLaunchIntents) {
+            if (pm.queryIntentActivities(i, MATCH_DEFAULT_ONLY).isNotEmpty()) return true
+        }
+        return false
     }
 
     /**
@@ -39,11 +43,27 @@ public object HuaweiUtils {
         return resolveInfos.isNotEmpty()
     }
 
+    @JvmStatic
+    public val huaweiAppListIntent: Intent = Intent().apply {
+        setClassName(PACKAGE_NAME_HUAWEI, CLASS_NAME_APP_LIST)
+    }
+
     @JvmStatic
     public val huaweiPowerManagerIntent: Intent = Intent().apply {
         setClassName(PACKAGE_NAME_HUAWEI, CLASS_NAME_POWER_MANAGER)
     }
 
+    /**
+     * First tries [huaweiAppListIntent] and then falls back to [huaweiPowerManagerIntent].
+     */
+    @JvmStatic
+    public val huaweiAppLaunchIntents: List<Intent> = listOf(
+        // First try to open StartupNormalAppListActivity
+        huaweiAppListIntent,
+        // Fall back to HwPowerManagerActivity
+        huaweiPowerManagerIntent,
+    )
+
     @JvmStatic
     public val huaweiProtectedAppsIntent: Intent = Intent().apply {
         setClassName(PACKAGE_NAME_HUAWEI, CLASS_NAME_PROTECTED_APPS)
diff --git a/lib/src/main/res/values/strings.xml b/lib/src/main/res/values/strings.xml
index 62db8f4..ce4fe3f 100644
--- a/lib/src/main/res/values/strings.xml
+++ b/lib/src/main/res/values/strings.xml
@@ -12,6 +12,7 @@
     <string name="dnkm_huawei_app_launch_text">Please tap the button below, open the \"App launch\" screen and make sure this app is set to \"Manage manually\".</string>
     <string name="dnkm_huawei_app_launch_button">Open Battery Settings</string>
     <string name="dnkm_huawei_app_launch_help">If this app is not set to \"Manage manually\" in the \"App launch\" screen, it will not be able to run in the background.</string>
+    <string name="dnkm_huawei_app_launch_error_toast">Could not open battery settings</string>
 
     <string name="dnkm_xiaomi_text">To run in the background, this app needs to be locked to the recent apps list.</string>
     <string name="dnkm_xiaomi_button">Lock to recent apps</string>
@@ -21,4 +22,6 @@
 
     <string name="dnkm_got_it">Got it</string>
     <string name="dnkm_help">Help</string>
+
+    <string name="dnkm_warning_dozed">%s was unable to run in the background</string>
 </resources>
-- 
GitLab