diff --git a/controller/app/src/main/AndroidManifest.xml b/controller/app/src/main/AndroidManifest.xml index e741a75fd..5c86696cb 100644 --- a/controller/app/src/main/AndroidManifest.xml +++ b/controller/app/src/main/AndroidManifest.xml @@ -119,6 +119,13 @@ android:theme="@style/Theme.K2Go" /> + + The card answers one question — "is a newer REST-core build available, and can I act on it?" — + * and four states cover it: + *
    + *
  • {@link Kind#OFFLINE} — no network. There is no honest Update affordance (a rebuild needs the + * internet to fetch), so the card says "No connection" rather than offering an action it can't + * complete. Offline wins over any cached value for exactly this reason.
  • + *
  • {@link Kind#UPDATE_AVAILABLE} — a newer build exists; the primary action is Update.
  • + *
  • {@link Kind#UP_TO_DATE} — on the latest; the primary action is a manual Rebuild + * (de-emphasized).
  • + *
  • {@link Kind#CHECKING} — online, no result yet and nothing cached to fall back on.
  • + *
+ * + *

The version subtitle ("v1.2.7 → v1.3.0") needs both versions, which only a live check + * carries — the cache stores just the boolean (ADFA-5026). A cached UPDATE_AVAILABLE therefore shows + * the state without the arrow; {@link #targetVersion()} is null there, and the caller omits it. + */ +public final class DashboardCardState { + + public enum Kind { CHECKING, UP_TO_DATE, UPDATE_AVAILABLE, OFFLINE } + + private final Kind kind; + private final String installedVersion; // may be null/empty when unknown + private final String targetVersion; // non-null only for a LIVE UPDATE_AVAILABLE + + private DashboardCardState(Kind kind, String installedVersion, String targetVersion) { + this.kind = kind; + this.installedVersion = installedVersion; + this.targetVersion = targetVersion; + } + + /** + * @param online whether the device has an internet-capable network + * @param liveOk whether a live update-check just succeeded (false = it errored or hasn't run) + * @param liveUpdate the live check's "newer build available" result (only read when liveOk) + * @param installed the installed version from the live check (for the subtitle), or null + * @param available the available version from the live check (for the subtitle), or null + * @param hasCache whether a previous successful check left a cached value + * @param cachedUpdate the cached "update available" flag (only read when hasCache and not liveOk) + */ + public static DashboardCardState resolve(boolean online, boolean liveOk, boolean liveUpdate, + String installed, String available, + boolean hasCache, boolean cachedUpdate) { + if (!online) { + return new DashboardCardState(Kind.OFFLINE, installed, null); + } + if (liveOk) { + return liveUpdate + ? new DashboardCardState(Kind.UPDATE_AVAILABLE, installed, available) + : new DashboardCardState(Kind.UP_TO_DATE, installed, null); + } + if (hasCache) { + // Online but the check failed (box stopped): fall back to the last-known state. The cache + // holds only the boolean, so an available UPDATE_AVAILABLE here has no target version. + return cachedUpdate + ? new DashboardCardState(Kind.UPDATE_AVAILABLE, installed, null) + : new DashboardCardState(Kind.UP_TO_DATE, installed, null); + } + return new DashboardCardState(Kind.CHECKING, installed, null); + } + + public Kind kind() { return kind; } + + /** The primary button is "Update" only when a newer build is available; otherwise "Rebuild". */ + public boolean primaryIsUpdate() { return kind == Kind.UPDATE_AVAILABLE; } + + /** The card shows a "No connection" affordance instead of an actionable state. */ + public boolean isOffline() { return kind == Kind.OFFLINE; } + + /** The installed version, for the subtitle; may be null/empty when unknown. */ + public String installedVersion() { return installedVersion; } + + /** The target version for the "vX → vY" subtitle, or null when it should not be shown (every + * state but a LIVE UPDATE_AVAILABLE). Present iff {@link #showsVersionArrow()}. */ + public String targetVersion() { return targetVersion; } + + /** True when both versions are known and differ, so the "vX → vY" subtitle is worth showing. */ + public boolean showsVersionArrow() { + return targetVersion != null && !targetVersion.isEmpty() + && installedVersion != null && !installedVersion.isEmpty() + && !targetVersion.equals(installedVersion); + } +} diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/DashboardCancelConfirmActivity.java b/controller/app/src/main/java/org/iiab/controller/redesign/DashboardCancelConfirmActivity.java new file mode 100644 index 000000000..0bc2829b4 --- /dev/null +++ b/controller/app/src/main/java/org/iiab/controller/redesign/DashboardCancelConfirmActivity.java @@ -0,0 +1,46 @@ +/* + * ============================================================================ + * Name : DashboardCancelConfirmActivity.java + * Author : AppDevForAll + * Copyright : Copyright (c) 2026 AppDevForAll + * Description : ADFA-5339. Confirmation gate for cancelling a background dashboard update FROM THE + * NOTIFICATION. A notification action can't host a dialog, so its Cancel routes here — a + * windowless, transparent activity that shows the same "Stop the update?" dialog the + * card uses, and only on a positive answer signals DashboardRebuildService. Dismiss or + * "Keep updating" just finishes, leaving the update running. (The in-card Cancel shows + * the dialog inline in DashboardDetailFragment; this covers the notification path.) + * ============================================================================ + */ +package org.iiab.controller.redesign; + +import android.content.Intent; +import android.os.Bundle; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; + +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + +import org.iiab.controller.R; + +public final class DashboardCancelConfirmActivity extends AppCompatActivity { + + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // If the update already ended between tapping the notification and getting here, there is + // nothing to cancel — just close without a dialog. + if (!DashboardRebuildService.isRunning()) { finish(); return; } + new MaterialAlertDialogBuilder(this) + .setTitle(R.string.k2go_dash_cancel_confirm_title) + .setMessage(R.string.k2go_dash_cancel_confirm_msg) + .setNegativeButton(R.string.k2go_dash_cancel_confirm_keep, (d, w) -> finish()) + .setPositiveButton(R.string.k2go_dash_cancel_confirm_stop, (d, w) -> { + ContextCompat.startForegroundService(this, new Intent(this, DashboardRebuildService.class) + .setAction(DashboardRebuildService.ACTION_CANCEL)); + finish(); + }) + .setOnCancelListener(d -> finish()) // tap-outside / back = keep updating + .show(); + } +} diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/DashboardCardStatus.java b/controller/app/src/main/java/org/iiab/controller/redesign/DashboardCardStatus.java new file mode 100644 index 000000000..a43779226 --- /dev/null +++ b/controller/app/src/main/java/org/iiab/controller/redesign/DashboardCardStatus.java @@ -0,0 +1,63 @@ +/* + * ============================================================================ + * Name : DashboardCardStatus.java + * Author : AppDevForAll + * Copyright : Copyright (c) 2026 AppDevForAll + * Description : ADFA-5339. The one place that FETCHES the Dashboard card's update state — cache-first, + * then a live /update-check, degrading to offline — and delivers it as a resolved + * DashboardCardState. Both surfaces (the detail card and the module-hub row) call this + * and only render; without it each would re-implement the cache/live/offline dance and + * they would drift. The rule that turns inputs into a state is the pure + * DashboardCardState (domain); this is the glue that gathers the inputs. + * ============================================================================ + */ +package org.iiab.controller.redesign; + +import android.content.Context; + +import org.iiab.controller.dashboard.domain.DashboardCardState; + +public final class DashboardCardStatus { + + private DashboardCardStatus() {} + + /** Delivered on the main thread. May be called TWICE: once immediately with the cached/offline + * state (so the pill isn't stuck on "Checking…"), then again with the live result. The caller + * guards its own view lifecycle (isAdded) before painting. */ + public interface Listener { void onState(DashboardCardState state); } + + /** + * Resolve the card state and hand it to {@code l}. Cache and connectivity are read synchronously + * on the calling (main) thread; the live check is async via {@link DashboardClient#updateCheck}, + * which already posts back to main. + */ + public static void fetch(Context context, Listener l) { + final Context ctx = context.getApplicationContext(); + final boolean online = DashboardRebuild.hasInternet(ctx); + + // Cached-first (ADFA-5026) so the UI isn't blank while the live check runs; the cache holds + // only the boolean, so no versions -> no arrow from a cached state. + if (UpdateStatusCache.has(ctx)) { + l.onState(DashboardCardState.resolve( + online, false, false, null, null, true, UpdateStatusCache.updateAvailable(ctx))); + } else if (!online) { + l.onState(DashboardCardState.resolve(false, false, false, null, null, false, false)); + } + if (!online) return; // offline: no live check; the state above stands + + DashboardClient.updateCheck(new DashboardClient.UpdateCb() { + @Override public void onResult(String installed, String available, boolean updateAvailable) { + UpdateStatusCache.save(ctx, updateAvailable); + l.onState(DashboardCardState.resolve( + true, true, updateAvailable, installed, available, true, updateAvailable)); + } + @Override public void onErr(String message) { + // Online but the check failed (box stopped): fall back to the cached state, or Checking + // when nothing is cached. Re-read connectivity in case the network just dropped. + l.onState(DashboardCardState.resolve( + DashboardRebuild.hasInternet(ctx), false, false, null, null, + UpdateStatusCache.has(ctx), UpdateStatusCache.updateAvailable(ctx))); + } + }); + } +} diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/DashboardDetailFragment.java b/controller/app/src/main/java/org/iiab/controller/redesign/DashboardDetailFragment.java index a6d7084bc..dbd661ed4 100644 --- a/controller/app/src/main/java/org/iiab/controller/redesign/DashboardDetailFragment.java +++ b/controller/app/src/main/java/org/iiab/controller/redesign/DashboardDetailFragment.java @@ -36,6 +36,7 @@ import com.google.android.material.progressindicator.LinearProgressIndicator; import org.iiab.controller.R; +import org.iiab.controller.dashboard.domain.DashboardCardState; import org.iiab.controller.util.AppExecutors; public class DashboardDetailFragment extends Fragment { @@ -158,33 +159,56 @@ private void resolveInitialUpdatingState() { * emphasis. Shows the cached last-known state right away (if any) so the pill isn't stuck on * "Checking…", then refreshes from the live check; on failure it keeps the cached state. */ private void fetchUpdateStatus() { - final Context ctx = requireContext().getApplicationContext(); - if (UpdateStatusCache.has(ctx)) applyUpdateStatus(UpdateStatusCache.updateAvailable(ctx)); - DashboardClient.updateCheck(new DashboardClient.UpdateCb() { - @Override public void onResult(String installed, String available, boolean updateAvailable) { - if (!isAdded()) return; - UpdateStatusCache.save(ctx, updateAvailable); - applyUpdateStatus(updateAvailable); - } - @Override public void onErr(String message) { - if (!isAdded()) return; - if (UpdateStatusCache.has(ctx)) applyUpdateStatus(UpdateStatusCache.updateAvailable(ctx)); - else if (statusChip != null) statusChip.setVisibility(View.GONE); // nothing to show yet - } - }); + // ADFA-5339: the cache/live/offline orchestration lives once in DashboardCardStatus, shared + // with the hub row; this fragment only paints what it delivers. + DashboardCardStatus.fetch(requireContext(), s -> { if (isAdded()) applyCardState(s); }); } - /** Reflect the update status: recolor the pill in place and, when already on the latest, de-emphasize - * Rebuild + show the "not needed" hint (never blocks — the user can still rebuild). */ - private void applyUpdateStatus(boolean updateAvailable) { + /** ADFA-5339: paint the card from the shared {@link DashboardCardState} rule — the pill, the primary + * button (Update vs Rebuild), the version subtitle and the "on latest" hint. The rule lives in the + * domain so this and the hub row can't drift; this method only renders it. Never blocks — the user + * can still Rebuild manually. */ + private void applyCardState(DashboardCardState s) { if (statusChip != null) { - if (updateAvailable) styleChip(statusChip, getString(R.string.k2go_dash_chip_update), R.color.k2go_amber); - else styleChip(statusChip, getString(R.string.k2go_dash_chip_uptodate), R.color.k2go_leaf); + switch (s.kind()) { + case OFFLINE: + styleChip(statusChip, getString(R.string.k2go_dash_no_connection), R.color.k2go_muted); break; + case CHECKING: + styleChip(statusChip, getString(R.string.k2go_dash_chip_checking), R.color.k2go_muted); break; + case UP_TO_DATE: + styleChip(statusChip, getString(R.string.k2go_dash_chip_uptodate), R.color.k2go_leaf); break; + case UPDATE_AVAILABLE: + styleChip(statusChip, getString(R.string.k2go_dash_chip_update), R.color.k2go_amber); break; + } statusChip.setVisibility(View.VISIBLE); } - if (updating) return; // ADFA-5333: a rebuild is in flight — keep Rebuild disabled and the bar shown - if (rebuild != null) rebuild.setAlpha(updateAvailable ? 1f : 0.6f); - if (rebuildHint != null) rebuildHint.setVisibility(updateAvailable ? View.GONE : View.VISIBLE); + // Fork B (ADFA-5339): the version target rides in the subtitle, not the button. Only a LIVE + // update-available carries both versions; every other state restores the descriptive subtitle. + View root = getView(); + TextView sub = root != null ? (TextView) root.findViewById(R.id.k2go_moddet_sub) : null; + if (sub != null) { + if (s.showsVersionArrow()) { + sub.setText(getString(R.string.k2go_dash_card_sub_update_fmt, + s.installedVersion(), s.targetVersion())); + } else { + sub.setText(R.string.k2go_dash_detail_sub); + } + } + if (updating) return; // ADFA-5333: a rebuild is in flight — leave the button/bar as they are + if (rebuild != null) { + if (s.primaryIsUpdate()) { + rebuild.setText(R.string.k2go_dash_update); + rebuild.setAlpha(1f); + } else { + rebuild.setText(R.string.k2go_dash_rebuild); + // De-emphasize only when we KNOW we're on the latest; offline/checking stay full. + rebuild.setAlpha(s.kind() == DashboardCardState.Kind.UP_TO_DATE ? 0.6f : 1f); + } + } + if (rebuildHint != null) { + rebuildHint.setVisibility( + s.kind() == DashboardCardState.Kind.UP_TO_DATE ? View.VISIBLE : View.GONE); + } } /** Insert a small muted note directly above the Rebuild button (hidden until we know we're on the @@ -281,6 +305,17 @@ private View buildUpdatingRow(Button rebuildBtn) { /** Ask the service to cancel; the bar stays until STATE_CANCELLED lands (or the update finishes if it * was too late). Disable the affordance to avoid double taps. */ private void onCancelUpdate() { + // ADFA-5339: stopping a live update is worth a confirm — the swap may be seconds away, and a + // stray tap shouldn't abandon it. Only on a positive answer do we signal the server. + new com.google.android.material.dialog.MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.k2go_dash_cancel_confirm_title) + .setMessage(R.string.k2go_dash_cancel_confirm_msg) + .setNegativeButton(R.string.k2go_dash_cancel_confirm_keep, null) + .setPositiveButton(R.string.k2go_dash_cancel_confirm_stop, (d, w) -> signalCancel()) + .show(); + } + + private void signalCancel() { if (updatingCancel != null) updatingCancel.setEnabled(false); Context ctx = requireContext(); ContextCompat.startForegroundService(ctx, diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/DashboardRebuildService.java b/controller/app/src/main/java/org/iiab/controller/redesign/DashboardRebuildService.java index 9dcbedeea..476f4aa9c 100644 --- a/controller/app/src/main/java/org/iiab/controller/redesign/DashboardRebuildService.java +++ b/controller/app/src/main/java/org/iiab/controller/redesign/DashboardRebuildService.java @@ -242,8 +242,12 @@ private PendingIntent openDashboardDetail() { /** Ongoing "updating…" notification. Not dismissible and does NOT auto-cancel on tap — while the * update runs it stays put as the way back to the in-app indicator. Carries a Cancel action. */ private Notification buildOngoing() { - PendingIntent cancel = PendingIntent.getService(this, 1, - new Intent(this, DashboardRebuildService.class).setAction(ACTION_CANCEL), + // ADFA-5339: the Cancel action asks first. It can't show a dialog from the notification, so it + // routes through a transparent confirm activity that signals ACTION_CANCEL only on a yes — + // instead of firing the cancel straight at the service as it used to. + PendingIntent cancel = PendingIntent.getActivity(this, 1, + new Intent(this, DashboardCancelConfirmActivity.class) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP), PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); return new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle(getString(R.string.k2go_dash_live_title)) diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/ModuleHubFragment.java b/controller/app/src/main/java/org/iiab/controller/redesign/ModuleHubFragment.java index cc0865142..a867495a0 100644 --- a/controller/app/src/main/java/org/iiab/controller/redesign/ModuleHubFragment.java +++ b/controller/app/src/main/java/org/iiab/controller/redesign/ModuleHubFragment.java @@ -554,6 +554,18 @@ private void addSystemDashboardCard() { host.addView(row); fetchDashVersion(sub); + // ADFA-5339: make the row update-aware through the shared status helper — the pill reads + // "Update" when a newer build exists, "Rebuild" otherwise (offline stays "Rebuild"; the + // confirm gate refuses it), and a LIVE update also shows the version arrow in the subtitle + // (Fork B). fetchDashVersion above set the base installed-version subtitle for every other state. + DashboardCardStatus.fetch(requireContext(), s -> { + if (!isAdded()) return; + rebuild.setText(s.primaryIsUpdate() ? R.string.k2go_dash_update : R.string.k2go_dash_rebuild); + if (s.showsVersionArrow()) { + sub.setText(getString(R.string.k2go_dash_card_sub_update_fmt, + s.installedVersion(), s.targetVersion())); + } + }); } /** Show the installed dash-node version on the card. Read from the rootfs package.json on disk diff --git a/controller/app/src/main/res/values/strings_5339.xml b/controller/app/src/main/res/values/strings_5339.xml new file mode 100644 index 000000000..40c1a14ef --- /dev/null +++ b/controller/app/src/main/res/values/strings_5339.xml @@ -0,0 +1,27 @@ + + + + + Update + + REST core · v%1$s → v%2$s + + No connection + + Stop the update? + The dashboard stays on its current version. + Stop update + Keep updating + diff --git a/controller/app/src/main/res/values/themes_k2go.xml b/controller/app/src/main/res/values/themes_k2go.xml index b86fb9380..9ca897acd 100644 --- a/controller/app/src/main/res/values/themes_k2go.xml +++ b/controller/app/src/main/res/values/themes_k2go.xml @@ -123,4 +123,16 @@ @font/atkinson_hyperlegible 12sp + + + diff --git a/controller/app/src/test/java/org/iiab/controller/dashboard/domain/DashboardCardStateTest.java b/controller/app/src/test/java/org/iiab/controller/dashboard/domain/DashboardCardStateTest.java new file mode 100644 index 000000000..424376aa5 --- /dev/null +++ b/controller/app/src/test/java/org/iiab/controller/dashboard/domain/DashboardCardStateTest.java @@ -0,0 +1,78 @@ +package org.iiab.controller.dashboard.domain; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.iiab.controller.dashboard.domain.DashboardCardState.Kind; +import org.junit.Test; + +/** Pure-JVM tests for the Dashboard card's update-affordance rule (ADFA-5339). */ +public class DashboardCardStateTest { + + @Test public void offlineOverridesEverythingAndOffersNoUpdate() { + // Even with a cached "update available", offline shows No connection — a rebuild can't fetch. + DashboardCardState s = DashboardCardState.resolve( + false, false, false, "1.2.7", null, true, true); + assertEquals(Kind.OFFLINE, s.kind()); + assertTrue(s.isOffline()); + assertFalse(s.primaryIsUpdate()); + assertFalse("no version arrow while offline", s.showsVersionArrow()); + } + + @Test public void liveUpdateAvailableIsUpdateWithTheArrow() { + DashboardCardState s = DashboardCardState.resolve( + true, true, true, "1.2.7", "1.3.0", false, false); + assertEquals(Kind.UPDATE_AVAILABLE, s.kind()); + assertTrue(s.primaryIsUpdate()); + assertTrue(s.showsVersionArrow()); + assertEquals("1.3.0", s.targetVersion()); + assertEquals("1.2.7", s.installedVersion()); + } + + @Test public void liveUpToDateIsRebuildNoArrow() { + DashboardCardState s = DashboardCardState.resolve( + true, true, false, "1.3.0", "1.3.0", false, false); + assertEquals(Kind.UP_TO_DATE, s.kind()); + assertFalse(s.primaryIsUpdate()); + assertFalse(s.showsVersionArrow()); + assertNull(s.targetVersion()); + } + + @Test public void onlineButCheckFailedFallsBackToCache() { + DashboardCardState up = DashboardCardState.resolve( + true, false, false, "1.2.7", null, true, true); + assertEquals(Kind.UPDATE_AVAILABLE, up.kind()); + // The cache holds only the boolean, so a cached update has no target version to arrow to. + assertFalse("cached update has no target version", up.showsVersionArrow()); + assertNull(up.targetVersion()); + + DashboardCardState latest = DashboardCardState.resolve( + true, false, false, "1.3.0", null, true, false); + assertEquals(Kind.UP_TO_DATE, latest.kind()); + } + + @Test public void onlineNoResultNoCacheIsChecking() { + DashboardCardState s = DashboardCardState.resolve( + true, false, false, null, null, false, false); + assertEquals(Kind.CHECKING, s.kind()); + assertFalse(s.primaryIsUpdate()); + assertFalse(s.isOffline()); + } + + @Test public void sameVersionNeverArrows() { + // A malformed "update available" whose target equals installed must not draw "v1.3.0 → v1.3.0". + DashboardCardState s = DashboardCardState.resolve( + true, true, true, "1.3.0", "1.3.0", false, false); + assertEquals(Kind.UPDATE_AVAILABLE, s.kind()); + assertFalse(s.showsVersionArrow()); + } + + @Test public void missingVersionsDoNotArrow() { + DashboardCardState s = DashboardCardState.resolve( + true, true, true, "", "1.3.0", false, false); + assertTrue(s.primaryIsUpdate()); + assertFalse("no installed version -> no arrow", s.showsVersionArrow()); + } +}