forked from processing/processing-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevice.java
More file actions
418 lines (364 loc) · 13.7 KB
/
Device.java
File metadata and controls
418 lines (364 loc) · 13.7 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2013-16 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.mode.android;
import processing.app.Base;
import processing.app.RunnerListener;
import processing.app.exec.LineProcessor;
import processing.app.exec.ProcessRegistry;
import processing.app.exec.ProcessResult;
import processing.app.exec.StreamPump;
import processing.core.PApplet;
import processing.mode.android.LogEntry.Severity;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Device {
private final Devices env;
private final String id;
private final String features;
private final Set<Integer> activeProcesses = new HashSet<Integer>();
private final Set<DeviceListener> listeners =
Collections.synchronizedSet(new HashSet<DeviceListener>());
// public static final String APP_STARTED = "android.device.app.started";
// public static final String APP_ENDED = "android.device.app.ended";
private String packageName = "";
// mutable state
private Process logcat;
public Device(final Devices env, final String id) {
this.env = env;
this.id = id;
// http://android.stackexchange.com/questions/82169/howto-get-devices-features-with-adb
String concat = "";
try {
final ProcessResult res = adb("shell", "getprop", "ro.build.characteristics");
for (String line : res) {
concat += "," + line.toLowerCase();
}
} catch (final Exception e) {
}
this.features = concat;
}
public void bringLauncherToFront() {
try {
adb("shell", "am", "start",
"-a", "android.intent.action.MAIN",
"-c", "android.intent.category.HOME");
} catch (final Exception e) {
e.printStackTrace(System.err);
}
}
public boolean hasFeature(String feature) {
return -1 < features.indexOf(feature);
}
public String getName() {
String name = "";
try {
ProcessResult result = AndroidSDK.runADB("-s", id, "shell", "getprop", "ro.product.brand");
if (result.succeeded()) {
name += result.getStdout() + " ";
}
result = AndroidSDK.runADB("-s", id, "shell", "getprop", "ro.product.model");
if (result.succeeded()) {
name += result.getStdout();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return name + " [" + id + "]";
}
// adb -s emulator-5556 install helloWorld.apk
// : adb -s HT91MLC00031 install bin/Brightness-debug.apk
// 532 KB/s (190588 bytes in 0.349s)
// pkg: /data/local/tmp/Brightness-debug.apk
// Failure [INSTALL_FAILED_ALREADY_EXISTS]
// : adb -s HT91MLC00031 install -r bin/Brightness-debug.apk
// 1151 KB/s (190588 bytes in 0.161s)
// pkg: /data/local/tmp/Brightness-debug.apk
// Success
// safe to just always include the -r (reinstall) flag
public boolean installApp(final AndroidBuild build, final RunnerListener status) {
if (!isAlive()) {
return false;
}
bringLauncherToFront();
try {
final ProcessResult installResult = adb("install", "-r", build.getPathForAPK());
if (!installResult.succeeded()) {
status.statusError("Could not install the sketch.");
System.err.println(installResult);
return false;
}
String errorMsg = null;
for (final String line : installResult) {
if (line.startsWith("Failure")) {
errorMsg = line.substring(8);
if (line.contains("INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES")) {
boolean removeResult = removeApp(build.getPackageName());
if (removeResult) return installApp(build, status);
}
System.err.println(line);
}
}
if (errorMsg == null) {
status.statusNotice("Done installing.");
return true;
}
status.statusError("Error while installing " + errorMsg);
} catch (final IOException e) {
status.statusError(e);
} catch (final InterruptedException e) {
}
return false;
}
public boolean removeApp(String packageName) throws IOException, InterruptedException {
final ProcessResult removeResult = adb("uninstall", packageName);
if (!removeResult.succeeded()) {
return false;
}
return true;
}
// different version that actually runs through JDI:
// http://asantoso.wordpress.com/2009/09/26/using-jdb-with-adb-to-debugging-of-android-app-on-a-real-device/
public boolean launchApp(final String packageName)
throws IOException, InterruptedException {
if (!isAlive()) {
return false;
}
String[] cmd = {
"shell", "am", "start",
"-e", "debug", "true",
"-a", "android.intent.action.MAIN",
"-c", "android.intent.category.LAUNCHER",
"-n", packageName + "/.MainActivity"
};
// PApplet.println(cmd);
ProcessResult pr = adb(cmd);
if (Base.DEBUG) {
System.out.println(pr.toString());
}
// Sometimes this shows up on stdout, even though it returns 'success'
// Error type 2
// android.util.AndroidException: Can't connect to activity manager; is the system running?
if (pr.getStdout().contains("android.util.AndroidException")) {
System.err.println(pr.getStdout());
return false;
}
return pr.succeeded();
}
public boolean isEmulator() {
return id.startsWith("emulator");
}
public void setPackageName(String pkgName) {
packageName = pkgName;
}
// I/Process ( 9213): Sending signal. PID: 9213 SIG: 9
private static final Pattern SIG = Pattern
.compile("PID:\\s+(\\d+)\\s+SIG:\\s+(\\d+)");
private final List<String> stackTrace = new ArrayList<String>();
private class LogLineProcessor implements LineProcessor {
public void processLine(final String line) {
final LogEntry entry = new LogEntry(line);
// System.err.println("***************************************************");
// System.out.println(line);
// System.err.println(activeProcesses);
// System.err.println(entry.message);
if (entry.message.startsWith("PROCESSING")) {
// Old start/stop process detection, does not seem to work anymore.
// Should be ok to remove at some point.
if (entry.message.contains("onStart")) {
startProc(entry.source, entry.pid);
} else if (entry.message.contains("onStop")) {
endProc(entry.pid);
}
} else if (packageName != null && !packageName.equals("") &&
entry.message.contains("Start proc") &&
entry.message.contains(packageName)) {
// Sample message string from logcat when starting process:
// "Start proc 29318:processing.test.sketch001/u0a403 for activity processing.test.sketch001/.MainActivity"
try {
int idx0 = entry.message.indexOf("Start proc") + 11;
int idx1 = entry.message.indexOf(packageName) - 1;
String pidStr = entry.message.substring(idx0, idx1);
int pid = Integer.parseInt(pidStr);
startProc(entry.source, pid);
} catch (Exception ex) {
System.err.println("AndroidDevice: cannot find process id, console output will be disabled.");
}
} else if (packageName != null && !packageName.equals("") &&
entry.message.contains("Killing") &&
entry.message.contains(packageName)) {
// Sample message string from logcat when stopping process:
// "Killing 31360:processing.test.test1/u0a403 (adj 900): remove task"
try {
int idx0 = entry.message.indexOf("Killing") + 8;
int idx1 = entry.message.indexOf(packageName) - 1;
String pidStr = entry.message.substring(idx0, idx1);
int pid = Integer.parseInt(pidStr);
endProc(pid);
} catch (Exception ex) {
System.err.println("AndroidDevice: cannot find process id, console output will continue. " + packageName);
}
} else if (entry.source.equals("Process")) {
handleCrash(entry);
} else if (activeProcesses.contains(entry.pid)) {
handleConsole(entry);
}
}
private void handleCrash(final LogEntry entry) {
final Matcher m = SIG.matcher(entry.message);
if (m.find()) {
final int pid = Integer.parseInt(m.group(1));
final int signal = Integer.parseInt(m.group(2));
if (activeProcesses.contains(pid)) { // only report crashes of *our* sketches, por favor
/*
* A crashed sketch first gets a signal 3, which causes the
* "you've crashed" dialog to appear on the device. After
* the user dismisses the dialog, a sig 9 is sent.
* TODO: is it possible to forcibly dismiss the crash dialog?
*/
if (signal == 3) {
endProc(pid);
reportStackTrace(entry);
}
}
}
}
private void handleConsole(final LogEntry entry) {
final boolean isStackTrace = entry.source.equals("AndroidRuntime")
&& entry.severity == Severity.Error;
if (isStackTrace) {
if (!entry.message.startsWith("Uncaught handler")) {
stackTrace.add(entry.message);
System.err.println(entry.message);
}
} else if (entry.source.equals("System.out")
|| entry.source.equals("System.err")) {
if (entry.severity.useErrorStream) {
System.err.println(entry.message);
} else {
System.out.println(entry.message);
}
}
}
}
private void reportStackTrace(final LogEntry entry) {
if (stackTrace.isEmpty()) {
System.err.println("That's weird. Proc " + entry.pid
+ " got signal 3, but there's no stack trace.");
}
final List<String> stackCopy = Collections
.unmodifiableList(new ArrayList<String>(stackTrace));
for (final DeviceListener listener : listeners) {
listener.stackTrace(stackCopy);
}
stackTrace.clear();
}
void initialize() throws IOException, InterruptedException {
adb("logcat", "-c");
final String[] cmd = generateAdbCommand("logcat", "-v", "brief");
final String title = PApplet.join(cmd, ' ');
logcat = Runtime.getRuntime().exec(cmd);
ProcessRegistry.watch(logcat);
new StreamPump(logcat.getInputStream(), "log: " + title).addTarget(
new LogLineProcessor()).start();
new StreamPump(logcat.getErrorStream(), "err: " + title).addTarget(
System.err).start();
new Thread(new Runnable() {
public void run() {
try {
logcat.waitFor();
// final int result = logcat.waitFor();
// System.err.println("AndroidDevice: " + getId() + " logcat exited "
// + (result == 0 ? "normally" : "with status " + result));
} catch (final InterruptedException e) {
System.err
.println("AndroidDevice: logcat process monitor interrupted");
} finally {
shutdown();
}
}
}, "AndroidDevice: logcat process monitor").start();
// System.err.println("Receiving log entries from " + id);
}
synchronized void shutdown() {
if (!isAlive()) {
return;
}
// System.err.println(id + " is shutting down.");
if (logcat != null) {
logcat.destroy();
logcat = null;
ProcessRegistry.unwatch(logcat);
}
env.deviceRemoved(this);
if (activeProcesses.size() > 0) {
for (final DeviceListener listener : listeners) {
listener.sketchStopped();
}
}
listeners.clear();
}
synchronized boolean isAlive() {
return logcat != null;
}
public String getId() {
return id;
}
public Devices getEnv() {
return env;
}
private void startProc(final String name, final int pid) {
// System.err.println("Process " + name + " started at pid " + pid);
activeProcesses.add(pid);
}
private void endProc(final int pid) {
// System.err.println("Process " + pid + " stopped.");
activeProcesses.remove(pid);
for (final DeviceListener listener : listeners) {
listener.sketchStopped();
}
}
public void addListener(final DeviceListener listener) {
listeners.add(listener);
}
public void removeListener(final DeviceListener listener) {
listeners.remove(listener);
}
private ProcessResult adb(final String... cmd) throws InterruptedException, IOException {
final String[] adbCmd = generateAdbCommand(cmd);
return AndroidSDK.runADB(adbCmd);
}
private String[] generateAdbCommand(final String... cmd) {
// final String[] adbCmd = new String[3 + cmd.length];
// adbCmd[0] = "adb";
// adbCmd[1] = "-s";
// adbCmd[2] = getId();
// System.arraycopy(cmd, 0, adbCmd, 3, cmd.length);
// return adbCmd;
return PApplet.concat(new String[] { "adb", "-s", getId() }, cmd);
}
@Override
public String toString() {
return "[AndroidDevice " + getId() + "]";
}
@Override
public boolean equals(Object obj) {
return ((Device) obj).getId().equals(getId());
}
}