Skip to content

Commit c3a5eba

Browse files
committed
Merge pull request #71 from imilka/sdkAutoDownload
Android SDK automatic download
2 parents f350607 + 1ab6a75 commit c3a5eba

File tree

4 files changed

+468
-30
lines changed

4 files changed

+468
-30
lines changed

src/processing/mode/android/AndroidEditor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@
2626
import processing.mode.java.JavaEditor;
2727

2828
import javax.swing.*;
29-
3029
import javax.swing.event.ChangeEvent;
3130
import javax.swing.event.ChangeListener;
3231
import java.awt.event.ActionEvent;
3332
import java.awt.event.ActionListener;
3433
import java.io.File;
3534
import java.io.IOException;
36-
import java.util.Timer;
3735
import java.util.TimerTask;
3836

3937
@SuppressWarnings("serial")
@@ -50,6 +48,8 @@ public UpdateDeviceListTask(JMenu deviceMenu) {
5048

5149
@Override
5250
public void run() {
51+
if(androidMode.getSDK() == null) return;
52+
5353
final Devices devices = Devices.getInstance();
5454
java.util.List<Device> deviceList = devices.findMultiple(false);
5555
Device selectedDevice = devices.getSelectedDevice();
@@ -212,7 +212,7 @@ public void actionPerformed(ActionEvent e) {
212212

213213
// start updating device menus
214214
UpdateDeviceListTask task = new UpdateDeviceListTask(deviceMenu);
215-
Timer timer = new Timer();
215+
java.util.Timer timer = new java.util.Timer();
216216
timer.schedule(task, 5000, 5000);
217217

218218
menu.addSeparator();

src/processing/mode/android/AndroidMode.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class AndroidMode extends JavaMode {
3535
private File coreZipLocation;
3636
private AndroidRunner runner;
3737

38+
public static boolean sdkDownloadInProgress = false;
3839

3940
public AndroidMode(Base base, File folder) {
4041
super(base, folder);
@@ -115,13 +116,23 @@ protected File getCoreZipLocation() {
115116
// return sdk;
116117
// }
117118

119+
public void loadSDK() {
120+
try {
121+
sdk = AndroidSDK.load();
122+
} catch (BadSDKException e) {
123+
e.printStackTrace();
124+
} catch (IOException e) {
125+
e.printStackTrace();
126+
}
127+
}
118128

119129
public void checkSDK(Editor parent) {
120130
if (sdk == null) {
121131
try {
122132
sdk = AndroidSDK.load();
133+
// FIXME REVERT THIS STATEMENT AFTER TESTING (should be ==)
123134
if (sdk == null) {
124-
sdk = AndroidSDK.locate(parent);
135+
sdk = AndroidSDK.locate(parent, this);
125136
}
126137
} catch (BadSDKException e) {
127138
e.printStackTrace();
@@ -130,10 +141,12 @@ public void checkSDK(Editor parent) {
130141
}
131142
}
132143
if (sdk == null) {
133-
Base.showWarning("It's gonna be a bad day",
134-
"The Android SDK could not be loaded.\n" +
135-
"Use of Android mode will be all but disabled.",
136-
null);
144+
if(!sdkDownloadInProgress) {
145+
Base.showWarning("It's gonna be a bad day",
146+
"The Android SDK could not be loaded.\n" +
147+
"Use of Android mode will be all but disabled.",
148+
null);
149+
}
137150
}
138151
}
139152

src/processing/mode/android/AndroidSDK.java

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
package processing.mode.android;
22

3-
import java.awt.FileDialog;
4-
import java.awt.Frame;
5-
import java.io.File;
6-
import java.io.IOException;
7-
import java.text.DateFormat;
8-
import java.text.ParseException;
9-
import java.text.SimpleDateFormat;
10-
import java.util.Date;
11-
12-
import javax.swing.JFileChooser;
13-
import javax.swing.JOptionPane;
14-
153
import processing.app.Base;
4+
import processing.app.Editor;
165
import processing.app.Platform;
176
import processing.app.Preferences;
187
import processing.app.exec.ProcessHelper;
198
import processing.app.exec.ProcessResult;
209
import processing.core.PApplet;
2110

11+
import javax.swing.*;
12+
import java.awt.*;
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.text.DateFormat;
16+
import java.text.ParseException;
17+
import java.text.SimpleDateFormat;
18+
import java.util.Date;
19+
2220
class AndroidSDK {
2321
private final File folder;
2422
private final File tools;
@@ -31,8 +29,10 @@ class AndroidSDK {
3129
private static final String ANDROID_SDK_SECONDARY =
3230
"The Android SDK does not appear to be installed, <br>" +
3331
"because the ANDROID_SDK variable is not set. <br>" +
34-
"If it is installed, click “Yes” to select the <br>" +
35-
"location of the SDK, or “No” to visit the SDK<br>" +
32+
"If it is installed, click “Locate SDK path” to select the <br>" +
33+
"location of the SDK, or “Download SDK” to let <br>" +
34+
"Processing download SDK automatically.<br><br>" +
35+
"If you want to download SDK manually, you can visit <br>"+
3636
"download site at http://developer.android.com/sdk.";
3737

3838
private static final String SELECT_ANDROID_SDK_FOLDER =
@@ -234,17 +234,18 @@ public static AndroidSDK load() throws BadSDKException, IOException {
234234
}
235235

236236

237-
static public AndroidSDK locate(final Frame window)
238-
throws BadSDKException, IOException {
239-
final int result = Base.showYesNoQuestion(window, "Android SDK",
240-
ANDROID_SDK_PRIMARY, ANDROID_SDK_SECONDARY);
237+
static public AndroidSDK locate(final Frame window, final AndroidMode androidMode)
238+
throws BadSDKException, IOException {
239+
final int result = showLocateDialog(window);
241240
if (result == JOptionPane.CANCEL_OPTION) {
242241
throw new BadSDKException("User canceled attempt to find SDK.");
243242
}
244-
if (result == JOptionPane.NO_OPTION) {
245-
// user admitted they don't have the SDK installed, and need help.
246-
Base.openURL(ANDROID_SDK_URL);
247-
throw new BadSDKException("No SDK installed.");
243+
if (result == JOptionPane.YES_OPTION) {
244+
// here we are going to download sdk automatically
245+
//Base.openURL(ANDROID_SDK_URL);
246+
//throw new BadSDKException("No SDK installed.");
247+
248+
return download(androidMode);
248249
}
249250
while (true) {
250251
// TODO this is really a yucky way to do this stuff. fix it.
@@ -263,6 +264,52 @@ static public AndroidSDK locate(final Frame window)
263264
}
264265
}
265266

267+
static public AndroidSDK download(final AndroidMode androidMode) throws BadSDKException {
268+
AndroidMode.sdkDownloadInProgress = true;
269+
270+
SwingUtilities.invokeLater(new Runnable() {
271+
@Override
272+
public void run() {
273+
SDKDownloader downloader = new SDKDownloader(androidMode);
274+
downloader.startDownload();
275+
}
276+
});
277+
return null;
278+
}
279+
280+
static public int showLocateDialog(Frame editor) {
281+
// Pane formatting adapted from the Quaqua guide
282+
// http://www.randelshofer.ch/quaqua/guide/joptionpane.html
283+
JOptionPane pane =
284+
new JOptionPane("<html> " +
285+
"<head> <style type=\"text/css\">"+
286+
"b { font: 13pt \"Lucida Grande\" }"+
287+
"p { font: 11pt \"Lucida Grande\"; margin-top: 8px; width: 300px }"+
288+
"</style> </head>" +
289+
"<b>" + ANDROID_SDK_PRIMARY + "</b>" +
290+
"<p>" + ANDROID_SDK_SECONDARY + "</p>",
291+
JOptionPane.QUESTION_MESSAGE);
292+
293+
String[] options = new String[] {
294+
"Download SDK automatically", "Locate SDK path manually"
295+
};
296+
pane.setOptions(options);
297+
298+
// highlight the safest option ala apple hig
299+
pane.setInitialValue(options[0]);
300+
301+
JDialog dialog = pane.createDialog(editor, null);
302+
dialog.setVisible(true);
303+
304+
Object result = pane.getValue();
305+
if (result == options[0]) {
306+
return JOptionPane.YES_OPTION;
307+
} else if (result == options[1]) {
308+
return JOptionPane.NO_OPTION;
309+
} else {
310+
return JOptionPane.CLOSED_OPTION;
311+
}
312+
}
266313

267314
// this was banished from Base because it encourages bad practice.
268315
// TODO figure out a better way to handle the above.

0 commit comments

Comments
 (0)