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