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