2 * Copyright (C) 2009 The Android Open Source Project
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package com.android.sdkuilib.internal.repository;
\r
19 import com.android.prefs.AndroidLocation.AndroidLocationException;
\r
20 import com.android.sdklib.ISdkLog;
\r
21 import com.android.sdklib.SdkManager;
\r
22 import com.android.sdklib.internal.avd.AvdManager;
\r
23 import com.android.sdklib.internal.repository.AddonPackage;
\r
24 import com.android.sdklib.internal.repository.Archive;
\r
25 import com.android.sdklib.internal.repository.ITask;
\r
26 import com.android.sdklib.internal.repository.ITaskFactory;
\r
27 import com.android.sdklib.internal.repository.ITaskMonitor;
\r
28 import com.android.sdklib.internal.repository.LocalSdkParser;
\r
29 import com.android.sdklib.internal.repository.Package;
\r
30 import com.android.sdklib.internal.repository.RepoSource;
\r
31 import com.android.sdklib.internal.repository.RepoSources;
\r
32 import com.android.sdklib.internal.repository.ToolPackage;
\r
33 import com.android.sdklib.internal.repository.Package.UpdateInfo;
\r
34 import com.android.sdkuilib.internal.repository.icons.ImageFactory;
\r
35 import com.android.sdkuilib.repository.UpdaterWindow.ISdkListener;
\r
37 import org.eclipse.swt.widgets.Shell;
\r
39 import java.io.ByteArrayOutputStream;
\r
40 import java.io.PrintStream;
\r
41 import java.util.ArrayList;
\r
42 import java.util.Collection;
\r
43 import java.util.HashMap;
\r
44 import java.util.Map;
\r
47 * Data shared between {@link UpdaterWindowImpl} and its pages.
\r
50 private String mOsSdkRoot;
\r
52 private final ISdkLog mSdkLog;
\r
53 private ITaskFactory mTaskFactory;
\r
54 private boolean mUserCanChangeSdkRoot;
\r
56 private SdkManager mSdkManager;
\r
57 private AvdManager mAvdManager;
\r
59 private final LocalSdkParser mLocalSdkParser = new LocalSdkParser();
\r
60 private final RepoSources mSources = new RepoSources();
\r
62 private final LocalSdkAdapter mLocalSdkAdapter = new LocalSdkAdapter(this);
\r
63 private final RepoSourcesAdapter mSourcesAdapter = new RepoSourcesAdapter(this);
\r
65 private ImageFactory mImageFactory;
\r
67 private final SettingsController mSettingsController = new SettingsController();
\r
69 private final ArrayList<ISdkListener> mListeners = new ArrayList<ISdkListener>();
\r
71 private Shell mWindowShell;
\r
73 public UpdaterData(String osSdkRoot, ISdkLog sdkLog) {
\r
74 mOsSdkRoot = osSdkRoot;
\r
80 // ----- getters, setters ----
\r
82 public void setOsSdkRoot(String osSdkRoot) {
\r
83 if (mOsSdkRoot == null || mOsSdkRoot.equals(osSdkRoot) == false) {
\r
84 mOsSdkRoot = osSdkRoot;
\r
89 public String getOsSdkRoot() {
\r
93 public void setTaskFactory(ITaskFactory taskFactory) {
\r
94 mTaskFactory = taskFactory;
\r
97 public ITaskFactory getTaskFactory() {
\r
98 return mTaskFactory;
\r
101 public void setUserCanChangeSdkRoot(boolean userCanChangeSdkRoot) {
\r
102 mUserCanChangeSdkRoot = userCanChangeSdkRoot;
\r
105 public boolean canUserChangeSdkRoot() {
\r
106 return mUserCanChangeSdkRoot;
\r
109 public RepoSources getSources() {
\r
113 public RepoSourcesAdapter getSourcesAdapter() {
\r
114 return mSourcesAdapter;
\r
117 public LocalSdkParser getLocalSdkParser() {
\r
118 return mLocalSdkParser;
\r
121 public LocalSdkAdapter getLocalSdkAdapter() {
\r
122 return mLocalSdkAdapter;
\r
125 public ISdkLog getSdkLog() {
\r
129 public void setImageFactory(ImageFactory imageFactory) {
\r
130 mImageFactory = imageFactory;
\r
133 public ImageFactory getImageFactory() {
\r
134 return mImageFactory;
\r
137 public SdkManager getSdkManager() {
\r
138 return mSdkManager;
\r
141 public AvdManager getAvdManager() {
\r
142 return mAvdManager;
\r
145 public SettingsController getSettingsController() {
\r
146 return mSettingsController;
\r
149 public void addListeners(ISdkListener listener) {
\r
150 if (mListeners.contains(listener) == false) {
\r
151 mListeners.add(listener);
\r
155 public void removeListener(ISdkListener listener) {
\r
156 mListeners.remove(listener);
\r
159 public void setWindowShell(Shell windowShell) {
\r
160 mWindowShell = windowShell;
\r
163 public Shell getWindowShell() {
\r
164 return mWindowShell;
\r
170 * Initializes the {@link SdkManager} and the {@link AvdManager}.
\r
172 private void initSdk() {
\r
173 mSdkManager = SdkManager.createManager(mOsSdkRoot, mSdkLog);
\r
175 mAvdManager = null; // remove the old one if needed.
\r
176 mAvdManager = new AvdManager(mSdkManager, mSdkLog);
\r
177 } catch (AndroidLocationException e) {
\r
178 mSdkLog.error(e, "Unable to read AVDs");
\r
181 // notify adapters/parsers
\r
184 // notify listeners.
\r
189 * Reloads the SDK content (targets).
\r
190 * <p/> This also reloads the AVDs in case their status changed.
\r
191 * <p/>This does not notify the listeners ({@link ISdkListener}).
\r
193 public void reloadSdk() {
\r
195 mSdkManager.reloadSdk(mSdkLog);
\r
198 if (mAvdManager != null) {
\r
200 mAvdManager.reloadAvds(mSdkLog);
\r
201 } catch (AndroidLocationException e) {
\r
206 // notify adapters?
\r
207 mLocalSdkParser.clearPackages();
\r
210 // notify listeners
\r
215 * Reloads the AVDs.
\r
216 * <p/>This does not notify the listeners.
\r
218 public void reloadAvds() {
\r
220 if (mAvdManager != null) {
\r
222 mAvdManager.reloadAvds(mSdkLog);
\r
223 } catch (AndroidLocationException e) {
\r
224 mSdkLog.error(e, null);
\r
230 * Returns the list of installed packages, parsing them if this has not yet been done.
\r
232 public Package[] getInstalledPackage() {
\r
233 LocalSdkParser parser = getLocalSdkParser();
\r
235 Package[] packages = parser.getPackages();
\r
237 if (packages == null) {
\r
238 // load on demand the first time
\r
239 packages = parser.parseSdk(getOsSdkRoot(), getSdkManager(), getSdkLog());
\r
246 * Notify the listeners ({@link ISdkListener}) that the SDK was reloaded.
\r
247 * <p/>This can be called from any thread.
\r
249 public void notifyListeners() {
\r
250 if (mWindowShell != null && mListeners.size() > 0) {
\r
251 mWindowShell.getDisplay().syncExec(new Runnable() {
\r
252 public void run() {
\r
253 for (ISdkListener listener : mListeners) {
\r
255 listener.onSdkChange();
\r
256 } catch (Throwable t) {
\r
257 mSdkLog.error(t, null);
\r
266 * Install the list of given {@link Archive}s. This is invoked by the user selecting some
\r
267 * packages in the remote page and then clicking "install selected".
\r
269 * @param archives The archives to install. Incompatible ones will be skipped.
\r
271 public void installArchives(final Collection<Archive> archives) {
\r
272 if (mTaskFactory == null) {
\r
273 throw new IllegalArgumentException("Task Factory is null");
\r
276 final boolean forceHttp = getSettingsController().getForceHttp();
\r
278 mTaskFactory.start("Installing Archives", new ITask() {
\r
279 public void run(ITaskMonitor monitor) {
\r
281 final int progressPerArchive = 2 * Archive.NUM_MONITOR_INC;
\r
282 monitor.setProgressMax(archives.size() * progressPerArchive);
\r
283 monitor.setDescription("Preparing to install archives");
\r
285 boolean installedAddon = false;
\r
286 boolean installedTools = false;
\r
288 int numInstalled = 0;
\r
289 for (Archive archive : archives) {
\r
291 int nextProgress = monitor.getProgress() + progressPerArchive;
\r
293 if (monitor.isCancelRequested()) {
\r
297 if (archive.getParentPackage() instanceof AddonPackage) {
\r
298 installedAddon = true;
\r
299 } else if (archive.getParentPackage() instanceof ToolPackage) {
\r
300 installedTools = true;
\r
303 if (archive.install(mOsSdkRoot, forceHttp, mSdkManager, monitor)) {
\r
307 } catch (Throwable t) {
\r
308 // Display anything unexpected in the monitor.
\r
309 String msg = t.getMessage();
\r
311 monitor.setResult("Unexpected Error installing '%1$s': %2$s",
\r
312 archive.getParentPackage().getShortDescription(), msg);
\r
314 // no error info? get the stack call to display it
\r
315 // At least that'll give us a better bug report.
\r
316 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
317 t.printStackTrace(new PrintStream(baos));
\r
320 monitor.setResult("Unexpected Error installing '%1$s'\n%2$s",
\r
321 archive.getParentPackage().getShortDescription(),
\r
326 // Always move the progress bar to the desired position.
\r
327 // This allows internal methods to not have to care in case
\r
328 // they abort early
\r
329 monitor.incProgress(nextProgress - monitor.getProgress());
\r
333 if (installedAddon) {
\r
334 // Update the USB vendor ids for adb
\r
336 mSdkManager.updateAdb();
\r
337 } catch (Exception e) {
\r
338 mSdkLog.error(e, "Update ADB failed");
\r
342 if (installedAddon || installedTools) {
\r
343 // We need to restart ADB. Actually since we don't know if it's even
\r
344 // running, maybe we should just kill it and not start it.
\r
345 // Note: it turns out even under Windows we don't need to kill adb
\r
346 // before updating the tools folder, as adb.exe is (surprisingly) not
\r
349 // TODO either bring in ddmlib and use its existing methods to stop adb
\r
350 // or use a shell exec to tools/adb.
\r
353 if (numInstalled == 0) {
\r
354 monitor.setDescription("Done. Nothing was installed.");
\r
356 monitor.setDescription("Done. %1$d %2$s installed.",
\r
358 numInstalled == 1 ? "package" : "packages");
\r
360 //notify listeners something was installed, so that they can refresh
\r
368 * Tries to update all the *existing* local packages.
\r
369 * This first refreshes all sources, then compares the available remote packages when
\r
370 * the current local ones and suggest updates to be done to the user. Finally all
\r
371 * selected updates are installed.
\r
373 * @param selectedArchives The list of remote archive to consider for the update.
\r
374 * This can be null, in which case a list of remote archive is fetched from all
\r
375 * available sources.
\r
377 public void updateOrInstallAll(Collection<Archive> selectedArchives) {
\r
378 if (selectedArchives == null) {
\r
379 refreshSources(true);
\r
382 final Map<Archive, Archive> updates = findUpdates(selectedArchives);
\r
384 if (selectedArchives != null) {
\r
385 // Not only we want to perform updates but we also want to install the
\r
386 // selected archives. If they do not match an update, list them anyway
\r
387 // except they map themselves to null (no "old" archive)
\r
388 for (Archive a : selectedArchives) {
\r
389 if (!updates.containsValue(a)) {
\r
390 updates.put(a, null);
\r
395 UpdateChooserDialog dialog = new UpdateChooserDialog(this, updates);
\r
398 Collection<Archive> result = dialog.getResult();
\r
399 if (result != null && result.size() > 0) {
\r
400 installArchives(result);
\r
405 * Refresh all sources. This is invoked either internally (reusing an existing monitor)
\r
406 * or as a UI callback on the remote page "Refresh" button (in which case the monitor is
\r
407 * null and a new task should be created.)
\r
409 * @param forceFetching When true, load sources that haven't been loaded yet.
\r
410 * When false, only refresh sources that have been loaded yet.
\r
412 public void refreshSources(final boolean forceFetching) {
\r
413 assert mTaskFactory != null;
\r
415 final boolean forceHttp = getSettingsController().getForceHttp();
\r
417 mTaskFactory.start("Refresh Sources",new ITask() {
\r
418 public void run(ITaskMonitor monitor) {
\r
419 RepoSource[] sources = mSources.getSources();
\r
420 monitor.setProgressMax(sources.length);
\r
421 for (RepoSource source : sources) {
\r
422 if (forceFetching ||
\r
423 source.getPackages() != null ||
\r
424 source.getFetchError() != null) {
\r
425 source.load(monitor.createSubMonitor(1), forceHttp);
\r
427 monitor.incProgress(1);
\r
434 * Check the local archives vs the remote available packages to find potential updates.
\r
435 * Return a map [remote archive => local archive] of suitable update candidates.
\r
436 * Returns null if there's an unexpected error. Otherwise returns a map that can be
\r
437 * empty but not null.
\r
439 * @param selectedArchives The list of remote archive to consider for the update.
\r
440 * This can be null, in which case a list of remote archive is fetched from all
\r
441 * available sources.
\r
443 private Map<Archive, Archive> findUpdates(Collection<Archive> selectedArchives) {
\r
444 // Map [remote archive => local archive] of suitable update candidates
\r
445 Map<Archive, Archive> result = new HashMap<Archive, Archive>();
\r
447 // First go thru all sources and make a local list of all available archives
\r
448 // sorted by package class.
\r
449 HashMap<Class<? extends Package>, ArrayList<Archive>> availPkgs =
\r
450 new HashMap<Class<? extends Package>, ArrayList<Archive>>();
\r
452 if (selectedArchives != null) {
\r
453 // Only consider the archives given
\r
455 for (Archive a : selectedArchives) {
\r
456 // Only add compatible archives
\r
457 if (a.isCompatible()) {
\r
458 Class<? extends Package> clazz = a.getParentPackage().getClass();
\r
460 ArrayList<Archive> list = availPkgs.get(clazz);
\r
461 if (list == null) {
\r
462 availPkgs.put(clazz, list = new ArrayList<Archive>());
\r
470 // Get all the available archives from all loaded sources
\r
471 RepoSource[] remoteSources = getSources().getSources();
\r
473 for (RepoSource remoteSrc : remoteSources) {
\r
474 Package[] remotePkgs = remoteSrc.getPackages();
\r
475 if (remotePkgs != null) {
\r
476 for (Package remotePkg : remotePkgs) {
\r
477 Class<? extends Package> clazz = remotePkg.getClass();
\r
479 ArrayList<Archive> list = availPkgs.get(clazz);
\r
480 if (list == null) {
\r
481 availPkgs.put(clazz, list = new ArrayList<Archive>());
\r
484 for (Archive a : remotePkg.getArchives()) {
\r
485 // Only add compatible archives
\r
486 if (a.isCompatible()) {
\r
495 Package[] localPkgs = getLocalSdkParser().getPackages();
\r
496 if (localPkgs == null) {
\r
497 // This is unexpected. The local sdk parser should have been called first.
\r
501 for (Package localPkg : localPkgs) {
\r
502 // get the available archive list for this package type
\r
503 ArrayList<Archive> list = availPkgs.get(localPkg.getClass());
\r
505 // if this list is empty, we'll never find anything that matches
\r
506 if (list == null || list.size() == 0) {
\r
510 // local packages should have one archive at most
\r
511 Archive[] localArchives = localPkg.getArchives();
\r
512 if (localArchives != null && localArchives.length > 0) {
\r
513 Archive localArchive = localArchives[0];
\r
514 // only consider archive compatible with the current platform
\r
515 if (localArchive != null && localArchive.isCompatible()) {
\r
517 // We checked all this archive stuff because that's what eventually gets
\r
518 // installed, but the "update" mechanism really works on packages. So now
\r
519 // the real question: is there a remote package that can update this
\r
522 for (Archive availArchive : list) {
\r
523 UpdateInfo info = localPkg.canBeUpdatedBy(availArchive.getParentPackage());
\r
524 if (info == UpdateInfo.UPDATE) {
\r
526 result.put(availArchive, localArchive);
\r