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 
17 package com.android.inputmethod.latin.settings;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.content.res.Resources;
22 import android.os.Bundle;
23 import android.preference.Preference;
24 
25 import com.android.inputmethod.latin.AudioAndHapticFeedbackManager;
26 import com.android.inputmethod.latin.R;
27 import com.android.inputmethod.latin.SubtypeSwitcher;
28 
29 /**
30  * "Preferences" settings sub screen.
31  *
32  * This settings sub screen handles the following input preferences.
33  * - Auto-capitalization
34  * - Double-space period
35  * - Vibrate on keypress
36  * - Sound on keypress
37  * - Popup on keypress
38  * - Voice input key
39  */
40 public final class PreferencesSettingsFragment extends SubScreenFragment {
41     @Override
onCreate(final Bundle icicle)42     public void onCreate(final Bundle icicle) {
43         super.onCreate(icicle);
44         addPreferencesFromResource(R.xml.prefs_screen_preferences);
45 
46         final Resources res = getResources();
47         final Context context = getActivity();
48 
49         // When we are called from the Settings application but we are not already running, some
50         // singleton and utility classes may not have been initialized.  We have to call
51         // initialization method of these classes here. See {@link LatinIME#onCreate()}.
52         SubtypeSwitcher.init(context);
53 
54         final boolean showVoiceKeyOption = res.getBoolean(
55                 R.bool.config_enable_show_voice_key_option);
56         if (!showVoiceKeyOption) {
57             removePreference(Settings.PREF_VOICE_INPUT_KEY);
58         }
59         if (!AudioAndHapticFeedbackManager.getInstance().hasVibrator()) {
60             removePreference(Settings.PREF_VIBRATE_ON);
61         }
62         if (!Settings.readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) {
63             removePreference(Settings.PREF_POPUP_ON);
64         }
65 
66         refreshEnablingsOfKeypressSoundAndVibrationSettings();
67     }
68 
69     @Override
onResume()70     public void onResume() {
71         super.onResume();
72         final Preference voiceInputKeyOption = findPreference(Settings.PREF_VOICE_INPUT_KEY);
73         if (voiceInputKeyOption != null) {
74             final boolean isShortcutImeEnabled = SubtypeSwitcher.getInstance()
75                     .isShortcutImeEnabled();
76             voiceInputKeyOption.setEnabled(isShortcutImeEnabled);
77             voiceInputKeyOption.setSummary(
78                     isShortcutImeEnabled ? null : getText(R.string.voice_input_disabled_summary));
79         }
80     }
81 
82     @Override
onSharedPreferenceChanged(final SharedPreferences prefs, final String key)83     public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
84         final Resources res = getResources();
85         if (key.equals(Settings.PREF_POPUP_ON)) {
86             setPreferenceEnabled(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY,
87                     Settings.readKeyPreviewPopupEnabled(prefs, res));
88         }
89         refreshEnablingsOfKeypressSoundAndVibrationSettings();
90     }
91 
refreshEnablingsOfKeypressSoundAndVibrationSettings()92     private void refreshEnablingsOfKeypressSoundAndVibrationSettings() {
93         final SharedPreferences prefs = getSharedPreferences();
94         final Resources res = getResources();
95         setPreferenceEnabled(Settings.PREF_VIBRATION_DURATION_SETTINGS,
96                 Settings.readVibrationEnabled(prefs, res));
97         setPreferenceEnabled(Settings.PREF_KEYPRESS_SOUND_VOLUME,
98                 Settings.readKeypressSoundEnabled(prefs, res));
99     }
100 }
101