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