From ef120bea7b8cb3b891ae3f39d7a97989c107a16f Mon Sep 17 00:00:00 2001 From: Satoshi Kataoka Date: Fri, 19 Apr 2013 01:09:02 +0900 Subject: [PATCH] Fix sort order of InputMethodSubtype Bug: 8638372 Change-Id: I65d30f968708dc836635f687aa51b51816daccdb --- .../InputMethodAndSubtypeEnabler.java | 18 ++++++++--- .../inputmethod/InputMethodPreference.java | 32 ++++++++++++++----- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/com/android/settings/inputmethod/InputMethodAndSubtypeEnabler.java b/src/com/android/settings/inputmethod/InputMethodAndSubtypeEnabler.java index 13f44358ccf..f3addf367cf 100644 --- a/src/com/android/settings/inputmethod/InputMethodAndSubtypeEnabler.java +++ b/src/com/android/settings/inputmethod/InputMethodAndSubtypeEnabler.java @@ -36,10 +36,13 @@ import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodSubtype; +import java.text.Collator; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; +import java.util.Locale; public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment { private static final String TAG =InputMethodAndSubtypeEnabler.class.getSimpleName(); @@ -54,6 +57,7 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment { private String mInputMethodId; private String mTitle; private String mSystemLocale = ""; + private Collator mCollator = Collator.getInstance(); @Override public void onCreate(Bundle icicle) { @@ -84,7 +88,9 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment { } } - mSystemLocale = config.locale.toString(); + final Locale locale = config.locale; + mSystemLocale = locale.toString(); + mCollator = Collator.getInstance(locale); onCreateIMM(); setPreferenceScreen(createPreferenceHierarchy()); } @@ -259,7 +265,7 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment { } } else { final CheckBoxPreference chkbxPref = new SubtypeCheckBoxPreference( - context, subtype.getLocale(), mSystemLocale); + context, subtype.getLocale(), mSystemLocale, mCollator); chkbxPref.setKey(imiId + subtype.hashCode()); chkbxPref.setTitle(subtypeLabel); subtypePreferences.add(chkbxPref); @@ -370,9 +376,10 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment { private static class SubtypeCheckBoxPreference extends CheckBoxPreference { private final boolean mIsSystemLocale; private final boolean mIsSystemLanguage; + private final Collator mCollator; public SubtypeCheckBoxPreference( - Context context, String subtypeLocale, String systemLocale) { + Context context, String subtypeLocale, String systemLocale, Collator collator) { super(context); if (TextUtils.isEmpty(subtypeLocale)) { mIsSystemLocale = false; @@ -382,6 +389,7 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment { mIsSystemLanguage = mIsSystemLocale || subtypeLocale.startsWith(systemLocale.substring(0, 2)); } + mCollator = collator; } @Override @@ -411,10 +419,10 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment { if (TextUtils.isEmpty(t1)) { return -1; } - return t0.toString().compareTo(t1.toString()); + return mCollator.compare(t0.toString(), t1.toString()); } else { Log.w(TAG, "Illegal preference type."); - return -1; + return super.compareTo(p); } } } diff --git a/src/com/android/settings/inputmethod/InputMethodPreference.java b/src/com/android/settings/inputmethod/InputMethodPreference.java index f064c08a2a3..56b6e673850 100644 --- a/src/com/android/settings/inputmethod/InputMethodPreference.java +++ b/src/com/android/settings/inputmethod/InputMethodPreference.java @@ -28,6 +28,7 @@ import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.preference.CheckBoxPreference; +import android.preference.Preference; import android.preference.PreferenceActivity; import android.provider.Settings; import android.text.TextUtils; @@ -42,11 +43,11 @@ import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; +import java.text.Collator; import java.util.Comparator; import java.util.List; -public class InputMethodPreference extends CheckBoxPreference - implements Comparator { +public class InputMethodPreference extends CheckBoxPreference { private static final String TAG = InputMethodPreference.class.getSimpleName(); private final SettingsPreferenceFragment mFragment; private final InputMethodInfo mImi; @@ -54,6 +55,7 @@ public class InputMethodPreference extends CheckBoxPreference private final Intent mSettingsIntent; private final boolean mAlwaysChecked; private final boolean mIsSystemIme; + private final Collator mCollator; private AlertDialog mDialog = null; private ImageView mInputMethodSettingsButton; @@ -95,6 +97,7 @@ public class InputMethodPreference extends CheckBoxPreference if (mAlwaysChecked) { setEnabled(false); } + mCollator = Collator.getInstance(fragment.getResources().getConfiguration().locale); } @Override @@ -276,13 +279,26 @@ public class InputMethodPreference extends CheckBoxPreference } @Override - public int compare(InputMethodPreference arg0, InputMethodPreference arg1) { - if (arg0.isEnabled() == arg0.isEnabled()) { - return arg0.mImi.getId().compareTo(arg1.mImi.getId()); - } else { - // Prefer system IMEs - return arg0.isEnabled() ? 1 : -1; + public int compareTo(Preference p) { + if (!(p instanceof InputMethodPreference)) { + return super.compareTo(p); } + final InputMethodPreference imp = (InputMethodPreference) p; + final boolean priority0 = mIsSystemIme && mAlwaysChecked; + final boolean priority1 = imp.mIsSystemIme && imp.mAlwaysChecked; + if (priority0 == priority1) { + final CharSequence t0 = getTitle(); + final CharSequence t1 = imp.getTitle(); + if (TextUtils.isEmpty(t0)) { + return 1; + } + if (TextUtils.isEmpty(t1)) { + return -1; + } + return mCollator.compare(t0.toString(), t1.toString()); + } + // Prefer always checked system IMEs + return priority0 ? -1 : 1; } private void saveImeSettings() {