microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.0.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/androidEmulatorManager.ts

107lines · 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";
68a5b8d5JiglioNero5 years ago11nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
12const localize = nls.loadMessageBundle();
13export interface IAndroidEmulator extends IVirtualDevice {
14}
15
16export class AndroidEmulatorManager extends VirtualDeviceManager {
17private static readonly EMULATOR_COMMAND = "emulator";
18private static readonly EMULATOR_LIST_AVDS_COMMAND = `-list-avds`;
19private static readonly EMULATOR_AVD_START_COMMAND = `-avd`;
20
21private static readonly EMULATOR_START_TIMEOUT = 120;
22
23private logger: OutputChannelLogger = OutputChannelLogger.getChannel(OutputChannelLogger.MAIN_CHANNEL_NAME, true);
24
25private adbHelper: AdbHelper;
26private childProcess: ChildProcess;
27
28constructor(adbHelper: AdbHelper) {
29super();
30this.adbHelper = adbHelper;
31this.childProcess = new ChildProcess();
32}
33
34public async startEmulator(target: string): Promise<IAndroidEmulator | null> {
35const onlineDevices = await this.adbHelper.getOnlineDevices();
36for (let i = 0; i < onlineDevices.length; i++){
37if (onlineDevices[i].id === target) {
38return {id: onlineDevices[i].id};
39}
40}
41if (target && (await this.adbHelper.getOnlineDevices()).length === 0) {
42if (target === "simulator") {
43const newEmulator = await this.startSelection();
44if (newEmulator) {
45const emulatorId = await this.tryLaunchEmulatorByName(newEmulator);
46return {name: newEmulator, id: emulatorId};
47}
48}
49else if (!target.includes("device")) {
50const emulatorId = await this.tryLaunchEmulatorByName(target);
51return {name: target, id: emulatorId};
52}
53}
54return null;
55}
56
57public async tryLaunchEmulatorByName(emulatorName: string): Promise<string> {
58return new Promise((resolve, reject) => {
59const emulatorProcess = this.childProcess.spawn(AndroidEmulatorManager.EMULATOR_COMMAND, [AndroidEmulatorManager.EMULATOR_AVD_START_COMMAND, emulatorName], {
60detached: true,
dd8375caJiglioNero5 years ago61}, true);
68a5b8d5JiglioNero5 years ago62emulatorProcess.outcome.catch((error) => {
507fd5fdJiglioNero5 years ago63if (process.platform == "win32" && process.env.SESSIONNAME && process.env.SESSIONNAME.toLowerCase().includes("rdp-tcp")) {
64this.logger.warning(localize("RDPEmulatorWarning", "Android emulator was launched from the Windows RDP session, this might lead to failures."));
65}
66reject(ErrorHelper.getInternalError(InternalErrorCode.VirtualDeviceSelectionError, error));
68a5b8d5JiglioNero5 years ago67});
119d7878JiglioNero5 years ago68emulatorProcess.spawnedProcess.unref();
68a5b8d5JiglioNero5 years ago69
70const rejectTimeout = setTimeout(() => {
71cleanup();
507fd5fdJiglioNero5 years ago72reject(ErrorHelper.getInternalError(InternalErrorCode.VirtualDeviceSelectionError, localize("EmulatorStartWarning", "Could not start the emulator {0} within {1} seconds.", emulatorName, AndroidEmulatorManager.EMULATOR_START_TIMEOUT)));
68a5b8d5JiglioNero5 years ago73}, AndroidEmulatorManager.EMULATOR_START_TIMEOUT * 1000);
74
75const bootCheckInterval = setInterval(async () => {
76const connectedDevices = await this.adbHelper.getOnlineDevices();
77if (connectedDevices.length > 0) {
78this.logger.info(localize("EmulatorLaunched", "Launched emulator {0}", emulatorName));
79cleanup();
80resolve(connectedDevices[0].id);
81}
82}, 1000);
83
84const cleanup = () => {
85clearTimeout(rejectTimeout);
86clearInterval(bootCheckInterval);
87};
88});
89}
90
91public startSelection(): Promise<string | undefined> {
92return this.selectVirtualDevice();
93}
94
95protected async getVirtualDevicesNamesList(): Promise<string[]> {
96const res = await this.childProcess.execToString(`${AndroidEmulatorManager.EMULATOR_COMMAND} ${AndroidEmulatorManager.EMULATOR_LIST_AVDS_COMMAND}`);
97let emulatorsList: string[] = [];
98if (res) {
99emulatorsList = res.split(/\r?\n|\r/g);
100const indexOfBlank = emulatorsList.indexOf("");
101if (emulatorsList.indexOf("") >= 0) {
102emulatorsList.splice(indexOfBlank, 1);
103}
104}
105return emulatorsList;
106}
107}