Modify Comparator in ZonePicker
The previous method "compareTo" isn't locale-sensitive, it cannot sort the String in some other languages. So Collator is recommended to be applyed here. Bug: Sort doesn't work in some languages. Test: make RunSettingsRoboTests -j40 ROBOTEST_FILTER=ZonePickerComparatorTest Change-Id: Id107ab938cceefc77f9fb6918a0445fc92a0fcb7 Signed-off-by: tiansiming <tiansiming@xiaomi.com>
This commit is contained in:
@@ -22,6 +22,7 @@ import android.app.AlarmManager;
|
||||
import android.app.ListFragment;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
@@ -38,6 +39,7 @@ import com.android.settings.core.instrumentation.Instrumentable;
|
||||
import com.android.settings.core.instrumentation.VisibilityLoggerMixin;
|
||||
import com.android.settingslib.datetime.ZoneGetter;
|
||||
|
||||
import java.text.Collator;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
@@ -257,15 +259,21 @@ public class ZonePicker extends ListFragment implements Instrumentable {
|
||||
mVisibilityLoggerMixin.onPause();
|
||||
}
|
||||
|
||||
private static class MyComparator implements Comparator<Map<?, ?>> {
|
||||
@VisibleForTesting
|
||||
static class MyComparator implements Comparator<Map<?, ?>> {
|
||||
private final Collator mCollator;
|
||||
private String mSortingKey;
|
||||
private boolean mSortedByName;
|
||||
|
||||
public MyComparator(String sortingKey) {
|
||||
mCollator = Collator.getInstance();
|
||||
mSortingKey = sortingKey;
|
||||
mSortedByName = ZoneGetter.KEY_DISPLAY_LABEL.equals(sortingKey);
|
||||
}
|
||||
|
||||
public void setSortingKey(String sortingKey) {
|
||||
mSortingKey = sortingKey;
|
||||
mSortedByName = ZoneGetter.KEY_DISPLAY_LABEL.equals(sortingKey);
|
||||
}
|
||||
|
||||
public int compare(Map<?, ?> map1, Map<?, ?> map2) {
|
||||
@@ -282,7 +290,11 @@ public class ZonePicker extends ListFragment implements Instrumentable {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ((Comparable) value1).compareTo(value2);
|
||||
if (mSortedByName) {
|
||||
return mCollator.compare(value1, value2);
|
||||
} else {
|
||||
return ((Comparable) value1).compareTo(value2);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isComparable(Object value) {
|
||||
|
Reference in New Issue
Block a user