commonroutesServer/www/platforms/android/cordova/lib/Adb.js
www/platforms/android/cordova/lib/Adb.jsRaw
1/**
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*/
19
20var Q = require('q');
21var os = require('os');
22var events = require('cordova-common').events;
23var spawn = require('cordova-common').superspawn.spawn;
24var CordovaError = require('cordova-common').CordovaError;
25
26var Adb = {};
27
28function isDevice(line) {
29 return line.match(/\w+\tdevice/) && !line.match(/emulator/);
30}
31
32function isEmulator(line) {
33 return line.match(/device/) && line.match(/emulator/);
34}
35
36/**
37 * Lists available/connected devices and emulators
38 *
39 * @param {Object} opts Various options
40 * @param {Boolean} opts.emulators Specifies whether this method returns
41 * emulators only
42 *
43 * @return {Promise<String[]>} list of available/connected
44 * devices/emulators
45 */
46Adb.devices = function (opts) {
47 return spawn('adb', ['devices'], {cwd: os.tmpdir()})
48 .then(function(output) {
49 return output.split('\n').filter(function (line) {
50 // Filter out either real devices or emulators, depending on options
51 return (line && opts && opts.emulators) ? isEmulator(line) : isDevice(line);
52 }).map(function (line) {
53 return line.replace(/\tdevice/, '').replace('\r', '');
54 });
55 });
56};
57
58Adb.install = function (target, packagePath, opts) {
59 events.emit('verbose', 'Installing apk ' + packagePath + ' on target ' + target + '...');
60 var args = ['-s', target, 'install'];
61 if (opts && opts.replace) args.push('-r');
62 return spawn('adb', args.concat(packagePath), {cwd: os.tmpdir()})
63 .then(function(output) {
64 // 'adb install' seems to always returns no error, even if installation fails
65 // so we catching output to detect installation failure
66 if (output.match(/Failure/)) {
67 if (output.match(/INSTALL_PARSE_FAILED_NO_CERTIFICATES/)) {
68 output += '\n\n' + 'Sign the build using \'-- --keystore\' or \'--buildConfig\'' +
69 ' or sign and deploy the unsigned apk manually using Android tools.';
70 } else if (output.match(/INSTALL_FAILED_VERSION_DOWNGRADE/)) {
71 output += '\n\n' + 'You\'re trying to install apk with a lower versionCode that is already installed.' +
72 '\nEither uninstall an app or increment the versionCode.';
73 }
74
75 return Q.reject(new CordovaError('Failed to install apk to device: ' + output));
76 }
77 });
78};
79
80Adb.uninstall = function (target, packageId) {
81 events.emit('verbose', 'Uninstalling package ' + packageId + ' from target ' + target + '...');
82 return spawn('adb', ['-s', target, 'uninstall', packageId], {cwd: os.tmpdir()});
83};
84
85Adb.shell = function (target, shellCommand) {
86 events.emit('verbose', 'Running adb shell command "' + shellCommand + '" on target ' + target + '...');
87 var args = ['-s', target, 'shell'];
88 shellCommand = shellCommand.split(/\s+/);
89 return spawn('adb', args.concat(shellCommand), {cwd: os.tmpdir()})
90 .catch(function (output) {
91 return Q.reject(new CordovaError('Failed to execute shell command "' +
92 shellCommand + '"" on device: ' + output));
93 });
94};
95
96Adb.start = function (target, activityName) {
97 events.emit('verbose', 'Starting application "' + activityName + '" on target ' + target + '...');
98 return Adb.shell(target, 'am start -W -a android.intent.action.MAIN -n' + activityName)
99 .catch(function (output) {
100 return Q.reject(new CordovaError('Failed to start application "' +
101 activityName + '"" on device: ' + output));
102 });
103};
104
105module.exports = Adb;