DefaultAutofillPicker.java revision 4fa4a2e3f5383dfa0f82470188ba2ea12601f689
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
17package com.android.settings.applications.defaultapps;
18
19import android.content.ComponentName;
20import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
23import android.content.pm.ServiceInfo;
24import android.provider.Settings;
25import android.service.autofill.AutoFillService;
26import android.service.autofill.AutofillService;
27import android.service.autofill.AutofillServiceInfo;
28import android.text.TextUtils;
29
30import com.android.internal.logging.nano.MetricsProto;
31import com.android.settings.R;
32
33import java.util.ArrayList;
34import java.util.List;
35
36public class DefaultAutofillPicker extends DefaultAppPickerFragment {
37
38    static final String SETTING = Settings.Secure.AUTOFILL_SERVICE;
39    // TODO(b/35956626): remove once clients migrated
40    static final Intent OLD_AUTO_FILL_PROBE = new Intent(AutoFillService.OLD_SERVICE_INTERFACE);
41    static final Intent AUTOFILL_PROBE = new Intent(AutofillService.SERVICE_INTERFACE);
42
43    @Override
44    public int getMetricsCategory() {
45        return MetricsProto.MetricsEvent.DEFAULT_AUTOFILL_PICKER;
46    }
47
48    @Override
49    protected boolean shouldShowItemNone() {
50        return true;
51    }
52
53    @Override
54    protected List<DefaultAppInfo> getCandidates() {
55        final List<DefaultAppInfo> candidates = new ArrayList<>();
56        final List<ResolveInfo> resolveInfos = mPm.getPackageManager()
57                .queryIntentServices(AUTOFILL_PROBE, PackageManager.GET_META_DATA);
58        for (ResolveInfo info : resolveInfos) {
59            candidates.add(new DefaultAppInfo(mUserId, new ComponentName(
60                    info.serviceInfo.packageName, info.serviceInfo.name)));
61        }
62        final List<ResolveInfo> oldResolveInfos = mPm.getPackageManager()
63                .queryIntentServices(OLD_AUTO_FILL_PROBE, PackageManager.GET_META_DATA);
64        for (ResolveInfo info : oldResolveInfos) {
65            candidates.add(new DefaultAppInfo(mUserId, new ComponentName(
66                    info.serviceInfo.packageName, info.serviceInfo.name)));
67        }
68        return candidates;
69    }
70
71    @Override
72    protected String getDefaultAppKey() {
73        return Settings.Secure.getString(getContext().getContentResolver(), SETTING);
74    }
75
76    @Override
77    protected String getConfirmationMessage(DefaultAppInfo appInfo) {
78        if (appInfo == null) {
79            return null;
80        }
81        final CharSequence appName = appInfo.loadLabel(mPm.getPackageManager());
82        return getContext().getString(R.string.autofill_confirmation_message, appName);
83    }
84
85    @Override
86    protected boolean setDefaultAppKey(String key) {
87        Settings.Secure.putString(getContext().getContentResolver(), SETTING, key);
88        return true;
89    }
90
91    /**
92     * Provides Intent to setting activity for the specified auto-fill service.
93     */
94    static final class AutofillSettingIntentProvider implements SettingIntentProvider {
95
96        private final String mSelectedKey;
97        private final PackageManager mPackageManager;
98
99        public AutofillSettingIntentProvider(PackageManager packageManager, String key) {
100            mSelectedKey = key;
101            mPackageManager = packageManager;
102        }
103
104        @Override
105        public Intent getIntent() {
106            final List<ResolveInfo> resolveInfos = mPackageManager.queryIntentServices(
107                    AUTOFILL_PROBE, PackageManager.GET_META_DATA);
108
109            for (ResolveInfo resolveInfo : resolveInfos) {
110                final ServiceInfo serviceInfo = resolveInfo.serviceInfo;
111                final String flattenKey = new ComponentName(
112                        serviceInfo.packageName, serviceInfo.name).flattenToString();
113                if (TextUtils.equals(mSelectedKey, flattenKey)) {
114                    final String settingsActivity = new AutofillServiceInfo(
115                            mPackageManager, serviceInfo)
116                            .getSettingsActivity();
117                    if (TextUtils.isEmpty(settingsActivity)) {
118                        return null;
119                    }
120                    return new Intent(Intent.ACTION_MAIN).setComponent(
121                            new ComponentName(serviceInfo.packageName, settingsActivity));
122                }
123            }
124
125            final List<ResolveInfo> oldResolveInfos = mPackageManager.queryIntentServices(
126                    OLD_AUTO_FILL_PROBE, PackageManager.GET_META_DATA);
127
128            for (ResolveInfo resolveInfo : oldResolveInfos) {
129                final ServiceInfo serviceInfo = resolveInfo.serviceInfo;
130                final String flattenKey = new ComponentName(
131                        serviceInfo.packageName, serviceInfo.name).flattenToString();
132                if (TextUtils.equals(mSelectedKey, flattenKey)) {
133                    final String settingsActivity = new AutofillServiceInfo(
134                            mPackageManager, serviceInfo)
135                            .getSettingsActivity();
136                    if (TextUtils.isEmpty(settingsActivity)) {
137                        return null;
138                    }
139                    return new Intent(Intent.ACTION_MAIN).setComponent(
140                            new ComponentName(serviceInfo.packageName, settingsActivity));
141                }
142            }
143            return null;
144        }
145    }
146}
147