forked from naman14/gnome-android-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adbhelper.js
155 lines (114 loc) · 5.47 KB
/
adbhelper.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const GLib = imports.gi.GLib;
function findDevices() {
let [res, out, error] = GLib.spawn_sync(null, ["bash", "-c", "adb devices -l | awk 'NR>1 {print $1}'"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
if (!isEmpty(error.toString())) {
return { error: error.toString() };
}
if (!isEmpty(out.toString())) {
let devices = [];
let array = out.toString().split('\n');
for (var i = 0; i < array.length; i++) {
let deviceId = array[i];
if (!isEmpty(deviceId)) {
devices.push(getDeviceDetail(deviceId));
}
}
return {
devices: devices
}
} else {
return { error: "No devices found" }
}
}
function getDeviceDetail(deviceId) {
let [res, out, error] = GLib.spawn_sync(null, ["bash", "-c", "adb -s " + deviceId + " shell getprop ro.product.model"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
let device;
if (!isEmpty(error.toString())) {
return;
}
if (!isEmpty(out.toString())) {
device = { deviceId: deviceId, name: out.toString() }
return device;
}
}
/**
* Get Android version
*/
function getAndroidVersion(deviceId) {
let [res, out, error] = GLib.spawn_sync(null, ["bash", "-c", "adb -s " + deviceId + " shell getprop ro.build.version.release"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
let version;
if (!isEmpty(error.toString())) {
return;
}
if (!isEmpty(out.toString())) {
version = { version: out.toString() }
return version;
}
}
/**
* Get screenrecord process pid
*/
function getScreenrecordPid(deviceId) {
let [res, out, error] = GLib.spawn_sync(null, ["bash", "-c", "adb -s " + deviceId + " shell ps | grep screenrecord | awk '{print $2}'"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
let pid;
if (!isEmpty(error.toString())) {
return;
}
if (!isEmpty(out.toString())) {
pid = { pid: out.toString() }
return pid;
}
}
//start adb daemon on init
function startDaemon() {
GLib.spawn_async(null, ["bash", "-c", "adb devices"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
}
function takeScreenshot(deviceId) {
//current time
let time = '$(date +%Y-%m-%d-%H:%M:%S)';
// get Android version
let version = getAndroidVersion(deviceId).version.trim();
// reference: http://www.stkent.com/2016/08/28/capturing-Nougat-screenshots-using-adb-shell.html
// if Nougat (7) use new direct method
if (version >= 7) {
GLib.spawn_async(null, ["bash", "-c", "adb -s " + deviceId + " shell screencap -p > ~/Desktop/screen" + time + ".png"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
} else {
// if lower than Nougat use old method
GLib.spawn_async(null, ["bash", "-c", "adb -s " + deviceId + " shell screencap -p | sed 's/\r$//' > ~/Desktop/screen" + time + ".png"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
// in case the image is corrupted comment the line above and uncomment line under
//GLib.spawn_async(null, ["bash", "-c", "adb -s "+ deviceId +" shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > ~/Desktop/screen"+ time +".png"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
}
}
function recordScreen(deviceId) {
let time = '$(date +%Y-%m-%d-%H:%M:%S)';
GLib.spawn_async(null, ["bash", "-c", "adb -s " + deviceId + " shell screenrecord /sdcard/screenrecord.mp4"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
}
function stopScreenRecording(deviceId) {
let time = '$(date +%Y-%m-%d-%H:%M:%S)';
// get PID of the screenrecord process
let pid = getScreenrecordPid(deviceId).pid.trim();
GLib.spawn_sync(null, ["bash", "-c", "adb -s " + deviceId + " shell kill -SIGINT " + pid], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
//GLib.spawn_sync(null, ["bash", "-c", "adb -s "+ deviceId +" pull /sdcard/screenrecord.mp4 ~/Desktop/screenrecord-"+ time +".mp4"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
// kill screencrecord causes unexpected end of the video. Sleep 1 gives us 1 s to finish the screenrecord process. Increase if video still corrputed
GLib.spawn_sync(null, ["bash", "-c", "sleep 1; adb -s " + deviceId + " pull /sdcard/screenrecord.mp4 ~/Desktop/screenrecord-" + time + ".mp4"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
}
function establishTCPConnection(deviceId) {
let deviceIp = getDeviceIp(deviceId);
GLib.spawn_sync(null, ["bash", "-c", "adb -s " + deviceId + " tcpip 5555"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
let [res, out, error] = GLib.spawn_sync(null, ["bash", "-c", "adb -s " + deviceId + " connect " + deviceIp + ":5555"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
return out.toString().trim();
}
function useUsb() {
let [res, out, error] = GLib.spawn_async(null, ["bash", "-c", "adb usb"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
}
function captureBugReport(deviceId) {
let time = '$(date +%Y-%m-%d-%H:%M:%S)';
let [res, out, error] = GLib.spawn_async(null, ["bash", "-c", "adb -s " + deviceId + " bugreport > ~/Desktop/bugreport" + time + ".txt"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
}
function getDeviceIp(deviceId) {
let [res, out, error] = GLib.spawn_sync(null, ["bash", "-c", "adb -s " + deviceId + " shell ip route | awk '{print $9}'"], null, GLib.SpawnFlags.SEARCH_PATH, null, null);
return out.toString().trim();
}
function isEmpty(str) {
return (!str || str.length === 0 || !str.trim());
}