Use OnPreferenceChangeLister for subtype enabler
Change-Id: Id9eaed14803b6c4e194f1cf62900de0a053da012
This commit is contained in:
@@ -23,6 +23,7 @@ import android.content.res.Configuration;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.CheckBoxPreference;
|
import android.preference.CheckBoxPreference;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
|
import android.preference.Preference.OnPreferenceChangeListener;
|
||||||
import android.preference.PreferenceCategory;
|
import android.preference.PreferenceCategory;
|
||||||
import android.preference.PreferenceScreen;
|
import android.preference.PreferenceScreen;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
@@ -40,7 +41,8 @@ import java.util.Comparator;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
|
public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment
|
||||||
|
implements OnPreferenceChangeListener {
|
||||||
private boolean mHaveHardKeyboard;
|
private boolean mHaveHardKeyboard;
|
||||||
private final HashMap<String, List<Preference>> mInputMethodAndSubtypePrefsMap =
|
private final HashMap<String, List<Preference>> mInputMethodAndSubtypePrefsMap =
|
||||||
new HashMap<>();
|
new HashMap<>();
|
||||||
@@ -119,32 +121,34 @@ 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 onPreferenceChange(final Preference pref, final Object newValue) {
|
||||||
final Preference preference) {
|
if (!(newValue instanceof Boolean)) {
|
||||||
if (!(preference instanceof CheckBoxPreference)) {
|
return true; // Invoke default behavior.
|
||||||
return super.onPreferenceTreeClick(preferenceScreen, preference);
|
|
||||||
}
|
}
|
||||||
final CheckBoxPreference chkPref = (CheckBoxPreference) preference;
|
final boolean isChecking = (Boolean) newValue;
|
||||||
|
|
||||||
for (final String imiId : mAutoSelectionPrefsMap.keySet()) {
|
for (final String imiId : mAutoSelectionPrefsMap.keySet()) {
|
||||||
if (mAutoSelectionPrefsMap.get(imiId) == chkPref) {
|
// An auto select subtype preference is changing.
|
||||||
// We look for the first preference item in subtype enabler. The first item is used
|
if (mAutoSelectionPrefsMap.get(imiId) == pref) {
|
||||||
// for turning on/off subtype auto selection. We are in the subtype enabler and
|
final CheckBoxPreference autoSelectionPref = (CheckBoxPreference) pref;
|
||||||
// trying selecting subtypes automatically.
|
autoSelectionPref.setChecked(isChecking);
|
||||||
setAutoSelectionSubtypesEnabled(imiId, chkPref.isChecked());
|
// Enable or disable subtypes depending on the auto selection preference.
|
||||||
return super.onPreferenceTreeClick(preferenceScreen, preference);
|
setAutoSelectionSubtypesEnabled(imiId, autoSelectionPref.isChecked());
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// A subtype preference is changing.
|
||||||
// Turns off a subtype.
|
if (pref instanceof InputMethodSubtypePreference) {
|
||||||
if (!chkPref.isChecked()) {
|
final InputMethodSubtypePreference subtypePref = (InputMethodSubtypePreference) pref;
|
||||||
|
subtypePref.setChecked(isChecking);
|
||||||
|
if (!subtypePref.isChecked()) {
|
||||||
|
// It takes care of the case where no subtypes are explicitly enabled then the auto
|
||||||
|
// selection preference is going to be checked.
|
||||||
updateAutoSelectionPreferences();
|
updateAutoSelectionPreferences();
|
||||||
return super.onPreferenceTreeClick(preferenceScreen, preference);
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
return super.onPreferenceTreeClick(preferenceScreen, preference);
|
}
|
||||||
|
return true; // Invoke default behavior.
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addInputMethodSubtypePreferences(final InputMethodInfo imi,
|
private void addInputMethodSubtypePreferences(final InputMethodInfo imi,
|
||||||
@@ -166,6 +170,7 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
|
|||||||
final CheckBoxPreference autoSelectionPref = new CheckBoxPreference(context);
|
final CheckBoxPreference autoSelectionPref = new CheckBoxPreference(context);
|
||||||
mAutoSelectionPrefsMap.put(imiId, autoSelectionPref);
|
mAutoSelectionPrefsMap.put(imiId, autoSelectionPref);
|
||||||
keyboardSettingsCategory.addPreference(autoSelectionPref);
|
keyboardSettingsCategory.addPreference(autoSelectionPref);
|
||||||
|
autoSelectionPref.setOnPreferenceChangeListener(this);
|
||||||
|
|
||||||
final PreferenceCategory activeInputMethodsCategory = new PreferenceCategory(context);
|
final PreferenceCategory activeInputMethodsCategory = new PreferenceCategory(context);
|
||||||
activeInputMethodsCategory.setTitle(R.string.active_input_method_subtypes);
|
activeInputMethodsCategory.setTitle(R.string.active_input_method_subtypes);
|
||||||
@@ -199,6 +204,7 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
|
|||||||
for (int index = 0; index < prefCount; ++index) {
|
for (int index = 0; index < prefCount; ++index) {
|
||||||
final Preference pref = subtypePreferences.get(index);
|
final Preference pref = subtypePreferences.get(index);
|
||||||
activeInputMethodsCategory.addPreference(pref);
|
activeInputMethodsCategory.addPreference(pref);
|
||||||
|
pref.setOnPreferenceChangeListener(this);
|
||||||
InputMethodAndSubtypeUtil.removeUnnecessaryNonPersistentPreference(pref);
|
InputMethodAndSubtypeUtil.removeUnnecessaryNonPersistentPreference(pref);
|
||||||
}
|
}
|
||||||
mInputMethodAndSubtypePrefsMap.put(imiId, subtypePreferences);
|
mInputMethodAndSubtypePrefsMap.put(imiId, subtypePreferences);
|
||||||
|
Reference in New Issue
Block a user