diff options
Diffstat (limited to 'java/src')
13 files changed, 785 insertions, 401 deletions
diff --git a/java/src/com/android/inputmethod/latin/settings/CorrectionSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/CorrectionSettingsFragment.java new file mode 100644 index 000000000..ec29a7eb2 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/settings/CorrectionSettingsFragment.java @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.settings; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.os.Build; +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.Preference; + +import com.android.inputmethod.dictionarypack.DictionarySettingsActivity; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.userdictionary.UserDictionaryList; +import com.android.inputmethod.latin.userdictionary.UserDictionarySettings; + +import java.util.TreeSet; + +/** + * "Text correction" settings sub screen. + * + * This settings sub screen handles the following text correction preferences. + * - Personal dictionary + * - Add-on dictionaries + * - Block offensive words + * - Auto-correction + * - Show correction suggestions + * - Personalized suggestions + * - Suggest Contact names + * - Next-word suggestions + */ +public final class CorrectionSettingsFragment extends SubScreenFragment { + private static final boolean DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS = false; + private static final boolean USE_INTERNAL_PERSONAL_DICTIONARY_SETTIGS = + DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS + || Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2; + + @Override + public void onCreate(final Bundle icicle) { + super.onCreate(icicle); + addPreferencesFromResource(R.xml.prefs_screen_correction); + + final Context context = getActivity(); + final PackageManager pm = context.getPackageManager(); + + ensureConsistencyOfAutoCorrectionSettings(); + + final Preference dictionaryLink = findPreference(Settings.PREF_CONFIGURE_DICTIONARIES_KEY); + final Intent intent = dictionaryLink.getIntent(); + intent.setClassName(context.getPackageName(), DictionarySettingsActivity.class.getName()); + final int number = pm.queryIntentActivities(intent, 0).size(); + if (0 >= number) { + removePreference(Settings.PREF_CONFIGURE_DICTIONARIES_KEY); + } + + final Preference editPersonalDictionary = + findPreference(Settings.PREF_EDIT_PERSONAL_DICTIONARY); + final Intent editPersonalDictionaryIntent = editPersonalDictionary.getIntent(); + final ResolveInfo ri = USE_INTERNAL_PERSONAL_DICTIONARY_SETTIGS ? null + : pm.resolveActivity( + editPersonalDictionaryIntent, PackageManager.MATCH_DEFAULT_ONLY); + if (ri == null) { + overwriteUserDictionaryPreference(editPersonalDictionary); + } + } + + @Override + public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { + ensureConsistencyOfAutoCorrectionSettings(); + } + + private void ensureConsistencyOfAutoCorrectionSettings() { + final String autoCorrectionOff = getString( + R.string.auto_correction_threshold_mode_index_off); + final ListPreference autoCorrectionThresholdPref = (ListPreference)findPreference( + Settings.PREF_AUTO_CORRECTION_THRESHOLD); + final String currentSetting = autoCorrectionThresholdPref.getValue(); + setPreferenceEnabled( + Settings.PREF_BIGRAM_PREDICTIONS, !currentSetting.equals(autoCorrectionOff)); + } + + private void overwriteUserDictionaryPreference(final Preference userDictionaryPreference) { + final Activity activity = getActivity(); + final TreeSet<String> localeList = UserDictionaryList.getUserDictionaryLocalesSet(activity); + if (null == localeList) { + // The locale list is null if and only if the user dictionary service is + // not present or disabled. In this case we need to remove the preference. + getPreferenceScreen().removePreference(userDictionaryPreference); + } else if (localeList.size() <= 1) { + userDictionaryPreference.setFragment(UserDictionarySettings.class.getName()); + // If the size of localeList is 0, we don't set the locale parameter in the + // extras. This will be interpreted by the UserDictionarySettings class as + // meaning "the current locale". + // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesSet() + // the locale list always has at least one element, since it always includes the current + // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesSet(). + if (localeList.size() == 1) { + final String locale = (String)localeList.toArray()[0]; + userDictionaryPreference.getExtras().putString("locale", locale); + } + } else { + userDictionaryPreference.setFragment(UserDictionaryList.class.getName()); + } + } +} diff --git a/java/src/com/android/inputmethod/latin/settings/AdditionalSubtypeSettings.java b/java/src/com/android/inputmethod/latin/settings/CustomInputStyleSettingsFragment.java index ad411f9ee..21f2afd01 100644 --- a/java/src/com/android/inputmethod/latin/settings/AdditionalSubtypeSettings.java +++ b/java/src/com/android/inputmethod/latin/settings/CustomInputStyleSettingsFragment.java @@ -43,7 +43,6 @@ import android.widget.SpinnerAdapter; import android.widget.Toast; import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils; -import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.RichInputMethodManager; import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; @@ -54,7 +53,7 @@ import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import java.util.ArrayList; import java.util.TreeSet; -public final class AdditionalSubtypeSettings extends PreferenceFragment { +public final class CustomInputStyleSettingsFragment extends PreferenceFragment { private RichInputMethodManager mRichImm; private SharedPreferences mPrefs; private SubtypeLocaleAdapter mSubtypeLocaleAdapter; @@ -124,9 +123,8 @@ public final class AdditionalSubtypeSettings extends PreferenceFragment { if (localeString.equals(SubtypeLocaleUtils.NO_LANGUAGE)) { final String displayName = context.getString(R.string.subtype_no_language); return new SubtypeLocaleItem(localeString, displayName); - } else { - return new SubtypeLocaleItem(localeString); } + return new SubtypeLocaleItem(localeString); } } @@ -385,7 +383,7 @@ public final class AdditionalSubtypeSettings extends PreferenceFragment { } } - public AdditionalSubtypeSettings() { + public CustomInputStyleSettingsFragment() { // Empty constructor for fragment generation. } diff --git a/java/src/com/android/inputmethod/latin/settings/DebugSettings.java b/java/src/com/android/inputmethod/latin/settings/DebugSettings.java index e4271adac..dce11b434 100644 --- a/java/src/com/android/inputmethod/latin/settings/DebugSettings.java +++ b/java/src/com/android/inputmethod/latin/settings/DebugSettings.java @@ -16,28 +16,7 @@ package com.android.inputmethod.latin.settings; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.res.Resources; -import android.os.Bundle; -import android.os.Process; -import android.preference.Preference; -import android.preference.Preference.OnPreferenceClickListener; -import android.preference.PreferenceFragment; -import android.preference.PreferenceGroup; -import android.preference.PreferenceScreen; -import android.preference.TwoStatePreference; - -import com.android.inputmethod.latin.DictionaryDumpBroadcastReceiver; -import com.android.inputmethod.latin.DictionaryFacilitator; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.debug.ExternalDictionaryGetterForDebug; -import com.android.inputmethod.latin.utils.ApplicationUtils; -import com.android.inputmethod.latin.utils.ResourceUtils; - -public final class DebugSettings extends PreferenceFragment - implements SharedPreferences.OnSharedPreferenceChangeListener { - +public final class DebugSettings { public static final String PREF_DEBUG_MODE = "debug_mode"; public static final String PREF_FORCE_NON_DISTINCT_MULTITOUCH = "force_non_distinct_multitouch"; public static final String PREF_KEY_PREVIEW_SHOW_UP_START_SCALE = @@ -48,251 +27,10 @@ public final class DebugSettings extends PreferenceFragment "pref_key_preview_show_up_duration"; public static final String PREF_KEY_PREVIEW_DISMISS_DURATION = "pref_key_preview_dismiss_duration"; - private static final String PREF_READ_EXTERNAL_DICTIONARY = "read_external_dictionary"; - private static final String PREF_KEY_DUMP_DICTS = "pref_key_dump_dictionaries"; - private static final String PREF_KEY_DUMP_DICT_PREFIX = "pref_key_dump_dictionaries"; - private static final String DICT_NAME_KEY_FOR_EXTRAS = "dict_name"; public static final String PREF_SLIDING_KEY_INPUT_PREVIEW = "pref_sliding_key_input_preview"; public static final String PREF_KEY_LONGPRESS_TIMEOUT = "pref_key_longpress_timeout"; - private boolean mServiceNeedsRestart = false; - private TwoStatePreference mDebugMode; - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.prefs_screen_debug); - TwoStatePreferenceHelper.replaceCheckBoxPreferencesBySwitchPreferences( - getPreferenceScreen()); - SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); - prefs.registerOnSharedPreferenceChangeListener(this); - - final PreferenceScreen readExternalDictionary = - (PreferenceScreen) findPreference(PREF_READ_EXTERNAL_DICTIONARY); - if (null != readExternalDictionary) { - readExternalDictionary.setOnPreferenceClickListener( - new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(final Preference arg0) { - ExternalDictionaryGetterForDebug.chooseAndInstallDictionary( - getActivity()); - mServiceNeedsRestart = true; - return true; - } - }); - } - - final PreferenceGroup dictDumpPreferenceGroup = - (PreferenceGroup)findPreference(PREF_KEY_DUMP_DICTS); - final OnPreferenceClickListener dictDumpPrefClickListener = - new DictDumpPrefClickListener(this); - for (final String dictName : DictionaryFacilitator.DICT_TYPE_TO_CLASS.keySet()) { - final Preference preference = new Preference(getActivity()); - preference.setKey(PREF_KEY_DUMP_DICT_PREFIX + dictName); - preference.setTitle("Dump " + dictName + " dictionary"); - preference.setOnPreferenceClickListener(dictDumpPrefClickListener); - preference.getExtras().putString(DICT_NAME_KEY_FOR_EXTRAS, dictName); - dictDumpPreferenceGroup.addPreference(preference); - } - final Resources res = getResources(); - setupKeyLongpressTimeoutSettings(prefs, res); - setupKeyPreviewAnimationDuration(prefs, res, PREF_KEY_PREVIEW_SHOW_UP_DURATION, - res.getInteger(R.integer.config_key_preview_show_up_duration)); - setupKeyPreviewAnimationDuration(prefs, res, PREF_KEY_PREVIEW_DISMISS_DURATION, - res.getInteger(R.integer.config_key_preview_dismiss_duration)); - setupKeyPreviewAnimationScale(prefs, res, PREF_KEY_PREVIEW_SHOW_UP_START_SCALE, - ResourceUtils.getFloatFromFraction( - res, R.fraction.config_key_preview_show_up_start_scale)); - setupKeyPreviewAnimationScale(prefs, res, PREF_KEY_PREVIEW_DISMISS_END_SCALE, - ResourceUtils.getFloatFromFraction( - res, R.fraction.config_key_preview_dismiss_end_scale)); - - mServiceNeedsRestart = false; - mDebugMode = (TwoStatePreference) findPreference(PREF_DEBUG_MODE); - updateDebugMode(); - } - - private static class DictDumpPrefClickListener implements OnPreferenceClickListener { - final PreferenceFragment mPreferenceFragment; - - public DictDumpPrefClickListener(final PreferenceFragment preferenceFragment) { - mPreferenceFragment = preferenceFragment; - } - - @Override - public boolean onPreferenceClick(final Preference arg0) { - final String dictName = arg0.getExtras().getString(DICT_NAME_KEY_FOR_EXTRAS); - if (dictName != null) { - final Intent intent = - new Intent(DictionaryDumpBroadcastReceiver.DICTIONARY_DUMP_INTENT_ACTION); - intent.putExtra(DictionaryDumpBroadcastReceiver.DICTIONARY_NAME_KEY, dictName); - mPreferenceFragment.getActivity().sendBroadcast(intent); - } - return true; - } - } - - @Override - public void onStop() { - super.onStop(); - if (mServiceNeedsRestart) { - Process.killProcess(Process.myPid()); - } - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { - if (key.equals(PREF_DEBUG_MODE) && mDebugMode != null) { - mDebugMode.setChecked(prefs.getBoolean(PREF_DEBUG_MODE, false)); - updateDebugMode(); - mServiceNeedsRestart = true; - return; - } - if (key.equals(PREF_FORCE_NON_DISTINCT_MULTITOUCH)) { - mServiceNeedsRestart = true; - return; - } - } - - private void updateDebugMode() { - if (mDebugMode == null) { - return; - } - boolean isDebugMode = mDebugMode.isChecked(); - final String version = getResources().getString( - R.string.version_text, ApplicationUtils.getVersionName(getActivity())); - if (!isDebugMode) { - mDebugMode.setTitle(version); - mDebugMode.setSummary(""); - } else { - mDebugMode.setTitle(getResources().getString(R.string.prefs_debug_mode)); - mDebugMode.setSummary(version); - } - } - - private void setupKeyLongpressTimeoutSettings(final SharedPreferences sp, - final Resources res) { - final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference( - PREF_KEY_LONGPRESS_TIMEOUT); - if (pref == null) { - return; - } - pref.setInterface(new SeekBarDialogPreference.ValueProxy() { - @Override - public void writeValue(final int value, final String key) { - sp.edit().putInt(key, value).apply(); - } - - @Override - public void writeDefaultValue(final String key) { - sp.edit().remove(key).apply(); - } - - @Override - public int readValue(final String key) { - return Settings.readKeyLongpressTimeout(sp, res); - } - - @Override - public int readDefaultValue(final String key) { - return Settings.readDefaultKeyLongpressTimeout(res); - } - - @Override - public String getValueText(final int value) { - return res.getString(R.string.abbreviation_unit_milliseconds, value); - } - - @Override - public void feedbackValue(final int value) {} - }); - } - - private void setupKeyPreviewAnimationScale(final SharedPreferences sp, final Resources res, - final String prefKey, final float defaultValue) { - final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(prefKey); - if (pref == null) { - return; - } - pref.setInterface(new SeekBarDialogPreference.ValueProxy() { - private static final float PERCENTAGE_FLOAT = 100.0f; - - private float getValueFromPercentage(final int percentage) { - return percentage / PERCENTAGE_FLOAT; - } - - private int getPercentageFromValue(final float floatValue) { - return (int)(floatValue * PERCENTAGE_FLOAT); - } - - @Override - public void writeValue(final int value, final String key) { - sp.edit().putFloat(key, getValueFromPercentage(value)).apply(); - } - - @Override - public void writeDefaultValue(final String key) { - sp.edit().remove(key).apply(); - } - - @Override - public int readValue(final String key) { - return getPercentageFromValue( - Settings.readKeyPreviewAnimationScale(sp, key, defaultValue)); - } - - @Override - public int readDefaultValue(final String key) { - return getPercentageFromValue(defaultValue); - } - - @Override - public String getValueText(final int value) { - if (value < 0) { - return res.getString(R.string.settings_system_default); - } - return String.format("%d%%", value); - } - - @Override - public void feedbackValue(final int value) {} - }); - } - - private void setupKeyPreviewAnimationDuration(final SharedPreferences sp, final Resources res, - final String prefKey, final int defaultValue) { - final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(prefKey); - if (pref == null) { - return; - } - pref.setInterface(new SeekBarDialogPreference.ValueProxy() { - @Override - public void writeValue(final int value, final String key) { - sp.edit().putInt(key, value).apply(); - } - - @Override - public void writeDefaultValue(final String key) { - sp.edit().remove(key).apply(); - } - - @Override - public int readValue(final String key) { - return Settings.readKeyPreviewAnimationDuration(sp, key, defaultValue); - } - - @Override - public int readDefaultValue(final String key) { - return defaultValue; - } - - @Override - public String getValueText(final int value) { - return res.getString(R.string.abbreviation_unit_milliseconds, value); - } - - @Override - public void feedbackValue(final int value) {} - }); + private DebugSettings() { + // This class is not publicly instantiable. } } diff --git a/java/src/com/android/inputmethod/latin/settings/DebugSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/DebugSettingsFragment.java new file mode 100644 index 000000000..4e41d52a3 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/settings/DebugSettingsFragment.java @@ -0,0 +1,283 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.settings; + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.Resources; +import android.os.Bundle; +import android.os.Process; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceGroup; +import android.preference.TwoStatePreference; + +import com.android.inputmethod.latin.DictionaryDumpBroadcastReceiver; +import com.android.inputmethod.latin.DictionaryFacilitator; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.debug.ExternalDictionaryGetterForDebug; +import com.android.inputmethod.latin.utils.ApplicationUtils; +import com.android.inputmethod.latin.utils.ResourceUtils; + +import java.util.Locale; + +/** + * "Debug mode" settings sub screen. + * + * This settings sub screen handles a several preference options for debugging. + */ +public final class DebugSettingsFragment extends SubScreenFragment + implements OnPreferenceClickListener { + private static final String PREF_READ_EXTERNAL_DICTIONARY = "read_external_dictionary"; + private static final String PREF_KEY_DUMP_DICTS = "pref_key_dump_dictionaries"; + private static final String PREF_KEY_DUMP_DICT_PREFIX = "pref_key_dump_dictionaries"; + + private boolean mServiceNeedsRestart = false; + private Preference mReadExternalDictionaryPref; + private TwoStatePreference mDebugMode; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + addPreferencesFromResource(R.xml.prefs_screen_debug); + + mReadExternalDictionaryPref = findPreference(PREF_READ_EXTERNAL_DICTIONARY); + if (mReadExternalDictionaryPref != null) { + mReadExternalDictionaryPref.setOnPreferenceClickListener(this); + } + + final PreferenceGroup dictDumpPreferenceGroup = + (PreferenceGroup)findPreference(PREF_KEY_DUMP_DICTS); + for (final String dictName : DictionaryFacilitator.DICT_TYPE_TO_CLASS.keySet()) { + final Preference pref = new DictDumpPreference(getActivity(), dictName); + pref.setOnPreferenceClickListener(this); + dictDumpPreferenceGroup.addPreference(pref); + } + final Resources res = getResources(); + setupKeyLongpressTimeoutSettings(); + setupKeyPreviewAnimationDuration(DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_DURATION, + res.getInteger(R.integer.config_key_preview_show_up_duration)); + setupKeyPreviewAnimationDuration(DebugSettings.PREF_KEY_PREVIEW_DISMISS_DURATION, + res.getInteger(R.integer.config_key_preview_dismiss_duration)); + setupKeyPreviewAnimationScale(DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_START_SCALE, + ResourceUtils.getFloatFromFraction( + res, R.fraction.config_key_preview_show_up_start_scale)); + setupKeyPreviewAnimationScale(DebugSettings.PREF_KEY_PREVIEW_DISMISS_END_SCALE, + ResourceUtils.getFloatFromFraction( + res, R.fraction.config_key_preview_dismiss_end_scale)); + + mServiceNeedsRestart = false; + mDebugMode = (TwoStatePreference) findPreference(DebugSettings.PREF_DEBUG_MODE); + updateDebugMode(); + } + + private static class DictDumpPreference extends Preference { + public final String mDictName; + + public DictDumpPreference(final Context context, final String dictName) { + super(context); + setKey(PREF_KEY_DUMP_DICT_PREFIX + dictName); + setTitle("Dump " + dictName + " dictionary"); + mDictName = dictName; + } + } + + @Override + public boolean onPreferenceClick(final Preference pref) { + final Context context = getActivity(); + if (pref == mReadExternalDictionaryPref) { + ExternalDictionaryGetterForDebug.chooseAndInstallDictionary(context); + mServiceNeedsRestart = true; + return true; + } + if (pref instanceof DictDumpPreference) { + final DictDumpPreference dictDumpPref = (DictDumpPreference)pref; + final String dictName = dictDumpPref.mDictName; + final Intent intent = new Intent( + DictionaryDumpBroadcastReceiver.DICTIONARY_DUMP_INTENT_ACTION); + intent.putExtra(DictionaryDumpBroadcastReceiver.DICTIONARY_NAME_KEY, dictName); + context.sendBroadcast(intent); + return true; + } + return true; + } + + @Override + public void onStop() { + super.onStop(); + if (mServiceNeedsRestart) { + Process.killProcess(Process.myPid()); + } + } + + @Override + public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { + if (key.equals(DebugSettings.PREF_DEBUG_MODE) && mDebugMode != null) { + mDebugMode.setChecked(prefs.getBoolean(DebugSettings.PREF_DEBUG_MODE, false)); + updateDebugMode(); + mServiceNeedsRestart = true; + return; + } + if (key.equals(DebugSettings.PREF_FORCE_NON_DISTINCT_MULTITOUCH)) { + mServiceNeedsRestart = true; + return; + } + } + + private void updateDebugMode() { + boolean isDebugMode = mDebugMode.isChecked(); + final String version = getString( + R.string.version_text, ApplicationUtils.getVersionName(getActivity())); + if (!isDebugMode) { + mDebugMode.setTitle(version); + mDebugMode.setSummary(null); + } else { + mDebugMode.setTitle(getString(R.string.prefs_debug_mode)); + mDebugMode.setSummary(version); + } + } + + private void setupKeyLongpressTimeoutSettings() { + final SharedPreferences prefs = getSharedPreferences(); + final Resources res = getResources(); + final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference( + DebugSettings.PREF_KEY_LONGPRESS_TIMEOUT); + if (pref == null) { + return; + } + pref.setInterface(new SeekBarDialogPreference.ValueProxy() { + @Override + public void writeValue(final int value, final String key) { + prefs.edit().putInt(key, value).apply(); + } + + @Override + public void writeDefaultValue(final String key) { + prefs.edit().remove(key).apply(); + } + + @Override + public int readValue(final String key) { + return Settings.readKeyLongpressTimeout(prefs, res); + } + + @Override + public int readDefaultValue(final String key) { + return Settings.readDefaultKeyLongpressTimeout(res); + } + + @Override + public String getValueText(final int value) { + return res.getString(R.string.abbreviation_unit_milliseconds, value); + } + + @Override + public void feedbackValue(final int value) {} + }); + } + + private void setupKeyPreviewAnimationScale(final String prefKey, final float defaultValue) { + final SharedPreferences prefs = getSharedPreferences(); + final Resources res = getResources(); + final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(prefKey); + if (pref == null) { + return; + } + pref.setInterface(new SeekBarDialogPreference.ValueProxy() { + private static final float PERCENTAGE_FLOAT = 100.0f; + + private float getValueFromPercentage(final int percentage) { + return percentage / PERCENTAGE_FLOAT; + } + + private int getPercentageFromValue(final float floatValue) { + return (int)(floatValue * PERCENTAGE_FLOAT); + } + + @Override + public void writeValue(final int value, final String key) { + prefs.edit().putFloat(key, getValueFromPercentage(value)).apply(); + } + + @Override + public void writeDefaultValue(final String key) { + prefs.edit().remove(key).apply(); + } + + @Override + public int readValue(final String key) { + return getPercentageFromValue( + Settings.readKeyPreviewAnimationScale(prefs, key, defaultValue)); + } + + @Override + public int readDefaultValue(final String key) { + return getPercentageFromValue(defaultValue); + } + + @Override + public String getValueText(final int value) { + if (value < 0) { + return res.getString(R.string.settings_system_default); + } + return String.format(Locale.ROOT, "%d%%", value); + } + + @Override + public void feedbackValue(final int value) {} + }); + } + + private void setupKeyPreviewAnimationDuration(final String prefKey, final int defaultValue) { + final SharedPreferences prefs = getSharedPreferences(); + final Resources res = getResources(); + final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(prefKey); + if (pref == null) { + return; + } + pref.setInterface(new SeekBarDialogPreference.ValueProxy() { + @Override + public void writeValue(final int value, final String key) { + prefs.edit().putInt(key, value).apply(); + } + + @Override + public void writeDefaultValue(final String key) { + prefs.edit().remove(key).apply(); + } + + @Override + public int readValue(final String key) { + return Settings.readKeyPreviewAnimationDuration(prefs, key, defaultValue); + } + + @Override + public int readDefaultValue(final String key) { + return defaultValue; + } + + @Override + public String getValueText(final int value) { + return res.getString(R.string.abbreviation_unit_milliseconds, value); + } + + @Override + public void feedbackValue(final int value) {} + }); + } +} diff --git a/java/src/com/android/inputmethod/latin/settings/GestureSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/GestureSettingsFragment.java new file mode 100644 index 000000000..b95c35637 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/settings/GestureSettingsFragment.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.settings; + +import android.content.SharedPreferences; +import android.os.Bundle; + +import com.android.inputmethod.latin.R; + +/** + * "Gesture typing preferences" settings sub screen. + * + * This settings sub screen handles the following gesture typing preferences. + * - Enable gesture typing + * - Dynamic floating preview + * - Show gesture trail + * - Phrase gesture + */ +public final class GestureSettingsFragment extends SubScreenFragment { + @Override + public void onCreate(final Bundle icicle) { + super.onCreate(icicle); + addPreferencesFromResource(R.xml.prefs_screen_gesture); + } + + @Override + public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { + // Nothing to do here. + } +} diff --git a/java/src/com/android/inputmethod/latin/settings/InputSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/InputSettingsFragment.java new file mode 100644 index 000000000..f459d68dd --- /dev/null +++ b/java/src/com/android/inputmethod/latin/settings/InputSettingsFragment.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.settings; + +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Resources; +import android.os.Bundle; +import android.preference.Preference; + +import com.android.inputmethod.latin.AudioAndHapticFeedbackManager; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.SubtypeSwitcher; + +/** + * "Input preferences" settings sub screen. + * + * This settings sub screen handles the following input preferences. + * - Auto-capitalization + * - Double-space period + * - Vibrate on keypress + * - Sound on keypress + * - Popup on keypress + * - Voice input key + */ +public final class InputSettingsFragment extends SubScreenFragment { + @Override + public void onCreate(final Bundle icicle) { + super.onCreate(icicle); + addPreferencesFromResource(R.xml.prefs_screen_input); + + final Resources res = getResources(); + final Context context = getActivity(); + + // When we are called from the Settings application but we are not already running, some + // singleton and utility classes may not have been initialized. We have to call + // initialization method of these classes here. See {@link LatinIME#onCreate()}. + SubtypeSwitcher.init(context); + + final boolean showVoiceKeyOption = res.getBoolean( + R.bool.config_enable_show_voice_key_option); + if (!showVoiceKeyOption) { + removePreference(Settings.PREF_VOICE_INPUT_KEY); + } + if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) { + removePreference(Settings.PREF_VIBRATE_ON); + } + if (!Settings.readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) { + removePreference(Settings.PREF_POPUP_ON); + } + + refreshEnablingsOfKeypressSoundAndVibrationSettings(); + } + + @Override + public void onResume() { + super.onResume(); + final Preference voiceInputKeyOption = findPreference(Settings.PREF_VOICE_INPUT_KEY); + if (voiceInputKeyOption != null) { + final boolean isShortcutImeEnabled = SubtypeSwitcher.getInstance() + .isShortcutImeEnabled(); + voiceInputKeyOption.setEnabled(isShortcutImeEnabled); + voiceInputKeyOption.setSummary( + isShortcutImeEnabled ? null : getText(R.string.voice_input_disabled_summary)); + } + } + + @Override + public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { + final Resources res = getResources(); + if (key.equals(Settings.PREF_POPUP_ON)) { + setPreferenceEnabled(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY, + Settings.readKeyPreviewPopupEnabled(prefs, res)); + } + refreshEnablingsOfKeypressSoundAndVibrationSettings(); + } + + private void refreshEnablingsOfKeypressSoundAndVibrationSettings() { + final SharedPreferences prefs = getSharedPreferences(); + final Resources res = getResources(); + setPreferenceEnabled(Settings.PREF_VIBRATION_DURATION_SETTINGS, + Settings.readVibrationEnabled(prefs, res)); + setPreferenceEnabled(Settings.PREF_KEYPRESS_SOUND_VOLUME, + Settings.readKeypressSoundEnabled(prefs, res)); + } +} diff --git a/java/src/com/android/inputmethod/latin/settings/MultiLingualSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/MultiLingualSettingsFragment.java new file mode 100644 index 000000000..f40106ba9 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/settings/MultiLingualSettingsFragment.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.settings; + +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Resources; +import android.os.Bundle; +import android.preference.PreferenceScreen; +import android.text.TextUtils; +import android.view.inputmethod.InputMethodSubtype; + +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; +import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; + +import java.util.ArrayList; + +/** + * "Multi lingual options" settings sub screen. + * + * This settings sub screen handles the following input preferences. + * - Language switch key + * - Switch to other input methods + * - Custom input styles + */ +public final class MultiLingualSettingsFragment extends SubScreenFragment { + @Override + public void onCreate(final Bundle icicle) { + super.onCreate(icicle); + addPreferencesFromResource(R.xml.prefs_screen_multi_lingual); + + final Context context = getActivity(); + + // When we are called from the Settings application but we are not already running, some + // singleton and utility classes may not have been initialized. We have to call + // initialization method of these classes here. See {@link LatinIME#onCreate()}. + SubtypeLocaleUtils.init(context); + + if (!Settings.ENABLE_SHOW_LANGUAGE_SWITCH_KEY_SETTINGS) { + removePreference(Settings.PREF_SHOW_LANGUAGE_SWITCH_KEY); + removePreference(Settings.PREF_INCLUDE_OTHER_IMES_IN_LANGUAGE_SWITCH_LIST); + } + } + + @Override + public void onResume() { + super.onResume(); + updateCustomInputStylesSummary(); + } + + @Override + public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { + // Nothing to do here. + } + + private void updateCustomInputStylesSummary() { + final SharedPreferences prefs = getSharedPreferences(); + final Resources res = getResources(); + final PreferenceScreen customInputStyles = + (PreferenceScreen)findPreference(Settings.PREF_CUSTOM_INPUT_STYLES); + final String prefSubtype = Settings.readPrefAdditionalSubtypes(prefs, res); + final InputMethodSubtype[] subtypes = + AdditionalSubtypeUtils.createAdditionalSubtypesArray(prefSubtype); + final ArrayList<String> subtypeNames = new ArrayList<>(); + for (final InputMethodSubtype subtype : subtypes) { + subtypeNames.add(SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype)); + } + // TODO: A delimiter of custom input styles should be localized. + customInputStyles.setSummary(TextUtils.join(", ", subtypeNames)); + } +} diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java b/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java index c899507e3..c7b9dcdd9 100644 --- a/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java +++ b/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java @@ -35,8 +35,7 @@ public final class SettingsActivity extends PreferenceActivity { return intent; } - // TODO: Uncomment the override annotation once we start using SDK version 19. - // @Override + @Override public boolean isValidFragment(String fragmentName) { return FragmentUtils.isValidFragment(fragmentName); } diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java index b289174c6..0f0166143 100644 --- a/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java +++ b/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java @@ -21,11 +21,8 @@ import android.app.backup.BackupManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.media.AudioManager; -import android.os.Build; import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; @@ -36,32 +33,19 @@ import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.inputmethod.InputMethodSubtype; -import com.android.inputmethod.dictionarypack.DictionarySettingsActivity; import com.android.inputmethod.keyboard.KeyboardTheme; import com.android.inputmethod.latin.AudioAndHapticFeedbackManager; import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SubtypeSwitcher; import com.android.inputmethod.latin.define.ProductionFlags; import com.android.inputmethod.latin.setup.LauncherIconVisibilityManager; -import com.android.inputmethod.latin.userdictionary.UserDictionaryList; -import com.android.inputmethod.latin.userdictionary.UserDictionarySettings; -import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; import com.android.inputmethod.latin.utils.ApplicationUtils; import com.android.inputmethod.latin.utils.FeedbackUtils; -import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethodcommon.InputMethodSettingsFragment; -import java.util.TreeSet; - public final class SettingsFragment extends InputMethodSettingsFragment implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG = SettingsFragment.class.getSimpleName(); - private static final boolean DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS = false; - private static final boolean USE_INTERNAL_PERSONAL_DICTIONARY_SETTIGS = - DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS - || Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2; private static final int NO_MENU_GROUP = Menu.NONE; // We don't care about menu grouping. private static final int MENU_FEEDBACK = Menu.FIRST; // The first menu item id and order. @@ -114,23 +98,11 @@ public final class SettingsFragment extends InputMethodSettingsFragment // When we are called from the Settings application but we are not already running, some // singleton and utility classes may not have been initialized. We have to call // initialization method of these classes here. See {@link LatinIME#onCreate()}. - SubtypeSwitcher.init(context); - SubtypeLocaleUtils.init(context); AudioAndHapticFeedbackManager.init(context); final SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); prefs.registerOnSharedPreferenceChangeListener(this); - ensureConsistencyOfAutoCorrectionSettings(); - - final PreferenceScreen inputScreen = - (PreferenceScreen) findPreference(Settings.SCREEN_INPUT); - final PreferenceScreen multiLingualScreen = - (PreferenceScreen) findPreference(Settings.SCREEN_MULTI_LINGUAL); - final PreferenceScreen gestureScreen = - (PreferenceScreen) findPreference(Settings.SCREEN_GESTURE); - final PreferenceScreen correctionScreen = - (PreferenceScreen) findPreference(Settings.SCREEN_CORRECTION); final PreferenceScreen advancedScreen = (PreferenceScreen) findPreference(Settings.SCREEN_ADVANCED); final PreferenceScreen debugScreen = @@ -140,25 +112,12 @@ public final class SettingsFragment extends InputMethodSettingsFragment advancedScreen.removePreference(debugScreen); } - final boolean showVoiceKeyOption = res.getBoolean( - R.bool.config_enable_show_voice_key_option); - if (!showVoiceKeyOption) { - removePreference(Settings.PREF_VOICE_INPUT_KEY, inputScreen); - } - if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) { - removePreference(Settings.PREF_VIBRATE_ON, inputScreen); removePreference(Settings.PREF_VIBRATION_DURATION_SETTINGS, advancedScreen); } - if (!Settings.ENABLE_SHOW_LANGUAGE_SWITCH_KEY_SETTINGS) { - removePreference(Settings.PREF_SHOW_LANGUAGE_SWITCH_KEY, multiLingualScreen); - removePreference( - Settings.PREF_INCLUDE_OTHER_IMES_IN_LANGUAGE_SWITCH_LIST, multiLingualScreen); - } // TODO: consolidate key preview dismiss delay with the key preview animation parameters. if (!Settings.readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) { - removePreference(Settings.PREF_POPUP_ON, inputScreen); removePreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY, advancedScreen); } else { // TODO: Cleanup this setup. @@ -185,15 +144,6 @@ public final class SettingsFragment extends InputMethodSettingsFragment removePreference(Settings.PREF_SHOW_SETUP_WIZARD_ICON, advancedScreen); } - final PreferenceScreen dictionaryLink = - (PreferenceScreen) findPreference(Settings.PREF_CONFIGURE_DICTIONARIES_KEY); - final Intent intent = dictionaryLink.getIntent(); - intent.setClassName(context.getPackageName(), DictionarySettingsActivity.class.getName()); - final int number = context.getPackageManager().queryIntentActivities(intent, 0).size(); - if (0 >= number) { - correctionScreen.removePreference(dictionaryLink); - } - if (ProductionFlags.IS_METRICS_LOGGING_SUPPORTED) { final Preference enableMetricsLogging = findPreference(Settings.PREF_ENABLE_METRICS_LOGGING); @@ -208,18 +158,8 @@ public final class SettingsFragment extends InputMethodSettingsFragment removePreference(Settings.PREF_ENABLE_METRICS_LOGGING, advancedScreen); } - final Preference editPersonalDictionary = - findPreference(Settings.PREF_EDIT_PERSONAL_DICTIONARY); - final Intent editPersonalDictionaryIntent = editPersonalDictionary.getIntent(); - final ResolveInfo ri = USE_INTERNAL_PERSONAL_DICTIONARY_SETTIGS ? null - : context.getPackageManager().resolveActivity( - editPersonalDictionaryIntent, PackageManager.MATCH_DEFAULT_ONLY); - if (ri == null) { - overwriteUserDictionaryPreference(editPersonalDictionary); - } - if (!Settings.readFromBuildConfigIfGestureInputEnabled(res)) { - getPreferenceScreen().removePreference(gestureScreen); + getPreferenceScreen().removePreference(findPreference(Settings.SCREEN_GESTURE)); } AdditionalFeaturesSettingUtils.addAdditionalFeaturesPreferences(context, this); @@ -233,15 +173,6 @@ public final class SettingsFragment extends InputMethodSettingsFragment public void onResume() { super.onResume(); final SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); - final Resources res = getResources(); - final Preference voiceInputKeyOption = findPreference(Settings.PREF_VOICE_INPUT_KEY); - if (voiceInputKeyOption != null) { - final boolean isShortcutImeEnabled = SubtypeSwitcher.getInstance() - .isShortcutImeEnabled(); - voiceInputKeyOption.setEnabled(isShortcutImeEnabled); - voiceInputKeyOption.setSummary(isShortcutImeEnabled ? null - : res.getText(R.string.voice_input_disabled_summary)); - } final TwoStatePreference showSetupWizardIcon = (TwoStatePreference)findPreference(Settings.PREF_SHOW_SETUP_WIZARD_ICON); if (showSetupWizardIcon != null) { @@ -258,7 +189,6 @@ public final class SettingsFragment extends InputMethodSettingsFragment keyboardThemePref.setSummary(entryIndex < 0 ? null : entries[entryIndex]); keyboardThemePref.setValue(value); } - updateCustomInputStylesSummary(prefs, res); } @Override @@ -296,37 +226,11 @@ public final class SettingsFragment extends InputMethodSettingsFragment } else if (key.equals(Settings.PREF_SHOW_SETUP_WIZARD_ICON)) { LauncherIconVisibilityManager.updateSetupWizardIconVisibility(getActivity()); } - ensureConsistencyOfAutoCorrectionSettings(); updateListPreferenceSummaryToCurrentValue(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY); updateListPreferenceSummaryToCurrentValue(Settings.PREF_KEYBOARD_THEME); refreshEnablingsOfKeypressSoundAndVibrationSettings(prefs, getResources()); } - private void ensureConsistencyOfAutoCorrectionSettings() { - final String autoCorrectionOff = getResources().getString( - R.string.auto_correction_threshold_mode_index_off); - final ListPreference autoCorrectionThresholdPref = (ListPreference)findPreference( - Settings.PREF_AUTO_CORRECTION_THRESHOLD); - final String currentSetting = autoCorrectionThresholdPref.getValue(); - setPreferenceEnabled( - Settings.PREF_BIGRAM_PREDICTIONS, !currentSetting.equals(autoCorrectionOff)); - } - - private void updateCustomInputStylesSummary(final SharedPreferences prefs, - final Resources res) { - final PreferenceScreen customInputStyles = - (PreferenceScreen)findPreference(Settings.PREF_CUSTOM_INPUT_STYLES); - final String prefSubtype = Settings.readPrefAdditionalSubtypes(prefs, res); - final InputMethodSubtype[] subtypes = - AdditionalSubtypeUtils.createAdditionalSubtypesArray(prefSubtype); - final StringBuilder styles = new StringBuilder(); - for (final InputMethodSubtype subtype : subtypes) { - if (styles.length() > 0) styles.append(", "); - styles.append(SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype)); - } - customInputStyles.setSummary(styles); - } - private void refreshEnablingsOfKeypressSoundAndVibrationSettings( final SharedPreferences sp, final Resources res) { setPreferenceEnabled(Settings.PREF_VIBRATION_DURATION_SETTINGS, @@ -432,30 +336,6 @@ public final class SettingsFragment extends InputMethodSettingsFragment }); } - private void overwriteUserDictionaryPreference(Preference userDictionaryPreference) { - final Activity activity = getActivity(); - final TreeSet<String> localeList = UserDictionaryList.getUserDictionaryLocalesSet(activity); - if (null == localeList) { - // The locale list is null if and only if the user dictionary service is - // not present or disabled. In this case we need to remove the preference. - getPreferenceScreen().removePreference(userDictionaryPreference); - } else if (localeList.size() <= 1) { - userDictionaryPreference.setFragment(UserDictionarySettings.class.getName()); - // If the size of localeList is 0, we don't set the locale parameter in the - // extras. This will be interpreted by the UserDictionarySettings class as - // meaning "the current locale". - // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesSet() - // the locale list always has at least one element, since it always includes the current - // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesSet(). - if (localeList.size() == 1) { - final String locale = (String)localeList.toArray()[0]; - userDictionaryPreference.getExtras().putString("locale", locale); - } - } else { - userDictionaryPreference.setFragment(UserDictionaryList.class.getName()); - } - } - @Override public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { if (FeedbackUtils.isFeedbackFormSupported()) { diff --git a/java/src/com/android/inputmethod/latin/settings/SubScreenFragment.java b/java/src/com/android/inputmethod/latin/settings/SubScreenFragment.java new file mode 100644 index 000000000..c70bf2997 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/settings/SubScreenFragment.java @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.settings; + +import android.app.backup.BackupManager; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.PreferenceFragment; +import android.preference.PreferenceScreen; +import android.util.Log; + +/** + * A base abstract class for a {@link PreferenceFragment} that implements a nested + * {@link PreferenceScreen} of the main preference screen. + */ +abstract class SubScreenFragment extends PreferenceFragment + implements OnSharedPreferenceChangeListener { + private OnSharedPreferenceChangeListener mSharedPreferenceChangeListener; + + static void setPreferenceEnabled(final String prefKey, final boolean enabled, + final PreferenceScreen screen) { + final Preference preference = screen.findPreference(prefKey); + if (preference != null) { + preference.setEnabled(enabled); + } + } + + static void removePreference(final String prefKey, final PreferenceScreen screen) { + final Preference preference = screen.findPreference(prefKey); + if (preference != null) { + screen.removePreference(preference); + } + } + + static void updateListPreferenceSummaryToCurrentValue(final String prefKey, + final PreferenceScreen screen) { + // Because the "%s" summary trick of {@link ListPreference} doesn't work properly before + // KitKat, we need to update the summary programmatically. + final ListPreference listPreference = (ListPreference)screen.findPreference(prefKey); + if (listPreference == null) { + return; + } + final CharSequence entries[] = listPreference.getEntries(); + final int entryIndex = listPreference.findIndexOfValue(listPreference.getValue()); + listPreference.setSummary(entryIndex < 0 ? null : entries[entryIndex]); + } + + final void setPreferenceEnabled(final String prefKey, final boolean enabled) { + setPreferenceEnabled(prefKey, enabled, getPreferenceScreen()); + } + + final void removePreference(final String prefKey) { + removePreference(prefKey, getPreferenceScreen()); + } + + final void updateListPreferenceSummaryToCurrentValue(final String prefKey) { + updateListPreferenceSummaryToCurrentValue(prefKey, getPreferenceScreen()); + } + + final SharedPreferences getSharedPreferences() { + return getPreferenceManager().getSharedPreferences(); + } + + @Override + public void addPreferencesFromResource(final int preferencesResId) { + super.addPreferencesFromResource(preferencesResId); + TwoStatePreferenceHelper.replaceCheckBoxPreferencesBySwitchPreferences( + getPreferenceScreen()); + } + + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mSharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() { + @Override + public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { + final SubScreenFragment fragment = SubScreenFragment.this; + final Context context = fragment.getActivity(); + if (context == null || fragment.getPreferenceScreen() == null) { + final String tag = fragment.getClass().getSimpleName(); + // TODO: Introduce a static function to register this class and ensure that + // onCreate must be called before "onSharedPreferenceChanged" is called. + Log.w(tag, "onSharedPreferenceChanged called before activity starts."); + return; + } + new BackupManager(context).dataChanged(); + fragment.onSharedPreferenceChanged(prefs, key); + } + }; + getSharedPreferences().registerOnSharedPreferenceChangeListener( + mSharedPreferenceChangeListener); + } + + @Override + public void onDestroy() { + getSharedPreferences().unregisterOnSharedPreferenceChangeListener( + mSharedPreferenceChangeListener); + super.onDestroy(); + } +} diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java index 14ab2dbbf..34e01197a 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerSession.java @@ -153,7 +153,7 @@ public final class AndroidSpellCheckerSession extends AndroidWordLevelSpellCheck */ private SentenceSuggestionsInfo[] splitAndSuggest(TextInfo[] textInfos, int suggestionsLimit) { if (textInfos == null || textInfos.length == 0) { - return SentenceLevelAdapter.EMPTY_SENTENCE_SUGGESTIONS_INFOS; + return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo(); } SentenceLevelAdapter sentenceLevelAdapter; synchronized(this) { @@ -168,7 +168,7 @@ public final class AndroidSpellCheckerSession extends AndroidWordLevelSpellCheck } } if (sentenceLevelAdapter == null) { - return SentenceLevelAdapter.EMPTY_SENTENCE_SUGGESTIONS_INFOS; + return SentenceLevelAdapter.getEmptySentenceSuggestionsInfo(); } final int infosSize = textInfos.length; final SentenceSuggestionsInfo[] retval = new SentenceSuggestionsInfo[infosSize]; diff --git a/java/src/com/android/inputmethod/latin/spellcheck/SentenceLevelAdapter.java b/java/src/com/android/inputmethod/latin/spellcheck/SentenceLevelAdapter.java index ae582ea25..51c4b1ee8 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/SentenceLevelAdapter.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/SentenceLevelAdapter.java @@ -35,9 +35,16 @@ import java.util.Locale; * rewrite everything for any small change. */ public class SentenceLevelAdapter { - public static final SentenceSuggestionsInfo[] EMPTY_SENTENCE_SUGGESTIONS_INFOS = - new SentenceSuggestionsInfo[] {}; + private static class EmptySentenceSuggestionsInfosInitializationHolder { + public static final SentenceSuggestionsInfo[] EMPTY_SENTENCE_SUGGESTIONS_INFOS = + new SentenceSuggestionsInfo[]{}; + } private static final SuggestionsInfo EMPTY_SUGGESTIONS_INFO = new SuggestionsInfo(0, null); + + public static SentenceSuggestionsInfo[] getEmptySentenceSuggestionsInfo() { + return EmptySentenceSuggestionsInfosInitializationHolder.EMPTY_SENTENCE_SUGGESTIONS_INFOS; + } + /** * Container for split TextInfo parameters */ diff --git a/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java b/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java index e300bd1d3..322e953c8 100644 --- a/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java @@ -18,8 +18,12 @@ package com.android.inputmethod.latin.utils; import com.android.inputmethod.dictionarypack.DictionarySettingsFragment; import com.android.inputmethod.latin.about.AboutPreferences; -import com.android.inputmethod.latin.settings.AdditionalSubtypeSettings; -import com.android.inputmethod.latin.settings.DebugSettings; +import com.android.inputmethod.latin.settings.CorrectionSettingsFragment; +import com.android.inputmethod.latin.settings.CustomInputStyleSettingsFragment; +import com.android.inputmethod.latin.settings.DebugSettingsFragment; +import com.android.inputmethod.latin.settings.GestureSettingsFragment; +import com.android.inputmethod.latin.settings.InputSettingsFragment; +import com.android.inputmethod.latin.settings.MultiLingualSettingsFragment; import com.android.inputmethod.latin.settings.SettingsFragment; import com.android.inputmethod.latin.spellcheck.SpellCheckerSettingsFragment; import com.android.inputmethod.latin.userdictionary.UserDictionaryAddWordFragment; @@ -34,8 +38,12 @@ public class FragmentUtils { static { sLatinImeFragments.add(DictionarySettingsFragment.class.getName()); sLatinImeFragments.add(AboutPreferences.class.getName()); - sLatinImeFragments.add(AdditionalSubtypeSettings.class.getName()); - sLatinImeFragments.add(DebugSettings.class.getName()); + sLatinImeFragments.add(InputSettingsFragment.class.getName()); + sLatinImeFragments.add(MultiLingualSettingsFragment.class.getName()); + sLatinImeFragments.add(CustomInputStyleSettingsFragment.class.getName()); + sLatinImeFragments.add(GestureSettingsFragment.class.getName()); + sLatinImeFragments.add(CorrectionSettingsFragment.class.getName()); + sLatinImeFragments.add(DebugSettingsFragment.class.getName()); sLatinImeFragments.add(SettingsFragment.class.getName()); sLatinImeFragments.add(SpellCheckerSettingsFragment.class.getName()); sLatinImeFragments.add(UserDictionaryAddWordFragment.class.getName()); |