|
| 1 | +/* |
| 2 | + * ============================================================================ |
| 3 | + * Name : CodeAssetsRefresh.java |
| 4 | + * Author : AppDevForAll |
| 5 | + * Copyright : Copyright (c) 2026 AppDevForAll |
| 6 | + * Description : K2GO-437. The single "Update build assets" flow, shared by the module detail button and |
| 7 | + * the module action sheet row so neither duplicates it. Modeled on AddonsRefresh: it gates |
| 8 | + * like the dashboard update (needs internet, then metered consent), shows minimal inline |
| 9 | + * progress (a description, a live one-line output tail, an indeterminate bar and a Cancel) |
| 10 | + * injected right after the trigger view, runs the box refresh on an IO thread, and reports |
| 11 | + * the outcome in a snackbar. |
| 12 | + * |
| 13 | + * Lifecycle: there is NO persistent app-side state. The box refresh job is detached |
| 14 | + * (setsid), so a host that goes away mid-run just drops the UI updates (guarded by |
| 15 | + * View.isAttachedToWindow()); the box finishes on its own and the tree is swapped in only |
| 16 | + * on success. The only state is the box's own status/pid files, which the box manages. |
| 17 | + * ============================================================================ |
| 18 | + */ |
| 19 | +package org.appdevforall.k2go.codeassets.presentation; |
| 20 | + |
| 21 | +import android.app.Activity; |
| 22 | +import android.content.Context; |
| 23 | +import android.os.Handler; |
| 24 | +import android.os.Looper; |
| 25 | +import android.text.TextUtils; |
| 26 | +import android.view.Gravity; |
| 27 | +import android.view.View; |
| 28 | +import android.view.ViewGroup; |
| 29 | +import android.widget.LinearLayout; |
| 30 | +import android.widget.TextView; |
| 31 | + |
| 32 | +import androidx.annotation.NonNull; |
| 33 | +import androidx.core.content.ContextCompat; |
| 34 | + |
| 35 | +import com.google.android.material.progressindicator.LinearProgressIndicator; |
| 36 | + |
| 37 | +import org.appdevforall.k2go.R; |
| 38 | +import org.appdevforall.k2go.codeassets.data.CodeAssetsRefreshClient; |
| 39 | +import org.appdevforall.k2go.util.AppExecutors; |
| 40 | +import org.appdevforall.k2go.util.Snackbars; |
| 41 | + |
| 42 | +public final class CodeAssetsRefresh { |
| 43 | + |
| 44 | + private CodeAssetsRefresh() {} |
| 45 | + |
| 46 | + /** |
| 47 | + * Gate (internet, then metered consent) then run the refresh with progress injected right after |
| 48 | + * {@code trigger}. The trigger stays in place (only disabled) as a visible anchor for the snackbar. |
| 49 | + */ |
| 50 | + public static void start(@NonNull Activity act, @NonNull View trigger) { |
| 51 | + if (!org.appdevforall.k2go.networkpolicy.data.AndroidNetworkClassifier.hasInternet(act)) { |
| 52 | + Snackbars.make(trigger, act.getString(R.string.k2go_dash_needs_internet)).show(); |
| 53 | + return; |
| 54 | + } |
| 55 | + // Same metered gate as the dashboard update: the box downloads the build assets over the device network. |
| 56 | + org.appdevforall.k2go.networkpolicy.presentation.NetworkPolicyGate.guardHeavyStart(act, () -> run(trigger)); |
| 57 | + } |
| 58 | + |
| 59 | + private static void run(@NonNull View trigger) { |
| 60 | + final ViewGroup parent = (ViewGroup) trigger.getParent(); |
| 61 | + if (parent == null || !trigger.isAttachedToWindow()) return; // host went away during the gate |
| 62 | + final Context ctx = trigger.getContext(); |
| 63 | + final Handler main = new Handler(Looper.getMainLooper()); |
| 64 | + final float d = ctx.getResources().getDisplayMetrics().density; |
| 65 | + final int side = Math.round(20 * d); |
| 66 | + |
| 67 | + final LinearLayout progress = new LinearLayout(ctx); |
| 68 | + progress.setOrientation(LinearLayout.VERTICAL); |
| 69 | + LinearLayout.LayoutParams plp = new LinearLayout.LayoutParams( |
| 70 | + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 71 | + plp.leftMargin = side; plp.rightMargin = side; plp.topMargin = Math.round(8 * d); |
| 72 | + progress.setLayoutParams(plp); |
| 73 | + |
| 74 | + final TextView label = new TextView(ctx); |
| 75 | + label.setText(R.string.k2go_code_assets_updating); |
| 76 | + label.setTextAppearance(com.google.android.material.R.style.TextAppearance_Material3_BodySmall); |
| 77 | + label.setTextColor(ContextCompat.getColor(ctx, R.color.k2go_muted)); |
| 78 | + progress.addView(label); |
| 79 | + |
| 80 | + final TextView liveLine = new TextView(ctx); |
| 81 | + liveLine.setTextAppearance(com.google.android.material.R.style.TextAppearance_Material3_BodySmall); |
| 82 | + liveLine.setTextColor(ContextCompat.getColor(ctx, R.color.k2go_muted)); |
| 83 | + liveLine.setMaxLines(1); |
| 84 | + liveLine.setEllipsize(TextUtils.TruncateAt.END); |
| 85 | + LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams( |
| 86 | + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 87 | + llp.topMargin = Math.round(2 * d); |
| 88 | + liveLine.setLayoutParams(llp); |
| 89 | + progress.addView(liveLine); |
| 90 | + |
| 91 | + final LinearLayout barLine = new LinearLayout(ctx); |
| 92 | + barLine.setOrientation(LinearLayout.HORIZONTAL); |
| 93 | + barLine.setGravity(Gravity.CENTER_VERTICAL); |
| 94 | + LinearLayout.LayoutParams barLineLp = new LinearLayout.LayoutParams( |
| 95 | + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 96 | + barLineLp.topMargin = Math.round(4 * d); |
| 97 | + barLine.setLayoutParams(barLineLp); |
| 98 | + |
| 99 | + final LinearProgressIndicator bar = new LinearProgressIndicator(ctx); |
| 100 | + bar.setIndeterminate(true); |
| 101 | + LinearLayout.LayoutParams blp = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f); |
| 102 | + bar.setLayoutParams(blp); |
| 103 | + barLine.addView(bar); |
| 104 | + |
| 105 | + final TextView cancel = new TextView(ctx); |
| 106 | + cancel.setText(R.string.k2go_dash_cancel); |
| 107 | + cancel.setAllCaps(true); |
| 108 | + cancel.setTextAppearance(com.google.android.material.R.style.TextAppearance_Material3_LabelLarge); |
| 109 | + cancel.setTextColor(ContextCompat.getColor(ctx, R.color.k2go_teal)); |
| 110 | + int hp = Math.round(12 * d), vp = Math.round(6 * d); |
| 111 | + cancel.setPadding(hp, vp, hp, vp); |
| 112 | + barLine.addView(cancel); |
| 113 | + progress.addView(barLine); |
| 114 | + |
| 115 | + parent.addView(progress, parent.indexOfChild(trigger) + 1); |
| 116 | + trigger.setEnabled(false); // stays in place as an anchor; re-enabled when the refresh settles |
| 117 | + |
| 118 | + cancel.setOnClickListener(cv -> { |
| 119 | + cancel.setEnabled(false); |
| 120 | + label.setText(R.string.k2go_code_assets_update_cancelling); |
| 121 | + AppExecutors.get().io().execute(() -> new CodeAssetsRefreshClient().cancel()); |
| 122 | + }); |
| 123 | + |
| 124 | + AppExecutors.get().io().execute(() -> { |
| 125 | + final CodeAssetsRefreshClient client = new CodeAssetsRefreshClient(); |
| 126 | + final CodeAssetsRefreshClient.Result r = client.refresh(rawLine -> { |
| 127 | + final String shown = rawLine.trim(); |
| 128 | + main.post(() -> { if (liveLine.isAttachedToWindow()) liveLine.setText(shown); }); |
| 129 | + }); |
| 130 | + final int failed = client.lastFailed(); |
| 131 | + final boolean upToDate = client.lastUpToDate(); |
| 132 | + main.post(() -> { |
| 133 | + if (!trigger.isAttachedToWindow()) return; |
| 134 | + parent.removeView(progress); |
| 135 | + trigger.setEnabled(true); |
| 136 | + Snackbars.make(trigger, ctx.getString(messageFor(r, failed, upToDate))).show(); |
| 137 | + }); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + /** Map the refresh outcome to a user message covering every state. */ |
| 142 | + private static int messageFor(CodeAssetsRefreshClient.Result r, int failed, boolean upToDate) { |
| 143 | + if (r == CodeAssetsRefreshClient.Result.CANCELLED) return R.string.k2go_code_assets_update_cancelled; |
| 144 | + if (r != CodeAssetsRefreshClient.Result.DONE) return R.string.k2go_code_assets_update_failed; // box unreachable |
| 145 | + if (failed > 0) return R.string.k2go_code_assets_update_some_failed; // some files could not be fetched |
| 146 | + if (upToDate) return R.string.k2go_code_assets_update_none; |
| 147 | + return R.string.k2go_code_assets_update_done; |
| 148 | + } |
| 149 | +} |
0 commit comments