From 2b3a9a7edf8465fd1cc87d03865e66c9918e45cb Mon Sep 17 00:00:00 2001 From: Xavier Ducrohet Date: Fri, 4 Dec 2009 19:07:02 -0800 Subject: [PATCH] AVD Selector now sort AVDs Also fixed the IAndroidTarget comparison to sort first by revision and then per platform or addon and then per vendor/name (for add-ons) BUG 2302823 --- .../sdklib/src/com/android/sdklib/AddOnTarget.java | 43 +++++++++++----------- .../src/com/android/sdklib/AndroidVersion.java | 37 ++++++++++++++++++- .../src/com/android/sdklib/PlatformTarget.java | 22 ++++++----- .../android/sdklib/internal/avd/AvdManager.java | 31 +++++++++++++++- .../sdkuilib/internal/widgets/AvdSelector.java | 8 ++++ 5 files changed, 108 insertions(+), 33 deletions(-) diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/AddOnTarget.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/AddOnTarget.java index f5fcf9024..918409957 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/AddOnTarget.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/AddOnTarget.java @@ -269,38 +269,39 @@ final class AddOnTarget implements IAndroidTarget { } /* - * Always return +1 if the object we compare to is a platform. - * Otherwise, do vendor then name then api version comparison. + * Order by API level (preview/n count as between n and n+1). + * At the same API level, order as: Platform first, then add-on ordered by vendor and then name * (non-Javadoc) * @see java.lang.Comparable#compareTo(java.lang.Object) */ public int compareTo(IAndroidTarget target) { - if (target.isPlatform()) { - return +1; + // quick check. + if (this == target) { + return 0; } - // compare vendor - int value = mVendor.compareTo(target.getVendor()); + int versionDiff = getVersion().compareTo(target.getVersion()); - // if same vendor, compare name - if (value == 0) { - value = mName.compareTo(target.getName()); - } - - // if same vendor/name, compare version - if (value == 0) { - if (getVersion().isPreview() == true) { - value = target.getVersion().isPreview() == true ? - getVersion().getApiString().compareTo(target.getVersion().getApiString()) : - +1; // put the preview at the end. + // only if the version are the same do we care about platform/add-ons. + if (versionDiff == 0) { + // platforms go before add-ons. + if (target.isPlatform()) { + return +1; } else { - value = target.getVersion().isPreview() == true ? - -1 : // put the preview at the end : - getVersion().getApiLevel() - target.getVersion().getApiLevel(); + AddOnTarget targetAddOn = (AddOnTarget)target; + + // both are add-ons of the same version. Compare per vendor then by name + int vendorDiff = mVendor.compareTo(targetAddOn.mVendor); + if (vendorDiff == 0) { + return mName.compareTo(targetAddOn.mName); + } else { + return vendorDiff; + } } + } - return value; + return versionDiff; } // ---- local methods. diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/AndroidVersion.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/AndroidVersion.java index 13c3ea19a..6ac76c5b2 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/AndroidVersion.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/AndroidVersion.java @@ -37,7 +37,7 @@ import java.util.Properties; * For generic UI display of the API version, {@link #getApiString()} is to be used. * */ -public class AndroidVersion { +public final class AndroidVersion implements Comparable { private static final String PROP_API_LEVEL = "AndroidVersion.ApiLevel"; //$NON-NLS-1$ private static final String PROP_CODENAME = "AndroidVersion.CodeName"; //$NON-NLS-1$ @@ -200,4 +200,39 @@ public class AndroidVersion { // but it's acceptable. return mApiLevel; } + + /** + * Compares this object with the specified object for order. Returns a + * negative integer, zero, or a positive integer as this object is less + * than, equal to, or greater than the specified object. + * + * @param o the Object to be compared. + * @return a negative integer, zero, or a positive integer as this object is + * less than, equal to, or greater than the specified object. + */ + public int compareTo(AndroidVersion o) { + if (mCodename == null) { + if (o.mCodename == null) { + return mApiLevel - o.mApiLevel; + } else { + if (mApiLevel == o.mApiLevel) { + return -1; // same api level but o is a preview for next version + } + + return mApiLevel - o.mApiLevel; + } + } else { + // 'this' is a preview + if (mApiLevel == o.mApiLevel) { + if (o.mCodename == null) { + return +1; + } else { + return mCodename.compareTo(o.mCodename); // strange case where the 2 previews + // have different codename? + } + } else { + return mApiLevel - o.mApiLevel; + } + } + } } diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/PlatformTarget.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/PlatformTarget.java index d0f009f51..7c013b012 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/PlatformTarget.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/PlatformTarget.java @@ -229,26 +229,28 @@ final class PlatformTarget implements IAndroidTarget { } /* - * Always return -1 if the object we compare to is an addon. - * Otherwise, compare api level. + * Order by API level (preview/n count as between n and n+1). + * At the same API level, order as: Platform first, then add-on ordered by vendor and then name * (non-Javadoc) * @see java.lang.Comparable#compareTo(java.lang.Object) */ public int compareTo(IAndroidTarget target) { - if (target.isPlatform() == false) { - return -1; + // quick check. + if (this == target) { + return 0; } - int apiDiff = mVersion.getApiLevel() - target.getVersion().getApiLevel(); + int versionDiff = mVersion.compareTo(target.getVersion()); - if (mVersion.getCodename() != null && apiDiff == 0) { - if (target.getVersion().getCodename() == null) { - return +1; // preview showed last + // only if the version are the same do we care about add-ons. + if (versionDiff == 0) { + // platforms go before add-ons. + if (target.isPlatform() == false) { + return -1; } - return mVersion.getCodename().compareTo(target.getVersion().getCodename()); } - return apiDiff; + return versionDiff; } // ---- platform only methods. diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java index f5e7c1a2e..9499798b8 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java @@ -138,7 +138,7 @@ public final class AvdManager { public final static String HARDWARE_INI = "hardware.ini"; //$NON-NLS-1$ /** An immutable structure describing an Android Virtual Device. */ - public static final class AvdInfo { + public static final class AvdInfo implements Comparable { /** * Status for an {@link AvdInfo}. Indicates whether or not this AVD is valid. @@ -306,6 +306,35 @@ public final class AvdManager { return null; } + + /** + * Compares this object with the specified object for order. Returns a + * negative integer, zero, or a positive integer as this object is less + * than, equal to, or greater than the specified object. + * + * @param o the Object to be compared. + * @return a negative integer, zero, or a positive integer as this object is + * less than, equal to, or greater than the specified object. + */ + public int compareTo(AvdInfo o) { + // first handle possible missing targets (if the AVD failed to load for + // unresolved targets. + if (mTarget == null) { + return +1; + } else if (o.mTarget == null) { + return -1; + } + + // then compare the targets + int targetDiff = mTarget.compareTo(o.mTarget); + + if (targetDiff == 0) { + // same target? compare on the avd name + return mName.compareTo(o.mName); + } + + return targetDiff; + } } private final ArrayList mAllAvdList = new ArrayList(); diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java index 533871780..c302cd645 100644 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java @@ -59,6 +59,8 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; /** @@ -717,6 +719,12 @@ public final class AvdSelector { } if (avds != null && avds.length > 0) { + Arrays.sort(avds, new Comparator() { + public int compare(AvdInfo o1, AvdInfo o2) { + return o1.compareTo(o2); + } + }); + table.setEnabled(true); if (mTargetFilter != null) { -- 2.11.0