DefaultAutofillPicker.java revision db2b55a2440991449d7c1f7d87938ee3b6fda6aa
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.app.Activity;
20import android.content.ComponentName;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.content.pm.ServiceInfo;
25import android.provider.Settings;
26import android.service.autofill.AutofillService;
27import android.service.autofill.AutofillServiceInfo;
28import android.text.Html;
29import android.text.TextUtils;
30
31import com.android.internal.logging.nano.MetricsProto;
32import com.android.settings.R;
33
34import java.util.ArrayList;
35import java.util.List;
36
37public class DefaultAutofillPicker extends DefaultAppPickerFragment {
38
39    static final String SETTING = Settings.Secure.AUTOFILL_SERVICE;
40    static final Intent AUTOFILL_PROBE = new Intent(AutofillService.SERVICE_INTERFACE);
41
42    static final String EXTRA_PACKAGE_NAME = "package_name";
43
44    @Override
45    public int getMetricsCategory() {
46        return MetricsProto.MetricsEvent.DEFAULT_AUTOFILL_PICKER;
47    }
48
49    @Override
50    protected boolean shouldShowItemNone() {
51        return true;
52    }
53
54    @Override
55    protected List<DefaultAppInfo> getCandidates() {
56        final List<DefaultAppInfo> candidates = new ArrayList<>();
57        final List<ResolveInfo> resolveInfos = mPm.getPackageManager()
58                .queryIntentServices(AUTOFILL_PROBE, PackageManager.GET_META_DATA);
59        for (ResolveInfo info : resolveInfos) {
60            candidates.add(new DefaultAppInfo(mPm, mUserId, new ComponentName(
61                    info.serviceInfo.packageName, info.serviceInfo.name)));
62        }
63        return candidates;
64    }
65
66    @Override
67    protected String getDefaultKey() {
68        return Settings.Secure.getString(getContext().getContentResolver(), SETTING);
69    }
70
71    @Override
72    protected CharSequence getConfirmationMessage(CandidateInfo appInfo) {
73        if (appInfo == null) {
74            return null;
75        }
76        final CharSequence appName = appInfo.loadLabel();
77        final String message = getContext().getString(
78                R.string.autofill_confirmation_message, appName);
79        return Html.fromHtml(message);
80    }
81
82    @Override
83    protected boolean setDefaultKey(String key) {
84        Settings.Secure.putString(getContext().getContentResolver(), SETTING, key);
85
86        // Check if activity was launched from Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE
87        // intent, and set proper result if so...
88        final Activity activity = getActivity();
89        if (activity != null) {
90            final String packageName = activity.getIntent().getStringExtra(EXTRA_PACKAGE_NAME);
91            if (packageName != null) {
92                final int result = key != null && key.startsWith(packageName) ? Activity.RESULT_OK
93                        : Activity.RESULT_CANCELED;
94                activity.setResult(result);
95                activity.finish();
96            }
97        }
98        return true;
99    }
100
101    /**
102     * Provides Intent to setting activity for the specified autofill service.
103     */
104    static final class AutofillSettingIntentProvider implements SettingIntentProvider {
105
106        private final String mSelectedKey;
107        private final PackageManager mPackageManager;
108
109        public AutofillSettingIntentProvider(PackageManager packageManager, String key) {
110            mSelectedKey = key;
111            mPackageManager = packageManager;
112        }
113
114        @Override
115        public Intent getIntent() {
116            final List<ResolveInfo> resolveInfos = mPackageManager.queryIntentServices(
117                    AUTOFILL_PROBE, PackageManager.GET_META_DATA);
118
119            for (ResolveInfo resolveInfo : resolveInfos) {
120                final ServiceInfo serviceInfo = resolveInfo.serviceInfo;
121                final String flattenKey = new ComponentName(
122                        serviceInfo.packageName, serviceInfo.name).flattenToString();
123                if (TextUtils.equals(mSelectedKey, flattenKey)) {
124                    final String settingsActivity = new AutofillServiceInfo(
125                            mPackageManager, serviceInfo)
126                            .getSettingsActivity();
127                    if (TextUtils.isEmpty(settingsActivity)) {
128                        return null;
129                    }
130                    return new Intent(Intent.ACTION_MAIN).setComponent(
131                            new ComponentName(serviceInfo.packageName, settingsActivity));
132                }
133            }
134
135            return null;
136        }
137    }
138}
139