forked from processing/processing-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidRunner.java
More file actions
334 lines (287 loc) · 10.9 KB
/
Copy pathAndroidRunner.java
File metadata and controls
334 lines (287 loc) · 10.9 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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-16 The Processing Foundation
Copyright (c) 2011-12 Ben Fry and Casey Reas
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 java.io.PrintStream;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import processing.app.ui.Editor;
import processing.app.Messages;
import processing.app.RunnerListener;
import processing.app.SketchException;
import processing.mode.java.runner.Runner;
public class AndroidRunner implements DeviceListener {
AndroidBuild build;
RunnerListener listener;
protected PrintStream sketchErr;
protected PrintStream sketchOut;
public AndroidRunner(AndroidBuild build, RunnerListener listener) {
this.build = build;
this.listener = listener;
if (listener instanceof Editor) {
Editor editor = (Editor) listener;
sketchErr = editor.getConsole().getErr();
sketchOut = editor.getConsole().getOut();
} else {
sketchErr = System.err;
sketchOut = System.out;
}
}
public void launch(Future<Device> deviceFuture, boolean wear) {
// try {
// runSketchOnDevice(Devices.getInstance().getEmulator(), "debug", AndroidEditor.this);
// } catch (final MonitorCanceled ok) {
// sketchStopped();
// statusNotice("Canceled.");
// }
listener.statusNotice("Waiting for device to become available...");
// final Device device = waitForDevice(deviceFuture, monitor);
final Device device = waitForDevice(deviceFuture, listener);
if (device == null || !device.isAlive()) {
listener.statusError("Lost connection with device while launching. Try again.");
// Reset the server, in case that's the problem. Sometimes when
// launching the emulator times out, the device list refuses to update.
Devices.killAdbServer();
return;
}
// if (!wear && device.hasFeature("watch")) {
// listener.statusError("Trying to install a regular app or wallpaper on a watch.");
// System.err.println("For some reason, Processing is trying to install the sketch\n" +
// "on the paired watch instead of the mobile device.");
//
// listener.statusError("Trying to install a watch face on a non-watch device. Select correct device.");
// return;
// }
device.addListener(this);
device.setPackageName(build.getPackageName());
// if (listener.isHalted()) {
//// if (monitor.isCanceled()) {
// throw new MonitorCanceled();
// }
// monitor.setNote("Installing sketch on " + device.getId());
listener.statusNotice("Installing sketch on " + device.getId());
// this stopped working with Android SDK tools revision 17
if (!device.installApp(build, listener)) {
if (device.getId().contains("emulator")) {
// More detailed message when using the emulator, to following discussion in
// https://code.google.com/p/android/issues/detail?id=104305
listener.statusError("Lost connection with emulator while installing. Try again.");
Messages.showWarning("The emulator is slooow...",
"This is common when the emulator is booting up for the first time.\n" +
"Just try again once the emulator is ready, or set the\n" +
"ADB_INSTALL_TIMEOUT environmental variable to have a.\n" +
"longer timeout, for example 5 minutes or more");
} else {
listener.statusError("Lost connection with device while installing. Try again.");
}
Devices.killAdbServer(); // see above
return;
}
// if (!build.antInstall()) {
// }
// if (monitor.isCanceled()) {
// throw new MonitorCanceled();
// }
// monitor.setNote("Starting sketch on " + device.getId());
listener.statusNotice("Starting sketch on " + device.getId());
if (startSketch(build, device)) {
listener.statusNotice("Sketch launched "
+ (device.isEmulator() ? "in the emulator" : "on the device") + ".");
} else {
listener.statusError("Could not start the sketch.");
}
listener.stopIndeterminate();
lastRunDevice = device;
//} finally {
// build.cleanup();
//}
//} finally {
////monitor.close();
//listener.stopIndeterminate();
//}
}
private volatile Device lastRunDevice = null;
/**
* @param target "debug" or "release"
*/
/*
private void runSketchOnDevice(Sketch sketch,
Future<Device> deviceFuture,
String target,
RunnerListener listener) {
// final IndeterminateProgressMonitor monitor =
// new IndeterminateProgressMonitor(this,
// "Building and launching...",
// "Creating project...");
AndroidBuild build = new AndroidBuild(sketch, listener);
try {
try {
if (build.createProject(target) == null) {
return;
}
} catch (SketchException se) {
listener.statusError(se);
} catch (IOException e) {
listener.statusError(e);
}
try {
// if (monitor.isCanceled()) {
// throw new MonitorCanceled();
// }
// monitor.setNote("Building...");
listener.statusNotice("Building...");
try {
if (!build.antBuild(target)) {
return;
}
} catch (SketchException se) {
listener.statusError(se);
}
// if (monitor.isCanceled()) {
// throw new MonitorCanceled();
// }
// monitor.setNote("Waiting for device to become available...");
listener.statusNotice("Waiting for device to become available...");
// final Device device = waitForDevice(deviceFuture, monitor);
final Device device = waitForDevice(deviceFuture, listener);
if (device == null || !device.isAlive()) {
listener.statusError("Device killed or disconnected.");
return;
}
device.addListener(this);
// if (listener.isHalted()) {
//// if (monitor.isCanceled()) {
// throw new MonitorCanceled();
// }
// monitor.setNote("Installing sketch on " + device.getId());
listener.statusNotice("Installing sketch on " + device.getId());
if (!device.installApp(build.getPathForAPK(target), listener)) {
listener.statusError("Device killed or disconnected.");
return;
}
// if (monitor.isCanceled()) {
// throw new MonitorCanceled();
// }
// monitor.setNote("Starting sketch on " + device.getId());
listener.statusNotice("Starting sketch on " + device.getId());
if (startSketch(build, device)) {
listener.statusNotice("Sketch launched on the "
+ (device.isEmulator() ? "emulator" : "device") + ".");
} else {
listener.statusError("Could not start the sketch.");
}
lastRunDevice = device;
} finally {
build.cleanup();
}
} finally {
// monitor.close();
listener.stopIndeterminate();
}
}
*/
// if user asks for 480x320, 320x480, 854x480 etc, then launch like that
// though would need to query the emulator to see if it can do that
private boolean startSketch(AndroidBuild build, final Device device) {
final String packageName = build.getPackageName();
try {
if (device.launchApp(packageName)) {
return true;
}
} catch (final Exception e) {
e.printStackTrace(System.err);
}
return false;
}
private Device waitForDevice(Future<Device> deviceFuture, RunnerListener listener) {
for (int i = 0; i < 120; i++) {
// if (monitor.isCanceled()) {
if (listener.isHalted()) {
deviceFuture.cancel(true);
// throw new MonitorCanceled();
return null;
}
try {
return deviceFuture.get(1, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
listener.statusError("Interrupted.");
return null;
} catch (final ExecutionException e) {
listener.statusError(e);
return null;
} catch (final TimeoutException expected) {
}
}
listener.statusError("No, on second thought, I'm giving up " +
"on waiting for that device to show up.");
return null;
}
private static final Pattern LOCATION =
Pattern.compile("\\(([^:]+):(\\d+)\\)");
private static final Pattern EXCEPTION_PARSER =
Pattern.compile("^\\s*([a-z]+(?:\\.[a-z]+)+)(?:: .+)?$",
Pattern.CASE_INSENSITIVE);
/**
* Currently figures out the first relevant stack trace line
* by looking for the telltale presence of "processing.android"
* in the package. If the packaging for droid sketches changes,
* this method will have to change too.
*/
public void stackTrace(final List<String> trace) {
final Iterator<String> frames = trace.iterator();
final String exceptionLine = frames.next();
final Matcher m = EXCEPTION_PARSER.matcher(exceptionLine);
if (!m.matches()) {
System.err.println("Can't parse this exception line:");
System.err.println(exceptionLine);
listener.statusError("Unknown exception");
return;
}
final String exceptionClass = m.group(1);
// if (Runner.handleCommonErrors(exceptionClass, exceptionLine, listener)) {
// return;
// }
Runner.handleCommonErrors(exceptionClass, exceptionLine, listener, sketchErr);
while (frames.hasNext()) {
final String line = frames.next();
if (line.contains("processing.android")) {
final Matcher lm = LOCATION.matcher(line);
if (lm.find()) {
final String filename = lm.group(1);
final int lineNumber = Integer.parseInt(lm.group(2)) - 1;
final SketchException rex =
build.placeException(exceptionLine, filename, lineNumber);
listener.statusError(rex == null ? new SketchException(exceptionLine, false) : rex);
return;
}
}
}
}
// called by AndroidMode.handleStop()...
public void close() {
if (lastRunDevice != null) {
lastRunDevice.bringLauncherToFront();
}
}
// sketch stopped on the device
public void sketchStopped() {
listener.stopIndeterminate();
listener.statusHalt();
}
}