diff options
Diffstat (limited to 'app/src/main/java/invalid/lena/scrcpy/Main.java')
| -rw-r--r-- | app/src/main/java/invalid/lena/scrcpy/Main.java | 58 |
1 files changed, 27 insertions, 31 deletions
diff --git a/app/src/main/java/invalid/lena/scrcpy/Main.java b/app/src/main/java/invalid/lena/scrcpy/Main.java index eb8d400..28a0870 100644 --- a/app/src/main/java/invalid/lena/scrcpy/Main.java +++ b/app/src/main/java/invalid/lena/scrcpy/Main.java @@ -18,12 +18,14 @@ import java.util.List; // Pairing form + saved-device list. // -// Layout: host, pair-port, pair-code, connect-port + a single "Pair and save" -// button. After a successful pair() against the daemon, the row is appended -// to devices.json with the *connect* port (different from the pair port on -// Android 11+). Tapping a saved row launches the Mirror activity with the -// target host/port; Mirror owns its own Adb instance loaded from the same -// on-disk keypair. +// Layout: device address ("ip:port"), pair-port, pair-code + a single +// "Pair and save" button. The address is the connect endpoint printed on +// the target's Wireless debugging screen; pairing happens on a different, +// short-lived port from the pairing dialog, against the same host. After a +// successful pair() against the daemon, the row is appended to devices.json +// with the *connect* port. Tapping a saved row launches the Mirror activity +// with the target host/port; Mirror owns its own Adb instance loaded from +// the same on-disk keypair. public final class Main extends Activity { private volatile Adb adb; @@ -41,10 +43,9 @@ public final class Main extends Activity { Ui.padForInsets(findViewById(R.id.root), WindowInsets.Type.systemBars() | WindowInsets.Type.ime()); - EditText host = findViewById(R.id.host); + EditText address = findViewById(R.id.device_address); EditText pairPort = findViewById(R.id.pair_port); EditText pairCode = findViewById(R.id.pair_code); - EditText connectPort = findViewById(R.id.connect_port); pairButton = findViewById(R.id.pair); View settingsBtn = findViewById(R.id.settings); ListView devices = findViewById(R.id.devices); @@ -89,7 +90,7 @@ public final class Main extends Activity { Devices.Device d = adapter.getItem(pos); new AlertDialog.Builder(this) .setTitle(R.string.forget_device) - .setMessage(d.host + ":" + d.port) + .setMessage(d.toString()) .setPositiveButton(android.R.string.ok, (dlg, w) -> { Log.i("forget device: %s", d); try { @@ -110,41 +111,36 @@ public final class Main extends Activity { pairButton.setOnClickListener(v -> { if (adb == null) return; // still initialising - String h = host.getText().toString().trim(); - String pp = pairPort.getText().toString().trim(); - String pc = pairCode.getText().toString().trim(); - String cp = connectPort.getText().toString().trim(); - if (TextUtils.isEmpty(h) || TextUtils.isEmpty(pp) - || TextUtils.isEmpty(pc) || TextUtils.isEmpty(cp)) { - Toast.makeText(this, "fill all four fields", Toast.LENGTH_SHORT).show(); + // The saved endpoint is the address field verbatim; pairing + // reuses its host with the pairing port. + Devices.Device target = Devices.parseAddress(address.getText().toString()); + if (target == null) { + Toast.makeText(this, R.string.bad_address, Toast.LENGTH_LONG).show(); return; } - int pairP, connP; - try { - pairP = Integer.parseInt(pp); - connP = Integer.parseInt(cp); - } catch (NumberFormatException e) { - Toast.makeText(this, "ports must be numeric", Toast.LENGTH_SHORT).show(); + int pairP = Devices.parsePort(pairPort.getText().toString()); + if (pairP < 0) { + Toast.makeText(this, R.string.bad_pair_port, Toast.LENGTH_LONG).show(); return; } - if (pairP < 1 || pairP > 65535 || connP < 1 || connP > 65535) { - Toast.makeText(this, "ports must be 1-65535", Toast.LENGTH_SHORT).show(); + String pc = pairCode.getText().toString().trim(); + if (TextUtils.isEmpty(pc)) { + Toast.makeText(this, R.string.bad_pair_code, Toast.LENGTH_LONG).show(); return; } pairButton.setEnabled(false); Toast.makeText(this, R.string.pairing, Toast.LENGTH_SHORT).show(); - new Thread(() -> pairAndSave(h, pairP, pc, connP, pairButton), "pair").start(); + new Thread(() -> pairAndSave(target, pairP, pc, pairButton), "pair").start(); }); } - private void pairAndSave(String host, int pairPort, String code, int connPort, Button btn) { + private void pairAndSave(Devices.Device target, int pairPort, String code, Button btn) { try { - Log.i("pair: %s:%d", host, pairPort); - boolean ok = adb.pair(host, pairPort, code); + Log.i("pair: %s:%d", target.host, pairPort); + boolean ok = adb.pair(target.host, pairPort, code); if (!ok) throw new IllegalStateException("pair returned false"); - Log.i("pair ok host=%s pair_port=%d", host, pairPort); - Devices.Device d = new Devices.Device(host, connPort); - List<Devices.Device> updated = Devices.upsert(this, d); + Log.i("pair ok host=%s pair_port=%d", target.host, pairPort); + List<Devices.Device> updated = Devices.upsert(this, target); runOnUiThread(() -> { adapter.clear(); adapter.addAll(updated); |