Remove Collator from InputMethodSubtypePreference

Change-Id: Id1eb16aee77460c5c930ad08e1354501bfd0b5ab
This commit is contained in:
Tadashi G. Takaoka
2014-07-12 15:55:05 +09:00
parent 47a359a7da
commit 55aee12406
3 changed files with 33 additions and 38 deletions

View File

@@ -16,10 +16,6 @@
package com.android.settings.inputmethod; package com.android.settings.inputmethod;
import com.android.internal.inputmethod.InputMethodUtils;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@@ -37,12 +33,16 @@ import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import com.android.internal.inputmethod.InputMethodUtils;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import java.text.Collator; import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment { public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
private static final String TAG = InputMethodAndSubtypeEnabler.class.getSimpleName(); private static final String TAG = InputMethodAndSubtypeEnabler.class.getSimpleName();
@@ -52,11 +52,10 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
new HashMap<>(); new HashMap<>();
final private HashMap<String, CheckBoxPreference> mSubtypeAutoSelectionCBMap = new HashMap<>(); final private HashMap<String, CheckBoxPreference> mSubtypeAutoSelectionCBMap = new HashMap<>();
private InputMethodManager mImm; private InputMethodManager mImm;
// TODO: Change mInputMethodProperties to Map
private List<InputMethodInfo> mInputMethodProperties; private List<InputMethodInfo> mInputMethodProperties;
private String mInputMethodId; private String mInputMethodId;
private String mTitle; private String mTitle;
private String mSystemLocale = "";
private Collator mCollator = Collator.getInstance();
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
@@ -87,10 +86,7 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
} }
} }
final Locale locale = config.locale; mInputMethodProperties = mImm.getInputMethodList();
mSystemLocale = locale.toString();
mCollator = Collator.getInstance(locale);
onCreateIMM();
setPreferenceScreen(createPreferenceHierarchy()); setPreferenceScreen(createPreferenceHierarchy());
} }
@@ -211,21 +207,13 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
} }
} }
private void onCreateIMM() {
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
// TODO: Change mInputMethodProperties to Map
mInputMethodProperties = imm.getInputMethodList();
}
private PreferenceScreen createPreferenceHierarchy() { private PreferenceScreen createPreferenceHierarchy() {
// Root // Root
final PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getActivity()); final PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getActivity());
final Context context = getActivity(); final Context context = getActivity();
int N = (mInputMethodProperties == null ? 0 : mInputMethodProperties.size()); final Collator collator = Collator.getInstance();
final int N = (mInputMethodProperties == null ? 0 : mInputMethodProperties.size());
for (int i = 0; i < N; ++i) { for (int i = 0; i < N; ++i) {
final InputMethodInfo imi = mInputMethodProperties.get(i); final InputMethodInfo imi = mInputMethodProperties.get(i);
final int subtypeCount = imi.getSubtypeCount(); final int subtypeCount = imi.getSubtypeCount();
@@ -267,13 +255,21 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
} }
} else { } else {
final CheckBoxPreference chkbxPref = new InputMethodSubtypePreference( final CheckBoxPreference chkbxPref = new InputMethodSubtypePreference(
context, subtype, imi, mCollator); context, subtype, imi);
chkbxPref.setKey(imiId + subtype.hashCode()); chkbxPref.setKey(imiId + subtype.hashCode());
chkbxPref.setTitle(subtypeLabel); chkbxPref.setTitle(subtypeLabel);
subtypePreferences.add(chkbxPref); subtypePreferences.add(chkbxPref);
} }
} }
Collections.sort(subtypePreferences); Collections.sort(subtypePreferences, new Comparator<Preference>() {
@Override
public int compare(Preference lhs, Preference rhs) {
if (lhs instanceof InputMethodSubtypePreference) {
return ((InputMethodSubtypePreference)lhs).compareTo(rhs, collator);
}
return lhs.compareTo(rhs);
}
});
for (int j = 0; j < subtypePreferences.size(); ++j) { for (int j = 0; j < subtypePreferences.size(); ++j) {
activeInputMethodsCategory.addPreference(subtypePreferences.get(j)); activeInputMethodsCategory.addPreference(subtypePreferences.get(j));
} }

View File

@@ -231,6 +231,9 @@ class InputMethodPreference extends SwitchPreference implements OnPreferenceClic
} }
int compareTo(final InputMethodPreference rhs, final Collator collator) { int compareTo(final InputMethodPreference rhs, final Collator collator) {
if (this == rhs) {
return 0;
}
if (mHasPriorityInSorting == rhs.mHasPriorityInSorting) { if (mHasPriorityInSorting == rhs.mHasPriorityInSorting) {
final CharSequence t0 = getTitle(); final CharSequence t0 = getTitle();
final CharSequence t1 = rhs.getTitle(); final CharSequence t1 = rhs.getTitle();

View File

@@ -20,7 +20,6 @@ import android.content.Context;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.Preference; import android.preference.Preference;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
@@ -36,14 +35,11 @@ import java.util.Locale;
*/ */
//TODO: Make this non-persistent. //TODO: Make this non-persistent.
class InputMethodSubtypePreference extends CheckBoxPreference { class InputMethodSubtypePreference extends CheckBoxPreference {
private static final String TAG = InputMethodSubtypePreference.class.getSimpleName();
private final boolean mIsSystemLocale; private final boolean mIsSystemLocale;
private final boolean mIsSystemLanguage; private final boolean mIsSystemLanguage;
private final Collator mCollator;
public InputMethodSubtypePreference(final Context context, final InputMethodSubtype subtype, InputMethodSubtypePreference(final Context context, final InputMethodSubtype subtype,
final InputMethodInfo imi, final Collator collator) { final InputMethodInfo imi) {
super(context); super(context);
setKey(imi.getId() + subtype.hashCode()); setKey(imi.getId() + subtype.hashCode());
final CharSequence subtypeLabel = subtype.getDisplayName(context, final CharSequence subtypeLabel = subtype.getDisplayName(context,
@@ -60,15 +56,16 @@ class InputMethodSubtypePreference extends CheckBoxPreference {
|| InputMethodUtils.getLanguageFromLocaleString(subtypeLocaleString) || InputMethodUtils.getLanguageFromLocaleString(subtypeLocaleString)
.equals(systemLocale.getLanguage()); .equals(systemLocale.getLanguage());
} }
mCollator = collator;
} }
@Override int compareTo(final Preference rhs, final Collator collator) {
public int compareTo(Preference p) { if (this == rhs) {
if (p instanceof InputMethodSubtypePreference) { return 0;
final InputMethodSubtypePreference pref = ((InputMethodSubtypePreference)p); }
if (rhs instanceof InputMethodSubtypePreference) {
final InputMethodSubtypePreference pref = (InputMethodSubtypePreference) rhs;
final CharSequence t0 = getTitle(); final CharSequence t0 = getTitle();
final CharSequence t1 = pref.getTitle(); final CharSequence t1 = rhs.getTitle();
if (TextUtils.equals(t0, t1)) { if (TextUtils.equals(t0, t1)) {
return 0; return 0;
} }
@@ -90,9 +87,8 @@ class InputMethodSubtypePreference extends CheckBoxPreference {
if (TextUtils.isEmpty(t1)) { if (TextUtils.isEmpty(t1)) {
return -1; return -1;
} }
return mCollator.compare(t0.toString(), t1.toString()); return collator.compare(t0.toString(), t1.toString());
} }
Log.w(TAG, "Illegal preference type."); return super.compareTo(rhs);
return super.compareTo(p);
} }
} }