From 20f80f39d22201eb710299938b7f5c18f455465b Mon Sep 17 00:00:00 2001 From: Luis Guzman Date: Thu, 19 Nov 2026 20:45:57 -0600 Subject: [PATCH 1/6] K2GO-587 fix(clone): keep the phones in reach on a shared Wi-Fi Send starts on the Wi-Fi network when the phone is on one, so no Wi-Fi Direct group starts for nothing. That group can end a direct Wi-Fi link (TDLS) to the receiver on one side only. The receiver now drops a dead link to the sender and checks a "no" again. --- .../k2go/hotspot/HotspotMode.java | 9 +++ .../k2go/redesign/CloneFragment.java | 4 +- .../k2go/sync/cable/data/SenderReach.java | 73 +++++++++++++++++++ .../cable/presentation/CloneLinkRows.java | 38 +++++----- .../sync/presentation/SyncStateViewModel.java | 14 +--- .../k2go/hotspot/HotspotModeTest.java | 6 ++ 6 files changed, 114 insertions(+), 30 deletions(-) create mode 100644 controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java diff --git a/controller/app/src/main/java/org/appdevforall/k2go/hotspot/HotspotMode.java b/controller/app/src/main/java/org/appdevforall/k2go/hotspot/HotspotMode.java index d27e9d576..51ad45ca1 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/hotspot/HotspotMode.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/hotspot/HotspotMode.java @@ -56,6 +56,15 @@ public static HotspotMode of(Context context, HotspotHolders.Holder holder, Mode }, initial); } + /** + * The first choice of a sharing screen: the Wi-Fi network the phone is already on, else the + * hotspot. A Wi-Fi Direct group started for nothing can break a direct Wi-Fi link (TDLS) + * between the two phones on the same network (K2GO-584). + */ + public static Mode initialFor(boolean onWifiNetwork) { + return onWifiNetwork ? Mode.WIFI : Mode.HOTSPOT; + } + public Mode mode() { return mode; } 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 50a4142c8..cd396e6a7 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 @@ -403,7 +403,9 @@ private void setSide(Side sd) { // a phone with a system is the natural source for both the app and the system.) systemPresent = org.appdevforall.k2go.SystemStateEvaluator.isSystemInstalled(requireContext()); page = Page.PREPARE; - if (systemPresent) setMode(Mode.HOTSPOT); else render(); // ADFA-4785: enter Send at step 1 + // ADFA-4785: enter Send at step 1. K2GO-584: on the phone's Wi-Fi network when it is on one. + if (systemPresent) setMode(HotspotMode.initialFor(CloneLinkRows.onWifiNetwork(requireContext()))); + else render(); return; } rStage = RStage.JOIN; pasteCode.collapse(); diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java new file mode 100644 index 000000000..23bfc228e --- /dev/null +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java @@ -0,0 +1,73 @@ +/* + * ============================================================================ + * Name : SenderReach.java + * Author : AppDevForAll + * Copyright : Copyright (c) 2026 AppDevForAll + * Description : Does the sender answer on its rsync port over Wi-Fi (K2GO-584)? + * Two phones on one Wi-Fi network set up a direct link (TDLS) by + * themselves under heavy traffic. When the sender then starts a + * Wi-Fi Direct group, some drivers end their side of that link + * without telling the receiver, which keeps sending into the dead + * link until Wi-Fi restarts. So a failed connect drops the link + * on this phone and tries once more, through the router. + * https://developer.android.com/reference/android/net/wifi/WifiManager#setTdlsEnabled(java.net.InetAddress,%20boolean) + * ============================================================================ + */ +package org.appdevforall.k2go.sync.cable.data; + +import android.content.Context; +import android.net.wifi.WifiManager; +import android.util.Log; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; + +public final class SenderReach { + + private static final String TAG = "SenderReach"; + + private SenderReach() { + } + + /** + * The paired sender at {@code host:port} accepts a connection, after one direct-link drop if + * needed. Only for the sender's own address: a router has no direct link to drop. + */ + public static boolean connect(Context c, String host, int port, int timeoutMs) { + if (host == null) return false; + String why = failure(host, port, timeoutMs); + if (why == null) return true; + Log.i(TAG, host + ":" + port + " does not answer (" + why + ")"); // refused vs timeout + WifiManager wm = (WifiManager) c.getApplicationContext().getSystemService(Context.WIFI_SERVICE); + return dropDirectLink(wm, host) && failure(host, port, timeoutMs) == null; + } + + /** A plain connect, with no repair. */ + public static boolean answers(String host, int port, int timeoutMs) { + return host != null && failure(host, port, timeoutMs) == null; + } + + /** Null when the connect works, else the reason. */ + private static String failure(String host, int port, int timeoutMs) { + try (Socket s = new Socket()) { + s.connect(new InetSocketAddress(host, port), timeoutMs); + return null; + } catch (Exception e) { + return String.valueOf(e.getMessage()); + } + } + + /** False on a phone without TDLS: then there is no link to drop. */ + private static boolean dropDirectLink(WifiManager wm, String host) { + if (wm == null || !wm.isTdlsSupported()) return false; + try { + wm.setTdlsEnabled(InetAddress.getByName(host), false); // a literal address: no DNS + Log.i(TAG, "dropped the direct Wi-Fi link to " + host); + return true; + } catch (Exception e) { + Log.w(TAG, "could not drop the direct Wi-Fi link: " + e.getMessage()); + return false; + } + } +} diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java index 576cb391f..4cb5a73a0 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java @@ -33,6 +33,7 @@ import org.appdevforall.k2go.R; import org.appdevforall.k2go.hotspot.LocalHotspotManager; import org.appdevforall.k2go.sync.cable.data.CableSession; +import org.appdevforall.k2go.sync.cable.data.SenderReach; import org.appdevforall.k2go.sync.cable.data.ShareLogWatch; import org.appdevforall.k2go.sync.cable.data.UsbPresence; import org.appdevforall.k2go.sync.cable.domain.LinkRule; @@ -40,8 +41,6 @@ import org.appdevforall.k2go.sync.cable.domain.WifiLink; import org.appdevforall.k2go.util.AppExecutors; -import java.net.InetSocketAddress; -import java.net.Socket; public final class CloneLinkRows { @@ -63,6 +62,7 @@ public final class CloneLinkRows { /** The sender's address from its pairing data (QR or cable), or null before pairing. */ private volatile String senderHost; private volatile boolean probeOnce; + private volatile boolean repairTried; // set by a "no" after a repair, cleared by a "yes" private boolean inFlight; // main thread only: one check at a time private final Runnable check = this::checkWifi; @@ -160,8 +160,9 @@ public void showSender(boolean hotspotMode, LocalHotspotManager.State hs) { /** * Receiver: checks the Wi-Fi every few seconds until stopChecks(). With {@code probeSender} - * false (during a transfer) it does not connect to the sender's daemon, which allows only a few - * connections; it keeps the last answer and still follows Wi-Fi on/off and the band. + * false (during a transfer) it keeps a "yes" without a new connection, because the sender's + * daemon allows only a few; a "no" is checked again (a failed connect takes no connection). It + * still follows Wi-Fi on/off and the band. */ public void startChecks(int senderRsyncPort, boolean probeSender) { senderSide = false; @@ -197,13 +198,20 @@ private void checkWifi() { WifiManager wm = (WifiManager) app.getSystemService(Context.WIFI_SERVICE); boolean on = wm != null && wm.isWifiEnabled(); int freq = on ? wm.getConnectionInfo().getFrequency() : -1; - boolean probeNow = probeSender || probeOnce; - probeOnce = false; String host = senderHost; + boolean probeNow = probeSender || probeOnce || (host != null && !lastAnswers); + probeOnce = false; String gw = on ? gateway(wm.getDhcpInfo().gateway) : null; boolean viaHotspot = host == null || host.equals(gw); // the hotspot gateway is the sender String target = host != null ? host : gw; - boolean answers = on && (probeNow ? senderAnswers(target, port) : lastAnswers); + // A direct link exists only to the sender on a shared network; one repair per run of "no". + boolean repair = !viaHotspot && !repairTried; + // Phone to phone through a home router can take over a second (K2GO-576 tests). + boolean answers = on && (probeNow + ? (repair ? SenderReach.connect(app, host, port, 3000) : SenderReach.answers(target, port, 3000)) + : lastAnswers); + if (probeNow && repair && on && !answers) repairTried = true; + if (answers) repairTried = false; lastAnswers = answers; if (probeNow) lastProbed = on ? target : null; if (probeNow) CableSession.get().reportWifi(answers); // the sender shows the same verdict @@ -224,6 +232,11 @@ private static String gateway(int le) { return (le & 0xFF) + "." + ((le >> 8) & 0xFF) + "." + ((le >> 16) & 0xFF) + "." + ((le >> 24) & 0xFF); } + /** The phone is on a Wi-Fi network: the same signal that shows the band in the Wi-Fi row. */ + public static boolean onWifiNetwork(Context c) { + return WifiLink.band(ownFrequency(c)) != WifiLink.Band.NONE; + } + @SuppressWarnings("deprecation") private static int ownFrequency(Context c) { WifiManager wm = (WifiManager) c.getApplicationContext().getSystemService(Context.WIFI_SERVICE); @@ -235,17 +248,6 @@ private static String rate(long bytesPerSec) { return String.format(java.util.Locale.ROOT, "%.1fMB/s", bytesPerSec / 1e6); } - private static boolean senderAnswers(String host, int port) { - if (host == null) return false; - try (Socket s = new Socket()) { - // Phone to phone through a home router can take over a second (K2GO-576 tests). - s.connect(new InetSocketAddress(host, port), 3000); - return true; - } catch (Exception e) { - return false; - } - } - /** * Receiver, during a pull: what each path carries. A Wi-Fi path that moves data is proof that * the sender answers on Wi-Fi, so the row shows it even while the daemon probe is paused. diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java index 1fdead1b5..26013adcf 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java @@ -25,6 +25,7 @@ import org.appdevforall.k2go.hotspot.LocalHotspotManager; import org.appdevforall.k2go.sync.cable.data.CableSession; import org.appdevforall.k2go.sync.cable.data.ChunkedPull; +import org.appdevforall.k2go.sync.cable.data.SenderReach; import org.appdevforall.k2go.sync.domain.ShareConfig; import org.appdevforall.k2go.sync.transport.NetworkInterfaces; import org.appdevforall.k2go.sync.transport.TransportEngine; @@ -32,8 +33,6 @@ import org.appdevforall.k2go.util.AppExecutors; import java.io.File; -import java.net.InetSocketAddress; -import java.net.Socket; public class SyncStateViewModel extends ViewModel { @@ -112,15 +111,8 @@ public void startProbe(Context appCtx, ShareConfig shareConfig, SyncHandshakeHel // (Wi-Fi/hotspot) is flagged "no internet" and Android gives the app no default network. WifiNetworkBinder.bindToHostNetwork(appCtx, creds.ip); - boolean reachable = false; - try { - Socket socket = new Socket(); - socket.connect(new InetSocketAddress(creds.ip, creds.port), 2000); - socket.close(); - reachable = true; - } catch (Exception e) { - Log.w(TAG, "Reachability probe to " + creds.ip + ":" + creds.port + " failed", e); - } + boolean reachable = SenderReach.connect(appCtx, creds.ip, creds.port, 2000); + if (!reachable) Log.w(TAG, "Reachability probe to " + creds.ip + ":" + creds.port + " failed"); // K2GO-576: no Wi-Fi path, but a cable link: the dry-run goes over the cable. String probeHost = creds.ip; int probePort = creds.port; diff --git a/controller/app/src/test/java/org/appdevforall/k2go/hotspot/HotspotModeTest.java b/controller/app/src/test/java/org/appdevforall/k2go/hotspot/HotspotModeTest.java index 040033e86..bd7e9eab3 100644 --- a/controller/app/src/test/java/org/appdevforall/k2go/hotspot/HotspotModeTest.java +++ b/controller/app/src/test/java/org/appdevforall/k2go/hotspot/HotspotModeTest.java @@ -25,6 +25,12 @@ private static final class FakePort implements HotspotMode.Port { @Override public boolean releaseDropsClients() { return dropsClients; } } + @Test + public void startsOnTheWifiNetworkOnlyWhenThePhoneIsOnOne() { + assertEquals(HotspotMode.Mode.WIFI, HotspotMode.initialFor(true)); + assertEquals(HotspotMode.Mode.HOTSPOT, HotspotMode.initialFor(false)); + } + @Test public void hotspotHoldsAndWifiReleases() { FakePort port = new FakePort(); From 724a24e7b015dd76288f1ac5a611f9b2efa2fe35 Mon Sep 17 00:00:00 2001 From: Luis Guzman Date: Fri, 20 Nov 2026 07:17:47 -0600 Subject: [PATCH 2/6] K2GO-587 fix(clone): repair only a direct link, once per sender The gateway (router or the sender hotspot) has no direct link, so SenderReach skips the repair there for every caller. A new sender gets a new repair try. --- .../org/appdevforall/k2go/hotspot/HotspotMode.java | 2 +- .../appdevforall/k2go/redesign/CloneFragment.java | 2 +- .../k2go/sync/cable/data/SenderReach.java | 13 +++++++++++-- .../k2go/sync/cable/presentation/CloneLinkRows.java | 12 +++--------- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/controller/app/src/main/java/org/appdevforall/k2go/hotspot/HotspotMode.java b/controller/app/src/main/java/org/appdevforall/k2go/hotspot/HotspotMode.java index 51ad45ca1..36c64836c 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/hotspot/HotspotMode.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/hotspot/HotspotMode.java @@ -59,7 +59,7 @@ public static HotspotMode of(Context context, HotspotHolders.Holder holder, Mode /** * The first choice of a sharing screen: the Wi-Fi network the phone is already on, else the * hotspot. A Wi-Fi Direct group started for nothing can break a direct Wi-Fi link (TDLS) - * between the two phones on the same network (K2GO-584). + * between the two phones on the same network (K2GO-587). */ public static Mode initialFor(boolean onWifiNetwork) { return onWifiNetwork ? Mode.WIFI : Mode.HOTSPOT; 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 cd396e6a7..88c2e6a90 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 @@ -403,7 +403,7 @@ private void setSide(Side sd) { // a phone with a system is the natural source for both the app and the system.) systemPresent = org.appdevforall.k2go.SystemStateEvaluator.isSystemInstalled(requireContext()); page = Page.PREPARE; - // ADFA-4785: enter Send at step 1. K2GO-584: on the phone's Wi-Fi network when it is on one. + // ADFA-4785: enter Send at step 1. K2GO-587: on the phone's Wi-Fi network when it is on one. if (systemPresent) setMode(HotspotMode.initialFor(CloneLinkRows.onWifiNetwork(requireContext()))); else render(); return; diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java index 23bfc228e..3ab4fd9dc 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java @@ -3,7 +3,7 @@ * Name : SenderReach.java * Author : AppDevForAll * Copyright : Copyright (c) 2026 AppDevForAll - * Description : Does the sender answer on its rsync port over Wi-Fi (K2GO-584)? + * Description : Does the sender answer on its rsync port over Wi-Fi (K2GO-587)? * Two phones on one Wi-Fi network set up a direct link (TDLS) by * themselves under heavy traffic. When the sender then starts a * Wi-Fi Direct group, some drivers end their side of that link @@ -32,7 +32,7 @@ private SenderReach() { /** * The paired sender at {@code host:port} accepts a connection, after one direct-link drop if - * needed. Only for the sender's own address: a router has no direct link to drop. + * needed. The gateway (a router, or the sender's own hotspot) has no direct link to drop. */ public static boolean connect(Context c, String host, int port, int timeoutMs) { if (host == null) return false; @@ -40,9 +40,18 @@ public static boolean connect(Context c, String host, int port, int timeoutMs) { if (why == null) return true; Log.i(TAG, host + ":" + port + " does not answer (" + why + ")"); // refused vs timeout WifiManager wm = (WifiManager) c.getApplicationContext().getSystemService(Context.WIFI_SERVICE); + if (host.equals(gateway(wm))) return false; return dropDirectLink(wm, host) && failure(host, port, timeoutMs) == null; } + /** The Wi-Fi gateway (a router, or the sender on its own hotspot), or null with no DHCP lease. */ + @SuppressWarnings("deprecation") // getDhcpInfo: no permission needed for the gateway + public static String gateway(WifiManager wm) { + int le = wm != null ? wm.getDhcpInfo().gateway : 0; + if (le == 0) return null; + return (le & 0xFF) + "." + ((le >> 8) & 0xFF) + "." + ((le >> 16) & 0xFF) + "." + ((le >> 24) & 0xFF); + } + /** A plain connect, with no repair. */ public static boolean answers(String host, int port, int timeoutMs) { return host != null && failure(host, port, timeoutMs) == null; diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java index 4cb5a73a0..c28819548 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java @@ -181,6 +181,7 @@ public void setSenderHost(String host) { if (host == null ? senderHost == null : host.equals(senderHost)) return; senderHost = host; probeOnce = host != null; + repairTried = false; // a new sender gets its own repair } public void stopChecks() { @@ -201,11 +202,10 @@ private void checkWifi() { String host = senderHost; boolean probeNow = probeSender || probeOnce || (host != null && !lastAnswers); probeOnce = false; - String gw = on ? gateway(wm.getDhcpInfo().gateway) : null; + String gw = on ? SenderReach.gateway(wm) : null; boolean viaHotspot = host == null || host.equals(gw); // the hotspot gateway is the sender String target = host != null ? host : gw; - // A direct link exists only to the sender on a shared network; one repair per run of "no". - boolean repair = !viaHotspot && !repairTried; + boolean repair = host != null && !repairTried; // one repair per run of "no" // Phone to phone through a home router can take over a second (K2GO-576 tests). boolean answers = on && (probeNow ? (repair ? SenderReach.connect(app, host, port, 3000) : SenderReach.answers(target, port, 3000)) @@ -226,12 +226,6 @@ private void checkWifi() { }); } - /** The hotspot gateway (the sender on its own hotspot), or null with no DHCP lease. */ - private static String gateway(int le) { - if (le == 0) return null; - return (le & 0xFF) + "." + ((le >> 8) & 0xFF) + "." + ((le >> 16) & 0xFF) + "." + ((le >> 24) & 0xFF); - } - /** The phone is on a Wi-Fi network: the same signal that shows the band in the Wi-Fi row. */ public static boolean onWifiNetwork(Context c) { return WifiLink.band(ownFrequency(c)) != WifiLink.Band.NONE; From a526f33d89ea2def87013f6be4893f1be1a85a2a Mon Sep 17 00:00:00 2001 From: Luis Guzman Date: Fri, 20 Nov 2026 08:15:32 -0600 Subject: [PATCH 3/6] K2GO-587 fix(clone): find a lost cable peer in 3 s, not 6 s PING every 1 s, three missed PINGs close the link. The cable row showed Linked for 6 to 8 s after the sender app stopped. --- .../java/org/appdevforall/k2go/sync/cable/data/CableMux.java | 2 +- .../org/appdevforall/k2go/sync/cable/domain/PeerAlive.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/CableMux.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/CableMux.java index b80c44849..a5e2912e1 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/CableMux.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/CableMux.java @@ -71,7 +71,7 @@ public interface Opener { private volatile long lastRx; // last bytes from the cable, or the end of local work private volatile boolean peerPings; // the peer runs the keepalive (an older K2Go does not) private volatile boolean busy; // the read thread writes to a local socket - private static final long PING_MS = 2_000; + private static final long PING_MS = 1_000; public CableMux(UsbLink link, Opener opener, Listener listener) { this.link = link; diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/PeerAlive.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/PeerAlive.java index 3625ceb28..f10d2cb9f 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/PeerAlive.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/PeerAlive.java @@ -11,8 +11,8 @@ public final class PeerAlive { - /** Each side sends PING every 2 s; three missed PINGs mean the peer is gone. */ - public static final long DEAD_MS = 6_000; + /** Each side sends PING every 1 s; three missed PINGs mean the peer is gone (K2GO-587). */ + public static final long DEAD_MS = 3_000; private PeerAlive() { } From 47a0127348599c9ee8366e989ff03780b11b7ced Mon Sep 17 00:00:00 2001 From: Luis Guzman Date: Fri, 20 Nov 2026 08:30:42 -0600 Subject: [PATCH 4/6] K2GO-587 refactor(clone): one rule for the direct-link repair DirectLinkRepair (pure JVM, tested): repair only when the sender does not answer (a refusal proves the link works), never toward the gateway, once per run of failed checks. During a copy a 'no' is checked again only while its repair is unused. --- .../k2go/sync/cable/data/SenderReach.java | 54 ++++++++++++------- .../sync/cable/domain/DirectLinkRepair.java | 33 ++++++++++++ .../cable/presentation/CloneLinkRows.java | 26 +++++---- .../sync/presentation/SyncStateViewModel.java | 3 +- .../cable/domain/DirectLinkRepairTest.java | 39 ++++++++++++++ 5 files changed, 123 insertions(+), 32 deletions(-) create mode 100644 controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/DirectLinkRepair.java create mode 100644 controller/app/src/test/java/org/appdevforall/k2go/sync/cable/domain/DirectLinkRepairTest.java diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java index 3ab4fd9dc..7e0ae3f43 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java @@ -8,8 +8,8 @@ * themselves under heavy traffic. When the sender then starts a * Wi-Fi Direct group, some drivers end their side of that link * without telling the receiver, which keeps sending into the dead - * link until Wi-Fi restarts. So a failed connect drops the link - * on this phone and tries once more, through the router. + * link until Wi-Fi restarts. DirectLinkRepair decides when this + * phone drops the link and tries once more, through the router. * https://developer.android.com/reference/android/net/wifi/WifiManager#setTdlsEnabled(java.net.InetAddress,%20boolean) * ============================================================================ */ @@ -19,6 +19,10 @@ import android.net.wifi.WifiManager; import android.util.Log; +import org.appdevforall.k2go.sync.cable.domain.DirectLinkRepair; +import org.appdevforall.k2go.sync.cable.domain.DirectLinkRepair.Outcome; + +import java.net.ConnectException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; @@ -27,21 +31,34 @@ public final class SenderReach { private static final String TAG = "SenderReach"; + /** One check: does the sender answer, and did this check use the repair. */ + public static final class Result { + public final boolean answers; + public final boolean repaired; + + Result(boolean answers, boolean repaired) { + this.answers = answers; + this.repaired = repaired; + } + } + private SenderReach() { } /** - * The paired sender at {@code host:port} accepts a connection, after one direct-link drop if - * needed. The gateway (a router, or the sender's own hotspot) has no direct link to drop. + * Connects to {@code host:port}. The caller owns the run of failed checks: {@code triedInThisRun} + * comes from DirectLinkRepair.triedAfter, or false for a check that starts its own run. */ - public static boolean connect(Context c, String host, int port, int timeoutMs) { - if (host == null) return false; - String why = failure(host, port, timeoutMs); - if (why == null) return true; - Log.i(TAG, host + ":" + port + " does not answer (" + why + ")"); // refused vs timeout + public static Result connect(Context c, String host, int port, int timeoutMs, boolean triedInThisRun) { + if (host == null) return new Result(false, false); + Outcome first = outcome(host, port, timeoutMs); + if (first == Outcome.ANSWERS) return new Result(true, false); WifiManager wm = (WifiManager) c.getApplicationContext().getSystemService(Context.WIFI_SERVICE); - if (host.equals(gateway(wm))) return false; - return dropDirectLink(wm, host) && failure(host, port, timeoutMs) == null; + if (!DirectLinkRepair.shouldRepair(first, host.equals(gateway(wm)), triedInThisRun)) { + return new Result(false, false); + } + if (!dropDirectLink(wm, host)) return new Result(false, false); + return new Result(outcome(host, port, timeoutMs) == Outcome.ANSWERS, true); } /** The Wi-Fi gateway (a router, or the sender on its own hotspot), or null with no DHCP lease. */ @@ -52,18 +69,15 @@ public static String gateway(WifiManager wm) { return (le & 0xFF) + "." + ((le >> 8) & 0xFF) + "." + ((le >> 16) & 0xFF) + "." + ((le >> 24) & 0xFF); } - /** A plain connect, with no repair. */ - public static boolean answers(String host, int port, int timeoutMs) { - return host != null && failure(host, port, timeoutMs) == null; - } - - /** Null when the connect works, else the reason. */ - private static String failure(String host, int port, int timeoutMs) { + private static Outcome outcome(String host, int port, int timeoutMs) { try (Socket s = new Socket()) { s.connect(new InetSocketAddress(host, port), timeoutMs); - return null; + return Outcome.ANSWERS; } catch (Exception e) { - return String.valueOf(e.getMessage()); + // No log: before pairing the receiver checks the router every few seconds. + // Android reports a refusal as ConnectException "... ECONNREFUSED (Connection refused)". + boolean refused = e instanceof ConnectException && String.valueOf(e.getMessage()).contains("ECONNREFUSED"); + return refused ? Outcome.REFUSED : Outcome.NO_ANSWER; } } diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/DirectLinkRepair.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/DirectLinkRepair.java new file mode 100644 index 000000000..11f0eea28 --- /dev/null +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/DirectLinkRepair.java @@ -0,0 +1,33 @@ +/* + * ============================================================================ + * Name : DirectLinkRepair.java + * Author : AppDevForAll + * Copyright : Copyright (c) 2026 AppDevForAll + * Description : When does the receiver drop the direct Wi-Fi link (TDLS) to + * the sender and try once more (K2GO-587)? Pure JVM. + * Only a connect with no answer points to a dead direct link: a + * refusal proves that packets reach the sender. The gateway (a + * router, or the sender's own hotspot) has no direct link. One + * repair per run of failed checks; an answer ends the run. + * ============================================================================ + */ +package org.appdevforall.k2go.sync.cable.domain; + +public final class DirectLinkRepair { + + /** The result of one connect to the sender. */ + public enum Outcome { ANSWERS, REFUSED, NO_ANSWER } + + private DirectLinkRepair() { + } + + /** Drop the direct link and connect once more? */ + public static boolean shouldRepair(Outcome first, boolean hostIsGateway, boolean triedInThisRun) { + return first == Outcome.NO_ANSWER && !hostIsGateway && !triedInThisRun; + } + + /** Is a repair already used in this run, after a check that {@code repaired} and got {@code answers}? */ + public static boolean triedAfter(boolean tried, boolean repaired, boolean answers) { + return !answers && (tried || repaired); + } +} diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java index c28819548..f1d007bae 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/presentation/CloneLinkRows.java @@ -36,6 +36,7 @@ import org.appdevforall.k2go.sync.cable.data.SenderReach; import org.appdevforall.k2go.sync.cable.data.ShareLogWatch; import org.appdevforall.k2go.sync.cable.data.UsbPresence; +import org.appdevforall.k2go.sync.cable.domain.DirectLinkRepair; import org.appdevforall.k2go.sync.cable.domain.LinkRule; import org.appdevforall.k2go.sync.cable.domain.ShareSessions; import org.appdevforall.k2go.sync.cable.domain.WifiLink; @@ -62,7 +63,7 @@ public final class CloneLinkRows { /** The sender's address from its pairing data (QR or cable), or null before pairing. */ private volatile String senderHost; private volatile boolean probeOnce; - private volatile boolean repairTried; // set by a "no" after a repair, cleared by a "yes" + private volatile boolean repairTried; // DirectLinkRepair.triedAfter: the run of "no" used its repair private boolean inFlight; // main thread only: one check at a time private final Runnable check = this::checkWifi; @@ -161,8 +162,8 @@ public void showSender(boolean hotspotMode, LocalHotspotManager.State hs) { /** * Receiver: checks the Wi-Fi every few seconds until stopChecks(). With {@code probeSender} * false (during a transfer) it keeps a "yes" without a new connection, because the sender's - * daemon allows only a few; a "no" is checked again (a failed connect takes no connection). It - * still follows Wi-Fi on/off and the band. + * daemon allows only a few; a "no" is checked again until its repair is used (a failed connect + * takes no connection). It still follows Wi-Fi on/off and the band. */ public void startChecks(int senderRsyncPort, boolean probeSender) { senderSide = false; @@ -200,18 +201,21 @@ private void checkWifi() { boolean on = wm != null && wm.isWifiEnabled(); int freq = on ? wm.getConnectionInfo().getFrequency() : -1; String host = senderHost; - boolean probeNow = probeSender || probeOnce || (host != null && !lastAnswers); + // During a transfer a "no" is checked again only while its repair is unused. + boolean probeNow = probeSender || probeOnce || (host != null && !lastAnswers && !repairTried); probeOnce = false; String gw = on ? SenderReach.gateway(wm) : null; boolean viaHotspot = host == null || host.equals(gw); // the hotspot gateway is the sender String target = host != null ? host : gw; - boolean repair = host != null && !repairTried; // one repair per run of "no" - // Phone to phone through a home router can take over a second (K2GO-576 tests). - boolean answers = on && (probeNow - ? (repair ? SenderReach.connect(app, host, port, 3000) : SenderReach.answers(target, port, 3000)) - : lastAnswers); - if (probeNow && repair && on && !answers) repairTried = true; - if (answers) repairTried = false; + boolean answers = lastAnswers; + if (probeNow && on) { + // Phone to phone through a home router can take over a second (K2GO-576 tests). + SenderReach.Result r = SenderReach.connect(app, target, port, 3000, repairTried); + answers = r.answers; + repairTried = DirectLinkRepair.triedAfter(repairTried, r.repaired, r.answers); + } else if (!on) { + answers = false; + } lastAnswers = answers; if (probeNow) lastProbed = on ? target : null; if (probeNow) CableSession.get().reportWifi(answers); // the sender shows the same verdict diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java index 26013adcf..94ccda9b1 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java @@ -111,7 +111,8 @@ public void startProbe(Context appCtx, ShareConfig shareConfig, SyncHandshakeHel // (Wi-Fi/hotspot) is flagged "no internet" and Android gives the app no default network. WifiNetworkBinder.bindToHostNetwork(appCtx, creds.ip); - boolean reachable = SenderReach.connect(appCtx, creds.ip, creds.port, 2000); + // A pull start is its own run of checks: it may use one direct-link repair (K2GO-587). + boolean reachable = SenderReach.connect(appCtx, creds.ip, creds.port, 2000, false).answers; if (!reachable) Log.w(TAG, "Reachability probe to " + creds.ip + ":" + creds.port + " failed"); // K2GO-576: no Wi-Fi path, but a cable link: the dry-run goes over the cable. String probeHost = creds.ip; diff --git a/controller/app/src/test/java/org/appdevforall/k2go/sync/cable/domain/DirectLinkRepairTest.java b/controller/app/src/test/java/org/appdevforall/k2go/sync/cable/domain/DirectLinkRepairTest.java new file mode 100644 index 000000000..f4dec3634 --- /dev/null +++ b/controller/app/src/test/java/org/appdevforall/k2go/sync/cable/domain/DirectLinkRepairTest.java @@ -0,0 +1,39 @@ +package org.appdevforall.k2go.sync.cable.domain; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.appdevforall.k2go.sync.cable.domain.DirectLinkRepair.Outcome; +import org.junit.Test; + +public class DirectLinkRepairTest { + + @Test + public void noAnswerFromAPeerIsRepairedOnce() { + assertTrue(DirectLinkRepair.shouldRepair(Outcome.NO_ANSWER, false, false)); + assertFalse(DirectLinkRepair.shouldRepair(Outcome.NO_ANSWER, false, true)); + } + + @Test + public void refusalProvesTheLinkWorks() { + assertFalse(DirectLinkRepair.shouldRepair(Outcome.REFUSED, false, false)); + } + + @Test + public void answerNeedsNoRepair() { + assertFalse(DirectLinkRepair.shouldRepair(Outcome.ANSWERS, false, false)); + } + + @Test + public void gatewayHasNoDirectLink() { + assertFalse(DirectLinkRepair.shouldRepair(Outcome.NO_ANSWER, true, false)); + } + + @Test + public void repairIsUsedUntilAnAnswer() { + assertTrue(DirectLinkRepair.triedAfter(false, true, false)); // repaired, still no answer + assertTrue(DirectLinkRepair.triedAfter(true, false, false)); // the run of "no" goes on + assertFalse(DirectLinkRepair.triedAfter(true, false, true)); // an answer ends the run + assertFalse(DirectLinkRepair.triedAfter(false, false, false)); // refused: no repair used + } +} From 7585df03b9cbc534ede66c9b52e1494facaff0ec Mon Sep 17 00:00:00 2001 From: Luis Guzman Date: Fri, 20 Nov 2026 08:30:42 -0600 Subject: [PATCH 5/6] K2GO-587 refactor(clone): PING period and dead time in one place PeerAlive owns PING_MS; DEAD_MS = 3 * PING_MS. --- .../org/appdevforall/k2go/sync/cable/data/CableMux.java | 7 +++---- .../org/appdevforall/k2go/sync/cable/domain/PeerAlive.java | 7 +++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/CableMux.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/CableMux.java index a5e2912e1..e64425bcd 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/CableMux.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/CableMux.java @@ -71,7 +71,6 @@ public interface Opener { private volatile long lastRx; // last bytes from the cable, or the end of local work private volatile boolean peerPings; // the peer runs the keepalive (an older K2Go does not) private volatile boolean busy; // the read thread writes to a local socket - private static final long PING_MS = 1_000; public CableMux(UsbLink link, Opener opener, Listener listener) { this.link = link; @@ -234,13 +233,13 @@ private void handle(CableFrame f) throws IOException { /** * A peer that dies (app killed, phone off) does not always end our blocked USB read, so the - * link would look alive forever. Each side sends PING every PING_MS; PeerAlive decides when - * silence closes the link. + * link would look alive forever. Each side sends PING every PeerAlive.PING_MS; PeerAlive + * decides when silence closes the link. */ private void keepAlive() { try { while (!closed) { - Thread.sleep(PING_MS); + Thread.sleep(PeerAlive.PING_MS); if (closed) return; if (PeerAlive.isDead(peerPings, busy, System.currentTimeMillis() - lastRx)) { Log.w(TAG, "nothing from the peer for " + PeerAlive.DEAD_MS + " ms: link closed"); diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/PeerAlive.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/PeerAlive.java index f10d2cb9f..4b0c194a6 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/PeerAlive.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/domain/PeerAlive.java @@ -11,8 +11,11 @@ public final class PeerAlive { - /** Each side sends PING every 1 s; three missed PINGs mean the peer is gone (K2GO-587). */ - public static final long DEAD_MS = 3_000; + /** Each side sends PING this often (K2GO-587). */ + public static final long PING_MS = 1_000; + + /** Three missed PINGs mean the peer is gone. */ + public static final long DEAD_MS = 3 * PING_MS; private PeerAlive() { } From f89128ab238cfae866b14d82a753ec94259deadf Mon Sep 17 00:00:00 2001 From: Luis Guzman Date: Fri, 20 Nov 2026 09:10:14 -0600 Subject: [PATCH 6/6] K2GO-587 fix(clone): keep the connect error in the pull log The reachability warning at pull start lost the reason when SenderReach replaced the inline socket check. --- .../k2go/sync/cable/data/SenderReach.java | 43 +++++++++++++------ .../sync/presentation/SyncStateViewModel.java | 5 ++- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java index 7e0ae3f43..9f73455dc 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/cable/data/SenderReach.java @@ -31,14 +31,27 @@ public final class SenderReach { private static final String TAG = "SenderReach"; - /** One check: does the sender answer, and did this check use the repair. */ + /** One check: does the sender answer, did this check use the repair, and why not, for the log. */ public static final class Result { public final boolean answers; public final boolean repaired; + public final String why; // the last connect error, null when the sender answers - Result(boolean answers, boolean repaired) { + Result(boolean answers, boolean repaired, String why) { this.answers = answers; this.repaired = repaired; + this.why = why; + } + } + + /** One connect: its outcome and the error text. */ + private static final class Try { + final Outcome outcome; + final String why; + + Try(Outcome outcome, String why) { + this.outcome = outcome; + this.why = why; } } @@ -50,15 +63,16 @@ private SenderReach() { * comes from DirectLinkRepair.triedAfter, or false for a check that starts its own run. */ public static Result connect(Context c, String host, int port, int timeoutMs, boolean triedInThisRun) { - if (host == null) return new Result(false, false); - Outcome first = outcome(host, port, timeoutMs); - if (first == Outcome.ANSWERS) return new Result(true, false); + if (host == null) return new Result(false, false, "no address"); + Try first = attempt(host, port, timeoutMs); + if (first.outcome == Outcome.ANSWERS) return new Result(true, false, null); WifiManager wm = (WifiManager) c.getApplicationContext().getSystemService(Context.WIFI_SERVICE); - if (!DirectLinkRepair.shouldRepair(first, host.equals(gateway(wm)), triedInThisRun)) { - return new Result(false, false); + if (!DirectLinkRepair.shouldRepair(first.outcome, host.equals(gateway(wm)), triedInThisRun) + || !dropDirectLink(wm, host)) { + return new Result(false, false, first.why); } - if (!dropDirectLink(wm, host)) return new Result(false, false); - return new Result(outcome(host, port, timeoutMs) == Outcome.ANSWERS, true); + Try second = attempt(host, port, timeoutMs); + return new Result(second.outcome == Outcome.ANSWERS, true, second.why); } /** The Wi-Fi gateway (a router, or the sender on its own hotspot), or null with no DHCP lease. */ @@ -69,15 +83,16 @@ public static String gateway(WifiManager wm) { return (le & 0xFF) + "." + ((le >> 8) & 0xFF) + "." + ((le >> 16) & 0xFF) + "." + ((le >> 24) & 0xFF); } - private static Outcome outcome(String host, int port, int timeoutMs) { + private static Try attempt(String host, int port, int timeoutMs) { try (Socket s = new Socket()) { s.connect(new InetSocketAddress(host, port), timeoutMs); - return Outcome.ANSWERS; + return new Try(Outcome.ANSWERS, null); } catch (Exception e) { - // No log: before pairing the receiver checks the router every few seconds. + // No log here: before pairing the receiver checks the router every few seconds. // Android reports a refusal as ConnectException "... ECONNREFUSED (Connection refused)". - boolean refused = e instanceof ConnectException && String.valueOf(e.getMessage()).contains("ECONNREFUSED"); - return refused ? Outcome.REFUSED : Outcome.NO_ANSWER; + String why = String.valueOf(e.getMessage()); + boolean refused = e instanceof ConnectException && why.contains("ECONNREFUSED"); + return new Try(refused ? Outcome.REFUSED : Outcome.NO_ANSWER, why); } } diff --git a/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java b/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java index 94ccda9b1..b48f7bb7a 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/sync/presentation/SyncStateViewModel.java @@ -112,8 +112,9 @@ public void startProbe(Context appCtx, ShareConfig shareConfig, SyncHandshakeHel WifiNetworkBinder.bindToHostNetwork(appCtx, creds.ip); // A pull start is its own run of checks: it may use one direct-link repair (K2GO-587). - boolean reachable = SenderReach.connect(appCtx, creds.ip, creds.port, 2000, false).answers; - if (!reachable) Log.w(TAG, "Reachability probe to " + creds.ip + ":" + creds.port + " failed"); + SenderReach.Result reach = SenderReach.connect(appCtx, creds.ip, creds.port, 2000, false); + boolean reachable = reach.answers; + if (!reachable) Log.w(TAG, "Reachability probe to " + creds.ip + ":" + creds.port + " failed: " + reach.why); // K2GO-576: no Wi-Fi path, but a cable link: the dry-run goes over the cable. String probeHost = creds.ip; int probePort = creds.port;