Repository navigation
fix(mobile): hide finished Android Live Update when viewed or covered by alerts #41706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,6 +97,32 @@ object AgentNotifications { | |
| threadOnScreen = path | ||
| } | ||
|
|
||
| @Synchronized | ||
| fun setThreadOnScreen(context: Context, path: String?) { | ||
| threadOnScreen = path | ||
| if (path != null) { | ||
| val prefs = context.getSharedPreferences(STORE, Context.MODE_PRIVATE) | ||
| val lastTerminal = prefs | ||
| .takeUnless { it.getBoolean("lastActive", false) } | ||
| ?.getString("lastTerminal", null) | ||
| val matchesSingleRow = lastTerminal != null && | ||
| lastTerminal.endsWith("\ntrue") && | ||
| lastTerminal.substringBefore('\n') == path | ||
| if (matchesSingleRow) { | ||
| prefs.edit().putString("coveredTerminal", lastTerminal).apply() | ||
| cancelActivity(context) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun isThreadOnScreen(path: String?): Boolean { | ||
| val resumed = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast( | ||
| Lifecycle.State.RESUMED | ||
| ) | ||
| val visibleThread = threadOnScreen | ||
| return resumed && visibleThread != null && path == visibleThread | ||
| } | ||
|
|
||
| @Synchronized | ||
| fun dismiss(context: Context) { | ||
| context.getSharedPreferences( | ||
|
|
@@ -145,11 +171,7 @@ object AgentNotifications { | |
| ?: prefs.getStringSet("seenAlerts", emptySet()).orEmpty().toList() | ||
| if (alertId != null && alertId !in seen) { | ||
| // Consume suppressed alerts so retries cannot resurface them later. | ||
| val resumed = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast( | ||
| Lifecycle.State.RESUMED | ||
| ) | ||
| val visibleThread = threadOnScreen | ||
| val onScreen = resumed && visibleThread != null && data["alert_path"] == visibleThread | ||
| val onScreen = isThreadOnScreen(data["alert_path"]) | ||
| if (!onScreen) { | ||
| postAlert(context, scheme, data, alertId) | ||
| } | ||
|
|
@@ -226,10 +248,10 @@ object AgentNotifications { | |
| updatedAt: Long, | ||
| alerted: Boolean | ||
| ) { | ||
| val terminal = singleTerminal(data) | ||
| // Only a one-row card on the alert's own thread is known to show nothing more | ||
| // than the alert. Grouped alerts and cards with other rows stay visible. | ||
| val covered = terminal?.takeIf { alerted && data["alert_path"] == data["activity_path"] } | ||
| val terminal = terminalKey(data) | ||
| // Hide a finished card when its alert already covered the visible rows or the | ||
| // user is already looking at the finished thread on screen. | ||
| val covered = coveredTerminal(data, terminal, alerted) | ||
| if (covered != null) coverTerminal(prefs, covered, updatedAt) | ||
| if (updatedAt < prefs.getLong("lastUpdate", 0)) { | ||
| // A newer replay of this same finish may already show the card. | ||
|
|
@@ -261,14 +283,30 @@ object AgentNotifications { | |
| } | ||
| } | ||
|
|
||
| /** Identifies a finished one-thread card by its path, phase and absolute expiry. */ | ||
| private fun singleTerminal(data: Map<String, String>): String? { | ||
| private fun coveredTerminal( | ||
| data: Map<String, String>, | ||
| terminal: String?, | ||
| alerted: Boolean | ||
| ): String? = terminal?.takeIf { | ||
| val singleRow = data.containsKey("activity_line_0") && !data.containsKey("activity_line_1") | ||
| if (singleRow) { | ||
| (alerted && data["alert_path"] == data["activity_path"]) || | ||
| isThreadOnScreen(data["activity_path"]) | ||
| } else { | ||
| alerted && data["alert_path"] == "/" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The relay filters the grouped alert input to The alert payload must not cover card rows it does not name. Define the required behavior before choosing a mechanism: a multi-row finished card must remain visible while any row it lists is not covered by a posted alert. Choose between a producer-side coverage signal and a narrower client-side rule. The inspected payload does not identify rows, so the Android check cannot be per row without a new field or a narrower rule.
|
||
| } | ||
| } | ||
|
|
||
| /** Identifies a finished card by its path, phase, scope and absolute expiry. */ | ||
| private fun terminalKey(data: Map<String, String>): String? { | ||
| if (data["active"] == "true") return null | ||
| val phase = data["activity_phase"] | ||
| val finished = phase == "completed" || phase == "failed" | ||
| val single = data.containsKey("activity_line_0") && !data.containsKey("activity_line_1") | ||
| val hasRow = data.containsKey("activity_line_0") | ||
| val single = hasRow && !data.containsKey("activity_line_1") | ||
| val expiresAt = data["activity_expires_at"] | ||
| return "${data["activity_path"]}\n$phase\n$expiresAt" | ||
| .takeIf { finished && single && expiresAt != null } | ||
| return "${data["activity_path"]}\n$phase\n$expiresAt\n$single" | ||
| .takeIf { finished && hasRow && expiresAt != null } | ||
| } | ||
|
|
||
| private fun isCovered(prefs: SharedPreferences, terminal: String?) = | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reduce the return count to unblock mobile lint.
The supplied CI output reports that this function has three returns. Detekt permits only two, so
vp run lint:mobilefails.Restructure the guards without changing the checks for null paths, active work, or matching single-row terminals.
GitHub Actions: CI / 2_Mobile Native Static Analysis.txt
[error] 101-101:
vp run lint:mobilefailed duringdetekt --config detekt.yml. Detekt ReturnCount:setThreadOnScreenhas 3 return statements, exceeding the limit of 2.GitHub Actions: CI / Mobile Native Static Analysis
[error] 101-101:
vp run lint:mobilefailed during detekt analysis.setThreadOnScreenhas 3 return statements, exceeding the ReturnCount limit of 2.Source: Pipeline failures