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.Context;
20import android.content.pm.PackageManager;
21import android.nfc.NfcAdapter;
22import android.os.UserManager;
23import android.support.v7.preference.Preference;
24
25import com.android.settings.R;
26import com.android.settings.core.PreferenceControllerMixin;
27import com.android.settings.nfc.PaymentBackend;
28import com.android.settingslib.core.AbstractPreferenceController;
29
30public class DefaultPaymentSettingsPreferenceController extends AbstractPreferenceController
31        implements PreferenceControllerMixin {
32
33    private final NfcAdapter mNfcAdapter;
34    private final PackageManager mPackageManager;
35    private final UserManager mUserManager;
36    private PaymentBackend mPaymentBackend;
37
38    public DefaultPaymentSettingsPreferenceController(Context context) {
39        super(context);
40        mPackageManager = context.getPackageManager();
41        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
42        mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext);
43    }
44
45    @Override
46    public boolean isAvailable() {
47        return mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
48                && mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
49                && mUserManager.isAdminUser()
50                && mNfcAdapter != null
51                && mNfcAdapter.isEnabled();
52    }
53
54    @Override
55    public void updateState(Preference preference) {
56        if (mPaymentBackend == null) {
57            if (mNfcAdapter != null) {
58                mPaymentBackend = new PaymentBackend(mContext);
59            } else {
60                mPaymentBackend = null;
61            }
62        }
63        if (mPaymentBackend == null) {
64            return;
65        }
66        mPaymentBackend.refresh();
67        final PaymentBackend.PaymentAppInfo app = mPaymentBackend.getDefaultApp();
68        if (app != null) {
69            preference.setSummary(app.label);
70        } else {
71            preference.setSummary(R.string.app_list_preference_none);
72        }
73    }
74
75    @Override
76    public String getPreferenceKey() {
77        return "default_payment_app";
78    }
79}
80