diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentManagerFragment.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentManagerFragment.java index 497d15bae..6053b5516 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentManagerFragment.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentManagerFragment.java @@ -33,11 +33,7 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import com.journeyapps.barcodescanner.ScanContract; -import com.journeyapps.barcodescanner.ScanOptions; - import org.appdevforall.k2go.R; -import org.appdevforall.k2go.contentexchange.data.ShareHandshake; import org.appdevforall.k2go.contentexchange.domain.ContentGroup; import org.appdevforall.k2go.contentexchange.domain.ContentItem; import org.appdevforall.k2go.contentexchange.domain.ItemRef; @@ -77,9 +73,6 @@ public class ContentManagerFragment extends Fragment { private Button deleteButton; private Button shareButton; - /** The invite-QR scanner for the receive flow (same ZXing contract Clone uses). */ - private ActivityResultLauncher inviteScan; - /** The selection captured when Share was tapped, held across the hotspot-permission round-trip. */ private List pendingShare; /** Runtime hotspot permission (NEARBY_WIFI_DEVICES / ACCESS_FINE_LOCATION), requested before the @@ -115,7 +108,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c new ActivityResultContracts.OpenDocumentTree(), this::onImportTreePicked); hotspotPerm = registerForActivityResult( new ActivityResultContracts.RequestMultiplePermissions(), result -> onHotspotPermissionResult()); - inviteScan = registerForActivityResult(new ScanContract(), r -> onInviteScanned(r.getContents())); exportButton.setEnabled(true); exportButton.setOnClickListener(v -> onExportClicked()); importButton.setOnClickListener(v -> onImportClicked()); @@ -345,31 +337,11 @@ private void onReceiveClicked() { } // Tear our own hotspot down the moment Receive starts: the sender's AP IP (always 192.168.49.1) // collides with our own hotspot IP if we are hosting, which traps the connection locally. - // Stopping here lets the interface release while the user scans; ContentReceiveFragment also - // waits for the release before it connects. K2GO-376. + // Stopping here lets the interface release while the user joins and scans; ContentReceiveFragment + // also waits for the release before it connects. K2GO-376. LocalHotspotManager.get().stop(); - ScanOptions o = new ScanOptions(); - o.setDesiredBarcodeFormats(ScanOptions.QR_CODE); - o.setPrompt(getString(R.string.k2go_cx_receive_scan_prompt)); - o.setBeepEnabled(false); - o.setBarcodeImageEnabled(false); - o.setOrientationLocked(false); - inviteScan.launch(o); - } - - /** The scanner returned. Null means the user cancelled; a non-invite or invalid QR is rejected. */ - private void onInviteScanned(@Nullable String contents) { - if (!isAdded() || contents == null) { - return; - } - ShareHandshake.ShareInvite invite = ShareHandshake.decode(contents); - if (invite == null) { - String msg = getString(R.string.k2go_cx_receive_invalid); - Snackbar.make(requireView(), msg, SnackbarDuration.millisForText(msg)).show(); - return; - } requireActivity().getSupportFragmentManager().beginTransaction() - .replace(R.id.k2go_setup_host, ContentReceiveFragment.newInstance(invite)) + .replace(R.id.k2go_setup_host, new ContentReceiveStartFragment()) .addToBackStack(null) .commit(); } @@ -501,10 +473,12 @@ private void onHotspotPermissionResult() { private void startShare(List selected) { ItemRef[] refs = new ItemRef[selected.size()]; + long bytes = 0; for (int i = 0; i < selected.size(); i++) { refs[i] = new ItemRef(selected.get(i).app(), selected.get(i).localRef()); + bytes += selected.get(i).bytes(); } - ContentShareService.start(requireContext(), refs); + ContentShareService.start(requireContext(), refs, bytes); pendingShare = null; viewModel.clearSelection(); showShareProgress(); diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentReceiveStartFragment.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentReceiveStartFragment.java new file mode 100644 index 000000000..638f65bb3 --- /dev/null +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentReceiveStartFragment.java @@ -0,0 +1,90 @@ +/* + * ============================================================================ + * Name : ContentReceiveStartFragment.java + * Author : AppDevForAll + * Copyright : Copyright (c) 2026 AppDevForAll + * Description : Receive over Wi-Fi, start screen (modelled on Clone's Receive): join the sender's + * Wi-Fi, then scan the invite or enter it as text. A valid invite opens + * ContentReceiveFragment. + * ============================================================================ + */ +package org.appdevforall.k2go.contentexchange.presentation; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.activity.result.ActivityResultLauncher; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; + +import com.journeyapps.barcodescanner.ScanContract; +import com.journeyapps.barcodescanner.ScanOptions; + +import org.appdevforall.k2go.R; +import org.appdevforall.k2go.contentexchange.data.ShareHandshake; +import org.appdevforall.k2go.redesign.CodeText; +import org.appdevforall.k2go.redesign.QrSection; +import org.appdevforall.k2go.util.Snackbars; + +public class ContentReceiveStartFragment extends Fragment { + + private static final String STATE_PASTE_OPEN = "pasteOpen"; + + private CodeText.PasteBlock pasteCode; + private ActivityResultLauncher inviteScan; + + @Override + public void onCreate(@Nullable Bundle s) { + super.onCreate(s); + inviteScan = registerForActivityResult(new ScanContract(), r -> onInvite(r.getContents())); + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle s) { + View root = inflater.inflate(R.layout.fragment_k2go_content_receive_start, container, false); + + QrSection.setStep(root.findViewById(R.id.k2go_cxst_join_hdr), "1", getString(R.string.k2go_cx_share_step_join)); + QrSection.setStep(root.findViewById(R.id.k2go_cxst_receive_hdr), "2", + getString(R.string.k2go_cx_share_step_receive)); + root.findViewById(R.id.k2go_cxst_wifi).setOnClickListener(v -> { + if (!CodeText.openWifiSettings(requireContext())) { + Snackbars.make(v, R.string.k2go_clone_toast_open_wifi).show(); + } + }); + root.findViewById(R.id.k2go_cxst_scan).setOnClickListener( + v -> inviteScan.launch(CodeText.scanOptions(getString(R.string.k2go_cx_receive_scan_prompt)))); + + pasteCode = new CodeText.PasteBlock(root, this::onInvite); + pasteCode.setExpanded(s != null && s.getBoolean(STATE_PASTE_OPEN)); + pasteCode.show(true); + return root; + } + + @Override + public void onSaveInstanceState(@NonNull Bundle out) { + super.onSaveInstanceState(out); + if (pasteCode != null) { + out.putBoolean(STATE_PASTE_OPEN, pasteCode.isExpanded()); + } + } + + /** A scanned or pasted invite. Null means the scan was cancelled; an invalid code is rejected. */ + private void onInvite(@Nullable String contents) { + if (!isAdded() || contents == null) { + return; + } + ShareHandshake.ShareInvite invite = ShareHandshake.decode(contents); + if (invite == null) { + Snackbars.make(requireView(), R.string.k2go_cx_receive_invalid).show(); + return; + } + requireActivity().getSupportFragmentManager().beginTransaction() + .replace(R.id.k2go_setup_host, ContentReceiveFragment.newInstance(invite)) + .addToBackStack(null) + .commit(); + } +} diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentShareFragment.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentShareFragment.java index 7e3aa75eb..9aff9926f 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentShareFragment.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentShareFragment.java @@ -6,7 +6,7 @@ * Description : K2GO-376. Shows the Share invite: two stacked QR sections (join the hotspot, then * scan to receive) once the sender foreground service has opened the share session and * the hotspot is up. Reads state only (the ShareSession + the shared LocalHotspotManager - * state); all work is the service's. Done closes the gate + the hotspot via the service. + * state); all work is the service's. Done or Back closes the gate + the hotspot via the service. * ============================================================================ */ package org.appdevforall.k2go.contentexchange.presentation; @@ -18,6 +18,7 @@ import android.widget.Button; import android.widget.TextView; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; @@ -28,9 +29,12 @@ import org.appdevforall.k2go.hotspot.HotspotPermissions; import org.appdevforall.k2go.hotspot.LocalHotspotManager; import org.appdevforall.k2go.hotspot.data.ApAddressResolver; +import org.appdevforall.k2go.redesign.CodeText; import org.appdevforall.k2go.redesign.QrSection; import org.appdevforall.k2go.redesign.SegmentedTabs; import org.appdevforall.k2go.sync.transport.NetworkStateLiveData; +import org.appdevforall.k2go.ui.dialog.BrandDialog; +import org.appdevforall.k2go.util.ByteFormatter; public class ContentShareFragment extends Fragment { @@ -41,6 +45,11 @@ public class ContentShareFragment extends Fragment { private View receiveRoot; private QrSection secJoin; private QrSection secReceive; + private View codeLabel; + private View codeBlock; + private TextView codeText; + /** The invite shown as text; Copy / Share hand out this exact payload. */ + private String invitePayload = ""; private View transportRow; private TextView transportHotspot; private TextView transportWifi; @@ -72,10 +81,13 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c R.id.sec_caption, R.id.sec_subcaption, R.id.sec_fallback_toggle, R.id.sec_fallback, R.id.sec_fallback_values); secReceive = new QrSection(receiveRoot, R.id.sec_qr_frame, R.id.sec_qr, R.id.sec_qr_ph, R.id.sec_caption, R.id.sec_subcaption, R.id.sec_fallback_toggle, R.id.sec_fallback, R.id.sec_fallback_values); - ((TextView) joinRoot.findViewById(R.id.sec_badge)).setText("1"); - ((TextView) joinRoot.findViewById(R.id.sec_title)).setText(R.string.k2go_cx_share_step_join); - ((TextView) receiveRoot.findViewById(R.id.sec_badge)).setText("2"); - ((TextView) receiveRoot.findViewById(R.id.sec_title)).setText(R.string.k2go_cx_share_step_receive); + QrSection.setStep(joinRoot, "1", getString(R.string.k2go_cx_share_step_join)); + + codeLabel = root.findViewById(R.id.k2go_code_label); + codeBlock = root.findViewById(R.id.k2go_code_block); + codeText = root.findViewById(R.id.k2go_code_text); + root.findViewById(R.id.k2go_code_copy).setOnClickListener(v -> CodeText.copy(v, invitePayload)); + root.findViewById(R.id.k2go_code_share).setOnClickListener(v -> CodeText.share(requireContext(), invitePayload)); transportRow = root.findViewById(R.id.k2go_cxs_transport); transportHotspot = root.findViewById(R.id.k2go_cxs_transport_hotspot); @@ -83,10 +95,27 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c transportHotspot.setOnClickListener(v -> selectTransport(false)); transportWifi.setOnClickListener(v -> selectTransport(true)); - done.setOnClickListener(v -> { - ContentShareService.cancel(requireContext()); - goBack(); - }); + done.setOnClickListener(v -> stopAndLeave()); + // Back ends the share like Done (a share left running would make the next Share reopen it + // instead of the new selection), but asks first, as Clone does: Back is easy to hit mid-transfer. + requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), + new OnBackPressedCallback(true) { + @Override + public void handleOnBackPressed() { + if (session.hasError()) { + stopAndLeave(); // nothing is being sent + return; + } + new BrandDialog(requireContext()) + .setTitle(R.string.k2go_clone_stopshare_title) + // Own message, not Clone's "can start again": resume is not + // guaranteed for every app yet (K2GO-577 covers ZIM only). + .setMessage(R.string.k2go_cx_share_stop_msg) + .setNegative(R.string.k2go_cancel, null) + .setDestructive(R.string.k2go_clone_stop_confirm, () -> stopAndLeave()) + .show(); + } + }); // The AP IPv4 lands a beat after the hotspot reports ON; re-render when it (or the phase) changes // so the receive QR appears the moment every piece is ready. Observed once, tied to the view. hs.state().observe(getViewLifecycleOwner(), st -> render()); @@ -137,7 +166,9 @@ private void render() { goBack(); return; } - subtitle.setText(getString(R.string.k2go_cx_share_subtitle_fmt, session.itemCount())); + // Same "N selected : size" line Content manager showed before Share. + subtitle.setText(getString(R.string.k2go_cx_selected_fmt, session.itemCount(), + ByteFormatter.humanBytes(session.totalBytes()))); if (session.hasError()) { showStatus(session.errorMessage() != null @@ -179,6 +210,7 @@ private void renderInvite() { } transportRow.setVisibility(View.GONE); waiting.setVisibility(View.GONE); + showInviteText(null); return; } @@ -201,6 +233,7 @@ private void renderInvite() { SegmentedTabs.paint(transportWifi, useWifiAddress); } joinRoot.setVisibility(both && useWifiAddress ? View.GONE : View.VISIBLE); + numberSteps(); String ip = both ? (useWifiAddress ? wifiIp : hotspotIp) : (hotspotIp != null ? hotspotIp : wifiIp); @@ -212,7 +245,6 @@ private void renderInvite() { getString(R.string.k2go_fallback_wifi, ssid), getString(R.string.k2go_fallback_pass, pass)}); // Section 2: the share invite QR. Needs the AP IPv4, which resolves a beat after ON. - secReceive.caption.setText(R.string.k2go_cx_share_receive_caption); secReceive.subCaption.setText(""); // The core returns its own path (/v1/share//manifest); the receiver reaches it through // the off-box gate prefix, so the invite carries the prefixed path. @@ -223,6 +255,7 @@ private void renderInvite() { if (invite == null) { // No dead QR at a guessed address; hold the slot until the real AP IP arrives (render reruns). secReceive.setQrPending(requireContext(), getString(R.string.k2go_cx_share_resolving_address)); + secReceive.caption.setText(""); // no "Ready! Scan" until there is a QR to scan secReceive.setFallback(requireContext(), null); waiting.setVisibility(View.GONE); } else { @@ -230,6 +263,16 @@ private void renderInvite() { secReceive.setFallback(requireContext(), null); waiting.setVisibility(View.VISIBLE); } + showInviteText(invite); + } + + /** Show the invite as copyable text under the QR, or hide the block when there is no invite. */ + private void showInviteText(@Nullable String invite) { + invitePayload = invite != null ? invite : ""; + codeText.setText(invitePayload); + int vis = invite != null ? View.VISIBLE : View.GONE; + codeLabel.setVisibility(vis); + codeBlock.setVisibility(vis); } /** Pick the address the receive QR carries and repaint the strip (Connect/Clone mode pattern). */ @@ -251,12 +294,27 @@ private void showStatus(String text) { receiveRoot.setVisibility(View.GONE); transportRow.setVisibility(View.GONE); waiting.setVisibility(View.GONE); + showInviteText(null); } private void showSections() { status.setVisibility(View.GONE); joinRoot.setVisibility(View.VISIBLE); receiveRoot.setVisibility(View.VISIBLE); + numberSteps(); + } + + /** Number the steps the user sees: with the join step hidden (Wi-Fi transport), the invite is 1. + * The caption is Clone's one-code / two-code wording. */ + private void numberSteps() { + boolean joinShown = joinRoot.getVisibility() == View.VISIBLE; + QrSection.setStep(receiveRoot, joinShown ? "2" : "1", getString(R.string.k2go_cx_action_share)); + secReceive.caption.setText(joinShown ? R.string.k2go_clone_ready_scan2 : R.string.k2go_clone_ready_scan); + } + + private void stopAndLeave() { + ContentShareService.cancel(requireContext()); + goBack(); } /** diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentShareService.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentShareService.java index 5da4e76a0..f42e2a189 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentShareService.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentShareService.java @@ -49,6 +49,7 @@ public final class ContentShareService extends Service { public static final String EXTRA_APPS = "apps"; public static final String EXTRA_LOCAL_REFS = "localRefs"; + public static final String EXTRA_TOTAL_BYTES = "totalBytes"; private static final String CHANNEL_ID = "content_share_channel"; private static final int NOTIFICATION_ID = NotificationIds.CONTENT_SHARE; @@ -70,7 +71,7 @@ static ShareSession session() { // ---- static control surface (the fragment calls these) ------------------------------------- - public static void start(Context ctx, ItemRef[] refs) { + public static void start(Context ctx, ItemRef[] refs, long totalBytes) { String[] apps = new String[refs.length]; String[] locals = new String[refs.length]; for (int i = 0; i < refs.length; i++) { @@ -79,7 +80,8 @@ public static void start(Context ctx, ItemRef[] refs) { } Intent i = new Intent(ctx, ContentShareService.class).setAction(ACTION_START) .putExtra(EXTRA_APPS, apps) - .putExtra(EXTRA_LOCAL_REFS, locals); + .putExtra(EXTRA_LOCAL_REFS, locals) + .putExtra(EXTRA_TOTAL_BYTES, totalBytes); ContextCompat.startForegroundService(ctx, i); } @@ -137,7 +139,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { stopSelf(); return START_NOT_STICKY; } - SESSION.beginPreparing(refs.length); + SESSION.beginPreparing(refs.length, intent.getLongExtra(EXTRA_TOTAL_BYTES, 0L)); startForeground(NOTIFICATION_ID, buildNotification()); acquireHardwareLocks(); ensureHotspot(); diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ShareSession.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ShareSession.java index 17837dada..5255f36f3 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ShareSession.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ShareSession.java @@ -31,6 +31,7 @@ enum Phase { PREPARING, READY, ERROR } private volatile boolean active = false; // a share run exists (set until clear) private volatile Phase phase = Phase.PREPARING; private int itemCount; + private long totalBytes; private String sessionId; private String token; private String manifestUrl; @@ -42,9 +43,11 @@ void setListener(Listener l) { // ---- service-side mutation ----------------------------------------------------------------- - /** Start a run: the item count is fixed now; the invite arrives once conex-core opens the session. */ - void beginPreparing(int itemCount) { + /** Start a run: the item count and total size are fixed now; the invite arrives once conex-core + * opens the session. */ + void beginPreparing(int itemCount, long totalBytes) { this.itemCount = itemCount; + this.totalBytes = totalBytes; this.sessionId = null; this.token = null; this.manifestUrl = null; @@ -73,6 +76,7 @@ void clear() { active = false; phase = Phase.PREPARING; itemCount = 0; + totalBytes = 0; sessionId = null; token = null; manifestUrl = null; @@ -110,6 +114,10 @@ int itemCount() { return itemCount; } + long totalBytes() { + return totalBytes; + } + String sessionId() { return sessionId; } diff --git a/controller/app/src/main/java/org/appdevforall/k2go/redesign/CloneFragment.java b/controller/app/src/main/java/org/appdevforall/k2go/redesign/CloneFragment.java index 760e3f791..1435fa139 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/redesign/CloneFragment.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/redesign/CloneFragment.java @@ -1,7 +1,5 @@ package org.appdevforall.k2go.redesign; -import android.content.ClipData; -import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; import android.graphics.Color; @@ -15,7 +13,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; -import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; @@ -140,8 +137,7 @@ private enum Page { PREPARE, COPY } // Receive side private SyncStateViewModel syncVm; private LinearLayout receiveBox, progressBox; - private EditText paste; - private TextView receiveStart, pStatus, pFile, pStats, cancel; + private TextView pStatus, pFile, pStats, cancel; private LinearProgressIndicator pbar; // ADFA-5143: typed as itself, so setProgressCompat is available private com.airbnb.lottie.LottieAnimationView anim; // ADFA-5143: the clone loop // ADFA-5143: read once — renderReceive() runs on every progress tick and this is a Settings read. @@ -151,10 +147,10 @@ private enum Page { PREPARE, COPY } private TextView confirmSys, confirmContent, confirmTotal; private enum RStage { JOIN, START } private RStage rStage = RStage.JOIN; - private boolean pasteExpanded = false; + private CodeText.PasteBlock pasteCode; private ActivityResultLauncher barcodeLauncher; - private LinearLayout rcvSteps, rcvIntro, rcvNotice, pasteBlock; - private TextView rcvCaption, rcvScan, rcvSub, rcvSkip, rcvSkipHint, rcvCamNote, rcvShowPaste; + private LinearLayout rcvSteps, rcvIntro, rcvNotice; + private TextView rcvCaption, rcvScan, rcvSub, rcvSkip, rcvSkipHint, rcvCamNote; // ADFA-4784: incompatibility hard-block state (receiver). >=0 means "showing not-compatible" // for a scanned/pasted code advertising that host architecture. private LinearLayout rcvIncompat; @@ -211,8 +207,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c secJoinBadge = joinRoot.findViewById(R.id.sec_badge); secJoinTitle = joinRoot.findViewById(R.id.sec_title); secJoinBadge.setText("1"); - ((TextView) getAppRoot.findViewById(R.id.sec_badge)).setText("2"); - ((TextView) getAppRoot.findViewById(R.id.sec_title)).setText(getString(R.string.k2go_badge_getapp)); + QrSection.setStep(getAppRoot, "2", getString(R.string.k2go_badge_getapp)); advance = v.findViewById(R.id.k2go_clone_advance); stop = v.findViewById(R.id.k2go_clone_stop); actionFooter = v.findViewById(R.id.k2go_clone_action_footer); // ADFA-5154: pinned action button @@ -224,8 +219,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c sizeContent = v.findViewById(R.id.k2go_clone_size_content); sizeTotal = v.findViewById(R.id.k2go_clone_size_total); receiveBox = v.findViewById(R.id.k2go_clone_receive_box); - paste = v.findViewById(R.id.k2go_clone_paste); - receiveStart = v.findViewById(R.id.k2go_clone_receive_start); + pasteCode = new CodeText.PasteBlock(v, this::onReceiveStart); rcvSteps = v.findViewById(R.id.k2go_rcv_steps); rcvCaption = v.findViewById(R.id.k2go_rcv_caption); rcvIntro = v.findViewById(R.id.k2go_rcv_intro); @@ -235,8 +229,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c rcvSkip = v.findViewById(R.id.k2go_rcv_skip); rcvSkipHint = v.findViewById(R.id.k2go_rcv_skiphint); rcvCamNote = v.findViewById(R.id.k2go_rcv_camnote); - rcvShowPaste = v.findViewById(R.id.k2go_rcv_showpaste); - pasteBlock = v.findViewById(R.id.k2go_rcv_pasteblock); rcvIncompat = v.findViewById(R.id.k2go_rcv_incompat); incompatWhy = v.findViewById(R.id.k2go_rcv_incompat_why); incompatWhyText = v.findViewById(R.id.k2go_rcv_incompat_whytext); @@ -282,11 +274,11 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c confirmFresh = v.findViewById(R.id.k2go_rcv_confirm_fresh); v.findViewById(R.id.k2go_rcv_confirm_go).setOnClickListener(x -> startReceiveTransfer()); v.findViewById(R.id.k2go_rcv_confirm_cancel).setOnClickListener(x -> { syncVm.cancelProbe(); renderReceive(); }); - showcode = v.findViewById(R.id.k2go_clone_showcode); - codeblock = v.findViewById(R.id.k2go_clone_codeblock); - codetext = v.findViewById(R.id.k2go_clone_codetext); - copyBtn = v.findViewById(R.id.k2go_clone_copy); - shareBtn = v.findViewById(R.id.k2go_clone_share); + showcode = v.findViewById(R.id.k2go_code_label); + codeblock = v.findViewById(R.id.k2go_code_block); + codetext = v.findViewById(R.id.k2go_code_text); + copyBtn = v.findViewById(R.id.k2go_code_copy); + shareBtn = v.findViewById(R.id.k2go_code_share); // ADFA-5154: the Get-app QR lives inline on Page 1 (secGetApp); "Can't scan? Share another way" // hands the installed APK to the Android share sheet. v.findViewById(R.id.k2go_sendapp_share).setOnClickListener(x -> shareApkViaSheet()); @@ -306,24 +298,15 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c render(); }); // ADFA-5236: no reveal toggle — the code is shown always under a fixed label (see showCodeAsText). - copyBtn.setOnClickListener(x -> { - ClipboardManager cm = (ClipboardManager) requireContext().getSystemService(Context.CLIPBOARD_SERVICE); - if (cm != null) cm.setPrimaryClip(ClipData.newPlainText("K2Go transfer code", currentPayload)); - Toast.makeText(requireContext(), getString(R.string.k2go_clone_toast_code_copied), Toast.LENGTH_SHORT).show(); - }); - shareBtn.setOnClickListener(x -> { - Intent i = new Intent(Intent.ACTION_SEND).setType("text/plain").putExtra(Intent.EXTRA_TEXT, currentPayload); - startActivity(Intent.createChooser(i, getString(R.string.k2go_clone_chooser_send_code))); - }); + copyBtn.setOnClickListener(x -> CodeText.copy(x, currentPayload)); + shareBtn.setOnClickListener(x -> CodeText.share(requireContext(), currentPayload)); - receiveStart.setOnClickListener(x -> onReceiveStart()); cancel.setOnClickListener(x -> onReceiveCancel()); rcvScan.setOnClickListener(x -> { if (rStage == RStage.JOIN) openWifiSettings(); else launchScanner(getString(R.string.k2go_clone_scan_prompt_receive)); }); rcvSkip.setOnClickListener(x -> { rStage = RStage.START; render(); }); - rcvShowPaste.setOnClickListener(x -> { pasteExpanded = !pasteExpanded; renderReceive(); }); hs.state().observe(getViewLifecycleOwner(), st -> render()); // ADFA-5064: same rescue as Connect — redraw the handshake/URL QR when the device's network @@ -415,7 +398,7 @@ private void setSide(Side sd) { if (systemPresent) setMode(Mode.HOTSPOT); else render(); // ADFA-4785: enter Send at step 1 return; } - rStage = RStage.JOIN; pasteExpanded = false; + rStage = RStage.JOIN; pasteCode.collapse(); incompatHostBits = -1; incompatWhyOpen = false; incompatTechOpen = false; // ADFA-4784: fresh on entry render(); } @@ -972,9 +955,7 @@ private void showStartButton() { // ------------------------------------------------------------------ Receive - private void onReceiveStart() { - String json = paste.getText().toString().trim(); - if (json.isEmpty()) { Toast.makeText(requireContext(), getString(R.string.k2go_clone_toast_paste_first), Toast.LENGTH_SHORT).show(); return; } + private void onReceiveStart(String json) { SyncHandshakeHelper.SyncCredentials creds = SyncHandshakeHelper.parsePayload(json); if (creds == null) { Toast.makeText(requireContext(), getString(R.string.k2go_clone_toast_code_invalid), Toast.LENGTH_LONG).show(); return; } if (!archCompatible(creds.archBits)) { showIncompat(creds.archBits); return; } // ADFA-4784 @@ -1099,7 +1080,7 @@ private void renderReceive() { rcvIntro.setVisibility(View.GONE); rcvNotice.setVisibility(View.GONE); rcvScan.setVisibility(View.GONE); rcvSub.setVisibility(View.GONE); rcvSkip.setVisibility(View.GONE); rcvSkipHint.setVisibility(View.GONE); - rcvCamNote.setVisibility(View.GONE); rcvShowPaste.setVisibility(View.GONE); pasteBlock.setVisibility(View.GONE); + rcvCamNote.setVisibility(View.GONE); pasteCode.show(false); SyncTransferState.Phase ph = st.phase; if (ph == SyncTransferState.Phase.CONFIRM) { showProgress(false); @@ -1146,14 +1127,14 @@ private void renderReceive() { rcvIntro.setVisibility(View.GONE); rcvNotice.setVisibility(View.GONE); rcvScan.setVisibility(View.GONE); rcvSub.setVisibility(View.GONE); rcvSkip.setVisibility(View.GONE); rcvSkipHint.setVisibility(View.GONE); - rcvCamNote.setVisibility(View.GONE); rcvShowPaste.setVisibility(View.GONE); pasteBlock.setVisibility(View.GONE); + rcvCamNote.setVisibility(View.GONE); pasteCode.show(false); rcvIncompat.setVisibility(View.VISIBLE); incompatWhyText.setVisibility(incompatWhyOpen ? View.VISIBLE : View.GONE); incompatWhy.setText(R.string.k2go_clone_why_incompat); - setExpandChevron(incompatWhy, incompatWhyOpen, R.color.k2go_warn_ink); + CodeText.setExpandChevron(incompatWhy, incompatWhyOpen, R.color.k2go_warn_ink); incompatTechText.setVisibility(incompatTechOpen ? View.VISIBLE : View.GONE); incompatTech.setText(R.string.k2go_clone_technical_details); - setExpandChevron(incompatTech, incompatTechOpen, R.color.k2go_muted); + CodeText.setExpandChevron(incompatTech, incompatTechOpen, R.color.k2go_muted); incompatTechText.setText(getString(R.string.k2go_clone_tech_arch, bitsLabel(archBits()), bitsLabel(incompatHostBits))); return; } @@ -1174,27 +1155,10 @@ private void renderReceive() { rcvSkip.setVisibility(atJoin ? View.VISIBLE : View.GONE); rcvSkipHint.setVisibility(atJoin ? View.VISIBLE : View.GONE); rcvCamNote.setVisibility(View.GONE); - rcvShowPaste.setVisibility(atJoin ? View.GONE : View.VISIBLE); - rcvShowPaste.setText(R.string.k2go_clone_scan_enter_text); - setExpandChevron(rcvShowPaste, pasteExpanded, R.color.k2go_teal); - pasteBlock.setVisibility((!atJoin && pasteExpanded) ? View.VISIBLE : View.GONE); + pasteCode.show(!atJoin); renderExitZone(); // ADFA-5155: the success-exit status band at the bottom of step 2 } - /** ADFA-5304: Material 3 expand/collapse affordance — one trailing icon that toggles state - * (expand_more when closed, expand_less when open), tinted to the row's role colour. */ - private void setExpandChevron(android.widget.TextView tv, boolean open, int colorRes) { - android.graphics.drawable.Drawable d = androidx.core.content.ContextCompat.getDrawable( - requireContext(), open ? R.drawable.ic_expand_less : R.drawable.ic_expand_more); - if (d != null) { - d = d.mutate(); - androidx.core.graphics.drawable.DrawableCompat.setTint(d, - androidx.core.content.ContextCompat.getColor(requireContext(), colorRes)); - } - tv.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, d, null); - tv.setCompoundDrawablePadding((int) (6 * getResources().getDisplayMetrics().density)); - } - // ---------------------------------------------------------- ADFA-5155: success-exit service wait /** The status band under step 2: "starting services…" -> (if slow) offer a re-scan -> "Opening…" countdown. */ @@ -1305,13 +1269,7 @@ private void buildReceiveSteps() { } private void launchScanner(String prompt) { - ScanOptions o = new ScanOptions(); - o.setDesiredBarcodeFormats(ScanOptions.QR_CODE); - o.setPrompt(prompt); - o.setBeepEnabled(false); - o.setBarcodeImageEnabled(false); - o.setOrientationLocked(false); - barcodeLauncher.launch(o); + barcodeLauncher.launch(CodeText.scanOptions(prompt)); } private void onScan(String contents) { @@ -1325,14 +1283,8 @@ private void onScan(String contents) { } private void openWifiSettings() { - try { - startActivity(new android.content.Intent(android.provider.Settings.ACTION_WIFI_SETTINGS)); - } catch (Exception e) { - try { - startActivity(new android.content.Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)); - } catch (Exception e2) { - Toast.makeText(requireContext(), getString(R.string.k2go_clone_toast_open_wifi), Toast.LENGTH_LONG).show(); - } + if (!CodeText.openWifiSettings(requireContext())) { + Toast.makeText(requireContext(), getString(R.string.k2go_clone_toast_open_wifi), Toast.LENGTH_LONG).show(); } } diff --git a/controller/app/src/main/java/org/appdevforall/k2go/redesign/CodeText.java b/controller/app/src/main/java/org/appdevforall/k2go/redesign/CodeText.java new file mode 100644 index 000000000..89e728dcf --- /dev/null +++ b/controller/app/src/main/java/org/appdevforall/k2go/redesign/CodeText.java @@ -0,0 +1,148 @@ +/* + * ============================================================================ + * Name : CodeText.java + * Author : AppDevForAll + * Copyright : Copyright (c) 2026 AppDevForAll + * Description : The phone-to-phone code path shared by Clone and Content Exchange: scan the + * code, or move it as text (copy / share on the sender, paste on the receiver), + * and join the sender's Wi-Fi. Binds view_k2go_code_text / view_k2go_code_paste. + * ============================================================================ + */ +package org.appdevforall.k2go.redesign; + +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.provider.Settings; +import android.view.View; +import android.widget.EditText; +import android.widget.TextView; + +import androidx.core.content.ContextCompat; +import androidx.core.graphics.drawable.DrawableCompat; +import androidx.core.util.Consumer; + +import com.journeyapps.barcodescanner.ScanOptions; + +import org.appdevforall.k2go.R; +import org.appdevforall.k2go.util.Snackbars; + +public final class CodeText { + + private CodeText() { + } + + /** Copy the payload to the clipboard and confirm with a snackbar on {@code anchor}. */ + public static void copy(View anchor, String payload) { + ClipboardManager cm = (ClipboardManager) anchor.getContext().getSystemService(Context.CLIPBOARD_SERVICE); + if (cm != null) { + cm.setPrimaryClip(ClipData.newPlainText("K2Go transfer code", payload)); + } + Snackbars.make(anchor, R.string.k2go_clone_toast_code_copied).show(); + } + + /** Hand the payload to the Android share sheet (chat, email, a remote session). */ + public static void share(Context ctx, String payload) { + Intent i = new Intent(Intent.ACTION_SEND).setType("text/plain").putExtra(Intent.EXTRA_TEXT, payload); + ctx.startActivity(Intent.createChooser(i, ctx.getString(R.string.k2go_clone_chooser_send_code))); + } + + /** The QR scanner setup both receivers use. */ + public static ScanOptions scanOptions(String prompt) { + ScanOptions o = new ScanOptions(); + o.setDesiredBarcodeFormats(ScanOptions.QR_CODE); + o.setPrompt(prompt); + o.setBeepEnabled(false); + o.setBarcodeImageEnabled(false); + o.setOrientationLocked(false); + return o; + } + + /** Open Android Wi-Fi settings (wireless settings as a fallback) to join the sender's network. + * Returns false when neither screen can open, so the caller can tell the user. */ + public static boolean openWifiSettings(Context ctx) { + for (String action : new String[]{Settings.ACTION_WIFI_SETTINGS, Settings.ACTION_WIRELESS_SETTINGS}) { + try { + ctx.startActivity(new Intent(action)); + return true; + } catch (Exception ignored) { + // try the next screen + } + } + return false; + } + + /** ADFA-5304: Material 3 expand/collapse affordance (the paste link and Clone's other disclosure + * rows): one trailing icon, expand_more when closed and expand_less when open, tinted to the + * row's role colour. */ + public static void setExpandChevron(TextView tv, boolean open, int colorRes) { + Context ctx = tv.getContext(); + Drawable d = ContextCompat.getDrawable(ctx, open ? R.drawable.ic_expand_less : R.drawable.ic_expand_more); + if (d != null) { + d = d.mutate(); + DrawableCompat.setTint(d, ContextCompat.getColor(ctx, colorRes)); + } + tv.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, d, null); + tv.setCompoundDrawablePadding((int) (6 * ctx.getResources().getDisplayMetrics().density)); + } + + /** + * The receiver's "Scan didn't work? Enter the code as text" block (view_k2go_code_paste): the link + * toggles the paste field; "Use this code" hands the trimmed text to {@code onCode}, or asks for a + * paste when the field is empty. The host decides when the block is shown; this owns the rest. + */ + public static final class PasteBlock { + private final TextView toggle; + private final View block; + private final EditText field; + private boolean shown; + private boolean expanded; + + public PasteBlock(View root, Consumer onCode) { + toggle = root.findViewById(R.id.k2go_code_showpaste); + block = root.findViewById(R.id.k2go_code_pasteblock); + field = root.findViewById(R.id.k2go_code_paste); + toggle.setOnClickListener(v -> { + expanded = !expanded; + apply(); + }); + root.findViewById(R.id.k2go_code_use).setOnClickListener(v -> { + String text = field.getText().toString().trim(); + if (text.isEmpty()) { + Snackbars.make(v, R.string.k2go_clone_toast_paste_first).show(); + return; + } + onCode.accept(text); + }); + } + + /** Show or hide the whole block (the link and, when open, the field). */ + public void show(boolean show) { + shown = show; + apply(); + } + + /** Close the field, for example when the flow starts over. */ + public void collapse() { + expanded = false; + apply(); + } + + public boolean isExpanded() { + return expanded; + } + + public void setExpanded(boolean open) { + expanded = open; + apply(); + } + + private void apply() { + toggle.setVisibility(shown ? View.VISIBLE : View.GONE); + setExpandChevron(toggle, expanded, R.color.k2go_teal); + block.setVisibility(shown && expanded ? View.VISIBLE : View.GONE); + } + } +} diff --git a/controller/app/src/main/java/org/appdevforall/k2go/redesign/QrSection.java b/controller/app/src/main/java/org/appdevforall/k2go/redesign/QrSection.java index a978a8852..b696eaaf4 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/redesign/QrSection.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/redesign/QrSection.java @@ -60,6 +60,12 @@ public QrSection(View r, int frameId, int qrId, int phId, int capId, int subId, fallbackValues = r.findViewById(valuesId); } + /** Number and title a step header (view_k2go_step_header) found under {@code root}. */ + public static void setStep(View root, String number, CharSequence title) { + ((TextView) root.findViewById(R.id.sec_badge)).setText(number); + ((TextView) root.findViewById(R.id.sec_title)).setText(title); + } + /** Draw {@code data} into the QR, or clear it and show {@code placeholder} in the slot. */ public void setQr(Context ctx, String data, String placeholder) { stopPending(); // K2GO-375: a real QR or a static placeholder ends any "resolving…" animation diff --git a/controller/app/src/main/res/drawable/k2go_field_bg.xml b/controller/app/src/main/res/drawable/k2go_field_bg.xml new file mode 100644 index 000000000..dabaadcb7 --- /dev/null +++ b/controller/app/src/main/res/drawable/k2go_field_bg.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/controller/app/src/main/res/layout/fragment_k2go_clone.xml b/controller/app/src/main/res/layout/fragment_k2go_clone.xml index b6920429a..9aed501cd 100644 --- a/controller/app/src/main/res/layout/fragment_k2go_clone.xml +++ b/controller/app/src/main/res/layout/fragment_k2go_clone.xml @@ -134,7 +134,7 @@ + android:background="@drawable/k2go_card_bg" android:contentDescription="@string/qr_content_description" /> @@ -174,27 +174,7 @@ - - - - - - - - - - + @@ -255,21 +235,7 @@ android:gravity="center" android:layout_marginTop="10dp" android:text="@string/k2go_clone_camera_q" android:textColor="@color/k2go_muted" android:textAppearance="?attr/textAppearanceBodySmall" android:visibility="gone" /> - - - - -