}
/*
- * 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.
* 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$
// 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;
+ }
+ }
+ }
}
}
/*
- * 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.
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.
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>();
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
/**
}
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) {