forked from processing/processing-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidSDK.java
More file actions
910 lines (771 loc) · 34 KB
/
Copy pathAndroidSDK.java
File metadata and controls
910 lines (771 loc) · 34 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2013-17 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.Messages;
import processing.app.Platform;
import processing.app.Preferences;
import processing.app.exec.ProcessHelper;
import processing.app.exec.ProcessResult;
import processing.app.ui.Toolkit;
import processing.core.PApplet;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
/**
* Class holding all needed references (path, tools, etc) to the SDK used by
* the mode.
*/
class AndroidSDK {
public static boolean adbDisabled = false;
final static private int FONT_SIZE = Toolkit.zoom(11);
final static private int TEXT_MARGIN = Toolkit.zoom(8);
final static private int TEXT_WIDTH = Toolkit.zoom(300);
private final File folder;
private final File tools;
private final File platforms;
private final File targetPlatform;
private final File androidJar;
private final File platformTools;
private final File buildTools;
private final File avdManager;
private final File sdkManager;
private final File wearablePath;
private final File supportLibPath;
private static final String SDK_DOWNLOAD_URL =
"https://developer.android.com/studio/index.html#downloads";
private static final String USE_ENV_SDK_TITLE = "Found an Android SDK!";
private static final String USE_ENV_SDK_MESSAGE =
"Processing found a valid Android SDK that seems to be in use already. " +
"Processing could use this SDK too, or download a new one.<br><br>" +
"Sharing the same SDK across different development tools, like Processing " +
"and Android Studio, will save space (the SDK may use up to several GBs), " +
"but when one tool updates the SDK, it can create problems in the other. " +
"If Processing downloads a new SDK, it will keep it separate from the one " +
"it just found.<br><br>" +
"What do you want to do?";
private static final String MISSING_SDK_TITLE =
"Cannot find an Android SDK...";
private static final String MISSING_SDK_MESSAGE =
"Processing did not find an Android SDK on this computer. " +
"If there is one, and you know where it is, click \"Locate SDK path\" " +
"to select it, or \"Download SDK\" to let Processing download the SDK automatically.<br><br>" +
"If you want to download the SDK manually, you can get "+
"the command line tools from <a href=\"" + SDK_DOWNLOAD_URL + "\">here</a>. " +
"Make sure to install the SDK platform for API " + AndroidBuild.TARGET_SDK + ".";
private static final String INVALID_SDK_TITLE =
"Android SDK is not valid...";
private static final String INVALID_SDK_MESSAGE =
"Processing found an Android SDK, but is not valid. It could be missing " +
"some files, or might not be including the required platform for " +
"API " + AndroidBuild.TARGET_SDK + ".<br><br>" +
"If a valid SDK is available in a different location, " +
"click \"Locate SDK path\" to select it, or \"Download SDK\" to let " +
"Processing download the SDK automatically.<br><br>" +
"If you want to download the SDK manually, you can get "+
"the command line tools from <a href=\"" + SDK_DOWNLOAD_URL + "\">here</a>. " +
"Make sure to install the SDK platform for API " + AndroidBuild.TARGET_SDK + ".";
private static final String COMMAND_LINE_TUT_URL =
"http://android.processing.org/tutorials/command_line/index.html";
private static final String ANDROID_SYS_IMAGE_PRIMARY =
"Download phone system image?";
private static final String ANDROID_SYS_IMAGE_SECONDARY =
"The system image needed by the emulator does not appear to be installed. " +
"Do you want Processing to download and install it now? <br><br>" +
"Otherwise, you will need to do it through the sdkmanager<br>" +
"command line tool, check <a href=\"" + COMMAND_LINE_TUT_URL +
"\">this online tutorial</a> for more info.";
private static final String ANDROID_SYS_IMAGE_WEAR_PRIMARY =
"Download watch system image?";
private static final String ANDROID_SYS_IMAGE_WEAR_SECONDARY =
"The system image needed by the emulator does not appear to be installed. " +
"Do you want Processing to download and install it now? <br><br>" +
"Otherwise, you will need to do it through the sdkmanager<br>" +
"command line tool, check <a href=\"" + COMMAND_LINE_TUT_URL +
"\">this online tutorial</a> for more info.";
private static final String SELECT_ANDROID_SDK_FOLDER =
"Choose the location of the Android SDK";
private static final String SDK_INSTALL_TITLE = "SDK installed!";
private static final String PROCESSING_FOR_ANDROID_URL =
"http://android.processing.org/";
private static final String WHATS_NEW_URL =
"http://android.processing.org/whatsnew.html";
private static final String DRIVER_INSTALL_URL =
"https://developer.android.com/studio/run/oem-usb.html#InstallingDriver";
private static final String SDK_INSTALL_MESSAGE =
"Processing just downloaded and installed the Android SDK succesfully. " +
"The Android mode is now ready to use!<br><br>" +
"For documentation, examples, and tutorials, " +
"visit the <a href=\"" + PROCESSING_FOR_ANDROID_URL + "\">Processing for Android website</a>, and " +
"if you updated from version 3 of the mode, check the <a href=\"" + WHATS_NEW_URL + "\">what's new page</a>.";
private static final String SDK_EXISTS_TITLE = "SDK configured!";
private static final String SDK_EXISTS_MESSAGE =
"Processing will use the existing Android SDK. " +
"The Android mode is now ready to use!<br><br>" +
"For documentation, examples, and tutorials, " +
"visit the <a href=\"" + PROCESSING_FOR_ANDROID_URL + "\">Processing for Android website</a>, and " +
"if you updated from version 3 of the mode, check the <a href=\"" + WHATS_NEW_URL + "\">what's new page</a>.";
private static final String DRIVER_INSTALL_MESSAGE = "<br><br>" +
"If you are planning to use Google Nexus devices, then you need the " +
"Google USB Driver to connect them to Processing. You will have to " +
"install the driver manually following <a href=\"" + DRIVER_INSTALL_URL +
"\">these instructions</a>.<br><br>" +
"The installation files are available in this folder:</br>";
private static final String SDK_LICENSE_URL =
"https://developer.android.com/studio/terms.html";
private static final String SDK_LICENSE_TITLE = "Accept SDK license?";
private static final String SDK_LICENSE_MESSAGE =
"You need to accept the terms of the Android SDK license from Google in " +
"order to use the SDK. Read the license <a href=\"" + SDK_LICENSE_URL +
"\">from here</a>.";
private static final String NO_SDK_LICENSE_TITLE = "SDK license not accepted";
private static final String NO_SDK_LICENSE_MESSAGE =
"The Android SDK was installed, but will not be usable. You can accept " +
"the license at a later time by opening a terminal, changing to the " +
"SDK folder, and then running the following command:<br><br>" +
"tools/bin/sdkmanager --licenses";
private static final String SYSTEM_32BIT_TITLE = "System is 32 bit...";
private static final String SYSTEM_32BIT_URL =
"https://askubuntu.com/questions/710426/android-sdk-on-ubuntu-32bit";
private static final String SYSTEM_32BIT_MESSAGE =
"The Android SDK no longer includes 32 bit platform tools (adb, etc.), and so they will not work.<br><br>" +
"<a href=\"" + SYSTEM_32BIT_URL + "\">This thread</a> provides some possible workarounds.";
private static final int NO_ERROR = 0;
private static final int SKIP_ENV_SDK = 1;
private static final int MISSING_SDK = 2;
private static final int INVALID_SDK = 3;
private static int loadError = NO_ERROR;
public AndroidSDK(File folder) throws BadSDKException, IOException {
this.folder = folder;
if (!folder.exists()) {
throw new BadSDKException(folder + " does not exist");
}
tools = new File(folder, "tools");
if (!tools.exists()) {
throw new BadSDKException("There is no tools folder in " + folder);
}
platformTools = new File(folder, "platform-tools");
if (!platformTools.exists()) {
throw new BadSDKException("There is no platform-tools folder in " + folder);
}
buildTools = new File(folder, "build-tools");
if (!buildTools.exists()) {
throw new BadSDKException("There is no build-tools folder in " + folder);
}
platforms = new File(folder, "platforms");
if (!platforms.exists()) {
throw new BadSDKException("There is no platforms folder in " + folder);
}
targetPlatform = new File(platforms, AndroidBuild.TARGET_PLATFORM);
if (!targetPlatform.exists()) {
throw new BadSDKException("There is no Android " +
AndroidBuild.TARGET_SDK + " in " + platforms.getAbsolutePath());
}
androidJar = new File(targetPlatform, "android.jar");
if (!androidJar.exists()) {
throw new BadSDKException("android.jar for plaform " +
AndroidBuild.TARGET_SDK + " is missing from " + targetPlatform.getAbsolutePath());
}
wearablePath = new File(folder, "extras/google/m2repository/com/google/android/support/wearable");
if (!wearablePath.exists()) {
throw new BadSDKException("There is no wearable folder in " + folder);
}
supportLibPath = new File(folder, "extras/android/m2repository/com/android/support");
if (!supportLibPath.exists()) {
throw new BadSDKException("There is no support library folder in " + folder);
}
avdManager = findCliTool(new File(tools, "bin"), "avdmanager");
sdkManager = findCliTool(new File(tools, "bin"), "sdkmanager");
String path = Platform.getenv("PATH");
Platform.setenv("ANDROID_SDK", folder.getCanonicalPath());
path = platformTools.getCanonicalPath() + File.pathSeparator +
tools.getCanonicalPath() + File.pathSeparator + path;
String javaHomeProp = System.getProperty("java.home");
File javaHome = new File(javaHomeProp).getCanonicalFile();
Platform.setenv("JAVA_HOME", javaHome.getCanonicalPath());
path = new File(javaHome, "bin").getCanonicalPath() + File.pathSeparator + path;
Platform.setenv("PATH", path);
checkDebugCertificate();
}
/**
* If a debug certificate exists, check its expiration date. If it's expired,
* remove it so that it doesn't cause problems during the build.
*/
protected void checkDebugCertificate() {
File dotAndroidFolder = new File(System.getProperty("user.home"), ".android");
File keystoreFile = new File(dotAndroidFolder, "debug.keystore");
if (keystoreFile.exists()) {
// keytool -list -v -storepass android -keystore debug.keystore
ProcessHelper ph = new ProcessHelper(new String[] {
"keytool", "-list", "-v",
"-storepass", "android",
"-keystore", keystoreFile.getAbsolutePath()
});
try {
ProcessResult result = ph.execute();
if (result.succeeded()) {
// Valid from: Mon Nov 02 15:38:52 EST 2009 until: Tue Nov 02 16:38:52 EDT 2010
String[] lines = PApplet.split(result.getStdout(), '\n');
for (String line : lines) {
String[] m = PApplet.match(line, "Valid from: .* until: (.*)");
if (m != null) {
String timestamp = m[1].trim();
// "Sun Jan 22 11:09:08 EST 2012"
// Hilariously, this is the format of Date.toString(), however
// it isn't the default for SimpleDateFormat or others. Yay!
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
try {
Date date = df.parse(timestamp);
long expireMillis = date.getTime();
if (expireMillis < System.currentTimeMillis()) {
System.out.println("Removing expired debug.keystore file.");
String hidingName = "debug.keystore." + AndroidMode.getDateStamp(expireMillis);
File hidingFile = new File(keystoreFile.getParent(), hidingName);
if (!keystoreFile.renameTo(hidingFile)) {
System.err.println("Could not remove the expired debug.keystore file.");
System.err.println("Please remove the file " + keystoreFile.getAbsolutePath());
}
// } else {
// System.out.println("Nah, that won't expire until " + date); //timestamp);
}
} catch (ParseException pe) {
System.err.println("The date “" + timestamp + "� could not be parsed.");
System.err.println("Please report this as a bug so we can fix it.");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public File getToolsFolder() {
return tools;
}
public String getAvdManagerPath() {
return avdManager.getAbsolutePath();
}
public File getSdkFolder() {
return folder;
}
public File getTargetPlatform() {
return targetPlatform;
}
public File getAndroidJarPath() {
return androidJar;
}
public File getBuildToolsFolder() {
return buildTools;
}
public File getPlatformToolsFolder() {
return platformTools;
}
public File getWearableFolder() {
return wearablePath;
}
public File getSupportLibrary() {
return supportLibPath;
}
public File getZipAlignTool() {
File[] files = buildTools.listFiles();
String name = Platform.isWindows() ? "zipalign.exe" : "zipalign";
for (File f: files) {
File z = new File(f, name);
if (z.exists()) return z;
}
return null;
}
// Write to the process input, so the licenses will be accepted. In
// principle, We only need 7 'y', one for the 'yes' to the first
// 'review licenses?' question, the rest for the 6 licenses, but adding
// 10 just in case, having more does not cause any trouble.
private static final String response = "y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n";
private void acceptLicenses() {
ProcessBuilder pb = new ProcessBuilder(sdkManager.getAbsolutePath(),
"--licenses");
pb.redirectErrorStream(true);
try {
Process process = pb.start();
final OutputStream os = process.getOutputStream();
final InputStream is = process.getInputStream();
// Read the process output, otherwise read() will block and wait for new
// data to read
new Thread(new Runnable() {
public void run() {
byte[] b = new byte[1024];
try {
while (is.read(b) != -1) { }
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}, "AndroidSDK: reading licenses").start();
Thread.sleep(1000);
os.write(response.getBytes());
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static public File getHAXMInstallerFolder() {
String sdkPrefsPath = Preferences.get("android.sdk.path");
File sdkPath = new File(sdkPrefsPath);
return new File(sdkPath, "extras/intel/HAXM");
}
static public File getGoogleDriverFolder() {
String sdkPrefsPath = Preferences.get("android.sdk.path");
File sdkPath = new File(sdkPrefsPath);
return new File(sdkPath, "extras/google/usb_driver");
}
/**
* Checks a path to see if there's a tools/android file inside, a rough check
* for the SDK installation. Also figures out the name of android/android.bat
* so that it can be called explicitly.
*/
private static File findCliTool(final File tools, String name)
throws BadSDKException {
if (new File(tools, name + ".bat").exists()) {
return new File(tools, name + ".bat");
}
if (new File(tools, name).exists()) {
return new File(tools, name);
}
throw new BadSDKException("Cannot find " + name + " in " + tools);
}
/**
* Check for a set android.sdk.path preference. If the pref
* is set, and refers to a legitimate Android SDK, then use that.
*
* Check for the ANDROID_SDK environment variable. If the variable is set,
* and refers to a legitimate Android SDK, then use that and save the pref.
*
* Prompt the user to select an Android SDK. If the user selects a
* legitimate Android SDK, then use that, and save the preference.
*
* @return an AndroidSDK
* @throws BadSDKException
* @throws IOException
*/
public static AndroidSDK load(boolean checkEnvSDK, Frame editor) throws IOException {
loadError = NO_ERROR;
// Give priority to preferences:
// https://github.com/processing/processing-android/issues/372
final String sdkPrefsPath = Preferences.get("android.sdk.path");
if (sdkPrefsPath != null && !sdkPrefsPath.equals("")) {
try {
final AndroidSDK androidSDK = new AndroidSDK(new File(sdkPrefsPath));
Preferences.set("android.sdk.path", sdkPrefsPath);
return androidSDK;
} catch (final BadSDKException badPref) {
Preferences.unset("android.sdk.path");
loadError = INVALID_SDK;
}
}
final String sdkEnvPath = Platform.getenv("ANDROID_SDK");
if (sdkEnvPath != null && !sdkEnvPath.equals("")) {
try {
final AndroidSDK androidSDK = new AndroidSDK(new File(sdkEnvPath));
if (checkEnvSDK && editor != null) {
// There is a valid SDK in the environment, but let's give the user
// the option to not to use it. After this, we should go straight to
// download a new SDK.
int result = showEnvSDKDialog(editor);
if (result != JOptionPane.YES_OPTION) {
loadError = SKIP_ENV_SDK;
return null;
}
}
// Set this value in preferences.txt, in case ANDROID_SDK
// gets knocked out later. For instance, by that pesky Eclipse,
// which nukes all env variables when launching from the IDE.
Preferences.set("android.sdk.path", sdkEnvPath);
// If we are here, it means that there was no SDK path in the preferences
// and the user wants to use the SDK found in the environment. This
// means we just installed the mode for the first time, so we show a
// welcome message with some useful info.
AndroidUtil.showMessage(SDK_EXISTS_TITLE, SDK_EXISTS_MESSAGE);
return androidSDK;
} catch (final BadSDKException badEnv) {
Preferences.unset("android.sdk.path");
loadError = INVALID_SDK;
}
} else if (loadError == NO_ERROR) {
loadError = MISSING_SDK;
}
return null;
}
static public AndroidSDK locate(final Frame window, final AndroidMode androidMode)
throws BadSDKException, CancelException, IOException {
if (loadError == SKIP_ENV_SDK) {
// The user does not want to use the environment SDK, so let's simply
// download a new one to the sketchbook folder.
return download(window, androidMode);
}
// At this point, there is no ANDROID_SDK env variable, no SDK in the preferences,
// or either one was invalid, so we will continue by asking the user to either locate
// a valid SDK manually, or download a new one.
int result = showLocateDialog(window);
if (result == JOptionPane.YES_OPTION) {
return download(window, androidMode);
} else if (result == JOptionPane.NO_OPTION) {
// User will manually select folder containing SDK folder
File folder = selectFolder(SELECT_ANDROID_SDK_FOLDER, null, window);
if (folder == null) {
throw new CancelException("User canceled attempt to find SDK");
} else {
final AndroidSDK androidSDK = new AndroidSDK(folder);
Preferences.set("android.sdk.path", folder.getAbsolutePath());
return androidSDK;
}
} else {
throw new CancelException("User canceled attempt to find SDK");
}
}
static public boolean locateSysImage(final Frame window,
final AndroidMode androidMode, final boolean wear, final boolean ask)
throws BadSDKException, CancelException, IOException {
final int result = showDownloadSysImageDialog(window, wear);
if (result == JOptionPane.YES_OPTION) {
return downloadSysImage(window, androidMode, wear, ask);
} else if (result == JOptionPane.NO_OPTION) {
return false;
} else {
return false;
}
}
static public AndroidSDK download(final Frame editor, final AndroidMode androidMode)
throws BadSDKException, CancelException {
final SDKDownloader downloader = new SDKDownloader(editor);
downloader.run(); // This call blocks until the SDK download complete, or user cancels.
if (downloader.cancelled()) {
throw new CancelException("User canceled SDK download");
}
AndroidSDK sdk = downloader.getSDK();
if (sdk == null) {
throw new BadSDKException("SDK could not be downloaded");
}
final int result = showSDKLicenseDialog(editor);
if (result == JOptionPane.YES_OPTION) {
sdk.acceptLicenses();
String msg = SDK_INSTALL_MESSAGE;
File driver = AndroidSDK.getGoogleDriverFolder();
if (Platform.isWindows() && driver.exists()) {
msg += DRIVER_INSTALL_MESSAGE + driver.getAbsolutePath();
}
AndroidUtil.showMessage(SDK_INSTALL_TITLE, msg);
} else {
AndroidUtil.showMessage(NO_SDK_LICENSE_TITLE, NO_SDK_LICENSE_MESSAGE);
}
if (Platform.isLinux() && Platform.getNativeBits() == 32) {
AndroidUtil.showMessage(SYSTEM_32BIT_TITLE, SYSTEM_32BIT_MESSAGE);
}
return sdk;
}
static public boolean downloadSysImage(final Frame editor,
final AndroidMode androidMode, final boolean wear, final boolean ask)
throws BadSDKException, CancelException {
final SysImageDownloader downloader = new SysImageDownloader(editor, wear, ask);
downloader.run(); // This call blocks until the SDK download complete, or user cancels.
if (downloader.cancelled()) {
throw new CancelException("User canceled emulator download");
}
boolean res = downloader.getResult();
if (!res) {
throw new BadSDKException("Emulator could not be downloaded");
}
return res;
}
static public int showEnvSDKDialog(Frame editor) {
String title = USE_ENV_SDK_TITLE;
String htmlString = "<html> " +
"<head> <style type=\"text/css\">" +
"p { font: " + FONT_SIZE + "pt \"Lucida Grande\"; " +
"margin: " + TEXT_MARGIN + "px; " +
"width: " + TEXT_WIDTH + "px }" +
"</style> </head>" +
"<body> <p>" + USE_ENV_SDK_MESSAGE + "</p> </body> </html>";
JEditorPane pane = new JEditorPane("text/html", htmlString);
pane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
Platform.openURL(e.getURL().toString());
}
}
});
pane.setEditable(false);
JLabel label = new JLabel();
pane.setBackground(label.getBackground());
String[] options = new String[] { "Use existing SDK", "Download new SDK" };
int result = JOptionPane.showOptionDialog(null, pane, title,
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
if (result == JOptionPane.YES_OPTION) {
return JOptionPane.YES_OPTION;
} else if (result == JOptionPane.NO_OPTION) {
return JOptionPane.NO_OPTION;
} else {
return JOptionPane.CLOSED_OPTION;
}
}
static public int showLocateDialog(Frame editor) {
// How to show a option dialog containing clickable links:
// http://stackoverflow.com/questions/8348063/clickable-links-in-joptionpane
String htmlString = "<html> " +
"<head> <style type=\"text/css\">" +
"p { font: " + FONT_SIZE + "pt \"Lucida Grande\"; " +
"margin: " + TEXT_MARGIN + "px; " +
"width: " + TEXT_WIDTH + "px }" +
"</style> </head>";
String title = "";
if (loadError == MISSING_SDK) {
htmlString += "<body> <p>" + MISSING_SDK_MESSAGE + "</p> </body> </html>";
title = MISSING_SDK_TITLE;
} else if (loadError == INVALID_SDK) {
htmlString += "<body> <p>" + INVALID_SDK_MESSAGE + "</p> </body> </html>";
title = INVALID_SDK_TITLE;
}
JEditorPane pane = new JEditorPane("text/html", htmlString);
pane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
Platform.openURL(e.getURL().toString());
}
}
});
pane.setEditable(false);
JLabel label = new JLabel();
pane.setBackground(label.getBackground());
String[] options = new String[] {
"Download SDK automatically", "Locate SDK path manually"
};
int result = JOptionPane.showOptionDialog(null, pane, title,
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
if (result == JOptionPane.YES_OPTION) {
return JOptionPane.YES_OPTION;
} else if (result == JOptionPane.NO_OPTION) {
return JOptionPane.NO_OPTION;
} else {
return JOptionPane.CLOSED_OPTION;
}
}
static public int showDownloadSysImageDialog(Frame editor, boolean wear) {
String title = wear ? ANDROID_SYS_IMAGE_WEAR_PRIMARY : ANDROID_SYS_IMAGE_PRIMARY;
String msg = wear ? ANDROID_SYS_IMAGE_WEAR_SECONDARY : ANDROID_SYS_IMAGE_SECONDARY;
String htmlString = "<html> " +
"<head> <style type=\"text/css\">"+
"p { font: " + FONT_SIZE + "pt \"Lucida Grande\"; " +
"margin: " + TEXT_MARGIN + "px; " +
"width: " + TEXT_WIDTH + "px }" +
"</style> </head>" + "<body> <p>" + msg + "</p> </body> </html>";
JEditorPane pane = new JEditorPane("text/html", htmlString);
pane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
Platform.openURL(e.getURL().toString());
}
}
});
pane.setEditable(false);
JLabel label = new JLabel();
pane.setBackground(label.getBackground());
String[] options = new String[] { "Yes", "No" };
int result = JOptionPane.showOptionDialog(null, pane, title,
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
if (result == JOptionPane.YES_OPTION) {
return JOptionPane.YES_OPTION;
} else if (result == JOptionPane.NO_OPTION) {
return JOptionPane.NO_OPTION;
} else {
return JOptionPane.CLOSED_OPTION;
}
}
static public int showSDKLicenseDialog(Frame editor) {
String title = SDK_LICENSE_TITLE;
String msg = SDK_LICENSE_MESSAGE;
String htmlString = "<html> " +
"<head> <style type=\"text/css\">"+
"p { font: " + FONT_SIZE + "pt \"Lucida Grande\"; " +
"margin: " + TEXT_MARGIN + "px; " +
"width: " + TEXT_WIDTH + "px }" +
"</style> </head>" + "<body> <p>" + msg + "</p> </body> </html>";
JEditorPane pane = new JEditorPane("text/html", htmlString);
pane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
Platform.openURL(e.getURL().toString());
}
}
});
pane.setEditable(false);
JLabel label = new JLabel();
pane.setBackground(label.getBackground());
String[] options = new String[] { "Yes", "No" };
int result = JOptionPane.showOptionDialog(null, pane, title,
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
if (result == JOptionPane.YES_OPTION) {
return JOptionPane.YES_OPTION;
} else if (result == JOptionPane.NO_OPTION) {
return JOptionPane.NO_OPTION;
} else {
return JOptionPane.CLOSED_OPTION;
}
}
// this was banished from Base because it encourages bad practice.
// TODO figure out a better way to handle the above.
static public File selectFolder(String prompt, File folder, Frame frame) {
if (Platform.isMacOS()) {
if (frame == null) frame = new Frame(); //.pack();
FileDialog fd = new FileDialog(frame, prompt, FileDialog.LOAD);
if (folder != null) {
fd.setDirectory(folder.getParent());
//fd.setFile(folder.getName());
}
System.setProperty("apple.awt.fileDialogForDirectories", "true");
fd.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
fd.setVisible(true);
System.setProperty("apple.awt.fileDialogForDirectories", "false");
if (fd.getFile() == null) {
return null;
}
return new File(fd.getDirectory(), fd.getFile());
} else {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(prompt);
if (folder != null) {
fc.setSelectedFile(folder);
}
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returned = fc.showOpenDialog(frame);
if (returned == JFileChooser.APPROVE_OPTION) {
return fc.getSelectedFile();
}
}
return null;
}
private static final String ADB_DAEMON_MSG_1 = "daemon not running";
private static final String ADB_DAEMON_MSG_2 = "daemon started successfully";
public ProcessResult runADB(final String... cmd)
throws InterruptedException, IOException {
if (adbDisabled) {
throw new IOException("adb is currently disabled");
}
final String[] adbCmd;
if (!cmd[0].contains("adb")) {
File abdPath = Platform.isWindows() ? new File(platformTools, "adb.exe") :
new File(platformTools, "adb");
adbCmd = PApplet.splice(cmd, abdPath.getCanonicalPath(), 0);
} else {
adbCmd = cmd;
}
// printing this here to see if anyone else is killing the adb server
if (processing.app.Base.DEBUG) {
PApplet.printArray(adbCmd);
}
try {
ProcessResult adbResult = new ProcessHelper(adbCmd).execute();
// Ignore messages about starting up an adb daemon
String out = adbResult.getStdout();
if (out.contains(ADB_DAEMON_MSG_1) && out.contains(ADB_DAEMON_MSG_2)) {
StringBuilder sb = new StringBuilder();
for (String line : out.split("\n")) {
if (!out.contains(ADB_DAEMON_MSG_1) &&
!out.contains(ADB_DAEMON_MSG_2)) {
sb.append(line).append("\n");
}
}
return new ProcessResult(adbResult.getCmd(),
adbResult.getResult(),
sb.toString(),
adbResult.getStderr(),
adbResult.getTime());
}
return adbResult;
} catch (IOException ioe) {
if (-1 < ioe.getMessage().indexOf("Permission denied")) {
Messages.showWarning("Trouble with adb!",
"Could not run the adb tool from the Android SDK.\n" +
"One possibility is that its executable permission\n" +
"is not properly set. You can try setting this\n" +
"permission manually, or re-installing the SDK.\n\n" +
"The mode will be disabled until this problem is fixed.\n");
adbDisabled = true;
}
throw ioe;
}
}
static class SDKTarget {
public int version = 0;
public String name;
}
public ArrayList<SDKTarget> getAvailableSdkTargets() throws IOException {
ArrayList<SDKTarget> targets = new ArrayList<SDKTarget>();
for(File platform : platforms.listFiles()) {
File propFile = new File(platform, "build.prop");
if (!propFile.exists()) continue;
SDKTarget target = new SDKTarget();
BufferedReader br = new BufferedReader(new FileReader(propFile));
String line;
while ((line = br.readLine()) != null) {
String[] lineData = line.split("=");
if (lineData[0].equals("ro.build.version.sdk")) {
target.version = Integer.valueOf(lineData[1]);
}
if (lineData[0].equals("ro.build.version.release")) {
target.name = lineData[1];
break;
}
}
br.close();
if (target.version != 0 && target.name != null) targets.add(target);
}
return targets;
}
@SuppressWarnings("serial")
static public class BadSDKException extends Exception {
public BadSDKException(final String message) {
super(message);
}
}
@SuppressWarnings("serial")
static public class CancelException extends Exception {
public CancelException(final String message) {
super(message);
}
}
}