1 /*
2  * Copyright (C) 2019 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.car.developeroptions.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.internal.app.AssistUtils;
28 import com.android.car.developeroptions.R;
29 import com.android.car.developeroptions.applications.defaultapps.DefaultAppPickerFragment;
30 import com.android.settingslib.applications.DefaultAppInfo;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
36 
37     private VoiceInputHelper mHelper;
38     private AssistUtils mAssistUtils;
39     private String mAssistRestrict;
40 
41     @Override
getMetricsCategory()42     public int getMetricsCategory() {
43         return SettingsEnums.DEFAULT_VOICE_INPUT_PICKER;
44     }
45 
46     @Override
onAttach(Context context)47     public void onAttach(Context context) {
48         super.onAttach(context);
49         mAssistUtils = new AssistUtils(context);
50         mHelper = new VoiceInputHelper(context);
51         mHelper.buildUi();
52         final ComponentName assist = getCurrentAssist();
53         if (isCurrentAssistVoiceService(assist, getCurrentService(mHelper))) {
54             mAssistRestrict = assist.flattenToShortString();
55         }
56     }
57 
58     @Override
getPreferenceScreenResId()59     protected int getPreferenceScreenResId() {
60         return R.xml.default_voice_settings;
61     }
62 
63     @Override
getCandidates()64     protected List<VoiceInputDefaultAppInfo> getCandidates() {
65         final List<VoiceInputDefaultAppInfo> candidates = new ArrayList<>();
66         final Context context = getContext();
67         boolean hasEnabled = true;
68         for (VoiceInputHelper.InteractionInfo info : mHelper.mAvailableInteractionInfos) {
69             final boolean enabled = TextUtils.equals(info.key, mAssistRestrict);
70             hasEnabled |= enabled;
71             candidates.add(new VoiceInputDefaultAppInfo(context, mPm, mUserId, info, enabled));
72         }
73 
74         final boolean assistIsService = !hasEnabled;
75         for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
76             final boolean enabled = !assistIsService;
77             candidates.add(new VoiceInputDefaultAppInfo(context, mPm, mUserId, info, enabled));
78         }
79         return candidates;
80     }
81 
82     @Override
getDefaultKey()83     protected String getDefaultKey() {
84         final ComponentName currentService = getCurrentService(mHelper);
85         if (currentService == null) {
86             return null;
87         }
88         return currentService.flattenToShortString();
89     }
90 
91     @Override
setDefaultKey(String value)92     protected boolean setDefaultKey(String value) {
93         for (VoiceInputHelper.InteractionInfo info : mHelper.mAvailableInteractionInfos) {
94             if (TextUtils.equals(value, info.key)) {
95                 Settings.Secure.putString(getContext().getContentResolver(),
96                         Settings.Secure.VOICE_INTERACTION_SERVICE, value);
97                 Settings.Secure.putString(getContext().getContentResolver(),
98                         Settings.Secure.VOICE_RECOGNITION_SERVICE,
99                         new ComponentName(info.service.packageName,
100                                 info.serviceInfo.getRecognitionService())
101                                 .flattenToShortString());
102                 return true;
103             }
104         }
105 
106         for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
107             if (TextUtils.equals(value, info.key)) {
108                 Settings.Secure.putString(getContext().getContentResolver(),
109                         Settings.Secure.VOICE_INTERACTION_SERVICE, "");
110                 Settings.Secure.putString(getContext().getContentResolver(),
111                         Settings.Secure.VOICE_RECOGNITION_SERVICE, value);
112                 return true;
113             }
114         }
115         return true;
116     }
117 
getCurrentService(VoiceInputHelper helper)118     public static ComponentName getCurrentService(VoiceInputHelper helper) {
119         if (helper.mCurrentVoiceInteraction != null) {
120             return helper.mCurrentVoiceInteraction;
121         } else if (helper.mCurrentRecognizer != null) {
122             return helper.mCurrentRecognizer;
123         } else {
124             return null;
125         }
126     }
127 
getCurrentAssist()128     private ComponentName getCurrentAssist() {
129         return mAssistUtils.getAssistComponentForUser(mUserId);
130     }
131 
isCurrentAssistVoiceService(ComponentName currentAssist, ComponentName currentVoiceService)132     public static boolean isCurrentAssistVoiceService(ComponentName currentAssist,
133             ComponentName currentVoiceService) {
134         return currentAssist == null && currentVoiceService == null ||
135                 currentAssist != null && currentAssist.equals(currentVoiceService);
136     }
137 
138     public static class VoiceInputDefaultAppInfo extends DefaultAppInfo {
139 
140         public VoiceInputHelper.BaseInfo mInfo;
141 
VoiceInputDefaultAppInfo(Context context, PackageManager pm, int userId, VoiceInputHelper.BaseInfo info, boolean enabled)142         public VoiceInputDefaultAppInfo(Context context, PackageManager pm, int userId,
143                 VoiceInputHelper.BaseInfo info, boolean enabled) {
144             super(context, pm, userId, info.componentName, null /* summary */, enabled);
145             mInfo = info;
146         }
147 
148         @Override
getKey()149         public String getKey() {
150             return mInfo.key;
151         }
152 
153         @Override
loadLabel()154         public CharSequence loadLabel() {
155             if (mInfo instanceof VoiceInputHelper.InteractionInfo) {
156                 return mInfo.appLabel;
157             } else {
158                 return mInfo.label;
159             }
160         }
161 
getSettingIntent()162         public Intent getSettingIntent() {
163             if (mInfo.settings == null) {
164                 return null;
165             }
166             return new Intent(Intent.ACTION_MAIN).setComponent(mInfo.settings);
167         }
168     }
169 }
170