/
android.js
72 lines (53 loc) · 1.89 KB
/
android.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var sys = require('sys');
var exec = require('child_process').exec;
exports.isInstalled = function (pattern, isInstalledCallback) {
var isInstalledCommand = "adb shell pm list packages| grep package:" + pattern;
console.log(isInstalledCommand);
exec(isInstalledCommand, function (err, stdout, stderr) {
console.log("result:\n" + stdout);
isInstalledCallback(stdout.indexOf(pattern) >= 0);
});
}
exports.install = function (pathToApk, completedCallback) {
sys.print("installing " + pathToApk);
var install = "adb install " + pathToApk;
exec(install, function (err, stdout, stderr) {
sys.print("result of " + install + ": " + stdout);
completedCallback();
});
}
exports.firstDevice = function (callback) {
adbDevices = exec("adb devices", function (error, stdout, stderr) {
var findDeviceId = /\n([\d\w]+)\s+/m;
var match = findDeviceId.exec(stdout);
console.log("adb devices:\n" + stdout);
if (match != null) {
callback(match[1]);
} else {
callback();
}
});
}
exports.push = function (from, to, callback) {
exec("adb push " + from + " " + to, function(error, stdout, stderr){
callback(error);
});
}
exports.sendBroadcast = function (options, callback) {
var extras = "";
for (key in options.extras) {
extras += " -e \"" + key + "\" " + options.extras[key];
}
var broadcastCmd = "adb shell am broadcast -a \"" + options.action + "\"" + extras;
console.log("sending command: " + broadcastCmd);
exec(broadcastCmd, function (error, stdout, stderr) {
var dataMatch = (/data="(\{.*\})"/m).exec(stdout);
var response = {};
if (dataMatch) {
response.data = dataMatch[1];
response.message = stdout;
}
callback(response);
}
);
};