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 package com.android.settings.nfc;
17 
18 import android.content.Context;
19 import android.nfc.NfcAdapter;
20 import android.os.UserManager;
21 
22 import androidx.preference.PreferenceScreen;
23 import androidx.preference.TwoStatePreference;
24 
25 import com.android.settings.R;
26 import com.android.settings.core.TogglePreferenceController;
27 import com.android.settingslib.core.lifecycle.LifecycleObserver;
28 import com.android.settingslib.core.lifecycle.events.OnPause;
29 import com.android.settingslib.core.lifecycle.events.OnResume;
30 
31 public class SecureNfcPreferenceController extends TogglePreferenceController
32         implements LifecycleObserver, OnResume, OnPause {
33 
34     private final NfcAdapter mNfcAdapter;
35     private SecureNfcEnabler mSecureNfcEnabler;
36     private final UserManager mUserManager;
37 
SecureNfcPreferenceController(Context context, String key)38     public SecureNfcPreferenceController(Context context, String key) {
39         super(context, key);
40         mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
41         mUserManager = context.getSystemService(UserManager.class);
42     }
43 
44     @Override
displayPreference(PreferenceScreen screen)45     public void displayPreference(PreferenceScreen screen) {
46         super.displayPreference(screen);
47         if (!isAvailable()) {
48             mSecureNfcEnabler = null;
49             return;
50         }
51 
52         final TwoStatePreference switchPreference = screen.findPreference(getPreferenceKey());
53 
54         mSecureNfcEnabler = new SecureNfcEnabler(mContext, switchPreference);
55     }
56 
57     @Override
isChecked()58     public boolean isChecked() {
59         return mNfcAdapter.isSecureNfcEnabled();
60     }
61 
62     @Override
setChecked(boolean isChecked)63     public boolean setChecked(boolean isChecked) {
64         if (isToggleable()) {
65             return mNfcAdapter.enableSecureNfc(isChecked);
66         } else {
67             return false;
68         }
69     }
70 
71     @Override
72     @AvailabilityStatus
getAvailabilityStatus()73     public int getAvailabilityStatus() {
74         if (mNfcAdapter == null) {
75             return UNSUPPORTED_ON_DEVICE;
76         }
77         return mNfcAdapter.isSecureNfcSupported()
78                 ? AVAILABLE
79                 : UNSUPPORTED_ON_DEVICE;
80     }
81 
82     @Override
hasAsyncUpdate()83     public boolean hasAsyncUpdate() {
84         return true;
85     }
86 
87     @Override
isPublicSlice()88     public boolean isPublicSlice() {
89         return true;
90     }
91 
92     @Override
getSliceHighlightMenuRes()93     public int getSliceHighlightMenuRes() {
94         return R.string.menu_key_connected_devices;
95     }
96 
97     @Override
onResume()98     public void onResume() {
99         if (mSecureNfcEnabler != null) {
100             mSecureNfcEnabler.resume();
101         }
102     }
103 
104     @Override
onPause()105     public void onPause() {
106         if (mSecureNfcEnabler != null) {
107             mSecureNfcEnabler.pause();
108         }
109     }
110 
isToggleable()111     private boolean isToggleable() {
112         if (!mUserManager.isPrimaryUser()) {
113             return false;
114         }
115         return true;
116     }
117 
118 }
119