SDK Manager: suggest install of new platforms, addons or extra packages.

Supports the new "android update sdk" action.

Change-Id: Ib096f1f024639018252c58d4f9c8872ead3c39b9
This commit is contained in:
Raphael
2009-10-09 18:24:08 -07:00
parent e4c98c0191
commit e0a265f558
9 changed files with 328 additions and 55 deletions

View File

@@ -124,7 +124,7 @@ public class LocalPackagesPage extends Composite implements ISdkListener {
mUpdateButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
onUpdateInstalledPackage(); //$hide$ (hide from SWT designer)
onUpdateSelected(); //$hide$ (hide from SWT designer)
}
});
@@ -255,7 +255,7 @@ public class LocalPackagesPage extends Composite implements ISdkListener {
mDescriptionLabel.setText(""); //$NON-NLS1-$
}
private void onUpdateInstalledPackage() {
private void onUpdateSelected() {
mUpdaterData.updateOrInstallAll(null /*selectedArchives*/);
}

View File

@@ -146,12 +146,14 @@ class UpdaterData {
return mSettingsController;
}
/** Adds a listener ({@link ISdkListener}) that is notified when the SDK is reloaded. */
public void addListeners(ISdkListener listener) {
if (mListeners.contains(listener) == false) {
mListeners.add(listener);
}
}
/** Removes a listener ({@link ISdkListener}) that is notified when the SDK is reloaded. */
public void removeListener(ISdkListener listener) {
mListeners.remove(listener);
}
@@ -444,9 +446,15 @@ class UpdaterData {
/**
* Tries to update all the *existing* local packages.
* This first refreshes all sources, then compares the available remote packages with
* the current local ones and suggest updates to be done to the user. Finally all
* selected updates are installed.
* <p/>
* There are two modes of operation:
* <ul>
* <li>If selectedArchives is null, refreshes all sources, compares the available remote
* packages with the current local ones and suggest updates to be done to the user (including
* new platforms that the users doesn't have yet).
* <li>If selectedArchives is not null, this represents a list of archives/packages that
* the user wants to install or update, so just process these.
* </ul>
*
* @param selectedArchives The list of remote archive to consider for the update.
* This can be null, in which case a list of remote archive is fetched from all
@@ -463,6 +471,13 @@ class UpdaterData {
getSources(),
getLocalSdkParser().getPackages());
if (selectedArchives == null) {
ul.addNewPlatforms(archives, getSources(), getLocalSdkParser().getPackages());
}
// TODO if selectedArchives is null and archives.len==0, find if there's
// any new platform we can suggest to install instead.
UpdateChooserDialog dialog = new UpdateChooserDialog(getWindowShell(), this, archives);
dialog.open();

View File

@@ -19,6 +19,7 @@ package com.android.sdkuilib.internal.repository;
import com.android.sdklib.AndroidVersion;
import com.android.sdklib.internal.repository.AddonPackage;
import com.android.sdklib.internal.repository.Archive;
import com.android.sdklib.internal.repository.ExtraPackage;
import com.android.sdklib.internal.repository.MinToolsPackage;
import com.android.sdklib.internal.repository.Package;
import com.android.sdklib.internal.repository.PlatformPackage;
@@ -29,6 +30,7 @@ import com.android.sdklib.internal.repository.Package.UpdateInfo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
/**
* The logic to compute which packages to install, based on the choices
@@ -39,8 +41,6 @@ import java.util.Collection;
*/
class UpdaterLogic {
private RepoSources mSources;
/**
* Compute which packages to install by taking the user selection
* and adding dependent packages as needed.
@@ -53,29 +53,127 @@ class UpdaterLogic {
RepoSources sources,
Package[] localPkgs) {
mSources = sources;
ArrayList<ArchiveInfo> archives = new ArrayList<ArchiveInfo>();
ArrayList<Package> remotePkgs = new ArrayList<Package>();
RepoSource[] remoteSources = sources.getSources();
if (selectedArchives == null) {
selectedArchives = findUpdates(localPkgs, remotePkgs);
selectedArchives = findUpdates(localPkgs, remotePkgs, remoteSources);
}
for (Archive a : selectedArchives) {
insertArchive(a, archives, selectedArchives, remotePkgs, localPkgs, false);
insertArchive(a,
archives,
selectedArchives,
remotePkgs,
remoteSources,
localPkgs,
false /*automated*/);
}
return archives;
}
/**
* Finds new platforms that the user does not have in his/her local SDK
* and adds them to the list of archives to install.
*/
public void addNewPlatforms(ArrayList<ArchiveInfo> archives,
RepoSources sources,
Package[] localPkgs) {
// Find the highest platform installed
float currentPlatformScore = 0;
float currentAddonScore = 0;
HashMap<String, Float> currentExtraScore = new HashMap<String, Float>();
for (Package p : localPkgs) {
int rev = p.getRevision();
int api = 0;
boolean isPreview = false;
if (p instanceof PlatformPackage) {
AndroidVersion vers = ((PlatformPackage) p).getVersion();
api = vers.getApiLevel();
isPreview = vers.isPreview();
} else if (p instanceof AddonPackage) {
AndroidVersion vers = ((AddonPackage) p).getVersion();
api = vers.getApiLevel();
isPreview = vers.isPreview();
} else if (!(p instanceof ExtraPackage)) {
continue;
}
// The score is 10*api + (1 if preview) + rev/100
// This allows previews to rank above a non-preview and
// allows revisions to rank appropriately.
float score = api * 10 + (isPreview ? 1 : 0) + rev/100.f;
if (p instanceof PlatformPackage) {
currentPlatformScore = Math.max(currentPlatformScore, score);
} else if (p instanceof AddonPackage) {
currentAddonScore = Math.max(currentAddonScore, score);
} else if (p instanceof ExtraPackage) {
currentExtraScore.put(((ExtraPackage) p).getPath(), score);
}
}
RepoSource[] remoteSources = sources.getSources();
ArrayList<Package> remotePkgs = new ArrayList<Package>();
fetchRemotePackages(remotePkgs, remoteSources);
for (Package p : remotePkgs) {
int rev = p.getRevision();
int api = 0;
boolean isPreview = false;
if (p instanceof PlatformPackage) {
AndroidVersion vers = ((PlatformPackage) p).getVersion();
api = vers.getApiLevel();
isPreview = vers.isPreview();
} else if (p instanceof AddonPackage) {
AndroidVersion vers = ((AddonPackage) p).getVersion();
api = vers.getApiLevel();
isPreview = vers.isPreview();
} else if (!(p instanceof ExtraPackage)) {
continue;
}
float score = api * 10 + (isPreview ? 1 : 0) + rev/100.f;
boolean shouldAdd = false;
if (p instanceof PlatformPackage) {
shouldAdd = score > currentPlatformScore;
} else if (p instanceof AddonPackage) {
shouldAdd = score > currentAddonScore;
} else if (p instanceof ExtraPackage) {
String key = ((ExtraPackage) p).getPath();
shouldAdd = !currentExtraScore.containsKey(key) ||
score > currentExtraScore.get(key).floatValue();
}
if (shouldAdd) {
// We should suggest this package for installation.
for (Archive a : p.getArchives()) {
if (a.isCompatible()) {
insertArchive(a,
archives,
null /*selectedArchives*/,
remotePkgs,
remoteSources,
localPkgs,
true /*automated*/);
}
}
}
}
}
/**
* Find suitable updates to all current local packages.
*/
private Collection<Archive> findUpdates(Package[] localPkgs, ArrayList<Package> remotePkgs) {
private Collection<Archive> findUpdates(Package[] localPkgs,
ArrayList<Package> remotePkgs,
RepoSource[] remoteSources) {
ArrayList<Archive> updates = new ArrayList<Archive>();
fetchRemotePackages(remotePkgs);
fetchRemotePackages(remotePkgs, remoteSources);
for (Package localPkg : localPkgs) {
for (Package remotePkg : remotePkgs) {
@@ -100,6 +198,7 @@ class UpdaterLogic {
ArrayList<ArchiveInfo> outArchives,
Collection<Archive> selectedArchives,
ArrayList<Package> remotePkgs,
RepoSource[] remoteSources,
Package[] localPkgs,
boolean automated) {
Package p = archive.getParentPackage();
@@ -114,15 +213,32 @@ class UpdaterLogic {
}
// find dependencies
ArchiveInfo dep = findDependency(p, outArchives, selectedArchives, remotePkgs, localPkgs);
ArchiveInfo dep = findDependency(p,
outArchives,
selectedArchives,
remotePkgs,
remoteSources,
localPkgs);
ArchiveInfo ai = new ArchiveInfo(
// Make sure it's not a dup
ArchiveInfo ai = null;
for (ArchiveInfo ai2 : outArchives) {
if (ai2.getNewArchive().getParentPackage().sameItemAs(archive.getParentPackage())) {
ai = ai2;
break;
}
}
if (ai == null) {
ai = new ArchiveInfo(
archive, //newArchive
updatedArchive, //replaced
dep //dependsOn
);
outArchives.add(ai);
}
outArchives.add(ai);
if (dep != null) {
dep.addDependencyFor(ai);
}
@@ -134,6 +250,7 @@ class UpdaterLogic {
ArrayList<ArchiveInfo> outArchives,
Collection<Archive> selectedArchives,
ArrayList<Package> remotePkgs,
RepoSource[] remoteSources,
Package[] localPkgs) {
// Current dependencies can be:
@@ -144,13 +261,23 @@ class UpdaterLogic {
AddonPackage addon = (AddonPackage) pkg;
return findPlatformDependency(
addon, outArchives, selectedArchives, remotePkgs, localPkgs);
addon,
outArchives,
selectedArchives,
remotePkgs,
remoteSources,
localPkgs);
} else if (pkg instanceof MinToolsPackage) {
MinToolsPackage platformOrExtra = (MinToolsPackage) pkg;
return findToolsDependency(
platformOrExtra, outArchives, selectedArchives, remotePkgs, localPkgs);
platformOrExtra,
outArchives,
selectedArchives,
remotePkgs,
remoteSources,
localPkgs);
}
return null;
@@ -168,6 +295,7 @@ class UpdaterLogic {
ArrayList<ArchiveInfo> outArchives,
Collection<Archive> selectedArchives,
ArrayList<Package> remotePkgs,
RepoSource[] remoteSources,
Package[] localPkgs) {
// This is the requirement to match.
int rev = platformOrExtra.getMinToolsRevision();
@@ -200,20 +328,22 @@ class UpdaterLogic {
}
// Otherwise look in the selected archives.
for (Archive a : selectedArchives) {
Package p = a.getParentPackage();
if (p instanceof ToolPackage) {
if (((ToolPackage) p).getRevision() >= rev) {
// It's not already in the list of things to install, so add it now
return insertArchive(a, outArchives,
selectedArchives, remotePkgs, localPkgs,
true);
if (selectedArchives != null) {
for (Archive a : selectedArchives) {
Package p = a.getParentPackage();
if (p instanceof ToolPackage) {
if (((ToolPackage) p).getRevision() >= rev) {
// It's not already in the list of things to install, so add it now
return insertArchive(a, outArchives,
selectedArchives, remotePkgs, remoteSources, localPkgs,
true /*automated*/);
}
}
}
}
// Finally nothing matched, so let's look at all available remote packages
fetchRemotePackages(remotePkgs);
fetchRemotePackages(remotePkgs, remoteSources);
for (Package p : remotePkgs) {
if (p instanceof ToolPackage) {
if (((ToolPackage) p).getRevision() >= rev) {
@@ -222,8 +352,8 @@ class UpdaterLogic {
for (Archive a : p.getArchives()) {
if (a.isCompatible()) {
return insertArchive(a, outArchives,
selectedArchives, remotePkgs, localPkgs,
true);
selectedArchives, remotePkgs, remoteSources, localPkgs,
true /*automated*/);
}
}
}
@@ -247,6 +377,7 @@ class UpdaterLogic {
ArrayList<ArchiveInfo> outArchives,
Collection<Archive> selectedArchives,
ArrayList<Package> remotePkgs,
RepoSource[] remoteSources,
Package[] localPkgs) {
// This is the requirement to match.
AndroidVersion v = addon.getVersion();
@@ -276,20 +407,22 @@ class UpdaterLogic {
}
// Otherwise look in the selected archives.
for (Archive a : selectedArchives) {
Package p = a.getParentPackage();
if (p instanceof PlatformPackage) {
if (v.equals(((PlatformPackage) p).getVersion())) {
// It's not already in the list of things to install, so add it now
return insertArchive(a, outArchives,
selectedArchives, remotePkgs, localPkgs,
true);
if (selectedArchives != null) {
for (Archive a : selectedArchives) {
Package p = a.getParentPackage();
if (p instanceof PlatformPackage) {
if (v.equals(((PlatformPackage) p).getVersion())) {
// It's not already in the list of things to install, so add it now
return insertArchive(a, outArchives,
selectedArchives, remotePkgs, remoteSources, localPkgs,
true /*automated*/);
}
}
}
}
// Finally nothing matched, so let's look at all available remote packages
fetchRemotePackages(remotePkgs);
fetchRemotePackages(remotePkgs, remoteSources);
for (Package p : remotePkgs) {
if (p instanceof PlatformPackage) {
if (v.equals(((PlatformPackage) p).getVersion())) {
@@ -298,8 +431,8 @@ class UpdaterLogic {
for (Archive a : p.getArchives()) {
if (a.isCompatible()) {
return insertArchive(a, outArchives,
selectedArchives, remotePkgs, localPkgs,
true);
selectedArchives, remotePkgs, remoteSources, localPkgs,
true /*automated*/);
}
}
}
@@ -315,14 +448,11 @@ class UpdaterLogic {
}
/** Fetch all remote packages only if really needed. */
protected void fetchRemotePackages(ArrayList<Package> remotePkgs) {
protected void fetchRemotePackages(ArrayList<Package> remotePkgs, RepoSource[] remoteSources) {
if (remotePkgs.size() > 0) {
return;
}
// Get all the available packages from all loaded sources
RepoSource[] remoteSources = mSources.getSources();
for (RepoSource remoteSrc : remoteSources) {
Package[] pkgs = remoteSrc.getPackages();
if (pkgs != null) {

View File

@@ -60,7 +60,11 @@ public class UpdaterWindowImpl {
private ArrayList<Object[]> mExtraPages;
/** A factory to create progress task dialogs. */
private ProgressTaskFactory mTaskFactory;
/** The initial page to display. If null or not a know class, the first page will be displayed.
* Must be set before the first call to {@link #open()}. */
private Class<? extends Composite> mInitialPage;
/** Sets whether the auto-update wizard will be shown when opening the window. */
private boolean mRequestAutoUpdate;
// --- UI members ---
@@ -148,7 +152,7 @@ public class UpdaterWindowImpl {
// Hide everything down-below from SWT designer
//$hide>>$
// --- UI Callbacks -----------
// --- Public API -----------
/**
@@ -169,14 +173,42 @@ public class UpdaterWindowImpl {
mExtraPages.add(new Object[]{ title, pageClass });
}
/**
* Indicate the initial page that should be selected when the window opens.
* This must be called before the call to {@link #open()}.
* If null or if the page class is not found, the first page will be selected.
*/
public void setInitialPage(Class<? extends Composite> pageClass) {
mInitialPage = pageClass;
}
/**
* Sets whether the auto-update wizard will be shown when opening the window.
* <p/>
* This must be called before the call to {@link #open()}.
*/
public void setRequestAutoUpdate(boolean requestAutoUpdate) {
mRequestAutoUpdate = requestAutoUpdate;
}
/**
* Adds a new listener to be notified when a change is made to the content of the SDK.
*/
public void addListeners(ISdkListener listener) {
mUpdaterData.addListeners(listener);
}
/**
* Removes a new listener to be notified anymore when a change is made to the content of
* the SDK.
*/
public void removeListener(ISdkListener listener) {
mUpdaterData.removeListener(listener);
}
// --- Internals & UI Callbacks -----------
/**
* Helper to return the SWT shell.
*/
@@ -230,12 +262,25 @@ public class UpdaterWindowImpl {
addPage(mRemotePackagesPage, "Available Packages");
addExtraPages();
displayPage(0);
mPageList.setSelection(0);
int pageIndex = 0;
int i = 0;
for (Composite p : mPages) {
if (p.getClass().equals(mInitialPage)) {
pageIndex = i;
break;
}
i++;
}
displayPage(pageIndex);
mPageList.setSelection(pageIndex);
setupSources();
initializeSettings();
mUpdaterData.notifyListeners();
if (mRequestAutoUpdate) {
mUpdaterData.updateOrInstallAll(null /*selectedArchives*/);
}
}
/**

View File

@@ -69,6 +69,25 @@ public class UpdaterWindow {
mWindow.registerExtraPage(title, pageClass);
}
/**
* Indicate the initial page that should be selected when the window opens.
* <p/>
* This must be called before the call to {@link #open()}.
* If null or if the page class is not found, the first page will be selected.
*/
public void setInitialPage(Class<? extends Composite> pageClass) {
mWindow.setInitialPage(pageClass);
}
/**
* Sets whether the auto-update wizard will be shown when opening the window.
* <p/>
* This must be called before the call to {@link #open()}.
*/
public void setRequestAutoUpdate(boolean requestAutoUpdate) {
mWindow.setRequestAutoUpdate(requestAutoUpdate);
}
/**
* Adds a new listener to be notified when a change is made to the content of the SDK.
*/

View File

@@ -21,6 +21,7 @@ import com.android.sdklib.internal.repository.MockAddonPackage;
import com.android.sdklib.internal.repository.MockPlatformPackage;
import com.android.sdklib.internal.repository.MockToolPackage;
import com.android.sdklib.internal.repository.Package;
import com.android.sdklib.internal.repository.RepoSource;
import java.util.ArrayList;
import java.util.Arrays;
@@ -37,7 +38,10 @@ public class UpdaterLogicTest extends TestCase {
}
@Override
protected void fetchRemotePackages(ArrayList<Package> remotePkgs) {
protected void fetchRemotePackages(ArrayList<Package> remotePkgs,
RepoSource[] remoteSources) {
// Ignore remoteSources and instead uses the remotePackages list given to the
// constructor.
if (mRemotePackages != null) {
remotePkgs.addAll(Arrays.asList(mRemotePackages));
}
@@ -59,13 +63,14 @@ public class UpdaterLogicTest extends TestCase {
// a2 depends on p2, which is not in the locals
Package[] locals = { p1, a1 };
assertNull(mul.findPlatformDependency(a2, out, selected, remote, locals));
RepoSource[] sources = null;
assertNull(mul.findPlatformDependency(a2, out, selected, remote, sources, locals));
assertEquals(0, out.size());
// p2 is now selected, and should be scheduled for install in out
Archive p2_archive = p2.getArchives()[0];
selected.add(p2_archive);
ArchiveInfo ai2 = mul.findPlatformDependency(a2, out, selected, remote, locals);
ArchiveInfo ai2 = mul.findPlatformDependency(a2, out, selected, remote, sources, locals);
assertNotNull(ai2);
assertSame(p2_archive, ai2.getNewArchive());
assertEquals(1, out.size());
@@ -86,13 +91,14 @@ public class UpdaterLogicTest extends TestCase {
// p2 depends on t2, which is not locally installed
Package[] locals = { t1 };
assertNull(mul.findToolsDependency(p2, out, selected, remote, locals));
RepoSource[] sources = null;
assertNull(mul.findToolsDependency(p2, out, selected, remote, sources, locals));
assertEquals(0, out.size());
// t2 is now selected and can be used as a dependency
Archive t2_archive = t2.getArchives()[0];
selected.add(t2_archive);
ArchiveInfo ai2 = mul.findToolsDependency(p2, out, selected, remote, locals);
ArchiveInfo ai2 = mul.findToolsDependency(p2, out, selected, remote, sources, locals);
assertNotNull(ai2);
assertSame(t2_archive, ai2.getNewArchive());
assertEquals(1, out.size());