protected async launchSimulator()

in src/extension/android/androidTargetManager.ts [142:212]


    protected async launchSimulator(emulatorTarget: IMobileTarget): Promise<AndroidTarget> {
        return new Promise<AndroidTarget>((resolve, reject) => {
            const emulatorProcess = this.childProcess.spawn(
                AndroidTargetManager.EMULATOR_COMMAND,
                [AndroidTargetManager.EMULATOR_AVD_START_COMMAND, emulatorTarget.name as string],
                {
                    detached: true,
                },
                true,
            );
            emulatorProcess.outcome.catch(error => {
                if (
                    process.platform == "win32" &&
                    process.env.SESSIONNAME &&
                    process.env.SESSIONNAME.toLowerCase().includes("rdp-tcp")
                ) {
                    this.logger.warning(
                        localize(
                            "RDPEmulatorWarning",
                            "Android emulator was launched from the Windows RDP session, this might lead to failures.",
                        ),
                    );
                }
                reject(
                    new Error(`Virtual device launch finished with an exception: ${String(error)}`),
                );
            });
            emulatorProcess.spawnedProcess.unref();

            const condition = async () => {
                const connectedDevices = await this.adbHelper.getOnlineTargets();
                for (const target of connectedDevices) {
                    const onlineAvdName = await this.adbHelper.getAvdNameById(target.id);
                    if (onlineAvdName === emulatorTarget.name) {
                        return target.id;
                    }
                }
                return null;
            };

            void PromiseUtil.waitUntil<string>(
                condition,
                1000,
                AndroidTargetManager.EMULATOR_START_TIMEOUT * 1000,
            ).then(emulatorId => {
                if (emulatorId) {
                    emulatorTarget.id = emulatorId;
                    emulatorTarget.isOnline = true;
                    this.logger.info(
                        localize(
                            "EmulatorLaunched",
                            "Launched Android emulator {0}",
                            emulatorTarget.name,
                        ),
                    );
                    resolve(AndroidTarget.fromInterface(<IDebuggableMobileTarget>emulatorTarget));
                } else {
                    reject(
                        new Error(
                            `Virtual device launch finished with an exception: ${localize(
                                "EmulatorStartWarning",
                                "Could not start the emulator {0} within {1} seconds.",
                                emulatorTarget.name,
                                AndroidTargetManager.EMULATOR_START_TIMEOUT,
                            )}`,
                        ),
                    );
                }
            });
        });
    }