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