4
4
import com .android .repository .impl .meta .RepositoryPackages ;
5
5
import com .android .repository .io .FileOpUtils ;
6
6
import com .android .repository .io .impl .FileSystemFileOp ;
7
+ import com .android .repository .util .InstallerUtil ;
7
8
import com .android .sdklib .repository .AndroidSdkHandler ;
9
+ import com .android .sdklib .repository .installer .SdkInstallerUtil ;
8
10
import com .android .sdklib .repository .legacy .LegacyDownloader ;
9
- import processing . app . Platform ;
11
+ import com . android . sdklib . tool . SdkManagerCli ;
10
12
import processing .app .ui .Editor ;
11
13
12
14
import javax .swing .*;
23
25
import java .io .IOException ;
24
26
import java .util .ArrayList ;
25
27
import java .util .Arrays ;
28
+ import java .util .List ;
26
29
import java .util .Vector ;
27
30
import java .util .concurrent .CancellationException ;
28
31
import java .util .concurrent .ExecutionException ;
@@ -48,7 +51,6 @@ public class SDKUpdater extends JFrame implements PropertyChangeListener {
48
51
private QueryTask queryTask ;
49
52
private DownloadTask downloadTask ;
50
53
private boolean downloadTaskRunning ;
51
- private Process backgroundProcess ;
52
54
53
55
private DefaultTableModel modelInstalled ;
54
56
private DefaultTableModel modelUpdates ;
@@ -106,6 +108,9 @@ protected Object doInBackground() throws Exception {
106
108
updatesList = new Vector <>();
107
109
installedList = new Vector <>();
108
110
111
+ /* Following code is from listPackages() of com.android.sdklib.tool.SdkManagerCli
112
+ with some changes
113
+ */
109
114
AndroidSdkHandler mHandler = AndroidSdkHandler .getInstance (AndroidSDK .load ().getSdkFolder ());
110
115
111
116
ProgressIndicator progress = new ConsoleProgressIndicator ();
@@ -166,8 +171,9 @@ protected void done() {
166
171
modelInstalled .fireTableDataChanged ();
167
172
}
168
173
} catch (InterruptedException | CancellationException e ) {
169
- backgroundProcess . destroy ( );
174
+ this . cancel ( false );
170
175
} catch (ExecutionException e ) {
176
+ this .cancel (true );
171
177
JOptionPane .showMessageDialog (null ,
172
178
e .getCause ().toString (), "Error" , JOptionPane .ERROR_MESSAGE );
173
179
e .printStackTrace ();
@@ -179,18 +185,44 @@ class DownloadTask extends SwingWorker {
179
185
@ Override
180
186
protected Object doInBackground () throws Exception {
181
187
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
+ }
194
226
195
227
return null ;
196
228
}
@@ -208,8 +240,9 @@ protected void done() {
208
240
queryTask .addPropertyChangeListener (SDKUpdater .this );
209
241
queryTask .execute ();
210
242
} catch (InterruptedException | CancellationException e ) {
211
- backgroundProcess . destroy ( );
243
+ this . cancel ( true );
212
244
} catch (ExecutionException e ) {
245
+ this .cancel (true );
213
246
JOptionPane .showMessageDialog (null ,
214
247
e .getCause ().toString (), "Error" , JOptionPane .ERROR_MESSAGE );
215
248
e .printStackTrace ();
@@ -218,6 +251,34 @@ protected void done() {
218
251
progressBar .setIndeterminate (false );
219
252
}
220
253
}
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
+ }
221
282
}
222
283
223
284
private void createLayout () {
0 commit comments