From dffcc39ba5cd82bb28e06f0664a3ac8c5f31c33f Mon Sep 17 00:00:00 2001 From: Jia Arlan Date: Fri, 11 May 2012 16:27:07 +0800 Subject: [PATCH] Fix of Settings application sort crash The original implementation of compareTo function will sometimes cause crash when the power usage list above 32 items. When comparing double values a proper comparison method needs to be used. Using just subtraction does not take into account NaN:S, infinities and +/-0 numbers. In certain cirtumstances it seems that using subtraction causes compareTo to return values that is not expected by the sorting code and causes an illegal argument exception with "Comparison method violates its general contract!". This problem only happens if the sort code is called arrays containing more than 32 (currently) due to how ComparableTimSort works (call chain is Collections.sort -> Arrays.sort(Object[]) -> ComparableTimSort. Change-Id: If732f04797a3c8b2a43568c90bb73a1ec69a4c98 --- src/com/android/settings/fuelgauge/BatterySipper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/android/settings/fuelgauge/BatterySipper.java b/src/com/android/settings/fuelgauge/BatterySipper.java index f11d6cabdd7..ffc6651f7dc 100644 --- a/src/com/android/settings/fuelgauge/BatterySipper.java +++ b/src/com/android/settings/fuelgauge/BatterySipper.java @@ -93,7 +93,7 @@ class BatterySipper implements Comparable { public int compareTo(BatterySipper other) { // Return the flipped value because we want the items in descending order - return (int) (other.getSortValue() - getSortValue()); + return Double.compare(other.getSortValue(), getSortValue()); } void getQuickNameIconForUid(Uid uidObj) {