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