microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.5.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/android/androidEmulatorManager.ts

161lines · modeblame

68a5b8d5JiglioNero5 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import { AdbHelper } from "./adb";
5import { ChildProcess } from "../../common/node/childProcess";
6import { IVirtualDevice, VirtualDeviceManager } from "../VirtualDeviceManager";
7import { OutputChannelLogger } from "../log/OutputChannelLogger";
8import * as nls from "vscode-nls";
507fd5fdJiglioNero5 years ago9import { ErrorHelper } from "../../common/error/errorHelper";
10import { InternalErrorCode } from "../../common/error/internalErrorCode";
8df5011eYuri Skorokhodov5 years ago11import { QuickPickOptions, window } from "vscode";
34472878RedMickey5 years ago12nls.config({
13messageFormat: nls.MessageFormat.bundle,
14bundleFormat: nls.BundleFormat.standalone,
15})();
68a5b8d5JiglioNero5 years ago16const localize = nls.loadMessageBundle();
34472878RedMickey5 years ago17
18// eslint-disable-next-line @typescript-eslint/no-empty-interface
19export interface IAndroidEmulator extends IVirtualDevice {}
68a5b8d5JiglioNero5 years ago20
21export class AndroidEmulatorManager extends VirtualDeviceManager {
22private static readonly EMULATOR_COMMAND = "emulator";
23private static readonly EMULATOR_LIST_AVDS_COMMAND = `-list-avds`;
24private static readonly EMULATOR_AVD_START_COMMAND = `-avd`;
25
26private static readonly EMULATOR_START_TIMEOUT = 120;
27
34472878RedMickey5 years ago28private logger: OutputChannelLogger = OutputChannelLogger.getChannel(
29OutputChannelLogger.MAIN_CHANNEL_NAME,
30true,
31);
68a5b8d5JiglioNero5 years ago32
33private adbHelper: AdbHelper;
34private childProcess: ChildProcess;
35
36constructor(adbHelper: AdbHelper) {
37super();
38this.adbHelper = adbHelper;
39this.childProcess = new ChildProcess();
40}
41
42public async startEmulator(target: string): Promise<IAndroidEmulator | null> {
43const onlineDevices = await this.adbHelper.getOnlineDevices();
34472878RedMickey5 years ago44for (let i = 0; i < onlineDevices.length; i++) {
68a5b8d5JiglioNero5 years ago45if (onlineDevices[i].id === target) {
34472878RedMickey5 years ago46return { id: onlineDevices[i].id };
68a5b8d5JiglioNero5 years ago47}
48}
49if (target && (await this.adbHelper.getOnlineDevices()).length === 0) {
50if (target === "simulator") {
51const newEmulator = await this.startSelection();
52if (newEmulator) {
53const emulatorId = await this.tryLaunchEmulatorByName(newEmulator);
34472878RedMickey5 years ago54return { name: newEmulator, id: emulatorId };
68a5b8d5JiglioNero5 years ago55}
34472878RedMickey5 years ago56} else if (!target.includes("device")) {
68a5b8d5JiglioNero5 years ago57const emulatorId = await this.tryLaunchEmulatorByName(target);
34472878RedMickey5 years ago58return { name: target, id: emulatorId };
68a5b8d5JiglioNero5 years ago59}
60}
61return null;
62}
63
64public async tryLaunchEmulatorByName(emulatorName: string): Promise<string> {
65return new Promise((resolve, reject) => {
34472878RedMickey5 years ago66const emulatorProcess = this.childProcess.spawn(
67AndroidEmulatorManager.EMULATOR_COMMAND,
68[AndroidEmulatorManager.EMULATOR_AVD_START_COMMAND, emulatorName],
69{
70detached: true,
71},
72true,
73);
74emulatorProcess.outcome.catch(error => {
75if (
76process.platform == "win32" &&
77process.env.SESSIONNAME &&
78process.env.SESSIONNAME.toLowerCase().includes("rdp-tcp")
79) {
80this.logger.warning(
81localize(
82"RDPEmulatorWarning",
83"Android emulator was launched from the Windows RDP session, this might lead to failures.",
84),
85);
507fd5fdJiglioNero5 years ago86}
34472878RedMickey5 years ago87reject(
88ErrorHelper.getInternalError(
89InternalErrorCode.VirtualDeviceSelectionError,
90error,
91),
92);
68a5b8d5JiglioNero5 years ago93});
119d7878JiglioNero5 years ago94emulatorProcess.spawnedProcess.unref();
68a5b8d5JiglioNero5 years ago95
96const rejectTimeout = setTimeout(() => {
97cleanup();
34472878RedMickey5 years ago98reject(
99ErrorHelper.getInternalError(
100InternalErrorCode.VirtualDeviceSelectionError,
101localize(
102"EmulatorStartWarning",
103"Could not start the emulator {0} within {1} seconds.",
104emulatorName,
105AndroidEmulatorManager.EMULATOR_START_TIMEOUT,
106),
107),
108);
68a5b8d5JiglioNero5 years ago109}, AndroidEmulatorManager.EMULATOR_START_TIMEOUT * 1000);
110
111const bootCheckInterval = setInterval(async () => {
112const connectedDevices = await this.adbHelper.getOnlineDevices();
113if (connectedDevices.length > 0) {
34472878RedMickey5 years ago114this.logger.info(
115localize("EmulatorLaunched", "Launched emulator {0}", emulatorName),
116);
68a5b8d5JiglioNero5 years ago117cleanup();
118resolve(connectedDevices[0].id);
119}
120}, 1000);
121
122const cleanup = () => {
123clearTimeout(rejectTimeout);
124clearInterval(bootCheckInterval);
125};
126});
127}
128
129public startSelection(): Promise<string | undefined> {
130return this.selectVirtualDevice();
131}
132
8df5011eYuri Skorokhodov5 years ago133public async selectOnlineDevice(): Promise<string | undefined> {
134const emulatorsList = (await this.adbHelper.getOnlineDevices()).map(device => device.id);
135const quickPickOptions: QuickPickOptions = {
136ignoreFocusOut: true,
137canPickMany: false,
138placeHolder: localize("SelectOnlineDevice", "Select online Android device"),
139};
140let result: string | undefined = emulatorsList[0];
141if (emulatorsList.length > 1) {
142result = await window.showQuickPick(emulatorsList, quickPickOptions);
143}
144return result?.toString();
145}
146
68a5b8d5JiglioNero5 years ago147protected async getVirtualDevicesNamesList(): Promise<string[]> {
34472878RedMickey5 years ago148const res = await this.childProcess.execToString(
149`${AndroidEmulatorManager.EMULATOR_COMMAND} ${AndroidEmulatorManager.EMULATOR_LIST_AVDS_COMMAND}`,
150);
68a5b8d5JiglioNero5 years ago151let emulatorsList: string[] = [];
152if (res) {
153emulatorsList = res.split(/\r?\n|\r/g);
154const indexOfBlank = emulatorsList.indexOf("");
155if (emulatorsList.indexOf("") >= 0) {
156emulatorsList.splice(indexOfBlank, 1);
157}
158}
159return emulatorsList;
160}
161}