microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/adb.ts
273lines · modeblame
52f3873ddigeff10 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 | | |
db6fd42aRuslan Bikkinin7 years ago | 4 | import { ChildProcess, ISpawnResult } from "../../common/node/childProcess"; |
| 5 | import { CommandExecutor } from "../../common/commandExecutor"; | |
| 6 | import * as path from "path"; | |
| 7 | import * as fs from "fs"; | |
| 8 | import { ILogger } from "../log/LogHelper"; | |
8b383051Ruslan Bikkinin7 years ago | 9 | import * as os from "os"; |
d7d405aeYuri Skorokhodov7 years ago | 10 | import * as nls from "vscode-nls"; |
34472878RedMickey5 years ago | 11 | nls.config({ |
| 12 | messageFormat: nls.MessageFormat.bundle, | |
| 13 | bundleFormat: nls.BundleFormat.standalone, | |
| 14 | })(); | |
d7d405aeYuri Skorokhodov7 years ago | 15 | const localize = nls.loadMessageBundle(); |
52f3873ddigeff10 years ago | 16 | |
ce7fd946digeff10 years ago | 17 | // See android versions usage at: http://developer.android.com/about/dashboards/index.html |
52f3873ddigeff10 years ago | 18 | export enum AndroidAPILevel { |
ce7fd946digeff10 years ago | 19 | Marshmallow = 23, |
52f3873ddigeff10 years ago | 20 | LOLLIPOP_MR1 = 22, |
34472878RedMickey5 years ago | 21 | LOLLIPOP = 21 /* Supports adb reverse */, |
52f3873ddigeff10 years ago | 22 | KITKAT = 19, |
| 23 | JELLY_BEAN_MR2 = 18, | |
| 24 | JELLY_BEAN_MR1 = 17, | |
| 25 | JELLY_BEAN = 16, | |
| 26 | ICE_CREAM_SANDWICH_MR1 = 15, | |
| 27 | GINGERBREAD_MR1 = 10, | |
| 28 | } | |
| 29 | | |
7daed3fcArtem Egorov8 years ago | 30 | enum KeyEvents { |
| 31 | KEYCODE_BACK = 4, | |
| 32 | KEYCODE_DPAD_UP = 19, | |
| 33 | KEYCODE_DPAD_DOWN = 20, | |
| 34 | KEYCODE_DPAD_CENTER = 23, | |
| 35 | KEYCODE_MENU = 82, | |
| 36 | } | |
| 37 | | |
52f3873ddigeff10 years ago | 38 | export enum DeviceType { |
| 39 | AndroidSdkEmulator, // These seem to have emulator-<port> ids | |
27710197Vladimir Kotikov8 years ago | 40 | Other, |
52f3873ddigeff10 years ago | 41 | } |
| 42 | | |
| 43 | export interface IDevice { | |
| 44 | id: string; | |
| 45 | isOnline: boolean; | |
| 46 | type: DeviceType; | |
| 47 | } | |
| 48 | | |
7daed3fcArtem Egorov8 years ago | 49 | const AndroidSDKEmulatorPattern = /^emulator-\d{1,5}$/; |
52f3873ddigeff10 years ago | 50 | |
7daed3fcArtem Egorov8 years ago | 51 | export class AdbHelper { |
db6fd42aRuslan Bikkinin7 years ago | 52 | private childProcess: ChildProcess = new ChildProcess(); |
| 53 | private commandExecutor: CommandExecutor = new CommandExecutor(); | |
| 54 | private adbExecutable: string = ""; | |
78c2b4deRedMickey6 years ago | 55 | private launchActivity: string; |
db6fd42aRuslan Bikkinin7 years ago | 56 | |
78c2b4deRedMickey6 years ago | 57 | constructor(projectRoot: string, logger?: ILogger, launchActivity: string = "MainActivity") { |
8152571aYuri Skorokhodov6 years ago | 58 | this.adbExecutable = this.getAdbPath(projectRoot, logger); |
78c2b4deRedMickey6 years ago | 59 | this.launchActivity = launchActivity; |
db6fd42aRuslan Bikkinin7 years ago | 60 | } |
52f3873ddigeff10 years ago | 61 | |
| 62 | /** | |
| 63 | * Gets the list of Android connected devices and emulators. | |
| 64 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 65 | public getConnectedDevices(): Promise<IDevice[]> { |
34472878RedMickey5 years ago | 66 | return this.childProcess.execToString(`${this.adbExecutable} devices`).then(output => { |
| 67 | return this.parseConnectedDevices(output); | |
| 68 | }); | |
52f3873ddigeff10 years ago | 69 | } |
| 70 | | |
78c2b4deRedMickey6 years ago | 71 | public setLaunchActivity(launchActivity: string): void { |
| 72 | this.launchActivity = launchActivity; | |
| 73 | } | |
| 74 | | |
52f3873ddigeff10 years ago | 75 | /** |
| 76 | * Broadcasts an intent to reload the application in debug mode. | |
| 77 | */ | |
34472878RedMickey5 years ago | 78 | public switchDebugMode( |
| 79 | projectRoot: string, | |
| 80 | packageName: string, | |
| 81 | enable: boolean, | |
| 82 | debugTarget?: string, | |
| 83 | ): Promise<void> { | |
| 84 | let enableDebugCommand = `${this.adbExecutable} ${ | |
| 85 | debugTarget ? "-s " + debugTarget : "" | |
| 86 | } shell am broadcast -a "${packageName}.RELOAD_APP_ACTION" --ez jsproxy ${enable}`; | |
| 87 | return new CommandExecutor(projectRoot) | |
| 88 | .execute(enableDebugCommand) | |
| 89 | .then(() => { | |
| 90 | // We should stop and start application again after RELOAD_APP_ACTION, otherwise app going to hangs up | |
| 91 | return new Promise(resolve => { | |
ce5e88eeYuri Skorokhodov5 years ago | 92 | setTimeout(() => { |
34472878RedMickey5 years ago | 93 | this.stopApp(projectRoot, packageName, debugTarget).then(() => { |
| 94 | return resolve(); | |
| 95 | }); | |
ce5e88eeYuri Skorokhodov5 years ago | 96 | }, 200); // We need a little delay after broadcast command |
| 97 | }); | |
b57ea017Artem Egorov8 years ago | 98 | }) |
| 99 | .then(() => { | |
| 100 | return this.launchApp(projectRoot, packageName, debugTarget); | |
| 101 | }); | |
52f3873ddigeff10 years ago | 102 | } |
| 103 | | |
| 104 | /** | |
| 105 | * Sends an intent which launches the main activity of the application. | |
| 106 | */ | |
34472878RedMickey5 years ago | 107 | public launchApp( |
| 108 | projectRoot: string, | |
| 109 | packageName: string, | |
| 110 | debugTarget?: string, | |
| 111 | ): Promise<void> { | |
| 112 | let launchAppCommand = `${this.adbExecutable} ${ | |
| 113 | debugTarget ? "-s " + debugTarget : "" | |
| 114 | } shell am start -n ${packageName}/.${this.launchActivity}`; | |
52f3873ddigeff10 years ago | 115 | return new CommandExecutor(projectRoot).execute(launchAppCommand); |
| 116 | } | |
| 117 | | |
ce5e88eeYuri Skorokhodov5 years ago | 118 | public stopApp(projectRoot: string, packageName: string, debugTarget?: string): Promise<void> { |
34472878RedMickey5 years ago | 119 | let stopAppCommand = `${this.adbExecutable} ${ |
| 120 | debugTarget ? "-s " + debugTarget : "" | |
| 121 | } shell am force-stop ${packageName}`; | |
b57ea017Artem Egorov8 years ago | 122 | return new CommandExecutor(projectRoot).execute(stopAppCommand); |
| 123 | } | |
| 124 | | |
ce5e88eeYuri Skorokhodov5 years ago | 125 | public apiVersion(deviceId: string): Promise<AndroidAPILevel> { |
52f3873ddigeff10 years ago | 126 | return this.executeQuery(deviceId, "shell getprop ro.build.version.sdk").then(output => |
34472878RedMickey5 years ago | 127 | parseInt(output, 10), |
| 128 | ); | |
52f3873ddigeff10 years ago | 129 | } |
| 130 | | |
ce5e88eeYuri Skorokhodov5 years ago | 131 | public reverseAdb(deviceId: string, packagerPort: number): Promise<void> { |
b57ea017Artem Egorov8 years ago | 132 | return this.execute(deviceId, `reverse tcp:${packagerPort} tcp:${packagerPort}`); |
52f3873ddigeff10 years ago | 133 | } |
| 134 | | |
ce5e88eeYuri Skorokhodov5 years ago | 135 | public showDevMenu(deviceId?: string): Promise<void> { |
34472878RedMickey5 years ago | 136 | let command = `${this.adbExecutable} ${ |
| 137 | deviceId ? "-s " + deviceId : "" | |
| 138 | } shell input keyevent ${KeyEvents.KEYCODE_MENU}`; | |
7daed3fcArtem Egorov8 years ago | 139 | return this.commandExecutor.execute(command); |
| 140 | } | |
| 141 | | |
ce5e88eeYuri Skorokhodov5 years ago | 142 | public reloadApp(deviceId?: string): Promise<void> { |
34472878RedMickey5 years ago | 143 | let command = `${this.adbExecutable} ${ |
| 144 | deviceId ? "-s " + deviceId : "" | |
| 145 | } shell input text "RR"`; | |
e23ea82cAlter Code7 years ago | 146 | return this.commandExecutor.execute(command); |
7daed3fcArtem Egorov8 years ago | 147 | } |
| 148 | | |
ce5e88eeYuri Skorokhodov5 years ago | 149 | public getOnlineDevices(): Promise<IDevice[]> { |
7daed3fcArtem Egorov8 years ago | 150 | return this.getConnectedDevices().then(devices => { |
34472878RedMickey5 years ago | 151 | return devices.filter(device => device.isOnline); |
7daed3fcArtem Egorov8 years ago | 152 | }); |
| 153 | } | |
| 154 | | |
db6fd42aRuslan Bikkinin7 years ago | 155 | public startLogCat(adbParameters: string[]): ISpawnResult { |
b78a1aaeYuri Skorokhodov5 years ago | 156 | return this.childProcess.spawn(this.adbExecutable.replace(/\"/g, ""), adbParameters); |
db6fd42aRuslan Bikkinin7 years ago | 157 | } |
| 158 | | |
8df5011eYuri Skorokhodov5 years ago | 159 | public parseSdkLocation(fileContent: string, logger?: ILogger): string | null { |
8b383051Ruslan Bikkinin7 years ago | 160 | const matches = fileContent.match(/^sdk\.dir=(.+)$/m); |
| 161 | if (!matches || !matches[1]) { | |
| 162 | if (logger) { | |
34472878RedMickey5 years ago | 163 | logger.info( |
| 164 | localize( | |
| 165 | "NoSdkDirFoundInLocalPropertiesFile", | |
| 166 | "No sdk.dir value found in local.properties file. Using Android SDK location from PATH.", | |
| 167 | ), | |
| 168 | ); | |
8b383051Ruslan Bikkinin7 years ago | 169 | } |
| 170 | return null; | |
| 171 | } | |
| 172 | | |
| 173 | let sdkLocation = matches[1].trim(); | |
| 174 | if (os.platform() === "win32") { | |
| 175 | // For Windows we need to unescape files separators and drive letter separators | |
| 176 | sdkLocation = sdkLocation.replace(/\\\\/g, "\\").replace("\\:", ":"); | |
| 177 | } | |
| 178 | if (logger) { | |
34472878RedMickey5 years ago | 179 | logger.info( |
| 180 | localize( | |
| 181 | "UsindAndroidSDKLocationDefinedInLocalPropertiesFile", | |
| 182 | "Using Android SDK location defined in android/local.properties file: {0}.", | |
| 183 | sdkLocation, | |
| 184 | ), | |
| 185 | ); | |
8b383051Ruslan Bikkinin7 years ago | 186 | } |
| 187 | | |
| 188 | return sdkLocation; | |
| 189 | } | |
| 190 | | |
8152571aYuri Skorokhodov6 years ago | 191 | public getAdbPath(projectRoot: string, logger?: ILogger): string { |
| 192 | // Trying to read sdk location from local.properties file and if we succueded then | |
| 193 | // we would run adb from inside it, otherwise we would rely to PATH | |
| 194 | const sdkLocation = this.getSdkLocationFromLocalPropertiesFile(projectRoot, logger); | |
| 195 | return sdkLocation ? `"${path.join(sdkLocation, "platform-tools", "adb")}"` : "adb"; | |
| 196 | } | |
| 197 | | |
db6fd42aRuslan Bikkinin7 years ago | 198 | private parseConnectedDevices(input: string): IDevice[] { |
52f3873ddigeff10 years ago | 199 | let result: IDevice[] = []; |
| 200 | let regex = new RegExp("^(\\S+)\\t(\\S+)$", "mg"); | |
| 201 | let match = regex.exec(input); | |
| 202 | while (match != null) { | |
34472878RedMickey5 years ago | 203 | result.push({ |
| 204 | id: match[1], | |
| 205 | isOnline: match[2] === "device", | |
| 206 | type: this.extractDeviceType(match[1]), | |
| 207 | }); | |
52f3873ddigeff10 years ago | 208 | match = regex.exec(input); |
| 209 | } | |
| 210 | return result; | |
| 211 | } | |
| 212 | | |
db6fd42aRuslan Bikkinin7 years ago | 213 | private extractDeviceType(id: string): DeviceType { |
52f3873ddigeff10 years ago | 214 | return id.match(AndroidSDKEmulatorPattern) |
| 215 | ? DeviceType.AndroidSdkEmulator | |
| 216 | : DeviceType.Other; | |
| 217 | } | |
| 218 | | |
ce5e88eeYuri Skorokhodov5 years ago | 219 | private executeQuery(deviceId: string, command: string): Promise<string> { |
52f3873ddigeff10 years ago | 220 | return this.childProcess.execToString(this.generateCommandForDevice(deviceId, command)); |
| 221 | } | |
| 222 | | |
ce5e88eeYuri Skorokhodov5 years ago | 223 | private execute(deviceId: string, command: string): Promise<void> { |
52f3873ddigeff10 years ago | 224 | return this.commandExecutor.execute(this.generateCommandForDevice(deviceId, command)); |
| 225 | } | |
| 226 | | |
db6fd42aRuslan Bikkinin7 years ago | 227 | private generateCommandForDevice(deviceId: string, adbCommand: string): string { |
| 228 | return `${this.adbExecutable} -s "${deviceId}" ${adbCommand}`; | |
| 229 | } | |
| 230 | | |
34472878RedMickey5 years ago | 231 | private getSdkLocationFromLocalPropertiesFile( |
| 232 | projectRoot: string, | |
| 233 | logger?: ILogger, | |
| 234 | ): string | null { | |
db6fd42aRuslan Bikkinin7 years ago | 235 | const localPropertiesFilePath = path.join(projectRoot, "android", "local.properties"); |
| 236 | if (!fs.existsSync(localPropertiesFilePath)) { | |
| 237 | if (logger) { | |
34472878RedMickey5 years ago | 238 | logger.info( |
| 239 | localize( | |
| 240 | "LocalPropertiesFileDoesNotExist", | |
| 241 | "local.properties file doesn't exist. Using Android SDK location from PATH.", | |
| 242 | ), | |
| 243 | ); | |
db6fd42aRuslan Bikkinin7 years ago | 244 | } |
| 245 | return null; | |
| 246 | } | |
| 247 | | |
8b383051Ruslan Bikkinin7 years ago | 248 | let fileContent: string; |
db6fd42aRuslan Bikkinin7 years ago | 249 | try { |
| 250 | fileContent = fs.readFileSync(localPropertiesFilePath).toString(); | |
| 251 | } catch (e) { | |
| 252 | if (logger) { | |
34472878RedMickey5 years ago | 253 | logger.error( |
| 254 | localize( | |
| 255 | "CouldNotReadFrom", | |
| 256 | "Couldn't read from {0}.", | |
| 257 | localPropertiesFilePath, | |
| 258 | ), | |
| 259 | e, | |
| 260 | e.stack, | |
| 261 | ); | |
| 262 | logger.info( | |
| 263 | localize( | |
| 264 | "UsingAndroidSDKLocationFromPATH", | |
| 265 | "Using Android SDK location from PATH.", | |
| 266 | ), | |
| 267 | ); | |
db6fd42aRuslan Bikkinin7 years ago | 268 | } |
| 269 | return null; | |
| 270 | } | |
8b383051Ruslan Bikkinin7 years ago | 271 | return this.parseSdkLocation(fileContent, logger); |
52f3873ddigeff10 years ago | 272 | } |
| 273 | } |