forked from processing/processing-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVD.java
More file actions
502 lines (413 loc) · 15.7 KB
/
AVD.java
File metadata and controls
502 lines (413 loc) · 15.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* -*- 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.Platform;
import processing.app.Preferences;
import processing.app.exec.LineProcessor;
import processing.app.exec.StreamPump;
import processing.core.PApplet;
import java.awt.Frame;
import java.io.*;
import java.util.ArrayList;
import java.util.Map;
public class AVD {
final static private int PHONE = 0;
final static private int WEAR = 1;
final static public String DEFAULT_ABI = "x86";
public static final String TARGET_SDK_ARM = "24";
public final static String DEFAULT_PHONE_PORT = "5566";
public final static String DEFAULT_WEAR_PORT = "5576";
static private final String AVD_CREATE_TITLE =
"Could not create the AVD";
static private final String AVD_CREATE_MESSAGE =
"The default Android emulator could not be set up. Make sure<br>" +
"that the Android SDK is installed properly, and that the<br>" +
"system images are installed for level %s.<br>" +
"(Between you and me, occasionally, this error is a red herring,<br>" +
"and your sketch may be launching shortly.)";
private static final String COMMAND_LINE_TUT_URL =
"https://developer.android.com/studio/command-line/avdmanager.html";
static private final String AVD_LOAD_TITLE =
"Could not load the AVD.";
static private final String AVD_LOAD_MESSAGE =
"This could mean that the Android tools need to be updated,<br>" +
"or that the Processing AVD should be deleted (it will<br>" +
"automatically re-created the next time you run Processing).<br><br>" +
"You can use the avdmanager command line tool to create AVDs manually and list " +
"the current AVDs, check <a href=\"" + COMMAND_LINE_TUT_URL + "\">this online tutorial</a> for more info.";
private static final String GETTING_START_TUT_URL =
"http://android.processing.org/tutorials/getting_started/index.html";
static private final String AVD_TARGET_TITLE =
"The SDK is not properly instaled";
static private final String AVD_TARGET_MESSAGE =
"Please re-read the installation instructions for Processing<br>" +
"found in <a href=\"" + GETTING_START_TUT_URL + "\">this online tutorial</a>.";
static final String DEFAULT_SDCARD_SIZE = "64M";
static final String DEVICE_DEFINITION = "Nexus One";
static final String DEVICE_SKIN = "480x800";
static final String DEVICE_WEAR_DEFINITION = "wear_square_280_280dpi";
static final String DEVICE_WEAR_SKIN = "280x280";
/** Name of this avd. */
protected String name;
protected String device;
protected String skin;
protected int type;
static ArrayList<String> avdList;
static ArrayList<String> badList;
/** "system-images;android-25;google_apis;x86" */
static ArrayList<String> wearImages;
static ArrayList<String> phoneImages;
private static Process process;
/** Default virtual device used by Processing. */
static public final AVD phoneAVD =
new AVD("processing-phone",
DEVICE_DEFINITION, DEVICE_SKIN, PHONE);
/** Default virtual wear device used by Processing. */
static public final AVD watchAVD =
new AVD("processing-watch",
DEVICE_WEAR_DEFINITION, DEVICE_WEAR_SKIN, WEAR);
public AVD(final String name, final String device, final String skin, int type) {
this.name = name;
this.device = device;
this.skin = skin;
this.type = type;
}
static public String getName(boolean wear) {
if (wear) {
return AVD.watchAVD.name;
} else {
return AVD.phoneAVD.name;
}
}
static public String getPreferredPlatform(boolean wear, String abi) {
if (wear) {
return AndroidBuild.TARGET_PLATFORM;
} else if (abi.equals("arm")) {
// The ARM images using Google APIs are too slow, so use the
// older Android (AOSP) images.
return "android-" + TARGET_SDK_ARM;
} else {
return AndroidBuild.TARGET_PLATFORM;
}
}
static public String getPreferredPort(boolean wear) {
String port = "";
if (wear) {
port = Preferences.get("android.emulator.port.wear");
if (port == null) {
port = DEFAULT_WEAR_PORT;
Preferences.set("android.emulator.port.wear", port);
}
} else {
port = Preferences.get("android.emulator.port.phone");
if (port == null) {
port = DEFAULT_PHONE_PORT;
Preferences.set("android.emulator.port.phone", port);
}
}
return port;
}
static protected String getPreferredTag(boolean wear, String abi) {
if (wear) {
return "android-wear";
} else if (abi.equals("arm")) {
// The ARM images using Google APIs are too slow, so use the
// older Android (AOSP) images.
return "default";
} else {
return "google_apis";
}
}
static protected String getPreferredABI() {
String abi = Preferences.get("android.emulator.image.abi");
if (abi == null) {
abi = DEFAULT_ABI;
Preferences.set("android.emulator.image.abi", abi);
}
return abi;
}
static protected void list(final AndroidSDK sdk) throws IOException {
String prefABI = getPreferredABI();
try {
avdList = new ArrayList<String>();
badList = new ArrayList<String>();
ProcessBuilder pb =
new ProcessBuilder(sdk.getAvdManagerPath(), "list", "avd");
Map<String, String> env = pb.environment();
env.clear();
env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
pb.redirectErrorStream(true);
process = pb.start();
StringWriter outWriter = new StringWriter();
new StreamPump(process.getInputStream(), "out: ").addTarget(outWriter).start();
process.waitFor();
String[] lines = PApplet.split(outWriter.toString(), '\n');
if (process.exitValue() == 0) {
String name = "";
String abi = "";
boolean badness = false;
for (String line : lines) {
String[] m = PApplet.match(line, "\\s+Name\\:\\s+(\\S+)");
String[] t = PApplet.match(line, "\\s+Tag/ABI\\:\\s+(\\S+)");
if (m != null) {
name = m[1];
}
if (t != null) {
abi = t[1];
if (-1 < abi.indexOf("/" + prefABI)) {
if (!badness) {
// System.out.println("good: " + m[1]);
avdList.add(name);
} else {
// System.out.println("bad: " + m[1]);
badList.add(name);
}
// } else {
// System.out.println("nope: " + line);
}
}
// "The following Android Virtual Devices could not be loaded:"
if (line.contains("could not be loaded:")) {
// System.out.println("starting the bad list");
// System.err.println("Could not list AVDs:");
// System.err.println(listResult);
badness = true;
// break;
}
}
} else {
System.err.println("Unhappy inside exists()");
System.err.println(outWriter.toString());
}
} catch (final InterruptedException ie) { }
finally {
process.destroy();
}
}
protected boolean exists(final AndroidSDK sdk) throws IOException {
if (avdList == null) {
list(sdk);
}
for (String avd : avdList) {
if (Base.DEBUG) {
System.out.println("AVD.exists() checking for " + name + " against " + avd);
}
if (avd.equals(name)) {
return true;
}
}
return false;
}
/**
* Return true if a member of the renowned and prestigious
* "The following Android Virtual Devices could not be loaded:" club.
* (Prestigious may also not be the right word.)
*/
protected boolean badness() {
for (String avd : badList) {
if (avd.equals(name)) {
return true;
}
}
return false;
}
protected boolean hasImages(final AndroidSDK sdk) throws IOException {
String abi = getPreferredABI();
if (type == PHONE) {
if (phoneImages == null) {
phoneImages = new ArrayList<String>();
getImages(phoneImages, sdk, abi);
}
return !phoneImages.isEmpty();
} else {
if (wearImages == null) {
wearImages = new ArrayList<String>();
getImages(wearImages, sdk, abi);
}
return !wearImages.isEmpty();
}
}
protected void refreshImages(final AndroidSDK sdk) throws IOException {
String abi = getPreferredABI();
if (type == PHONE) {
phoneImages = new ArrayList<String>();
getImages(phoneImages, sdk, abi);
} else {
wearImages = new ArrayList<String>();
getImages(wearImages, sdk, abi);
}
}
protected void getImages(final ArrayList<String> images, final AndroidSDK sdk,
final String imageAbi) throws IOException {
boolean wear = type == WEAR;
final String imagePlatform = getPreferredPlatform(wear, imageAbi);
final String imageTag = getPreferredTag(wear, imageAbi);
final String[] cmd = new String[] {
sdk.getAvdManagerPath(),
"create", "avd",
"-n", "dummy",
"-k", "dummy"
};
// Dummy avdmanager creation command to get the list of installed images
// TODO : Find a better way to get the list of installed images
ProcessBuilder pb = new ProcessBuilder(cmd);
if (Base.DEBUG) {
System.out.println(processing.core.PApplet.join(cmd, " "));
}
Map<String, String> env = pb.environment();
env.clear();
env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
pb.redirectErrorStream(true);
try {
process = pb.start();
StreamPump output = new StreamPump(process.getInputStream(), "out: ");
output.addTarget(new LineProcessor() {
@Override
public void processLine(String line) {
// System.out.println("dummy output ---> " + line);
if (images != null &&
line.contains(";" + imagePlatform) &&
line.contains(";" + imageTag) &&
line.contains(";" + imageAbi)) {
// System.out.println(" added image!");
images.add(line);
}
}
}).start();
process.waitFor();
} catch (final InterruptedException ie) {
ie.printStackTrace();
} finally {
process.destroy();
}
}
protected String getSdkId() throws IOException {
String abi = getPreferredABI();
if (type == PHONE) {
for (String image : phoneImages) {
if (image.contains(";" + abi)) return image;
}
} else {
for (String image : wearImages) {
if (image.contains(";" + abi)) return image;
}
}
// Could not find any suitable package
return "null";
}
protected boolean create(final AndroidSDK sdk) throws IOException {
File sketchbookFolder = processing.app.Base.getSketchbookFolder();
File androidFolder = new File(sketchbookFolder, "android");
if (!androidFolder.exists()) androidFolder.mkdir();
File avdPath = new File(androidFolder, "avd/" + name);
final String[] cmd = new String[] {
sdk.getAvdManagerPath(),
"create", "avd",
"-n", name,
"-k", getSdkId(),
"-c", DEFAULT_SDCARD_SIZE,
"-d", device,
"-p", avdPath.getAbsolutePath(),
"-f"
};
ProcessBuilder pb = new ProcessBuilder(cmd);
if (Base.DEBUG) {
System.out.println(processing.core.PApplet.join(cmd, " "));
}
// avdmanager create avd -n "Wear-Processing-0254" -k "system-images;android-25;google_apis;x86" -c 64M
// Set the list to null so that exists() will check again
avdList = null;
Map<String, String> env = pb.environment();
env.clear();
env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
pb.redirectErrorStream(true);
try {
process = pb.start();
// Passes 'no' to "Do you wish to create a custom hardware profile [no]"
OutputStream os = process.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
pw.println("no");
pw.flush();
pw.close();
os.flush();
os.close();
StringWriter outWriter = new StringWriter();
new StreamPump(process.getInputStream(), "out: ").addTarget(outWriter).start();
process.waitFor();
if (process.exitValue() == 0) {
// Add skin to AVD's config file
File configFile = new File(avdPath, "config.ini");
if (configFile.exists()) {
try (PrintWriter output = new PrintWriter(new FileWriter(configFile.getAbsolutePath(), true))) {
output.printf("%s\r\n", "skin.name=" + skin);
}
catch (Exception e) {}
}
return true;
}
if (outWriter.toString().contains("Package path is not valid")) {
// They didn't install the Google APIs
AndroidUtil.showMessage(AVD_TARGET_TITLE, AVD_TARGET_MESSAGE);
} else {
// Just generally not working
AndroidUtil.showMessage(AVD_CREATE_TITLE,
String.format(AVD_CREATE_MESSAGE, AndroidBuild.TARGET_SDK));
}
System.err.println(outWriter.toString());
//System.err.println(createAvdResult);
} catch (final InterruptedException ie) {
ie.printStackTrace();
} finally {
process.destroy();
}
return false;
}
static public boolean ensureProperAVD(final Frame window, final AndroidMode mode,
final AndroidSDK sdk, boolean wear) {
try {
AVD avd = wear ? watchAVD : phoneAVD;
if (avd.exists(sdk)) {
return true;
}
if (avd.badness()) {
AndroidUtil.showMessage(AVD_LOAD_TITLE, AVD_LOAD_MESSAGE);
return false;
}
if (!avd.hasImages(sdk)) {
// Check that the AVD for the other kind of device has been already
// downloaded, and if so, the downloader should not ask for an
// ABI again.
AVD other = wear ? phoneAVD : watchAVD;
boolean ask = !other.hasImages(sdk);
boolean res = AndroidSDK.locateSysImage(window, mode, wear, ask);
if (!res) {
return false;
} else {
avd.refreshImages(sdk);
}
}
if (avd.create(sdk)) {
return true;
}
} catch (final Exception e) {
e.printStackTrace();
AndroidUtil.showMessage(AVD_CREATE_TITLE,
String.format(AVD_CREATE_MESSAGE, AndroidBuild.TARGET_SDK));
}
return false;
}
}