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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions controller/app/src/main/java/org/appdevforall/k2go/QrActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import java.util.List;

import org.appdevforall.k2go.hotspot.domain.WifiQr;

public class QrActivity extends AppCompatActivity {

private TextView titleText;
Expand Down Expand Up @@ -164,12 +166,7 @@ private void fetchNetworkInterfaces() {
* SSID/password are auto-generated by the OS and cannot be chosen in advance.
*/
private void showWifiJoin(String ssid, String pass) {
String payload;
if (pass != null && !pass.isEmpty()) {
payload = "WIFI:T:WPA;S:" + escapeWifi(ssid) + ";P:" + escapeWifi(pass) + ";;";
} else {
payload = "WIFI:T:nopass;S:" + escapeWifi(ssid) + ";;";
}
String payload = WifiQr.payload(ssid, pass);
titleText.setText(getString(R.string.qr_title_join_wifi));
String creds = getString(R.string.qr_wifi_ssid, ssid);
if (pass != null && !pass.isEmpty()) {
Expand All @@ -182,20 +179,6 @@ private void showWifiJoin(String ssid, String pass) {
}
}

/** Escapes the WIFI: QR reserved characters: backslash, semicolon, comma, colon, quote. */
private static String escapeWifi(String v) {
if (v == null) return "";
StringBuilder sb = new StringBuilder(v.length());
for (int i = 0; i < v.length(); i++) {
char c = v.charAt(i);
if (c == '\\' || c == ';' || c == ',' || c == ':' || c == '"') {
sb.append('\\');
}
sb.append(c);
}
return sb.toString();
}

/**
* Generates a pure Black & White Bitmap using ZXing.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@
import org.appdevforall.k2go.contentexchange.domain.ItemRef;
import org.appdevforall.k2go.contentexchange.domain.ManagedContent;
import org.appdevforall.k2go.contentexchange.domain.StorageStatus;
import org.appdevforall.k2go.hotspot.HotspotPermissions;
import org.appdevforall.k2go.hotspot.LocalHotspotManager;
import org.appdevforall.k2go.storage.StorageProbe;
import org.appdevforall.k2go.ui.dialog.BrandDialog;
import org.appdevforall.k2go.util.ByteFormatter;
import org.appdevforall.k2go.util.SnackbarDuration;

import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -73,11 +69,6 @@ public class ContentManagerFragment extends Fragment {
private Button deleteButton;
private Button shareButton;

/** The selection captured when Share was tapped, held across the hotspot-permission round-trip. */
private List<ContentItem> pendingShare;
/** Runtime hotspot permission (NEARBY_WIFI_DEVICES / ACCESS_FINE_LOCATION), requested before the
* share foreground service brings the hotspot up (it cannot prompt from a service). */
private ActivityResultLauncher<String[]> hotspotPerm;

/** Set when a delete run is launched, so the manager reloads the list (and reclaimed space) when
* it next resumes. Cleared once the reload runs and the run has settled. */
Expand Down Expand Up @@ -106,8 +97,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
openTree = registerForActivityResult(new ActivityResultContracts.OpenDocumentTree(), this::onTreePicked);
openImportTree = registerForActivityResult(
new ActivityResultContracts.OpenDocumentTree(), this::onImportTreePicked);
hotspotPerm = registerForActivityResult(
new ActivityResultContracts.RequestMultiplePermissions(), result -> onHotspotPermissionResult());
exportButton.setEnabled(true);
exportButton.setOnClickListener(v -> onExportClicked());
importButton.setOnClickListener(v -> onImportClicked());
Expand Down Expand Up @@ -335,11 +324,8 @@ private void onReceiveClicked() {
showImportProgress();
return;
}
// 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 joins and scans; ContentReceiveFragment
// also waits for the release before it connects. K2GO-376.
LocalHotspotManager.get().stop();
// Our own hotspot stays up here: ContentReceiveFragment stops it only when the sender's address
// collides with it (K2GO-376, K2GO-577), so a Wi-Fi share does not cut Connect.
requireActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.k2go_setup_host, new ContentReceiveStartFragment())
.addToBackStack(null)
Expand Down Expand Up @@ -433,41 +419,16 @@ private void showProgress() {

// ---- share ---------------------------------------------------------------------------------

/** Share: if a run is still live, just show it; otherwise capture the selection and make sure the
* hotspot permission is granted (the service cannot prompt) before starting. */
/** Share: if a run is still live, just show it; otherwise start one over the selection. The share
* screen picks Hotspot or Wi-Fi and asks the hotspot permission itself (K2GO-577). */
private void onShareClicked() {
if (ContentShareService.session().hasSession()) {
showShareProgress();
return;
}
List<ContentItem> selected = viewModel.selectedItems();
if (selected.isEmpty()) {
return;
}
pendingShare = selected;
if (!LocalHotspotManager.isSupported()) {
String msg = getString(R.string.k2go_cx_share_hotspot_unsupported);
Snackbar.make(requireView(), msg, SnackbarDuration.millisForText(msg)).show();
return;
}
if (HotspotPermissions.granted(requireContext())) {
startShare(pendingShare);
} else {
hotspotPerm.launch(HotspotPermissions.required());
}
}

/** The hotspot-permission request returned: start when granted, otherwise say why we cannot. */
private void onHotspotPermissionResult() {
if (!isAdded()) {
return;
}
if (pendingShare != null && HotspotPermissions.granted(requireContext())) {
startShare(pendingShare);
} else {
pendingShare = null; // the request was declined; drop the held selection
String msg = getString(R.string.k2go_cx_share_need_permission);
Snackbar.make(requireView(), msg, SnackbarDuration.millisForText(msg)).show();
if (!selected.isEmpty()) {
startShare(selected);
}
}

Expand All @@ -479,7 +440,6 @@ private void startShare(List<ContentItem> selected) {
bytes += selected.get(i).bytes();
}
ContentShareService.start(requireContext(), refs, bytes);
pendingShare = null;
viewModel.clearSelection();
showShareProgress();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,25 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
* the kernel traps the connection on loopback (the local route beats any per-Network binding) and
* the receiver reaches itself instead of the sender ("Could not reach the sender"). Tear our own
* hotspot down and wait until the sender address is no longer one of our interfaces, then fetch.
* The hotspot stays off; only Share starts it again. K2GO-376.
* K2GO-376. Only on that collision (K2GO-577): a sender on the shared Wi-Fi does not touch our
* hotspot, so Connect keeps serving while this phone receives.
*/
private void prepareThenFetch() {
showConnecting();
LocalHotspotManager.get().stop(); // no-op if we are not hosting
waitHostRemoteThenFetch(0);
worker.execute(() -> {
boolean collides = NetworkInterfaces.isLocalAddress(host);
main.post(() -> {
if (!isAdded() || getView() == null) {
return;
}
if (collides) {
LocalHotspotManager.get().stop();
waitHostRemoteThenFetch(0);
} else {
fetchManifest();
}
});
});
}

/** Poll off the main thread until the sender address is no longer a local interface address, then
Expand Down
Loading
Loading