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.settings.applications.assist; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.provider.Settings; 23 import android.text.TextUtils; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.VisibleForTesting; 27 28 import com.android.car.settings.applications.defaultapps.DefaultAppsPickerBasePreferenceController; 29 import com.android.car.settings.common.FragmentController; 30 import com.android.internal.app.AssistUtils; 31 import com.android.settingslib.applications.DefaultAppInfo; 32 33 import java.util.ArrayList; 34 import java.util.List; 35 import java.util.Objects; 36 37 /** Business logic for displaying and choosing the default voice input service. */ 38 public class DefaultVoiceInputPickerPreferenceController extends 39 DefaultAppsPickerBasePreferenceController { 40 41 private final AssistUtils mAssistUtils; 42 private final VoiceInputInfoProvider mVoiceInputInfoProvider; 43 44 // Current assistant component name, used to restrict available voice inputs. 45 private String mAssistComponentName = null; 46 DefaultVoiceInputPickerPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)47 public DefaultVoiceInputPickerPreferenceController(Context context, String preferenceKey, 48 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 49 this(context, preferenceKey, fragmentController, uxRestrictions, new AssistUtils(context), 50 new VoiceInputInfoProvider(context)); 51 } 52 53 @VisibleForTesting DefaultVoiceInputPickerPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, AssistUtils assistUtils, VoiceInputInfoProvider voiceInputInfoProvider)54 DefaultVoiceInputPickerPreferenceController(Context context, String preferenceKey, 55 FragmentController fragmentController, CarUxRestrictions uxRestrictions, 56 AssistUtils assistUtils, VoiceInputInfoProvider voiceInputInfoProvider) { 57 super(context, preferenceKey, fragmentController, uxRestrictions); 58 mAssistUtils = assistUtils; 59 mVoiceInputInfoProvider = voiceInputInfoProvider; 60 if (Objects.equals(mAssistUtils.getAssistComponentForUser(getCurrentProcessUserId()), 61 VoiceInputUtils.getCurrentService(getContext()))) { 62 ComponentName cn = mAssistUtils.getAssistComponentForUser(getCurrentProcessUserId()); 63 if (cn != null) { 64 mAssistComponentName = cn.flattenToString(); 65 } 66 } 67 } 68 69 @NonNull 70 @Override getCandidates()71 protected List<DefaultAppInfo> getCandidates() { 72 List<DefaultAppInfo> candidates = new ArrayList<>(); 73 for (VoiceInputInfoProvider.VoiceInteractionInfo info : 74 mVoiceInputInfoProvider.getVoiceInteractionInfoList()) { 75 boolean enabled = TextUtils.equals(info.getComponentName().flattenToString(), 76 mAssistComponentName); 77 candidates.add( 78 new DefaultVoiceInputServiceInfo(getContext(), getContext().getPackageManager(), 79 getCurrentProcessUserId(), info, enabled)); 80 } 81 82 for (VoiceInputInfoProvider.VoiceRecognitionInfo info : 83 mVoiceInputInfoProvider.getVoiceRecognitionInfoList()) { 84 candidates.add( 85 new DefaultVoiceInputServiceInfo(getContext(), getContext().getPackageManager(), 86 getCurrentProcessUserId(), info, /* enabled= */ true)); 87 } 88 return candidates; 89 } 90 91 @Override getCurrentDefaultKey()92 protected String getCurrentDefaultKey() { 93 ComponentName cn = VoiceInputUtils.getCurrentService(getContext()); 94 if (cn == null) { 95 return null; 96 } 97 return cn.flattenToString(); 98 } 99 100 @Override setCurrentDefault(String key)101 protected void setCurrentDefault(String key) { 102 ComponentName cn = ComponentName.unflattenFromString(key); 103 VoiceInputInfoProvider.VoiceInputInfo info = mVoiceInputInfoProvider.getInfoForComponent( 104 cn); 105 106 if (info instanceof VoiceInputInfoProvider.VoiceInteractionInfo) { 107 VoiceInputInfoProvider.VoiceInteractionInfo interactionInfo = 108 (VoiceInputInfoProvider.VoiceInteractionInfo) info; 109 Settings.Secure.putString(getContext().getContentResolver(), 110 Settings.Secure.VOICE_INTERACTION_SERVICE, key); 111 Settings.Secure.putString(getContext().getContentResolver(), 112 Settings.Secure.VOICE_RECOGNITION_SERVICE, 113 new ComponentName(interactionInfo.getPackageName(), 114 interactionInfo.getRecognitionService()) 115 .flattenToString()); 116 } else if (info instanceof VoiceInputInfoProvider.VoiceRecognitionInfo) { 117 Settings.Secure.putString(getContext().getContentResolver(), 118 Settings.Secure.VOICE_INTERACTION_SERVICE, ""); 119 Settings.Secure.putString(getContext().getContentResolver(), 120 Settings.Secure.VOICE_RECOGNITION_SERVICE, key); 121 } 122 } 123 124 @Override includeNonePreference()125 protected boolean includeNonePreference() { 126 return false; 127 } 128 } 129