1 /*
2  * Copyright (C) 2020 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.settings.applications.specialaccess;
18 
19 import android.content.Context;
20 import android.nfc.NfcAdapter;
21 import android.nfc.cardemulation.CardEmulation;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.settings.R;
26 import com.android.settings.nfc.BaseNfcEnabler;
27 
28 /**
29  * PaymentSettingsEnabler is a helper to manage the payment feature enable / disable state.
30  * It enables / disables payment features based on NFC state, and ensures the summary of the
31  * preference is updated.
32  */
33 public class PaymentSettingsEnabler extends BaseNfcEnabler {
34     private final CardEmulation mCardEmuManager;
35     private final Preference mPreference;
36     boolean mIsPaymentAvailable;
37 
PaymentSettingsEnabler(Context context, Preference preference)38     public PaymentSettingsEnabler(Context context, Preference preference) {
39         super(context);
40         mCardEmuManager = CardEmulation.getInstance(super.mNfcAdapter);
41         mIsPaymentAvailable = false;
42         mPreference = preference;
43     }
44 
45     @Override
handleNfcStateChanged(int newState)46     protected void handleNfcStateChanged(int newState) {
47         switch (newState) {
48             case NfcAdapter.STATE_OFF:
49                 mPreference.setSummary(
50                         R.string.nfc_and_payment_settings_payment_off_nfc_off_summary);
51                 mPreference.setEnabled(false);
52                 break;
53             case NfcAdapter.STATE_ON:
54                 if (mIsPaymentAvailable) {
55                     mPreference.setSummary(null);
56                     mPreference.setEnabled(true);
57                 } else {
58                     mPreference.setSummary(
59                             R.string.nfc_and_payment_settings_no_payment_installed_summary);
60 
61                     mPreference.setEnabled(false);
62                 }
63                 break;
64         }
65     }
66 
67     @Override
resume()68     public void resume() {
69         if (!isNfcAvailable()) {
70             return;
71         }
72         if (mCardEmuManager.getServices(CardEmulation.CATEGORY_PAYMENT).isEmpty()) {
73             mIsPaymentAvailable = false;
74         } else {
75             mIsPaymentAvailable = true;
76         }
77         super.resume();
78     }
79 }
80