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