22
22
package processing .mode .android ;
23
23
24
24
import processing .app .Base ;
25
- import processing .app .exec .ProcessHelper ;
26
- import processing .app .exec .ProcessResult ;
25
+ import processing .app .Platform ;
27
26
import processing .core .PApplet ;
28
27
29
28
import java .awt .Frame ;
30
- import java .io .IOException ;
29
+ import java .io .* ;
31
30
import java .util .ArrayList ;
32
31
import java .util .HashMap ;
33
32
import java .util .List ;
@@ -68,40 +67,39 @@ public class AVD {
68
67
69
68
static final String DEFAULT_SDCARD_SIZE = "64M" ;
70
69
71
- static final String DEFAULT_SKIN = "WVGA800" ;
72
- static final String WEAR_SKIN = "AndroidWearSquare" ;
73
-
74
70
/** Name of this avd. */
75
71
protected String name ;
76
72
77
73
/** "android-7" or "Google Inc.:Google APIs:7" */
78
- protected String target ;
74
+ protected String sdkId ;
79
75
76
+ static boolean invalidPackage ;
80
77
static ArrayList <String > avdList ;
81
78
static ArrayList <String > badList ;
82
79
// static ArrayList<String> skinList;
83
80
84
81
private Map <String , String > preferredAbi = new HashMap <>(30 );
85
82
private List <String > abiList = new ArrayList <>();
86
- private String skin ;
83
+ private static Process process ;
87
84
88
85
/** Default virtual device used by Processing. */
89
86
static public final AVD mobileAVD =
90
87
new AVD ("Processing-0" + Base .getRevision (),
91
- AndroidBuild .TARGET_PLATFORM , SysImageDownloader .SYSTEM_IMAGE_TAG , DEFAULT_SKIN );
88
+ "system-images;" + AndroidBuild .TARGET_PLATFORM + ";" +
89
+ SysImageDownloader .SYSTEM_IMAGE_TAG + ";x86" , SysImageDownloader .SYSTEM_IMAGE_TAG );
92
90
// "Google Inc.:Google APIs:" + AndroidBuild.sdkVersion);
93
91
94
92
/** Default virtual wear device used by Processing. */
95
93
static public final AVD wearAVD =
96
94
new AVD ("Processing-Wear-0" + Base .getRevision (),
97
- AndroidBuild .TARGET_PLATFORM , SysImageDownloader .SYSTEM_IMAGE_WEAR_TAG , WEAR_SKIN );
95
+ "system-images;" + AndroidBuild .TARGET_PLATFORM + ";" +
96
+ SysImageDownloader .SYSTEM_IMAGE_WEAR_TAG + ";x86" , SysImageDownloader .SYSTEM_IMAGE_WEAR_TAG );
98
97
99
- public AVD (final String name , final String target ,
100
- final String tag , final String skin ) {
98
+ public AVD (final String name , final String sdkId ,
99
+ final String tag ) {
101
100
this .name = name ;
102
- this .target = target ;
103
- this .skin = skin ;
104
- initializeAbiList (tag );
101
+ this .sdkId = sdkId ;
102
+ //initializeAbiList(tag);
105
103
}
106
104
107
105
private void initializeAbiList (String tag ) {
@@ -121,11 +119,22 @@ static protected void list(final AndroidSDK sdk) throws IOException {
121
119
try {
122
120
avdList = new ArrayList <String >();
123
121
badList = new ArrayList <String >();
124
- ProcessResult listResult =
125
- new ProcessHelper (sdk .getAndroidToolPath (), "list" , "avds" ).execute ();
126
- if (listResult .succeeded ()) {
122
+ ProcessBuilder pb =
123
+ new ProcessBuilder (sdk .getAvdManagerPath (), "list" , "avd" );
124
+ Map <String , String > env = pb .environment ();
125
+ env .clear ();
126
+ env .put ("JAVA_HOME" , Platform .getJavaHome ().getCanonicalPath ());
127
+ pb .redirectErrorStream (true );
128
+
129
+ process = pb .start ();
130
+ InputStream stdout = process .getInputStream ();
131
+ BufferedReader reader = new BufferedReader (new InputStreamReader (stdout ));
132
+ process .waitFor ();
133
+
134
+ if (process .exitValue () == 0 ) {
127
135
boolean badness = false ;
128
- for (String line : listResult ) {
136
+ String line ;
137
+ while ((line = reader .readLine ()) != null ) {
129
138
String [] m = PApplet .match (line , "\\ s+Name\\ :\\ s+(\\ S+)" );
130
139
if (m != null ) {
131
140
if (!badness ) {
@@ -149,9 +158,14 @@ static protected void list(final AndroidSDK sdk) throws IOException {
149
158
}
150
159
} else {
151
160
System .err .println ("Unhappy inside exists()" );
152
- System .err .println (listResult );
161
+ String line ;
162
+ while ((line = reader .readLine ()) != null )
163
+ System .err .println (line );
153
164
}
154
165
} catch (final InterruptedException ie ) { }
166
+ finally {
167
+ process .destroy ();
168
+ }
155
169
}
156
170
157
171
@@ -188,18 +202,24 @@ protected boolean badness() {
188
202
189
203
protected void initTargets (final AndroidSDK sdk ) throws IOException {
190
204
preferredAbi .clear ();
191
- final String [] list_abi = {
192
- sdk .getAndroidToolPath (),
193
- "list" , "targets"
194
- };
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 ));
195
215
196
- ProcessHelper p = new ProcessHelper (list_abi );
197
216
try {
198
- final ProcessResult abiListResult = p . execute ();
217
+ process . waitFor ();
199
218
200
219
String api = null ;
201
220
String [] abis = null ;
202
- for (String line : abiListResult ) {
221
+ String line ;
222
+ while ((line = reader .readLine ()) != null ) {
203
223
line = line .trim ();
204
224
if (line .equals ("" )) continue ;
205
225
@@ -236,6 +256,8 @@ protected void initTargets(final AndroidSDK sdk) throws IOException {
236
256
}
237
257
}
238
258
} catch (InterruptedException e ) {
259
+ } finally {
260
+ process .destroy ();
239
261
}
240
262
}
241
263
@@ -248,43 +270,67 @@ protected boolean noTargets(final AndroidSDK sdk) throws IOException {
248
270
249
271
250
272
protected boolean create (final AndroidSDK sdk ) throws IOException {
251
- initTargets (sdk );
273
+ // initTargets(sdk);
252
274
253
- final String [] params = {
254
- sdk .getAndroidToolPath (),
275
+ ProcessBuilder pb = new ProcessBuilder (
276
+ sdk .getAvdManagerPath (),
255
277
"create" , "avd" ,
256
278
"-n" , name ,
257
- "-t" , target ,
258
- "-c" , DEFAULT_SDCARD_SIZE ,
259
- "-s" , skin ,
260
- "--abi" , preferredAbi .get (AndroidBuild .TARGET_SDK )
261
- };
262
-
263
- // sdk/tools/android create avd -n "Wear-Processing-0254" -t android-23 -c 64M -s AndroidWearSquare --abi android-wear/x86
279
+ "-k" , sdkId ,
280
+ "-c" , DEFAULT_SDCARD_SIZE
281
+ );
264
282
283
+ // avdmanager create avd -n "Wear-Processing-0254" -k "system-images;android-25;google_apis;x86" -c 64M
284
+
265
285
// Set the list to null so that exists() will check again
266
286
avdList = null ;
267
-
268
- ProcessHelper p = new ProcessHelper (params );
287
+
288
+ Map <String , String > env = pb .environment ();
289
+ env .clear ();
290
+ env .put ("JAVA_HOME" , Platform .getJavaHome ().getCanonicalPath ());
291
+ pb .redirectErrorStream (true );
292
+
269
293
try {
294
+ process = pb .start ();
295
+
296
+ InputStream stdout = process .getInputStream ();
297
+ BufferedReader reader = new BufferedReader (new InputStreamReader (stdout ));
298
+
270
299
// Passes 'no' to "Do you wish to create a custom hardware profile [no]"
300
+ OutputStream os = process .getOutputStream ();
301
+ PrintWriter pw = new PrintWriter (new OutputStreamWriter (os ));
302
+ pw .println ("no" );
303
+ pw .flush ();
304
+ pw .close ();
305
+ os .flush ();
306
+ os .close ();
271
307
272
- final ProcessResult createAvdResult = p .execute ("no" );
273
- if (createAvdResult .succeeded ()) {
308
+ process .waitFor ();
309
+
310
+ if (process .exitValue () == 0 ) {
274
311
return true ;
275
312
}
276
- if (createAvdResult .toString ().contains ("Target id is not valid" )) {
313
+
314
+ String line ;
315
+ StringBuilder output = new StringBuilder ();
316
+ while ((line = reader .readLine ()) != null ) {
317
+ output .append (line );
318
+ }
319
+ if (output .toString ().contains ("Package path is not valid" )) {
277
320
// They didn't install the Google APIs
278
- AndroidUtil .showMessage (AVD_TARGET_TITLE , AVD_TARGET_MESSAGE );
321
+ //AndroidUtil.showMessage(AVD_TARGET_TITLE, AVD_TARGET_MESSAGE);
322
+ invalidPackage = true ;
279
323
} else {
280
324
// Just generally not working
281
325
AndroidUtil .showMessage (AVD_CREATE_TITLE ,
282
326
String .format (AVD_CREATE_MESSAGE , AndroidBuild .TARGET_SDK ));
283
- System .out .println (createAvdResult );
284
327
}
328
+ System .out .println (output .toString ());
285
329
//System.err.println(createAvdResult);
286
330
} catch (final InterruptedException ie ) {
287
331
ie .printStackTrace ();
332
+ } finally {
333
+ process .destroy ();
288
334
}
289
335
290
336
return false ;
@@ -309,15 +355,20 @@ static public boolean ensureProperAVD(final Frame window, final AndroidMode mode
309
355
AndroidUtil .showMessage (AVD_LOAD_TITLE , AVD_LOAD_MESSAGE );
310
356
return false ;
311
357
}
312
- if (wearAVD .noTargets (sdk )) {
313
- boolean res = AndroidSDK .locateSysImage (window , mode , true );
314
- if (!res ) {
315
- return false ;
316
- }
317
- }
318
358
if (wearAVD .create (sdk )) {
319
359
return true ;
320
- }
360
+ }
361
+ if (invalidPackage ) {
362
+ boolean res = AndroidSDK .locateSysImage (window , mode , true );
363
+ if (!res ) {
364
+ return false ;
365
+ } else {
366
+ // Try again
367
+ if (wearAVD .create (sdk )) {
368
+ return true ;
369
+ }
370
+ }
371
+ }
321
372
} else {
322
373
if (mobileAVD .exists (sdk )) {
323
374
return true ;
@@ -326,14 +377,19 @@ static public boolean ensureProperAVD(final Frame window, final AndroidMode mode
326
377
AndroidUtil .showMessage (AVD_LOAD_TITLE , AVD_LOAD_MESSAGE );
327
378
return false ;
328
379
}
329
- if (mobileAVD .noTargets (sdk )) {
380
+ if (mobileAVD .create (sdk )) {
381
+ return true ;
382
+ }
383
+ if (invalidPackage ) {
330
384
boolean res = AndroidSDK .locateSysImage (window , mode , false );
331
385
if (!res ) {
332
- return false ;
386
+ return false ;
387
+ } else {
388
+ // Try again
389
+ if (mobileAVD .create (sdk )) {
390
+ return true ;
391
+ }
333
392
}
334
- }
335
- if (mobileAVD .create (sdk )) {
336
- return true ;
337
393
}
338
394
}
339
395
} catch (final Exception e ) {
0 commit comments