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