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.Manifest;
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.provider.Settings;
27 import android.service.autofill.AutofillService;
28 import android.text.Html;
29 
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 
33 import com.android.car.settings.R;
34 import com.android.car.settings.common.FragmentController;
35 import com.android.settingslib.applications.DefaultAppInfo;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 /** Business logic for displaying and choosing the default autofill service. */
41 public class DefaultAutofillPickerPreferenceController extends
42         DefaultAppsPickerBasePreferenceController {
43 
DefaultAutofillPickerPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44     public DefaultAutofillPickerPreferenceController(Context context, String preferenceKey,
45             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
46         super(context, preferenceKey, fragmentController, uxRestrictions);
47     }
48 
49     @NonNull
50     @Override
getCandidates()51     protected List<DefaultAppInfo> getCandidates() {
52         List<DefaultAppInfo> candidates = new ArrayList<>();
53         List<ResolveInfo> resolveInfos = getContext().getPackageManager().queryIntentServices(
54                 new Intent(AutofillService.SERVICE_INTERFACE), PackageManager.GET_META_DATA);
55         for (ResolveInfo info : resolveInfos) {
56             String permission = info.serviceInfo.permission;
57             if (Manifest.permission.BIND_AUTOFILL_SERVICE.equals(permission)) {
58                 candidates.add(new DefaultAppInfo(getContext(), getContext().getPackageManager(),
59                         getCurrentProcessUserId(),
60                         new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name)));
61             }
62         }
63         return candidates;
64     }
65 
66     @Override
getCurrentDefaultKey()67     protected String getCurrentDefaultKey() {
68         String setting = Settings.Secure.getString(getContext().getContentResolver(),
69                 Settings.Secure.AUTOFILL_SERVICE);
70         if (setting != null) {
71             ComponentName componentName = ComponentName.unflattenFromString(setting);
72             if (componentName != null) {
73                 return componentName.flattenToString();
74             }
75         }
76         return DefaultAppsPickerBasePreferenceController.NONE_PREFERENCE_KEY;
77     }
78 
79     @Override
setCurrentDefault(String key)80     protected void setCurrentDefault(String key) {
81         Settings.Secure.putString(getContext().getContentResolver(),
82                 Settings.Secure.AUTOFILL_SERVICE, key);
83     }
84 
85     @Override
86     @Nullable
getConfirmationMessage(DefaultAppInfo info)87     protected CharSequence getConfirmationMessage(DefaultAppInfo info) {
88         if (info == null) {
89             return null;
90         }
91 
92         CharSequence appName = info.loadLabel();
93         String message = getContext().getString(R.string.autofill_confirmation_message, appName);
94         return Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY);
95     }
96 }
97