• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.credentials;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.content.pm.ServiceInfo;
25 import android.credentials.CredentialManager;
26 import android.credentials.CredentialProviderInfo;
27 import android.os.UserHandle;
28 import android.provider.Settings;
29 import android.service.autofill.AutofillService;
30 import android.service.autofill.AutofillServiceInfo;
31 import android.text.TextUtils;
32 import android.util.Log;
33 import android.view.autofill.AutofillManager;
34 
35 import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;
36 import com.android.settingslib.applications.DefaultAppInfo;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 public class DefaultCombinedPreferenceController extends DefaultAppPreferenceController {
42 
43     private static final Intent AUTOFILL_PROBE = new Intent(AutofillService.SERVICE_INTERFACE);
44     private static final String TAG = "DefaultCombinedPreferenceController";
45 
46     private final AutofillManager mAutofillManager;
47     private final CredentialManager mCredentialManager;
48 
DefaultCombinedPreferenceController(Context context)49     public DefaultCombinedPreferenceController(Context context) {
50         super(context);
51 
52         mAutofillManager = mContext.getSystemService(AutofillManager.class);
53 
54         if (CredentialManager.isServiceEnabled(context)) {
55             mCredentialManager = mContext.getSystemService(CredentialManager.class);
56         } else {
57             mCredentialManager = null;
58         }
59     }
60 
61     @Override
isAvailable()62     public boolean isAvailable() {
63         return mAutofillManager != null
64                 && mCredentialManager != null
65                 && mAutofillManager.hasAutofillFeature()
66                 && mAutofillManager.isAutofillSupported();
67     }
68 
69     @Override
getPreferenceKey()70     public String getPreferenceKey() {
71         return "default_credman_autofill_main";
72     }
73 
74     @Override
getSettingIntent(DefaultAppInfo info)75     protected Intent getSettingIntent(DefaultAppInfo info) {
76         if (info == null) {
77             return null;
78         }
79         final AutofillSettingIntentProvider intentProvider =
80                 new AutofillSettingIntentProvider(mContext, getUser(), info.getKey());
81         return intentProvider.getIntent();
82     }
83 
84     @Override
getDefaultAppInfo()85     protected DefaultAppInfo getDefaultAppInfo() {
86         List<CombinedProviderInfo> providers = getAllProviders(getUser());
87         CombinedProviderInfo topProvider = CombinedProviderInfo.getTopProvider(providers);
88         if (topProvider != null) {
89             ServiceInfo brandingService = topProvider.getBrandingService();
90             if (brandingService == null) {
91                 return new DefaultAppInfo(
92                         mContext,
93                         mPackageManager,
94                         getUser(),
95                         topProvider.getApplicationInfo(),
96                         topProvider.getSettingsSubtitle(),
97                         true);
98             } else {
99                 return new DefaultAppInfo(
100                         mContext,
101                         mPackageManager,
102                         getUser(),
103                         brandingService,
104                         topProvider.getSettingsSubtitle(),
105                         true);
106             }
107         }
108         return null;
109     }
110 
getAllProviders(int userId)111     private List<CombinedProviderInfo> getAllProviders(int userId) {
112         final List<AutofillServiceInfo> autofillProviders =
113                 AutofillServiceInfo.getAvailableServices(mContext, userId);
114         final String selectedAutofillProvider =
115                 Settings.Secure.getStringForUser(
116                         mContext.getContentResolver(),
117                         DefaultCombinedPicker.AUTOFILL_SETTING,
118                         userId);
119 
120         final List<CredentialProviderInfo> credManProviders = new ArrayList<>();
121         if (mCredentialManager != null) {
122             credManProviders.addAll(
123                     mCredentialManager.getCredentialProviderServices(
124                             userId, CredentialManager.PROVIDER_FILTER_USER_PROVIDERS_ONLY));
125         }
126 
127         return CombinedProviderInfo.buildMergedList(
128                 autofillProviders, credManProviders, selectedAutofillProvider);
129     }
130 
131     @Override
showLabelAsTitle()132     protected boolean showLabelAsTitle() {
133         return true;
134     }
135 
136     @Override
showAppSummary()137     protected boolean showAppSummary() {
138         return true;
139     }
140 
141     /** Provides Intent to setting activity for the specified autofill service. */
142     static final class AutofillSettingIntentProvider {
143 
144         private final String mKey;
145         private final Context mContext;
146         private final int mUserId;
147 
AutofillSettingIntentProvider(Context context, int userId, String key)148         public AutofillSettingIntentProvider(Context context, int userId, String key) {
149             mKey = key;
150             mContext = context;
151             mUserId = userId;
152         }
153 
getIntent()154         public Intent getIntent() {
155             final List<ResolveInfo> resolveInfos =
156                     mContext.getPackageManager()
157                             .queryIntentServicesAsUser(
158                                     AUTOFILL_PROBE, PackageManager.GET_META_DATA, mUserId);
159 
160             for (ResolveInfo resolveInfo : resolveInfos) {
161                 final ServiceInfo serviceInfo = resolveInfo.serviceInfo;
162 
163                 // If there are multiple autofill services then pick the first one.
164                 if (mKey != null && mKey.startsWith(serviceInfo.packageName)) {
165                     final String settingsActivity;
166                     try {
167                         settingsActivity =
168                                 new AutofillServiceInfo(mContext, serviceInfo)
169                                         .getSettingsActivity();
170                     } catch (SecurityException e) {
171                         // Service does not declare the proper permission, ignore it.
172                         Log.e(TAG, "Error getting info for " + serviceInfo + ": " + e);
173                         return null;
174                     }
175                     if (TextUtils.isEmpty(settingsActivity)) {
176                         return null;
177                     }
178                     return new Intent(Intent.ACTION_MAIN)
179                             .setComponent(
180                                     new ComponentName(serviceInfo.packageName, settingsActivity));
181                 }
182             }
183 
184             return null;
185         }
186     }
187 
getUser()188     protected int getUser() {
189         return UserHandle.myUserId();
190     }
191 }
192