blob: e052f8ebf0f33e403662348eecd2306ab1d0ad01 [file] [log] [blame]
Mike LeBeau92c33522010-01-25 18:18:45 -05001/*
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
17package com.android.settings;
18
Mike LeBeau92c33522010-01-25 18:18:45 -050019import android.preference.Preference;
Mike LeBeau766a19b2010-02-11 22:57:12 -080020import android.preference.PreferenceCategory;
Mike LeBeau92c33522010-01-25 18:18:45 -050021import android.preference.PreferenceGroup;
Narayan Kamathe247cb82011-10-05 12:26:31 +010022import android.speech.tts.TtsEngines;
Mike LeBeau92c33522010-01-25 18:18:45 -050023
Dianne Hackbornff795ff2014-07-18 19:20:11 -070024import com.android.settings.voice.VoiceInputHelper;
Amith Yamasanib44161f2010-12-10 13:17:34 -080025
Mike LeBeau92c33522010-01-25 18:18:45 -050026/**
27 * Settings screen for voice input/output.
28 */
Dianne Hackbornff795ff2014-07-18 19:20:11 -070029public class VoiceInputOutputSettings {
Amith Yamasanib44161f2010-12-10 13:17:34 -080030
Mike LeBeau766a19b2010-02-11 22:57:12 -080031 private static final String TAG = "VoiceInputOutputSettings";
Amith Yamasanib44161f2010-12-10 13:17:34 -080032
Jean Chalard8ba5c422011-08-31 21:40:43 +090033 private static final String KEY_VOICE_CATEGORY = "voice_category";
Dianne Hackbornff795ff2014-07-18 19:20:11 -070034 private static final String KEY_VOICE_INPUT_SETTINGS = "voice_input_settings";
Narayan Kamathe247cb82011-10-05 12:26:31 +010035 private static final String KEY_TTS_SETTINGS = "tts_settings";
36
Mike LeBeau766a19b2010-02-11 22:57:12 -080037 private PreferenceGroup mParent;
Jean Chalard8ba5c422011-08-31 21:40:43 +090038 private PreferenceCategory mVoiceCategory;
Dianne Hackbornff795ff2014-07-18 19:20:11 -070039 private Preference mVoiceInputSettingsPref;
Narayan Kamathe247cb82011-10-05 12:26:31 +010040 private Preference mTtsSettingsPref;
Narayan Kamathe247cb82011-10-05 12:26:31 +010041 private final SettingsPreferenceFragment mFragment;
42 private final TtsEngines mTtsEngines;
Amith Yamasanib44161f2010-12-10 13:17:34 -080043
Amith Yamasanib44161f2010-12-10 13:17:34 -080044 public VoiceInputOutputSettings(SettingsPreferenceFragment fragment) {
45 mFragment = fragment;
Narayan Kamathe247cb82011-10-05 12:26:31 +010046 mTtsEngines = new TtsEngines(fragment.getPreferenceScreen().getContext());
Amith Yamasanib44161f2010-12-10 13:17:34 -080047 }
Mike LeBeau766a19b2010-02-11 22:57:12 -080048
Amith Yamasanib44161f2010-12-10 13:17:34 -080049 public void onCreate() {
50
Narayan Kamathe247cb82011-10-05 12:26:31 +010051 mParent = mFragment.getPreferenceScreen();
Jean Chalard8ba5c422011-08-31 21:40:43 +090052 mVoiceCategory = (PreferenceCategory) mParent.findPreference(KEY_VOICE_CATEGORY);
Dianne Hackbornff795ff2014-07-18 19:20:11 -070053 mVoiceInputSettingsPref = mVoiceCategory.findPreference(KEY_VOICE_INPUT_SETTINGS);
Dianne Hackborndf7e99b2014-04-17 18:35:29 -070054 mTtsSettingsPref = mVoiceCategory.findPreference(KEY_TTS_SETTINGS);
Amith Yamasanib44161f2010-12-10 13:17:34 -080055
Narayan Kamathe247cb82011-10-05 12:26:31 +010056 populateOrRemovePreferences();
Mike LeBeau92c33522010-01-25 18:18:45 -050057 }
Amith Yamasanib44161f2010-12-10 13:17:34 -080058
Narayan Kamathe247cb82011-10-05 12:26:31 +010059 private void populateOrRemovePreferences() {
Dianne Hackbornff795ff2014-07-18 19:20:11 -070060 boolean hasVoiceInputPrefs = populateOrRemoveVoiceInputPrefs();
Narayan Kamathe247cb82011-10-05 12:26:31 +010061 boolean hasTtsPrefs = populateOrRemoveTtsPrefs();
Dianne Hackbornff795ff2014-07-18 19:20:11 -070062 if (!hasVoiceInputPrefs && !hasTtsPrefs) {
Narayan Kamathe247cb82011-10-05 12:26:31 +010063 // 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
Dianne Hackbornff795ff2014-07-18 19:20:11 -070070 private boolean populateOrRemoveVoiceInputPrefs() {
71 VoiceInputHelper helper = new VoiceInputHelper(mFragment.getActivity());
72 if (!helper.hasItems()) {
73 mVoiceCategory.removePreference(mVoiceInputSettingsPref);
Dianne Hackborndf7e99b2014-04-17 18:35:29 -070074 return false;
75 }
76
Narayan Kamathe247cb82011-10-05 12:26:31 +010077 return true;
78 }
79
80 private boolean populateOrRemoveTtsPrefs() {
81 if (mTtsEngines.getEngines().isEmpty()) {
82 mVoiceCategory.removePreference(mTtsSettingsPref);
83 return false;
84 }
85
86 return true;
Mike LeBeau766a19b2010-02-11 22:57:12 -080087 }
Mike LeBeau92c33522010-01-25 18:18:45 -050088}