1 /* 2 * Copyright (C) 2010 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.provider.Settings; 22 23 import androidx.annotation.VisibleForTesting; 24 import androidx.preference.SwitchPreference; 25 26 /** 27 * NfcEnabler is a helper to manage the Nfc on/off checkbox preference. It turns on/off Nfc 28 * and ensures the summary of the preference reflects the current state. 29 */ 30 public class NfcEnabler extends BaseNfcEnabler { 31 private final SwitchPreference mPreference; 32 NfcEnabler(Context context, SwitchPreference preference)33 public NfcEnabler(Context context, SwitchPreference preference) { 34 super(context); 35 mPreference = preference; 36 } 37 38 @Override handleNfcStateChanged(int newState)39 protected void handleNfcStateChanged(int newState) { 40 switch (newState) { 41 case NfcAdapter.STATE_OFF: 42 mPreference.setChecked(false); 43 mPreference.setEnabled(isToggleable()); 44 break; 45 case NfcAdapter.STATE_ON: 46 mPreference.setChecked(true); 47 mPreference.setEnabled(true); 48 break; 49 case NfcAdapter.STATE_TURNING_ON: 50 mPreference.setChecked(true); 51 mPreference.setEnabled(false); 52 break; 53 case NfcAdapter.STATE_TURNING_OFF: 54 mPreference.setChecked(false); 55 mPreference.setEnabled(false); 56 break; 57 } 58 } 59 60 @VisibleForTesting isToggleable()61 boolean isToggleable() { 62 if (NfcPreferenceController.isToggleableInAirplaneMode(mContext) 63 || !NfcPreferenceController.shouldTurnOffNFCInAirplaneMode(mContext)) { 64 return true; 65 } 66 final int airplaneMode = Settings.Global.getInt( 67 mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0); 68 return airplaneMode != 1; 69 } 70 } 71