23
23
package processing .mode .android ;
24
24
25
25
import processing .app .Base ;
26
- import processing .app .Messages ;
27
26
import processing .app .Mode ;
28
27
import processing .app .Platform ;
29
28
import processing .app .Preferences ;
30
29
import processing .app .Settings ;
31
30
import processing .app .SketchException ;
32
- import processing .app .Util ;
33
- import processing .app .ui .Editor ;
31
+ import processing .app .tools .Tool ;
34
32
import processing .app .ui .EditorException ;
35
33
import processing .app .ui .EditorState ;
36
34
import processing .app .ui .EditorToolbar ;
46
44
import java .awt .event .ActionListener ;
47
45
import java .io .File ;
48
46
import java .io .IOException ;
49
- import java .lang .reflect .Constructor ;
50
- import java .lang .reflect .InvocationTargetException ;
51
- import java .net .MalformedURLException ;
52
- import java .net .URL ;
53
- import java .net .URLClassLoader ;
47
+ import java .util .ArrayList ;
48
+ import java .util .Collections ;
49
+ import java .util .List ;
54
50
import java .util .TimerTask ;
55
51
56
52
57
53
@ SuppressWarnings ("serial" )
58
54
public class AndroidEditor extends JavaEditor {
59
55
// Component selected by default
60
56
static public final String DEFAULT_COMPONENT = "app" ;
57
+
58
+ private JMenu androidMenu ;
61
59
62
60
private int appComponent ;
63
61
64
62
private Settings settings ;
65
63
private AndroidMode androidMode ;
66
64
65
+ private List <AndroidTool > androidTools ;
66
+
67
67
private java .util .Timer updateDevicesTimer ;
68
68
69
69
private JCheckBoxMenuItem fragmentItem ;
@@ -89,7 +89,10 @@ protected AndroidEditor(Base base, String path, EditorState state,
89
89
androidMode .resetUserSelection ();
90
90
androidMode .checkSDK (this );
91
91
92
- loadModeSettings ();
92
+ androidTools = loadAndroidTools ();
93
+ addToolsToMenu ();
94
+
95
+ loadModeSettings ();
93
96
}
94
97
95
98
@ Override
@@ -258,7 +261,7 @@ public void actionPerformed(ActionEvent e) {
258
261
259
262
260
263
public JMenu buildModeMenu () {
261
- JMenu menu = new JMenu ("Android" );
264
+ androidMenu = new JMenu ("Android" );
262
265
JMenuItem item ;
263
266
264
267
item = new JMenuItem ("Sketch Permissions" );
@@ -267,9 +270,9 @@ public void actionPerformed(ActionEvent e) {
267
270
new Permissions (sketch , appComponent , androidMode .getFolder ());
268
271
}
269
272
});
270
- menu .add (item );
273
+ androidMenu .add (item );
271
274
272
- menu .addSeparator ();
275
+ androidMenu .addSeparator ();
273
276
274
277
fragmentItem = new JCheckBoxMenuItem ("App" );
275
278
wallpaperItem = new JCheckBoxMenuItem ("Wallpaper" );
@@ -322,19 +325,19 @@ public void actionPerformed(ActionEvent e) {
322
325
watchfaceItem .setSelected (false );
323
326
vrItem .setSelected (false );
324
327
325
- menu .add (fragmentItem );
326
- menu .add (wallpaperItem );
327
- menu .add (watchfaceItem );
328
- menu .add (vrItem );
328
+ androidMenu .add (fragmentItem );
329
+ androidMenu .add (wallpaperItem );
330
+ androidMenu .add (watchfaceItem );
331
+ androidMenu .add (vrItem );
329
332
330
- menu .addSeparator ();
333
+ androidMenu .addSeparator ();
331
334
332
335
final JMenu mobDeveMenu = new JMenu ("Devices" );
333
336
334
337
JMenuItem noMobDevItem = new JMenuItem ("No connected devices" );
335
338
noMobDevItem .setEnabled (false );
336
339
mobDeveMenu .add (noMobDevItem );
337
- menu .add (mobDeveMenu );
340
+ androidMenu .add (mobDeveMenu );
338
341
339
342
// start updating device menus
340
343
UpdateDeviceListTask task = new UpdateDeviceListTask (mobDeveMenu );
@@ -345,94 +348,9 @@ public void actionPerformed(ActionEvent e) {
345
348
}
346
349
updateDevicesTimer .schedule (task , 5000 , 5000 );
347
350
348
- menu .addSeparator ();
349
-
350
- item = new JMenuItem ("Update SDK" );
351
- item .addActionListener (new ActionListener () {
352
- public void actionPerformed (ActionEvent e ) {
353
-
354
- File toolPath = new File (androidMode .getFolder (), "tools/SDKUpdater" );
355
- AndroidTool tool = null ;
356
- try {
357
- tool = new AndroidTool (toolPath , androidMode .getSDK ());
358
- } catch (Throwable e1 ) {
359
- // TODO Auto-generated catch block
360
- e1 .printStackTrace ();
361
- }
362
- if (tool != null ) {
363
- tool .init (base );
364
- tool .run ();
365
- }
366
-
367
-
368
- /*
369
- AndroidSDK sdk = androidMode.getSDK();
370
- File lib = new File(sdk.getToolsFolder(), "lib");
371
- File[] archives = Util.listJarFiles(lib);
372
- URL[] urlList = new URL[archives.length];
373
- for (int k = 0; k < archives.length; k++) {
374
- try {
375
- urlList[k] = archives[k].toURI().toURL();
376
- System.out.println(urlList[k]);
377
- } catch (MalformedURLException ex) {}
378
- }
379
-
380
-
381
- ClassLoader parent = androidMode.getClassLoader();
382
- System.out.println("Android mode CLASS loader " + AndroidMode.class.getClassLoader());
383
- System.out.println("Android mode class loader " + parent);
384
- ClassLoader loader = new URLClassLoader(urlList, parent);
385
-
386
-
387
- try {
388
- Class<?> updaterClass = loader.loadClass(SDKUpdater.class.getName());
389
- Constructor con = updaterClass.getConstructor(Editor.class, AndroidMode.class, ClassLoader.class);
390
- SDKUpdater updater = (SDKUpdater) con.newInstance(AndroidEditor.this, androidMode, loader);
391
-
392
- } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
393
- // TODO Auto-generated catch block
394
- e1.printStackTrace();
395
- }
396
-
397
- try {
398
- // This cast should be safe, since the only case when loader is not a
399
- // URLClassLoader is when no archives were found in the first place.
400
- ((URLClassLoader) loader).close();
401
- } catch (IOException e2) {
402
- e2.printStackTrace();
403
- }
404
- */
405
-
406
-
407
- // AndroidSDK sdk = androidMode.getSDK();
408
- // if (sdk != null) {
409
- // new SDKUpdater(sdk.getSdkFolder());
410
- // }
411
-
412
-
413
- }
414
- });
415
- menu .add (item );
416
-
417
- // item = new JMenuItem("AVD Manager");
418
- // item.addActionListener(new ActionListener() {
419
- // public void actionPerformed(ActionEvent e) {
420
- // File file = androidMode.getSDK().getAndroidTool();
421
- // PApplet.exec(new String[] { file.getAbsolutePath(), "avd" });
422
- // }
423
- // });
424
- // menu.add(item);
351
+ androidMenu .addSeparator ();
425
352
426
- item = new JMenuItem ("Reset ADB" );
427
- item .addActionListener (new ActionListener () {
428
- public void actionPerformed (ActionEvent e ) {
429
- // editor.statusNotice("Resetting the Android Debug Bridge server.");
430
- Devices .killAdbServer ();
431
- }
432
- });
433
- menu .add (item );
434
-
435
- return menu ;
353
+ return androidMenu ;
436
354
}
437
355
438
356
@@ -694,6 +612,54 @@ private void loadModeSettings() {
694
612
if (save ) androidMode .initManifest (sketch , appComponent );
695
613
} catch (IOException e ) {
696
614
System .err .println ("While creating " + sketchProps + ": " + e .getMessage ());
697
- }
615
+ }
616
+ }
617
+
618
+ private List <AndroidTool > loadAndroidTools () {
619
+ // This gets called before assigning mode to androidMode...
620
+ ArrayList <AndroidTool > outgoing = new ArrayList <AndroidTool >();
621
+ File toolPath = new File (androidMode .getFolder (), "tools/SDKUpdater" );
622
+ AndroidTool tool = null ;
623
+ try {
624
+ tool = new AndroidTool (toolPath , androidMode .getSDK ());
625
+ tool .init (base );
626
+ outgoing .add (tool );
627
+ } catch (Throwable e ) {
628
+ e .printStackTrace ();
629
+ }
630
+ Collections .sort (outgoing );
631
+ return outgoing ;
698
632
}
633
+
634
+ private void addToolsToMenu () {
635
+ JMenuItem item ;
636
+
637
+ for (final Tool tool : androidTools ) {
638
+ item = new JMenuItem (tool .getMenuTitle ());
639
+ item .addActionListener (new ActionListener () {
640
+ public void actionPerformed (ActionEvent e ) {
641
+ tool .run ();
642
+ }
643
+ });
644
+ androidMenu .add (item );
645
+ }
646
+
647
+ // item = new JMenuItem("AVD Manager");
648
+ // item.addActionListener(new ActionListener() {
649
+ // public void actionPerformed(ActionEvent e) {
650
+ // File file = androidMode.getSDK().getAndroidTool();
651
+ // PApplet.exec(new String[] { file.getAbsolutePath(), "avd" });
652
+ // }
653
+ // });
654
+ // menu.add(item);
655
+
656
+ item = new JMenuItem ("Reset ADB" );
657
+ item .addActionListener (new ActionListener () {
658
+ public void actionPerformed (ActionEvent e ) {
659
+ // editor.statusNotice("Resetting the Android Debug Bridge server.");
660
+ Devices .killAdbServer ();
661
+ }
662
+ });
663
+ androidMenu .add (item );
664
+ }
699
665
}
0 commit comments