1 /*
2  * Copyright (C) 2010 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.settings;
18 
19 import android.preference.Preference;
20 import android.preference.PreferenceCategory;
21 import android.preference.PreferenceGroup;
22 import android.speech.tts.TtsEngines;
23 
24 import com.android.settings.voice.VoiceInputHelper;
25 
26 /**
27  * Settings screen for voice input/output.
28  */
29 public class VoiceInputOutputSettings {
30 
31     private static final String TAG = "VoiceInputOutputSettings";
32 
33     private static final String KEY_VOICE_CATEGORY = "voice_category";
34     private static final String KEY_VOICE_INPUT_SETTINGS = "voice_input_settings";
35     private static final String KEY_TTS_SETTINGS = "tts_settings";
36 
37     private PreferenceGroup mParent;
38     private PreferenceCategory mVoiceCategory;
39     private Preference mVoiceInputSettingsPref;
40     private Preference mTtsSettingsPref;
41     private final SettingsPreferenceFragment mFragment;
42     private final TtsEngines mTtsEngines;
43 
VoiceInputOutputSettings(SettingsPreferenceFragment fragment)44     public VoiceInputOutputSettings(SettingsPreferenceFragment fragment) {
45         mFragment = fragment;
46         mTtsEngines = new TtsEngines(fragment.getPreferenceScreen().getContext());
47     }
48 
onCreate()49     public void onCreate() {
50 
51         mParent = mFragment.getPreferenceScreen();
52         mVoiceCategory = (PreferenceCategory) mParent.findPreference(KEY_VOICE_CATEGORY);
53         mVoiceInputSettingsPref = mVoiceCategory.findPreference(KEY_VOICE_INPUT_SETTINGS);
54         mTtsSettingsPref = mVoiceCategory.findPreference(KEY_TTS_SETTINGS);
55 
56         populateOrRemovePreferences();
57     }
58 
populateOrRemovePreferences()59     private void populateOrRemovePreferences() {
60         boolean hasVoiceInputPrefs = populateOrRemoveVoiceInputPrefs();
61         boolean hasTtsPrefs = populateOrRemoveTtsPrefs();
62         if (!hasVoiceInputPrefs && !hasTtsPrefs) {
63             // There were no TTS settings and no recognizer settings,
64             // so it should be safe to hide the preference category
65             // entirely.
66             mFragment.getPreferenceScreen().removePreference(mVoiceCategory);
67         }
68     }
69 
populateOrRemoveVoiceInputPrefs()70     private boolean populateOrRemoveVoiceInputPrefs() {
71         VoiceInputHelper helper = new VoiceInputHelper(mFragment.getActivity());
72         if (!helper.hasItems()) {
73             mVoiceCategory.removePreference(mVoiceInputSettingsPref);
74             return false;
75         }
76 
77         return true;
78     }
79 
populateOrRemoveTtsPrefs()80     private boolean populateOrRemoveTtsPrefs() {
81         if (mTtsEngines.getEngines().isEmpty()) {
82             mVoiceCategory.removePreference(mTtsSettingsPref);
83             return false;
84         }
85 
86         return true;
87     }
88 }
89