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.defaultapps; 18 19 import android.app.role.RoleManager; 20 import android.car.drivingstate.CarUxRestrictions; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ResolveInfo; 26 import android.service.voice.VoiceInteractionService; 27 import android.service.voice.VoiceInteractionServiceInfo; 28 29 import androidx.annotation.Nullable; 30 import androidx.annotation.VisibleForTesting; 31 32 import com.android.car.settings.common.FragmentController; 33 import com.android.car.ui.preference.CarUiTwoActionIconPreference; 34 import com.android.internal.util.CollectionUtils; 35 import com.android.settingslib.applications.DefaultAppInfo; 36 37 import java.util.List; 38 39 /** 40 * Business logic to show the currently selected default assistant and also show the assistant 41 * settings, if it exists. 42 */ 43 public class DefaultAssistantPickerEntryPreferenceController extends 44 DefaultAppsPickerEntryBasePreferenceController { 45 46 private static final Intent ASSISTANT_SERVICE = new Intent( 47 VoiceInteractionService.SERVICE_INTERFACE); 48 49 private final RoleManager mRoleManager; 50 DefaultAssistantPickerEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)51 public DefaultAssistantPickerEntryPreferenceController(Context context, String preferenceKey, 52 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 53 super(context, preferenceKey, fragmentController, uxRestrictions); 54 mRoleManager = getContext().getSystemService(RoleManager.class); 55 } 56 57 @Nullable 58 @Override getCurrentDefaultAppInfo()59 protected DefaultAppInfo getCurrentDefaultAppInfo() { 60 ComponentName cn = getComponentName(); 61 if (cn == null) { 62 return null; 63 } 64 65 return new DefaultAppInfo(getContext(), getContext().getPackageManager(), 66 getCurrentProcessUserId(), cn); 67 } 68 69 @Override handlePreferenceClicked(CarUiTwoActionIconPreference preference)70 protected boolean handlePreferenceClicked(CarUiTwoActionIconPreference preference) { 71 String packageName = getContext().getPackageManager().getPermissionControllerPackageName(); 72 if (packageName != null) { 73 Intent intent = new Intent(Intent.ACTION_MANAGE_DEFAULT_APP) 74 .setPackage(packageName) 75 .putExtra(Intent.EXTRA_ROLE_NAME, RoleManager.ROLE_ASSISTANT); 76 getContext().startActivity(intent); 77 } 78 return true; 79 } 80 81 @Nullable 82 @Override getSettingIntent(@ullable DefaultAppInfo info)83 protected Intent getSettingIntent(@Nullable DefaultAppInfo info) { 84 ComponentName cn = getComponentName(); 85 if (cn == null) { 86 return null; 87 } 88 89 return new Intent(Intent.ACTION_MAIN).setComponent(cn); 90 } 91 getComponentName()92 private ComponentName getComponentName() { 93 String assistantPkgName = CollectionUtils.firstOrNull( 94 mRoleManager.getRoleHolders(RoleManager.ROLE_ASSISTANT)); 95 96 if (assistantPkgName == null) { 97 return null; 98 } 99 100 Intent probe = ASSISTANT_SERVICE.setPackage(assistantPkgName); 101 PackageManager pm = getContext().getPackageManager(); 102 List<ResolveInfo> services = pm.queryIntentServices(probe, PackageManager.GET_META_DATA); 103 if (services == null || services.isEmpty()) { 104 return null; 105 } 106 107 String activity = getAssistSettingsActivity(pm, services.get(0)); 108 if (activity == null) { 109 return null; 110 } 111 112 return new ComponentName(assistantPkgName, activity); 113 } 114 115 @VisibleForTesting getAssistSettingsActivity(PackageManager pm, ResolveInfo resolveInfo)116 String getAssistSettingsActivity(PackageManager pm, ResolveInfo resolveInfo) { 117 VoiceInteractionServiceInfo voiceInfo = new VoiceInteractionServiceInfo(pm, 118 resolveInfo.serviceInfo); 119 if (!voiceInfo.getSupportsAssist()) { 120 return null; 121 } 122 return voiceInfo.getSettingsActivity(); 123 } 124 } 125