microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/androidDeviceTracker.ts
65lines · modeblame
4bb0956eRedMickey5 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
4cd25962JiglioNero4 years ago | 4 | import { AdbHelper } from "./adb"; |
4bb0956eRedMickey5 years ago | 5 | import { DeviceStorage } from "../networkInspector/devices/deviceStorage"; |
| 6 | import { AndroidClientDevice } from "../networkInspector/devices/androidClientDevice"; | |
| 7 | import { NetworkInspectorServer } from "../networkInspector/networkInspectorServer"; | |
| 8 | import { DeviceStatus } from "../networkInspector/devices/baseClientDevice"; | |
| 9 | import { ClientOS } from "../networkInspector/clientUtils"; | |
| 10 | import { AbstractDeviceTracker } from "../abstractDeviceTracker"; | |
| 11 | | |
| 12 | export class AndroidDeviceTracker extends AbstractDeviceTracker { | |
| 13 | private adbHelper: AdbHelper; | |
| 14 | | |
| 15 | constructor(adbHelper: AdbHelper) { | |
| 16 | super(); | |
| 17 | this.adbHelper = adbHelper; | |
| 18 | } | |
| 19 | | |
| 20 | public async start(): Promise<void> { | |
| 21 | this.logger.debug("Start Android device tracker"); | |
| 22 | await this.queryDevicesLoop(); | |
| 23 | } | |
| 24 | | |
| 25 | public stop(): void { | |
| 26 | this.logger.debug("Stop Android device tracker"); | |
| 27 | this.isStop = true; | |
| 28 | } | |
| 29 | | |
| 30 | protected async queryDevices(): Promise<void> { | |
4cd25962JiglioNero4 years ago | 31 | const onlineDevices = await this.adbHelper.getOnlineTargets(); |
4bb0956eRedMickey5 years ago | 32 | let currentDevicesIds = new Set( |
| 33 | [...DeviceStorage.devices.keys()].filter( | |
| 34 | key => DeviceStorage.devices.get(key) instanceof AndroidClientDevice, | |
| 35 | ), | |
| 36 | ); | |
| 37 | | |
| 38 | for (const onlineDevice of onlineDevices) { | |
| 39 | if (currentDevicesIds.has(onlineDevice.id)) { | |
| 40 | currentDevicesIds.delete(onlineDevice.id); | |
| 41 | } else { | |
| 42 | const androidDevice = new AndroidClientDevice( | |
| 43 | onlineDevice.id, | |
4cd25962JiglioNero4 years ago | 44 | onlineDevice.isVirtualTarget, |
4bb0956eRedMickey5 years ago | 45 | ClientOS.Android, |
| 46 | ); | |
| 47 | await this.initAndroidDevice(androidDevice); | |
| 48 | DeviceStorage.devices.set(androidDevice.id, androidDevice); | |
| 49 | } | |
| 50 | } | |
| 51 | | |
| 52 | currentDevicesIds.forEach(oldDeviceId => { | |
| 53 | DeviceStorage.devices.delete(oldDeviceId); | |
| 54 | }); | |
| 55 | } | |
| 56 | | |
| 57 | private async initAndroidDevice(androidDevice: AndroidClientDevice) { | |
| 58 | await this.adbHelper.reverseAdb( | |
| 59 | androidDevice.id, | |
| 60 | NetworkInspectorServer.InsecureServerPort, | |
| 61 | ); | |
| 62 | await this.adbHelper.reverseAdb(androidDevice.id, NetworkInspectorServer.SecureServerPort); | |
| 63 | androidDevice.deviceStatus = DeviceStatus.Prepared; | |
| 64 | } | |
| 65 | } |