(DO NOT MERGE) Refactor InputMethodAndSubtypeEnabler to separate loops

Change-Id: I1b7d26c37293e03dc20f87d60eb146017a574c47
(cherry picked from commit 61d97ddda8)
This commit is contained in:
Tadashi G. Takaoka
2014-07-14 13:06:33 +09:00
parent 989c364f2d
commit fc8202463e

View File

@@ -51,7 +51,6 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
private InputMethodManager mImm;
// TODO: Change mInputMethodInfoList to Map
private List<InputMethodInfo> mInputMethodInfoList;
private String mInputMethodId;
private Collator mCollator;
private AlertDialog mDialog = null;
@@ -66,12 +65,22 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
// single Activity (see InputMethodAndSubtypeEnablerActivity). It should be available
// from a preference argument when the preference is launched as a part of the other
// Activity (like a right pane of 2-pane Settings app)
mInputMethodId = getStringExtraFromIntentOrArguments(
final String targetImi = getStringExtraFromIntentOrArguments(
android.provider.Settings.EXTRA_INPUT_METHOD_ID);
mInputMethodInfoList = mImm.getInputMethodList();
mCollator = Collator.getInstance();
setPreferenceScreen(createPreferenceHierarchy());
final PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getActivity());
final int imiCount = mInputMethodInfoList.size();
for (int index = 0; index < imiCount; ++index) {
final InputMethodInfo imi = mInputMethodInfoList.get(index);
// Add subtype preferences of this IME when it is specified or no IME is specified.
if (imi.getId().equals(targetImi) || TextUtils.isEmpty(targetImi)) {
addInputMethodSubtypePreferences(imi, root);
}
}
setPreferenceScreen(root);
}
private String getStringExtraFromIntentOrArguments(final String name) {
@@ -114,74 +123,78 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
mInputMethodInfoList, mHaveHardKeyboard);
}
// TODO: Stop overriding this method. Instead start using {@link OnPreferenceChangedListener}.
@Override
public boolean onPreferenceTreeClick(final PreferenceScreen preferenceScreen,
final Preference preference) {
if (preference instanceof CheckBoxPreference) {
if (!(preference instanceof CheckBoxPreference)) {
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
final CheckBoxPreference chkPref = (CheckBoxPreference) preference;
for (final String imiId : mAutoSelectionPrefsMap.keySet()) {
if (mAutoSelectionPrefsMap.get(imiId) == chkPref) {
// We look for the first preference item in subtype enabler.
// The first item is used for turning on/off subtype auto selection.
// We are in the subtype enabler and trying selecting subtypes automatically.
// We look for the first preference item in subtype enabler. The first item is used
// for turning on/off subtype auto selection. We are in the subtype enabler and
// trying selecting subtypes automatically.
setAutoSelectionSubtypesEnabled(imiId, chkPref.isChecked());
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
}
final String id = chkPref.getKey();
if (chkPref.isChecked()) {
// Turns off a subtype.
if (!chkPref.isChecked()) {
// TODO: Because no preference on this screen has {@link InputMethodInfo} id as a key,
// the following setSubtypesPreferenceEnabled call is effectively no-operation and
// can be removed.
InputMethodAndSubtypeUtil.setSubtypesPreferenceEnabled(
this, mInputMethodInfoList, id, false);
updateAutoSelectionPreferences();
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
// Turns on a subtype.
final InputMethodInfo imi = getInputMethodInfoById(id);
// TODO: Because no preference on this screen has {@link InputMethodInfo} id as a key,
// <code>imi</code> is always null and the following code can be removed.
if (imi == null) {
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
// Turns on a system IME's subtype.
if (InputMethodUtils.isSystemIme(imi)) {
InputMethodAndSubtypeUtil.setSubtypesPreferenceEnabled(
this, mInputMethodInfoList, id, true);
// This is a built-in IME, so no need to warn.
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
// Turns on a 3rd party IME's subtype.
// Turns off a subtype before showing a security warning dialog.
chkPref.setChecked(false);
if (mDialog == null) {
mDialog = (new AlertDialog.Builder(getActivity()))
.setTitle(android.R.string.dialog_alert_title)
.setCancelable(true)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
chkPref.setChecked(true);
InputMethodAndSubtypeUtil.setSubtypesPreferenceEnabled(
InputMethodAndSubtypeEnabler.this,
mInputMethodInfoList, id, true);
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
} else {
if (mDialog.isShowing()) {
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
}
mDialog.setMessage(getResources().getString(
R.string.ime_security_warning,
imi.getServiceInfo().applicationInfo.loadLabel(getPackageManager())));
mDialog.show();
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(android.R.string.dialog_alert_title);
final CharSequence label = imi.getServiceInfo().applicationInfo
.loadLabel(getPackageManager());
builder.setMessage(getString(R.string.ime_security_warning, label));
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
// The user explicitly enable the subtype.
chkPref.setChecked(true);
InputMethodAndSubtypeUtil.setSubtypesPreferenceEnabled(
this, mInputMethodInfoList, id, false);
updateAutoSelectionPreferences();
}
InputMethodAndSubtypeEnabler.this, mInputMethodInfoList, id, true);
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {}
});
mDialog = builder.create();
mDialog.show();
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
@@ -205,24 +218,14 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
}
}
private PreferenceScreen createPreferenceHierarchy() {
// Root
final PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getActivity());
private void addInputMethodSubtypePreferences(final InputMethodInfo imi,
final PreferenceScreen root) {
final Context context = getActivity();
final int imiCount = (mInputMethodInfoList == null ? 0 : mInputMethodInfoList.size());
for (int imiIndex = 0; imiIndex < imiCount; ++imiIndex) {
final InputMethodInfo imi = mInputMethodInfoList.get(imiIndex);
final int subtypeCount = imi.getSubtypeCount();
if (subtypeCount <= 1) {
continue;
return;
}
final String imiId = imi.getId();
// Add this subtype to the list when no IME is specified or when the IME of this
// subtype is the specified IME.
if (!TextUtils.isEmpty(mInputMethodId) && !mInputMethodId.equals(imiId)) {
continue;
}
final PreferenceCategory keyboardSettingsCategory = new PreferenceCategory(context);
root.addPreference(keyboardSettingsCategory);
final PackageManager pm = getPackageManager();
@@ -245,8 +248,8 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
final InputMethodSubtype subtype = imi.getSubtypeAt(index);
if (subtype.overridesImplicitlyEnabledSubtype()) {
if (autoSubtypeLabel == null) {
autoSubtypeLabel = subtype.getDisplayName(context,
imi.getPackageName(), imi.getServiceInfo().applicationInfo);
autoSubtypeLabel = subtype.getDisplayName(
context, imi.getPackageName(), imi.getServiceInfo().applicationInfo);
}
} else {
final Preference subtypePref = new InputMethodSubtypePreference(
@@ -258,7 +261,7 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
@Override
public int compare(final Preference lhs, final Preference rhs) {
if (lhs instanceof InputMethodSubtypePreference) {
return ((InputMethodSubtypePreference)lhs).compareTo(rhs, mCollator);
return ((InputMethodSubtypePreference) lhs).compareTo(rhs, mCollator);
}
return lhs.compareTo(rhs);
}
@@ -276,8 +279,6 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
autoSelectionPref.setTitle(autoSubtypeLabel);
}
}
return root;
}
private boolean isNoSubtypesExplicitlySelected(final String imiId) {
final List<Preference> subtypePrefs = mInputMethodAndSubtypePrefsMap.get(imiId);
@@ -319,20 +320,25 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
// When targetImiId is null, apply to all subtypes of all IMEs
for (final InputMethodInfo imi : mInputMethodInfoList) {
final String imiId = imi.getId();
if (targetImiId != null && !targetImiId.equals(imiId)) {
continue;
}
final CheckBoxPreference autoSelectionPref = mAutoSelectionPrefsMap.get(imiId);
// No need to update implicitly enabled subtypes when the user has unchecked the
// "subtype auto selection".
if (autoSelectionPref == null || !autoSelectionPref.isChecked()) {
continue;
}
if (imiId.equals(targetImiId) || targetImiId == null) {
updateImplicitlyEnabledSubtypesOf(imi, check);
}
}
}
private void updateImplicitlyEnabledSubtypesOf(final InputMethodInfo imi, final boolean check) {
final String imiId = imi.getId();
final List<Preference> subtypePrefs = mInputMethodAndSubtypePrefsMap.get(imiId);
final List<InputMethodSubtype> implicitlyEnabledSubtypes =
mImm.getEnabledInputMethodSubtypeList(imi, true);
if (subtypePrefs == null || implicitlyEnabledSubtypes == null) {
continue;
return;
}
for (final Preference pref : subtypePrefs) {
if (!(pref instanceof CheckBoxPreference)) {
@@ -342,7 +348,7 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
subtypePref.setChecked(false);
if (check) {
for (final InputMethodSubtype subtype : implicitlyEnabledSubtypes) {
String implicitlyEnabledSubtypePrefKey = imiId + subtype.hashCode();
final String implicitlyEnabledSubtypePrefKey = imiId + subtype.hashCode();
if (subtypePref.getKey().equals(implicitlyEnabledSubtypePrefKey)) {
subtypePref.setChecked(true);
break;
@@ -351,7 +357,6 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
}
}
}
}
private void updateAutoSelectionPreferences() {
for (final String imiId : mInputMethodAndSubtypePrefsMap.keySet()) {