Home | History | Annotate | Download | only in settings
      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.speech.tts.TtsEngines;
     20 import android.support.v7.preference.Preference;
     21 import android.support.v7.preference.PreferenceCategory;
     22 import android.support.v7.preference.PreferenceGroup;
     23 
     24 /**
     25  * Settings screen for voice input/output.
     26  */
     27 public class VoiceInputOutputSettings {
     28 
     29     private static final String TAG = "VoiceInputOutputSettings";
     30 
     31     private static final String KEY_VOICE_CATEGORY = "voice_category";
     32     private static final String KEY_TTS_SETTINGS = "tts_settings";
     33 
     34     private PreferenceGroup mParent;
     35     private PreferenceCategory mVoiceCategory;
     36     private Preference mVoiceInputSettingsPref;
     37     private Preference mTtsSettingsPref;
     38     private final SettingsPreferenceFragment mFragment;
     39     private final TtsEngines mTtsEngines;
     40 
     41     public VoiceInputOutputSettings(SettingsPreferenceFragment fragment) {
     42         mFragment = fragment;
     43         mTtsEngines = new TtsEngines(fragment.getPreferenceScreen().getContext());
     44     }
     45 
     46     public void onCreate() {
     47         mParent = mFragment.getPreferenceScreen();
     48         mVoiceCategory = (PreferenceCategory) mParent.findPreference(KEY_VOICE_CATEGORY);
     49         mTtsSettingsPref = mVoiceCategory.findPreference(KEY_TTS_SETTINGS);
     50 
     51         populateOrRemovePreferences();
     52     }
     53 
     54     private void populateOrRemovePreferences() {
     55         boolean hasTtsPrefs = populateOrRemoveTtsPrefs();
     56         if (!hasTtsPrefs) {
     57             // There were no TTS settings and no recognizer settings,
     58             // so it should be safe to hide the preference category
     59             // entirely.
     60             mFragment.getPreferenceScreen().removePreference(mVoiceCategory);
     61         }
     62     }
     63 
     64     private boolean populateOrRemoveTtsPrefs() {
     65         if (mTtsEngines.getEngines().isEmpty()) {
     66             mVoiceCategory.removePreference(mTtsSettingsPref);
     67             return false;
     68         }
     69 
     70         return true;
     71     }
     72 }
     73