OSDN Git Service

AVD Selector now sort AVDs
authorXavier Ducrohet <xav@android.com>
Sat, 5 Dec 2009 03:07:02 +0000 (19:07 -0800)
committerXavier Ducrohet <xav@android.com>
Sat, 5 Dec 2009 03:11:22 +0000 (19:11 -0800)
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

sdkmanager/libs/sdklib/src/com/android/sdklib/AddOnTarget.java
sdkmanager/libs/sdklib/src/com/android/sdklib/AndroidVersion.java
sdkmanager/libs/sdklib/src/com/android/sdklib/PlatformTarget.java
sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java
sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java

index f5fcf90..9184099 100644 (file)
@@ -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.
index 13c3ea1..6ac76c5 100644 (file)
@@ -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<AndroidVersion> {
 
     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;
+            }
+        }
+    }
 }
index d0f009f..7c013b0 100644 (file)
@@ -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.
index f5e7c1a..9499798 100644 (file)
@@ -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<AvdInfo> {
 
         /**
          * 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<AvdInfo> mAllAvdList = new ArrayList<AvdInfo>();
index 5338717..c302cd6 100644 (file)
@@ -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<AvdInfo>() {
+                public int compare(AvdInfo o1, AvdInfo o2) {
+                    return o1.compareTo(o2);
+                }
+            });
+
             table.setEnabled(true);
 
             if (mTargetFilter != null) {