Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions controller/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@
android:value="Imports selected library content from a user-chosen drive through the co-located server REST engine." />
</service>

<service
android:name=".contentexchange.presentation.ContentDeleteService"
android:exported="false"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="Removes selected library content from the co-located server to reclaim device storage." />
</service>

<!-- ADFA-5333: runs the LIVE dash-node update in the background (notification only) instead of
behind a blocking modal. Same shape as the download services above: not exported, and
specialUse because it drives the in-server blue-green rebuild rather than a stock transfer. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public final class CxCoreDataSource {

private static final int CONNECT_TIMEOUT_MS = 5000;
private static final int READ_TIMEOUT_MS = 20000;
/** Delete blocks on the box's own delete (Kolibri deletechannel, Kiwix reindex), which can run
* well past the read-side timeout, so it waits longer than a plain read. */
private static final int DELETE_READ_TIMEOUT_MS = 120000;

/** {@code GET /v1/apps} -> {@code {"apps": [...]}}. */
public JSONObject getApps() throws Exception {
Expand Down Expand Up @@ -68,7 +71,11 @@ public JSONObject postImportItem(JSONObject body) throws Exception {
return postJson(V1 + "/import/item", body);
}

/** {@code DELETE /v1/content/{app}/{localRef}} -> {@code {ok, freedBytes}}. */
/**
* {@code DELETE /v1/content/{app}/{localRef}} -> {@code {ok, freedBytes}} on success or
* {@code {ok:false, reason}} on an adapter failure. Path segments are percent-encoded (the core
* decodes them). A 4xx/5xx (e.g. 404 for an item already gone) throws, like the other calls.
*/
public JSONObject deleteContent(String app, String localRef) throws Exception {
return deleteJson(V1 + "/content/" + encodeSegment(app) + "/" + encodeSegment(localRef));
}
Expand Down Expand Up @@ -132,7 +139,7 @@ private static JSONObject deleteJson(String urlStr) throws Exception {
try {
c.setUseCaches(false);
c.setConnectTimeout(CONNECT_TIMEOUT_MS);
c.setReadTimeout(READ_TIMEOUT_MS);
c.setReadTimeout(DELETE_READ_TIMEOUT_MS);
c.setRequestMethod("DELETE");
c.setRequestProperty("Accept", "application/json");
int code = c.getResponseCode();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* ============================================================================
* Name : CxCoreDeleteSource.java
* Author : AppDevForAll
* Copyright : Copyright (c) 2026 AppDevForAll
* Description : K2GO-427. Data-layer implementation of the delete source port against
* cx-core (through dash-node's /v1 relay): DELETE /v1/content/{app}/{localRef}.
* Maps the core JSON {ok, freedBytes} | {ok:false, reason} onto the domain
* DeleteResult, so the domain stays JSON-free.
* ============================================================================
*/
package org.appdevforall.k2go.contentexchange.data;

import org.appdevforall.k2go.contentexchange.domain.ContentDeleteSource;
import org.appdevforall.k2go.contentexchange.domain.DeleteResult;
import org.appdevforall.k2go.contentexchange.domain.ItemRef;
import org.json.JSONObject;

/**
* Deletes one item on the box and maps the response to {@link DeleteResult}.
*
* <p>Stateless and thread-confined to the delete worker. A transport failure (box unreachable, a
* 4xx/5xx) throws; a reached box that could not delete the item returns {@code {ok:false, reason}},
* which maps to {@link DeleteResult#failure}.
*/
public final class CxCoreDeleteSource implements ContentDeleteSource {

private final CxCoreDataSource dataSource;

public CxCoreDeleteSource(CxCoreDataSource dataSource) {
this.dataSource = dataSource;
}

@Override
public DeleteResult delete(ItemRef ref) throws Exception {
JSONObject response = dataSource.deleteContent(ref.app(), ref.localRef());
if (response.optBoolean("ok", false)) {
return DeleteResult.ok(response.optLong("freedBytes", 0L));
}
return DeleteResult.failure(response.optString("reason", response.optString("error", "")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.appdevforall.k2go.contentexchange.domain;

/**
* Domain port for the BOX side of a delete: remove one item from the box and report what it
* reclaimed. The Data layer implements it against the core REST API (through dash-node's
* {@code /v1/*} relay: {@code DELETE /v1/content/{app}/{localRef}}); the domain never learns the
* transport or the JSON shape.
*
* <p>The box owns the real deletion (it delegates to each app's own delete, never removes files
* blindly), so there is no client-side "is this deletable" rule to duplicate here: the item address
* is all this port needs. Must be called off the main thread.
*/
public interface ContentDeleteSource {

/** Delete one item on the box. Returns the outcome; throws only when the box is unreachable. */
DeleteResult delete(ItemRef ref) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package org.appdevforall.k2go.contentexchange.domain;

/**
* The pure state of one delete run: a per-item status plus the bytes each delete reclaimed, and the
* derived overall progress. The service mutates it as it drains the queue; the UI reads the derived
* figures. No Android, no IO: unit-testable on a plain JVM.
*
* <p>Delete is atomic per item (the box removes the item or it fails), so there is no within-item
* byte progress like the export copy has: overall progress is settled-item count, not a byte ratio.
* The item is the unit of resume (ADR "Resilience"): a failed item is re-queued and re-done on its
* own; the rest are untouched. The arrays are indexed by the original request order and set to one
* length in {@link #begin}, so the UI can never read a length mismatch.
*/
public final class DeleteJob {

public static final int PENDING = 0;
public static final int ACTIVE = 1;
public static final int DONE = 2;
public static final int FAILED = 3;

private int[] status = new int[0];
/** Bytes reclaimed per item, as the box reported for each successful delete. */
private long[] freed = new long[0];
private int index = -1;

/** Start a run over {@code count} items, all PENDING. */
public void begin(int count) {
int n = Math.max(0, count);
status = new int[n];
freed = new long[n];
index = -1;
}

public int size() {
return status.length;
}

public int status(int i) {
return i >= 0 && i < status.length ? status[i] : FAILED;
}

public long freed(int i) {
return i >= 0 && i < freed.length ? freed[i] : 0L;
}

/** The item currently being deleted, or -1 when none is active. */
public int index() {
return index;
}

/** The first item still waiting, or -1 when the queue is drained. */
public int firstPending() {
for (int i = 0; i < status.length; i++) {
if (status[i] == PENDING) {
return i;
}
}
return -1;
}

public void setActive(int i) {
if (i >= 0 && i < status.length) {
status[i] = ACTIVE;
index = i;
}
}

/** Mark item {@code i} deleted and record the bytes the box reclaimed for it. */
public void setDone(int i, long freedBytes) {
if (i >= 0 && i < status.length) {
status[i] = DONE;
freed[i] = Math.max(0L, freedBytes);
}
}

public void setFailed(int i) {
if (i >= 0 && i < status.length) {
status[i] = FAILED;
freed[i] = 0L;
}
}

/** Re-queue every failed item (reset its reclaimed figure) for a retry. Returns how many changed. */
public int requeueFailed() {
int changed = 0;
for (int i = 0; i < status.length; i++) {
if (status[i] == FAILED) {
status[i] = PENDING;
freed[i] = 0L;
changed++;
}
}
return changed;
}

public boolean hasFailed() {
for (int st : status) {
if (st == FAILED) {
return true;
}
}
return false;
}

public int doneCount() {
return count(DONE);
}

public int failedCount() {
return count(FAILED);
}

private int count(int target) {
int c = 0;
for (int st : status) {
if (st == target) {
c++;
}
}
return c;
}

/** No item is still pending or active (the run has settled; some may have failed). */
public boolean isSettled() {
if (status.length == 0) {
return false;
}
for (int st : status) {
if (st == PENDING || st == ACTIVE) {
return false;
}
}
return true;
}

/** Every item was deleted successfully. */
public boolean allDone() {
return status.length > 0 && doneCount() == status.length;
}

/** Total bytes reclaimed across the items deleted so far. */
public long freedTotal() {
long t = 0L;
for (long f : freed) {
t += f;
}
return t;
}

/** Overall percent by settled-item count (delete has no within-item byte progress). */
public int overallPercent() {
int n = status.length;
if (n == 0) {
return 0;
}
int settled = doneCount() + failedCount();
return (int) Math.min(100L, (long) settled * 100L / n);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.appdevforall.k2go.contentexchange.domain;

/**
* The outcome of deleting one item: success with the bytes the box reclaimed, or failure with a
* reason. Immutable value object. Pure domain type: the Data layer maps the core JSON
* {@code {ok, freedBytes} | {ok:false, reason}} onto it, so the domain never learns the JSON shape.
*/
public final class DeleteResult {

private final boolean ok;
private final long freedBytes;
private final String reason;

private DeleteResult(boolean ok, long freedBytes, String reason) {
this.ok = ok;
this.freedBytes = freedBytes;
this.reason = reason;
}

public static DeleteResult ok(long freedBytes) {
return new DeleteResult(true, Math.max(0L, freedBytes), "");
}

public static DeleteResult failure(String reason) {
return new DeleteResult(false, 0L, reason != null ? reason : "");
}

public boolean ok() {
return ok;
}

/** Bytes reclaimed by this delete (0 when it failed, or when the box reported no figure). */
public long freedBytes() {
return freedBytes;
}

public String reason() {
return reason;
}
}
Loading
Loading