• 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 android.nfc;
18 
19 import android.annotation.CallbackExecutor;
20 import android.annotation.NonNull;
21 import android.annotation.RequiresPermission;
22 import android.annotation.SdkConstant;
23 import android.annotation.SdkConstant.SdkConstantType;
24 import android.annotation.SystemApi;
25 import android.app.Activity;
26 import android.app.ActivityThread;
27 import android.app.OnActivityPausedListener;
28 import android.app.PendingIntent;
29 import android.compat.annotation.UnsupportedAppUsage;
30 import android.content.Context;
31 import android.content.IntentFilter;
32 import android.content.pm.IPackageManager;
33 import android.content.pm.PackageManager;
34 import android.net.Uri;
35 import android.nfc.tech.MifareClassic;
36 import android.nfc.tech.Ndef;
37 import android.nfc.tech.NfcA;
38 import android.nfc.tech.NfcF;
39 import android.os.Build;
40 import android.os.Bundle;
41 import android.os.Handler;
42 import android.os.IBinder;
43 import android.os.RemoteException;
44 import android.os.ServiceManager;
45 import android.util.Log;
46 
47 import java.io.IOException;
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.concurrent.Executor;
52 
53 /**
54  * Represents the local NFC adapter.
55  * <p>
56  * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC
57  * adapter for this Android device.
58  *
59  * <div class="special reference">
60  * <h3>Developer Guides</h3>
61  * <p>For more information about using NFC, read the
62  * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p>
63  * <p>To perform basic file sharing between devices, read
64  * <a href="{@docRoot}training/beam-files/index.html">Sharing Files with NFC</a>.
65  * </div>
66  */
67 public final class NfcAdapter {
68     static final String TAG = "NFC";
69 
70     private final NfcControllerAlwaysOnListener mControllerAlwaysOnListener;
71 
72     /**
73      * Intent to start an activity when a tag with NDEF payload is discovered.
74      *
75      * <p>The system inspects the first {@link NdefRecord} in the first {@link NdefMessage} and
76      * looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the
77      * intent will contain the URI in its data field. If a MIME record is found the intent will
78      * contain the MIME type in its type field. This allows activities to register
79      * {@link IntentFilter}s targeting specific content on tags. Activities should register the
80      * most specific intent filters possible to avoid the activity chooser dialog, which can
81      * disrupt the interaction with the tag as the user interacts with the screen.
82      *
83      * <p>If the tag has an NDEF payload this intent is started before
84      * {@link #ACTION_TECH_DISCOVERED}. If any activities respond to this intent neither
85      * {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started.
86      *
87      * <p>The MIME type or data URI of this intent are normalized before dispatch -
88      * so that MIME, URI scheme and URI host are always lower-case.
89      */
90     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
91     public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED";
92 
93     /**
94      * Intent to start an activity when a tag is discovered and activities are registered for the
95      * specific technologies on the tag.
96      *
97      * <p>To receive this intent an activity must include an intent filter
98      * for this action and specify the desired tech types in a
99      * manifest <code>meta-data</code> entry. Here is an example manfiest entry:
100      * <pre>
101      * &lt;activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"&gt;
102      *     &lt;!-- Add a technology filter --&gt;
103      *     &lt;intent-filter&gt;
104      *         &lt;action android:name="android.nfc.action.TECH_DISCOVERED" /&gt;
105      *     &lt;/intent-filter&gt;
106      *
107      *     &lt;meta-data android:name="android.nfc.action.TECH_DISCOVERED"
108      *         android:resource="@xml/filter_nfc"
109      *     /&gt;
110      * &lt;/activity&gt;</pre>
111      *
112      * <p>The meta-data XML file should contain one or more <code>tech-list</code> entries
113      * each consisting or one or more <code>tech</code> entries. The <code>tech</code> entries refer
114      * to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA".
115      *
116      * <p>A tag matches if any of the
117      * <code>tech-list</code> sets is a subset of {@link Tag#getTechList() Tag.getTechList()}. Each
118      * of the <code>tech-list</code>s is considered independently and the
119      * activity is considered a match is any single <code>tech-list</code> matches the tag that was
120      * discovered. This provides AND and OR semantics for filtering desired techs. Here is an
121      * example that will match any tag using {@link NfcF} or any tag using {@link NfcA},
122      * {@link MifareClassic}, and {@link Ndef}:
123      *
124      * <pre>
125      * &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
126      *     &lt;!-- capture anything using NfcF --&gt;
127      *     &lt;tech-list&gt;
128      *         &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
129      *     &lt;/tech-list&gt;
130      *
131      *     &lt;!-- OR --&gt;
132      *
133      *     &lt;!-- capture all MIFARE Classics with NDEF payloads --&gt;
134      *     &lt;tech-list&gt;
135      *         &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
136      *         &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
137      *         &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
138      *     &lt;/tech-list&gt;
139      * &lt;/resources&gt;</pre>
140      *
141      * <p>This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before
142      * {@link #ACTION_TAG_DISCOVERED}. If any activities respond to {@link #ACTION_NDEF_DISCOVERED}
143      * this intent will not be started. If any activities respond to this intent
144      * {@link #ACTION_TAG_DISCOVERED} will not be started.
145      */
146     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
147     public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED";
148 
149     /**
150      * Intent to start an activity when a tag is discovered.
151      *
152      * <p>This intent will not be started when a tag is discovered if any activities respond to
153      * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag.
154      */
155     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
156     public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED";
157 
158     /**
159      * Broadcast Action: Intent to notify an application that a transaction event has occurred
160      * on the Secure Element.
161      *
162      * <p>This intent will only be sent if the application has requested permission for
163      * {@link android.Manifest.permission#NFC_TRANSACTION_EVENT} and if the application has the
164      * necessary access to Secure Element which witnessed the particular event.
165      */
166     @RequiresPermission(android.Manifest.permission.NFC_TRANSACTION_EVENT)
167     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
168     public static final String ACTION_TRANSACTION_DETECTED =
169             "android.nfc.action.TRANSACTION_DETECTED";
170 
171     /**
172      * Broadcast Action: Intent to notify if the preferred payment service changed.
173      *
174      * <p>This intent will only be sent to the application has requested permission for
175      * {@link android.Manifest.permission#NFC_PREFERRED_PAYMENT_INFO} and if the application
176      * has the necessary access to Secure Element which witnessed the particular event.
177      */
178     @RequiresPermission(android.Manifest.permission.NFC_PREFERRED_PAYMENT_INFO)
179     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
180     public static final String ACTION_PREFERRED_PAYMENT_CHANGED =
181             "android.nfc.action.PREFERRED_PAYMENT_CHANGED";
182 
183     /**
184      * Broadcast to only the activity that handles ACTION_TAG_DISCOVERED
185      * @hide
186      */
187     public static final String ACTION_TAG_LEFT_FIELD = "android.nfc.action.TAG_LOST";
188 
189     /**
190      * Mandatory extra containing the {@link Tag} that was discovered for the
191      * {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
192      * {@link #ACTION_TAG_DISCOVERED} intents.
193      */
194     public static final String EXTRA_TAG = "android.nfc.extra.TAG";
195 
196     /**
197      * Extra containing an array of {@link NdefMessage} present on the discovered tag.<p>
198      * This extra is mandatory for {@link #ACTION_NDEF_DISCOVERED} intents,
199      * and optional for {@link #ACTION_TECH_DISCOVERED}, and
200      * {@link #ACTION_TAG_DISCOVERED} intents.<p>
201      * When this extra is present there will always be at least one
202      * {@link NdefMessage} element. Most NDEF tags have only one NDEF message,
203      * but we use an array for future compatibility.
204      */
205     public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES";
206 
207     /**
208      * Optional extra containing a byte array containing the ID of the discovered tag for
209      * the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
210      * {@link #ACTION_TAG_DISCOVERED} intents.
211      */
212     public static final String EXTRA_ID = "android.nfc.extra.ID";
213 
214     /**
215      * Broadcast Action: The state of the local NFC adapter has been
216      * changed.
217      * <p>For example, NFC has been turned on or off.
218      * <p>Always contains the extra field {@link #EXTRA_ADAPTER_STATE}
219      */
220     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
221     public static final String ACTION_ADAPTER_STATE_CHANGED =
222             "android.nfc.action.ADAPTER_STATE_CHANGED";
223 
224     /**
225      * Used as an int extra field in {@link #ACTION_ADAPTER_STATE_CHANGED}
226      * intents to request the current power state. Possible values are:
227      * {@link #STATE_OFF},
228      * {@link #STATE_TURNING_ON},
229      * {@link #STATE_ON},
230      * {@link #STATE_TURNING_OFF},
231      */
232     public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE";
233 
234     /**
235      * Mandatory byte[] extra field in {@link #ACTION_TRANSACTION_DETECTED}
236      */
237     public static final String EXTRA_AID = "android.nfc.extra.AID";
238 
239     /**
240      * Optional byte[] extra field in {@link #ACTION_TRANSACTION_DETECTED}
241      */
242     public static final String EXTRA_DATA = "android.nfc.extra.DATA";
243 
244     /**
245      * Mandatory String extra field in {@link #ACTION_TRANSACTION_DETECTED}
246      * Indicates the Secure Element on which the transaction occurred.
247      * eSE1...eSEn for Embedded Secure Elements, SIM1...SIMn for UICC, etc.
248      */
249     public static final String EXTRA_SECURE_ELEMENT_NAME = "android.nfc.extra.SECURE_ELEMENT_NAME";
250 
251     /**
252      * Mandatory String extra field in {@link #ACTION_PREFERRED_PAYMENT_CHANGED}
253      * Indicates the condition when trigger this event. Possible values are:
254      * {@link #PREFERRED_PAYMENT_LOADED},
255      * {@link #PREFERRED_PAYMENT_CHANGED},
256      * {@link #PREFERRED_PAYMENT_UPDATED},
257      */
258     public static final String EXTRA_PREFERRED_PAYMENT_CHANGED_REASON =
259             "android.nfc.extra.PREFERRED_PAYMENT_CHANGED_REASON";
260     /**
261      * Nfc is enabled and the preferred payment aids are registered.
262      */
263     public static final int PREFERRED_PAYMENT_LOADED = 1;
264     /**
265      * User selected another payment application as the preferred payment.
266      */
267     public static final int PREFERRED_PAYMENT_CHANGED = 2;
268     /**
269      * Current preferred payment has issued an update (registered/unregistered new aids or has been
270      * updated itself).
271      */
272     public static final int PREFERRED_PAYMENT_UPDATED = 3;
273 
274     public static final int STATE_OFF = 1;
275     public static final int STATE_TURNING_ON = 2;
276     public static final int STATE_ON = 3;
277     public static final int STATE_TURNING_OFF = 4;
278 
279     /**
280      * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
281      * <p>
282      * Setting this flag enables polling for Nfc-A technology.
283      */
284     public static final int FLAG_READER_NFC_A = 0x1;
285 
286     /**
287      * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
288      * <p>
289      * Setting this flag enables polling for Nfc-B technology.
290      */
291     public static final int FLAG_READER_NFC_B = 0x2;
292 
293     /**
294      * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
295      * <p>
296      * Setting this flag enables polling for Nfc-F technology.
297      */
298     public static final int FLAG_READER_NFC_F = 0x4;
299 
300     /**
301      * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
302      * <p>
303      * Setting this flag enables polling for Nfc-V (ISO15693) technology.
304      */
305     public static final int FLAG_READER_NFC_V = 0x8;
306 
307     /**
308      * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
309      * <p>
310      * Setting this flag enables polling for NfcBarcode technology.
311      */
312     public static final int FLAG_READER_NFC_BARCODE = 0x10;
313 
314     /**
315      * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
316      * <p>
317      * Setting this flag allows the caller to prevent the
318      * platform from performing an NDEF check on the tags it
319      * finds.
320      */
321     public static final int FLAG_READER_SKIP_NDEF_CHECK = 0x80;
322 
323     /**
324      * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
325      * <p>
326      * Setting this flag allows the caller to prevent the
327      * platform from playing sounds when it discovers a tag.
328      */
329     public static final int FLAG_READER_NO_PLATFORM_SOUNDS = 0x100;
330 
331     /**
332      * Int Extra for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
333      * <p>
334      * Setting this integer extra allows the calling application to specify
335      * the delay that the platform will use for performing presence checks
336      * on any discovered tag.
337      */
338     public static final String EXTRA_READER_PRESENCE_CHECK_DELAY = "presence";
339 
340     /** @hide */
341     @SystemApi
342     public static final int FLAG_NDEF_PUSH_NO_CONFIRM = 0x1;
343 
344     /** @hide */
345     public static final String ACTION_HANDOVER_TRANSFER_STARTED =
346             "android.nfc.action.HANDOVER_TRANSFER_STARTED";
347 
348     /** @hide */
349     public static final String ACTION_HANDOVER_TRANSFER_DONE =
350             "android.nfc.action.HANDOVER_TRANSFER_DONE";
351 
352     /** @hide */
353     public static final String EXTRA_HANDOVER_TRANSFER_STATUS =
354             "android.nfc.extra.HANDOVER_TRANSFER_STATUS";
355 
356     /** @hide */
357     public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
358     /** @hide */
359     public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;
360 
361     /** @hide */
362     public static final String EXTRA_HANDOVER_TRANSFER_URI =
363             "android.nfc.extra.HANDOVER_TRANSFER_URI";
364 
365     /**
366      * Broadcast Action: Notify possible NFC transaction blocked because device is locked.
367      * <p>An external NFC field detected when device locked and SecureNfc enabled.
368      * @hide
369      */
370     public static final String ACTION_REQUIRE_UNLOCK_FOR_NFC =
371             "android.nfc.action.REQUIRE_UNLOCK_FOR_NFC";
372 
373     // Guarded by NfcAdapter.class
374     static boolean sIsInitialized = false;
375     static boolean sHasNfcFeature;
376     static boolean sHasBeamFeature;
377 
378     // Final after first constructor, except for
379     // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
380     // recovery
381     @UnsupportedAppUsage
382     static INfcAdapter sService;
383     static INfcTag sTagService;
384     static INfcCardEmulation sCardEmulationService;
385     static INfcFCardEmulation sNfcFCardEmulationService;
386 
387     /**
388      * The NfcAdapter object for each application context.
389      * There is a 1-1 relationship between application context and
390      * NfcAdapter object.
391      */
392     static HashMap<Context, NfcAdapter> sNfcAdapters = new HashMap(); //guard by NfcAdapter.class
393 
394     /**
395      * NfcAdapter used with a null context. This ctor was deprecated but we have
396      * to support it for backwards compatibility. New methods that require context
397      * might throw when called on the null-context NfcAdapter.
398      */
399     static NfcAdapter sNullContextNfcAdapter;  // protected by NfcAdapter.class
400 
401     final NfcActivityManager mNfcActivityManager;
402     final Context mContext;
403     final HashMap<NfcUnlockHandler, INfcUnlockHandler> mNfcUnlockHandlers;
404     final Object mLock;
405 
406     ITagRemovedCallback mTagRemovedListener; // protected by mLock
407 
408     /**
409      * A callback to be invoked when the system finds a tag while the foreground activity is
410      * operating in reader mode.
411      * <p>Register your {@code ReaderCallback} implementation with {@link
412      * NfcAdapter#enableReaderMode} and disable it with {@link
413      * NfcAdapter#disableReaderMode}.
414      * @see NfcAdapter#enableReaderMode
415      */
416     public interface ReaderCallback {
onTagDiscovered(Tag tag)417         public void onTagDiscovered(Tag tag);
418     }
419 
420     /**
421      * A listener to be invoked when NFC controller always on state changes.
422      * <p>Register your {@code ControllerAlwaysOnListener} implementation with {@link
423      * NfcAdapter#registerControllerAlwaysOnListener} and disable it with {@link
424      * NfcAdapter#unregisterControllerAlwaysOnListener}.
425      * @see #registerControllerAlwaysOnListener
426      * @hide
427      */
428     @SystemApi
429     public interface ControllerAlwaysOnListener {
430         /**
431          * Called on NFC controller always on state changes
432          */
onControllerAlwaysOnChanged(boolean isEnabled)433         void onControllerAlwaysOnChanged(boolean isEnabled);
434     }
435 
436     /**
437      * A callback to be invoked when the system successfully delivers your {@link NdefMessage}
438      * to another device.
439      * @see #setOnNdefPushCompleteCallback
440      * @deprecated this feature is deprecated. File sharing can work using other technology like
441      * Bluetooth.
442      */
443     @java.lang.Deprecated
444     public interface OnNdefPushCompleteCallback {
445         /**
446          * Called on successful NDEF push.
447          *
448          * <p>This callback is usually made on a binder thread (not the UI thread).
449          *
450          * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
451          * @see #setNdefPushMessageCallback
452          */
onNdefPushComplete(NfcEvent event)453         public void onNdefPushComplete(NfcEvent event);
454     }
455 
456     /**
457      * A callback to be invoked when another NFC device capable of NDEF push (Android Beam)
458      * is within range.
459      * <p>Implement this interface and pass it to {@link
460      * NfcAdapter#setNdefPushMessageCallback setNdefPushMessageCallback()} in order to create an
461      * {@link NdefMessage} at the moment that another device is within range for NFC. Using this
462      * callback allows you to create a message with data that might vary based on the
463      * content currently visible to the user. Alternatively, you can call {@link
464      * #setNdefPushMessage setNdefPushMessage()} if the {@link NdefMessage} always contains the
465      * same data.
466      * @deprecated this feature is deprecated. File sharing can work using other technology like
467      * Bluetooth.
468      */
469     @java.lang.Deprecated
470     public interface CreateNdefMessageCallback {
471         /**
472          * Called to provide a {@link NdefMessage} to push.
473          *
474          * <p>This callback is usually made on a binder thread (not the UI thread).
475          *
476          * <p>Called when this device is in range of another device
477          * that might support NDEF push. It allows the application to
478          * create the NDEF message only when it is required.
479          *
480          * <p>NDEF push cannot occur until this method returns, so do not
481          * block for too long.
482          *
483          * <p>The Android operating system will usually show a system UI
484          * on top of your activity during this time, so do not try to request
485          * input from the user to complete the callback, or provide custom NDEF
486          * push UI. The user probably will not see it.
487          *
488          * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
489          * @return NDEF message to push, or null to not provide a message
490          */
createNdefMessage(NfcEvent event)491         public NdefMessage createNdefMessage(NfcEvent event);
492     }
493 
494 
495      /**
496      * @deprecated this feature is deprecated. File sharing can work using other technology like
497      * Bluetooth.
498      */
499     @java.lang.Deprecated
500     public interface CreateBeamUrisCallback {
createBeamUris(NfcEvent event)501         public Uri[] createBeamUris(NfcEvent event);
502     }
503 
504     /**
505      * A callback that is invoked when a tag is removed from the field.
506      * @see NfcAdapter#ignore
507      */
508     public interface OnTagRemovedListener {
onTagRemoved()509         void onTagRemoved();
510     }
511 
512     /**
513      * A callback to be invoked when an application has registered as a
514      * handler to unlock the device given an NFC tag at the lockscreen.
515      * @hide
516      */
517     @SystemApi
518     public interface NfcUnlockHandler {
519         /**
520          * Called at the lock screen to attempt to unlock the device with the given tag.
521          * @param tag the detected tag, to be used to unlock the device
522          * @return true if the device was successfully unlocked
523          */
onUnlockAttempted(Tag tag)524         public boolean onUnlockAttempted(Tag tag);
525     }
526 
527     /**
528      * Helper to check if this device has FEATURE_NFC_BEAM, but without using
529      * a context.
530      * Equivalent to
531      * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC_BEAM)
532      */
hasBeamFeature()533     private static boolean hasBeamFeature() {
534         IPackageManager pm = ActivityThread.getPackageManager();
535         if (pm == null) {
536             Log.e(TAG, "Cannot get package manager, assuming no Android Beam feature");
537             return false;
538         }
539         try {
540             return pm.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM, 0);
541         } catch (RemoteException e) {
542             Log.e(TAG, "Package manager query failed, assuming no Android Beam feature", e);
543             return false;
544         }
545     }
546 
547     /**
548      * Helper to check if this device has FEATURE_NFC, but without using
549      * a context.
550      * Equivalent to
551      * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)
552      */
hasNfcFeature()553     private static boolean hasNfcFeature() {
554         IPackageManager pm = ActivityThread.getPackageManager();
555         if (pm == null) {
556             Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
557             return false;
558         }
559         try {
560             return pm.hasSystemFeature(PackageManager.FEATURE_NFC, 0);
561         } catch (RemoteException e) {
562             Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
563             return false;
564         }
565     }
566 
567     /**
568      * Helper to check if this device is NFC HCE capable, by checking for
569      * FEATURE_NFC_HOST_CARD_EMULATION and/or FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
570      * but without using a context.
571      */
hasNfcHceFeature()572     private static boolean hasNfcHceFeature() {
573         IPackageManager pm = ActivityThread.getPackageManager();
574         if (pm == null) {
575             Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
576             return false;
577         }
578         try {
579             return pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION, 0)
580                 || pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF, 0);
581         } catch (RemoteException e) {
582             Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
583             return false;
584         }
585     }
586 
587     /**
588      * Return list of Secure Elements which support off host card emulation.
589      *
590      * @return List<String> containing secure elements on the device which supports
591      *                      off host card emulation. eSE for Embedded secure element,
592      *                      SIM for UICC and so on.
593      * @hide
594      */
getSupportedOffHostSecureElements()595     public @NonNull List<String> getSupportedOffHostSecureElements() {
596         List<String> offHostSE = new ArrayList<String>();
597         IPackageManager pm = ActivityThread.getPackageManager();
598         if (pm == null) {
599             Log.e(TAG, "Cannot get package manager, assuming no off-host CE feature");
600             return offHostSE;
601         }
602         try {
603             if (pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC, 0)) {
604                 offHostSE.add("SIM");
605             }
606             if (pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE, 0)) {
607                 offHostSE.add("eSE");
608             }
609         } catch (RemoteException e) {
610             Log.e(TAG, "Package manager query failed, assuming no off-host CE feature", e);
611             offHostSE.clear();
612             return offHostSE;
613         }
614         return offHostSE;
615     }
616 
617     /**
618      * Returns the NfcAdapter for application context,
619      * or throws if NFC is not available.
620      * @hide
621      */
622     @UnsupportedAppUsage
getNfcAdapter(Context context)623     public static synchronized NfcAdapter getNfcAdapter(Context context) {
624         if (!sIsInitialized) {
625             sHasNfcFeature = hasNfcFeature();
626             sHasBeamFeature = hasBeamFeature();
627             boolean hasHceFeature = hasNfcHceFeature();
628             /* is this device meant to have NFC */
629             if (!sHasNfcFeature && !hasHceFeature) {
630                 Log.v(TAG, "this device does not have NFC support");
631                 throw new UnsupportedOperationException();
632             }
633             sService = getServiceInterface();
634             if (sService == null) {
635                 Log.e(TAG, "could not retrieve NFC service");
636                 throw new UnsupportedOperationException();
637             }
638             if (sHasNfcFeature) {
639                 try {
640                     sTagService = sService.getNfcTagInterface();
641                 } catch (RemoteException e) {
642                     Log.e(TAG, "could not retrieve NFC Tag service");
643                     throw new UnsupportedOperationException();
644                 }
645             }
646             if (hasHceFeature) {
647                 try {
648                     sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface();
649                 } catch (RemoteException e) {
650                     Log.e(TAG, "could not retrieve NFC-F card emulation service");
651                     throw new UnsupportedOperationException();
652                 }
653                 try {
654                     sCardEmulationService = sService.getNfcCardEmulationInterface();
655                 } catch (RemoteException e) {
656                     Log.e(TAG, "could not retrieve card emulation service");
657                     throw new UnsupportedOperationException();
658                 }
659             }
660 
661             sIsInitialized = true;
662         }
663         if (context == null) {
664             if (sNullContextNfcAdapter == null) {
665                 sNullContextNfcAdapter = new NfcAdapter(null);
666             }
667             return sNullContextNfcAdapter;
668         }
669         NfcAdapter adapter = sNfcAdapters.get(context);
670         if (adapter == null) {
671             adapter = new NfcAdapter(context);
672             sNfcAdapters.put(context, adapter);
673         }
674         return adapter;
675     }
676 
677     /** get handle to NFC service interface */
getServiceInterface()678     private static INfcAdapter getServiceInterface() {
679         /* get a handle to NFC service */
680         IBinder b = ServiceManager.getService("nfc");
681         if (b == null) {
682             return null;
683         }
684         return INfcAdapter.Stub.asInterface(b);
685     }
686 
687     /**
688      * Helper to get the default NFC Adapter.
689      * <p>
690      * Most Android devices will only have one NFC Adapter (NFC Controller).
691      * <p>
692      * This helper is the equivalent of:
693      * <pre>
694      * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
695      * NfcAdapter adapter = manager.getDefaultAdapter();</pre>
696      * @param context the calling application's context
697      *
698      * @return the default NFC adapter, or null if no NFC adapter exists
699      */
getDefaultAdapter(Context context)700     public static NfcAdapter getDefaultAdapter(Context context) {
701         if (context == null) {
702             throw new IllegalArgumentException("context cannot be null");
703         }
704         context = context.getApplicationContext();
705         if (context == null) {
706             throw new IllegalArgumentException(
707                     "context not associated with any application (using a mock context?)");
708         }
709 
710         if (getServiceInterface() == null) {
711             // NFC is not available
712             return null;
713         }
714 
715         /* use getSystemService() for consistency */
716         NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
717         if (manager == null) {
718             // NFC not available
719             return null;
720         }
721         return manager.getDefaultAdapter();
722     }
723 
724     /**
725      * Legacy NfcAdapter getter, always use {@link #getDefaultAdapter(Context)} instead.<p>
726      * This method was deprecated at API level 10 (Gingerbread MR1) because a context is required
727      * for many NFC API methods. Those methods will fail when called on an NfcAdapter
728      * object created from this method.<p>
729      * @deprecated use {@link #getDefaultAdapter(Context)}
730      * @hide
731      */
732     @Deprecated
733     @UnsupportedAppUsage
getDefaultAdapter()734     public static NfcAdapter getDefaultAdapter() {
735         // introduced in API version 9 (GB 2.3)
736         // deprecated in API version 10 (GB 2.3.3)
737         // removed from public API in version 16 (ICS MR2)
738         // should maintain as a hidden API for binary compatibility for a little longer
739         Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " +
740                 "NfcAdapter.getDefaultAdapter(Context) instead", new Exception());
741 
742         return NfcAdapter.getNfcAdapter(null);
743     }
744 
NfcAdapter(Context context)745     NfcAdapter(Context context) {
746         mContext = context;
747         mNfcActivityManager = new NfcActivityManager(this);
748         mNfcUnlockHandlers = new HashMap<NfcUnlockHandler, INfcUnlockHandler>();
749         mTagRemovedListener = null;
750         mLock = new Object();
751         mControllerAlwaysOnListener = new NfcControllerAlwaysOnListener(getService());
752     }
753 
754     /**
755      * @hide
756      */
757     @UnsupportedAppUsage
getContext()758     public Context getContext() {
759         return mContext;
760     }
761 
762     /**
763      * Returns the binder interface to the service.
764      * @hide
765      */
766     @UnsupportedAppUsage
getService()767     public INfcAdapter getService() {
768         isEnabled();  // NOP call to recover sService if it is stale
769         return sService;
770     }
771 
772     /**
773      * Returns the binder interface to the tag service.
774      * @hide
775      */
getTagService()776     public INfcTag getTagService() {
777         isEnabled();  // NOP call to recover sTagService if it is stale
778         return sTagService;
779     }
780 
781     /**
782      * Returns the binder interface to the card emulation service.
783      * @hide
784      */
getCardEmulationService()785     public INfcCardEmulation getCardEmulationService() {
786         isEnabled();
787         return sCardEmulationService;
788     }
789 
790     /**
791      * Returns the binder interface to the NFC-F card emulation service.
792      * @hide
793      */
getNfcFCardEmulationService()794     public INfcFCardEmulation getNfcFCardEmulationService() {
795         isEnabled();
796         return sNfcFCardEmulationService;
797     }
798 
799     /**
800      * Returns the binder interface to the NFC-DTA test interface.
801      * @hide
802      */
getNfcDtaInterface()803     public INfcDta getNfcDtaInterface() {
804         if (mContext == null) {
805             throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
806                     + " NFC extras APIs");
807         }
808         try {
809             return sService.getNfcDtaInterface(mContext.getPackageName());
810         } catch (RemoteException e) {
811             attemptDeadServiceRecovery(e);
812             // Try one more time
813             if (sService == null) {
814                 Log.e(TAG, "Failed to recover NFC Service.");
815                 return null;
816             }
817             try {
818                 return sService.getNfcDtaInterface(mContext.getPackageName());
819             } catch (RemoteException ee) {
820                 Log.e(TAG, "Failed to recover NFC Service.");
821             }
822             return null;
823         }
824     }
825 
826     /**
827      * NFC service dead - attempt best effort recovery
828      * @hide
829      */
830     @UnsupportedAppUsage
attemptDeadServiceRecovery(Exception e)831     public void attemptDeadServiceRecovery(Exception e) {
832         Log.e(TAG, "NFC service dead - attempting to recover", e);
833         INfcAdapter service = getServiceInterface();
834         if (service == null) {
835             Log.e(TAG, "could not retrieve NFC service during service recovery");
836             // nothing more can be done now, sService is still stale, we'll hit
837             // this recovery path again later
838             return;
839         }
840         // assigning to sService is not thread-safe, but this is best-effort code
841         // and on a well-behaved system should never happen
842         sService = service;
843         try {
844             sTagService = service.getNfcTagInterface();
845         } catch (RemoteException ee) {
846             Log.e(TAG, "could not retrieve NFC tag service during service recovery");
847             // nothing more can be done now, sService is still stale, we'll hit
848             // this recovery path again later
849             return;
850         }
851 
852         try {
853             sCardEmulationService = service.getNfcCardEmulationInterface();
854         } catch (RemoteException ee) {
855             Log.e(TAG, "could not retrieve NFC card emulation service during service recovery");
856         }
857 
858         try {
859             sNfcFCardEmulationService = service.getNfcFCardEmulationInterface();
860         } catch (RemoteException ee) {
861             Log.e(TAG, "could not retrieve NFC-F card emulation service during service recovery");
862         }
863 
864         return;
865     }
866 
867     /**
868      * Return true if this NFC Adapter has any features enabled.
869      *
870      * <p>If this method returns false, the NFC hardware is guaranteed not to
871      * generate or respond to any NFC communication over its NFC radio.
872      * <p>Applications can use this to check if NFC is enabled. Applications
873      * can request Settings UI allowing the user to toggle NFC using:
874      * <p><pre>startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))</pre>
875      *
876      * @see android.provider.Settings#ACTION_NFC_SETTINGS
877      * @return true if this NFC Adapter has any features enabled
878      */
isEnabled()879     public boolean isEnabled() {
880         try {
881             return sService.getState() == STATE_ON;
882         } catch (RemoteException e) {
883             attemptDeadServiceRecovery(e);
884             // Try one more time
885             if (sService == null) {
886                 Log.e(TAG, "Failed to recover NFC Service.");
887                 return false;
888             }
889             try {
890                 return sService.getState() == STATE_ON;
891             } catch (RemoteException ee) {
892                 Log.e(TAG, "Failed to recover NFC Service.");
893             }
894             return false;
895         }
896     }
897 
898     /**
899      * Return the state of this NFC Adapter.
900      *
901      * <p>Returns one of {@link #STATE_ON}, {@link #STATE_TURNING_ON},
902      * {@link #STATE_OFF}, {@link #STATE_TURNING_OFF}.
903      *
904      * <p>{@link #isEnabled()} is equivalent to
905      * <code>{@link #getAdapterState()} == {@link #STATE_ON}</code>
906      *
907      * @return the current state of this NFC adapter
908      *
909      * @hide
910      */
911     @UnsupportedAppUsage
getAdapterState()912     public int getAdapterState() {
913         try {
914             return sService.getState();
915         } catch (RemoteException e) {
916             attemptDeadServiceRecovery(e);
917             // Try one more time
918             if (sService == null) {
919                 Log.e(TAG, "Failed to recover NFC Service.");
920                 return NfcAdapter.STATE_OFF;
921             }
922             try {
923                 return sService.getState();
924             } catch (RemoteException ee) {
925                 Log.e(TAG, "Failed to recover NFC Service.");
926             }
927             return NfcAdapter.STATE_OFF;
928         }
929     }
930 
931     /**
932      * Enable NFC hardware.
933      *
934      * <p>This call is asynchronous. Listen for
935      * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
936      * operation is complete.
937      *
938      * <p>If this returns true, then either NFC is already on, or
939      * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
940      * to indicate a state transition. If this returns false, then
941      * there is some problem that prevents an attempt to turn
942      * NFC on (for example we are in airplane mode and NFC is not
943      * toggleable in airplane mode on this platform).
944      *
945      * @hide
946      */
947     @SystemApi
948     @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
enable()949     public boolean enable() {
950         try {
951             return sService.enable();
952         } catch (RemoteException e) {
953             attemptDeadServiceRecovery(e);
954             // Try one more time
955             if (sService == null) {
956                 Log.e(TAG, "Failed to recover NFC Service.");
957                 return false;
958             }
959             try {
960                 return sService.enable();
961             } catch (RemoteException ee) {
962                 Log.e(TAG, "Failed to recover NFC Service.");
963             }
964             return false;
965         }
966     }
967 
968     /**
969      * Disable NFC hardware.
970      *
971      * <p>No NFC features will work after this call, and the hardware
972      * will not perform or respond to any NFC communication.
973      *
974      * <p>This call is asynchronous. Listen for
975      * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
976      * operation is complete.
977      *
978      * <p>If this returns true, then either NFC is already off, or
979      * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
980      * to indicate a state transition. If this returns false, then
981      * there is some problem that prevents an attempt to turn
982      * NFC off.
983      *
984      * @hide
985      */
986     @SystemApi
987     @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
disable()988     public boolean disable() {
989         try {
990             return sService.disable(true);
991         } catch (RemoteException e) {
992             attemptDeadServiceRecovery(e);
993             // Try one more time
994             if (sService == null) {
995                 Log.e(TAG, "Failed to recover NFC Service.");
996                 return false;
997             }
998             try {
999                 return sService.disable(true);
1000             } catch (RemoteException ee) {
1001                 Log.e(TAG, "Failed to recover NFC Service.");
1002             }
1003             return false;
1004         }
1005     }
1006 
1007     /**
1008      * Disable NFC hardware.
1009      * @hide
1010     */
1011     @SystemApi
1012     @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
disable(boolean persist)1013     public boolean disable(boolean persist) {
1014         try {
1015             return sService.disable(persist);
1016         } catch (RemoteException e) {
1017             attemptDeadServiceRecovery(e);
1018             // Try one more time
1019             if (sService == null) {
1020                 Log.e(TAG, "Failed to recover NFC Service.");
1021                 return false;
1022             }
1023             try {
1024                 return sService.disable(persist);
1025             } catch (RemoteException ee) {
1026                 Log.e(TAG, "Failed to recover NFC Service.");
1027             }
1028             return false;
1029         }
1030     }
1031 
1032     /**
1033      * Pauses polling for a {@code timeoutInMs} millis. If polling must be resumed before timeout,
1034      * use {@link #resumePolling()}.
1035      * @hide
1036      */
pausePolling(int timeoutInMs)1037     public void pausePolling(int timeoutInMs) {
1038         try {
1039             sService.pausePolling(timeoutInMs);
1040         } catch (RemoteException e) {
1041             attemptDeadServiceRecovery(e);
1042         }
1043     }
1044 
1045     /**
1046      * Resumes default polling for the current device state if polling is paused. Calling
1047      * this while polling is not paused is a no-op.
1048      *
1049      * @hide
1050      */
resumePolling()1051     public void resumePolling() {
1052         try {
1053             sService.resumePolling();
1054         } catch (RemoteException e) {
1055             attemptDeadServiceRecovery(e);
1056         }
1057     }
1058 
1059     /**
1060      * Set one or more {@link Uri}s to send using Android Beam (TM). Every
1061      * Uri you provide must have either scheme 'file' or scheme 'content'.
1062      *
1063      * <p>For the data provided through this method, Android Beam tries to
1064      * switch to alternate transports such as Bluetooth to achieve a fast
1065      * transfer speed. Hence this method is very suitable
1066      * for transferring large files such as pictures or songs.
1067      *
1068      * <p>The receiving side will store the content of each Uri in
1069      * a file and present a notification to the user to open the file
1070      * with a {@link android.content.Intent} with action
1071      * {@link android.content.Intent#ACTION_VIEW}.
1072      * If multiple URIs are sent, the {@link android.content.Intent} will refer
1073      * to the first of the stored files.
1074      *
1075      * <p>This method may be called at any time before {@link Activity#onDestroy},
1076      * but the URI(s) are only made available for Android Beam when the
1077      * specified activity(s) are in resumed (foreground) state. The recommended
1078      * approach is to call this method during your Activity's
1079      * {@link Activity#onCreate} - see sample
1080      * code below. This method does not immediately perform any I/O or blocking work,
1081      * so is safe to call on your main thread.
1082      *
1083      * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
1084      * have priority over both {@link #setNdefPushMessage} and
1085      * {@link #setNdefPushMessageCallback}.
1086      *
1087      * <p>If {@link #setBeamPushUris} is called with a null Uri array,
1088      * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
1089      * then the Uri push will be completely disabled for the specified activity(s).
1090      *
1091      * <p>Code example:
1092      * <pre>
1093      * protected void onCreate(Bundle savedInstanceState) {
1094      *     super.onCreate(savedInstanceState);
1095      *     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1096      *     if (nfcAdapter == null) return;  // NFC not available on this device
1097      *     nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this);
1098      * }</pre>
1099      * And that is it. Only one call per activity is necessary. The Android
1100      * OS will automatically release its references to the Uri(s) and the
1101      * Activity object when it is destroyed if you follow this pattern.
1102      *
1103      * <p>If your Activity wants to dynamically supply Uri(s),
1104      * then set a callback using {@link #setBeamPushUrisCallback} instead
1105      * of using this method.
1106      *
1107      * <p class="note">Do not pass in an Activity that has already been through
1108      * {@link Activity#onDestroy}. This is guaranteed if you call this API
1109      * during {@link Activity#onCreate}.
1110      *
1111      * <p class="note">If this device does not support alternate transports
1112      * such as Bluetooth or WiFI, calling this method does nothing.
1113      *
1114      * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1115      *
1116      * @param uris an array of Uri(s) to push over Android Beam
1117      * @param activity activity for which the Uri(s) will be pushed
1118      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1119      * @deprecated this feature is deprecated. File sharing can work using other technology like
1120      * Bluetooth.
1121      */
1122     @java.lang.Deprecated
setBeamPushUris(Uri[] uris, Activity activity)1123     public void setBeamPushUris(Uri[] uris, Activity activity) {
1124         synchronized (NfcAdapter.class) {
1125             if (!sHasNfcFeature) {
1126                 throw new UnsupportedOperationException();
1127             }
1128             if (!sHasBeamFeature) {
1129                 return;
1130             }
1131         }
1132         if (activity == null) {
1133             throw new NullPointerException("activity cannot be null");
1134         }
1135         if (uris != null) {
1136             for (Uri uri : uris) {
1137                 if (uri == null) throw new NullPointerException("Uri not " +
1138                         "allowed to be null");
1139                 String scheme = uri.getScheme();
1140                 if (scheme == null || (!scheme.equalsIgnoreCase("file") &&
1141                         !scheme.equalsIgnoreCase("content"))) {
1142                     throw new IllegalArgumentException("URI needs to have " +
1143                             "either scheme file or scheme content");
1144                 }
1145             }
1146         }
1147         mNfcActivityManager.setNdefPushContentUri(activity, uris);
1148     }
1149 
1150     /**
1151      * Set a callback that will dynamically generate one or more {@link Uri}s
1152      * to send using Android Beam (TM). Every Uri the callback provides
1153      * must have either scheme 'file' or scheme 'content'.
1154      *
1155      * <p>For the data provided through this callback, Android Beam tries to
1156      * switch to alternate transports such as Bluetooth to achieve a fast
1157      * transfer speed. Hence this method is very suitable
1158      * for transferring large files such as pictures or songs.
1159      *
1160      * <p>The receiving side will store the content of each Uri in
1161      * a file and present a notification to the user to open the file
1162      * with a {@link android.content.Intent} with action
1163      * {@link android.content.Intent#ACTION_VIEW}.
1164      * If multiple URIs are sent, the {@link android.content.Intent} will refer
1165      * to the first of the stored files.
1166      *
1167      * <p>This method may be called at any time before {@link Activity#onDestroy},
1168      * but the URI(s) are only made available for Android Beam when the
1169      * specified activity(s) are in resumed (foreground) state. The recommended
1170      * approach is to call this method during your Activity's
1171      * {@link Activity#onCreate} - see sample
1172      * code below. This method does not immediately perform any I/O or blocking work,
1173      * so is safe to call on your main thread.
1174      *
1175      * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
1176      * have priority over both {@link #setNdefPushMessage} and
1177      * {@link #setNdefPushMessageCallback}.
1178      *
1179      * <p>If {@link #setBeamPushUris} is called with a null Uri array,
1180      * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
1181      * then the Uri push will be completely disabled for the specified activity(s).
1182      *
1183      * <p>Code example:
1184      * <pre>
1185      * protected void onCreate(Bundle savedInstanceState) {
1186      *     super.onCreate(savedInstanceState);
1187      *     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1188      *     if (nfcAdapter == null) return;  // NFC not available on this device
1189      *     nfcAdapter.setBeamPushUrisCallback(callback, this);
1190      * }</pre>
1191      * And that is it. Only one call per activity is necessary. The Android
1192      * OS will automatically release its references to the Uri(s) and the
1193      * Activity object when it is destroyed if you follow this pattern.
1194      *
1195      * <p class="note">Do not pass in an Activity that has already been through
1196      * {@link Activity#onDestroy}. This is guaranteed if you call this API
1197      * during {@link Activity#onCreate}.
1198      *
1199      * <p class="note">If this device does not support alternate transports
1200      * such as Bluetooth or WiFI, calling this method does nothing.
1201      *
1202      * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1203      *
1204      * @param callback callback, or null to disable
1205      * @param activity activity for which the Uri(s) will be pushed
1206      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1207      * @deprecated this feature is deprecated. File sharing can work using other technology like
1208      * Bluetooth.
1209      */
1210     @java.lang.Deprecated
setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity)1211     public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) {
1212         synchronized (NfcAdapter.class) {
1213             if (!sHasNfcFeature) {
1214                 throw new UnsupportedOperationException();
1215             }
1216             if (!sHasBeamFeature) {
1217                 return;
1218             }
1219         }
1220         if (activity == null) {
1221             throw new NullPointerException("activity cannot be null");
1222         }
1223         mNfcActivityManager.setNdefPushContentUriCallback(activity, callback);
1224     }
1225 
1226     /**
1227      * Set a static {@link NdefMessage} to send using Android Beam (TM).
1228      *
1229      * <p>This method may be called at any time before {@link Activity#onDestroy},
1230      * but the NDEF message is only made available for NDEF push when the
1231      * specified activity(s) are in resumed (foreground) state. The recommended
1232      * approach is to call this method during your Activity's
1233      * {@link Activity#onCreate} - see sample
1234      * code below. This method does not immediately perform any I/O or blocking work,
1235      * so is safe to call on your main thread.
1236      *
1237      * <p>Only one NDEF message can be pushed by the currently resumed activity.
1238      * If both {@link #setNdefPushMessage} and
1239      * {@link #setNdefPushMessageCallback} are set, then
1240      * the callback will take priority.
1241      *
1242      * <p>If neither {@link #setNdefPushMessage} or
1243      * {@link #setNdefPushMessageCallback} have been called for your activity, then
1244      * the Android OS may choose to send a default NDEF message on your behalf,
1245      * such as a URI for your application.
1246      *
1247      * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
1248      * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
1249      * then NDEF push will be completely disabled for the specified activity(s).
1250      * This also disables any default NDEF message the Android OS would have
1251      * otherwise sent on your behalf for those activity(s).
1252      *
1253      * <p>If you want to prevent the Android OS from sending default NDEF
1254      * messages completely (for all activities), you can include a
1255      * {@code <meta-data>} element inside the {@code <application>}
1256      * element of your AndroidManifest.xml file, like this:
1257      * <pre>
1258      * &lt;application ...>
1259      *     &lt;meta-data android:name="android.nfc.disable_beam_default"
1260      *         android:value="true" />
1261      * &lt;/application></pre>
1262      *
1263      * <p>The API allows for multiple activities to be specified at a time,
1264      * but it is strongly recommended to just register one at a time,
1265      * and to do so during the activity's {@link Activity#onCreate}. For example:
1266      * <pre>
1267      * protected void onCreate(Bundle savedInstanceState) {
1268      *     super.onCreate(savedInstanceState);
1269      *     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1270      *     if (nfcAdapter == null) return;  // NFC not available on this device
1271      *     nfcAdapter.setNdefPushMessage(ndefMessage, this);
1272      * }</pre>
1273      * And that is it. Only one call per activity is necessary. The Android
1274      * OS will automatically release its references to the NDEF message and the
1275      * Activity object when it is destroyed if you follow this pattern.
1276      *
1277      * <p>If your Activity wants to dynamically generate an NDEF message,
1278      * then set a callback using {@link #setNdefPushMessageCallback} instead
1279      * of a static message.
1280      *
1281      * <p class="note">Do not pass in an Activity that has already been through
1282      * {@link Activity#onDestroy}. This is guaranteed if you call this API
1283      * during {@link Activity#onCreate}.
1284      *
1285      * <p class="note">For sending large content such as pictures and songs,
1286      * consider using {@link #setBeamPushUris}, which switches to alternate transports
1287      * such as Bluetooth to achieve a fast transfer rate.
1288      *
1289      * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1290      *
1291      * @param message NDEF message to push over NFC, or null to disable
1292      * @param activity activity for which the NDEF message will be pushed
1293      * @param activities optional additional activities, however we strongly recommend
1294      *        to only register one at a time, and to do so in that activity's
1295      *        {@link Activity#onCreate}
1296      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1297      * @deprecated this feature is deprecated. File sharing can work using other technology like
1298      * Bluetooth.
1299      */
1300     @java.lang.Deprecated
setNdefPushMessage(NdefMessage message, Activity activity, Activity ... activities)1301     public void setNdefPushMessage(NdefMessage message, Activity activity,
1302             Activity ... activities) {
1303         synchronized (NfcAdapter.class) {
1304             if (!sHasNfcFeature) {
1305                 throw new UnsupportedOperationException();
1306             }
1307             if (!sHasBeamFeature) {
1308                 return;
1309             }
1310         }
1311         int targetSdkVersion = getSdkVersion();
1312         try {
1313             if (activity == null) {
1314                 throw new NullPointerException("activity cannot be null");
1315             }
1316             mNfcActivityManager.setNdefPushMessage(activity, message, 0);
1317             for (Activity a : activities) {
1318                 if (a == null) {
1319                     throw new NullPointerException("activities cannot contain null");
1320                 }
1321                 mNfcActivityManager.setNdefPushMessage(a, message, 0);
1322             }
1323         } catch (IllegalStateException e) {
1324             if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1325                 // Less strict on old applications - just log the error
1326                 Log.e(TAG, "Cannot call API with Activity that has already " +
1327                         "been destroyed", e);
1328             } else {
1329                 // Prevent new applications from making this mistake, re-throw
1330                 throw(e);
1331             }
1332         }
1333     }
1334 
1335     /**
1336      * @hide
1337      */
1338     @SystemApi
setNdefPushMessage(NdefMessage message, Activity activity, int flags)1339     public void setNdefPushMessage(NdefMessage message, Activity activity, int flags) {
1340         synchronized (NfcAdapter.class) {
1341             if (!sHasNfcFeature) {
1342                 throw new UnsupportedOperationException();
1343             }
1344         }
1345         if (activity == null) {
1346             throw new NullPointerException("activity cannot be null");
1347         }
1348         mNfcActivityManager.setNdefPushMessage(activity, message, flags);
1349     }
1350 
1351     /**
1352      * Set a callback that dynamically generates NDEF messages to send using Android Beam (TM).
1353      *
1354      * <p>This method may be called at any time before {@link Activity#onDestroy},
1355      * but the NDEF message callback can only occur when the
1356      * specified activity(s) are in resumed (foreground) state. The recommended
1357      * approach is to call this method during your Activity's
1358      * {@link Activity#onCreate} - see sample
1359      * code below. This method does not immediately perform any I/O or blocking work,
1360      * so is safe to call on your main thread.
1361      *
1362      * <p>Only one NDEF message can be pushed by the currently resumed activity.
1363      * If both {@link #setNdefPushMessage} and
1364      * {@link #setNdefPushMessageCallback} are set, then
1365      * the callback will take priority.
1366      *
1367      * <p>If neither {@link #setNdefPushMessage} or
1368      * {@link #setNdefPushMessageCallback} have been called for your activity, then
1369      * the Android OS may choose to send a default NDEF message on your behalf,
1370      * such as a URI for your application.
1371      *
1372      * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
1373      * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
1374      * then NDEF push will be completely disabled for the specified activity(s).
1375      * This also disables any default NDEF message the Android OS would have
1376      * otherwise sent on your behalf for those activity(s).
1377      *
1378      * <p>If you want to prevent the Android OS from sending default NDEF
1379      * messages completely (for all activities), you can include a
1380      * {@code <meta-data>} element inside the {@code <application>}
1381      * element of your AndroidManifest.xml file, like this:
1382      * <pre>
1383      * &lt;application ...>
1384      *     &lt;meta-data android:name="android.nfc.disable_beam_default"
1385      *         android:value="true" />
1386      * &lt;/application></pre>
1387      *
1388      * <p>The API allows for multiple activities to be specified at a time,
1389      * but it is strongly recommended to just register one at a time,
1390      * and to do so during the activity's {@link Activity#onCreate}. For example:
1391      * <pre>
1392      * protected void onCreate(Bundle savedInstanceState) {
1393      *     super.onCreate(savedInstanceState);
1394      *     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1395      *     if (nfcAdapter == null) return;  // NFC not available on this device
1396      *     nfcAdapter.setNdefPushMessageCallback(callback, this);
1397      * }</pre>
1398      * And that is it. Only one call per activity is necessary. The Android
1399      * OS will automatically release its references to the callback and the
1400      * Activity object when it is destroyed if you follow this pattern.
1401      *
1402      * <p class="note">Do not pass in an Activity that has already been through
1403      * {@link Activity#onDestroy}. This is guaranteed if you call this API
1404      * during {@link Activity#onCreate}.
1405      * <p class="note">For sending large content such as pictures and songs,
1406      * consider using {@link #setBeamPushUris}, which switches to alternate transports
1407      * such as Bluetooth to achieve a fast transfer rate.
1408      * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1409      *
1410      * @param callback callback, or null to disable
1411      * @param activity activity for which the NDEF message will be pushed
1412      * @param activities optional additional activities, however we strongly recommend
1413      *        to only register one at a time, and to do so in that activity's
1414      *        {@link Activity#onCreate}
1415      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1416      * @deprecated this feature is deprecated. File sharing can work using other technology like
1417      * Bluetooth.
1418      */
1419     @java.lang.Deprecated
setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity, Activity ... activities)1420     public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
1421             Activity ... activities) {
1422         synchronized (NfcAdapter.class) {
1423             if (!sHasNfcFeature) {
1424                 throw new UnsupportedOperationException();
1425             }
1426             if (!sHasBeamFeature) {
1427                 return;
1428             }
1429         }
1430         int targetSdkVersion = getSdkVersion();
1431         try {
1432             if (activity == null) {
1433                 throw new NullPointerException("activity cannot be null");
1434             }
1435             mNfcActivityManager.setNdefPushMessageCallback(activity, callback, 0);
1436             for (Activity a : activities) {
1437                 if (a == null) {
1438                     throw new NullPointerException("activities cannot contain null");
1439                 }
1440                 mNfcActivityManager.setNdefPushMessageCallback(a, callback, 0);
1441             }
1442         } catch (IllegalStateException e) {
1443             if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1444                 // Less strict on old applications - just log the error
1445                 Log.e(TAG, "Cannot call API with Activity that has already " +
1446                         "been destroyed", e);
1447             } else {
1448                 // Prevent new applications from making this mistake, re-throw
1449                 throw(e);
1450             }
1451         }
1452     }
1453 
1454     /**
1455      * @hide
1456      */
1457     @UnsupportedAppUsage
setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity, int flags)1458     public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
1459             int flags) {
1460         if (activity == null) {
1461             throw new NullPointerException("activity cannot be null");
1462         }
1463         mNfcActivityManager.setNdefPushMessageCallback(activity, callback, flags);
1464     }
1465 
1466     /**
1467      * Set a callback on successful Android Beam (TM).
1468      *
1469      * <p>This method may be called at any time before {@link Activity#onDestroy},
1470      * but the callback can only occur when the
1471      * specified activity(s) are in resumed (foreground) state. The recommended
1472      * approach is to call this method during your Activity's
1473      * {@link Activity#onCreate} - see sample
1474      * code below. This method does not immediately perform any I/O or blocking work,
1475      * so is safe to call on your main thread.
1476      *
1477      * <p>The API allows for multiple activities to be specified at a time,
1478      * but it is strongly recommended to just register one at a time,
1479      * and to do so during the activity's {@link Activity#onCreate}. For example:
1480      * <pre>
1481      * protected void onCreate(Bundle savedInstanceState) {
1482      *     super.onCreate(savedInstanceState);
1483      *     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1484      *     if (nfcAdapter == null) return;  // NFC not available on this device
1485      *     nfcAdapter.setOnNdefPushCompleteCallback(callback, this);
1486      * }</pre>
1487      * And that is it. Only one call per activity is necessary. The Android
1488      * OS will automatically release its references to the callback and the
1489      * Activity object when it is destroyed if you follow this pattern.
1490      *
1491      * <p class="note">Do not pass in an Activity that has already been through
1492      * {@link Activity#onDestroy}. This is guaranteed if you call this API
1493      * during {@link Activity#onCreate}.
1494      *
1495      * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1496      *
1497      * @param callback callback, or null to disable
1498      * @param activity activity for which the NDEF message will be pushed
1499      * @param activities optional additional activities, however we strongly recommend
1500      *        to only register one at a time, and to do so in that activity's
1501      *        {@link Activity#onCreate}
1502      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1503      * @deprecated this feature is deprecated. File sharing can work using other technology like
1504      * Bluetooth.
1505      */
1506     @java.lang.Deprecated
setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback, Activity activity, Activity ... activities)1507     public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback,
1508             Activity activity, Activity ... activities) {
1509         synchronized (NfcAdapter.class) {
1510             if (!sHasNfcFeature) {
1511                 throw new UnsupportedOperationException();
1512             }
1513             if (!sHasBeamFeature) {
1514                 return;
1515             }
1516         }
1517         int targetSdkVersion = getSdkVersion();
1518         try {
1519             if (activity == null) {
1520                 throw new NullPointerException("activity cannot be null");
1521             }
1522             mNfcActivityManager.setOnNdefPushCompleteCallback(activity, callback);
1523             for (Activity a : activities) {
1524                 if (a == null) {
1525                     throw new NullPointerException("activities cannot contain null");
1526                 }
1527                 mNfcActivityManager.setOnNdefPushCompleteCallback(a, callback);
1528             }
1529         } catch (IllegalStateException e) {
1530             if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1531                 // Less strict on old applications - just log the error
1532                 Log.e(TAG, "Cannot call API with Activity that has already " +
1533                         "been destroyed", e);
1534             } else {
1535                 // Prevent new applications from making this mistake, re-throw
1536                 throw(e);
1537             }
1538         }
1539     }
1540 
1541     /**
1542      * Enable foreground dispatch to the given Activity.
1543      *
1544      * <p>This will give priority to the foreground activity when
1545      * dispatching a discovered {@link Tag} to an application.
1546      *
1547      * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents
1548      * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and
1549      * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED}
1550      * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled
1551      * by passing in the tech lists separately. Each first level entry in the tech list represents
1552      * an array of technologies that must all be present to match. If any of the first level sets
1553      * match then the dispatch is routed through the given PendingIntent. In other words, the second
1554      * level is ANDed together and the first level entries are ORed together.
1555      *
1556      * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters
1557      * that acts a wild card and will cause the foreground activity to receive all tags via the
1558      * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent.
1559      *
1560      * <p>This method must be called from the main thread, and only when the activity is in the
1561      * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before
1562      * the completion of their {@link Activity#onPause} callback to disable foreground dispatch
1563      * after it has been enabled.
1564      *
1565      * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1566      *
1567      * @param activity the Activity to dispatch to
1568      * @param intent the PendingIntent to start for the dispatch
1569      * @param filters the IntentFilters to override dispatching for, or null to always dispatch
1570      * @param techLists the tech lists used to perform matching for dispatching of the
1571      *      {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent
1572      * @throws IllegalStateException if the Activity is not currently in the foreground
1573      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1574      */
enableForegroundDispatch(Activity activity, PendingIntent intent, IntentFilter[] filters, String[][] techLists)1575     public void enableForegroundDispatch(Activity activity, PendingIntent intent,
1576             IntentFilter[] filters, String[][] techLists) {
1577         synchronized (NfcAdapter.class) {
1578             if (!sHasNfcFeature) {
1579                 throw new UnsupportedOperationException();
1580             }
1581         }
1582         if (activity == null || intent == null) {
1583             throw new NullPointerException();
1584         }
1585         if (!activity.isResumed()) {
1586             throw new IllegalStateException("Foreground dispatch can only be enabled " +
1587                     "when your activity is resumed");
1588         }
1589         try {
1590             TechListParcel parcel = null;
1591             if (techLists != null && techLists.length > 0) {
1592                 parcel = new TechListParcel(techLists);
1593             }
1594             ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
1595                     mForegroundDispatchListener);
1596             sService.setForegroundDispatch(intent, filters, parcel);
1597         } catch (RemoteException e) {
1598             attemptDeadServiceRecovery(e);
1599         }
1600     }
1601 
1602     /**
1603      * Disable foreground dispatch to the given activity.
1604      *
1605      * <p>After calling {@link #enableForegroundDispatch}, an activity
1606      * must call this method before its {@link Activity#onPause} callback
1607      * completes.
1608      *
1609      * <p>This method must be called from the main thread.
1610      *
1611      * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1612      *
1613      * @param activity the Activity to disable dispatch to
1614      * @throws IllegalStateException if the Activity has already been paused
1615      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1616      */
disableForegroundDispatch(Activity activity)1617     public void disableForegroundDispatch(Activity activity) {
1618         synchronized (NfcAdapter.class) {
1619             if (!sHasNfcFeature) {
1620                 throw new UnsupportedOperationException();
1621             }
1622         }
1623         ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,
1624                 mForegroundDispatchListener);
1625         disableForegroundDispatchInternal(activity, false);
1626     }
1627 
1628     OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() {
1629         @Override
1630         public void onPaused(Activity activity) {
1631             disableForegroundDispatchInternal(activity, true);
1632         }
1633     };
1634 
disableForegroundDispatchInternal(Activity activity, boolean force)1635     void disableForegroundDispatchInternal(Activity activity, boolean force) {
1636         try {
1637             sService.setForegroundDispatch(null, null, null);
1638             if (!force && !activity.isResumed()) {
1639                 throw new IllegalStateException("You must disable foreground dispatching " +
1640                         "while your activity is still resumed");
1641             }
1642         } catch (RemoteException e) {
1643             attemptDeadServiceRecovery(e);
1644         }
1645     }
1646 
1647     /**
1648      * Limit the NFC controller to reader mode while this Activity is in the foreground.
1649      *
1650      * <p>In this mode the NFC controller will only act as an NFC tag reader/writer,
1651      * thus disabling any peer-to-peer (Android Beam) and card-emulation modes of
1652      * the NFC adapter on this device.
1653      *
1654      * <p>Use {@link #FLAG_READER_SKIP_NDEF_CHECK} to prevent the platform from
1655      * performing any NDEF checks in reader mode. Note that this will prevent the
1656      * {@link Ndef} tag technology from being enumerated on the tag, and that
1657      * NDEF-based tag dispatch will not be functional.
1658      *
1659      * <p>For interacting with tags that are emulated on another Android device
1660      * using Android's host-based card-emulation, the recommended flags are
1661      * {@link #FLAG_READER_NFC_A} and {@link #FLAG_READER_SKIP_NDEF_CHECK}.
1662      *
1663      * @param activity the Activity that requests the adapter to be in reader mode
1664      * @param callback the callback to be called when a tag is discovered
1665      * @param flags Flags indicating poll technologies and other optional parameters
1666      * @param extras Additional extras for configuring reader mode.
1667      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1668      */
enableReaderMode(Activity activity, ReaderCallback callback, int flags, Bundle extras)1669     public void enableReaderMode(Activity activity, ReaderCallback callback, int flags,
1670             Bundle extras) {
1671         synchronized (NfcAdapter.class) {
1672             if (!sHasNfcFeature) {
1673                 throw new UnsupportedOperationException();
1674             }
1675         }
1676         mNfcActivityManager.enableReaderMode(activity, callback, flags, extras);
1677     }
1678 
1679     /**
1680      * Restore the NFC adapter to normal mode of operation: supporting
1681      * peer-to-peer (Android Beam), card emulation, and polling for
1682      * all supported tag technologies.
1683      *
1684      * @param activity the Activity that currently has reader mode enabled
1685      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1686      */
disableReaderMode(Activity activity)1687     public void disableReaderMode(Activity activity) {
1688         synchronized (NfcAdapter.class) {
1689             if (!sHasNfcFeature) {
1690                 throw new UnsupportedOperationException();
1691             }
1692         }
1693         mNfcActivityManager.disableReaderMode(activity);
1694     }
1695 
1696     /**
1697      * Manually invoke Android Beam to share data.
1698      *
1699      * <p>The Android Beam animation is normally only shown when two NFC-capable
1700      * devices come into range.
1701      * By calling this method, an Activity can invoke the Beam animation directly
1702      * even if no other NFC device is in range yet. The Beam animation will then
1703      * prompt the user to tap another NFC-capable device to complete the data
1704      * transfer.
1705      *
1706      * <p>The main advantage of using this method is that it avoids the need for the
1707      * user to tap the screen to complete the transfer, as this method already
1708      * establishes the direction of the transfer and the consent of the user to
1709      * share data. Callers are responsible for making sure that the user has
1710      * consented to sharing data on NFC tap.
1711      *
1712      * <p>Note that to use this method, the passed in Activity must have already
1713      * set data to share over Beam by using method calls such as
1714      * {@link #setNdefPushMessageCallback} or
1715      * {@link #setBeamPushUrisCallback}.
1716      *
1717      * @param activity the current foreground Activity that has registered data to share
1718      * @return whether the Beam animation was successfully invoked
1719      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1720      * @deprecated this feature is deprecated. File sharing can work using other technology like
1721      * Bluetooth.
1722      */
1723     @java.lang.Deprecated
invokeBeam(Activity activity)1724     public boolean invokeBeam(Activity activity) {
1725         synchronized (NfcAdapter.class) {
1726             if (!sHasNfcFeature) {
1727                 throw new UnsupportedOperationException();
1728             }
1729             if (!sHasBeamFeature) {
1730                 return false;
1731             }
1732         }
1733         if (activity == null) {
1734             throw new NullPointerException("activity may not be null.");
1735         }
1736         enforceResumed(activity);
1737         try {
1738             sService.invokeBeam();
1739             return true;
1740         } catch (RemoteException e) {
1741             Log.e(TAG, "invokeBeam: NFC process has died.");
1742             attemptDeadServiceRecovery(e);
1743             return false;
1744         }
1745     }
1746 
1747     /**
1748      * @hide
1749      */
invokeBeam(BeamShareData shareData)1750     public boolean invokeBeam(BeamShareData shareData) {
1751         try {
1752             Log.e(TAG, "invokeBeamInternal()");
1753             sService.invokeBeamInternal(shareData);
1754             return true;
1755         } catch (RemoteException e) {
1756             Log.e(TAG, "invokeBeam: NFC process has died.");
1757             attemptDeadServiceRecovery(e);
1758             return false;
1759         }
1760     }
1761 
1762     /**
1763      * Enable NDEF message push over NFC while this Activity is in the foreground.
1764      *
1765      * <p>You must explicitly call this method every time the activity is
1766      * resumed, and you must call {@link #disableForegroundNdefPush} before
1767      * your activity completes {@link Activity#onPause}.
1768      *
1769      * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1770      * instead: it automatically hooks into your activity life-cycle,
1771      * so you do not need to call enable/disable in your onResume/onPause.
1772      *
1773      * <p>For NDEF push to function properly the other NFC device must
1774      * support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or
1775      * Android's "com.android.npp" (Ndef Push Protocol). This was optional
1776      * on Gingerbread level Android NFC devices, but SNEP is mandatory on
1777      * Ice-Cream-Sandwich and beyond.
1778      *
1779      * <p>This method must be called from the main thread.
1780      *
1781      * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1782      *
1783      * @param activity foreground activity
1784      * @param message a NDEF Message to push over NFC
1785      * @throws IllegalStateException if the activity is not currently in the foreground
1786      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1787      * @deprecated use {@link #setNdefPushMessage} instead
1788      */
1789     @Deprecated
enableForegroundNdefPush(Activity activity, NdefMessage message)1790     public void enableForegroundNdefPush(Activity activity, NdefMessage message) {
1791         synchronized (NfcAdapter.class) {
1792             if (!sHasNfcFeature) {
1793                 throw new UnsupportedOperationException();
1794             }
1795             if (!sHasBeamFeature) {
1796                 return;
1797             }
1798         }
1799         if (activity == null || message == null) {
1800             throw new NullPointerException();
1801         }
1802         enforceResumed(activity);
1803         mNfcActivityManager.setNdefPushMessage(activity, message, 0);
1804     }
1805 
1806     /**
1807      * Disable NDEF message push over P2P.
1808      *
1809      * <p>After calling {@link #enableForegroundNdefPush}, an activity
1810      * must call this method before its {@link Activity#onPause} callback
1811      * completes.
1812      *
1813      * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1814      * instead: it automatically hooks into your activity life-cycle,
1815      * so you do not need to call enable/disable in your onResume/onPause.
1816      *
1817      * <p>This method must be called from the main thread.
1818      *
1819      * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1820      *
1821      * @param activity the Foreground activity
1822      * @throws IllegalStateException if the Activity has already been paused
1823      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1824      * @deprecated use {@link #setNdefPushMessage} instead
1825      */
1826     @Deprecated
disableForegroundNdefPush(Activity activity)1827     public void disableForegroundNdefPush(Activity activity) {
1828         synchronized (NfcAdapter.class) {
1829             if (!sHasNfcFeature) {
1830                 throw new UnsupportedOperationException();
1831             }
1832             if (!sHasBeamFeature) {
1833                 return;
1834             }
1835         }
1836         if (activity == null) {
1837             throw new NullPointerException();
1838         }
1839         enforceResumed(activity);
1840         mNfcActivityManager.setNdefPushMessage(activity, null, 0);
1841         mNfcActivityManager.setNdefPushMessageCallback(activity, null, 0);
1842         mNfcActivityManager.setOnNdefPushCompleteCallback(activity, null);
1843     }
1844 
1845     /**
1846      * Sets Secure NFC feature.
1847      * <p>This API is for the Settings application.
1848      * @return True if successful
1849      * @hide
1850      */
1851     @SystemApi
1852     @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
enableSecureNfc(boolean enable)1853     public boolean enableSecureNfc(boolean enable) {
1854         if (!sHasNfcFeature) {
1855             throw new UnsupportedOperationException();
1856         }
1857         try {
1858             return sService.setNfcSecure(enable);
1859         } catch (RemoteException e) {
1860             attemptDeadServiceRecovery(e);
1861             // Try one more time
1862             if (sService == null) {
1863                 Log.e(TAG, "Failed to recover NFC Service.");
1864                 return false;
1865             }
1866             try {
1867                 return sService.setNfcSecure(enable);
1868             } catch (RemoteException ee) {
1869                 Log.e(TAG, "Failed to recover NFC Service.");
1870             }
1871             return false;
1872         }
1873     }
1874 
1875     /**
1876      * Checks if the device supports Secure NFC functionality.
1877      *
1878      * @return True if device supports Secure NFC, false otherwise
1879      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1880      */
isSecureNfcSupported()1881     public boolean isSecureNfcSupported() {
1882         if (!sHasNfcFeature) {
1883             throw new UnsupportedOperationException();
1884         }
1885         try {
1886             return sService.deviceSupportsNfcSecure();
1887         } catch (RemoteException e) {
1888             attemptDeadServiceRecovery(e);
1889             // Try one more time
1890             if (sService == null) {
1891                 Log.e(TAG, "Failed to recover NFC Service.");
1892                 return false;
1893             }
1894             try {
1895                 return sService.deviceSupportsNfcSecure();
1896             } catch (RemoteException ee) {
1897                 Log.e(TAG, "Failed to recover NFC Service.");
1898             }
1899             return false;
1900         }
1901     }
1902 
1903     /**
1904      * Checks Secure NFC feature is enabled.
1905      *
1906      * @return True if Secure NFC is enabled, false otherwise
1907      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1908      * @throws UnsupportedOperationException if device doesn't support
1909      *         Secure NFC functionality. {@link #isSecureNfcSupported}
1910      */
isSecureNfcEnabled()1911     public boolean isSecureNfcEnabled() {
1912         if (!sHasNfcFeature) {
1913             throw new UnsupportedOperationException();
1914         }
1915         try {
1916             return sService.isNfcSecureEnabled();
1917         } catch (RemoteException e) {
1918             attemptDeadServiceRecovery(e);
1919             // Try one more time
1920             if (sService == null) {
1921                 Log.e(TAG, "Failed to recover NFC Service.");
1922                 return false;
1923             }
1924             try {
1925                 return sService.isNfcSecureEnabled();
1926             } catch (RemoteException ee) {
1927                 Log.e(TAG, "Failed to recover NFC Service.");
1928             }
1929             return false;
1930         }
1931     }
1932 
1933     /**
1934      * Enable NDEF Push feature.
1935      * <p>This API is for the Settings application.
1936      * @hide
1937      */
1938     @SystemApi
1939     @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
enableNdefPush()1940     public boolean enableNdefPush() {
1941         if (!sHasNfcFeature) {
1942             throw new UnsupportedOperationException();
1943         }
1944         try {
1945             return sService.enableNdefPush();
1946         } catch (RemoteException e) {
1947             attemptDeadServiceRecovery(e);
1948             return false;
1949         }
1950     }
1951 
1952     /**
1953      * Disable NDEF Push feature.
1954      * <p>This API is for the Settings application.
1955      * @hide
1956      */
1957     @SystemApi
1958     @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
disableNdefPush()1959     public boolean disableNdefPush() {
1960         synchronized (NfcAdapter.class) {
1961             if (!sHasNfcFeature) {
1962                 throw new UnsupportedOperationException();
1963             }
1964         }
1965         try {
1966             return sService.disableNdefPush();
1967         } catch (RemoteException e) {
1968             attemptDeadServiceRecovery(e);
1969             return false;
1970         }
1971     }
1972 
1973     /**
1974      * Return true if the NDEF Push (Android Beam) feature is enabled.
1975      * <p>This function will return true only if both NFC is enabled, and the
1976      * NDEF Push feature is enabled.
1977      * <p>Note that if NFC is enabled but NDEF Push is disabled then this
1978      * device can still <i>receive</i> NDEF messages, it just cannot send them.
1979      * <p>Applications cannot directly toggle the NDEF Push feature, but they
1980      * can request Settings UI allowing the user to toggle NDEF Push using
1981      * <code>startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))</code>
1982      * <p>Example usage in an Activity that requires NDEF Push:
1983      * <p><pre>
1984      * protected void onResume() {
1985      *     super.onResume();
1986      *     if (!nfcAdapter.isEnabled()) {
1987      *         startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
1988      *     } else if (!nfcAdapter.isNdefPushEnabled()) {
1989      *         startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
1990      *     }
1991      * }</pre>
1992      *
1993      * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS
1994      * @return true if NDEF Push feature is enabled
1995      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1996      * @deprecated this feature is deprecated. File sharing can work using other technology like
1997      * Bluetooth.
1998      */
1999     @java.lang.Deprecated
2000 
isNdefPushEnabled()2001     public boolean isNdefPushEnabled() {
2002         synchronized (NfcAdapter.class) {
2003             if (!sHasNfcFeature) {
2004                 throw new UnsupportedOperationException();
2005             }
2006             if (!sHasBeamFeature) {
2007                 return false;
2008             }
2009         }
2010         try {
2011             return sService.isNdefPushEnabled();
2012         } catch (RemoteException e) {
2013             attemptDeadServiceRecovery(e);
2014             return false;
2015         }
2016     }
2017 
2018     /**
2019      * Signals that you are no longer interested in communicating with an NFC tag
2020      * for as long as it remains in range.
2021      *
2022      * All future attempted communication to this tag will fail with {@link IOException}.
2023      * The NFC controller will be put in a low-power polling mode, allowing the device
2024      * to save power in cases where it's "attached" to a tag all the time (e.g. a tag in
2025      * car dock).
2026      *
2027      * Additionally the debounceMs parameter allows you to specify for how long the tag needs
2028      * to have gone out of range, before it will be dispatched again.
2029      *
2030      * Note: the NFC controller typically polls at a pretty slow interval (100 - 500 ms).
2031      * This means that if the tag repeatedly goes in and out of range (for example, in
2032      * case of a flaky connection), and the controller happens to poll every time the
2033      * tag is out of range, it *will* re-dispatch the tag after debounceMs, despite the tag
2034      * having been "in range" during the interval.
2035      *
2036      * Note 2: if a tag with another UID is detected after this API is called, its effect
2037      * will be cancelled; if this tag shows up before the amount of time specified in
2038      * debounceMs, it will be dispatched again.
2039      *
2040      * Note 3: some tags have a random UID, in which case this API won't work reliably.
2041      *
2042      * @param tag        the {@link android.nfc.Tag Tag} to ignore.
2043      * @param debounceMs minimum amount of time the tag needs to be out of range before being
2044      *                   dispatched again.
2045      * @param tagRemovedListener listener to be called when the tag is removed from the field.
2046      *                           Note that this will only be called if the tag has been out of range
2047      *                           for at least debounceMs, or if another tag came into range before
2048      *                           debounceMs. May be null in case you don't want a callback.
2049      * @param handler the {@link android.os.Handler Handler} that will be used for delivering
2050      *                the callback. if the handler is null, then the thread used for delivering
2051      *                the callback is unspecified.
2052      * @return false if the tag couldn't be found (or has already gone out of range), true otherwise
2053      */
ignore(final Tag tag, int debounceMs, final OnTagRemovedListener tagRemovedListener, final Handler handler)2054     public boolean ignore(final Tag tag, int debounceMs,
2055                           final OnTagRemovedListener tagRemovedListener, final Handler handler) {
2056         ITagRemovedCallback.Stub iListener = null;
2057         if (tagRemovedListener != null) {
2058             iListener = new ITagRemovedCallback.Stub() {
2059                 @Override
2060                 public void onTagRemoved() throws RemoteException {
2061                     if (handler != null) {
2062                         handler.post(new Runnable() {
2063                             @Override
2064                             public void run() {
2065                                 tagRemovedListener.onTagRemoved();
2066                             }
2067                         });
2068                     } else {
2069                         tagRemovedListener.onTagRemoved();
2070                     }
2071                     synchronized (mLock) {
2072                         mTagRemovedListener = null;
2073                     }
2074                 }
2075             };
2076         }
2077         synchronized (mLock) {
2078             mTagRemovedListener = iListener;
2079         }
2080         try {
2081             return sService.ignore(tag.getServiceHandle(), debounceMs, iListener);
2082         } catch (RemoteException e) {
2083             return false;
2084         }
2085     }
2086 
2087     /**
2088      * Inject a mock NFC tag.<p>
2089      * Used for testing purposes.
2090      * <p class="note">Requires the
2091      * {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
2092      * @hide
2093      */
dispatch(Tag tag)2094     public void dispatch(Tag tag) {
2095         if (tag == null) {
2096             throw new NullPointerException("tag cannot be null");
2097         }
2098         try {
2099             sService.dispatch(tag);
2100         } catch (RemoteException e) {
2101             attemptDeadServiceRecovery(e);
2102         }
2103     }
2104 
2105     /**
2106      * @hide
2107      */
setP2pModes(int initiatorModes, int targetModes)2108     public void setP2pModes(int initiatorModes, int targetModes) {
2109         try {
2110             sService.setP2pModes(initiatorModes, targetModes);
2111         } catch (RemoteException e) {
2112             attemptDeadServiceRecovery(e);
2113         }
2114     }
2115 
2116     /**
2117      * Registers a new NFC unlock handler with the NFC service.
2118      *
2119      * <p />NFC unlock handlers are intended to unlock the keyguard in the presence of a trusted
2120      * NFC device. The handler should return true if it successfully authenticates the user and
2121      * unlocks the keyguard.
2122      *
2123      * <p /> The parameter {@code tagTechnologies} determines which Tag technologies will be polled for
2124      * at the lockscreen. Polling for less tag technologies reduces latency, and so it is
2125      * strongly recommended to only provide the Tag technologies that the handler is expected to
2126      * receive. There must be at least one tag technology provided, otherwise the unlock handler
2127      * is ignored.
2128      *
2129      * @hide
2130      */
2131     @SystemApi
2132     @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
addNfcUnlockHandler(final NfcUnlockHandler unlockHandler, String[] tagTechnologies)2133     public boolean addNfcUnlockHandler(final NfcUnlockHandler unlockHandler,
2134                                        String[] tagTechnologies) {
2135         synchronized (NfcAdapter.class) {
2136             if (!sHasNfcFeature) {
2137                 throw new UnsupportedOperationException();
2138             }
2139         }
2140         // If there are no tag technologies, don't bother adding unlock handler
2141         if (tagTechnologies.length == 0) {
2142             return false;
2143         }
2144 
2145         try {
2146             synchronized (mLock) {
2147                 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
2148                     // update the tag technologies
2149                     sService.removeNfcUnlockHandler(mNfcUnlockHandlers.get(unlockHandler));
2150                     mNfcUnlockHandlers.remove(unlockHandler);
2151                 }
2152 
2153                 INfcUnlockHandler.Stub iHandler = new INfcUnlockHandler.Stub() {
2154                     @Override
2155                     public boolean onUnlockAttempted(Tag tag) throws RemoteException {
2156                         return unlockHandler.onUnlockAttempted(tag);
2157                     }
2158                 };
2159 
2160                 sService.addNfcUnlockHandler(iHandler,
2161                         Tag.getTechCodesFromStrings(tagTechnologies));
2162                 mNfcUnlockHandlers.put(unlockHandler, iHandler);
2163             }
2164         } catch (RemoteException e) {
2165             attemptDeadServiceRecovery(e);
2166             return false;
2167         } catch (IllegalArgumentException e) {
2168             Log.e(TAG, "Unable to register LockscreenDispatch", e);
2169             return false;
2170         }
2171 
2172         return true;
2173     }
2174 
2175     /**
2176      * Removes a previously registered unlock handler. Also removes the tag technologies
2177      * associated with the removed unlock handler.
2178      *
2179      * @hide
2180      */
2181     @SystemApi
2182     @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
removeNfcUnlockHandler(NfcUnlockHandler unlockHandler)2183     public boolean removeNfcUnlockHandler(NfcUnlockHandler unlockHandler) {
2184         synchronized (NfcAdapter.class) {
2185             if (!sHasNfcFeature) {
2186                 throw new UnsupportedOperationException();
2187             }
2188         }
2189         try {
2190             synchronized (mLock) {
2191                 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
2192                     sService.removeNfcUnlockHandler(mNfcUnlockHandlers.remove(unlockHandler));
2193                 }
2194 
2195                 return true;
2196             }
2197         } catch (RemoteException e) {
2198             attemptDeadServiceRecovery(e);
2199             return false;
2200         }
2201     }
2202 
2203     /**
2204      * @hide
2205      */
2206     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
getNfcAdapterExtrasInterface()2207     public INfcAdapterExtras getNfcAdapterExtrasInterface() {
2208         if (mContext == null) {
2209             throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
2210                     + " NFC extras APIs");
2211         }
2212         try {
2213             return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
2214         } catch (RemoteException e) {
2215             attemptDeadServiceRecovery(e);
2216             // Try one more time
2217             if (sService == null) {
2218                 Log.e(TAG, "Failed to recover NFC Service.");
2219                 return null;
2220             }
2221             try {
2222                 return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
2223             } catch (RemoteException ee) {
2224                 Log.e(TAG, "Failed to recover NFC Service.");
2225             }
2226             return null;
2227         }
2228     }
2229 
enforceResumed(Activity activity)2230     void enforceResumed(Activity activity) {
2231         if (!activity.isResumed()) {
2232             throw new IllegalStateException("API cannot be called while activity is paused");
2233         }
2234     }
2235 
getSdkVersion()2236     int getSdkVersion() {
2237         if (mContext == null) {
2238             return android.os.Build.VERSION_CODES.GINGERBREAD; // best guess
2239         } else {
2240             return mContext.getApplicationInfo().targetSdkVersion;
2241         }
2242     }
2243 
2244     /**
2245      * Sets NFC controller always on feature.
2246      * <p>This API is for the NFCC internal state management. It allows to discriminate
2247      * the controller function from the NFC function by keeping the NFC controller on without
2248      * any NFC RF enabled if necessary.
2249      * <p>This call is asynchronous. Register a listener {@link #ControllerAlwaysOnListener}
2250      * by {@link #registerControllerAlwaysOnListener} to find out when the operation is
2251      * complete.
2252      * <p>If this returns true, then either NFCC always on state has been set based on the value,
2253      * or a {@link ControllerAlwaysOnListener#onControllerAlwaysOnChanged(boolean)} will be invoked
2254      * to indicate the state change.
2255      * If this returns false, then there is some problem that prevents an attempt to turn NFCC
2256      * always on.
2257      * @param value if true the NFCC will be kept on (with no RF enabled if NFC adapter is
2258      * disabled), if false the NFCC will follow completely the Nfc adapter state.
2259      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
2260      * @return void
2261      * @hide
2262      */
2263     @SystemApi
2264     @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON)
setControllerAlwaysOn(boolean value)2265     public boolean setControllerAlwaysOn(boolean value) {
2266         if (!sHasNfcFeature) {
2267             throw new UnsupportedOperationException();
2268         }
2269         try {
2270             return sService.setControllerAlwaysOn(value);
2271         } catch (RemoteException e) {
2272             attemptDeadServiceRecovery(e);
2273             // Try one more time
2274             if (sService == null) {
2275                 Log.e(TAG, "Failed to recover NFC Service.");
2276                 return false;
2277             }
2278             try {
2279                 return sService.setControllerAlwaysOn(value);
2280             } catch (RemoteException ee) {
2281                 Log.e(TAG, "Failed to recover NFC Service.");
2282             }
2283             return false;
2284         }
2285     }
2286 
2287     /**
2288      * Checks NFC controller always on feature is enabled.
2289      *
2290      * @return True if NFC controller always on is enabled, false otherwise
2291      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
2292      * @hide
2293      */
2294     @SystemApi
2295     @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON)
isControllerAlwaysOn()2296     public boolean isControllerAlwaysOn() {
2297         try {
2298             return sService.isControllerAlwaysOn();
2299         } catch (RemoteException e) {
2300             attemptDeadServiceRecovery(e);
2301             // Try one more time
2302             if (sService == null) {
2303                 Log.e(TAG, "Failed to recover NFC Service.");
2304                 return false;
2305             }
2306             try {
2307                 return sService.isControllerAlwaysOn();
2308             } catch (RemoteException ee) {
2309                 Log.e(TAG, "Failed to recover NFC Service.");
2310             }
2311             return false;
2312         }
2313     }
2314 
2315     /**
2316      * Checks if the device supports NFC controller always on functionality.
2317      *
2318      * @return True if device supports NFC controller always on, false otherwise
2319      * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
2320      * @hide
2321      */
2322     @SystemApi
2323     @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON)
isControllerAlwaysOnSupported()2324     public boolean isControllerAlwaysOnSupported() {
2325         if (!sHasNfcFeature) {
2326             throw new UnsupportedOperationException();
2327         }
2328         try {
2329             return sService.isControllerAlwaysOnSupported();
2330         } catch (RemoteException e) {
2331             attemptDeadServiceRecovery(e);
2332             // Try one more time
2333             if (sService == null) {
2334                 Log.e(TAG, "Failed to recover NFC Service.");
2335                 return false;
2336             }
2337             try {
2338                 return sService.isControllerAlwaysOnSupported();
2339             } catch (RemoteException ee) {
2340                 Log.e(TAG, "Failed to recover NFC Service.");
2341             }
2342             return false;
2343         }
2344     }
2345 
2346     /**
2347      * Register a {@link ControllerAlwaysOnListener} to listen for NFC controller always on
2348      * state changes
2349      * <p>The provided listener will be invoked by the given {@link Executor}.
2350      *
2351      * @param executor an {@link Executor} to execute given listener
2352      * @param listener user implementation of the {@link ControllerAlwaysOnListener}
2353      * @hide
2354      */
2355     @SystemApi
2356     @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON)
registerControllerAlwaysOnListener( @onNull @allbackExecutor Executor executor, @NonNull ControllerAlwaysOnListener listener)2357     public void registerControllerAlwaysOnListener(
2358             @NonNull @CallbackExecutor Executor executor,
2359             @NonNull ControllerAlwaysOnListener listener) {
2360         mControllerAlwaysOnListener.register(executor, listener);
2361     }
2362 
2363     /**
2364      * Unregister the specified {@link ControllerAlwaysOnListener}
2365      * <p>The same {@link ControllerAlwaysOnListener} object used when calling
2366      * {@link #registerControllerAlwaysOnListener(Executor, ControllerAlwaysOnListener)}
2367      * must be used.
2368      *
2369      * <p>Listeners are automatically unregistered when application process goes away
2370      *
2371      * @param listener user implementation of the {@link ControllerAlwaysOnListener}
2372      * @hide
2373      */
2374     @SystemApi
2375     @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON)
unregisterControllerAlwaysOnListener( @onNull ControllerAlwaysOnListener listener)2376     public void unregisterControllerAlwaysOnListener(
2377             @NonNull ControllerAlwaysOnListener listener) {
2378         mControllerAlwaysOnListener.unregister(listener);
2379     }
2380 }
2381