mirror of
https://github.com/adelphes/android-dev-ext.git
synced 2025-12-24 02:19:15 +00:00
Support for device picker during launch (#86)
This commit is contained in:
@@ -305,6 +305,19 @@ class AndroidDebugSession extends DebugSession {
|
||||
}
|
||||
}
|
||||
|
||||
extractTargetDeviceID(s) {
|
||||
if (!s || typeof s !== 'string') {
|
||||
return '';
|
||||
}
|
||||
// the device picker returns a stringified object
|
||||
try {
|
||||
const o = JSON.parse(s);
|
||||
return o.serial || s;
|
||||
} catch {
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
async attachRequest(response, args) {
|
||||
this.debug_mode = 'attach';
|
||||
if (args && args.trace) {
|
||||
@@ -313,6 +326,14 @@ class AndroidDebugSession extends DebugSession {
|
||||
}
|
||||
D(`Attach: ${JSON.stringify(args)}`);
|
||||
|
||||
if (args.targetDevice === 'null') {
|
||||
// "null" is returned from the device picker if there's an error or if the
|
||||
// user cancels.
|
||||
D('targetDevice === "null"');
|
||||
this.sendEvent(new TerminatedEvent(false));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!args.processId) {
|
||||
this.LOG(`Attach failed: Missing "processId" property in launch.json`);
|
||||
this.sendEvent(new TerminatedEvent(false));
|
||||
@@ -348,7 +369,7 @@ class AndroidDebugSession extends DebugSession {
|
||||
try {
|
||||
let { processId, targetDevice } = attach_info;
|
||||
if (!targetDevice) {
|
||||
targetDevice = args.targetDevice;
|
||||
targetDevice = this.extractTargetDeviceID(args.targetDevice);
|
||||
}
|
||||
// make sure ADB exists and is started and look for a connected device
|
||||
await checkADBStarted(args.autoStartADB !== false);
|
||||
@@ -416,6 +437,14 @@ class AndroidDebugSession extends DebugSession {
|
||||
}
|
||||
D(`Launch: ${JSON.stringify(args)}`);
|
||||
|
||||
if (args.targetDevice === 'null') {
|
||||
// "null" is returned from the device picker if there's an error or if the
|
||||
// user cancels.
|
||||
D('targetDevice === "null"');
|
||||
this.sendEvent(new TerminatedEvent(false));
|
||||
return;
|
||||
}
|
||||
|
||||
// app_src_root must end in a path-separator for correct validation of sub-paths
|
||||
this.app_src_root = ensure_path_end_slash(args.appSrcRoot);
|
||||
this.apk_fpn = args.apkFile;
|
||||
@@ -465,7 +494,8 @@ class AndroidDebugSession extends DebugSession {
|
||||
|
||||
// make sure ADB exists and is started and look for a device to install on
|
||||
await checkADBStarted(args.autoStartADB !== false);
|
||||
this._device = await this.findSuitableDevice(args.targetDevice, true);
|
||||
const targetDevice = this.extractTargetDeviceID(args.targetDevice);
|
||||
this._device = await this.findSuitableDevice(targetDevice, true);
|
||||
this._device.adbclient = new ADBClient(this._device.serial);
|
||||
|
||||
// install the APK we are going to debug
|
||||
|
||||
@@ -9,7 +9,7 @@ class BuildInfo {
|
||||
* @param {string} pkgname
|
||||
* @param {Map<string,PackageInfo>} packages
|
||||
* @param {string} launchActivity
|
||||
* @param {string[]} amCommandArgs custom arguments passed to `am start`
|
||||
* @param {string[]} [amCommandArgs] custom arguments passed to `am start`
|
||||
*/
|
||||
constructor(pkgname, packages, launchActivity, amCommandArgs) {
|
||||
this.pkgname = pkgname;
|
||||
|
||||
@@ -26,9 +26,10 @@ async function showDevicePicker(vscode, devices) {
|
||||
/**
|
||||
*
|
||||
* @param {import('vscode')} vscode
|
||||
* @param {'Attach'|'Logcat display'} action
|
||||
* @param {'Launch'|'Attach'|'Logcat display'} action
|
||||
* @param {{alwaysShow:boolean}} [options]
|
||||
*/
|
||||
async function selectTargetDevice(vscode, action) {
|
||||
async function selectTargetDevice(vscode, action, options) {
|
||||
const devices = await new ADBClient().list_devices();
|
||||
let device;
|
||||
switch(devices.length) {
|
||||
@@ -36,14 +37,20 @@ async function selectTargetDevice(vscode, action) {
|
||||
vscode.window.showWarningMessage(`${action} failed. No Android devices are connected.`);
|
||||
return null;
|
||||
case 1:
|
||||
return devices[0]; // only one device - just use it
|
||||
if (!options || !options.alwaysShow) {
|
||||
return devices[0]; // only one device - just use it
|
||||
}
|
||||
break;
|
||||
}
|
||||
device = await showDevicePicker(vscode, devices);
|
||||
if (!device) {
|
||||
return null; // user cancelled
|
||||
}
|
||||
// the user might take a while to choose the device, so once
|
||||
// chosen, recheck it exists
|
||||
const current_devices = await new ADBClient().list_devices();
|
||||
if (!current_devices.find(d => d.serial === device.serial)) {
|
||||
vscode.window.showInformationMessage(`${action} failed. The target device is disconnected`);
|
||||
vscode.window.showInformationMessage(`${action} failed. The target device is disconnected.`);
|
||||
return null;
|
||||
}
|
||||
return device;
|
||||
|
||||
Reference in New Issue
Block a user