Skip to content

Commit f3ba562

Browse files
committed
Removed sdkmanager in download task
1 parent 9d11217 commit f3ba562

File tree

3 files changed

+79
-17
lines changed

3 files changed

+79
-17
lines changed

build.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
mode/guava-18.0.jar;
4343
mode/httpcore-4.1.jar;
4444
mode/repository-25.3.2.jar;
45-
mode/sdklib-25.3.2.jar"
45+
mode/sdklib-25.3.2.jar;
46+
mode/commons-compress-1.8.1.jar"
4647
debug="on">
4748
<src path="src" />
4849
</javac>

mode/commons-compress-1.8.1.jar

357 KB
Binary file not shown.

src/processing/mode/android/SDKUpdater.java

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import com.android.repository.impl.meta.RepositoryPackages;
55
import com.android.repository.io.FileOpUtils;
66
import com.android.repository.io.impl.FileSystemFileOp;
7+
import com.android.repository.util.InstallerUtil;
78
import com.android.sdklib.repository.AndroidSdkHandler;
9+
import com.android.sdklib.repository.installer.SdkInstallerUtil;
810
import com.android.sdklib.repository.legacy.LegacyDownloader;
9-
import processing.app.Platform;
11+
import com.android.sdklib.tool.SdkManagerCli;
1012
import processing.app.ui.Editor;
1113

1214
import javax.swing.*;
@@ -23,6 +25,7 @@
2325
import java.io.IOException;
2426
import java.util.ArrayList;
2527
import java.util.Arrays;
28+
import java.util.List;
2629
import java.util.Vector;
2730
import java.util.concurrent.CancellationException;
2831
import java.util.concurrent.ExecutionException;
@@ -48,7 +51,6 @@ public class SDKUpdater extends JFrame implements PropertyChangeListener {
4851
private QueryTask queryTask;
4952
private DownloadTask downloadTask;
5053
private boolean downloadTaskRunning;
51-
private Process backgroundProcess;
5254

5355
private DefaultTableModel modelInstalled;
5456
private DefaultTableModel modelUpdates;
@@ -106,6 +108,9 @@ protected Object doInBackground() throws Exception {
106108
updatesList = new Vector<>();
107109
installedList = new Vector<>();
108110

111+
/* Following code is from listPackages() of com.android.sdklib.tool.SdkManagerCli
112+
with some changes
113+
*/
109114
AndroidSdkHandler mHandler = AndroidSdkHandler.getInstance(AndroidSDK.load().getSdkFolder());
110115

111116
ProgressIndicator progress = new ConsoleProgressIndicator();
@@ -166,8 +171,9 @@ protected void done() {
166171
modelInstalled.fireTableDataChanged();
167172
}
168173
} catch (InterruptedException | CancellationException e) {
169-
backgroundProcess.destroy();
174+
this.cancel(false);
170175
} catch (ExecutionException e) {
176+
this.cancel(true);
171177
JOptionPane.showMessageDialog(null,
172178
e.getCause().toString(), "Error", JOptionPane.ERROR_MESSAGE);
173179
e.printStackTrace();
@@ -179,18 +185,44 @@ class DownloadTask extends SwingWorker {
179185
@Override
180186
protected Object doInBackground() throws Exception {
181187
downloadTaskRunning = true;
182-
ArrayList<String> cmd = new ArrayList<>();
183-
String path = toolsFolder + File.separator + "bin" + File.separator;
184-
if (Platform.isWindows())
185-
path += "sdkmanager.bat";
186-
else
187-
path += "sdkmanager";
188-
cmd.add(path);
189-
cmd.add("--update");
190-
191-
ProcessBuilder process = new ProcessBuilder(cmd);
192-
backgroundProcess = process.start();
193-
backgroundProcess.waitFor();
188+
189+
/* Following code is from installPackages() of com.android.sdklib.tool.SdkManagerCli
190+
with some changes
191+
*/
192+
AndroidSdkHandler mHandler = AndroidSdkHandler.getInstance(AndroidSDK.load().getSdkFolder());
193+
194+
FileSystemFileOp fop = (FileSystemFileOp) FileOpUtils.create();
195+
CustomSettings settings = new CustomSettings();
196+
LegacyDownloader downloader = new LegacyDownloader(fop, settings);
197+
ProgressIndicator progress = new ConsoleProgressIndicator();
198+
199+
RepoManager mRepoManager = mHandler.getSdkManager(progress);
200+
mRepoManager.loadSynchronously(0, progress, downloader, settings);
201+
202+
List<RemotePackage> remotes = new ArrayList<>();
203+
for (String path : settings.getPaths(mRepoManager)) {
204+
RemotePackage p = mRepoManager.getPackages().getRemotePackages().get(path);
205+
if (p == null) {
206+
progress.logWarning("Failed to find package " + path);
207+
throw new SdkManagerCli.CommandFailedException();
208+
}
209+
remotes.add(p);
210+
}
211+
remotes = InstallerUtil.computeRequiredPackages(
212+
remotes, mRepoManager.getPackages(), progress);
213+
if (remotes != null) {
214+
for (RemotePackage p : remotes) {
215+
Installer installer = SdkInstallerUtil.findBestInstallerFactory(p, mHandler)
216+
.createInstaller(p, mRepoManager, downloader, mHandler.getFileOp());
217+
if (!(installer.prepare(progress) && installer.complete(progress))) {
218+
// there was an error, abort.
219+
throw new SdkManagerCli.CommandFailedException();
220+
}
221+
}
222+
} else {
223+
progress.logWarning("Unable to compute a complete list of dependencies.");
224+
throw new SdkManagerCli.CommandFailedException();
225+
}
194226

195227
return null;
196228
}
@@ -208,8 +240,9 @@ protected void done() {
208240
queryTask.addPropertyChangeListener(SDKUpdater.this);
209241
queryTask.execute();
210242
} catch (InterruptedException | CancellationException e) {
211-
backgroundProcess.destroy();
243+
this.cancel(true);
212244
} catch (ExecutionException e) {
245+
this.cancel(true);
213246
JOptionPane.showMessageDialog(null,
214247
e.getCause().toString(), "Error", JOptionPane.ERROR_MESSAGE);
215248
e.printStackTrace();
@@ -218,6 +251,34 @@ protected void done() {
218251
progressBar.setIndeterminate(false);
219252
}
220253
}
254+
255+
class CustomSettings implements SettingsController {
256+
/* Dummy implementation with some necessary methods from the original
257+
implementation in com.android.sdklib.tool.SdkManagerCli
258+
*/
259+
@Override
260+
public boolean getForceHttp() {
261+
return false;
262+
}
263+
264+
@Override
265+
public void setForceHttp(boolean b) { }
266+
267+
@Override
268+
public Channel getChannel() {
269+
return null;
270+
}
271+
272+
public java.util.List<String> getPaths(RepoManager mgr) {
273+
List<String> updates = new ArrayList<>();
274+
for(UpdatablePackage upd : mgr.getPackages().getUpdatedPkgs()) {
275+
if(!upd.getRemote().obsolete()) {
276+
updates.add(upd.getRepresentative().getPath());
277+
}
278+
}
279+
return updates;
280+
}
281+
}
221282
}
222283

223284
private void createLayout() {

0 commit comments

Comments
 (0)