1 /*
2  * Copyright (C) 2022 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.language;
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 /** Controls the Voice Input setting. */
35 public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
36 
37     private VoiceInputHelper mHelper;
38 
39     @Override
getMetricsCategory()40     public int getMetricsCategory() {
41         return SettingsEnums.DEFAULT_VOICE_INPUT_PICKER;
42     }
43 
44     @Override
onAttach(Context context)45     public void onAttach(Context context) {
46         super.onAttach(context);
47         mHelper = new VoiceInputHelper(context);
48         mHelper.buildUi();
49     }
50 
51     @Override
getPreferenceScreenResId()52     protected int getPreferenceScreenResId() {
53         return R.xml.default_voice_settings;
54     }
55 
56     @Override
getCandidates()57     protected List<VoiceInputDefaultAppInfo> getCandidates() {
58         final List<VoiceInputDefaultAppInfo> candidates = new ArrayList<>();
59         final Context context = getContext();
60 
61         for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
62             final boolean enabled = true;
63             candidates.add(new VoiceInputDefaultAppInfo(context, mPm, mUserId, info, enabled));
64         }
65         return candidates;
66     }
67 
68     @Override
getDefaultKey()69     protected String getDefaultKey() {
70         final ComponentName currentService = getCurrentService(mHelper);
71         if (currentService == null) {
72             return null;
73         }
74         return currentService.flattenToShortString();
75     }
76 
77     @Override
setDefaultKey(String value)78     protected boolean setDefaultKey(String value) {
79         for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
80             if (TextUtils.equals(value, info.mKey)) {
81                 Settings.Secure.putString(getContext().getContentResolver(),
82                         Settings.Secure.VOICE_RECOGNITION_SERVICE, value);
83                 return true;
84             }
85         }
86         return true;
87     }
88 
89     /** Gets the current recognition service component. */
getCurrentService(VoiceInputHelper helper)90     public static ComponentName getCurrentService(VoiceInputHelper helper) {
91         return helper.mCurrentRecognizer;
92     }
93 
94     /** Stores the info of the Voice Input provider. */
95     public static class VoiceInputDefaultAppInfo extends DefaultAppInfo {
96 
97         public VoiceInputHelper.BaseInfo mInfo;
98 
VoiceInputDefaultAppInfo(Context context, PackageManager pm, int userId, VoiceInputHelper.BaseInfo info, boolean enabled)99         public VoiceInputDefaultAppInfo(Context context, PackageManager pm, int userId,
100                 VoiceInputHelper.BaseInfo info, boolean enabled) {
101             super(context, pm, userId, info.mComponentName, null /* summary */, enabled);
102             mInfo = info;
103         }
104 
105         @Override
getKey()106         public String getKey() {
107             return mInfo.mKey;
108         }
109 
110         @Override
loadLabel()111         public CharSequence loadLabel() {
112             return mInfo.mLabel;
113         }
114 
115         /** Gets the setting intent. */
getSettingIntent()116         public Intent getSettingIntent() {
117             if (mInfo.mSettings == null) {
118                 return null;
119             }
120             return new Intent(Intent.ACTION_MAIN).setComponent(mInfo.mSettings);
121         }
122     }
123 }
124