device.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 exec = require('./exec'),
  19. Q = require('q'),
  20. os = require('os'),
  21. build = require('./build'),
  22. appinfo = require('./appinfo');
  23. /**
  24. * Returns a promise for the list of the device ID's found
  25. * @param lookHarder When true, try restarting adb if no devices are found.
  26. */
  27. module.exports.list = function(lookHarder) {
  28. function helper() {
  29. return exec('adb devices', os.tmpdir())
  30. .then(function(output) {
  31. var response = output.split('\n');
  32. var device_list = [];
  33. for (var i = 1; i < response.length; i++) {
  34. if (response[i].match(/\w+\tdevice/) && !response[i].match(/emulator/)) {
  35. device_list.push(response[i].replace(/\tdevice/, '').replace('\r', ''));
  36. }
  37. }
  38. return device_list;
  39. });
  40. }
  41. return helper()
  42. .then(function(list) {
  43. if (list.length === 0 && lookHarder) {
  44. // adb kill-server doesn't seem to do the trick.
  45. // Could probably find a x-platform version of killall, but I'm not actually
  46. // sure that this scenario even happens on non-OSX machines.
  47. return exec('killall adb')
  48. .then(function() {
  49. console.log('Restarting adb to see if more devices are detected.');
  50. return helper();
  51. }, function() {
  52. // For non-killall OS's.
  53. return list;
  54. });
  55. }
  56. return list;
  57. });
  58. };
  59. module.exports.resolveTarget = function(target) {
  60. return this.list(true)
  61. .then(function(device_list) {
  62. if (!device_list || !device_list.length) {
  63. return Q.reject('ERROR: Failed to deploy to device, no devices found.');
  64. }
  65. // default device
  66. target = target || device_list[0];
  67. if (device_list.indexOf(target) < 0) {
  68. return Q.reject('ERROR: Unable to find target \'' + target + '\'.');
  69. }
  70. return build.detectArchitecture(target)
  71. .then(function(arch) {
  72. return { target: target, arch: arch, isEmulator: false };
  73. });
  74. });
  75. };
  76. /*
  77. * Installs a previously built application on the device
  78. * and launches it.
  79. * Returns a promise.
  80. */
  81. module.exports.install = function(target, buildResults) {
  82. return Q().then(function() {
  83. if (target && typeof target == 'object') {
  84. return target;
  85. }
  86. return module.exports.resolveTarget(target);
  87. }).then(function(resolvedTarget) {
  88. var apk_path = build.findBestApkForArchitecture(buildResults, resolvedTarget.arch);
  89. var launchName = appinfo.getActivityName();
  90. console.log('Using apk: ' + apk_path);
  91. console.log('Installing app on device...');
  92. var cmd = 'adb -s ' + resolvedTarget.target + ' install -r "' + apk_path + '"';
  93. return exec(cmd, os.tmpdir())
  94. .then(function(output) {
  95. if (output.match(/Failure/)) return Q.reject('ERROR: Failed to install apk to device: ' + output);
  96. //unlock screen
  97. var cmd = 'adb -s ' + resolvedTarget.target + ' shell input keyevent 82';
  98. return exec(cmd, os.tmpdir());
  99. }, function(err) { return Q.reject('ERROR: Failed to install apk to device: ' + err); })
  100. .then(function() {
  101. // launch the application
  102. console.log('Launching application...');
  103. var cmd = 'adb -s ' + resolvedTarget.target + ' shell am start -W -a android.intent.action.MAIN -n ' + launchName;
  104. return exec(cmd, os.tmpdir());
  105. }).then(function() {
  106. console.log('LAUNCH SUCCESS');
  107. }, function(err) {
  108. return Q.reject('ERROR: Failed to launch application on device: ' + err);
  109. });
  110. });
  111. };