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.car.drivingstate.CarUxRestrictions; 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.content.pm.ServiceInfo; 26 import android.provider.Settings; 27 import android.service.autofill.AutofillService; 28 import android.service.autofill.AutofillServiceInfo; 29 import android.text.TextUtils; 30 import android.view.autofill.AutofillManager; 31 32 import androidx.annotation.Nullable; 33 34 import com.android.car.settings.common.FragmentController; 35 import com.android.car.settings.common.Logger; 36 import com.android.settingslib.applications.DefaultAppInfo; 37 38 import java.util.List; 39 40 /** Business logic for displaying the currently selected autofill app. */ 41 public class DefaultAutofillPickerEntryPreferenceController extends 42 DefaultAppsPickerEntryBasePreferenceController { 43 44 private static final Logger LOG = new Logger( 45 DefaultAutofillPickerEntryPreferenceController.class); 46 private final AutofillManager mAutofillManager; 47 DefaultAutofillPickerEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)48 public DefaultAutofillPickerEntryPreferenceController(Context context, String preferenceKey, 49 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 50 super(context, preferenceKey, fragmentController, uxRestrictions); 51 mAutofillManager = context.getSystemService(AutofillManager.class); 52 } 53 54 @Override getAvailabilityStatus()55 protected int getAvailabilityStatus() { 56 if (mAutofillManager != null && mAutofillManager.isAutofillSupported()) { 57 return AVAILABLE; 58 } 59 return UNSUPPORTED_ON_DEVICE; 60 } 61 62 @Nullable 63 @Override getCurrentDefaultAppInfo()64 protected DefaultAppInfo getCurrentDefaultAppInfo() { 65 String flattenComponent = Settings.Secure.getString(getContext().getContentResolver(), 66 Settings.Secure.AUTOFILL_SERVICE); 67 if (!TextUtils.isEmpty(flattenComponent)) { 68 DefaultAppInfo appInfo = new DefaultAppInfo(getContext(), 69 getContext().getPackageManager(), getCurrentProcessUserId(), 70 ComponentName.unflattenFromString(flattenComponent)); 71 return appInfo; 72 } 73 return null; 74 } 75 76 @Nullable 77 @Override getSettingIntent(@ullable DefaultAppInfo info)78 protected Intent getSettingIntent(@Nullable DefaultAppInfo info) { 79 if (info == null) { 80 return null; 81 } 82 83 Intent intent = new Intent(AutofillService.SERVICE_INTERFACE); 84 List<ResolveInfo> resolveInfos = getContext().getPackageManager().queryIntentServices( 85 intent, PackageManager.GET_META_DATA); 86 87 for (ResolveInfo resolveInfo : resolveInfos) { 88 ServiceInfo serviceInfo = resolveInfo.serviceInfo; 89 String flattenKey = new ComponentName(serviceInfo.packageName, 90 serviceInfo.name).flattenToString(); 91 if (TextUtils.equals(info.getKey(), flattenKey)) { 92 String settingsActivity; 93 try { 94 settingsActivity = new AutofillServiceInfo(getContext(), serviceInfo) 95 .getSettingsActivity(); 96 } catch (SecurityException e) { 97 // Service does not declare the proper permission, ignore it. 98 LOG.w("Error getting info for " + serviceInfo + ": " + e); 99 continue; 100 } 101 if (TextUtils.isEmpty(settingsActivity)) { 102 continue; 103 } 104 return new Intent(Intent.ACTION_MAIN).setComponent( 105 new ComponentName(serviceInfo.packageName, settingsActivity)); 106 } 107 } 108 109 return null; 110 } 111 } 112