public async collectTargets()

in src/extension/android/androidTargetManager.ts [88:134]


    public async collectTargets(targetType?: TargetType): Promise<void> {
        const targetList: IMobileTarget[] = [];
        const collectSimulators = !targetType || targetType === TargetType.Simulator;
        const collectDevices = !targetType || targetType === TargetType.Device;

        try {
            if (collectSimulators) {
                const emulatorsNames: string[] = await this.adbHelper.getAvdsNames();
                targetList.push(
                    ...emulatorsNames.map(name => ({
                        name,
                        isOnline: false,
                        isVirtualTarget: true,
                    })),
                );
            }
        } catch (error) {
            // We throw an exception only if the target type is explicitly specified,
            // otherwise we collect only those targets that we can collect
            if (targetType === TargetType.Simulator) {
                throw error;
            }
            this.logger.warning(
                localize(
                    "CouldNotUseEmulators",
                    "An error occurred while trying to get installed emulators: {0}\nContinue using only online targets",
                    error instanceof Error ? error.message : error.toString(),
                ),
            );
        }

        const onlineTargets = await this.adbHelper.getOnlineTargets();
        for (const device of onlineTargets) {
            if (device.isVirtualTarget && collectSimulators) {
                const avdName = await this.adbHelper.getAvdNameById(device.id);
                const emulatorTarget = targetList.find(target => target.name === avdName);
                if (emulatorTarget) {
                    emulatorTarget.isOnline = true;
                    emulatorTarget.id = device.id;
                }
            } else if (!device.isVirtualTarget && collectDevices) {
                targetList.push({ id: device.id, isOnline: true, isVirtualTarget: false });
            }
        }

        this.targets = targetList;
    }