AdvancedSettingsFragment.java revision 9bdcb131602aa614db37d817d7fe1220ab6271b8
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.latin.settings;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.content.res.Resources;
22import android.media.AudioManager;
23import android.os.Bundle;
24import android.preference.ListPreference;
25import android.preference.Preference;
26import android.preference.TwoStatePreference;
27
28import com.android.inputmethod.latin.AudioAndHapticFeedbackManager;
29import com.android.inputmethod.latin.R;
30import com.android.inputmethod.latin.define.ProductionFlags;
31import com.android.inputmethod.latin.setup.LauncherIconVisibilityManager;
32
33/**
34 * "Advanced" settings sub screen.
35 *
36 * This settings sub screen handles the following advanced preferences.
37 * - Key popup dismiss delay
38 * - Keypress vibration duration
39 * - Keypress sound volume
40 * - Show app icon
41 * - Improve keyboard
42 * - Debug settings
43 */
44public final class AdvancedSettingsFragment extends SubScreenFragment {
45    @Override
46    public void onCreate(final Bundle icicle) {
47        super.onCreate(icicle);
48        addPreferencesFromResource(R.xml.prefs_screen_advanced);
49
50        final Resources res = getResources();
51        final Context context = getActivity();
52
53        // When we are called from the Settings application but we are not already running, some
54        // singleton and utility classes may not have been initialized.  We have to call
55        // initialization method of these classes here. See {@link LatinIME#onCreate()}.
56        AudioAndHapticFeedbackManager.init(context);
57
58        final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
59
60        if (!Settings.isInternal(prefs)) {
61            removePreference(Settings.SCREEN_DEBUG);
62        }
63
64        if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) {
65            removePreference(Settings.PREF_VIBRATION_DURATION_SETTINGS);
66        }
67
68        // TODO: consolidate key preview dismiss delay with the key preview animation parameters.
69        if (!Settings.readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) {
70            removePreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
71        } else {
72            // TODO: Cleanup this setup.
73            final ListPreference keyPreviewPopupDismissDelay =
74                    (ListPreference) findPreference(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
75            final String popupDismissDelayDefaultValue = Integer.toString(res.getInteger(
76                    R.integer.config_key_preview_linger_timeout));
77            keyPreviewPopupDismissDelay.setEntries(new String[] {
78                    res.getString(R.string.key_preview_popup_dismiss_no_delay),
79                    res.getString(R.string.key_preview_popup_dismiss_default_delay),
80            });
81            keyPreviewPopupDismissDelay.setEntryValues(new String[] {
82                    "0",
83                    popupDismissDelayDefaultValue
84            });
85            if (null == keyPreviewPopupDismissDelay.getValue()) {
86                keyPreviewPopupDismissDelay.setValue(popupDismissDelayDefaultValue);
87            }
88            keyPreviewPopupDismissDelay.setEnabled(
89                    Settings.readKeyPreviewPopupEnabled(prefs, res));
90        }
91
92        if (!res.getBoolean(R.bool.config_setup_wizard_available)) {
93            removePreference(Settings.PREF_SHOW_SETUP_WIZARD_ICON);
94        }
95
96        if (ProductionFlags.IS_METRICS_LOGGING_SUPPORTED) {
97            final Preference enableMetricsLogging =
98                    findPreference(Settings.PREF_ENABLE_METRICS_LOGGING);
99            if (enableMetricsLogging != null) {
100                final int applicationLabelRes = context.getApplicationInfo().labelRes;
101                final String applicationName = res.getString(applicationLabelRes);
102                final String enableMetricsLoggingTitle = res.getString(
103                        R.string.enable_metrics_logging, applicationName);
104                enableMetricsLogging.setTitle(enableMetricsLoggingTitle);
105            }
106        } else {
107            removePreference(Settings.PREF_ENABLE_METRICS_LOGGING);
108        }
109
110        setupKeypressVibrationDurationSettings();
111        setupKeypressSoundVolumeSettings();
112        refreshEnablingsOfKeypressSoundAndVibrationSettings();
113    }
114
115    @Override
116    public void onResume() {
117        super.onResume();
118        final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
119        final TwoStatePreference showSetupWizardIcon =
120                (TwoStatePreference)findPreference(Settings.PREF_SHOW_SETUP_WIZARD_ICON);
121        if (showSetupWizardIcon != null) {
122            showSetupWizardIcon.setChecked(Settings.readShowSetupWizardIcon(prefs, getActivity()));
123        }
124        updateListPreferenceSummaryToCurrentValue(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
125    }
126
127    @Override
128    public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
129        final Resources res = getResources();
130        if (key.equals(Settings.PREF_POPUP_ON)) {
131            setPreferenceEnabled(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY,
132                    Settings.readKeyPreviewPopupEnabled(prefs, res));
133        } else if (key.equals(Settings.PREF_SHOW_SETUP_WIZARD_ICON)) {
134            LauncherIconVisibilityManager.updateSetupWizardIconVisibility(getActivity());
135        }
136        updateListPreferenceSummaryToCurrentValue(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
137        refreshEnablingsOfKeypressSoundAndVibrationSettings();
138    }
139
140    private void refreshEnablingsOfKeypressSoundAndVibrationSettings() {
141        final SharedPreferences prefs = getSharedPreferences();
142        final Resources res = getResources();
143        setPreferenceEnabled(Settings.PREF_VIBRATION_DURATION_SETTINGS,
144                Settings.readVibrationEnabled(prefs, res));
145        setPreferenceEnabled(Settings.PREF_KEYPRESS_SOUND_VOLUME,
146                Settings.readKeypressSoundEnabled(prefs, res));
147    }
148
149    private void setupKeypressVibrationDurationSettings() {
150        final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(
151                Settings.PREF_VIBRATION_DURATION_SETTINGS);
152        if (pref == null) {
153            return;
154        }
155        final SharedPreferences prefs = getSharedPreferences();
156        final Resources res = getResources();
157        pref.setInterface(new SeekBarDialogPreference.ValueProxy() {
158            @Override
159            public void writeValue(final int value, final String key) {
160                prefs.edit().putInt(key, value).apply();
161            }
162
163            @Override
164            public void writeDefaultValue(final String key) {
165                prefs.edit().remove(key).apply();
166            }
167
168            @Override
169            public int readValue(final String key) {
170                return Settings.readKeypressVibrationDuration(prefs, res);
171            }
172
173            @Override
174            public int readDefaultValue(final String key) {
175                return Settings.readDefaultKeypressVibrationDuration(res);
176            }
177
178            @Override
179            public void feedbackValue(final int value) {
180                AudioAndHapticFeedbackManager.getInstance().vibrate(value);
181            }
182
183            @Override
184            public String getValueText(final int value) {
185                if (value < 0) {
186                    return res.getString(R.string.settings_system_default);
187                }
188                return res.getString(R.string.abbreviation_unit_milliseconds, value);
189            }
190        });
191    }
192
193    private void setupKeypressSoundVolumeSettings() {
194        final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(
195                Settings.PREF_KEYPRESS_SOUND_VOLUME);
196        if (pref == null) {
197            return;
198        }
199        final SharedPreferences prefs = getSharedPreferences();
200        final Resources res = getResources();
201        final AudioManager am = (AudioManager)getActivity().getSystemService(Context.AUDIO_SERVICE);
202        pref.setInterface(new SeekBarDialogPreference.ValueProxy() {
203            private static final float PERCENTAGE_FLOAT = 100.0f;
204
205            private float getValueFromPercentage(final int percentage) {
206                return percentage / PERCENTAGE_FLOAT;
207            }
208
209            private int getPercentageFromValue(final float floatValue) {
210                return (int)(floatValue * PERCENTAGE_FLOAT);
211            }
212
213            @Override
214            public void writeValue(final int value, final String key) {
215                prefs.edit().putFloat(key, getValueFromPercentage(value)).apply();
216            }
217
218            @Override
219            public void writeDefaultValue(final String key) {
220                prefs.edit().remove(key).apply();
221            }
222
223            @Override
224            public int readValue(final String key) {
225                return getPercentageFromValue(Settings.readKeypressSoundVolume(prefs, res));
226            }
227
228            @Override
229            public int readDefaultValue(final String key) {
230                return getPercentageFromValue(Settings.readDefaultKeypressSoundVolume(res));
231            }
232
233            @Override
234            public String getValueText(final int value) {
235                if (value < 0) {
236                    return res.getString(R.string.settings_system_default);
237                }
238                return Integer.toString(value);
239            }
240
241            @Override
242            public void feedbackValue(final int value) {
243                am.playSoundEffect(
244                        AudioManager.FX_KEYPRESS_STANDARD, getValueFromPercentage(value));
245            }
246        });
247    }
248}
249