Refactor Debug settings to use SubScreenFragment

Bug: 16522808
Change-Id: I741e2fbc5f0a62c7d4ae00e603eef52cc2a6d086
Tadashi G. Takaoka 2014-07-24 00:48:52 -07:00
parent 00b49cad9c
commit 99295c8c5f
4 changed files with 289 additions and 268 deletions

View File

@ -68,7 +68,7 @@
android:defaultValue="true"
android:persistent="true" />
<PreferenceScreen
android:fragment="com.android.inputmethod.latin.settings.DebugSettings"
android:fragment="com.android.inputmethod.latin.settings.DebugSettingsFragment"
android:key="screen_debug"
android:title="Debug settings"
android:defaultValue="false"

View File

@ -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.
}
}

View File

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

View File

@ -20,7 +20,7 @@ import com.android.inputmethod.dictionarypack.DictionarySettingsFragment;
import com.android.inputmethod.latin.about.AboutPreferences;
import com.android.inputmethod.latin.settings.CorrectionSettingsFragment;
import com.android.inputmethod.latin.settings.CustomInputStyleSettingsFragment;
import com.android.inputmethod.latin.settings.DebugSettings;
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;
@ -43,7 +43,7 @@ public class FragmentUtils {
sLatinImeFragments.add(CustomInputStyleSettingsFragment.class.getName());
sLatinImeFragments.add(GestureSettingsFragment.class.getName());
sLatinImeFragments.add(CorrectionSettingsFragment.class.getName());
sLatinImeFragments.add(DebugSettings.class.getName());
sLatinImeFragments.add(DebugSettingsFragment.class.getName());
sLatinImeFragments.add(SettingsFragment.class.getName());
sLatinImeFragments.add(SpellCheckerSettingsFragment.class.getName());
sLatinImeFragments.add(UserDictionaryAddWordFragment.class.getName());