microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a2ddbba579382405cbf47e290ee67184a8f155c5

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/androidEmulatorManager.ts

102lines · modecode

1// 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";
9nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
10const localize = nls.loadMessageBundle();
11export interface IAndroidEmulator extends IVirtualDevice {
12}
13
14export class AndroidEmulatorManager extends VirtualDeviceManager {
15 private static readonly EMULATOR_COMMAND = "emulator";
16 private static readonly EMULATOR_LIST_AVDS_COMMAND = `-list-avds`;
17 private static readonly EMULATOR_AVD_START_COMMAND = `-avd`;
18
19 private static readonly EMULATOR_START_TIMEOUT = 120;
20
21 private logger: OutputChannelLogger = OutputChannelLogger.getChannel(OutputChannelLogger.MAIN_CHANNEL_NAME, true);
22
23 private adbHelper: AdbHelper;
24 private childProcess: ChildProcess;
25
26 constructor(adbHelper: AdbHelper) {
27 super();
28 this.adbHelper = adbHelper;
29 this.childProcess = new ChildProcess();
30 }
31
32 public async startEmulator(target: string): Promise<IAndroidEmulator | null> {
33 const onlineDevices = await this.adbHelper.getOnlineDevices();
34 for (let i = 0; i < onlineDevices.length; i++){
35 if (onlineDevices[i].id === target) {
36 return {id: onlineDevices[i].id};
37 }
38 }
39 if (target && (await this.adbHelper.getOnlineDevices()).length === 0) {
40 if (target === "simulator") {
41 const newEmulator = await this.startSelection();
42 if (newEmulator) {
43 const emulatorId = await this.tryLaunchEmulatorByName(newEmulator);
44 return {name: newEmulator, id: emulatorId};
45 }
46 }
47 else if (!target.includes("device")) {
48 const emulatorId = await this.tryLaunchEmulatorByName(target);
49 return {name: target, id: emulatorId};
50 }
51 }
52 return null;
53 }
54
55 public async tryLaunchEmulatorByName(emulatorName: string): Promise<string> {
56 return new Promise((resolve, reject) => {
57 const emulatorProcess = this.childProcess.spawn(AndroidEmulatorManager.EMULATOR_COMMAND, [AndroidEmulatorManager.EMULATOR_AVD_START_COMMAND, emulatorName], {
58 detached: true,
59 });
60 emulatorProcess.outcome.catch((error) => {
61 reject(error);
62 });
63 emulatorProcess.spawnedProcess.unref();
64
65 const rejectTimeout = setTimeout(() => {
66 cleanup();
67 reject(`Could not start the emulator ${emulatorName} within ${AndroidEmulatorManager.EMULATOR_START_TIMEOUT} seconds.`);
68 }, AndroidEmulatorManager.EMULATOR_START_TIMEOUT * 1000);
69
70 const bootCheckInterval = setInterval(async () => {
71 const connectedDevices = await this.adbHelper.getOnlineDevices();
72 if (connectedDevices.length > 0) {
73 this.logger.info(localize("EmulatorLaunched", "Launched emulator {0}", emulatorName));
74 cleanup();
75 resolve(connectedDevices[0].id);
76 }
77 }, 1000);
78
79 const cleanup = () => {
80 clearTimeout(rejectTimeout);
81 clearInterval(bootCheckInterval);
82 };
83 });
84 }
85
86 public startSelection(): Promise<string | undefined> {
87 return this.selectVirtualDevice();
88 }
89
90 protected async getVirtualDevicesNamesList(): Promise<string[]> {
91 const res = await this.childProcess.execToString(`${AndroidEmulatorManager.EMULATOR_COMMAND} ${AndroidEmulatorManager.EMULATOR_LIST_AVDS_COMMAND}`);
92 let emulatorsList: string[] = [];
93 if (res) {
94 emulatorsList = res.split(/\r?\n|\r/g);
95 const indexOfBlank = emulatorsList.indexOf("");
96 if (emulatorsList.indexOf("") >= 0) {
97 emulatorsList.splice(indexOfBlank, 1);
98 }
99 }
100 return emulatorsList;
101 }
102}
103