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