microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/android/deviceHelper.ts
52lines · 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 class DeviceHelper { |
| 14 | |
| 15 | /** |
| 16 | * Gets the list of Android connected devices and emulators. |
| 17 | */ |
| 18 | public getConnectedDevices(): Q.Promise<IDevice[]> { |
| 19 | let childProcess = new ChildProcess(); |
| 20 | return childProcess.execToString("adb devices") |
| 21 | .then(output => { |
| 22 | return this.parseConnectedDevices(output); |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Broadcasts an intent to reload the application in debug mode. |
| 28 | */ |
| 29 | public reloadAppInDebugMode(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void> { |
| 30 | let enableDebugCommand = `adb ${debugTarget ? "-s " + debugTarget : ""} shell am broadcast -a "${packageName}.RELOAD_APP_ACTION" --ez jsproxy true`; |
| 31 | return new CommandExecutor(projectRoot).execute(enableDebugCommand); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Sends an intent which launches the main activity of the application. |
| 36 | */ |
| 37 | public launchApp(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void> { |
| 38 | let launchAppCommand = `adb -s ${debugTarget} shell am start -n ${packageName}/.MainActivity`; |
| 39 | return new CommandExecutor(projectRoot).execute(launchAppCommand); |
| 40 | } |
| 41 | |
| 42 | private parseConnectedDevices(input: string): IDevice[] { |
| 43 | let result: IDevice[] = []; |
| 44 | let regex = new RegExp("^(\\S+)\\t(\\S+)$", "mg"); |
| 45 | let match = regex.exec(input); |
| 46 | while (match != null) { |
| 47 | result.push({ id: match[1], isOnline: match[2] === "device" }); |
| 48 | match = regex.exec(input); |
| 49 | } |
| 50 | return result; |
| 51 | } |
| 52 | } |