summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/voice/VoiceInputSettings.java
blob: bac297e0f909c939b325c3b205c957945ee28527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.voice;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.preference.Preference;
import android.provider.Settings;
import android.service.voice.VoiceInteractionService;
import android.service.voice.VoiceInteractionServiceInfo;
import android.speech.RecognitionService;
import com.android.internal.logging.MetricsLogger;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.search.SearchIndexableRaw;
import com.android.settings.voice.VoiceInputPreference.RadioButtonGroupState;

import android.os.Bundle;
import android.preference.PreferenceCategory;
import android.widget.Checkable;

import java.util.ArrayList;
import java.util.List;

public class VoiceInputSettings extends SettingsPreferenceFragment implements
        Preference.OnPreferenceClickListener, RadioButtonGroupState, Indexable {

    private static final String TAG = "VoiceInputSettings";
    private static final boolean DBG = false;

    /**
     * Preference key for the engine selection preference.
     */
    private static final String KEY_SERVICE_PREFERENCE_SECTION =
            "voice_service_preference_section";

    private PreferenceCategory mServicePreferenceCategory;

    private CharSequence mInteractorSummary;
    private CharSequence mRecognizerSummary;
    private CharSequence mInteractorWarning;

    /**
     * The currently selected engine.
     */
    private String mCurrentKey;

    /**
     * The engine checkbox that is currently checked. Saves us a bit of effort
     * in deducing the right one from the currently selected engine.
     */
    private Checkable mCurrentChecked;

    private VoiceInputHelper mHelper;

    @Override
    protected int getMetricsCategory() {
        return MetricsLogger.VOICE_INPUT;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.voice_input_settings);

        mServicePreferenceCategory = (PreferenceCategory) findPreference(
                KEY_SERVICE_PREFERENCE_SECTION);

        mInteractorSummary = getActivity().getText(
                R.string.voice_interactor_preference_summary);
        mRecognizerSummary = getActivity().getText(
                R.string.voice_recognizer_preference_summary);
        mInteractorWarning = getActivity().getText(R.string.voice_interaction_security_warning);
    }

    @Override
    public void onStart() {
        super.onStart();
        initSettings();
    }

    private void initSettings() {
        mHelper = new VoiceInputHelper(getActivity());
        mHelper.buildUi();

        mServicePreferenceCategory.removeAll();

        if (mHelper.mCurrentVoiceInteraction != null) {
            mCurrentKey = mHelper.mCurrentVoiceInteraction.flattenToShortString();
        } else if (mHelper.mCurrentRecognizer != null) {
            mCurrentKey = mHelper.mCurrentRecognizer.flattenToShortString();
        } else {
            mCurrentKey = null;
        }

        for (int i=0; i<mHelper.mAvailableInteractionInfos.size(); i++) {
            VoiceInputHelper.InteractionInfo info = mHelper.mAvailableInteractionInfos.get(i);
            VoiceInputPreference pref = new VoiceInputPreference(getActivity(), info,
                    mInteractorSummary, mInteractorWarning, this);
            mServicePreferenceCategory.addPreference(pref);
        }

        for (int i=0; i<mHelper.mAvailableRecognizerInfos.size(); i++) {
            VoiceInputHelper.RecognizerInfo info = mHelper.mAvailableRecognizerInfos.get(i);
            VoiceInputPreference pref = new VoiceInputPreference(getActivity(), info,
                    mRecognizerSummary, null, this);
            mServicePreferenceCategory.addPreference(pref);
        }
    }

    @Override
    public Checkable getCurrentChecked() {
        return mCurrentChecked;
    }

    @Override
    public String getCurrentKey() {
        return mCurrentKey;
    }

    @Override
    public void setCurrentChecked(Checkable current) {
        mCurrentChecked = current;
    }

    @Override
    public void setCurrentKey(String key) {
        mCurrentKey = key;
        for (int i=0; i<mHelper.mAvailableInteractionInfos.size(); i++) {
            VoiceInputHelper.InteractionInfo info = mHelper.mAvailableInteractionInfos.get(i);
            if (info.key.equals(key)) {
                // Put the new value back into secure settings.
                Settings.Secure.putString(getActivity().getContentResolver(),
                        Settings.Secure.VOICE_INTERACTION_SERVICE, key);
                Settings.Secure.putString(getActivity().getContentResolver(),
                        Settings.Secure.VOICE_RECOGNITION_SERVICE,
                        new ComponentName(info.service.packageName,
                                info.serviceInfo.getRecognitionService())
                                .flattenToShortString());
                return;
            }
        }

        for (int i=0; i<mHelper.mAvailableRecognizerInfos.size(); i++) {
            VoiceInputHelper.RecognizerInfo info = mHelper.mAvailableRecognizerInfos.get(i);
            if (info.key.equals(key)) {
                Settings.Secure.putString(getActivity().getContentResolver(),
                        Settings.Secure.VOICE_INTERACTION_SERVICE, "");
                Settings.Secure.putString(getActivity().getContentResolver(),
                        Settings.Secure.VOICE_RECOGNITION_SERVICE, key);
                return;
            }
        }
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        if (preference instanceof VoiceInputPreference) {
            ((VoiceInputPreference)preference).doClick();
        }
        return true;
    }

    // For Search
    public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
        new BaseSearchIndexProvider() {

            @Override
            public List<SearchIndexableRaw> getRawDataToIndex(Context context,
                    boolean enabled) {

                List<SearchIndexableRaw> indexables = new ArrayList<>();

                final String screenTitle = context.getString(R.string.voice_input_settings_title);

                SearchIndexableRaw indexable = new SearchIndexableRaw(context);
                indexable.key = "voice_service_preference_section_title";
                indexable.title = context.getString(R.string.voice_service_preference_section_title);
                indexable.screenTitle = screenTitle;
                indexables.add(indexable);

                final List<ResolveInfo> voiceInteractions =
                        context.getPackageManager().queryIntentServices(
                                new Intent(VoiceInteractionService.SERVICE_INTERFACE),
                                PackageManager.GET_META_DATA);

                final int countInteractions = voiceInteractions.size();
                for (int i = 0; i < countInteractions; i++) {
                    ResolveInfo info = voiceInteractions.get(i);
                    VoiceInteractionServiceInfo visInfo = new VoiceInteractionServiceInfo(
                            context.getPackageManager(), info.serviceInfo);
                    if (visInfo.getParseError() != null) {
                        continue;
                    }
                    indexables.add(getSearchIndexableRaw(context, info, screenTitle));
                }

                final List<ResolveInfo> recognitions =
                        context.getPackageManager().queryIntentServices(
                                new Intent(RecognitionService.SERVICE_INTERFACE),
                                PackageManager.GET_META_DATA);

                final int countRecognitions = recognitions.size();
                for (int i = 0; i < countRecognitions; i++) {
                    ResolveInfo info = recognitions.get(i);
                    indexables.add(getSearchIndexableRaw(context, info, screenTitle));
                }

                return indexables;
            }

            private SearchIndexableRaw getSearchIndexableRaw(Context context,
                    ResolveInfo info, String screenTitle) {

                ServiceInfo serviceInfo = info.serviceInfo;
                ComponentName componentName = new ComponentName(serviceInfo.packageName,
                        serviceInfo.name);

                SearchIndexableRaw indexable = new SearchIndexableRaw(context);
                indexable.key = componentName.flattenToString();
                indexable.title = info.loadLabel(context.getPackageManager()).toString();
                indexable.screenTitle = screenTitle;

                return indexable;
            }
        };
}