Skip to content

armgit / andee

Public Imported from GitHub
0

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): re-read the local half when the card resumes

booboosui30f58feb0afc
diff --git a/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt b/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt
index 831abd1..8d10c05 100644
--- a/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt
+++ b/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt
@@ -207,6 +207,48 @@ object SelfCheck {
/** [run] with no network, for the caller's first paint. */
fun local(context: Context): List<Finding> = run(context, probe = false)
+ /**
+ * The three rows only a probe can answer — the backend, the version
+ * service, the voice host.
+ *
+ * Private because the *set* is the whole of what [mergeLocal] means: a
+ * caller that wants this behaviour calls [mergeLocal], and one that wants
+ * the raw facts calls [run]. Nothing outside should be matching on these
+ * ids by hand.
+ */
+ private val PROBED_IDS = setOf("backend", "version", "voice_host")
+
+ /**
+ * Splice a fresh no-probe pass over the last full one.
+ *
+ * [local] does not return the list minus three rows. With `probe = false`
+ * those three come back as "checking…" placeholders — which is exactly what
+ * lets a card paint its first frame instantly and fill in behind it — so
+ * handing that list to a card that has *already* answered them would wipe a
+ * real reading back to a placeholder, and nothing would come along to fill
+ * it in again: the probe only ever runs from a full pass.
+ *
+ * That is the whole job here. Every row this pass could not answer keeps
+ * what the previous pass found; every other row is the fresh reading, which
+ * is the point of re-reading at all. See
+ * [net.kuafuai.andee.ui.SelfCheckUi.refreshLocal] for the caller, and
+ * [SelfCheckMergeTest] for the cases that pin this down.
+ *
+ * ### The one thing it does not fix
+ *
+ * A device that has gone *offline* since the last full pass keeps the old
+ * network reading on these three rows, because a probe cannot tell "not
+ * asked" from "no network" — both land in the same branch of each one. The
+ * `network` row is local, so on its own it does turn red and say so, and
+ * that is the signal the user acts on. Splitting the two cases inside all
+ * three probes to make these agree is not worth three more branches today.
+ */
+ fun mergeLocal(fresh: List<Finding>, previous: List<Finding>?): List<Finding> {
+ if (previous == null) return fresh
+ val known = previous.associateBy { it.id }
+ return fresh.map { f -> if (f.id in PROBED_IDS) known[f.id] ?: f else f }
+ }
+
// ---- The checks ----
/**
diff --git a/app/src/main/java/net/kuafuai/andee/ui/SelfCheckActivity.kt b/app/src/main/java/net/kuafuai/andee/ui/SelfCheckActivity.kt
index b7ce7d4..14218c9 100644
--- a/app/src/main/java/net/kuafuai/andee/ui/SelfCheckActivity.kt
+++ b/app/src/main/java/net/kuafuai/andee/ui/SelfCheckActivity.kt
@@ -148,6 +148,21 @@ class SelfCheckActivity : StageActivity() {
*/
private var paintedLang = ""
+ /**
+ * Whether this page has already been resumed once.
+ *
+ * [onCreate] runs the check itself ([SelfCheckUi.start]), and the first
+ * resume lands immediately behind it — same launch, no user in between, so
+ * there is nothing new to read and a refresh there would be a local pass for
+ * nothing. Worse than nothing, in fact: that first pass's probe is still on
+ * its socket, and the refresh would overwrite the rows it is about to fill
+ * in with the placeholders it is about to replace.
+ *
+ * Every resume *after* this one is the case the refresher is for. See
+ * [onResume].
+ */
+ private var resumedOnce = false
+
/**
* Our settings sheet while it is up — see [openOwnSettings]. Held only so a
* second tap on 打开设置 does not stack a second window on the first.
@@ -415,6 +430,32 @@ class SelfCheckActivity : StageActivity() {
val now = VoiceConfig.uiLanguage(this)
if (paintedLang.isNotEmpty() && now != paintedLang) ui.repaintForLanguage()
paintedLang = now
+
+ // Coming back from a system screen, and re-reading the local half.
+ //
+ // This page's own rows send the user out — 去开启, 允许, 去下载 — and the
+ // only thing that used to happen on the way back was the language
+ // comparison above. So a row the user had just fixed stayed red until
+ // they found 重新检查 at the bottom of the card, and a stale red row
+ // reads as "the fix did not take". Being an Activity is what makes
+ // noticing this possible at all: the same list on a working device is an
+ // overlay owned by the service and gets no resume callback, so there the
+ // card is closed on the way out instead and reopened reads fresh — see
+ // `BodyCoreService.openSelfCheck`.
+ //
+ // Local only, deliberately. The probes are a socket that holds the card
+ // for seconds, and running them every time the user comes back would
+ // make the check slower the more of it you have already done. See
+ // [SelfCheckUi.refreshLocal] for the whole argument, including why the
+ // three probed rows are carried over rather than reset.
+ //
+ // Skipped on the first resume, which is [onCreate]'s own: see
+ // [resumedOnce].
+ if (!resumedOnce) {
+ resumedOnce = true
+ return
+ }
+ ui.refreshLocal()
}
override fun onDestroy() {
diff --git a/app/src/main/java/net/kuafuai/andee/ui/SelfCheckUi.kt b/app/src/main/java/net/kuafuai/andee/ui/SelfCheckUi.kt
index 50d0803..e5a0a58 100644
--- a/app/src/main/java/net/kuafuai/andee/ui/SelfCheckUi.kt
+++ b/app/src/main/java/net/kuafuai/andee/ui/SelfCheckUi.kt
@@ -157,6 +157,20 @@ class SelfCheckUi(
private var hero: LinearLayout? = null
private var rows: LinearLayout? = null
+ /**
+ * The rows' scroll container, kept for one reason only: [render] tears the
+ * rows down and rebuilds them, and a `ScrollView` whose children have just
+ * been removed has a content height of zero — the platform clamps its
+ * `scrollY` away the moment it relayouts. Without holding the view there is
+ * no way to put the user back where they were. See [render].
+ *
+ * Re-pointed by every [build], so after [rebuild] it is the *new* card's
+ * scroller; the outgoing one is simply dropped with the rest of the old
+ * card. Reading it while it is detached is harmless — [refreshLocal] and
+ * [repaintForLanguage] both bail on `root == null` before any redraw.
+ */
+ private var scroller: ScrollView? = null
+
/**
* The last list drawn, kept so a language switch can redraw it without
* re-running the probes.
@@ -321,6 +335,30 @@ class SelfCheckUi(
probe()
}
+ /**
+ * Re-read the local half and redraw — the resume-shaped subset of [start].
+ *
+ * This is what a user who has just been off granting something in a system
+ * Settings screen needs to see when they come back, and nothing they do not.
+ * [SelfCheck.local] is offline and instant by construction, so this is cheap
+ * enough to run on every resume; [start] deliberately is not, because its
+ * second half holds the card open on a socket for seconds — and a check that
+ * runs *because* the user is staring at it is the last place to spend that.
+ *
+ * The three rows a local pass cannot answer keep their last reading rather
+ * than being reset to "checking…" — see [SelfCheck.mergeLocal], which is
+ * where that decision lives and why. Everything else is the fresh reading,
+ * so a permission granted in the Settings screen is reflected on return,
+ * which is the whole point.
+ *
+ * Safe to call while the card is not up: it is a no-op, exactly like
+ * [repaintForLanguage].
+ */
+ fun refreshLocal() {
+ if (root == null) return
+ render(SelfCheck.mergeLocal(SelfCheck.local(context), last))
+ }
+
private fun probe() {
val app = context.applicationContext
prober.execute {
@@ -348,6 +386,16 @@ class SelfCheckUi(
val attention = found.filter { SelfCheck.needsAttention(it) }
val rest = found.filterNot { SelfCheck.needsAttention(it) }
+ // Where the user was, read before a single row is removed. Tearing the
+ // children out drops the scroller's content height to zero, and the
+ // platform clamps `scrollY` to that on the next layout pass — so every
+ // redraw used to land the list back at the summary. That was merely
+ // quiet while a redraw only happened on a deliberate tap; it stops being
+ // quiet now that [refreshLocal] redraws on every resume, because the row
+ // that sends the user out is very often the battery one, at the bottom.
+ // See [SelfCheckActivity.onResume].
+ val keep = scroller?.scrollY ?: 0
+
hero?.removeAllViews()
hero?.addView(summary(attention.size))
@@ -357,6 +405,16 @@ class SelfCheckUi(
if (attention.isNotEmpty()) rows?.addView(sectionHeading(R.string.check_page_notes_lead))
rest.forEach { rows?.addView(row(it)) }
}
+
+ // Posted rather than called: the replacement rows are not measured yet,
+ // so a `scrollTo` here would be clamped away by the layout that follows
+ // it. Still the same frame, so nothing lands on screen first.
+ //
+ // `> 0` and not `!= 0` on purpose: it keeps the untouched case a genuine
+ // no-op rather than a redundant layout request, and a list that shrank
+ // under the user (a red row turning green) is clamped by the platform to
+ // the new bottom, which is the answer we want anyway.
+ if (keep > 0) scroller?.post { scroller?.scrollTo(0, keep) }
}
// ---- Language ----
@@ -467,6 +525,9 @@ class SelfCheckUi(
// is one line too many.
isVerticalScrollBarEnabled = false
}
+ // Held as well as held locally: [render] needs it, and it only exists
+ // inside this method otherwise. See [scroller].
+ scroller = scroll
val body = LinearLayout(context).apply {
orientation = LinearLayout.VERTICAL
// Same 24dp as the settings form: this is the same card, and two
diff --git a/app/src/test/java/net/kuafuai/andee/device/SelfCheckMergeTest.kt b/app/src/test/java/net/kuafuai/andee/device/SelfCheckMergeTest.kt
new file mode 100644
index 0000000..d9a3a29
--- /dev/null
+++ b/app/src/test/java/net/kuafuai/andee/device/SelfCheckMergeTest.kt
@@ -0,0 +1,86 @@
+package net.kuafuai.andee.device
+
+import net.kuafuai.andee.device.SelfCheck.Finding
+import net.kuafuai.andee.device.SelfCheck.Level
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+/**
+ * A resumed card re-reads the local half and splices it over what a probe had
+ * already found. The splicing rule is small and easy to get subtly wrong in a
+ * way that only shows up on a device — a row that quietly reverts to "checking…"
+ * forever, or a permission that stays red after the user granted it. So it is
+ * pinned down here, away from the View layer it is used from.
+ *
+ * [SelfCheck.mergeLocal] carries the argument for the rule itself.
+ */
+class SelfCheckMergeTest {
+
+ /** The ids matter here and nothing else does, so `title`/`detail` are unused. */
+ private fun row(id: String, level: Level) = Finding(id = id, title = 0, level = level, detail = 0)
+
+ @Test
+ fun `a local re-read keeps the three rows only a probe could answer`() {
+ val previous = listOf(
+ row("backend", Level.OK),
+ row("version", Level.NOTE),
+ row("voice_host", Level.FAIL),
+ )
+ // What a no-probe pass produces for those same three: placeholders.
+ val fresh = listOf(
+ row("backend", Level.NOTE),
+ row("version", Level.NOTE),
+ row("voice_host", Level.NOTE),
+ )
+
+ val merged = SelfCheck.mergeLocal(fresh, previous)
+
+ assertEquals(Level.OK, merged.first { it.id == "backend" }.level)
+ assertEquals(Level.NOTE, merged.first { it.id == "version" }.level)
+ assertEquals(Level.FAIL, merged.first { it.id == "voice_host" }.level)
+ }
+
+ @Test
+ fun `every other row is the fresh reading, which is the whole point`() {
+ // The permission rows are what the user went to Settings to change.
+ val previous = listOf(row("overlay", Level.FAIL), row("accessibility", Level.FAIL))
+ val fresh = listOf(row("overlay", Level.OK), row("accessibility", Level.OK))
+
+ val merged = SelfCheck.mergeLocal(fresh, previous)
+
+ assertEquals(Level.OK, merged.first { it.id == "overlay" }.level)
+ assertEquals(Level.OK, merged.first { it.id == "accessibility" }.level)
+ }
+
+ @Test
+ fun `a first paint has nothing to keep`() {
+ val fresh = listOf(row("overlay", Level.OK), row("backend", Level.NOTE))
+
+ assertEquals(fresh, SelfCheck.mergeLocal(fresh, previous = null))
+ }
+
+ @Test
+ fun `a probed row the last pass never had stays a placeholder`() {
+ // Deeper than it looks: the alternative is dropping the row, which would
+ // silently shorten the list the first time a probe is added.
+ val merged = SelfCheck.mergeLocal(
+ fresh = listOf(row("backend", Level.NOTE)),
+ previous = listOf(row("overlay", Level.OK)),
+ )
+
+ assertEquals(Level.NOTE, merged.first { it.id == "backend" }.level)
+ }
+
+ @Test
+ fun `the fresh pass owns the shape of the list, not the previous one`() {
+ // Ids are the join key, not positions — a pass that reorders or drops
+ // rows must not hand the previous pass's rows a new identity.
+ val merged = SelfCheck.mergeLocal(
+ fresh = listOf(row("backend", Level.NOTE), row("overlay", Level.OK)),
+ previous = listOf(row("overlay", Level.FAIL), row("backend", Level.OK)),
+ )
+
+ assertEquals(listOf("backend", "overlay"), merged.map { it.id })
+ assertEquals(Level.OK, merged.first { it.id == "backend" }.level)
+ }
+}