• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.role.RoleManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.provider.Settings;
26 import android.service.voice.VoiceInteractionService;
27 import android.service.voice.VoiceInteractionServiceInfo;
28 import android.text.TextUtils;
29 
30 import androidx.annotation.VisibleForTesting;
31 import androidx.preference.Preference;
32 
33 import com.android.internal.app.AssistUtils;
34 import com.android.settings.R;
35 import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;
36 import com.android.settingslib.applications.DefaultAppInfo;
37 
38 import java.util.List;
39 
40 public class DefaultAssistPreferenceController extends DefaultAppPreferenceController {
41 
42     private final AssistUtils mAssistUtils;
43     private final boolean mShowSetting;
44     private final String mPrefKey;
45     private final Intent mIntent;
46 
DefaultAssistPreferenceController(Context context, String prefKey, boolean showSetting)47     public DefaultAssistPreferenceController(Context context, String prefKey,
48             boolean showSetting) {
49         super(context);
50         mPrefKey = prefKey;
51         mShowSetting = showSetting;
52         mAssistUtils = new AssistUtils(context);
53 
54         final String packageName = mPackageManager.getPermissionControllerPackageName();
55         if (packageName != null) {
56             mIntent = new Intent(Intent.ACTION_MANAGE_DEFAULT_APP)
57                     .setPackage(packageName)
58                     .putExtra(Intent.EXTRA_ROLE_NAME, RoleManager.ROLE_ASSISTANT);
59         } else {
60             mIntent = null;
61         }
62     }
63 
64     @Override
getSettingIntent(DefaultAppInfo info)65     protected Intent getSettingIntent(DefaultAppInfo info) {
66         if (!mShowSetting) {
67             return null;
68         }
69         final ComponentName cn = mAssistUtils.getAssistComponentForUser(mUserId);
70         if (cn == null) {
71             return null;
72         }
73         final Intent probe = new Intent(VoiceInteractionService.SERVICE_INTERFACE)
74                 .setPackage(cn.getPackageName());
75 
76         final List<ResolveInfo> services = mPackageManager.queryIntentServices(probe,
77                 PackageManager.GET_META_DATA);
78         if (services == null || services.isEmpty()) {
79             return null;
80         }
81         final String activity = getAssistSettingsActivity(cn, services.get(0), mPackageManager);
82         if (activity == null) {
83             return null;
84         }
85         return new Intent(Intent.ACTION_MAIN)
86                 .setComponent(new ComponentName(cn.getPackageName(), activity));
87     }
88 
89     @Override
handlePreferenceTreeClick(Preference preference)90     public boolean handlePreferenceTreeClick(Preference preference) {
91         if (TextUtils.equals(preference.getKey(), "default_assist")) {
92             if (mIntent != null) {
93                 mContext.startActivity(mIntent);
94             }
95             return true;
96         }
97         return false;
98     }
99 
100     @Override
isAvailable()101     public boolean isAvailable() {
102         return mContext.getResources().getBoolean(R.bool.config_show_assist_and_voice_input);
103     }
104 
105     @Override
getPreferenceKey()106     public String getPreferenceKey() {
107         return mPrefKey;
108     }
109 
110     @Override
getDefaultAppInfo()111     protected DefaultAppInfo getDefaultAppInfo() {
112         final ComponentName cn = mAssistUtils.getAssistComponentForUser(mUserId);
113         if (cn == null) {
114             return null;
115         }
116         return new DefaultAppInfo(mContext, mPackageManager, mUserId, cn);
117     }
118 
119     @VisibleForTesting
getAssistSettingsActivity(ComponentName cn, ResolveInfo resolveInfo, PackageManager pm)120     String getAssistSettingsActivity(ComponentName cn, ResolveInfo resolveInfo, PackageManager pm) {
121         final VoiceInteractionServiceInfo voiceInfo =
122             new VoiceInteractionServiceInfo(pm, resolveInfo.serviceInfo);
123         if (!voiceInfo.getSupportsAssist()) {
124             return null;
125         }
126         return voiceInfo.getSettingsActivity();
127     }
128 }
129