arnaucube
arnaucube/commonroutesServerPublic Self-hosted repository
commonroutesServer/www/platforms/android/cordova/lib/device.js
www/platforms/android/cordova/lib/device.jsRaw
  1#!/usr/bin/env node
  2
  3/*
  4       Licensed to the Apache Software Foundation (ASF) under one
  5       or more contributor license agreements.  See the NOTICE file
  6       distributed with this work for additional information
  7       regarding copyright ownership.  The ASF licenses this file
  8       to you under the Apache License, Version 2.0 (the
  9       "License"); you may not use this file except in compliance
 10       with the License.  You may obtain a copy of the License at
 11
 12         http://www.apache.org/licenses/LICENSE-2.0
 13
 14       Unless required by applicable law or agreed to in writing,
 15       software distributed under the License is distributed on an
 16       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17       KIND, either express or implied.  See the License for the
 18       specific language governing permissions and limitations
 19       under the License.
 20*/
 21
 22var Q     = require('q'),
 23    build = require('./build');
 24var path = require('path');
 25var Adb = require('./Adb');
 26var AndroidManifest = require('./AndroidManifest');
 27var spawn = require('cordova-common').superspawn.spawn;
 28var CordovaError = require('cordova-common').CordovaError;
 29var events = require('cordova-common').events;
 30
 31/**
 32 * Returns a promise for the list of the device ID's found
 33 * @param lookHarder When true, try restarting adb if no devices are found.
 34 */
 35module.exports.list = function(lookHarder) {
 36    return Adb.devices()
 37    .then(function(list) {
 38        if (list.length === 0 && lookHarder) {
 39            // adb kill-server doesn't seem to do the trick.
 40            // Could probably find a x-platform version of killall, but I'm not actually
 41            // sure that this scenario even happens on non-OSX machines.
 42            return spawn('killall', ['adb'])
 43            .then(function() {
 44                events.emit('verbose', 'Restarting adb to see if more devices are detected.');
 45                return Adb.devices();
 46            }, function() {
 47                // For non-killall OS's.
 48                return list;
 49            });
 50        }
 51        return list;
 52    });
 53};
 54
 55module.exports.resolveTarget = function(target) {
 56    return this.list(true)
 57    .then(function(device_list) {
 58        if (!device_list || !device_list.length) {
 59            return Q.reject(new CordovaError('Failed to deploy to device, no devices found.'));
 60        }
 61        // default device
 62        target = target || device_list[0];
 63
 64        if (device_list.indexOf(target) < 0) {
 65            return Q.reject('ERROR: Unable to find target \'' + target + '\'.');
 66        }
 67
 68        return build.detectArchitecture(target)
 69        .then(function(arch) {
 70            return { target: target, arch: arch, isEmulator: false };
 71        });
 72    });
 73};
 74
 75/*
 76 * Installs a previously built application on the device
 77 * and launches it.
 78 * Returns a promise.
 79 */
 80module.exports.install = function(target, buildResults) {
 81    return Q().then(function() {
 82        if (target && typeof target == 'object') {
 83            return target;
 84        }
 85        return module.exports.resolveTarget(target);
 86    }).then(function(resolvedTarget) {
 87        var apk_path = build.findBestApkForArchitecture(buildResults, resolvedTarget.arch);
 88        var manifest = new AndroidManifest(path.join(__dirname, '../../AndroidManifest.xml'));
 89        var pkgName = manifest.getPackageId();
 90        var launchName = pkgName + '/.' + manifest.getActivity().getName();
 91        events.emit('log', 'Using apk: ' + apk_path);
 92        events.emit('log', 'Package name: ' + pkgName);
 93
 94        return Adb.install(resolvedTarget.target, apk_path, {replace: true})
 95        .catch(function (error) {
 96            // CB-9557 CB-10157 only uninstall and reinstall app if the one that
 97            // is already installed on device was signed w/different certificate
 98            if (!/INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES/.test(error.toString()))
 99                throw error;
100
101            events.emit('warn', 'Uninstalling app from device and reinstalling it again because the ' +
102                'installed app already signed with different key');
103
104            // This promise is always resolved, even if 'adb uninstall' fails to uninstall app
105            // or the app doesn't installed at all, so no error catching needed.
106            return Adb.uninstall(resolvedTarget.target, pkgName)
107            .then(function() {
108                return Adb.install(resolvedTarget.target, apk_path, {replace: true});
109            });
110        })
111        .then(function() {
112            //unlock screen
113            return Adb.shell(resolvedTarget.target, 'input keyevent 82');
114        }).then(function() {
115            return Adb.start(resolvedTarget.target, launchName);
116        }).then(function() {
117            events.emit('log', 'LAUNCH SUCCESS');
118        });
119    });
120};