Fix sort order of InputMethodSubtype

Bug: 8638372
Change-Id: I65d30f968708dc836635f687aa51b51816daccdb
This commit is contained in:
Satoshi Kataoka
2013-04-19 01:09:02 +09:00
parent 1a72d9c49b
commit ef120bea7b
2 changed files with 37 additions and 13 deletions

View File

@@ -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);
}
}
}

View File

@@ -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<InputMethodPreference> {
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() {