-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
163 lines (133 loc) · 3.85 KB
/
extension.js
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
const vscode = require("vscode");
const commands = require("./commands");
const functions = require("./functions");
/**
* @param {vscode.ExtensionContext} context
*/
async function activate(context) {
const store = context.globalState;
let defaultDevice = store.get("default_device");
let defaultPort = store.get("port");
if (!defaultPort) {
store.update("port", "5555");
defaultPort = 5555;
}
let fastConnect = vscode.commands.registerCommand(
"android-debug-bridge.fastConnect",
async function () {
try {
if (defaultDevice == null) {
await vscode.commands.executeCommand(
"android-debug-bridge.setDefaultDevice"
);
if (!defaultDevice) return;
}
const devices = await functions.getDevices();
for (let i = 0; i < devices.devices.length; i++) {
const device = devices.devices[i];
if (device.isOffline) continue;
await functions.tcpip(defaultPort, device.serial);
}
await functions.connect(defaultDevice.ip, defaultPort);
vscode.window.showInformationMessage(
`Connected to ${defaultDevice.ip}:${defaultPort}`
);
} catch (err) {
const message = err.name != null ? `${err.name}: ${err.message}` : err;
vscode.window.showErrorMessage(message);
}
}
);
let connect = vscode.commands.registerCommand(
"android-debug-bridge.connect",
function () {
commands.establishConnection(defaultPort);
}
);
let devices = vscode.commands.registerCommand(
"android-debug-bridge.devices",
function () {
commands.showDeviceList("Available Android devices");
}
);
let restart = vscode.commands.registerCommand(
"android-debug-bridge.restart",
commands.restartServer
);
let usb = vscode.commands.registerCommand(
"android-debug-bridge.usb",
async function () {
const devices = await functions.getDevices();
for (let i = 0; i < devices.devices.length; i++) {
const device = devices.devices[i];
if (device.serial.includes(":")) {
await functions.usb(device.serial);
}
}
vscode.window.showInformationMessage("Restarted in usb mode");
}
);
let setDefaultDevice = vscode.commands.registerCommand(
"android-debug-bridge.setDefaultDevice",
async () => {
const device = await commands.showDeviceList(
"Select device to set as default",
true
);
if (!device) return;
device["ip"] = await functions.getDeviceIp(device.serial);
await store.update("default_device", device);
defaultDevice = device;
vscode.window.showInformationMessage(
`Default device set to ${device.label}`
);
}
);
let setPort = vscode.commands.registerCommand(
"android-debug-bridge.setPort",
async function () {
const port = await vscode.window.showInputBox({
placeHolder: "Port number",
prompt: "Port must be between 1024 and 65535",
validateInput: portValidator,
});
if (port && !portValidator(port)) {
await store.update("port", port);
defaultPort = port;
vscode.window.showInformationMessage(
`Port set to ${port}, this port will be used to next connection`
);
}
}
);
context.subscriptions.push(
fastConnect,
connect,
devices,
restart,
usb,
setDefaultDevice,
setPort
);
}
/**
*
* @param {string} value
* @returns
*/
const portValidator = (value) => {
let portNum = parseInt(value.trim());
if (isNaN(portNum)) {
return "Please enter a number";
}
if (portNum < 1024 || portNum > 65535) {
return "Please enter a valid port number";
}
return null;
};
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate,
};