Skip to content

Commit

Permalink
Guide installation of server extension when required, better error ha…
Browse files Browse the repository at this point in the history
…ndling
  • Loading branch information
nisargjhaveri committed Jan 9, 2025
1 parent 2989ec9 commit 45071e5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
19 changes: 16 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { TcpDeviceManager } from 'remote-adb/client';
import { RemoteAdbDeviceWrapper, RemoteAndroidDeviceListManager } from './remoteAndroidDevices';
import { logger } from './logger';

function logAndShowError(message: string) {
logger.error(message);
vscode.window.showErrorMessage(message);
}

export function activate(context: vscode.ExtensionContext) {
setRemoteAdbLogger(logger);

Expand All @@ -13,11 +18,19 @@ export function activate(context: vscode.ExtensionContext) {
});

context.subscriptions.push(vscode.commands.registerCommand('remote-adb.connectDevice', async (treeItem: RemoteAdbDeviceWrapper) => {
await remoteAndroidDeviceListManager.connect(treeItem);
try {
await remoteAndroidDeviceListManager.connect(treeItem);
} catch (e: any) {
logAndShowError("Failed to connect to device: " + e.message);
}
}));

context.subscriptions.push(vscode.commands.registerCommand('remote-adb.disconnectDevice', async (treeItem: RemoteAdbDeviceWrapper) => {
await remoteAndroidDeviceListManager.disconnect(treeItem);
try {
await remoteAndroidDeviceListManager.disconnect(treeItem);
} catch (e: any) {
logAndShowError("Failed to disconnect device: " + e.message);
}
}));

context.subscriptions.push(vscode.commands.registerCommand('remote-adb.addTcpDevice', async () => {
Expand All @@ -32,7 +45,7 @@ export function activate(context: vscode.ExtensionContext) {
let device = await TcpDeviceManager.createDevice(serial);

if (!device) {
throw new Error(`Cannot connect to '${serial}'. Please enter a valid hostname and port to connect`);
logAndShowError(`Cannot connect to '${serial}'. Please enter a valid hostname and port to connect`);
}
}));

Expand Down
66 changes: 61 additions & 5 deletions src/serverConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,66 @@ import { logger } from './logger';

let _serverConnection: ServerConnection|undefined = undefined;

const companionExtensionId = "nisargjhaveri.remote-adb-server";
const companionExtensionName = "Remote Android Debugging (Server)";

async function isCompanionAvailable(): Promise<boolean> {
try {
await vscode.commands.executeCommand("remote-adb-server.activate");
return true;
} catch (e) {
return false;
}
}

async function promptInstallCompanion(): Promise<boolean> {
logger.log("Showing prompt to install companion");

try {
logger.log(`Installing companion extension (${companionExtensionId})`);
await vscode.commands.executeCommand("workbench.extensions.installExtension", companionExtensionId, {
justification: "This is required to connect local Android device to remote for debugging.",
enable: true,
});
await new Promise((resolve) => setTimeout(resolve, 1000));
} catch (e) {
logger.error("Error installing companion extension:", e);
}

if (!await isCompanionAvailable()) {
const OPEN = "Install Extension";
vscode.window.showErrorMessage(
`[${companionExtensionName}](https://marketplace.visualstudio.com/items?itemName=${companionExtensionId}) extension is required. Please install manually and try again.`,
OPEN
).then((choice) => {
if (choice === OPEN) {
// vscode.commands.executeCommand("workbench.extensions.search", `@id:${companionExtensionId}`);
vscode.env.openExternal(vscode.Uri.parse(`vscode:extension/${companionExtensionId}`));
}
});

return false;
}

logger.log("Companion extension installed.");
return true;
}

async function getServerUri(): Promise<vscode.Uri> {
if (!await isCompanionAvailable()) {
if (!await promptInstallCompanion()) {
throw new Error(`Could not install and enable extension: [${companionExtensionName}](https://marketplace.visualstudio.com/items?itemName=${companionExtensionId})`);
}
}

const uri: vscode.Uri|undefined = await vscode.commands.executeCommand("remote-adb-server.getExternalUrl");
if (!uri) {
throw new Error("Could not start remote-adb server and get url");
}

return uri;
}

function getPassword(password?: string) {
//TODO: Ask for password if not provided?
return password;
Expand Down Expand Up @@ -52,11 +112,7 @@ export async function getServerConnection(): Promise<ServerConnection> {

if (!_serverConnection) {
logger.log("Getting server url from companion extension");
let uri: vscode.Uri|undefined = await vscode.commands.executeCommand("remote-adb.getExternalUrl");

if (!uri) {
throw new Error("Could not get server url");
}
let uri: vscode.Uri = await getServerUri();

_serverConnection = await createServerConnection(uri.toString());
}
Expand Down

0 comments on commit 45071e5

Please sign in to comment.