microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/android/deviceHelper.ts
58lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import {ChildProcess} from "../node/childProcess"; |
| 5 | import {CommandExecutor} from "../commandExecutor"; |
| 6 | import * as Q from "q"; |
| 7 | |
| 8 | export interface IDevice { |
| 9 | id: string; |
| 10 | isOnline: boolean; |
| 11 | } |
| 12 | |
| 13 | export interface IDeviceHelper { |
| 14 | getConnectedDevices(): Q.Promise<IDevice[]>; |
| 15 | reloadAppInDebugMode(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void>; |
| 16 | launchApp(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void>; |
| 17 | } |
| 18 | |
| 19 | export class DeviceHelper implements IDeviceHelper { |
| 20 | |
| 21 | /** |
| 22 | * Gets the list of Android connected devices and emulators. |
| 23 | */ |
| 24 | public getConnectedDevices(): Q.Promise<IDevice[]> { |
| 25 | let childProcess = new ChildProcess(); |
| 26 | return childProcess.execToString("adb devices") |
| 27 | .then(output => { |
| 28 | return this.parseConnectedDevices(output); |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Broadcasts an intent to reload the application in debug mode. |
| 34 | */ |
| 35 | public reloadAppInDebugMode(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void> { |
| 36 | let enableDebugCommand = `adb ${debugTarget ? "-s " + debugTarget : ""} shell am broadcast -a "${packageName}.RELOAD_APP_ACTION" --ez jsproxy true`; |
| 37 | return new CommandExecutor(projectRoot).execute(enableDebugCommand); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Sends an intent which launches the main activity of the application. |
| 42 | */ |
| 43 | public launchApp(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void> { |
| 44 | let launchAppCommand = `adb -s ${debugTarget} shell am start -n ${packageName}/.MainActivity`; |
| 45 | return new CommandExecutor(projectRoot).execute(launchAppCommand); |
| 46 | } |
| 47 | |
| 48 | private parseConnectedDevices(input: string): IDevice[] { |
| 49 | let result: IDevice[] = []; |
| 50 | let regex = new RegExp("^(\\S+)\\t(\\S+)$", "mg"); |
| 51 | let match = regex.exec(input); |
| 52 | while (match != null) { |
| 53 | result.push({ id: match[1], isOnline: match[2] === "device" }); |
| 54 | match = regex.exec(input); |
| 55 | } |
| 56 | return result; |
| 57 | } |
| 58 | } |