forked from processing/processing-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSysImageDownloader.java
More file actions
585 lines (493 loc) · 22.2 KB
/
Copy pathSysImageDownloader.java
File metadata and controls
585 lines (493 loc) · 22.2 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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2016 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 org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import processing.app.Platform;
import processing.app.Preferences;
import processing.app.exec.LineProcessor;
import processing.app.exec.StreamPump;
import processing.app.ui.Toolkit;
import processing.core.PApplet;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
@SuppressWarnings("serial")
public class SysImageDownloader extends JDialog implements PropertyChangeListener {
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 static final String EMULATOR_GUIDE_URL =
"https://developer.android.com/studio/run/emulator-acceleration.html";
private static final String SYS_IMAGE_SELECTION_MESSAGE =
"The Android emulator requires a system image to run. " +
"There are two types of system images available:<br><br>" +
"<b>1) ARM image -</b> slow but compatible with all computers, no extra configuration needed.<br><br>" +
"<b>2) x86 image -</b> fast but compatible only with Intel CPUs, extra configuration may be needed, see " +
"<a href=\"" + EMULATOR_GUIDE_URL + "\">this guide</a> for more details.";
private static final String HAXM_INSTALL_TITLE = "Some words of caution...";
private static final String HAXM_INSTALL_MESSAGE =
"Processing will install x86 images in the emulator. These images are fast, but " +
"also need the Intel Hardware Accelerated Execution Manager (Intel HAXM).<br><br>" +
"Processing will try to run the HAXM installer now, which may ask for your " +
"administrator password or additional permissions.";
private static final String KVM_LINUX_GUIDE_URL =
"https://developer.android.com/studio/run/emulator-acceleration.html#vm-linux";
private static final String KVM_INSTALL_MESSAGE =
"You chose to run x86 images in the emulator. This is great but you need " +
"to configure VM acceleration on Linux using the KVM package.<br><br>" +
"Follow <a href=\"" + KVM_LINUX_GUIDE_URL + "\">these instructions</a> " +
"to configure KVM.";
private static final String IA32LIBS_TITLE = "Additional setup may be required...";
private static final String IA32LIBS_MESSAGE =
"Looks like you are running a 64-bit version of Linux. In order<br>" +
"to create the SD card in the emulator, Processing needs the<br>" +
"ia32-libs compatibility package. On Ubuntu Linux, you can<br>" +
"install it by runing the following command: <br><br>" +
"sudo apt-get install lib32stdc++6";
private static final String SYS_IMAGES_ARM_URL = "https://dl.google.com/android/repository/sys-img/android/";
private static final String SYS_IMAGES_PHONE_URL = "https://dl.google.com/android/repository/sys-img/google_apis/";
private static final String SYS_IMAGES_PHONE_LIST = "sys-img2-1.xml";
private static final String SYS_IMAGES_WEAR_URL = "https://dl.google.com/android/repository/sys-img/android-wear/";
private static final String SYS_IMAGES_WEAR_LIST = "sys-img2-1.xml";
private static final String PROPERTY_CHANGE_EVENT_TOTAL = "total";
private static final String PROPERTY_CHANGE_EVENT_DOWNLOADED = "downloaded";
private JProgressBar progressBar;
private JLabel downloadedTextArea;
private DownloadTask downloadTask;
private Frame editor;
private boolean result;
private boolean wear;
private boolean askABI;
private String abi;
private boolean cancelled;
private int totalSize = 0;
class UrlHolder {
public String platformVersion;
public String sysImgUrl, sysImgTag, sysImgWearUrl, sysImgWearTag;
public String sysImgFilename, sysImgWearFilename;
public int totalSize = 0;
}
class DownloadTask extends SwingWorker<Object, Object> {
private int downloadedSize = 0;
private int BUFFER_SIZE = 4096;
@Override
protected Object doInBackground() throws Exception {
result = false;
// The SDK should already be detected by the android mode
String sdkPrefsPath = Preferences.get("android.sdk.path");
File sketchbookFolder = processing.app.Base.getSketchbookFolder();
File androidFolder = new File(sketchbookFolder, "android");
if (!androidFolder.exists()) androidFolder.mkdir();
File sdkFolder = new File(sdkPrefsPath);
if (!sdkFolder.exists()) {
throw new IOException("SDK folder does not exist " + sdkFolder.getAbsolutePath());
}
// creating sdk folders
File sysImgFolder = new File(sdkFolder, "system-images");
if (!sysImgFolder.exists()) sysImgFolder.mkdir();
// creating temp folder for downloaded zip packages
File tempFolder = new File(androidFolder, "temp");
if (!tempFolder.exists()) tempFolder.mkdir();
try {
String repo;
if (wear) {
repo = SYS_IMAGES_WEAR_URL + SYS_IMAGES_WEAR_LIST;
} else if (abi.equals("arm")) {
// The ARM images using Google APIs are too slow, so use the
// older Android (AOSP) images.
repo = SYS_IMAGES_ARM_URL + SYS_IMAGES_PHONE_LIST;
} else {
repo = SYS_IMAGES_PHONE_URL + SYS_IMAGES_PHONE_LIST;
}
UrlHolder downloadUrls = new UrlHolder();
getDownloadUrls(downloadUrls, repo, Platform.getName());
firePropertyChange(PROPERTY_CHANGE_EVENT_TOTAL, 0, downloadUrls.totalSize);
totalSize = downloadUrls.totalSize;
if (wear) {
// wear system images
File downloadedSysImgWear = new File(tempFolder, downloadUrls.sysImgWearFilename);
File tmp = new File(sysImgFolder, "android-" + AndroidBuild.TARGET_SDK);
if (!tmp.exists()) tmp.mkdir();
File sysImgWearFinalFolder = new File(tmp, downloadUrls.sysImgWearTag);
if (!sysImgWearFinalFolder.exists()) sysImgWearFinalFolder.mkdir();
downloadAndUnpack(downloadUrls.sysImgWearUrl, downloadedSysImgWear, sysImgWearFinalFolder, false);
fixSourceProperties(sysImgWearFinalFolder);
} else {
// mobile system images
File downloadedSysImg = new File(tempFolder, downloadUrls.sysImgFilename);
String level = abi.equals("arm") ? AVD.TARGET_SDK_ARM : AndroidBuild.TARGET_SDK;
File tmp = new File(sysImgFolder, "android-" + level);
if (!tmp.exists()) tmp.mkdir();
File sysImgFinalFolder = new File(tmp, downloadUrls.sysImgTag);
if (!sysImgFinalFolder.exists()) sysImgFinalFolder.mkdir();
downloadAndUnpack(downloadUrls.sysImgUrl, downloadedSysImg, sysImgFinalFolder, false);
fixSourceProperties(sysImgFinalFolder);
}
if (Platform.isLinux() || Platform.isMacOS()) {
Runtime.getRuntime().exec("chmod -R 755 " + sysImgFolder.getAbsolutePath());
}
for (File f: tempFolder.listFiles()) f.delete();
tempFolder.delete();
if (Platform.isLinux() && Platform.getVariant().equals("64")) {
AndroidUtil.showMessage(IA32LIBS_TITLE, IA32LIBS_MESSAGE);
}
result = true;
} catch (ParserConfigurationException e) {
// TODO Handle exceptions here somehow (ie show error message)
// and handle at least mkdir() results (above)
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void done() {
super.done();
setVisible(false);
dispose();
}
private void downloadAndUnpack(String urlString, File saveTo,
File unpackTo, boolean setExec) throws IOException {
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
e.printStackTrace();
return;
}
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
FileOutputStream outputStream = new FileOutputStream(saveTo);
byte[] b = new byte[BUFFER_SIZE];
int count;
while ((count = inputStream.read(b)) >= 0) {
outputStream.write(b, 0, count);
downloadedSize += count;
firePropertyChange(PROPERTY_CHANGE_EVENT_DOWNLOADED, 0, downloadedSize);
}
outputStream.flush(); outputStream.close(); inputStream.close();
inputStream.close();
outputStream.close();
AndroidUtil.extractFolder(saveTo, unpackTo, setExec);
}
// For some reason the source.properties file includes Addon entries,
// and this breaks the image...
private void fixSourceProperties(File imageFolder) {
for (File d: imageFolder.listFiles()) {
// Should iterate over the installed archs (x86, etc)
if (d.isDirectory()) {
for (File f: d.listFiles()) {
if (PApplet.getExtension(f.getName()).equals("properties")) {
String[] linesIn = PApplet.loadStrings(f);
String concat = "";
for (String l: linesIn) {
if (l.indexOf("Addon") == -1) concat += l + "\n";
}
String[] linesOut = concat.split("\n");
PApplet.saveStrings(f, linesOut);
}
}
}
}
}
private void getDownloadUrls(UrlHolder urlHolder,
String repositoryUrl, String requiredHostOs)
throws ParserConfigurationException, IOException, SAXException, XPathException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr;
NodeList remotePackages;
if (abi.equals("arm"))
expr = xpath.compile("//remotePackage[contains(@path, '" + AVD.TARGET_SDK_ARM + "')" +
"and contains(@path, \"armeabi-v7a\")]");
else
expr = xpath.compile("//remotePackage[contains(@path, '" + AndroidBuild.TARGET_SDK + "')" +
"and contains(@path, \"x86\")]");
if (wear) {
Document docSysImgWear = db.parse(new URL(repositoryUrl).openStream());
remotePackages = (NodeList) expr.evaluate(docSysImgWear, XPathConstants.NODESET);
NodeList childNodes = remotePackages.item(0).getChildNodes();
NodeList typeDetails = ((Element) childNodes).getElementsByTagName("type-details");
NodeList tag = ((Element) typeDetails.item(0)).getElementsByTagName("tag");
NodeList id = ((Element) tag.item(0)).getElementsByTagName("id");
urlHolder.sysImgWearTag = id.item(0).getTextContent();
NodeList archives = ((Element) childNodes).getElementsByTagName("archive");
NodeList archive = archives.item(0).getChildNodes();
NodeList complete = ((Element) archive).getElementsByTagName("complete");
NodeList url = ((Element) complete.item(0)).getElementsByTagName("url");
NodeList size = ((Element) complete.item(0)).getElementsByTagName("size");
urlHolder.sysImgWearFilename = url.item(0).getTextContent();
urlHolder.sysImgWearUrl = SYS_IMAGES_WEAR_URL + urlHolder.sysImgWearFilename;
urlHolder.totalSize += Integer.parseInt(size.item(0).getTextContent());
} else {
Document docSysImg = db.parse(new URL(repositoryUrl).openStream());
remotePackages = (NodeList) expr.evaluate(docSysImg, XPathConstants.NODESET);
NodeList childNodes = remotePackages.item(0).getChildNodes(); // Index 1 contains x86_64
NodeList typeDetails = ((Element) childNodes).getElementsByTagName("type-details");
//NodeList abi = ((Element) typeDetails.item(0)).getElementsByTagName("abi");
//NodeList api = ((Element) typeDetails.item(0)).getElementsByTagName("api-level");
//System.out.println(api.item(0).getTextContent());
NodeList tag = ((Element) typeDetails.item(0)).getElementsByTagName("tag");
NodeList id = ((Element) tag.item(0)).getElementsByTagName("id");
urlHolder.sysImgTag = id.item(0).getTextContent();
NodeList archives = ((Element) childNodes).getElementsByTagName("archive");
NodeList archive = archives.item(0).getChildNodes();
NodeList complete = ((Element) archive).getElementsByTagName("complete");
NodeList url = ((Element) complete.item(0)).getElementsByTagName("url");
NodeList size = ((Element) complete.item(0)).getElementsByTagName("size");
urlHolder.sysImgFilename = url.item(0).getTextContent();
String imgUrl = abi.equals("arm") ? SYS_IMAGES_ARM_URL : SYS_IMAGES_PHONE_URL;
urlHolder.sysImgUrl = imgUrl + urlHolder.sysImgFilename;
urlHolder.totalSize += Integer.parseInt(size.item(0).getTextContent());
}
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(PROPERTY_CHANGE_EVENT_TOTAL)) {
progressBar.setIndeterminate(false);
totalSize = (Integer) evt.getNewValue();
progressBar.setMaximum(totalSize);
} else if (evt.getPropertyName().equals(PROPERTY_CHANGE_EVENT_DOWNLOADED)) {
downloadedTextArea.setText(humanReadableByteCount((Integer) evt.getNewValue(), true)
+ " / " + humanReadableByteCount(totalSize, true));
progressBar.setValue((Integer) evt.getNewValue());
}
}
// http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
static public int showSysImageMessage() {
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>";
htmlString += "<body> <p> " + SYS_IMAGE_SELECTION_MESSAGE + " </p> </body> </html>";
String title = "Choose system image type to download...";
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 x86 image", "Use ARM image"
};
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;
}
}
public SysImageDownloader(Frame editor, boolean wear, boolean ask) {
super(editor, "System image download", true);
this.editor = editor;
this.wear = wear;
this.askABI = ask;
this.result = false;
createLayout();
}
public void run() {
cancelled = false;
abi = Preferences.get("android.emulator.image.abi");
if (abi == null || askABI) {
// Either there was no image architecture selected, or the default was set.
// In this case, we give the user the option to choose between ARM and x86
final int result;
// PROCESSOR_IDENTIFIER is only defined on Windows. For cross-platform CPU
// info, in the future we could use OSHI: https://github.com/oshi/oshi
String procId = System.getenv("PROCESSOR_IDENTIFIER");
if (procId != null) {
if (-1 < procId.indexOf("Intel")) {
// Intel CPU: we go for the x86 abi
result = JOptionPane.YES_OPTION;
} else {
// Another CPU, can only be AMD, so we go for ARM abi
result = JOptionPane.NO_OPTION;
}
} else if (Platform.isMacOS()) {
// Macs only have Intel CPUs, so we also go for the x86 abi
result = JOptionPane.YES_OPTION;
} else {
result = showSysImageMessage();
}
if (result == JOptionPane.YES_OPTION || result == JOptionPane.CLOSED_OPTION) {
abi = "x86";
installHAXM();
} else {
abi = "arm";
}
Preferences.set("android.emulator.image.abi", abi);
}
downloadTask = new DownloadTask();
downloadTask.addPropertyChangeListener(this);
downloadTask.execute();
setAlwaysOnTop(true);
setVisible(true);
}
public boolean cancelled() {
return cancelled;
}
public boolean getResult() {
return result;
}
static public void installHAXM() {
File haxmFolder = AndroidSDK.getHAXMInstallerFolder();
if (Platform.isLinux()) {
AndroidUtil.showMessage(HAXM_INSTALL_TITLE, KVM_INSTALL_MESSAGE);
} else if (haxmFolder.exists()) {
AndroidUtil.showMessage(HAXM_INSTALL_TITLE, HAXM_INSTALL_MESSAGE);
ProcessBuilder pb;
if (Platform.isWindows()) {
File exec = new File(haxmFolder, "silent_install.bat");
pb = new ProcessBuilder(exec.getAbsolutePath());
} else {
File exec = new File(haxmFolder, "HAXM installation");
pb = new ProcessBuilder(exec.getAbsolutePath());
}
pb.directory(haxmFolder);
pb.redirectErrorStream(true);
Process process = null;
try {
process = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
try {
StreamPump output = new StreamPump(process.getInputStream(), "HAXM: ");
output.addTarget(new LineProcessor() {
@Override
public void processLine(String line) {
System.out.println("HAXM: " + line);
}
}).start();
process.waitFor();
} catch (final InterruptedException ie) {
ie.printStackTrace();
} finally {
process.destroy();
}
}
}
}
private void createLayout() {
Container outer = getContentPane();
outer.removeAll();
Box pain = Box.createVerticalBox();
pain.setBorder(new EmptyBorder(13, 13, 13, 13));
outer.add(pain);
String labelText = wear ? "Downloading watch system image..." :
"Downloading phone system image...";
JLabel textarea = new JLabel(labelText);
textarea.setAlignmentX(LEFT_ALIGNMENT);
pain.add(textarea);
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
progressBar.setBorder(new EmptyBorder(10, 10, 10, 10) );
pain.add(progressBar);
downloadedTextArea = new JLabel("");
downloadedTextArea.setAlignmentX(LEFT_ALIGNMENT);
pain.add(downloadedTextArea);
// buttons
JPanel buttons = new JPanel();
// buttons.setPreferredSize(new Dimension(400, 35));
// JPanel buttons = new JPanel() {
// public Dimension getPreferredSize() {
// return new Dimension(400, 35);
// }
// public Dimension getMinimumSize() {
// return new Dimension(400, 35);
// }
// public Dimension getMaximumSize() {
// return new Dimension(400, 35);
// }
// };
// Box buttons = Box.createHorizontalBox();
buttons.setAlignmentX(LEFT_ALIGNMENT);
JButton cancelButton = new JButton("Cancel download");
Dimension dim = new Dimension(Toolkit.getButtonWidth()*2,
cancelButton.getPreferredSize().height);
cancelButton.setPreferredSize(dim);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (downloadTask != null) {
downloadTask.cancel(true);
}
setVisible(false);
cancelled = true;
}
});
cancelButton.setEnabled(true);
buttons.add(cancelButton);
// buttons.setMaximumSize(new Dimension(300, buttons.getPreferredSize().height));
pain.add(buttons);
JRootPane root = getRootPane();
root.setDefaultButton(cancelButton);
ActionListener disposer = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
setVisible(false);
}
};
Toolkit.registerWindowCloseKeys(root, disposer);
Toolkit.setIcon(this);
pack();
setResizable(false);
setLocationRelativeTo(editor);
}
}