1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | 'use strict';
|
18 |
|
19 | const appc = require('node-appc');
|
20 | const __ = appc.i18n(__dirname).__;
|
21 | const async = require('async');
|
22 | const fs = require('fs-extra');
|
23 | const net = require('net');
|
24 | const path = require('path');
|
25 | const spawn = require('child_process').spawn;
|
26 | const StreamSplitter = require('stream-splitter');
|
27 |
|
28 | require('colors');
|
29 |
|
30 | let connCounter = 0;
|
31 |
|
32 | module.exports = ADB;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var DEBUG = false;
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const DO_NOTHING = 0;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | const WAIT_FOR_COMMAND_RESULT = 1;
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | const WAIT_FOR_NEW_DATA = 2;
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | const BUFFER_UNTIL_CLOSE = 3;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | const WAIT_FOR_RESPONSE = 4;
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function Connection(adb) {
|
94 | this.adb = adb;
|
95 | this.port = adb.config && adb.config.get('android.adb.port') || 5037;
|
96 | this.socket = null;
|
97 | this.state = DO_NOTHING;
|
98 | this.connNum = ++connCounter;
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | Connection.prototype.exec = function exec(cmd, callback, opts) {
|
110 | var conn = this,
|
111 | socket = this.socket,
|
112 | doSend = !!socket,
|
113 | buffer = null,
|
114 | len = null;
|
115 | function send () {
|
116 | DEBUG && console.log('[' + conn.connNum + '] SENDING ' + cmd);
|
117 | conn.state = WAIT_FOR_COMMAND_RESULT;
|
118 | buffer = null;
|
119 | socket.write(('0000' + cmd.length.toString(16)).substr(-4).toUpperCase() + cmd);
|
120 | }
|
121 |
|
122 | this.opts = opts || {};
|
123 |
|
124 | if (!socket) {
|
125 | socket = this.socket = net.connect({
|
126 | port: this.port
|
127 | }, function () {
|
128 | DEBUG && console.log('[' + this.connNum + '] CONNECTED');
|
129 |
|
130 |
|
131 |
|
132 | setTimeout(function () {
|
133 | send();
|
134 | }, 200);
|
135 | }.bind(this));
|
136 |
|
137 | socket.setKeepAlive(true);
|
138 | socket.setNoDelay(true);
|
139 | } else {
|
140 | DEBUG && console.log('[' + this.connNum + '] SOCKET ALREADY OPEN, RE-LISTENING AND SENDING NEW COMMAND "' + cmd + '"');
|
141 | socket.removeAllListeners('data');
|
142 | socket.removeAllListeners('end');
|
143 | socket.removeAllListeners('error');
|
144 | }
|
145 |
|
146 | socket.on('data', function (data) {
|
147 | DEBUG && console.log('[' + this.connNum + '] RECEIVED ' + data.length + ' BYTES (state=' + this.state + ') (cmd=' + cmd + ')');
|
148 |
|
149 | if (this.state === DO_NOTHING) {
|
150 | return;
|
151 | }
|
152 |
|
153 | if (!buffer || buffer.length === 0) {
|
154 | buffer = data;
|
155 | } else {
|
156 | buffer += data;
|
157 | }
|
158 |
|
159 | DEBUG && console.log('[' + this.connNum + '] BUFFER LENGTH = ' + buffer.length);
|
160 |
|
161 | while (1) {
|
162 | switch (this.state) {
|
163 | case WAIT_FOR_COMMAND_RESULT:
|
164 | const result = buffer.slice(0, 4).toString();
|
165 | DEBUG && console.log('[' + this.connNum + '] RESULT ' + result);
|
166 | if (!/^OKAY|FAIL$/.test(result)) {
|
167 | callback(new Error(__('Unknown adb result "%s"', result)));
|
168 | return;
|
169 | }
|
170 | buffer = buffer.slice(4);
|
171 |
|
172 |
|
173 | if (result === 'FAIL') {
|
174 | len = 0;
|
175 | if (buffer.length >= 4) {
|
176 | len = parseInt(buffer.slice(0, 4), 16);
|
177 | isNaN(len) && (len = 0);
|
178 | buffer = buffer.slice(4);
|
179 | }
|
180 | len && (buffer = buffer.slice(0, len));
|
181 | DEBUG && console.log('[' + this.connNum + '] ERROR! ' + buffer.toString());
|
182 | this.state = DO_NOTHING;
|
183 |
|
184 |
|
185 | var err = new Error(buffer.toString());
|
186 | buffer = null;
|
187 | callback(err);
|
188 | conn.end();
|
189 | return;
|
190 | }
|
191 |
|
192 |
|
193 | if (buffer.length === 0) {
|
194 | if (this.opts.bufferUntilClose) {
|
195 | DEBUG && console.log('[' + this.connNum + '] DONE, SETTING STATE TO BUFFER_UNTIL_CLOSE');
|
196 | this.state = BUFFER_UNTIL_CLOSE;
|
197 | } else if (this.opts.waitForResponse) {
|
198 | DEBUG && console.log('[' + this.connNum + '] DONE, SETTING STATE TO WAIT_FOR_NEW_DATA');
|
199 | this.state = WAIT_FOR_NEW_DATA;
|
200 | } else {
|
201 | DEBUG && console.log('[' + this.connNum + '] DONE, SETTING STATE TO DO_NOTHING');
|
202 | this.state = DO_NOTHING;
|
203 | callback();
|
204 | }
|
205 | return;
|
206 | }
|
207 |
|
208 |
|
209 |
|
210 | if (this.opts.noLength) {
|
211 | DEBUG && console.log('[' + this.connNum + '] PUSHING REMAINING DATA INTO BUFFER AND SETTING STATE TO BUFFER_UNTIL_CLOSE');
|
212 | this.state = BUFFER_UNTIL_CLOSE;
|
213 | return;
|
214 | }
|
215 |
|
216 | this.state = WAIT_FOR_NEW_DATA;
|
217 | len = null;
|
218 |
|
219 |
|
220 | case WAIT_FOR_NEW_DATA:
|
221 |
|
222 | if (len === null && buffer.length >= 4) {
|
223 | len = parseInt(buffer.slice(0, 4), 16);
|
224 | DEBUG && console.log('[' + this.connNum + '] DETERMINING EXPECTED LENGTH...');
|
225 | isNaN(len) && (len = null);
|
226 | buffer = buffer.slice(4);
|
227 | }
|
228 |
|
229 |
|
230 | if (len === 0) {
|
231 | DEBUG && console.log('[' + this.connNum + '] NO EXPECTED LENGTH, FIRING CALLBACK');
|
232 | callback();
|
233 | buffer = null;
|
234 | len = null;
|
235 | return;
|
236 | } else if (len === null) {
|
237 | DEBUG && console.log('[' + this.connNum + '] NO EXPECTED LENGTH');
|
238 | if (this.opts.bufferUntilClose) {
|
239 | DEBUG && console.log('[' + this.connNum + '] BUFFERING DATA UNTIL SOCKET CLOSE');
|
240 | this.state = BUFFER_UNTIL_CLOSE;
|
241 | } else {
|
242 | buffer = null;
|
243 | len = null;
|
244 | this.state = WAIT_FOR_NEW_DATA;
|
245 | callback();
|
246 | }
|
247 | return;
|
248 | }
|
249 |
|
250 | DEBUG && console.log('[' + this.connNum + '] EXPECTED LENGTH = ' + len);
|
251 | DEBUG && console.log('[' + this.connNum + '] BUFFER LENGTH = ' + buffer.length);
|
252 |
|
253 |
|
254 | if (buffer.length >= len) {
|
255 |
|
256 | const result = buffer.slice(0, len);
|
257 | buffer = buffer.slice(len);
|
258 | DEBUG && console.log('[' + this.connNum + '] SUCCESS AND JUST THE RIGHT AMOUNT OF BYTES (' + len + ') WITH ' + buffer.length + ' BYTES LEFT');
|
259 | if (this.opts.bufferUntilClose) {
|
260 | this.state = BUFFER_UNTIL_CLOSE;
|
261 | } else {
|
262 | this.state = WAIT_FOR_NEW_DATA;
|
263 | len = null;
|
264 | buffer = null;
|
265 | callback(null, result);
|
266 | }
|
267 | } else {
|
268 |
|
269 | DEBUG && console.log('[' + this.connNum + '] WAITING FOR MORE DATA');
|
270 | }
|
271 | return;
|
272 |
|
273 | case BUFFER_UNTIL_CLOSE:
|
274 |
|
275 | return;
|
276 | case WAIT_FOR_RESPONSE:
|
277 | DEBUG && console.log('[' + this.connNum + '] DONE, RECEIVED RESPONSE');
|
278 | this.state = DO_NOTHING;
|
279 | callback(null, buffer);
|
280 | return;
|
281 | }
|
282 | }
|
283 | }.bind(this));
|
284 |
|
285 | socket.on('end', function () {
|
286 | DEBUG && console.log('[' + this.connNum + '] SOCKET CLOSED BY SERVER', (buffer && buffer.length));
|
287 | if (buffer) {
|
288 | if (!this.opts.waitForResponse) {
|
289 | callback(null, buffer);
|
290 | }
|
291 | buffer = null;
|
292 | }
|
293 | this.end();
|
294 | }.bind(this));
|
295 |
|
296 | socket.on('error', function (err) {
|
297 | this.end();
|
298 |
|
299 | if (!err.code || err.code !== 'ECONNREFUSED') {
|
300 | return callback(err);
|
301 | }
|
302 |
|
303 | this.adb.startServer(function (code) {
|
304 | if (code) {
|
305 | callback(new Error(__('Unable to start Android Debug Bridge server (exit code %s)', code)));
|
306 | } else {
|
307 | this.exec(cmd, callback, this.opts);
|
308 | }
|
309 | }.bind(this));
|
310 | }.bind(this));
|
311 |
|
312 | doSend && send();
|
313 | };
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | Connection.prototype.end = function end() {
|
319 | if (this.socket) {
|
320 | try {
|
321 | this.socket.end();
|
322 | } catch (ex) {
|
323 |
|
324 | }
|
325 | this.socket = null;
|
326 | }
|
327 | this.state = DO_NOTHING;
|
328 | };
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | function ADB(config) {
|
338 | this.config = config;
|
339 | if (config && config.get('android.debugadb', false)) {
|
340 | DEBUG = true;
|
341 | }
|
342 | }
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | ADB.prototype.version = function version(callback) {
|
349 | const conn = new Connection(this);
|
350 | conn.exec('host:version', function (err, data) {
|
351 | if (err) {
|
352 | return callback(err);
|
353 | }
|
354 | if (data === null || data === undefined) {
|
355 | return callback(new Error(`Unable to get adb version, received value ${data}`));
|
356 | }
|
357 |
|
358 | callback(null, '1.0.' + parseInt(data, 16));
|
359 | });
|
360 | };
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 | function parseDevices(adb, callback, err, data) {
|
370 | if (err) {
|
371 | callback(err);
|
372 | return;
|
373 | }
|
374 |
|
375 | var EmulatorManager = require('./emulator'),
|
376 | emuMgr = new EmulatorManager(adb.config);
|
377 |
|
378 | async.series((data || '').toString().split('\n').map(function (line) {
|
379 | return function (done) {
|
380 | var p = line.split(/\s+/);
|
381 | if (p.length <= 1) {
|
382 | return done();
|
383 | }
|
384 |
|
385 | var info = {
|
386 | id: p.shift(),
|
387 | state: p.shift()
|
388 | };
|
389 |
|
390 | if (info.state !== 'device') {
|
391 | emuMgr.isEmulator(info.id, function (err, emu) {
|
392 | info.emulator = emu || false;
|
393 | done(null, info);
|
394 | });
|
395 | return;
|
396 | }
|
397 |
|
398 | adb.shell(info.id, 'getprop', function (err, data) {
|
399 | if (!err && data) {
|
400 | const re = /^\[([^\]]*)\]: \[(.*)\]\s*$/;
|
401 | data.toString().split('\n').forEach(function (line) {
|
402 | const m = line.match(re);
|
403 | if (m) {
|
404 | const key = m[1];
|
405 | const value = m[2];
|
406 |
|
407 | switch (key) {
|
408 | case 'ro.product.model.internal':
|
409 | info.modelnumber = value;
|
410 | break;
|
411 | case 'ro.build.version.release':
|
412 | case 'ro.build.version.sdk':
|
413 | case 'ro.product.brand':
|
414 | case 'ro.product.device':
|
415 | case 'ro.product.manufacturer':
|
416 | case 'ro.product.model':
|
417 | case 'ro.product.name':
|
418 | info[key.split('.').pop()] = value;
|
419 | break;
|
420 | case 'ro.genymotion.version':
|
421 | info.genymotion = value;
|
422 | break;
|
423 | default:
|
424 | if (key.indexOf('ro.product.cpu.abi') === 0) {
|
425 | Array.isArray(info.abi) || (info.abi = []);
|
426 | value.split(',').forEach(function (abi) {
|
427 | abi = abi.trim();
|
428 | if (abi && info.abi.indexOf(abi) === -1) {
|
429 | info.abi.push(abi);
|
430 | }
|
431 | });
|
432 | }
|
433 | break;
|
434 | }
|
435 | }
|
436 | });
|
437 | }
|
438 |
|
439 | emuMgr.isEmulator(info.id, function (err, emu) {
|
440 | info.emulator = emu || false;
|
441 | done(null, info);
|
442 | });
|
443 | });
|
444 | };
|
445 | }), function (err, results) {
|
446 | callback(null, results.filter(device => !!device));
|
447 | });
|
448 | }
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 | ADB.prototype.devices = function devices(callback) {
|
455 | new Connection(this).exec('host:devices', function (err, data) {
|
456 | parseDevices(this, callback, err, data);
|
457 | }.bind(this), { waitForResponse: true });
|
458 | };
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 | ADB.prototype.trackDevices = function trackDevices(callback) {
|
466 | var conn = new Connection(this),
|
467 | _t = this,
|
468 | queue = async.queue(function (task, next) {
|
469 | parseDevices(_t, function (err, results) {
|
470 | callback(err, results);
|
471 | next();
|
472 | }, task.err, task.data);
|
473 | }, 1);
|
474 |
|
475 | conn.exec('host:track-devices', function (err, data) {
|
476 | queue.push({ err: err, data: data });
|
477 | }, { waitForResponse: true });
|
478 |
|
479 | return conn;
|
480 | };
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 | function androidDetect(config, callback) {
|
488 | (require('./android')).detect(config, null, function (results) {
|
489 | if (results.sdk && results.sdk.executables.adb) {
|
490 | callback(null, results);
|
491 | } else {
|
492 | callback(new Error(__('Android SDK not found')));
|
493 | }
|
494 | });
|
495 | }
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 | ADB.prototype.startServer = function startServer(callback) {
|
502 | androidDetect(this.config, function (err, results) {
|
503 | if (err) {
|
504 | return callback(err);
|
505 | }
|
506 | appc.subprocess.run(results.sdk.executables.adb, 'start-server', function (code, out, err) {
|
507 | callback(code ? new Error(__('Failed to start ADB (code %s): %s', code, err)) : null);
|
508 | });
|
509 | });
|
510 | };
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 | ADB.prototype.stopServer = function stopServer(callback) {
|
517 | androidDetect(this.config, function (err, results) {
|
518 | if (err) {
|
519 | return callback(err);
|
520 | }
|
521 | appc.subprocess.run(results.sdk.executables.adb, 'kill-server', function (code, _out, _err) {
|
522 | callback(code);
|
523 | });
|
524 | });
|
525 | };
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 | ADB.prototype.shell = function shell(deviceId, cmd, callback) {
|
536 | var conn = new Connection(this);
|
537 | conn.exec('host:transport:' + deviceId, function (err, _data) {
|
538 | if (err) {
|
539 | callback(err);
|
540 | } else {
|
541 | conn.exec('shell:' + cmd.replace(/^shell:/, ''), function (err, result) {
|
542 | callback(err, result);
|
543 | }, { bufferUntilClose: true, noLength: true });
|
544 | }
|
545 | });
|
546 | };
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 | ADB.prototype.installApp = function installApp(deviceId, apkFile, opts, callback) {
|
557 | if (typeof opts === 'function') {
|
558 | callback = opts;
|
559 | opts = {};
|
560 | }
|
561 | apkFile = appc.fs.resolvePath(apkFile);
|
562 | if (!fs.existsSync(apkFile)) {
|
563 | callback(new Error(__('APK file "%s" does not exist', apkFile)));
|
564 | return;
|
565 | }
|
566 |
|
567 | this.devices(function (err, devices) {
|
568 | if (err) {
|
569 | return callback(err);
|
570 | }
|
571 |
|
572 |
|
573 | devices = devices.filter(d => d.id === deviceId);
|
574 | if (devices.length < 1) {
|
575 | return callback(new Error(__('device not found')));
|
576 | }
|
577 | const deviceInfo = devices[0];
|
578 |
|
579 | androidDetect(this.config, function (err, results) {
|
580 | if (err) {
|
581 | return callback(err);
|
582 | }
|
583 |
|
584 |
|
585 | let deviceApiLevel = 1;
|
586 | if (deviceInfo.sdk) {
|
587 | const value = parseInt(deviceInfo.sdk);
|
588 | if (!isNaN(value)) {
|
589 | deviceApiLevel = value;
|
590 | }
|
591 | }
|
592 |
|
593 |
|
594 | const args = [];
|
595 | args.push('-s', deviceId);
|
596 | args.push('install');
|
597 | args.push('-r');
|
598 | if (deviceApiLevel >= 17) {
|
599 |
|
600 |
|
601 | args.push('-d');
|
602 | }
|
603 | args.push(apkFile);
|
604 |
|
605 |
|
606 | opts.logger && opts.logger.trace(__('Executing: %s', [ results.sdk.executables.adb ].concat(args).join(' ').cyan));
|
607 | appc.subprocess.run(results.sdk.executables.adb, args, function (code, out, err) {
|
608 | var m = out.match(/^Failure \[(.+)\]$/m);
|
609 | if ((code && err.indexOf('No space left on device') !== -1) || (!code && m && m[1] === 'INSTALL_FAILED_INSUFFICIENT_STORAGE')) {
|
610 | callback(new Error(__('Not enough free space on device')));
|
611 | } else if (m && m[1] === 'INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES') {
|
612 | callback(__('The app is already installed, but signed with a different certificate') + '\n'
|
613 | + __('You need to either manually uninstall the app or rebuild using the same certificate that was used to sign the installed app'));
|
614 | } else if (m) {
|
615 | callback(new Error(m[1]));
|
616 | } else if (code) {
|
617 | callback(new Error(out.trim() + '\n' + err.trim()));
|
618 | } else {
|
619 |
|
620 | m = out.match(/^Error: (.+)$/m);
|
621 | if (m) {
|
622 | callback(new Error(m[1]));
|
623 | } else {
|
624 | callback();
|
625 | }
|
626 | }
|
627 | });
|
628 | });
|
629 | }.bind(this));
|
630 | };
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 | ADB.prototype.ps = function ps(deviceId, callback) {
|
638 | var outputCallback = function (err, data) {
|
639 | if (err) {
|
640 | callback(err);
|
641 | } else {
|
642 |
|
643 | var dataStr = data.toString().trim();
|
644 | if (dataStr.startsWith('bad pid \'-A\'') || dataStr.endsWith('NAME')) {
|
645 | this.shell(deviceId, 'ps', outputCallback);
|
646 | } else {
|
647 | callback(null, data);
|
648 | }
|
649 | }
|
650 | }.bind(this);
|
651 | this.shell(deviceId, 'ps -A', outputCallback);
|
652 | };
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 | ADB.prototype.getPid = function getPid(deviceId, appid, callback) {
|
661 | this.ps(deviceId, function (err, data) {
|
662 | if (err) {
|
663 | callback(err);
|
664 | } else {
|
665 | var lines = data.toString().split('\n'),
|
666 | i = 0,
|
667 | len = lines.length,
|
668 | columns;
|
669 | for (; i < len; i++) {
|
670 | columns = lines[i].trim().split(/\s+/);
|
671 | if (columns.pop() == appid) {
|
672 | callback(null, parseInt(columns[1]));
|
673 | return;
|
674 | }
|
675 | }
|
676 | callback(null, 0);
|
677 | }
|
678 | });
|
679 | };
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 | ADB.prototype.startApp = function startApp(deviceId, appid, activity, callback) {
|
689 |
|
690 |
|
691 |
|
692 |
|
693 |
|
694 |
|
695 | this.shell(deviceId, 'am start -n ' + appid + '/.' + activity.replace(/^\./, '') + ' -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000', callback);
|
696 | };
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 | ADB.prototype.stopApp = function stopApp(deviceId, appid, callback) {
|
705 | this.getPid(deviceId, appid, function (err, pid) {
|
706 | if (!err && pid) {
|
707 | this.shell(deviceId, 'am force-stop ' + appid, function (err, data) {
|
708 | if (data.toString().indexOf('Unknown command: force-stop') !== -1) {
|
709 | this.shell(deviceId, 'kill ' + pid, callback);
|
710 | } else {
|
711 | callback(err, data);
|
712 | }
|
713 | }.bind(this));
|
714 | return;
|
715 | }
|
716 | callback(new Error(__('Application "%s" is not running', appid)));
|
717 | }.bind(this));
|
718 | };
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 | ADB.prototype.forward = function forward(deviceId, src, dest, callback) {
|
728 | androidDetect(this.config, function (err, results) {
|
729 | if (err) {
|
730 | return callback(err);
|
731 | }
|
732 | appc.subprocess.run(results.sdk.executables.adb, [ '-s', deviceId, 'forward', src, dest ], function (code, _out, _err) {
|
733 | callback(code);
|
734 | });
|
735 | });
|
736 | };
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 | ADB.prototype.push = function push(deviceId, src, dest, callback) {
|
746 | src = appc.fs.resolvePath(src);
|
747 | if (!fs.existsSync(src)) {
|
748 | callback(new Error(__('Source file "%s" does not exist', src)));
|
749 | } else {
|
750 | androidDetect(this.config, function (err, results) {
|
751 | if (err) {
|
752 | return callback(err);
|
753 | }
|
754 | appc.subprocess.run(results.sdk.executables.adb, [ '-s', deviceId, 'push', src, dest ], function (code, _out, _err) {
|
755 | callback(code);
|
756 | });
|
757 | });
|
758 | }
|
759 | };
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 | ADB.prototype.pull = function pull(deviceId, src, dest, callback) {
|
769 | dest = appc.fs.resolvePath(dest);
|
770 | var destDir = path.dirname(dest);
|
771 |
|
772 | try {
|
773 | fs.ensureDirSync(destDir);
|
774 |
|
775 | androidDetect(this.config, function (err, results) {
|
776 | if (err) {
|
777 | return callback(err);
|
778 | }
|
779 | appc.subprocess.run(results.sdk.executables.adb, [ '-s', deviceId, 'pull', src, dest ], function (code, _out, _err) {
|
780 | callback(code);
|
781 | });
|
782 | });
|
783 | } catch (ex) {
|
784 | callback(new Error(__('Failed to create destination directory "%s"', destDir)));
|
785 | }
|
786 | };
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 | ADB.prototype.logcat = function logcat(deviceId, handler, callback) {
|
796 | androidDetect(this.config, function (err, results) {
|
797 | if (err) {
|
798 | return callback(err);
|
799 | }
|
800 |
|
801 | var child = spawn(results.sdk.executables.adb, [ '-s', deviceId, 'logcat', '-v', 'brief', '-b', 'main' ]),
|
802 | splitter = child.stdout.pipe(StreamSplitter('\n'));
|
803 |
|
804 |
|
805 | splitter.encoding = 'utf8';
|
806 | splitter.on('token', function (data) {
|
807 | handler(data);
|
808 | });
|
809 |
|
810 | child.on('close', function () {
|
811 | callback();
|
812 | });
|
813 | });
|
814 | };
|
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 |
|
825 |
|
826 |
|
827 |
|
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 |
|
857 |
|
858 |
|
859 |
|
860 |
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 |
|
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|