Skip to content

Commit 95b8514

Browse files
committed
allow user to skip environment SDK
1 parent f3522a4 commit 95b8514

File tree

3 files changed

+103
-52
lines changed

3 files changed

+103
-52
lines changed

src/processing/mode/android/AndroidMode.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,6 @@ protected File getCoreZipLocation() {
181181
return coreZipLocation;
182182
}
183183

184-
185-
// public AndroidSDK loadSDK() throws BadSDKException, IOException {
186-
// if (sdk == null) {
187-
// sdk = AndroidSDK.load();
188-
// }
189-
// return sdk;
190-
// }
191-
192-
public void loadSDK() {
193-
try {
194-
sdk = AndroidSDK.load();
195-
} catch (IOException e) {
196-
e.printStackTrace();
197-
}
198-
}
199-
200184

201185
public void resetUserSelection() {
202186
userCancelledSDKSearch = false;
@@ -220,7 +204,7 @@ public void checkSDK(Editor editor) {
220204
Throwable tr = null;
221205
if (sdk == null) {
222206
try {
223-
sdk = AndroidSDK.load();
207+
sdk = AndroidSDK.load(true, editor);
224208
if (sdk == null) {
225209
sdk = AndroidSDK.locate(editor, this);
226210
}

src/processing/mode/android/AndroidSDK.java

Lines changed: 101 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,35 @@ class AndroidSDK {
6969
private static final String SDK_DOWNLOAD_URL =
7070
"https://developer.android.com/studio/index.html#downloads";
7171

72+
private static final String USE_ENV_SDK_TITLE = "Found an Android SDK!";
73+
private static final String USE_ENV_SDK_MESSAGE =
74+
"Processing found a valid Android SDK that seems to be in use already. " +
75+
"Processing could use this SDK too, or download a new one.<br><br>" +
76+
"Sharing the same SDK across different development tools, like Processing " +
77+
"and Android Studio, will save space (the SDK may use up to several GBs), " +
78+
"but when one tool updates the SDK, it can create problems in the other. " +
79+
"If Processing downloads a new SDK, it will keep it separate from the one " +
80+
"it just found.<br><br>" +
81+
"What do you want to do?";
82+
7283
private static final String MISSING_SDK_TITLE =
73-
"Cannot find the Android SDK...";
74-
84+
"Cannot find an Android SDK...";
7585
private static final String MISSING_SDK_MESSAGE =
76-
"The Android SDK does not appear to be installed, " +
77-
"because the ANDROID_SDK variable is not set. " +
78-
"If it is installed, click \"Locate SDK path\" to select the " +
79-
"location of the SDK, or \"Download SDK\" to let " +
80-
"Processing download the SDK automatically.<br><br>" +
86+
"Processing did not find an Android SDK on this computer. " +
87+
"If there is one, and you know where it is, click \"Locate SDK path\" " +
88+
"to select it, or \"Download SDK\" to let Processing download the SDK automatically.<br><br>" +
8189
"If you want to download the SDK manually, you can get "+
8290
"the command line tools from <a href=\"" + SDK_DOWNLOAD_URL + "\">here</a>. " +
8391
"Make sure to install the SDK platform for API " + AndroidBuild.TARGET_SDK + ".";
8492

8593
private static final String INVALID_SDK_TITLE =
86-
"The Android SDK is not valid...";
87-
94+
"Android SDK is not valid...";
8895
private static final String INVALID_SDK_MESSAGE =
89-
"The found Android SDK cannot be used by Processing. " +
90-
"It could be missing files, or does not include the required platform for " +
91-
"API " + AndroidBuild.TARGET_SDK + ". If a valid SDK is available in a different location, " +
92-
"click \"Locate SDK path\" to select the " +
93-
"location of the valid SDK, or \"Download SDK\" to let " +
96+
"Processing found an Android SDK, but is not valid. It could be missing " +
97+
"some files, or might not be including the required platform for " +
98+
"API " + AndroidBuild.TARGET_SDK + "<br><br>" +
99+
"If a valid SDK is available in a different location, " +
100+
"click \"Locate SDK path\" to select it, or \"Download SDK\" to let " +
94101
"Processing download the SDK automatically.<br><br>" +
95102
"If you want to download the SDK manually, you can get "+
96103
"the command line tools from <a href=\"" + SDK_DOWNLOAD_URL + "\">here</a>. " +
@@ -106,7 +113,8 @@ class AndroidSDK {
106113
"The system image needed by the emulator does not appear to be installed. " +
107114
"Do you want Processing to download and install it now? <br><br>" +
108115
"Otherwise, you will need to do it through the sdkmanager<br>" +
109-
"command line tool, check <a href=\"" + COMMAND_LINE_TUT_URL + "\">this online tutorial</a> for more info.";
116+
"command line tool, check <a href=\"" + COMMAND_LINE_TUT_URL +
117+
"\">this online tutorial</a> for more info.";
110118

111119
private static final String ANDROID_SYS_IMAGE_WEAR_PRIMARY =
112120
"Download watch system image?";
@@ -115,7 +123,8 @@ class AndroidSDK {
115123
"The system image needed by the emulator does not appear to be installed. " +
116124
"Do you want Processing to download and install it now? <br><br>" +
117125
"Otherwise, you will need to do it through the sdkmanager<br>" +
118-
"command line tool, check <a href=\"" + COMMAND_LINE_TUT_URL + "\">this online tutorial</a> for more info.";
126+
"command line tool, check <a href=\"" + COMMAND_LINE_TUT_URL +
127+
"\">this online tutorial</a> for more info.";
119128

120129
private static final String SELECT_ANDROID_SDK_FOLDER =
121130
"Choose the location of the Android SDK";
@@ -129,9 +138,10 @@ class AndroidSDK {
129138
"https://developer.android.com/studio/run/oem-usb.html#InstallingDriver";
130139

131140
private static final String SDK_INSTALL_MESSAGE =
132-
"Processing just downloaded and installed the Android SDK succesfully! " +
133-
"The Android mode is now ready to use, for more information visit " +
134-
"the <a href=\"" + PROCESSING_FOR_ANDROID_URL + "\">Processing for Android website</a>";
141+
"Processing just downloaded and installed the Android SDK succesfully. " +
142+
"The Android mode is now ready to use!<br><br>" +
143+
"For more information (reference, examples, and tutorials) " +
144+
"visit the <a href=\"" + PROCESSING_FOR_ANDROID_URL + "\">Processing for Android website</a>";
135145

136146
private static final String DRIVER_INSTALL_MESSAGE = "<br><br>" +
137147
"If you are planning to use Google Nexus devices, then you need the " +
@@ -140,9 +150,10 @@ class AndroidSDK {
140150
"\">these instructions</a>.<br><br>" +
141151
"The installation files are available in this folder:</br>";
142152

143-
private static final int NO_ERROR = 0;
144-
private static final int MISSING_SDK = 1;
145-
private static final int INVALID_SDK = 2;
153+
private static final int NO_ERROR = 0;
154+
private static final int SKIP_ENV_SDK = 1;
155+
private static final int MISSING_SDK = 2;
156+
private static final int INVALID_SDK = 3;
146157
private static int loadError = NO_ERROR;
147158

148159
public AndroidSDK(File folder) throws BadSDKException, IOException {
@@ -355,19 +366,13 @@ private static File findAvdManager(final File tools) throws BadSDKException {
355366
* @throws BadSDKException
356367
* @throws IOException
357368
*/
358-
public static AndroidSDK load() throws IOException {
369+
public static AndroidSDK load(boolean checkEnvSDK, Frame editor) throws IOException {
359370
loadError = NO_ERROR;
360371

361-
// Give priority to preferences. Rationale: when user runs the mode for the first time
362-
// and there is a valid SDK in the environment, it will be stored in the
363-
// preferences. So in this case, priority is given to an existing (and valid) SDK.
364-
// From that point on, if the preference value is changed then it means that the user
365-
// wants to use a custom SDK, so the mode should not revert back to the global SDK.
366-
// Also, using the global SDK risks breaking the mode as that SDK is most likely
367-
// going to be used by Android Studio, and we don't know what updates it may apply
368-
// that could be incompatible with the mode. So better keep using the local SDK in
369-
// the preferences.
372+
// Give priority to preferences:
373+
// https://github.com/processing/processing-android/issues/372
370374
final String sdkPrefsPath = Preferences.get("android.sdk.path");
375+
System.out.println("Processing preferences: " + sdkPrefsPath);
371376
if (sdkPrefsPath != null) {
372377
try {
373378
final AndroidSDK androidSDK = new AndroidSDK(new File(sdkPrefsPath));
@@ -380,9 +385,24 @@ public static AndroidSDK load() throws IOException {
380385
}
381386

382387
final String sdkEnvPath = Platform.getenv("ANDROID_SDK");
388+
System.out.println("ANDROID_SDK: " + sdkEnvPath);
383389
if (sdkEnvPath != null) {
384390
try {
385391
final AndroidSDK androidSDK = new AndroidSDK(new File(sdkEnvPath));
392+
393+
System.out.println("ENVIRONMENT SDK CAN BE USED BY PROCESSING!!");
394+
395+
if (checkEnvSDK && editor != null) {
396+
// There is a valid SDK in the environment, but let's give the user
397+
// the option to not to use it. After this, we should go straight to
398+
// download a new SDK.
399+
int result = showEnvSDKDialog(editor);
400+
if (result == JOptionPane.NO_OPTION) {
401+
loadError = SKIP_ENV_SDK;
402+
return null;
403+
}
404+
}
405+
386406
// Set this value in preferences.txt, in case ANDROID_SDK
387407
// gets knocked out later. For instance, by that pesky Eclipse,
388408
// which nukes all env variables when launching from the IDE.
@@ -402,11 +422,22 @@ public static AndroidSDK load() throws IOException {
402422

403423
static public AndroidSDK locate(final Frame window, final AndroidMode androidMode)
404424
throws BadSDKException, CancelException, IOException {
405-
final int result = showLocateDialog(window);
425+
426+
if (loadError == SKIP_ENV_SDK) {
427+
// The user does not want to use the environment SDK, so let's simply
428+
// download a new one to the sketchbook folder.
429+
return download(window, androidMode);
430+
}
431+
432+
// At this point, there is no ANDROID_SDK env variable, no SDK in the preferences,
433+
// or either one was invalid, so we will continue by asking the user to either locate
434+
// a valid SDK manually, or download a new one.
435+
int result = showLocateDialog(window);
436+
406437
if (result == JOptionPane.YES_OPTION) {
407438
return download(window, androidMode);
408439
} else if (result == JOptionPane.NO_OPTION) {
409-
// user will manually select folder containing SDK folder
440+
// User will manually select folder containing SDK folder
410441
File folder = selectFolder(SELECT_ANDROID_SDK_FOLDER, null, window);
411442
if (folder == null) {
412443
throw new CancelException("User canceled attempt to find SDK");
@@ -471,7 +502,43 @@ static public boolean downloadSysImage(final Frame editor,
471502
}
472503
return res;
473504
}
505+
474506

507+
static public int showEnvSDKDialog(Frame editor) {
508+
String title = USE_ENV_SDK_TITLE;
509+
String htmlString = "<html> " +
510+
"<head> <style type=\"text/css\">" +
511+
"p { font: " + FONT_SIZE + "pt \"Lucida Grande\"; " +
512+
"margin: " + TEXT_MARGIN + "px; " +
513+
"width: " + TEXT_WIDTH + "px }" +
514+
"</style> </head>" +
515+
"<body> <p>" + USE_ENV_SDK_MESSAGE + "</p> </body> </html>";
516+
JEditorPane pane = new JEditorPane("text/html", htmlString);
517+
pane.addHyperlinkListener(new HyperlinkListener() {
518+
@Override
519+
public void hyperlinkUpdate(HyperlinkEvent e) {
520+
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
521+
Platform.openURL(e.getURL().toString());
522+
}
523+
}
524+
});
525+
pane.setEditable(false);
526+
JLabel label = new JLabel();
527+
pane.setBackground(label.getBackground());
528+
529+
String[] options = new String[] { "Use existing SDK", "Download new SDK" };
530+
int result = JOptionPane.showOptionDialog(null, pane, title,
531+
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
532+
null, options, options[0]);
533+
if (result == JOptionPane.YES_OPTION) {
534+
return JOptionPane.YES_OPTION;
535+
} else if (result == JOptionPane.NO_OPTION) {
536+
return JOptionPane.NO_OPTION;
537+
} else {
538+
return JOptionPane.CLOSED_OPTION;
539+
}
540+
}
541+
475542

476543
static public int showLocateDialog(Frame editor) {
477544
// How to show a option dialog containing clickable links:

src/processing/mode/android/SDKDownloader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ protected Object doInBackground() throws Exception {
189189
// Done, let's set the environment and load the new SDK!
190190
Platform.setenv("ANDROID_SDK", sdkFolder.getAbsolutePath());
191191
Preferences.set("android.sdk.path", sdkFolder.getAbsolutePath());
192-
sdk = AndroidSDK.load();
192+
sdk = AndroidSDK.load(false, null);
193193
} catch (ParserConfigurationException e) {
194194
// TODO Handle exceptions here somehow (ie show error message)
195195
// and handle at least mkdir() results (above)

0 commit comments

Comments
 (0)