1 /* 2 * Copyright (C) 2017 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.applications.assist; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.net.Uri; 23 import android.support.v7.preference.Preference; 24 import android.support.v7.preference.PreferenceScreen; 25 import android.text.TextUtils; 26 27 import com.android.internal.app.AssistUtils; 28 import com.android.settings.applications.defaultapps.DefaultAppInfo; 29 import com.android.settings.applications.defaultapps.DefaultAppPreferenceController; 30 import com.android.settings.core.lifecycle.Lifecycle; 31 import com.android.settings.core.lifecycle.LifecycleObserver; 32 import com.android.settings.core.lifecycle.events.OnPause; 33 import com.android.settings.core.lifecycle.events.OnResume; 34 35 import java.util.List; 36 37 public class DefaultVoiceInputPreferenceController extends DefaultAppPreferenceController 38 implements LifecycleObserver, OnResume, OnPause { 39 40 private static final String KEY_VOICE_INPUT = "voice_input_settings"; 41 42 private VoiceInputHelper mHelper; 43 private AssistUtils mAssistUtils; 44 private PreferenceScreen mScreen; 45 private Preference mPreference; 46 private SettingObserver mSettingObserver; 47 48 public DefaultVoiceInputPreferenceController(Context context, Lifecycle lifecycle) { 49 super(context); 50 mSettingObserver = new SettingObserver(); 51 mAssistUtils = new AssistUtils(context); 52 mHelper = new VoiceInputHelper(context); 53 mHelper.buildUi(); 54 if (lifecycle != null) { 55 lifecycle.addObserver(this); 56 } 57 } 58 59 @Override 60 public boolean isAvailable() { 61 // If current assist is also voice service, don't show voice preference. 62 final ComponentName currentVoiceService = 63 DefaultVoiceInputPicker.getCurrentService(mHelper); 64 final ComponentName currentAssist = 65 mAssistUtils.getAssistComponentForUser(mUserId); 66 return !DefaultVoiceInputPicker.isCurrentAssistVoiceService( 67 currentAssist, currentVoiceService); 68 } 69 70 @Override 71 public String getPreferenceKey() { 72 return KEY_VOICE_INPUT; 73 } 74 75 @Override 76 public void displayPreference(PreferenceScreen screen) { 77 mScreen = screen; 78 mPreference = screen.findPreference(getPreferenceKey()); 79 super.displayPreference(screen); 80 } 81 82 @Override 83 public void onResume() { 84 mSettingObserver.register(mContext.getContentResolver(), true); 85 updatePreference(); 86 } 87 88 @Override 89 public void updateState(Preference preference) { 90 super.updateState(mPreference); 91 updatePreference(); 92 } 93 94 @Override 95 public void onPause() { 96 mSettingObserver.register(mContext.getContentResolver(), false); 97 } 98 99 @Override 100 protected DefaultAppInfo getDefaultAppInfo() { 101 final String defaultKey = getDefaultAppKey(); 102 if (defaultKey == null) { 103 return null; 104 } 105 for (VoiceInputHelper.InteractionInfo info : mHelper.mAvailableInteractionInfos) { 106 if (TextUtils.equals(defaultKey, info.key)) { 107 return new DefaultVoiceInputPicker.VoiceInputDefaultAppInfo(mPackageManager, 108 mUserId, info, true /* enabled */); 109 } 110 } 111 112 for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) { 113 if (TextUtils.equals(defaultKey, info.key)) { 114 return new DefaultVoiceInputPicker.VoiceInputDefaultAppInfo(mPackageManager, 115 mUserId, info, true /* enabled */); 116 } 117 } 118 return null; 119 } 120 121 @Override 122 protected Intent getSettingIntent(DefaultAppInfo info) { 123 final DefaultAppInfo appInfo = getDefaultAppInfo(); 124 if (appInfo == null 125 || !(appInfo instanceof DefaultVoiceInputPicker.VoiceInputDefaultAppInfo)) { 126 return null; 127 } 128 return ((DefaultVoiceInputPicker.VoiceInputDefaultAppInfo) appInfo).getSettingIntent(); 129 } 130 131 private void updatePreference() { 132 if (mPreference == null) { 133 return; 134 } 135 mHelper.buildUi(); 136 if (isAvailable()) { 137 if (mScreen.findPreference(getPreferenceKey()) == null) { 138 // add it if it's not on scree 139 mScreen.addPreference(mPreference); 140 } 141 } else { 142 mScreen.removePreference(mPreference); 143 } 144 } 145 146 private String getDefaultAppKey() { 147 final ComponentName currentService = DefaultVoiceInputPicker.getCurrentService(mHelper); 148 if (currentService == null) { 149 return null; 150 } 151 return currentService.flattenToShortString(); 152 } 153 154 class SettingObserver extends AssistSettingObserver { 155 @Override 156 protected List<Uri> getSettingUris() { 157 return null; 158 } 159 160 @Override 161 public void onSettingChange() { 162 updatePreference(); 163 } 164 } 165 } 166