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.content.Intent; 23 import android.database.ContentObserver; 24 import android.net.Uri; 25 import android.os.Handler; 26 import android.os.Looper; 27 import android.provider.Settings; 28 29 import androidx.annotation.Nullable; 30 31 import com.android.car.settings.applications.defaultapps.DefaultAppsPickerEntryBasePreferenceController; 32 import com.android.car.settings.common.FragmentController; 33 import com.android.internal.app.AssistUtils; 34 import com.android.settingslib.applications.DefaultAppInfo; 35 36 import java.util.Objects; 37 38 /** 39 * Business logic to show the currently selected default voice input service and also link to the 40 * service settings, if it exists. 41 */ 42 public class DefaultVoiceInputPickerEntryPreferenceController extends 43 DefaultAppsPickerEntryBasePreferenceController { 44 45 private static final Uri ASSIST_URI = Settings.Secure.getUriFor(Settings.Secure.ASSISTANT); 46 47 private final ContentObserver mSettingObserver = new ContentObserver( 48 new Handler(Looper.getMainLooper())) { 49 @Override 50 public void onChange(boolean selfChange, Uri uri) { 51 super.onChange(selfChange, uri); 52 53 if (ASSIST_URI.equals(uri)) { 54 refreshUi(); 55 } 56 } 57 }; 58 59 private final VoiceInputInfoProvider mVoiceInputInfoProvider; 60 private final AssistUtils mAssistUtils; 61 DefaultVoiceInputPickerEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)62 public DefaultVoiceInputPickerEntryPreferenceController(Context context, String preferenceKey, 63 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 64 super(context, preferenceKey, fragmentController, uxRestrictions); 65 mVoiceInputInfoProvider = new VoiceInputInfoProvider(context); 66 mAssistUtils = new AssistUtils(context); 67 } 68 69 @Override getAvailabilityStatus()70 protected int getAvailabilityStatus() { 71 ComponentName currentVoiceService = VoiceInputUtils.getCurrentService(getContext()); 72 ComponentName currentAssist = mAssistUtils.getAssistComponentForUser( 73 getCurrentProcessUserId()); 74 75 return Objects.equals(currentAssist, currentVoiceService) ? CONDITIONALLY_UNAVAILABLE 76 : AVAILABLE; 77 } 78 79 @Override onStartInternal()80 protected void onStartInternal() { 81 getContext().getContentResolver().registerContentObserver(ASSIST_URI, 82 /* notifyForDescendants= */ false, mSettingObserver); 83 } 84 85 @Override onStopInternal()86 protected void onStopInternal() { 87 getContext().getContentResolver().unregisterContentObserver(mSettingObserver); 88 } 89 90 @Nullable 91 @Override getCurrentDefaultAppInfo()92 protected DefaultAppInfo getCurrentDefaultAppInfo() { 93 VoiceInputInfoProvider.VoiceInputInfo info = mVoiceInputInfoProvider.getInfoForComponent( 94 VoiceInputUtils.getCurrentService(getContext())); 95 return (info == null) ? null : new DefaultVoiceInputServiceInfo(getContext(), 96 getContext().getPackageManager(), getCurrentProcessUserId(), info, 97 /* enabled= */ true); 98 } 99 100 @Nullable 101 @Override getSettingIntent(@ullable DefaultAppInfo info)102 protected Intent getSettingIntent(@Nullable DefaultAppInfo info) { 103 if (info instanceof DefaultVoiceInputServiceInfo) { 104 return ((DefaultVoiceInputServiceInfo) info).getSettingIntent(); 105 } 106 return null; 107 } 108 } 109