1 /*
2  * Copyright (C) 2019 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.nfc;
18 
19 import android.content.Context;
20 import android.nfc.NfcAdapter;
21 import android.os.UserManager;
22 
23 import androidx.preference.TwoStatePreference;
24 
25 import com.android.settings.R;
26 
27 /**
28  * SecureNfcEnabler is a helper to manage the Secure Nfc on/off checkbox preference
29  * It turns on/off Secure NFC and ensures the summary of the preference reflects
30  * the current state.
31  */
32 public class SecureNfcEnabler extends BaseNfcEnabler {
33     private final TwoStatePreference mPreference;
34     private final UserManager mUserManager;
35 
SecureNfcEnabler(Context context, TwoStatePreference preference)36     public SecureNfcEnabler(Context context, TwoStatePreference preference) {
37         super(context);
38         mPreference = preference;
39         mUserManager = context.getSystemService(UserManager.class);
40     }
41 
42     @Override
handleNfcStateChanged(int newState)43     protected void handleNfcStateChanged(int newState) {
44         switch (newState) {
45             case NfcAdapter.STATE_OFF:
46                 mPreference.setSummary(R.string.nfc_disabled_summary);
47                 mPreference.setEnabled(false);
48                 break;
49             case NfcAdapter.STATE_ON:
50                 mPreference.setSummary(R.string.nfc_secure_toggle_summary);
51                 mPreference.setChecked(mPreference.isChecked());
52                 mPreference.setEnabled(isToggleable());
53                 break;
54             case NfcAdapter.STATE_TURNING_ON:
55                 mPreference.setEnabled(false);
56                 break;
57             case NfcAdapter.STATE_TURNING_OFF:
58                 mPreference.setEnabled(false);
59                 break;
60         }
61     }
62 
isToggleable()63     private boolean isToggleable() {
64         if (!mUserManager.isPrimaryUser()) {
65             return false;
66         }
67         return true;
68     }
69 }
70