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、支持任意模型,记忆和密码绝不离开设备。
fix(permissions): report what the sheet actually did, and say who answers it
booboosuie7fbc594b231
diff --git a/app/src/main/java/net/kuafuai/andee/brain/LocalPrompt.kt b/app/src/main/java/net/kuafuai/andee/brain/LocalPrompt.ktindex c202f1e..f74fc6e 100644--- a/app/src/main/java/net/kuafuai/andee/brain/LocalPrompt.kt+++ b/app/src/main/java/net/kuafuai/andee/brain/LocalPrompt.kt@@ -111,7 +111,7 @@ Things to keep in mind:- When you do have to aim by eye, use the grid: every screenshot carries one, labelled A1 (top-left) to H12 (bottom-right), each label printed at the centre of its cell. Name the cell the target sits IN (not the label nearest to it) and which third of that cell it is in — {"cell": "D7", "part": "bottom-right"}; part is one of top-left, top, top-right, left, center, right, bottom-left, bottom, bottom-right. A cell alone is too coarse to tap with, so give the part every time unless the target fills the cell. For anything not clearly bigger than a cell — an icon, a ✕ close button, a small chip — check before you fire: call zoom_screen_region with the SAME cell and part; it returns a magnified view with a red crosshair exactly where that tap would land. Crosshair on the target → tap with those same arguments. Not on it → aim off that magnified image instead (its labels are lowercase; send them with on="zoom"). A wrong tap can open something you did not want; one look costs one step. Read the cell, do not measure pixels and do not rescale: the arithmetic you would otherwise do is where the misses come from.- For anything in a browser, prefer the Chrome on this device.- If something needs an account, always try Google sign-in first. If that is unsupported, register by email, taking the address from the vault (list_vault); if there is none, ask the user for help.-- type_text's return value is not to be trusted: what counts is that the field really holds the text. After every input, confirm with the screen reader or a screenshot before moving on; if it does not match, treat it as a failure (retry at most once).+- **A tool saying "I pressed it" is not the same as the thing being done.** Two of yours report only that much: type_text counts only if the field really holds the text, and request_permissions counts only if its `result` says granted — a request that was fired and a permission that is actually on are two different facts, and only the second one lets you carry on. After either of them, look before you carry on: confirm with the screen reader or a screenshot; if it does not match, treat it as a failure (retry at most once).- Do not retry forever: the same goal failing twice in a row means change route or report honestly. Never loop the same sentence or the same action. Do not use adb, shell, terminal commands, or developer tools.- Wait on long tasks (cold start, upload, transcode) by **escalating**: wait_for_screen's seconds must grow each time, 3→5→10→20, and **never send the same number twice in a row**. Two consecutive calls with identical arguments are cut off by the dead-loop guard, and what gets cut off is the whole task, not just that one wait. After each wait, read what came back: if the screen changed, move on; if it is still identical at 20 seconds it is not loading, it is stuck — say so, or change route.@@ -130,6 +130,12 @@ So when a request arrives, ask "**how many steps is this on the interface, and w**"It takes several steps" is not "I can't do it" — it just takes several steps.**+**And the system's permission sheet is one of them — this one surprises people, so read it twice.** It is *not* an ordinary screen: it exposes no elements at all, and it does not take a tap you inject. Measured on this device, four separate ways: `get_screen_element` comes back empty with a picture and no e-numbers, a tap aimed at 「允许」 does nothing, the same tap injected another way does nothing, and even the back key leaves it up. **The only hands it answers to are the user's.** So when `request_permissions` leaves a sheet on screen, that is the one step of the errand that is theirs — your job is to put it in front of them and say so, in one line, and then wait or carry on with what does not need it. Do not tap at it, do not fire it twice, and do not conclude from \"it did not react\" that the screen is broken — **it is reacting correctly to a tap it is built to ignore.**++How much ceremony that costs you is what the `mode` decides, and **the mode is the device's decision, not your own judgement about how sensitive the grant looks**. It follows what the grant reaches: your own data is `solo` — fire it, say one line, done asking. Other people's data, and things that cannot be taken back, are `handoff` — ask with ask_user first and say what you are waiting for. `settings` means there is no sheet at all and the errand is a switch inside Settings — that one is an ordinary screen and you can walk it yourself.++The one way to get this wrong is to answer a sheet that is not the system's. The system's belongs to `com.android.permissioncontroller`; a dialog drawn by some other app is not a permission request and is not yours to tap.+Only these genuinely cannot be done — and none of them is "the whole thing cannot be done". Each one means "**one step of it needs their hand**":- **The steps where the screen goes black**: payments, passwords, biometrics — security policy blocks you. Ask them to tap that one step, **and you carry on with the rest**.diff --git a/app/src/main/java/net/kuafuai/andee/device/PermissionRequestActivity.kt b/app/src/main/java/net/kuafuai/andee/device/PermissionRequestActivity.ktindex d71b577..efdf60b 100644--- a/app/src/main/java/net/kuafuai/andee/device/PermissionRequestActivity.kt+++ b/app/src/main/java/net/kuafuai/andee/device/PermissionRequestActivity.kt@@ -1,14 +1,31 @@package net.kuafuai.andee.deviceimport androidx.appcompat.app.AppCompatActivity+import android.content.pm.PackageManagerimport android.os.Bundle+import java.util.concurrent.CountDownLatch+import java.util.concurrent.TimeUnit/*** Translucent trampoline that hosts the runtime-permission dialogs. The app* is service-first (no main activity), but requestPermissions() needs a* live Activity window. Shows nothing, asks, dies.+ *+ * It is also the only place the answer ever exists. The verdict arrives in+ * [onRequestPermissionsResult], and this activity finishes the moment it has+ * it — there is no second chance to ask a dialog that has already gone. So+ * the verdict is handed back to whoever fired the request through [await],+ * rather than being dropped on the floor as it used to be.+ *+ * 🔴 The distinction that pays for all of this is "was a sheet drawn at all".+ * A refusal and a silent block both give [PackageManager.PERMISSION_DENIED],+ * and they demand opposite responses: one is worth explaining, the other is+ * pointless to repeat. The signal that separates them is+ * [shouldShowRequestPermissionRationale], which is an Activity method — so+ * this is the one place that can read it.*/class PermissionRequestActivity : AppCompatActivity() {+override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val perms = intent.getStringArrayExtra("permissions") ?: run { finish(); return }@@ -19,6 +36,60 @@ class PermissionRequestActivity : AppCompatActivity() {requestCode: Int, permissions: Array<out String>, grantResults: IntArray,) {super.onRequestPermissionsResult(requestCode, permissions, grantResults)+ settle(verdict(permissions, grantResults))finish()}++ private fun verdict(permissions: Array<out String>, grantResults: IntArray): String {+ if (grantResults.isNotEmpty() &&+ grantResults.all { it == PackageManager.PERMISSION_GRANTED }+ ) return GRANTED++ // Only the refused ones are asked about: a granted permission has no+ // rationale either, and counting it would call every partial grant a+ // silent block.+ val silent = permissions.indices.any { i ->+ grantResults.getOrNull(i) != PackageManager.PERMISSION_GRANTED &&+ !shouldShowRequestPermissionRationale(permissions[i])+ }+ return if (silent) NO_DIALOG else DENIED+ }++ companion object {+ /**+ * The words the brain branches on. Same four [PermissionsController]+ * reports, because they travel together.+ */+ internal const val GRANTED = "granted"+ internal const val DENIED = "denied"+ internal const val NO_DIALOG = "no_dialog"++ @Volatile private var latch: CountDownLatch? = null+ @Volatile private var outcome: String? = null++ /**+ * Clear the previous answer and open a fresh waiting point. Call this+ * BEFORE startActivity: the dialog can decide inside a millisecond,+ * and an answer that lands before the latch exists is lost.+ */+ internal fun arm() {+ outcome = null+ latch = CountDownLatch(1)+ }++ /**+ * The verdict, or null if none came within [ms] — meaning the dialog+ * is still up waiting for a human. Null is a fact, not a failure: it+ * is what tells the caller nobody has answered yet.+ */+ internal fun await(ms: Long): String? {+ latch?.await(ms, TimeUnit.MILLISECONDS)+ return outcome+ }++ private fun settle(verdict: String) {+ outcome = verdict+ latch?.countDown()+ }+ }}diff --git a/app/src/main/java/net/kuafuai/andee/device/PermissionsController.kt b/app/src/main/java/net/kuafuai/andee/device/PermissionsController.ktindex ad6335a..33a7f72 100644--- a/app/src/main/java/net/kuafuai/andee/device/PermissionsController.kt+++ b/app/src/main/java/net/kuafuai/andee/device/PermissionsController.kt@@ -2,6 +2,7 @@ package net.kuafuai.andee.deviceimport android.content.Contextimport android.content.Intent+import android.content.pm.PackageManagerimport android.net.Uriimport android.provider.Settingsimport androidx.annotation.StringRes@@ -90,6 +91,18 @@ class PermissionsController(private val context: Context) {* to fire the intent. Runtime permissions use the classic dialog* (needs an activity — we hand the request to the floating settings* host); special ones deep-link into Settings.+ *+ * confirm=true answers with what actually happened. It used to answer+ * `{"started": true}` — a hard-coded true meaning "startActivity did not+ * throw" — and the brain read that as "it is on" and carried on against a+ * permission it did not have. Those are two different facts and only one+ * of them is worth acting on, so the reply now carries:+ *+ * mode solo | handoff | settings — WHO answers the dialog, decided+ * here rather than by the brain's own judgement, because a+ * request that mixes them is answered as a whole+ * result granted | denied | no_dialog | waiting+ * next one line saying what to do about that result*/fun request(ids: JSONArray, confirm: Boolean): JSONObject {// The `say` sentence is shown/spoken to the user, so it is built in@@ -119,16 +132,25 @@ class PermissionsController(private val context: Context) {return JSONObject().put("say", say).put("confirm_needed", true).put("ids", ids)}+ val mode = routeOf(wanted.map { it.id })++ // Drop what is already on. Re-asking a granted permission draws no+ // dialog and answers nothing, so the brain would sit waiting on a+ // sheet that was never going to appear.+ val missing = runtime.filter { !isGranted(it.permission!!) }+// Fire the intents. Runtime permissions need an Activity to host the// dialog — the app has none in the classic sense, so route through// the dedicated PermissionRequestActivity (manifest-registered,// translucent, finishes itself).- if (runtime.isNotEmpty()) {+ var fired = false+ if (missing.isNotEmpty()) {+ PermissionRequestActivity.arm()val i = Intent(context, PermissionRequestActivity::class.java).apply {- putExtra("permissions", runtime.mapNotNull { it.permission }.toTypedArray())+ putExtra("permissions", missing.mapNotNull { it.permission }.toTypedArray())addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)}- runCatching { context.startActivity(i) }+ fired = runCatching { context.startActivity(i) }.isSuccess}special.forEach {runCatching {@@ -146,6 +168,101 @@ class PermissionsController(private val context: Context) {)}}- return JSONObject().put("started", true).put("say", say)++ // Three ways this ends, in the order they can be known:+ // - nothing was missing → it is on already; no dialog was owed+ // - a dialog was fired → wait out the grace, because that is the+ // window in which a silent block answers+ // - only a Settings page → nothing to wait on but the user+ val result = when {+ runtime.isNotEmpty() && missing.isEmpty() -> GRANTED+ missing.isNotEmpty() && !fired -> NO_DIALOG+ missing.isNotEmpty() -> PermissionRequestActivity.await(DIALOG_GRACE_MS) ?: WAITING+ else -> WAITING+ }++ return JSONObject()+ .put("mode", mode)+ .put("result", result)+ .put("next", nextFor(mode, result))+ .put("say", say)+ }++ private fun isGranted(permission: String): Boolean =+ context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED++ /**+ * One line telling the brain what this result means for its next call.+ * It lives here rather than in the tool description because it depends on+ * both the mode and the result, and a description cannot say "if what you+ * just got back was X".+ */+ private fun nextFor(mode: String, result: String): String = when (result) {+ GRANTED -> "It is on — carry on with what you needed it for."+ DENIED -> "They saw the sheet and refused. Do not fire it again; say what you cannot do without it."+ NO_DIALOG -> "The system never drew a sheet, so firing again is silently ignored. " ++ "Do not retry — say it did not go through and that this step needs their hand in Settings."+ // Nobody has answered yet, so the screen is the only thing that can+ // still tell the two cases apart: a sheet waiting on a finger, or a+ // sheet that never came and never will. Look first — firing again is+ // the one move that cannot help.+ //+ // And note what is NOT offered here: tapping the sheet. Measured, a+ // system permission sheet exposes no elements and ignores an injected+ // tap, so "tap Allow yourself" would be advice that cannot be taken.+ else -> "Look at the screen before anything else: if nothing was drawn, do not " ++ "fire again — say it did not go through and that this step needs their hand. " ++ when (mode) {+ "solo" -> "If the sheet is there, it is waiting on their finger — you cannot " ++ "tap it. Say one line that it is up, and carry on with what does not need it."+ "handoff" -> "If the sheet is there it is theirs to answer — ask with ask_user, " ++ "saying what you are waiting for."+ else -> "A Settings page is open and only they can flip that switch — " ++ "ask with ask_user and name the switch."+ }+ }++ companion object {+ /**+ * How long a fired dialog gets to answer before it counts as "still+ * waiting". This is a grace period, not a wait for the human: a+ * permission the system has stopped asking about answers in+ * milliseconds with no sheet drawn, while a real dialog stays up for+ * as long as it takes. Waiting on the person is somebody else's job —+ * the ball reading the screen in solo mode, or ask_user in handoff.+ */+ internal const val DIALOG_GRACE_MS = 800L++ internal const val WAITING = "waiting"+ private const val GRANTED = PermissionRequestActivity.GRANTED+ private const val DENIED = PermissionRequestActivity.DENIED+ private const val NO_DIALOG = PermissionRequestActivity.NO_DIALOG++ /**+ * The grants the ball may answer itself: data about the user's own+ * body and their own diary, and nothing that leaves the phone.+ *+ * 🔴 This is a whitelist on purpose. Everything not named here routes+ * to handoff — including a permission added to `catalog` later by+ * someone who never read this — so the failure direction is "the user+ * was asked once more than necessary" rather than "the ball agreed to+ * something on their behalf".+ */+ private val SOLO_IDS = setOf("location", "steps", "calendar", "calendar_write")++ /** Ids with no dialog at all — only a switch inside Settings. */+ private val SETTINGS_IDS = setOf("notifications", "overlay")++ /**+ * Which route a request takes, from the ids alone, so it is testable+ * without a device. Conservative by construction: one non-solo id+ * hands over the whole request, because the dialogs arrive stacked and+ * tapping one's way through a stack is how the wrong one gets answered.+ */+ internal fun routeOf(ids: List<String>): String = when {+ ids.any { it in SETTINGS_IDS } -> "settings"+ ids.isNotEmpty() && ids.all { it in SOLO_IDS } -> "solo"+ else -> "handoff"+ }}}diff --git a/app/src/main/java/net/kuafuai/andee/net/ToolSchemas.kt b/app/src/main/java/net/kuafuai/andee/net/ToolSchemas.ktindex 09881ae..1c869ea 100644--- a/app/src/main/java/net/kuafuai/andee/net/ToolSchemas.kt+++ b/app/src/main/java/net/kuafuai/andee/net/ToolSchemas.kt@@ -714,7 +714,45 @@ object ToolSchemas {tool(name = "request_permissions",method = "device.permissions.request",- description = "Guide the user through enabling permissions. Two-step by design: first call WITHOUT confirm → you get a 'say' line (read it aloud / show it) → user agrees → call again WITH confirm=true to fire the dialogs/deep-links. Only call with confirm=true after the user said yes.",+ description = """+ Guide the user through enabling permissions. Two-step by design: first+ call WITHOUT confirm → you get a 'say' line (read it aloud / show it) →+ user agrees → call again WITH confirm=true to actually fire it. Only+ call with confirm=true after the user said yes.++ confirm=true returns what happened, and that return decides your next+ move. Read it — never assume the dialog appeared:++ mode "solo" fired, and yours to tell them about in one line+ mode "handoff" fired; ask with ask_user, saying what you wait for+ mode "settings" no sheet exists — a Settings page was opened++ result "granted" done, carry on+ "denied" they saw it and refused+ "no_dialog" the system never drew a sheet at all+ "waiting" it is on screen and nobody has answered yet++ 🔴 **A system permission sheet cannot be tapped by you.** It exposes no+ elements (get_screen_element comes back empty, with a picture and no+ e-numbers) and it ignores a tap you inject. Putting it on screen is the+ whole of what firing does; answering it is the user's one step. So when+ the result is "waiting", say one line — 「屏幕上弹了个权限框,点一下允许」—+ and either wait via ask_user or go on with whatever does not need it.+ Do not tap at it, and do not read its stillness as the screen being+ broken.++ Never fire the same request twice in a row. "no_dialog" means the system+ has stopped asking (usually after two refusals) and firing again is+ silently ignored — report it instead: say the permission did not go+ through, and that this step needs their hand in Settings.++ **The mode is the device's decision, not yours.** It follows what the+ grant reaches — your own data is "solo", other people's data and things+ that cannot be taken back are "handoff" — and a request that mixes the+ two is handed over whole. What it changes for you is ceremony, not who+ taps: after firing, LOOK at the screen before you call anything that+ depends on the permission.+ """.trimIndent(),required = listOf("ids"),props = mapOf("ids" to prop("array", "Permission ids from get_permissions.missing[].id, e.g. [\"location\",\"sms\"]."),diff --git a/app/src/test/java/net/kuafuai/andee/device/PermissionsRouteTest.kt b/app/src/test/java/net/kuafuai/andee/device/PermissionsRouteTest.ktnew file mode 100644index 0000000..cbabe85--- /dev/null+++ b/app/src/test/java/net/kuafuai/andee/device/PermissionsRouteTest.kt@@ -0,0 +1,52 @@+package net.kuafuai.andee.device++import org.junit.Assert.assertEquals+import org.junit.Test++/**+ * The three routes are a product decision wearing a mechanism's clothes, so+ * they get pinned down where nobody can quietly move them. Everything here is+ * decided from the id list alone — [PermissionsController.routeOf] says why+ * that is possible.+ */+class PermissionsRouteTest {++ @Test+ fun `own data is the ball to answer`() {+ assertEquals("solo", PermissionsController.routeOf(listOf("location")))+ assertEquals("solo", PermissionsController.routeOf(listOf("steps", "calendar", "calendar_write")))+ }++ @Test+ fun `other people data and acts that cannot be taken back are handed over`() {+ assertEquals("handoff", PermissionsController.routeOf(listOf("contacts")))+ assertEquals("handoff", PermissionsController.routeOf(listOf("sms", "sms_send")))+ assertEquals("handoff", PermissionsController.routeOf(listOf("call_phone", "call_log")))+ }++ @Test+ fun `a grant with no dialog is a settings errand whatever else it is asked with`() {+ assertEquals("settings", PermissionsController.routeOf(listOf("notifications")))+ assertEquals("settings", PermissionsController.routeOf(listOf("overlay")))+ // Not a severity ranking: a Settings page is the one route nothing+ // here can tap at all, so it outranks both of the others.+ assertEquals("settings", PermissionsController.routeOf(listOf("location", "notifications")))+ assertEquals("settings", PermissionsController.routeOf(listOf("contacts", "overlay")))+ }++ @Test+ fun `one non solo id hands over the whole request`() {+ // Solo and handoff dialogs stack on screen, so a solo id must not be+ // tapped through while a handoff id is queued behind it.+ assertEquals("handoff", PermissionsController.routeOf(listOf("location", "contacts")))+ assertEquals("handoff", PermissionsController.routeOf(listOf("calendar", "location", "sms")))+ }++ @Test+ fun `an id nobody classified falls on the safe side`() {+ // The guard for the day a permission joins the catalog and this file is+ // not read: being asked once too often beats being answered for.+ assertEquals("handoff", PermissionsController.routeOf(listOf("some_future_power")))+ assertEquals("handoff", PermissionsController.routeOf(listOf("location", "some_future_power")))+ }+}