Skip to content

Commit 16b244d

Browse files
committed
fixed SDk search and install logic
1 parent 49658ad commit 16b244d

File tree

4 files changed

+110
-110
lines changed

4 files changed

+110
-110
lines changed

src/processing/mode/android/AndroidEditor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ protected AndroidEditor(Base base, String path, EditorState state,
5555
Mode mode) throws EditorException {
5656
super(base, path, state, mode);
5757
androidMode = (AndroidMode) mode;
58+
androidMode.resetUserSelection();
5859
androidMode.checkSDK(this);
5960
}
6061

src/processing/mode/android/AndroidMode.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import processing.app.ui.Editor;
3232
import processing.app.ui.EditorException;
3333
import processing.app.ui.EditorState;
34+
import processing.mode.android.AndroidSDK.CancelException;
3435
import processing.mode.java.JavaMode;
3536

3637
import java.io.File;
@@ -43,7 +44,9 @@ public class AndroidMode extends JavaMode {
4344
private AndroidSDK sdk;
4445
private File coreZipLocation;
4546
private AndroidRunner runner;
46-
47+
48+
private boolean checkingSDK = false;
49+
private boolean userCancelledSDKSearch = false;
4750

4851
public AndroidMode(Base base, File folder) {
4952
super(base, folder);
@@ -128,27 +131,46 @@ public void loadSDK() {
128131
}
129132
}
130133

131-
public void checkSDK(Editor editor) {
132-
if (sdk == null) {
134+
135+
public void resetUserSelection() {
136+
userCancelledSDKSearch = false;
137+
}
138+
139+
140+
public void checkSDK(Editor editor) {
141+
if (checkingSDK) {
142+
// Some other thread has invoked SDK checking, so wait until the first one
143+
// is done (it might involve downloading the SDK, etc).
144+
while (checkingSDK) {
145+
try {
146+
Thread.sleep(10);
147+
} catch (InterruptedException e) {
148+
return;
149+
}
150+
}
151+
}
152+
if (userCancelledSDKSearch) return;
153+
checkingSDK = true;
154+
Throwable tr = null;
155+
if (sdk == null) {
133156
try {
134157
sdk = AndroidSDK.load();
135-
// FIXME REVERT THIS STATEMENT AFTER TESTING (should be ==)
136-
if (sdk == null && editor != null) {
158+
if (sdk == null) {
137159
sdk = AndroidSDK.locate(editor, this);
138160
}
139-
} catch (BadSDKException e) {
140-
e.printStackTrace();
141-
} catch (IOException e) {
142-
e.printStackTrace();
161+
} catch (CancelException cancel) {
162+
userCancelledSDKSearch = true;
163+
tr = cancel;
164+
} catch (Exception other) {
165+
tr = other;
143166
}
144167
}
145168
if (sdk == null) {
146-
if (!AndroidSDK.isDownloading()) {
147-
Messages.showWarning("It's gonna be a bad day",
148-
"The Android SDK could not be loaded.\n" +
149-
"Use of Android Mode will be all but disabled.");
150-
}
169+
Messages.showWarning("Bad news...",
170+
"The Android SDK could not be loaded.\n" +
171+
"The Android Mode will be disabled.", tr);
151172
}
173+
checkingSDK = false;
152174
}
153175

154176

src/processing/mode/android/AndroidSDK.java

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class AndroidSDK {
2626
private final File platformTools;
2727
private final File androidTool;
2828

29-
static private boolean downloading;
30-
3129
private static final String ANDROID_SDK_PRIMARY =
3230
"Is the Android SDK installed?";
3331

@@ -45,14 +43,6 @@ class AndroidSDK {
4543
private static final String SELECT_ANDROID_SDK_FOLDER =
4644
"Choose the location of the Android SDK";
4745

48-
private static final String NOT_ANDROID_SDK =
49-
"The selected folder does not appear to contain an Android SDK,\n" +
50-
"or the SDK needs to be updated to the latest version.";
51-
52-
// private static final String ANDROID_SDK_URL =
53-
// "http://developer.android.com/sdk/";
54-
55-
5646
public AndroidSDK(File folder) throws BadSDKException, IOException {
5747
this.folder = folder;
5848
if (!folder.exists()) {
@@ -163,13 +153,6 @@ public File getSdkFolder() {
163153
}
164154

165155

166-
/*
167-
public File getToolsFolder() {
168-
return tools;
169-
}
170-
*/
171-
172-
173156
public File getPlatformToolsFolder() {
174157
return platformTools;
175158
}
@@ -242,51 +225,33 @@ public static AndroidSDK load() throws IOException {
242225

243226

244227
static public AndroidSDK locate(final Frame window, final AndroidMode androidMode)
245-
throws BadSDKException, IOException {
228+
throws BadSDKException, CancelException, IOException {
246229
final int result = showLocateDialog(window);
247-
if (result == JOptionPane.CANCEL_OPTION) {
248-
throw new BadSDKException("User canceled attempt to find SDK.");
249-
}
250230
if (result == JOptionPane.YES_OPTION) {
251-
// here we are going to download sdk automatically
252-
//Base.openURL(ANDROID_SDK_URL);
253-
//throw new BadSDKException("No SDK installed.");
254-
255-
return download(androidMode);
256-
}
257-
while (true) {
258-
// TODO this is really a yucky way to do this stuff. fix it.
231+
return download(window, androidMode);
232+
} else if (result == JOptionPane.NO_OPTION) {
233+
// user will manually select folder containing SDK folder
259234
File folder = selectFolder(SELECT_ANDROID_SDK_FOLDER, null, window);
260235
if (folder == null) {
261-
throw new BadSDKException("User canceled attempt to find SDK.");
262-
}
263-
try {
264-
final AndroidSDK androidSDK = new AndroidSDK(folder);
265-
Preferences.set("android.sdk.path", folder.getAbsolutePath());
266-
return androidSDK;
267-
268-
} catch (final BadSDKException nope) {
269-
JOptionPane.showMessageDialog(window, NOT_ANDROID_SDK);
236+
throw new CancelException("User canceled attempt to find SDK");
237+
} else {
238+
try {
239+
final AndroidSDK androidSDK = new AndroidSDK(folder);
240+
Preferences.set("android.sdk.path", folder.getAbsolutePath());
241+
return androidSDK;
242+
} catch (BadSDKException bad) {
243+
throw bad;
244+
}
270245
}
246+
} else {
247+
throw new CancelException("User canceled attempt to find SDK");
271248
}
272249
}
273250

274-
static boolean isDownloading() {
275-
return downloading;
276-
}
277-
278-
static public AndroidSDK download(final AndroidMode androidMode) throws BadSDKException {
279-
// TODO This is never set back to false once true [fry 150813]
280-
downloading = true;
281-
282-
EventQueue.invokeLater(new Runnable() {
283-
@Override
284-
public void run() {
285-
SDKDownloader downloader = new SDKDownloader(androidMode);
286-
downloader.startDownload();
287-
}
288-
});
289-
return null;
251+
static public AndroidSDK download(final Frame editor, final AndroidMode androidMode) throws BadSDKException {
252+
final SDKDownloader downloader = new SDKDownloader(editor, androidMode);
253+
downloader.run(); // This call blocks until the SDK download complete, or user cancels.
254+
return downloader.getSDK();
290255
}
291256

292257
static public int showLocateDialog(Frame editor) {
@@ -311,6 +276,7 @@ static public int showLocateDialog(Frame editor) {
311276
pane.setInitialValue(options[0]);
312277

313278
JDialog dialog = pane.createDialog(editor, null);
279+
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
314280
dialog.setVisible(true);
315281

316282
Object result = pane.getValue();
@@ -334,6 +300,7 @@ static public File selectFolder(String prompt, File folder, Frame frame) {
334300
//fd.setFile(folder.getName());
335301
}
336302
System.setProperty("apple.awt.fileDialogForDirectories", "true");
303+
fd.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
337304
fd.setVisible(true);
338305
System.setProperty("apple.awt.fileDialogForDirectories", "false");
339306
if (fd.getFile() == null) {
@@ -432,4 +399,18 @@ public ArrayList<SDKTarget> getAvailableSdkTargets() throws IOException {
432399

433400
return targets;
434401
}
402+
403+
@SuppressWarnings("serial")
404+
static public class BadSDKException extends Exception {
405+
public BadSDKException(final String message) {
406+
super(message);
407+
}
408+
}
409+
410+
@SuppressWarnings("serial")
411+
static public class CancelException extends Exception {
412+
public CancelException(final String message) {
413+
super(message);
414+
}
415+
}
435416
}

src/processing/mode/android/SDKDownloader.java

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.w3c.dom.NodeList;
77
import org.xml.sax.SAXException;
88

9-
import processing.app.Base;
109
import processing.app.Platform;
1110
import processing.app.Preferences;
1211
import processing.app.ui.Toolkit;
@@ -32,22 +31,26 @@
3231
import java.util.zip.ZipFile;
3332

3433
@SuppressWarnings("serial")
35-
public class SDKDownloader extends JFrame implements PropertyChangeListener {
34+
public class SDKDownloader extends JDialog implements PropertyChangeListener {
3635

3736
private static final String URL_REPOSITORY = "https://dl-ssl.google.com/android/repository/repository-11.xml";
3837
private static final String URL_REPOSITORY_FOLDER = "http://dl-ssl.google.com/android/repository/";
3938
private static final String URL_USB_DRIVER = "https://dl-ssl.google.com//android/repository/latest_usb_driver_windows.zip";
4039

4140
private static final String PLATFORM_API_LEVEL = "22";
4241

43-
public static final String PROPERTY_CHANGE_EVENT_TOTAL = "total";
42+
private static final String PROPERTY_CHANGE_EVENT_TOTAL = "total";
4443
private static final String PROPERTY_CHANGE_EVENT_DOWNLOADED = "downloaded";
4544

46-
private AndroidMode androidMode;
47-
48-
JProgressBar progressBar;
49-
JLabel downloadedTextArea;
45+
private JProgressBar progressBar;
46+
private JLabel downloadedTextArea;
5047

48+
private SDKDownloadTask downloadTask;
49+
50+
private Frame editor;
51+
private AndroidMode mode;
52+
private AndroidSDK sdk;
53+
5154
private int totalSize = 0;
5255
private static ZipFile zip;
5356

@@ -64,8 +67,14 @@ class SDKDownloadTask extends SwingWorker<Object, Object> {
6467

6568
@Override
6669
protected Object doInBackground() throws Exception {
67-
File modeFolder = new File(Base.getSketchbookModesFolder() + "/AndroidMode");
70+
71+
72+
// File modeDirectory = new File(folder, getTypeName());
73+
//
74+
// File modeFolder = new File(Base.getSketchbookModesFolder() + "/AndroidMode");
6875

76+
File modeFolder = mode.getFolder();
77+
6978
// creating sdk folders
7079
File sdkFolder = new File(modeFolder, "sdk");
7180
if (!sdkFolder.exists()) sdkFolder.mkdir();
@@ -114,9 +123,10 @@ protected Object doInBackground() throws Exception {
114123

115124
tempFolder.delete();
116125

126+
// Done, let's set the environment and load the new SDK!
117127
Platform.setenv("ANDROID_SDK", sdkFolder.getAbsolutePath());
118-
Preferences.set("android.sdk.path", sdkFolder.getAbsolutePath());
119-
androidMode.loadSDK();
128+
Preferences.set("android.sdk.path", sdkFolder.getAbsolutePath());
129+
sdk = AndroidSDK.load();
120130
} catch (ParserConfigurationException e) {
121131
// TODO Handle exceptions here somehow (ie show error message)
122132
// and handle at least mkdir() results (above)
@@ -133,6 +143,7 @@ protected Object doInBackground() throws Exception {
133143
protected void done() {
134144
super.done();
135145
setVisible(false);
146+
dispose();
136147
}
137148

138149
private void downloadAndUnpack(String urlString, File saveTo,
@@ -171,18 +182,6 @@ private void downloadAndUnpack(String urlString, File saveTo,
171182
extractFolder(saveTo, unpackTo, setExec);
172183
}
173184

174-
/*
175-
private String getOsString() {
176-
if (Base.isWindows()) {
177-
return "windows";
178-
} else if (Base.isLinux()) {
179-
return "linux";
180-
} else {
181-
return "macosx";
182-
}
183-
}
184-
*/
185-
186185
private SDKUrlHolder getDownloadUrls(String repositoryUrl, String requiredHostOs) throws ParserConfigurationException, IOException, SAXException {
187186
SDKUrlHolder urlHolder = new SDKUrlHolder();
188187

@@ -274,20 +273,26 @@ public static String humanReadableByteCount(long bytes, boolean si) {
274273
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
275274
}
276275

277-
public SDKDownloader(AndroidMode androidMode) {
278-
super("Android SDK downloading...");
279-
280-
this.androidMode = androidMode;
281-
276+
public SDKDownloader(Frame editor, AndroidMode mode) {
277+
super(editor, "Android SDK downloading...", true);
278+
this.editor = editor;
279+
this.mode = mode;
280+
this.sdk = null;
282281
createLayout();
283282
}
284-
285-
public void startDownload() {
286-
SDKDownloadTask downloadTask = new SDKDownloadTask();
283+
284+
public void run() {
285+
downloadTask = new SDKDownloadTask();
287286
downloadTask.addPropertyChangeListener(this);
288287
downloadTask.execute();
288+
setAlwaysOnTop(true);
289+
setVisible(true);
289290
}
290-
291+
292+
public AndroidSDK getSDK() {
293+
return sdk;
294+
}
295+
291296
private void createLayout() {
292297
Container outer = getContentPane();
293298
outer.removeAll();
@@ -337,6 +342,7 @@ private void createLayout() {
337342
cancelButton.setPreferredSize(dim);
338343
cancelButton.addActionListener(new ActionListener() {
339344
public void actionPerformed(ActionEvent e) {
345+
if (downloadTask != null) downloadTask.cancel(true);
340346
setVisible(false);
341347
}
342348
});
@@ -358,19 +364,9 @@ public void actionPerformed(ActionEvent actionEvent) {
358364

359365
pack();
360366

361-
/*
362-
Dimension screen = Toolkit.getScreenSize();
363-
Dimension windowSize = getSize();
364-
setLocation((screen.width - windowSize.width) / 2,
365-
(screen.height - windowSize.height) / 2);
366-
*/
367-
setLocationRelativeTo(null);
368-
369-
setVisible(true);
370-
setAlwaysOnTop(true);
367+
setLocationRelativeTo(editor);
371368
}
372369

373-
374370
static void extractFolder(File file, File newPath, boolean setExec) throws IOException {
375371
int BUFFER = 2048;
376372
zip = new ZipFile(file);

0 commit comments

Comments
 (0)