Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

device.js 4.7KB

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