• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.IntentFilter;
22 import android.net.Uri;
23 import android.nfc.NfcAdapter;
24 import android.provider.Settings;
25 import android.util.Log;
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.PreferenceScreen;
28 import androidx.preference.SwitchPreference;
29 
30 import com.android.settings.core.TogglePreferenceController;
31 import com.android.settings.slices.SliceBackgroundWorker;
32 import com.android.settingslib.core.lifecycle.LifecycleObserver;
33 import com.android.settingslib.core.lifecycle.events.OnPause;
34 import com.android.settingslib.core.lifecycle.events.OnResume;
35 
36 import java.io.IOException;
37 
38 public class NfcPreferenceController extends TogglePreferenceController
39         implements LifecycleObserver, OnResume, OnPause {
40 
41     public static final String KEY_TOGGLE_NFC = "toggle_nfc";
42     private final NfcAdapter mNfcAdapter;
43     private NfcEnabler mNfcEnabler;
44 
NfcPreferenceController(Context context, String key)45     public NfcPreferenceController(Context context, String key) {
46         super(context, key);
47         mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
48     }
49 
50     @Override
displayPreference(PreferenceScreen screen)51     public void displayPreference(PreferenceScreen screen) {
52         super.displayPreference(screen);
53         if (!isAvailable()) {
54             mNfcEnabler = null;
55             return;
56         }
57 
58         final SwitchPreference switchPreference = screen.findPreference(getPreferenceKey());
59 
60         mNfcEnabler = new NfcEnabler(mContext, switchPreference);
61 
62     }
63 
64     @Override
isChecked()65     public boolean isChecked() {
66         return mNfcAdapter.isEnabled();
67     }
68 
69     @Override
setChecked(boolean isChecked)70     public boolean setChecked(boolean isChecked) {
71         if (isChecked) {
72             mNfcAdapter.enable();
73         } else {
74             mNfcAdapter.disable();
75         }
76         return true;
77     }
78 
79     @Override
80     @AvailabilityStatus
getAvailabilityStatus()81     public int getAvailabilityStatus() {
82         return mNfcAdapter != null
83                 ? AVAILABLE
84                 : UNSUPPORTED_ON_DEVICE;
85     }
86 
87     @Override
hasAsyncUpdate()88     public boolean hasAsyncUpdate() {
89         return true;
90     }
91 
92     @Override
isSliceable()93     public boolean isSliceable() {
94         return true;
95     }
96 
97     @Override
getBackgroundWorkerClass()98     public Class<? extends SliceBackgroundWorker> getBackgroundWorkerClass() {
99         return NfcSliceWorker.class;
100     }
101 
102     @Override
onResume()103     public void onResume() {
104         if (mNfcEnabler != null) {
105             mNfcEnabler.resume();
106         }
107     }
108 
109     @Override
onPause()110     public void onPause() {
111         if (mNfcEnabler != null) {
112             mNfcEnabler.pause();
113         }
114     }
115 
shouldTurnOffNFCInAirplaneMode(Context context)116     public static boolean shouldTurnOffNFCInAirplaneMode(Context context) {
117         final String airplaneModeRadios = Settings.Global.getString(context.getContentResolver(),
118                 Settings.Global.AIRPLANE_MODE_RADIOS);
119         return airplaneModeRadios != null && airplaneModeRadios.contains(Settings.Global.RADIO_NFC);
120     }
121 
isToggleableInAirplaneMode(Context context)122     public static boolean isToggleableInAirplaneMode(Context context) {
123         final String toggleable = Settings.Global.getString(context.getContentResolver(),
124                 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
125         return toggleable != null && toggleable.contains(Settings.Global.RADIO_NFC);
126     }
127 
128     /**
129      * Listener for background changes to NFC.
130      *
131      * <p>
132      *     Listen to broadcasts from {@link NfcAdapter}. The worker will call notify changed on the
133      *     NFC Slice only when the following extras are present in the broadcast:
134      *     <ul>
135      *      <li>{@link NfcAdapter#STATE_ON}</li>
136      *      <li>{@link NfcAdapter#STATE_OFF}</li>
137      *     </ul>
138      */
139     public static class NfcSliceWorker extends SliceBackgroundWorker<Void> {
140 
141         private static final String TAG = "NfcSliceWorker";
142 
143         private static final IntentFilter NFC_FILTER =
144                 new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
145 
146         private NfcUpdateReceiver mUpdateObserver;
147 
NfcSliceWorker(Context context, Uri uri)148         public NfcSliceWorker(Context context, Uri uri) {
149             super(context, uri);
150             mUpdateObserver = new NfcUpdateReceiver(this);
151         }
152 
153         @Override
onSlicePinned()154         protected void onSlicePinned() {
155             getContext().registerReceiver(mUpdateObserver, NFC_FILTER);
156         }
157 
158         @Override
onSliceUnpinned()159         protected void onSliceUnpinned() {
160             getContext().unregisterReceiver(mUpdateObserver);
161         }
162 
163         @Override
close()164         public void close() throws IOException {
165             mUpdateObserver = null;
166         }
167 
updateSlice()168         public void updateSlice() {
169             notifySliceChange();
170         }
171 
172         public class NfcUpdateReceiver extends BroadcastReceiver {
173 
174             private final int NO_EXTRA = -1;
175 
176             private final NfcSliceWorker mSliceBackgroundWorker;
177 
NfcUpdateReceiver(NfcSliceWorker sliceWorker)178             public NfcUpdateReceiver(NfcSliceWorker sliceWorker) {
179                 mSliceBackgroundWorker = sliceWorker;
180             }
181 
182             @Override
onReceive(Context context, Intent intent)183             public void onReceive(Context context, Intent intent) {
184                 final int nfcStateExtra = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
185                         NO_EXTRA);
186 
187                 // Do nothing if state change is empty, or an intermediate step.
188                 if ( (nfcStateExtra == NO_EXTRA)
189                         || (nfcStateExtra == NfcAdapter.STATE_TURNING_ON)
190                         || (nfcStateExtra == NfcAdapter.STATE_TURNING_OFF)) {
191                     Log.d(TAG, "Transitional update, dropping broadcast");
192                     return;
193                 }
194 
195                 Log.d(TAG, "Nfc broadcast received, updating Slice.");
196                 mSliceBackgroundWorker.updateSlice();
197             }
198         }
199     }
200 }
201