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