Skip to content

Commit 032bb9b

Browse files
committed
switch priority env vs pref
1 parent 2682573 commit 032bb9b

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

src/processing/mode/android/AndroidSDK.java

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ class AndroidSDK {
8383
"Make sure to install the SDK platform for API " + AndroidBuild.TARGET_SDK + ".";
8484

8585
private static final String INVALID_SDK_TITLE =
86-
"Cannot find the required SDK platform version...";
86+
"The Android SDK is not valid...";
8787

8888
private static final String INVALID_SDK_MESSAGE =
89-
"The Android SDK appears to be installed, " +
90-
"however the SDK platform for API " + AndroidBuild.TARGET_SDK +
91-
" was not found. If it is available in a different location, " +
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, " +
9292
"click \"Locate SDK path\" to select the " +
93-
"location of the alternative SDK, or \"Download SDK\" to let " +
93+
"location of the valid SDK, or \"Download SDK\" to let " +
9494
"Processing download the SDK automatically.<br><br>" +
9595
"If you want to download the SDK manually, you can get "+
9696
"the command line tools from <a href=\"" + SDK_DOWNLOAD_URL + "\">here</a>. " +
@@ -137,7 +137,7 @@ class AndroidSDK {
137137
private static final int NO_ERROR = 0;
138138
private static final int MISSING_SDK = 1;
139139
private static final int INVALID_SDK = 2;
140-
private static int SDK_LOAD_ERROR = NO_ERROR;
140+
private static int loadError = NO_ERROR;
141141

142142
public AndroidSDK(File folder) throws BadSDKException, IOException {
143143
this.folder = folder;
@@ -336,12 +336,12 @@ private static File findAvdManager(final File tools) throws BadSDKException {
336336

337337

338338
/**
339+
* Check for a set android.sdk.path preference. If the pref
340+
* is set, and refers to a legitimate Android SDK, then use that.
341+
*
339342
* Check for the ANDROID_SDK environment variable. If the variable is set,
340343
* and refers to a legitimate Android SDK, then use that and save the pref.
341344
*
342-
* Check for a previously set android.sdk.path preference. If the pref
343-
* is set, and refers to a legitimate Android SDK, then use that.
344-
*
345345
* Prompt the user to select an Android SDK. If the user selects a
346346
* legitimate Android SDK, then use that, and save the preference.
347347
*
@@ -350,9 +350,29 @@ private static File findAvdManager(final File tools) throws BadSDKException {
350350
* @throws IOException
351351
*/
352352
public static AndroidSDK load() throws IOException {
353-
SDK_LOAD_ERROR = NO_ERROR;
353+
loadError = NO_ERROR;
354+
355+
// Give priority to preferences. Rationale: when user runs the mode for the first time
356+
// and there is a valid SDK in the environment, it will be stored in the
357+
// preferences. So in this case, priority is given to an existing (and valid) SDK.
358+
// From that point on, if the preference value is changed then it means that the user
359+
// wants to use a custom SDK, so the mode should not revert back to the global SDK.
360+
// Also, using the global SDK risks breaking the mode as that SDK is most likely
361+
// going to be used by Android Studio, and we don't know what updates it may apply
362+
// that could be incompatible with the mode. So better keep using the local SDK in
363+
// the preferences.
364+
final String sdkPrefsPath = Preferences.get("android.sdk.path");
365+
if (sdkPrefsPath != null) {
366+
try {
367+
final AndroidSDK androidSDK = new AndroidSDK(new File(sdkPrefsPath));
368+
Preferences.set("android.sdk.path", sdkPrefsPath);
369+
return androidSDK;
370+
} catch (final BadSDKException badPref) {
371+
Preferences.unset("android.sdk.path");
372+
loadError = INVALID_SDK;
373+
}
374+
}
354375

355-
// The environment variable is king. The preferences.txt entry is a page.
356376
final String sdkEnvPath = Platform.getenv("ANDROID_SDK");
357377
if (sdkEnvPath != null) {
358378
try {
@@ -362,27 +382,14 @@ public static AndroidSDK load() throws IOException {
362382
// which nukes all env variables when launching from the IDE.
363383
Preferences.set("android.sdk.path", sdkEnvPath);
364384
return androidSDK;
365-
} catch (final BadSDKException drop) { }
366-
}
367-
368-
// If android.sdk.path exists as a preference, make sure that the folder
369-
// is not bogus, otherwise the SDK may have been removed or deleted.
370-
final String sdkPrefsPath = Preferences.get("android.sdk.path");
371-
if (sdkPrefsPath != null) {
372-
try {
373-
final AndroidSDK androidSDK = new AndroidSDK(new File(sdkPrefsPath));
374-
// Set this value in preferences.txt, in case ANDROID_SDK
375-
// gets knocked out later. For instance, by that pesky Eclipse,
376-
// which nukes all env variables when launching from the IDE.
377-
Preferences.set("android.sdk.path", sdkPrefsPath);
378-
return androidSDK;
379-
} catch (final BadSDKException wellThatsThat) {
380-
Preferences.unset("android.sdk.path");
381-
SDK_LOAD_ERROR = INVALID_SDK;
385+
} catch (final BadSDKException badEnv) {
386+
Preferences.unset("android.sdk.path");
387+
loadError = INVALID_SDK;
382388
}
383-
} else {
384-
SDK_LOAD_ERROR = MISSING_SDK;
389+
} else if (loadError == NO_ERROR) {
390+
loadError = MISSING_SDK;
385391
}
392+
386393
return null;
387394
}
388395

@@ -467,10 +474,10 @@ static public int showLocateDialog(Frame editor) {
467474
"width: " + TEXT_WIDTH + "px }" +
468475
"</style> </head>";
469476
String title = "";
470-
if (SDK_LOAD_ERROR == MISSING_SDK) {
477+
if (loadError == MISSING_SDK) {
471478
htmlString += "<body> <p>" + MISSING_SDK_MESSAGE + "</p> </body> </html>";
472479
title = MISSING_SDK_TITLE;
473-
} else if (SDK_LOAD_ERROR == INVALID_SDK) {
480+
} else if (loadError == INVALID_SDK) {
474481
htmlString += "<body> <p>" + INVALID_SDK_MESSAGE + "</p> </body> </html>";
475482
title = INVALID_SDK_TITLE;
476483
}

0 commit comments

Comments
 (0)