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