1 /*
2  * Copyright (C) 2017 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.applications.assist;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.provider.Settings;
25 import android.text.TextUtils;
26 
27 import com.android.settings.R;
28 import com.android.settings.applications.defaultapps.DefaultAppPickerFragment;
29 import com.android.settingslib.applications.DefaultAppInfo;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
35 
36     private VoiceInputHelper mHelper;
37 
38     @Override
getMetricsCategory()39     public int getMetricsCategory() {
40         return SettingsEnums.DEFAULT_VOICE_INPUT_PICKER;
41     }
42 
43     @Override
onAttach(Context context)44     public void onAttach(Context context) {
45         super.onAttach(context);
46         mHelper = new VoiceInputHelper(context);
47         mHelper.buildUi();
48     }
49 
50     @Override
getPreferenceScreenResId()51     protected int getPreferenceScreenResId() {
52         return R.xml.default_voice_settings;
53     }
54 
55     @Override
getCandidates()56     protected List<VoiceInputDefaultAppInfo> getCandidates() {
57         final List<VoiceInputDefaultAppInfo> candidates = new ArrayList<>();
58         final Context context = getContext();
59 
60         for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
61             final boolean enabled = true;
62             candidates.add(new VoiceInputDefaultAppInfo(context, mPm, mUserId, info, enabled));
63         }
64         return candidates;
65     }
66 
67     @Override
getDefaultKey()68     protected String getDefaultKey() {
69         final ComponentName currentService = getCurrentService(mHelper);
70         if (currentService == null) {
71             return null;
72         }
73         return currentService.flattenToShortString();
74     }
75 
76     @Override
setDefaultKey(String value)77     protected boolean setDefaultKey(String value) {
78         for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
79             if (TextUtils.equals(value, info.key)) {
80                 Settings.Secure.putString(getContext().getContentResolver(),
81                         Settings.Secure.VOICE_RECOGNITION_SERVICE, value);
82                 return true;
83             }
84         }
85         return true;
86     }
87 
getCurrentService(VoiceInputHelper helper)88     public static ComponentName getCurrentService(VoiceInputHelper helper) {
89         return helper.mCurrentRecognizer;
90     }
91 
92     public static class VoiceInputDefaultAppInfo extends DefaultAppInfo {
93 
94         public VoiceInputHelper.BaseInfo mInfo;
95 
VoiceInputDefaultAppInfo(Context context, PackageManager pm, int userId, VoiceInputHelper.BaseInfo info, boolean enabled)96         public VoiceInputDefaultAppInfo(Context context, PackageManager pm, int userId,
97                 VoiceInputHelper.BaseInfo info, boolean enabled) {
98             super(context, pm, userId, info.componentName, null /* summary */, enabled);
99             mInfo = info;
100         }
101 
102         @Override
getKey()103         public String getKey() {
104             return mInfo.key;
105         }
106 
107         @Override
loadLabel()108         public CharSequence loadLabel() {
109             return mInfo.label;
110         }
111 
getSettingIntent()112         public Intent getSettingIntent() {
113             if (mInfo.settings == null) {
114                 return null;
115             }
116             return new Intent(Intent.ACTION_MAIN).setComponent(mInfo.settings);
117         }
118     }
119 }
120