aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/invalid/lena/scrcpy/Main.java
blob: eb8d40001c04d44894bbfba72b5a97606eff0198 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package invalid.lena.scrcpy;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowInsets;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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.
public final class Main extends Activity {

    private volatile Adb adb;
    private ArrayAdapter<Devices.Device> adapter;
    private Button pairButton;

    @Override
    protected void onCreate(Bundle saved) {
        super.onCreate(saved);
        setContentView(R.layout.main);

        // The IME inset is in the mask too: the window is edge-to-edge, so
        // nothing resizes it when the keyboard opens. Padding the root
        // shrinks the weighted device list and keeps the form reachable.
        Ui.padForInsets(findViewById(R.id.root),
                WindowInsets.Type.systemBars() | WindowInsets.Type.ime());

        EditText host        = findViewById(R.id.host);
        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);
        TextView devicesEmpty = findViewById(R.id.devices_empty);

        settingsBtn.setOnClickListener(v -> startActivity(
                new Intent(this, SettingsActivity.class)));

        adapter = new ArrayAdapter<>(this, R.layout.device_row, R.id.device_label, Devices.load(this));
        devices.setAdapter(adapter);
        devices.setEmptyView(devicesEmpty);

        // RSA keygen on first launch can take 1-3 s; never on the UI thread.
        pairButton.setEnabled(false);
        new Thread(() -> {
            try {
                Adb a = Adb.getInstance(this);
                runOnUiThread(() -> {
                    adb = a;
                    pairButton.setEnabled(true);
                });
            } catch (Exception e) {
                Log.e(e, "adb init failed");
                runOnUiThread(() -> {
                    Toast.makeText(this, "adb init failed: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                    finish();
                });
            }
        }, "adb-init").start();

        devices.setOnItemClickListener((parent, view, pos, id) -> {
            Devices.Device d = adapter.getItem(pos);
            Log.i("connect tap: %s", d);
            Intent i = new Intent(this, Mirror.class);
            i.putExtra(Mirror.EXTRA_HOST, d.host);
            i.putExtra(Mirror.EXTRA_PORT, d.port);
            startActivity(i);
        });

        devices.setOnItemLongClickListener((parent, view, pos, id) -> {
            Devices.Device d = adapter.getItem(pos);
            new AlertDialog.Builder(this)
                    .setTitle(R.string.forget_device)
                    .setMessage(d.host + ":" + d.port)
                     .setPositiveButton(android.R.string.ok, (dlg, w) -> {
                         Log.i("forget device: %s", d);
                        try {
                            List<Devices.Device> updated = Devices.remove(this, d);
                            adapter.clear();
                            adapter.addAll(updated);
                            adapter.notifyDataSetChanged();
                        } catch (Exception e) {
                            Log.e(e, "forget device: save failed");
                            Toast.makeText(this, "could not save device list",
                                    Toast.LENGTH_LONG).show();
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, null)
                    .show();
            return true;
        });

        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();
                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();
                return;
            }
            if (pairP < 1 || pairP > 65535 || connP < 1 || connP > 65535) {
                Toast.makeText(this, "ports must be 1-65535", Toast.LENGTH_SHORT).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();
        });
    }

    private void pairAndSave(String host, int pairPort, String code, int connPort, Button btn) {
        try {
            Log.i("pair: %s:%d", host, pairPort);
            boolean ok = adb.pair(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);
            runOnUiThread(() -> {
                adapter.clear();
                adapter.addAll(updated);
                adapter.notifyDataSetChanged();
                Toast.makeText(this, "paired and saved", Toast.LENGTH_SHORT).show();
                btn.setEnabled(true);
            });
        } catch (Exception e) {
            Log.e(e, "pair failed");
            runOnUiThread(() -> {
                Toast.makeText(this, "pair failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
                btn.setEnabled(true);
            });
        }
    }
}