microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/android/adb.ts

133lines · modeblame

52f3873ddigeff10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as Q from "q";
5
6import {ChildProcess} from "../../common/node/childProcess";
7import {CommandExecutor} from "../../common/commandExecutor";
8
ce7fd946digeff10 years ago9// See android versions usage at: http://developer.android.com/about/dashboards/index.html
52f3873ddigeff10 years ago10export enum AndroidAPILevel {
ce7fd946digeff10 years ago11Marshmallow = 23,
52f3873ddigeff10 years ago12LOLLIPOP_MR1 = 22,
13LOLLIPOP = 21, /* Supports adb reverse */
14KITKAT = 19,
15JELLY_BEAN_MR2 = 18,
16JELLY_BEAN_MR1 = 17,
17JELLY_BEAN = 16,
18ICE_CREAM_SANDWICH_MR1 = 15,
19GINGERBREAD_MR1 = 10,
20}
21
22export enum DeviceType {
23AndroidSdkEmulator, // These seem to have emulator-<port> ids
27710197Vladimir Kotikov8 years ago24Other,
52f3873ddigeff10 years ago25}
26
27const AndroidSDKEmulatorPattern = /^emulator-\d{1,5}$/;
28
29export interface IDevice {
30id: string;
31isOnline: boolean;
32type: DeviceType;
33}
34
35export interface IAdb {
36getConnectedDevices(): Q.Promise<IDevice[]>;
37launchApp(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void>;
38getOnlineDevices(): Q.Promise<IDevice[]>;
39reloadAppInDebugMode(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void>;
40apiVersion(deviceId: string): Q.Promise<AndroidAPILevel>;
41reverseAdd(deviceId: string, devicePort: string, computerPort: string): Q.Promise<void>;
42}
43
44export abstract class AdbEnhancements implements IAdb {
45public abstract getConnectedDevices(): Q.Promise<IDevice[]>;
46public abstract launchApp(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void>;
47public abstract reloadAppInDebugMode(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void>;
48public abstract apiVersion(deviceId: string): Q.Promise<AndroidAPILevel>;
49public abstract reverseAdd(deviceId: string, devicePort: string, computerPort: string): Q.Promise<void>;
50
51public getOnlineDevices(): Q.Promise<IDevice[]> {
52return this.getConnectedDevices().then(devices => {
53return devices.filter(device =>
54device.isOnline);
55});
56}
57}
58
59export class Adb extends AdbEnhancements {
60private childProcess: ChildProcess;
61private commandExecutor: CommandExecutor;
62
63constructor({childProcess = new ChildProcess(), commandExecutor = new CommandExecutor()} = {}) {
64super();
65this.childProcess = childProcess;
66this.commandExecutor = commandExecutor;
67}
68
69/**
70* Gets the list of Android connected devices and emulators.
71*/
72public getConnectedDevices(): Q.Promise<IDevice[]> {
73let childProcess = new ChildProcess();
74return childProcess.execToString("adb devices")
75.then(output => {
76return this.parseConnectedDevices(output);
77});
78}
79
80/**
81* Broadcasts an intent to reload the application in debug mode.
82*/
83public reloadAppInDebugMode(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void> {
84let enableDebugCommand = `adb ${debugTarget ? "-s " + debugTarget : ""} shell am broadcast -a "${packageName}.RELOAD_APP_ACTION" --ez jsproxy true`;
85return new CommandExecutor(projectRoot).execute(enableDebugCommand);
86}
87
88/**
89* Sends an intent which launches the main activity of the application.
90*/
91public launchApp(projectRoot: string, packageName: string, debugTarget?: string): Q.Promise<void> {
92let launchAppCommand = `adb -s ${debugTarget} shell am start -n ${packageName}/.MainActivity`;
93return new CommandExecutor(projectRoot).execute(launchAppCommand);
94}
95
96public apiVersion(deviceId: string): Q.Promise<AndroidAPILevel> {
97return this.executeQuery(deviceId, "shell getprop ro.build.version.sdk").then(output =>
98parseInt(output, 10));
99}
100
101public reverseAdd(deviceId: string, devicePort: string, computerPort: string): Q.Promise<void> {
102return this.execute(deviceId, `reverse tcp:${devicePort} tcp:${computerPort}`);
103}
104
105private parseConnectedDevices(input: string): IDevice[] {
106let result: IDevice[] = [];
107let regex = new RegExp("^(\\S+)\\t(\\S+)$", "mg");
108let match = regex.exec(input);
109while (match != null) {
110result.push({ id: match[1], isOnline: match[2] === "device", type: this.extractDeviceType(match[1]) });
111match = regex.exec(input);
112}
113return result;
114}
115
116private extractDeviceType(id: string): DeviceType {
117return id.match(AndroidSDKEmulatorPattern)
118? DeviceType.AndroidSdkEmulator
119: DeviceType.Other;
120}
121
122private executeQuery(deviceId: string, command: string): Q.Promise<string> {
123return this.childProcess.execToString(this.generateCommandForDevice(deviceId, command));
124}
125
126private execute(deviceId: string, command: string): Q.Promise<void> {
127return this.commandExecutor.execute(this.generateCommandForDevice(deviceId, command));
128}
129
130private generateCommandForDevice(deviceId: string, adbCommand: string): string {
131return `adb -s "${deviceId}" ${adbCommand}`;
132}
133}