• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.text.TextUtils;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.internal.annotations.Initializer;
31 import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;
32 import com.android.settingslib.applications.DefaultAppInfo;
33 import com.android.settingslib.core.lifecycle.Lifecycle;
34 import com.android.settingslib.core.lifecycle.LifecycleObserver;
35 import com.android.settingslib.core.lifecycle.events.OnPause;
36 import com.android.settingslib.core.lifecycle.events.OnResume;
37 
38 /** Controller of the Voice Input preference. */
39 public class DefaultVoiceInputPreferenceController extends DefaultAppPreferenceController
40         implements LifecycleObserver, OnResume, OnPause {
41 
42     private static final String KEY_VOICE_INPUT = "voice_input_settings";
43 
44     private VoiceInputHelper mHelper;
45     private PreferenceScreen mScreen;
46     private Preference mPreference;
47     private Context mContext;
48 
DefaultVoiceInputPreferenceController( @onNull Context context, @Nullable Lifecycle lifecycle)49     public DefaultVoiceInputPreferenceController(
50             @NonNull Context context, @Nullable Lifecycle lifecycle) {
51         super(context);
52         mContext = context;
53         mHelper = new VoiceInputHelper(context);
54         mHelper.buildUi();
55         if (lifecycle != null) {
56             lifecycle.addObserver(this);
57         }
58     }
59 
60     @Override
isAvailable()61     public boolean isAvailable() {
62         return mContext.getPackageManager().hasSystemFeature(
63                 PackageManager.FEATURE_VOICE_RECOGNIZERS);
64     }
65 
66     @Override
getPreferenceKey()67     public String getPreferenceKey() {
68         return KEY_VOICE_INPUT;
69     }
70 
71     @Override
72     @Initializer
displayPreference(PreferenceScreen screen)73     public void displayPreference(PreferenceScreen screen) {
74         mScreen = screen;
75         mPreference = screen.findPreference(getPreferenceKey());
76         super.displayPreference(screen);
77     }
78 
79     @Override
onResume()80     public void onResume() {
81         updatePreference();
82     }
83 
84     @Override
updateState(Preference preference)85     public void updateState(Preference preference) {
86         super.updateState(mPreference);
87         updatePreference();
88     }
89 
90     @Override
onPause()91     public void onPause() {}
92 
93     @Override
getDefaultAppInfo()94     protected DefaultAppInfo getDefaultAppInfo() {
95         final String defaultKey = getDefaultAppKey();
96         if (defaultKey == null) {
97             return null;
98         }
99 
100         for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
101             if (TextUtils.equals(defaultKey, info.mKey)) {
102                 return new DefaultVoiceInputPicker.VoiceInputDefaultAppInfo(mContext,
103                         mPackageManager, mUserId, info, true /* enabled */);
104             }
105         }
106         return null;
107     }
108 
109     @Override
getSettingIntent(DefaultAppInfo info)110     protected Intent getSettingIntent(DefaultAppInfo info) {
111         final DefaultAppInfo appInfo = getDefaultAppInfo();
112         if (appInfo == null
113                 || !(appInfo instanceof DefaultVoiceInputPicker.VoiceInputDefaultAppInfo)) {
114             return null;
115         }
116         return ((DefaultVoiceInputPicker.VoiceInputDefaultAppInfo) appInfo).getSettingIntent();
117     }
118 
updatePreference()119     private void updatePreference() {
120         if (mPreference == null) {
121             return;
122         }
123         mHelper.buildUi();
124         if (isAvailable()) {
125             if (mScreen.findPreference(getPreferenceKey()) == null) {
126                 // add it if it's not on scree
127                 mScreen.addPreference(mPreference);
128             }
129         } else {
130             mScreen.removePreference(mPreference);
131         }
132     }
133 
getDefaultAppKey()134     private String getDefaultAppKey() {
135         final ComponentName currentService = DefaultVoiceInputPicker.getCurrentService(mHelper);
136         if (currentService == null) {
137             return null;
138         }
139         return currentService.flattenToShortString();
140     }
141 }
142