DefaultAutofillPreferenceController.java revision 19b9f120dbe581a0aa8c1c0afb7b73a0ebed0343
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.Context;
21import android.content.Intent;
22import android.provider.Settings;
23import android.text.TextUtils;
24import android.view.autofill.AutofillManager;
25
26import com.android.settingslib.applications.DefaultAppInfo;
27
28public class DefaultAutofillPreferenceController extends DefaultAppPreferenceController {
29
30    private final AutofillManager mAutofillManager;
31
32    public DefaultAutofillPreferenceController(Context context) {
33        super(context);
34
35        mAutofillManager = mContext.getSystemService(AutofillManager.class);
36    }
37
38    @Override
39    public boolean isAvailable() {
40        return mAutofillManager != null
41                && mAutofillManager.hasAutofillFeature()
42                && mAutofillManager.isAutofillSupported();
43    }
44
45    @Override
46    public String getPreferenceKey() {
47        return "default_autofill";
48    }
49
50    @Override
51    protected Intent getSettingIntent(DefaultAppInfo info) {
52        if (info == null) {
53            return null;
54        }
55        final DefaultAutofillPicker.AutofillSettingIntentProvider intentProvider =
56                new DefaultAutofillPicker.AutofillSettingIntentProvider(
57                        mContext, info.getKey());
58        return intentProvider.getIntent();
59    }
60
61    @Override
62    protected DefaultAppInfo getDefaultAppInfo() {
63        final String flattenComponent = Settings.Secure.getString(mContext.getContentResolver(),
64                DefaultAutofillPicker.SETTING);
65        if (!TextUtils.isEmpty(flattenComponent)) {
66            DefaultAppInfo appInfo = new DefaultAppInfo(mContext, mPackageManager,
67                    mUserId, ComponentName.unflattenFromString(flattenComponent));
68            return appInfo;
69        }
70        return null;
71    }
72}
73