28
28
import java .awt .Frame ;
29
29
import java .io .*;
30
30
import java .util .ArrayList ;
31
- import java .util .HashMap ;
32
- import java .util .List ;
33
31
import java .util .Map ;
34
32
35
33
@@ -45,15 +43,15 @@ public class AVD {
45
43
"and your sketch may be launching shortly.)" ;
46
44
47
45
private static final String COMMAND_LINE_TUT_URL =
48
- "http ://android.processing.org/tutorials/command_line/index .html" ;
46
+ "https ://developer. android.com/studio/command-line/avdmanager .html" ;
49
47
50
48
static private final String AVD_LOAD_TITLE =
51
49
"Could not load the AVD." ;
52
50
static private final String AVD_LOAD_MESSAGE =
53
51
"This could mean that the Android tools need to be updated,<br>" +
54
52
"or that the Processing AVD should be deleted (it will<br>" +
55
53
"automatically re-created the next time you run Processing).<br><br>" +
56
- "You can use the SDK command- line tools to update the SDK or list<br>" +
54
+ "You can use the avdmanager command line tool to create AVDs manually and list<br>" +
57
55
"the current AVDs, check <a href=\" " + COMMAND_LINE_TUT_URL + "\" >this online tutorial</a> for more info." ;
58
56
59
57
private static final String GETTING_START_TUT_URL =
@@ -66,54 +64,52 @@ public class AVD {
66
64
"found in <a href=\" " + GETTING_START_TUT_URL + "\" >this online tutorial</a>." ;
67
65
68
66
static final String DEFAULT_SDCARD_SIZE = "64M" ;
67
+ static final String DEVICE_DEFINITION = "Nexus 5" ;
68
+ static final String DEVICE_SKIN = "1080x1920" ;
69
+
70
+ static final String DEVICE_WEAR_DEFINITION = "wear_square_280_280dpi" ;
71
+ static final String DEVICE_WEAR_SKIN = "280x280" ;
69
72
70
73
/** Name of this avd. */
71
74
protected String name ;
72
75
73
- /** "android-7" or "Google Inc.:Google APIs:7 " */
76
+ /** "system-images; android-25;google_apis;x86 " */
74
77
protected String sdkId ;
78
+
79
+ protected String device ;
80
+ protected String skin ;
75
81
76
82
static boolean invalidPackage ;
77
83
static ArrayList <String > avdList ;
78
84
static ArrayList <String > badList ;
79
85
// static ArrayList<String> skinList;
80
86
81
- private Map <String , String > preferredAbi = new HashMap <>(30 );
82
- private List <String > abiList = new ArrayList <>();
83
87
private static Process process ;
84
88
85
89
/** Default virtual device used by Processing. */
86
90
static public final AVD mobileAVD =
87
- new AVD ("Processing-0" + Base . getRevision () ,
91
+ new AVD ("processing-phone" ,
88
92
"system-images;" + AndroidBuild .TARGET_PLATFORM + ";" +
89
- SysImageDownloader .SYSTEM_IMAGE_TAG + ";x86" , SysImageDownloader . SYSTEM_IMAGE_TAG );
90
- // "Google Inc.:Google APIs:" + AndroidBuild.sdkVersion );
93
+ SysImageDownloader .SYSTEM_IMAGE_TAG + ";x86" ,
94
+ DEVICE_DEFINITION , DEVICE_SKIN );
91
95
92
96
/** Default virtual wear device used by Processing. */
93
97
static public final AVD wearAVD =
94
- new AVD ("Processing-Wear-0" + Base . getRevision () ,
98
+ new AVD ("processing-watch" ,
95
99
"system-images;" + AndroidBuild .TARGET_PLATFORM + ";" +
96
- SysImageDownloader .SYSTEM_IMAGE_WEAR_TAG + ";x86" , SysImageDownloader .SYSTEM_IMAGE_WEAR_TAG );
100
+ SysImageDownloader .SYSTEM_IMAGE_WEAR_TAG + ";x86" ,
101
+ DEVICE_WEAR_DEFINITION , DEVICE_WEAR_SKIN );
102
+
97
103
98
104
public AVD (final String name , final String sdkId ,
99
- final String tag ) {
105
+ final String device , final String skin ) {
100
106
this .name = name ;
101
107
this .sdkId = sdkId ;
108
+ this .device = device ;
109
+ this .skin = skin ;
102
110
//initializeAbiList(tag);
103
111
}
104
112
105
- private void initializeAbiList (String tag ) {
106
- if (abiList .size () == 0 ) {
107
- // The order in this list determines the preference of one abi over the other
108
- abiList .add (tag + "/x86" );
109
- abiList .add (tag + "/x86_64" );
110
- abiList .add (tag + "/armeabi-v7a" );
111
- // abiList.add("google_apis/x86");
112
- // abiList.add("google_apis/x86_64");
113
- // abiList.add("google_apis/armeabi-v7a");
114
- }
115
- }
116
-
117
113
118
114
static protected void list (final AndroidSDK sdk ) throws IOException {
119
115
try {
@@ -199,85 +195,23 @@ protected boolean badness() {
199
195
return false ;
200
196
}
201
197
202
-
203
- protected void initTargets (final AndroidSDK sdk ) throws IOException {
204
- preferredAbi .clear ();
205
- ProcessBuilder pb = new ProcessBuilder (sdk .getAvdManagerPath (), "list" , "target" );
206
-
207
- Map <String , String > env = pb .environment ();
208
- env .clear ();
209
- env .put ("JAVA_HOME" , Platform .getJavaHome ().getCanonicalPath ());
210
- pb .redirectErrorStream (true );
211
-
212
- process = pb .start ();
213
- InputStream stdout = process .getInputStream ();
214
- BufferedReader reader = new BufferedReader (new InputStreamReader (stdout ));
215
-
216
- try {
217
- process .waitFor ();
218
-
219
- String api = null ;
220
- String [] abis = null ;
221
- String line ;
222
- while ((line = reader .readLine ()) != null ) {
223
- line = line .trim ();
224
- if (line .equals ("" )) continue ;
225
-
226
- if (line .indexOf ("API level" ) == 0 ) {
227
- String [] m = line .split (":" );
228
- if (1 < m .length ) {
229
- api = m [1 ];
230
- api = api .trim ();
231
- }
232
- }
233
-
234
- if (line .indexOf ("Tag/ABIs" ) == 0 ) {
235
- String [] m = line .split (":" );
236
- if (1 < m .length ) {
237
- String str = m [1 ];
238
- abis = str .split ("," );
239
- for (int i = 0 ; i < abis .length ; i ++) {
240
- abis [i ] = abis [i ].trim ();
241
- }
242
- }
243
- }
244
-
245
- if (api != null && abis != null ) {
246
- for (String abi : abis ) {
247
- if (abiList .indexOf (abi ) == -1 ) continue ;
248
- if (preferredAbi .get (api ) == null ) {
249
- preferredAbi .put (api , abi );
250
- } else if (abiList .indexOf (preferredAbi .get (api )) < abiList .indexOf (abi )) {
251
- preferredAbi .put (api , abi );
252
- }
253
- }
254
- api = null ;
255
- abis = null ;
256
- }
257
- }
258
- } catch (InterruptedException e ) {
259
- } finally {
260
- process .destroy ();
261
- }
262
- }
263
-
264
-
265
- protected boolean noTargets (final AndroidSDK sdk ) throws IOException {
266
- initTargets (sdk );
267
- // return preferredAbi.size() == 0;
268
- return preferredAbi .get (AndroidBuild .TARGET_SDK ) == null ;
269
- }
270
-
271
198
272
199
protected boolean create (final AndroidSDK sdk ) throws IOException {
273
200
//initTargets(sdk);
274
-
201
+ File sketchbookFolder = processing .app .Base .getSketchbookFolder ();
202
+ File androidFolder = new File (sketchbookFolder , "android" );
203
+ if (!androidFolder .exists ()) androidFolder .mkdir ();
204
+ File avdPath = new File (androidFolder , "avd/" + name );
205
+
275
206
ProcessBuilder pb = new ProcessBuilder (
276
207
sdk .getAvdManagerPath (),
277
208
"create" , "avd" ,
278
- "-n" , name ,
209
+ "-n" , name ,
279
210
"-k" , sdkId ,
280
- "-c" , DEFAULT_SDCARD_SIZE
211
+ "-c" , DEFAULT_SDCARD_SIZE ,
212
+ "-d" , device ,
213
+ "-p" , avdPath .getAbsolutePath (),
214
+ "-f"
281
215
);
282
216
283
217
// avdmanager create avd -n "Wear-Processing-0254" -k "system-images;android-25;google_apis;x86" -c 64M
@@ -308,17 +242,26 @@ protected boolean create(final AndroidSDK sdk) throws IOException {
308
242
process .waitFor ();
309
243
310
244
if (process .exitValue () == 0 ) {
245
+ // Add skin to AVD's config file
246
+ File configFile = new File (avdPath , "config.ini" );
247
+ if (configFile .exists ()) {
248
+ try (PrintWriter output = new PrintWriter (new FileWriter (configFile .getAbsolutePath (), true )))
249
+ {
250
+ output .printf ("%s\r \n " , "skin.name=" + skin );
251
+ }
252
+ catch (Exception e ) {}
253
+ }
311
254
return true ;
312
255
}
313
256
314
257
String line ;
315
258
StringBuilder output = new StringBuilder ();
316
- while ((line = reader .readLine ()) != null ) {
259
+ while ((line = reader .readLine ()) != null ) {
317
260
output .append (line );
318
261
}
319
262
if (output .toString ().contains ("Package path is not valid" )) {
320
263
// They didn't install the Google APIs
321
- // AndroidUtil.showMessage(AVD_TARGET_TITLE, AVD_TARGET_MESSAGE);
264
+ AndroidUtil .showMessage (AVD_TARGET_TITLE , AVD_TARGET_MESSAGE );
322
265
invalidPackage = true ;
323
266
} else {
324
267
// Just generally not working
@@ -336,13 +279,15 @@ protected boolean create(final AndroidSDK sdk) throws IOException {
336
279
return false ;
337
280
}
338
281
282
+
339
283
static public String getPort (boolean wear ) {
340
284
if (wear ) {
341
285
return EmulatorController .WEAR_PORT ;
342
286
} else {
343
287
return EmulatorController .DEFAULT_PORT ;
344
288
}
345
289
}
290
+
346
291
347
292
static public boolean ensureProperAVD (final Frame window , final AndroidMode mode ,
348
293
final AndroidSDK sdk , boolean wear ) {
@@ -399,4 +344,94 @@ static public boolean ensureProperAVD(final Frame window, final AndroidMode mode
399
344
}
400
345
return false ;
401
346
}
347
+
348
+
349
+ //////////////////////////////////////////////////////////////////////////////
350
+ // To remove:
351
+
352
+ /*
353
+
354
+ private Map<String, String> preferredAbi = new HashMap<>(30);
355
+ private List<String> abiList = new ArrayList<>();
356
+
357
+ protected boolean noTargets(final AndroidSDK sdk) throws IOException {
358
+ initTargets(sdk);
359
+ // return preferredAbi.size() == 0;
360
+ return preferredAbi.get(AndroidBuild.TARGET_SDK) == null;
361
+ }
362
+
363
+
364
+ private void initializeAbiList(String tag) {
365
+ if (abiList.size() == 0) {
366
+ // The order in this list determines the preference of one abi over the other
367
+ abiList.add(tag + "/x86");
368
+ abiList.add(tag + "/x86_64");
369
+ abiList.add(tag + "/armeabi-v7a");
370
+ // abiList.add("google_apis/x86");
371
+ // abiList.add("google_apis/x86_64");
372
+ // abiList.add("google_apis/armeabi-v7a");
373
+ }
374
+ }
375
+
376
+ protected void initTargets(final AndroidSDK sdk) throws IOException {
377
+ preferredAbi.clear();
378
+ ProcessBuilder pb = new ProcessBuilder(sdk.getAvdManagerPath(), "list", "target");
379
+
380
+ Map<String, String> env = pb.environment();
381
+ env.clear();
382
+ env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
383
+ pb.redirectErrorStream(true);
384
+
385
+ process = pb.start();
386
+ InputStream stdout = process.getInputStream();
387
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
388
+
389
+ try {
390
+ process.waitFor();
391
+
392
+ String api = null;
393
+ String[] abis = null;
394
+ String line;
395
+ while ((line = reader.readLine()) != null) {
396
+ line = line.trim();
397
+ if (line.equals("")) continue;
398
+
399
+ if (line.indexOf("API level") == 0) {
400
+ String[] m = line.split(":");
401
+ if (1 < m.length) {
402
+ api = m[1];
403
+ api = api.trim();
404
+ }
405
+ }
406
+
407
+ if (line.indexOf("Tag/ABIs") == 0) {
408
+ String[] m = line.split(":");
409
+ if (1 < m.length) {
410
+ String str = m[1];
411
+ abis = str.split(",");
412
+ for (int i = 0; i < abis.length; i++) {
413
+ abis[i] = abis[i].trim();
414
+ }
415
+ }
416
+ }
417
+
418
+ if (api != null && abis != null) {
419
+ for (String abi: abis) {
420
+ if (abiList.indexOf(abi) == -1) continue;
421
+ if (preferredAbi.get(api) == null) {
422
+ preferredAbi.put(api, abi);
423
+ } else if (abiList.indexOf(preferredAbi.get(api)) < abiList.indexOf(abi)) {
424
+ preferredAbi.put(api, abi);
425
+ }
426
+ }
427
+ api = null;
428
+ abis = null;
429
+ }
430
+ }
431
+ } catch (InterruptedException e) {
432
+ } finally {
433
+ process.destroy();
434
+ }
435
+ }
436
+ */
402
437
}
0 commit comments