From 2c26c2c3eef89176fbb3e9758b2f8f0261646feb Mon Sep 17 00:00:00 2001 From: Luis Guzman Date: Tue, 25 Aug 2026 15:11:31 -0600 Subject: [PATCH 1/8] ADFA-4893 feat(zim): pause/resume + cancel on the download status screen App-side wiring of the engine's existing pause/resume for the ZIM stream: RestContentClient (pause/resume + 'paused' poll + onPaused default), ZimDownloadService (session pause; the sequential queue holds while paused), ZimPreparingFragment (Pause/Resume + Cancel row on the status screen). Reuses ADFA-5119 strings. Server side already supports it (kiwix, 4894). --- .../controller/content/RestContentClient.java | 31 ++++++++++++- .../redesign/ZimDownloadService.java | 45 +++++++++++++++++-- .../redesign/ZimPreparingFragment.java | 28 ++++++++++++ .../layout/fragment_k2go_zim_preparing.xml | 33 ++++++++++++++ 4 files changed, 133 insertions(+), 4 deletions(-) diff --git a/controller/app/src/main/java/org/iiab/controller/content/RestContentClient.java b/controller/app/src/main/java/org/iiab/controller/content/RestContentClient.java index 6bb620eb0..bbe1b85a3 100644 --- a/controller/app/src/main/java/org/iiab/controller/content/RestContentClient.java +++ b/controller/app/src/main/java/org/iiab/controller/content/RestContentClient.java @@ -19,7 +19,8 @@ * speed(bytes/s), * detail, error } * POST /api/kiwix/jobs/:id/cancel -> { ok:true } - * phase is one of: queued|downloading|indexing|processing|done|error|canceled. + * POST /api/kiwix/jobs/:id/pause|resume -> { ok:true } (ADFA-4893) + * phase is one of: queued|downloading|indexing|processing|paused|done|error|canceled. * ============================================================================ */ package org.iiab.controller.content; @@ -51,6 +52,8 @@ public interface Listener { void onLog(String line); // free-form detail line (for logs) void onDone(); // success (terminal) void onError(String message); // failure (terminal) + /** ADFA-4893: 'paused' phase (best-effort). Default no-op so existing implementers compile unchanged. */ + default void onPaused(int percent) {} } private static final String BASE = BoxEndpoints.API + "/kiwix"; @@ -116,6 +119,12 @@ private void pollOnce() { case "canceled": fail("canceled"); return; + case "paused": { + // ADFA-4893: not terminal — surface it and keep polling so resume/progress is seen. + final int pp = percent; + deliver(() -> listener.onPaused(pp)); + break; + } default: break; // queued / unknown: keep polling } @@ -137,6 +146,26 @@ public void cancel() { teardown(); } + /** ADFA-4893: best-effort pause of the in-flight job. Keeps polling so 'paused' is observed and resume works. */ + public void pause() { + final String id = jobId; + if (id != null && !id.isEmpty()) { + AppExecutors.get().io().execute(() -> { + try { httpJson("POST", BASE + "/jobs/" + id + "/pause", null); } catch (Exception ignore) { /* best effort */ } + }); + } + } + + /** ADFA-4893: best-effort resume of a paused job; polling continues, so progress flows again. */ + public void resume() { + final String id = jobId; + if (id != null && !id.isEmpty()) { + AppExecutors.get().io().execute(() -> { + try { httpJson("POST", BASE + "/jobs/" + id + "/resume", null); } catch (Exception ignore) { /* best effort */ } + }); + } + } + private void done() { if (finished) return; finished = true; diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/ZimDownloadService.java b/controller/app/src/main/java/org/iiab/controller/redesign/ZimDownloadService.java index ea606dca7..40bf77d39 100644 --- a/controller/app/src/main/java/org/iiab/controller/redesign/ZimDownloadService.java +++ b/controller/app/src/main/java/org/iiab/controller/redesign/ZimDownloadService.java @@ -41,6 +41,8 @@ public final class ZimDownloadService extends Service { public static final String ACTION_START = "org.iiab.controller.ZIM_DOWNLOAD_START"; public static final String ACTION_RETRY = "org.iiab.controller.ZIM_DOWNLOAD_RETRY"; public static final String ACTION_CANCEL = "org.iiab.controller.ZIM_DOWNLOAD_CANCEL"; + public static final String ACTION_PAUSE = "org.iiab.controller.ZIM_DOWNLOAD_PAUSE"; // ADFA-4893 + public static final String ACTION_RESUME = "org.iiab.controller.ZIM_DOWNLOAD_RESUME"; // ADFA-4893 public static final String EXTRA_FILES = "files"; public static final String EXTRA_LABELS = "labels"; public static final String EXTRA_BYTES = "bytes"; @@ -59,9 +61,11 @@ public interface Listener { void onUpdate(); } private static int sPercent = 0; private static long sSpeed = 0; private static volatile long sLastProgressAt = 0L; // ADFA-5146: last-progress heartbeat (elapsedRealtime) + private static volatile boolean sPaused = false; // ADFA-4893: active job paused on the server (session-level) private static Listener sListener; public static boolean isRunning() { return sRunning; } + public static boolean isPaused() { return sPaused; } public static boolean hasSession() { return sFiles.length > 0; } /** All items are terminal (done/failed) and nothing is in flight. */ public static boolean isComplete() { @@ -101,10 +105,21 @@ public static void retry(Context ctx, int i) { } } + /** ADFA-4893: pause the active job (the server keeps the partial). No-op if nothing is running. */ + public static void pause(Context ctx) { + if (!sRunning) return; + ContextCompat.startForegroundService(ctx, new Intent(ctx, ZimDownloadService.class).setAction(ACTION_PAUSE)); + } + + /** ADFA-4893: resume a paused job; polling/progress continues from the partial. */ + public static void resume(Context ctx) { + ContextCompat.startForegroundService(ctx, new Intent(ctx, ZimDownloadService.class).setAction(ACTION_RESUME)); + } + /** Clear the session so a new selection can start fresh. */ public static void finishSession() { sFiles = new String[0]; sLabels = new String[0]; sBytes = new long[0]; sStatus = new int[0]; - sIndex = 0; sPercent = 0; sSpeed = 0; sRunning = false; sLastProgressAt = 0L; + sIndex = 0; sPercent = 0; sSpeed = 0; sRunning = false; sPaused = false; sLastProgressAt = 0L; } private static final int MAX_ATTEMPTS = 3; // total tries per ZIM before marking it failed @@ -123,12 +138,28 @@ public int onStartCommand(Intent intent, int flags, int startId) { if (ACTION_CANCEL.equals(action)) { if (client != null) client.cancel(); - sRunning = false; + sRunning = false; sPaused = false; publish(); main.post(() -> { stopForeground(true); stopSelf(); }); return START_NOT_STICKY; } + // ADFA-4893: pause/resume the active job. Handled BEFORE the sRunning guard (the session is + // still "running" while paused). The queue never advances during a pause because the paused + // job never reaches onDone/onError, so processNext() isn't called. + if (ACTION_PAUSE.equals(action)) { + if (client != null) client.pause(); + sPaused = true; + publish(); + return START_NOT_STICKY; + } + if (ACTION_RESUME.equals(action)) { + if (client != null) client.resume(); + sPaused = false; + publish(); + return START_NOT_STICKY; + } + if (sRunning) return START_NOT_STICKY; if (ACTION_RETRY.equals(action)) { @@ -142,7 +173,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { if (sLabels == null) sLabels = sFiles; if (sBytes == null) sBytes = new long[sFiles.length]; sStatus = new int[sFiles.length]; - sIndex = 0; sPercent = 0; sSpeed = 0; + sIndex = 0; sPercent = 0; sSpeed = 0; sPaused = false; sLastProgressAt = android.os.SystemClock.elapsedRealtime(); // ADFA-5146: seed the heartbeat at session start } @@ -176,12 +207,20 @@ private void startItem(final int i) { client = new RestContentClient(); client.addZim(sFiles[i], new RestContentClient.Listener() { @Override public void onProgress(int percent, String speed) { + if (sPaused) sPaused = false; // ADFA-4893: progress means the server resumed sPercent = percent; sSpeed = parseRate(speed); sLastProgressAt = android.os.SystemClock.elapsedRealtime(); // ADFA-5146: heartbeat on each poll if (sStatus[i] != INDEXING) sStatus[i] = ACTIVE; publish(); updateNotification(sLabels[i]); } @Override public void onIndexing() { sStatus[i] = INDEXING; sLastProgressAt = android.os.SystemClock.elapsedRealtime(); publish(); } + @Override public void onPaused(int percent) { + // ADFA-4893: server acknowledged the pause. Keep the heartbeat fresh (a pause is + // intentional, not a dead session) and let the screen render the paused state. + sPercent = percent; sPaused = true; + sLastProgressAt = android.os.SystemClock.elapsedRealtime(); + publish(); + } @Override public void onLog(String line) { /* logcat only */ } @Override public void onDone() { android.util.Log.i("K2Go-Provision", "zim job done [" + i + "]"); sStatus[i] = DONE; publish(); processNext(); } @Override public void onError(String message) { diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/ZimPreparingFragment.java b/controller/app/src/main/java/org/iiab/controller/redesign/ZimPreparingFragment.java index 76c13de55..2edd9979d 100644 --- a/controller/app/src/main/java/org/iiab/controller/redesign/ZimPreparingFragment.java +++ b/controller/app/src/main/java/org/iiab/controller/redesign/ZimPreparingFragment.java @@ -12,16 +12,19 @@ */ package org.iiab.controller.redesign; +import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import java.util.Locale; @@ -33,6 +36,8 @@ public class ZimPreparingFragment extends Fragment { private TextView label, pct, detail; private ProgressBar bar; private LinearLayout listv; + private LinearLayout controls; // ADFA-4893: session control row (pause/resume + cancel) + private Button pauseBtn, cancelBtn; /** * ADFA-5074: {@code newInstance(boolean fromIndex)} used to live here, and every caller now @@ -53,6 +58,20 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c detail = root.findViewById(R.id.k2go_zprep_detail); bar = root.findViewById(R.id.k2go_zprep_bar); listv = root.findViewById(R.id.k2go_zprep_list); + // ADFA-4893: session controls on the status screen. Poll (service) drives label + visibility. + controls = root.findViewById(R.id.k2go_zprep_controls); + pauseBtn = root.findViewById(R.id.k2go_zprep_pause); + cancelBtn = root.findViewById(R.id.k2go_zprep_cancel); + pauseBtn.setOnClickListener(v -> { + android.content.Context ctx = requireContext().getApplicationContext(); + if (ZimDownloadService.isPaused()) ZimDownloadService.resume(ctx); + else ZimDownloadService.pause(ctx); + }); + cancelBtn.setOnClickListener(v -> { + android.content.Context ctx = requireContext().getApplicationContext(); + ContextCompat.startForegroundService(ctx, + new Intent(ctx, ZimDownloadService.class).setAction(ZimDownloadService.ACTION_CANCEL)); + }); // ADFA-5074: the shared animation block declares no asset; the rule picks it. org.iiab.controller.util.ProgressVisuals.apply(root, org.iiab.controller.system.domain.ContentType.ZIM); @@ -98,6 +117,15 @@ private void render() { detail.setText(getString(R.string.k2go_zim_prep_detail_fmt, gb(doneBytes / (1024L * 1024L)), gb(totalBytes / (1024L * 1024L)), speedPart, doneCount, n)); + // ADFA-4893: session controls — shown only while a job is in flight; poll is the source of truth. + boolean active = ZimDownloadService.isRunning() && !allDone; + controls.setVisibility(active ? View.VISIBLE : View.GONE); + if (active) { + boolean paused = ZimDownloadService.isPaused(); + pauseBtn.setText(paused ? R.string.k2go_dl_resume : R.string.k2go_dl_pause); + if (paused) label.setText(R.string.k2go_dl_paused); + } + drawChecklist(labels, status); } diff --git a/controller/app/src/main/res/layout/fragment_k2go_zim_preparing.xml b/controller/app/src/main/res/layout/fragment_k2go_zim_preparing.xml index 399a0e987..a010ead4a 100644 --- a/controller/app/src/main/res/layout/fragment_k2go_zim_preparing.xml +++ b/controller/app/src/main/res/layout/fragment_k2go_zim_preparing.xml @@ -75,6 +75,39 @@ android:textAppearance="?attr/textAppearanceBodySmall" android:textColor="@color/k2go_muted" /> + + + +