Andee lives on your phone or Mac and uses any app like you do—no integrations, no root, any model; memory and passwords never leave your device | Andee 住进你的手机或 Mac,像你一样操作任何 App——无需集成、无需 Root、支持任意模型,记忆和密码绝不离开设备。
feat(self-check): surface the MIUI auto-start switch
booboosuic93df4f2689d
diff --git a/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt b/app/src/main/java/net/kuafuai/andee/device/SelfCheck.ktindex 4c2ba6b..a1f6d32 100644--- a/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt+++ b/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt@@ -2,6 +2,7 @@ package net.kuafuai.andee.deviceimport android.Manifestimport android.accessibilityservice.AccessibilityServiceInfo+import android.app.AppOpsManagerimport android.content.ComponentNameimport android.content.Contextimport android.content.Intent@@ -148,6 +149,18 @@ object SelfCheck {* because the same action is the right door in both states.*/@StringRes val label: Int = 0,+ /**+ * An explicit component, as `"package/package.Class"`, for a screen+ * that has no action — a vendor's own settings page. When set it+ * takes precedence over [action]: the intent targets the component+ * directly, and [action] is kept only as a harmless label.+ *+ * Exists for one row: MIUI/HyperOS's auto-start manager, which is a+ * plain activity inside `com.miui.securitycenter` and reachable no+ * other way. `action` must still be non-null, so callers pass+ * `Intent.ACTION_MAIN` as the placeholder.+ */+ val component: String? = null,) : Fix/**@@ -247,6 +260,7 @@ object SelfCheck {voiceReach(context, probe, online),wakePhrase(context),background(context),+ autoStart(context),storage(context),)}@@ -1030,6 +1044,120 @@ object SelfCheck {}}+ /**+ * Whether the vendor will let this app come back after it is killed.+ *+ * MIUI/HyperOS keeps its own "auto-start" switch, separate from the AOSP+ * battery-optimisation whitelist [background] reads. That switch is the+ * gate on `AutoPowerKill`: Xiaomi's power policy reaps background processes+ * on a timer — measured at roughly one kill every 40–60 minutes, even as a+ * foreground service with a notification and even while on the AOSP+ * whitelist — and after a kill, `START_STICKY` cannot bring it back unless+ * auto-start is granted. The user's symptom for all of that is one+ * sentence: "it closes by itself after a while".+ *+ * The switch lives in AppOps as a vendor op, `OP_AUTO_START = 10008`, and+ * is read through the `checkOpNoThrow(int, int, String)` overload, which is+ * `@hide` and therefore reached by reflection. `MODE_ALLOWED` means on;+ * anything else means the process cannot self-revive.+ *+ * Only ever asked on a Xiaomi-family device — see [isXiaomi] — because on+ * every other ROM the op does not exist and a reading of it is noise. The+ * row is therefore absent rather than "not applicable": `run` builds the+ * list with `listOfNotNull`, and a null here is the same shape as the+ * API-gated [postNotifications] row.+ *+ * [Level.WARN], not FAIL: with auto-start off the app is fully alive *right+ * now* — it is the future that degrades, which is exactly the WARN contract.+ */+ private fun autoStart(context: Context): Finding? {+ if (!isXiaomi()) return null+ val on = autoStartAllowed(context)+ return Finding(+ id = "auto_start",+ title = R.string.check_title_auto_start,+ level = if (on) Level.OK else Level.WARN,+ detail = if (on) R.string.check_detail_auto_start_on+ else R.string.check_detail_auto_start_off,+ fix = if (on) Fix.Nothing else autoStartFix(context),+ )+ }++ /**+ * A Xiaomi-family ROM, the only place [autoStart] means anything.+ *+ * Matched on brand words rather than on the MIUI build property because+ * the property is read through a hidden API and the words are free.+ * Xiaomi covers Redmi and POCO, whose devices ship the same power policy+ * and the same AppOps op.+ */+ private fun isXiaomi(): Boolean {+ val words = "${Build.MANUFACTURER} ${Build.BRAND}".lowercase()+ return words.contains("xiaomi") || words.contains("redmi") || words.contains("poco")+ }++ /**+ * The live read of `OP_AUTO_START`.+ *+ * Reflection rather than the public `checkOpNoThrow(String, …)` because the+ * op has no name this app can spell: it is a vendor extension with only a+ * numeric code. The hidden `checkOpNoThrow(int, int, String)` overload is+ * what the platform itself uses for unnamed ops, and it is present across+ * the API levels this app ships to. Any failure answers false — "not+ * allowed" — which is the safe reading for a row whose whole job is to+ * catch a missing grant.+ */+ private fun autoStartAllowed(context: Context): Boolean {+ val appOps = context.getSystemService(Context.APP_OPS_SERVICE) as? AppOpsManager+ ?: return false+ return runCatching {+ val m = AppOpsManager::class.java.getMethod(+ "checkOpNoThrow",+ Int::class.javaPrimitiveType,+ Int::class.javaPrimitiveType,+ String::class.java,+ )+ val mode = m.invoke(+ appOps,+ MIUI_OP_AUTO_START,+ context.applicationInfo.uid,+ context.packageName,+ ) as Int+ mode == AppOpsManager.MODE_ALLOWED+ }.getOrDefault(false)+ }++ /**+ * The door to the auto-start switch, by the component that resolves first.+ *+ * The manager is a plain activity in `com.miui.securitycenter`, reachable+ * no other way, and MIUI has renamed the activity across versions — so the+ * candidate list is probed like [listenerSettings] and falls back to the+ * app's own details page, which on HyperOS is where the switch lives when+ * the dedicated manager is absent. An action is still required by+ * [Fix.Screen]; `ACTION_MAIN` is the placeholder, and [component] wins.+ */+ private fun autoStartFix(context: Context): Fix {+ val candidates = listOf(+ "com.miui.securitycenter/com.miui.permcenter.autostart.AutoStartManagementActivity",+ "com.miui.securitycenter/com.miui.permcenter.selfmanagement.AutoStartManagementActivity",+ )+ val pm = context.packageManager+ val component = candidates.firstOrNull { c ->+ val cn = ComponentName.unflattenFromString(c) ?: return@firstOrNull false+ // getActivityInfo throws NameNotFoundException when the activity is+ // absent, so success *is* the answer — there is no null to test.+ runCatching { pm.getActivityInfo(cn, 0) }.isSuccess+ }+ if (component != null) {+ return Fix.Screen(Intent.ACTION_MAIN, component = component)+ }+ return Fix.Screen(+ Settings.ACTION_APPLICATION_DETAILS_SETTINGS,+ "package:${context.packageName}",+ )+ }+/*** Room to write.*@@ -1371,4 +1499,11 @@ object SelfCheck {private const val MODEL_SAMPLE = 8private const val DISK_WARN_MB = 500Lprivate const val DISK_FAIL_MB = 100L++ /**+ * MIUI/HyperOS's `OP_AUTO_START` AppOps code — the vendor op behind the+ * "auto-start" switch. See [autoStart] for why it has a number but no+ * name, and why it is read through reflection.+ */+ private const val MIUI_OP_AUTO_START = 10008}diff --git a/app/src/main/java/net/kuafuai/andee/ui/SelfCheckUi.kt b/app/src/main/java/net/kuafuai/andee/ui/SelfCheckUi.ktindex 021299f..459eee1 100644--- a/app/src/main/java/net/kuafuai/andee/ui/SelfCheckUi.kt+++ b/app/src/main/java/net/kuafuai/andee/ui/SelfCheckUi.kt@@ -1,5 +1,6 @@package net.kuafuai.andee.ui+import android.content.ComponentNameimport android.content.Contextimport android.content.Intentimport android.graphics.Color@@ -928,6 +929,7 @@ class SelfCheckUi(*/private fun intentFor(fix: SelfCheck.Fix): Intent = when (fix) {is SelfCheck.Fix.Screen -> Intent(fix.action).apply {+ fix.component?.let { component = ComponentName.unflattenFromString(it) }fix.data?.let { data = Uri.parse(it) }fix.extras.forEach { (name, value) -> putExtra(name, value) }}diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xmlindex 515b701..034a009 100644--- a/app/src/main/res/values-en/strings.xml+++ b/app/src/main/res/values-en/strings.xml@@ -469,6 +469,7 @@<string name="check_title_voice_host">Voice service</string><string name="check_title_wake">Wake phrase</string><string name="check_title_battery">Background running</string>+ <string name="check_title_auto_start">Auto-start</string><string name="check_title_disk">Storage</string><string name="check_detail_accessibility_on">On. The ball and every screen action are running.</string>@@ -523,6 +524,8 @@<string name="check_detail_wake_off">Not taught yet. Pick a phrase (e.g. “Hey Andee”) and say it 3 times to wake me by voice. Everything still works without it, you just tap the ball to talk.</string><string name="check_detail_battery_on">Exempt, so pending reminders are not delayed.</string><string name="check_detail_battery_off">Not exempt from battery optimisation. Tap “Open settings” — the system asks “Ignore battery optimisations?”; take the button that ignores them.\nThe vendor’s own “App launch / background running” controls are a separate system; changing them leaves this row red. With the screen off a pending reminder may arrive late, and the wake phrase stops listening.</string>+ <string name="check_detail_auto_start_on">Allowed. The process can bring itself back after the system kills it.</string>+ <string name="check_detail_auto_start_off">Off. Xiaomi’s power policy reaps background processes on a timer, and without auto-start the assistant cannot come back after that — it looks like it just closes by itself. Tap to enable.</string><string name="check_detail_disk_ok">%1$d MB free.</string><string name="check_detail_disk_low">Only %1$d MB left; screenshots and pages will start going missing.</string><string name="check_detail_disk_full">Only %1$d MB left — almost nothing can be written.</string>diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xmlindex 88b4c35..d4cd56c 100644--- a/app/src/main/res/values/strings.xml+++ b/app/src/main/res/values/strings.xml@@ -554,6 +554,7 @@<string name="check_title_voice_host">语音服务可达</string><string name="check_title_wake">唤醒词</string><string name="check_title_battery">后台运行</string>+ <string name="check_title_auto_start">自启动</string><string name="check_title_disk">存储空间</string><string name="check_detail_accessibility_on">已开启,球和屏幕操作都在跑。</string>@@ -607,6 +608,8 @@<string name="check_detail_wake_off">还没录。自己选个词(例如「嘿 Andee」)录 3 遍就能喊醒。不录也能用,只是得点球。</string><string name="check_detail_battery_on">已在白名单,待办闹钟不会被推迟。</string><string name="check_detail_battery_off">不在电池优化白名单里。点右边「去开启」,系统会问「是否忽略电池优化?」,选「忽略」就好。\n厂商自己的「应用启动管理 / 后台运行」是另一套,在那里怎么改这项都是红的。息屏后待办提醒会晚到,唤醒词也会停。</string>+ <string name="check_detail_auto_start_on">已允许。进程被系统杀掉后能自己拉回来。</string>+ <string name="check_detail_auto_start_off">没开。小米的省电策略会定时杀掉后台进程,没开自启动就拉不回来,表现就是「用一会儿自己关掉」。点右边去开启。</string><string name="check_detail_disk_ok">剩余 %1$d MB。</string><string name="check_detail_disk_low">只剩 %1$d MB,再占下去会开始丢截图和页面。</string><string name="check_detail_disk_full">只剩 %1$d MB,基本写不进东西了。</string>