diff --git a/lib/common/definitions/mobile.d.ts b/lib/common/definitions/mobile.d.ts index 3fa23012fa..532c3aee2d 100644 --- a/lib/common/definitions/mobile.d.ts +++ b/lib/common/definitions/mobile.d.ts @@ -730,6 +730,12 @@ declare module Mobile { * @returns {Promise} Starts the emulator and returns the errors if some error occurs. */ startEmulator(options: Mobile.IStartEmulatorOptions): Promise; + + /** + * Called when emulator is lost. Its purpose is to clean any resources used by the instance. + * @returns {void} + */ + detach?(deviceInfo: Mobile.IDeviceInfo): void; } interface IStartEmulatorOutput { @@ -772,6 +778,11 @@ declare module Mobile { * @param imageIdentifier - The imagerIdentifier of the emulator. */ startEmulatorArgs(imageIdentifier: string): string[]; + /** + * Called when emulator is lost. Its purpose is to clean any resources used by the instance. + * @returns {void} + */ + detach?(deviceInfo: Mobile.IDeviceInfo): void; } interface IVirtualBoxService { diff --git a/lib/common/mobile/android/android-device.ts b/lib/common/mobile/android/android-device.ts index 1a2b87cc1d..33fa17a2d5 100644 --- a/lib/common/mobile/android/android-device.ts +++ b/lib/common/mobile/android/android-device.ts @@ -121,6 +121,12 @@ export class AndroidDevice implements Mobile.IAndroidDevice { } } + public detach(): void { + if (this.isEmulator) { + this.$androidEmulatorServices.detach(this.deviceInfo); + } + } + private async getDeviceDetails(shellCommandArgs: string[]): Promise { const parsedDetails: any = {}; diff --git a/lib/common/mobile/android/android-emulator-services.ts b/lib/common/mobile/android/android-emulator-services.ts index 48648c50be..0b193cd135 100644 --- a/lib/common/mobile/android/android-emulator-services.ts +++ b/lib/common/mobile/android/android-emulator-services.ts @@ -59,6 +59,10 @@ export class AndroidEmulatorServices implements Mobile.IEmulatorPlatformService }; } + public detach(deviceInfo: Mobile.IDeviceInfo) { + this.$androidVirtualDeviceService.detach(deviceInfo); + } + private async startEmulatorCore(options: Mobile.IAndroidStartEmulatorOptions): Promise<{runningEmulator: Mobile.IDeviceInfo, errors: string[], endTimeEpoch: number}> { const timeout = options.timeout || AndroidVirtualDevice.TIMEOUT_SECONDS; const endTimeEpoch = getCurrentEpochTime() + this.$utils.getMilliSecondsTimeout(timeout); diff --git a/lib/common/mobile/android/android-virtual-device-service.ts b/lib/common/mobile/android/android-virtual-device-service.ts index a81a391c6c..c98b859131 100644 --- a/lib/common/mobile/android/android-virtual-device-service.ts +++ b/lib/common/mobile/android/android-virtual-device-service.ts @@ -142,6 +142,12 @@ export class AndroidVirtualDeviceService implements Mobile.IAndroidVirtualDevice }); } + public detach(deviceInfo: Mobile.IDeviceInfo) { + if (this.mapEmulatorIdToImageIdentifier[deviceInfo.identifier]) { + delete this.mapEmulatorIdToImageIdentifier[deviceInfo.identifier]; + } + } + private async getEmulatorImagesCore(): Promise { let result: ISpawnResult = null; let devices: Mobile.IDeviceInfo[] = [];