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