forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroid-device-discovery.ts
125 lines (111 loc) · 2.89 KB
/
android-device-discovery.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
import { DeviceDiscovery } from "./device-discovery";
import { AndroidDevice } from "../android/android-device";
import { IInjector } from "../../definitions/yok";
import { injector } from "../../yok";
import * as _ from "lodash";
interface IAdbAndroidDeviceInfo {
identifier: string;
status: string;
}
export class AndroidDeviceDiscovery
extends DeviceDiscovery
implements Mobile.IAndroidDeviceDiscovery
{
private _devices: IAdbAndroidDeviceInfo[] = [];
private isStarted: boolean;
constructor(
private $injector: IInjector,
private $adb: Mobile.IAndroidDebugBridge,
private $mobileHelper: Mobile.IMobileHelper
) {
super();
}
private async createAndAddDevice(
adbDeviceInfo: IAdbAndroidDeviceInfo
): Promise<void> {
this._devices.push(adbDeviceInfo);
const device: Mobile.IAndroidDevice = this.$injector.resolve(
AndroidDevice,
{ identifier: adbDeviceInfo.identifier, status: adbDeviceInfo.status }
);
await device.init();
this.addDevice(device);
}
private deleteAndRemoveDevice(deviceIdentifier: string): void {
_.remove(this._devices, (d) => d.identifier === deviceIdentifier);
this.removeDevice(deviceIdentifier);
}
public async startLookingForDevices(
options?: Mobile.IDeviceLookingOptions
): Promise<void> {
if (
options &&
options.platform &&
!this.$mobileHelper.isAndroidPlatform(options.platform)
) {
return;
}
await this.ensureAdbServerStarted();
await this.checkForDevices();
}
private async checkForDevices(): Promise<void> {
const devices = await this.$adb.getDevices();
await this.checkCurrentData(devices);
}
private async checkCurrentData(result: string[]): Promise<void> {
const currentDevices: IAdbAndroidDeviceInfo[] = result.map(
(element: string) => {
// http://developer.android.com/tools/help/adb.html#devicestatus
const data = element.split("\t");
const identifier = data[0];
const status = data[1];
return {
identifier: identifier,
status: status,
};
}
);
_(this._devices)
.reject(
(d) =>
<any>(
_.find(
currentDevices,
(device) =>
device.identifier === d.identifier && device.status === d.status
)
)
)
.each((d: IAdbAndroidDeviceInfo) =>
this.deleteAndRemoveDevice(d.identifier)
);
await Promise.all(
_(currentDevices)
.reject(
(d) =>
<any>(
_.find(
this._devices,
(device) =>
device.identifier === d.identifier &&
device.status === d.status
)
)
)
.map((d: IAdbAndroidDeviceInfo) => this.createAndAddDevice(d))
.value()
);
}
public async ensureAdbServerStarted(): Promise<any> {
if (!this.isStarted) {
this.isStarted = true;
try {
return await this.$adb.executeCommand(["start-server"]);
} catch (err) {
this.isStarted = false;
throw err;
}
}
}
}
injector.register("androidDeviceDiscovery", AndroidDeviceDiscovery);