-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathandroid-emulator-services.ts
179 lines (149 loc) · 6.94 KB
/
android-emulator-services.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { AndroidVirtualDevice } from "../../constants";
import { getCurrentEpochTime, sleep } from "../../helpers";
import { EOL } from "os";
export class AndroidEmulatorServices implements Mobile.IEmulatorPlatformService {
constructor(private $androidGenymotionService: Mobile.IAndroidVirtualDeviceService,
private $androidVirtualDeviceService: Mobile.IAndroidVirtualDeviceService,
private $adb: Mobile.IAndroidDebugBridge,
private $childProcess: IChildProcess,
private $emulatorHelper: Mobile.IEmulatorHelper,
private $logger: ILogger,
private $utils: IUtils) { }
public async getEmulatorImages(): Promise<Mobile.IEmulatorImagesOutput> {
const adbDevicesOutput = await this.$adb.getDevicesSafe();
const avdAvailableEmulatorsOutput = await this.$androidVirtualDeviceService.getEmulatorImages(adbDevicesOutput);
const genyAvailableDevicesOutput = await this.$androidGenymotionService.getEmulatorImages(adbDevicesOutput);
const devices = _.concat(avdAvailableEmulatorsOutput.devices, genyAvailableDevicesOutput.devices)
.filter(item => !!item);
return {
devices,
errors: avdAvailableEmulatorsOutput.errors.concat(genyAvailableDevicesOutput.errors)
};
}
public async getRunningEmulatorIds(): Promise<string[]> {
const adbDevicesOutput = await this.$adb.getDevicesSafe();
const avds = await this.$androidVirtualDeviceService.getRunningEmulatorIds(adbDevicesOutput);
const genies = await this.$androidGenymotionService.getRunningEmulatorIds(adbDevicesOutput);
return avds.concat(genies);
}
public async getRunningEmulatorName(emulatorId: string): Promise<string> {
let result = await this.$androidVirtualDeviceService.getRunningEmulatorName(emulatorId);
if (!result) {
result = await this.$androidGenymotionService.getRunningEmulatorName(emulatorId);
}
return result;
}
public async getRunningEmulatorImageIdentifier(emulatorId: string): Promise<string> {
let result = await this.$androidVirtualDeviceService.getRunningEmulatorImageIdentifier(emulatorId);
if (!result) {
result = await this.$androidGenymotionService.getRunningEmulatorImageIdentifier(emulatorId);
}
return result;
}
public async startEmulator(options: Mobile.IAndroidStartEmulatorOptions): Promise<Mobile.IStartEmulatorOutput> {
const output = await this.startEmulatorCore(options);
let bootToCompleteOutput = null;
if (output && output.runningEmulator) {
bootToCompleteOutput = await this.waitForEmulatorBootToComplete(output.runningEmulator, output.endTimeEpoch, options.timeout);
}
return {
errors: ((output && output.errors) || []).concat((bootToCompleteOutput && bootToCompleteOutput.errors) || [])
};
}
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);
const availableEmulators = (await this.getEmulatorImages()).devices;
let emulator = this.$emulatorHelper.getEmulatorByStartEmulatorOptions(options, availableEmulators);
if (!emulator && !options.emulatorIdOrName && !options.imageIdentifier && !options.emulator) {
emulator = this.getBestFit(availableEmulators);
}
if (!emulator) {
return {
runningEmulator: null,
errors: [`No emulator image available for device identifier '${options.emulatorIdOrName || options.imageIdentifier}'.`],
endTimeEpoch
};
}
if (emulator.errorHelp) {
return {
runningEmulator: null,
errors: [emulator.errorHelp],
endTimeEpoch
};
}
this.spawnEmulator(emulator);
const isInfiniteWait = this.$utils.getMilliSecondsTimeout(timeout) === 0;
let hasTimeLeft = getCurrentEpochTime() < endTimeEpoch;
while (hasTimeLeft || isInfiniteWait) {
const emulators = (await this.getEmulatorImages()).devices;
const newEmulator = _.find(emulators, e => e.imageIdentifier === emulator.imageIdentifier);
if (newEmulator && this.$emulatorHelper.isEmulatorRunning(newEmulator)) {
return {
runningEmulator: newEmulator,
errors: [],
endTimeEpoch
};
}
await sleep(10000); // the emulator definitely takes its time to wake up
hasTimeLeft = getCurrentEpochTime() < endTimeEpoch;
}
if (!hasTimeLeft && !isInfiniteWait) {
return {
runningEmulator: null,
errors: [AndroidVirtualDevice.UNABLE_TO_START_EMULATOR_MESSAGE],
endTimeEpoch
};
}
}
private spawnEmulator(emulator: Mobile.IDeviceInfo): void {
let pathToEmulatorExecutable = null;
let startEmulatorArgs = null;
if (emulator.vendor === AndroidVirtualDevice.AVD_VENDOR_NAME) {
pathToEmulatorExecutable = this.$androidVirtualDeviceService.pathToEmulatorExecutable;
startEmulatorArgs = this.$androidVirtualDeviceService.startEmulatorArgs(emulator.imageIdentifier);
} else if (emulator.vendor === AndroidVirtualDevice.GENYMOTION_VENDOR_NAME) {
pathToEmulatorExecutable = this.$androidGenymotionService.pathToEmulatorExecutable;
startEmulatorArgs = this.$androidGenymotionService.startEmulatorArgs(emulator.imageIdentifier);
}
this.$logger.info(`Starting Android emulator with image ${emulator.imageIdentifier}`);
const childProcess = this.$childProcess.spawn(pathToEmulatorExecutable, startEmulatorArgs, { stdio: "ignore", detached: true });
childProcess.unref();
childProcess.on("error", (err: Error) => {
this.$logger.trace(`Error when starting emulator. More info: ${err}`);
});
}
private getBestFit(emulators: Mobile.IDeviceInfo[]) {
const best = _(emulators).maxBy(emulator => emulator.version);
return (best && best.version >= AndroidVirtualDevice.MIN_ANDROID_VERSION) ? best : null;
}
private async waitForEmulatorBootToComplete(emulator: Mobile.IDeviceInfo, endTimeEpoch: number, timeout: number): Promise<{runningEmulator: Mobile.IDeviceInfo, errors: string[]}> {
this.$logger.printInfoMessageOnSameLine("Waiting for emulator device initialization...");
const isInfiniteWait = this.$utils.getMilliSecondsTimeout(timeout || AndroidVirtualDevice.TIMEOUT_SECONDS) === 0;
while (getCurrentEpochTime() < endTimeEpoch || isInfiniteWait) {
const isEmulatorBootCompleted = await this.isEmulatorBootCompleted(emulator.identifier);
if (isEmulatorBootCompleted) {
this.$logger.printInfoMessageOnSameLine(EOL);
return {
runningEmulator: emulator,
errors: []
};
}
this.$logger.printInfoMessageOnSameLine(".");
await sleep(10000);
}
return {
runningEmulator: null,
errors: [AndroidVirtualDevice.UNABLE_TO_START_EMULATOR_MESSAGE]
};
}
private async isEmulatorBootCompleted(emulatorId: string): Promise<boolean> {
const output = await this.$adb.getPropertyValue(emulatorId, "dev.bootcomplete");
const matches = output.match("1");
return matches && matches.length > 0;
}
}
$injector.register("androidEmulatorServices", AndroidEmulatorServices);