• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.AssistUtils;
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     @VisibleForTesting
47     static final Intent ASSISTANT_SERVICE = new Intent(
48             VoiceInteractionService.SERVICE_INTERFACE);
49 
50     private final AssistUtils mAssistUtils;
51 
DefaultAssistantPickerEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)52     public DefaultAssistantPickerEntryPreferenceController(Context context, String preferenceKey,
53             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
54         super(context, preferenceKey, fragmentController, uxRestrictions);
55         mAssistUtils = new AssistUtils(context);
56     }
57 
58     @Nullable
59     @Override
getCurrentDefaultAppInfo()60     protected DefaultAppInfo getCurrentDefaultAppInfo() {
61         ComponentName cn = mAssistUtils.getAssistComponentForUser(getCurrentProcessUserId());
62         if (cn == null) {
63             return null;
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 = mAssistUtils.getAssistComponentForUser(getCurrentProcessUserId());
85         if (cn == null) {
86             return null;
87         }
88 
89         Intent probe = ASSISTANT_SERVICE.setPackage(cn.getPackageName());
90         PackageManager pm = getContext().getPackageManager();
91         List<ResolveInfo> services = pm.queryIntentServices(probe, PackageManager.GET_META_DATA);
92         if (services == null || services.isEmpty()) {
93             return null;
94         }
95 
96         String activity = getAssistSettingsActivity(pm, services.get(0));
97         if (activity == null) {
98             return null;
99         }
100 
101         return new Intent(Intent.ACTION_MAIN).setComponent(
102                 new ComponentName(cn.getPackageName(), activity));
103     }
104 
getAssistSettingsActivity(PackageManager pm, ResolveInfo resolveInfo)105     private String getAssistSettingsActivity(PackageManager pm, ResolveInfo resolveInfo) {
106         VoiceInteractionServiceInfo voiceInfo = new VoiceInteractionServiceInfo(pm,
107                 resolveInfo.serviceInfo);
108         if (!voiceInfo.getSupportsAssist()) {
109             return null;
110         }
111         return voiceInfo.getSettingsActivity();
112     }
113 }
114