blob: 21fed48189ebe6c5c681e2b7a091beb1e596a337 [file] [log] [blame]
Nick Pelly590b73b2010-10-12 13:00:50 -07001/*
Daniel Tomas90245642010-11-17 10:07:52 +01002 * 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.
Nick Pelly590b73b2010-10-12 13:00:50 -070015 */
16
17package android.nfc;
18
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060019import android.annotation.RequiresPermission;
Nick Pelly590b73b2010-10-12 13:00:50 -070020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
Martijn Coenen29c2e372014-07-29 13:54:21 -070022import android.annotation.SystemApi;
Mathew Inwood57069dc2018-07-31 15:33:20 +010023import android.annotation.UnsupportedAppUsage;
Jeff Hamilton52d32032011-01-08 15:31:26 -060024import android.app.Activity;
Nick Pellyfdf90862010-10-18 11:19:41 -070025import android.app.ActivityThread;
Jeff Hamilton52d32032011-01-08 15:31:26 -060026import android.app.OnActivityPausedListener;
27import android.app.PendingIntent;
Nick Pelly50b4d8f2010-12-07 22:40:28 -080028import android.content.Context;
Jeff Hamilton52d32032011-01-08 15:31:26 -060029import android.content.IntentFilter;
Nick Pellyfdf90862010-10-18 11:19:41 -070030import android.content.pm.IPackageManager;
31import android.content.pm.PackageManager;
Nick Pelly1d7e9062012-04-03 17:46:00 -070032import android.net.Uri;
Jeff Hamilton28319c02011-02-09 17:26:47 +090033import android.nfc.tech.MifareClassic;
34import android.nfc.tech.Ndef;
35import android.nfc.tech.NfcA;
36import android.nfc.tech.NfcF;
Martijn Coenen5b1e0322013-09-02 20:38:47 -070037import android.os.Bundle;
Martijn Coenend55def82016-04-14 15:19:31 +020038import android.os.Handler;
Nick Pelly590b73b2010-10-12 13:00:50 -070039import android.os.IBinder;
40import android.os.RemoteException;
41import android.os.ServiceManager;
42import android.util.Log;
43
Martijn Coenend55def82016-04-14 15:19:31 +020044import java.io.IOException;
Love Khanna755a8fa2017-05-17 15:52:16 +053045import java.util.HashMap;
Martijn Coenend55def82016-04-14 15:19:31 +020046
Nick Pelly590b73b2010-10-12 13:00:50 -070047/**
Nick Pelly74fe6c62011-02-02 22:37:40 -080048 * Represents the local NFC adapter.
Nick Pelly590b73b2010-10-12 13:00:50 -070049 * <p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -080050 * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC
51 * adapter for this Android device.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080052 *
53 * <div class="special reference">
54 * <h3>Developer Guides</h3>
55 * <p>For more information about using NFC, read the
56 * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p>
Scott Mainb38ad762013-10-17 11:57:06 -070057 * <p>To perform basic file sharing between devices, read
58 * <a href="{@docRoot}training/beam-files/index.html">Sharing Files with NFC</a>.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080059 * </div>
Nick Pelly590b73b2010-10-12 13:00:50 -070060 */
61public final class NfcAdapter {
Nick Pellyc84c89a2011-08-22 22:27:11 -070062 static final String TAG = "NFC";
Nick Pelly50b4d8f2010-12-07 22:40:28 -080063
Nick Pelly590b73b2010-10-12 13:00:50 -070064 /**
Jeff Hamilton641dd622010-12-02 09:16:22 -060065 * Intent to start an activity when a tag with NDEF payload is discovered.
Jeff Hamilton641dd622010-12-02 09:16:22 -060066 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090067 * <p>The system inspects the first {@link NdefRecord} in the first {@link NdefMessage} and
68 * looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the
69 * intent will contain the URI in its data field. If a MIME record is found the intent will
70 * contain the MIME type in its type field. This allows activities to register
71 * {@link IntentFilter}s targeting specific content on tags. Activities should register the
72 * most specific intent filters possible to avoid the activity chooser dialog, which can
73 * disrupt the interaction with the tag as the user interacts with the screen.
74 *
75 * <p>If the tag has an NDEF payload this intent is started before
76 * {@link #ACTION_TECH_DISCOVERED}. If any activities respond to this intent neither
Nick Pellyf003e262011-01-31 23:27:37 -080077 * {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started.
Nick Pellyc97a5522012-01-05 15:13:01 +110078 *
79 * <p>The MIME type or data URI of this intent are normalized before dispatch -
80 * so that MIME, URI scheme and URI host are always lower-case.
Jeff Hamilton641dd622010-12-02 09:16:22 -060081 */
82 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
83 public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED";
84
85 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +090086 * Intent to start an activity when a tag is discovered and activities are registered for the
87 * specific technologies on the tag.
Jeff Hamilton641dd622010-12-02 09:16:22 -060088 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090089 * <p>To receive this intent an activity must include an intent filter
90 * for this action and specify the desired tech types in a
91 * manifest <code>meta-data</code> entry. Here is an example manfiest entry:
92 * <pre>
Scott Main97e0a1c2012-06-25 11:22:10 -070093 * &lt;activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"&gt;
94 * &lt;!-- Add a technology filter --&gt;
95 * &lt;intent-filter&gt;
96 * &lt;action android:name="android.nfc.action.TECH_DISCOVERED" /&gt;
97 * &lt;/intent-filter&gt;
Jeff Hamilton641dd622010-12-02 09:16:22 -060098 *
Scott Main97e0a1c2012-06-25 11:22:10 -070099 * &lt;meta-data android:name="android.nfc.action.TECH_DISCOVERED"
100 * android:resource="@xml/filter_nfc"
101 * /&gt;
102 * &lt;/activity&gt;</pre>
Jeff Hamilton28319c02011-02-09 17:26:47 +0900103 *
104 * <p>The meta-data XML file should contain one or more <code>tech-list</code> entries
105 * each consisting or one or more <code>tech</code> entries. The <code>tech</code> entries refer
106 * to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA".
107 *
108 * <p>A tag matches if any of the
109 * <code>tech-list</code> sets is a subset of {@link Tag#getTechList() Tag.getTechList()}. Each
110 * of the <code>tech-list</code>s is considered independently and the
111 * activity is considered a match is any single <code>tech-list</code> matches the tag that was
112 * discovered. This provides AND and OR semantics for filtering desired techs. Here is an
113 * example that will match any tag using {@link NfcF} or any tag using {@link NfcA},
114 * {@link MifareClassic}, and {@link Ndef}:
115 *
116 * <pre>
117 * &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
118 * &lt;!-- capture anything using NfcF --&gt;
119 * &lt;tech-list&gt;
120 * &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
121 * &lt;/tech-list&gt;
122 *
123 * &lt;!-- OR --&gt;
124 *
125 * &lt;!-- capture all MIFARE Classics with NDEF payloads --&gt;
126 * &lt;tech-list&gt;
127 * &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
128 * &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
129 * &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
130 * &lt;/tech-list&gt;
Scott Main97e0a1c2012-06-25 11:22:10 -0700131 * &lt;/resources&gt;</pre>
Jeff Hamilton28319c02011-02-09 17:26:47 +0900132 *
133 * <p>This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before
134 * {@link #ACTION_TAG_DISCOVERED}. If any activities respond to {@link #ACTION_NDEF_DISCOVERED}
135 * this intent will not be started. If any activities respond to this intent
136 * {@link #ACTION_TAG_DISCOVERED} will not be started.
Jeff Hamilton641dd622010-12-02 09:16:22 -0600137 */
138 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Nick Pellyf003e262011-01-31 23:27:37 -0800139 public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED";
Jeff Hamilton641dd622010-12-02 09:16:22 -0600140
141 /**
Nick Pelly11b075e2010-10-28 13:39:37 -0700142 * Intent to start an activity when a tag is discovered.
Jeff Hamilton28319c02011-02-09 17:26:47 +0900143 *
144 * <p>This intent will not be started when a tag is discovered if any activities respond to
Jason parks01425362011-05-24 02:57:37 -0700145 * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag.
Nick Pelly590b73b2010-10-12 13:00:50 -0700146 */
147 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
148 public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED";
149
150 /**
Ruchi Kandoia0771612018-02-02 09:14:39 -0800151 * Broadcast Action: Intent to notify an application that an transaction event has occurred
152 * on the Secure Element.
153 *
154 * <p>This intent will only be sent if the application has requested permission for
155 * {@link android.Manifest.permission#NFC_TRANSACTION_EVENT} and if the application has the
156 * necessary access to Secure Element which witnessed the particular event.
157 */
158 @RequiresPermission(android.Manifest.permission.NFC_TRANSACTION_EVENT)
159 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
160 public static final String ACTION_TRANSACTION_DETECTED =
161 "android.nfc.action.TRANSACTION_DETECTED";
162
163 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600164 * Broadcast to only the activity that handles ACTION_TAG_DISCOVERED
165 * @hide
166 */
167 public static final String ACTION_TAG_LEFT_FIELD = "android.nfc.action.TAG_LOST";
168
169 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900170 * Mandatory extra containing the {@link Tag} that was discovered for the
171 * {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
172 * {@link #ACTION_TAG_DISCOVERED} intents.
Nick Pelly590b73b2010-10-12 13:00:50 -0700173 */
174 public static final String EXTRA_TAG = "android.nfc.extra.TAG";
175
176 /**
Nick Pellyc97a5522012-01-05 15:13:01 +1100177 * Extra containing an array of {@link NdefMessage} present on the discovered tag.<p>
178 * This extra is mandatory for {@link #ACTION_NDEF_DISCOVERED} intents,
179 * and optional for {@link #ACTION_TECH_DISCOVERED}, and
180 * {@link #ACTION_TAG_DISCOVERED} intents.<p>
181 * When this extra is present there will always be at least one
182 * {@link NdefMessage} element. Most NDEF tags have only one NDEF message,
183 * but we use an array for future compatibility.
Nick Pelly11b075e2010-10-28 13:39:37 -0700184 */
185 public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES";
186
187 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900188 * Optional extra containing a byte array containing the ID of the discovered tag for
189 * the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
190 * {@link #ACTION_TAG_DISCOVERED} intents.
Nick Pelly11b075e2010-10-28 13:39:37 -0700191 */
192 public static final String EXTRA_ID = "android.nfc.extra.ID";
193
194 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700195 * Broadcast Action: The state of the local NFC adapter has been
196 * changed.
197 * <p>For example, NFC has been turned on or off.
Martijn Coenen4ba5eaf2013-01-08 12:49:47 -0800198 * <p>Always contains the extra field {@link #EXTRA_ADAPTER_STATE}
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700199 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700200 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
201 public static final String ACTION_ADAPTER_STATE_CHANGED =
202 "android.nfc.action.ADAPTER_STATE_CHANGED";
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700203
204 /**
Martijn Coenen4ba5eaf2013-01-08 12:49:47 -0800205 * Used as an int extra field in {@link #ACTION_ADAPTER_STATE_CHANGED}
Nick Pelly8d32a012011-08-09 07:03:49 -0700206 * intents to request the current power state. Possible values are:
207 * {@link #STATE_OFF},
208 * {@link #STATE_TURNING_ON},
209 * {@link #STATE_ON},
210 * {@link #STATE_TURNING_OFF},
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700211 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700212 public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE";
213
Ruchi Kandoia0771612018-02-02 09:14:39 -0800214 /**
215 * Mandatory byte[] extra field in {@link #ACTION_TRANSACTION_DETECTED}
216 */
217 public static final String EXTRA_AID = "android.nfc.extra.AID";
218
219 /**
220 * Optional byte[] extra field in {@link #ACTION_TRANSACTION_DETECTED}
221 */
222 public static final String EXTRA_DATA = "android.nfc.extra.DATA";
223
224 /**
225 * Mandatory String extra field in {@link #ACTION_TRANSACTION_DETECTED}
226 * Indicates the Secure Element on which the transaction occurred.
227 * eSE1...eSEn for Embedded Secure Elements, SIM1...SIMn for UICC, etc.
228 */
Ruchi Kandoi9a33aefd2018-03-15 15:15:18 -0700229 public static final String EXTRA_SECURE_ELEMENT_NAME = "android.nfc.extra.SECURE_ELEMENT_NAME";
Ruchi Kandoia0771612018-02-02 09:14:39 -0800230
Nick Pelly8d32a012011-08-09 07:03:49 -0700231 public static final int STATE_OFF = 1;
Nick Pelly8d32a012011-08-09 07:03:49 -0700232 public static final int STATE_TURNING_ON = 2;
Nick Pelly8d32a012011-08-09 07:03:49 -0700233 public static final int STATE_ON = 3;
Nick Pelly8d32a012011-08-09 07:03:49 -0700234 public static final int STATE_TURNING_OFF = 4;
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700235
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700236 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700237 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700238 * <p>
239 * Setting this flag enables polling for Nfc-A technology.
240 */
241 public static final int FLAG_READER_NFC_A = 0x1;
242
243 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700244 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700245 * <p>
246 * Setting this flag enables polling for Nfc-B technology.
247 */
248 public static final int FLAG_READER_NFC_B = 0x2;
249
250 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700251 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700252 * <p>
253 * Setting this flag enables polling for Nfc-F technology.
254 */
255 public static final int FLAG_READER_NFC_F = 0x4;
256
257 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700258 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700259 * <p>
260 * Setting this flag enables polling for Nfc-V (ISO15693) technology.
261 */
262 public static final int FLAG_READER_NFC_V = 0x8;
263
264 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700265 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700266 * <p>
Martijn Coenenb523bfa2013-09-12 13:44:39 +0200267 * Setting this flag enables polling for NfcBarcode technology.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700268 */
Martijn Coenenb523bfa2013-09-12 13:44:39 +0200269 public static final int FLAG_READER_NFC_BARCODE = 0x10;
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700270
271 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700272 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700273 * <p>
274 * Setting this flag allows the caller to prevent the
275 * platform from performing an NDEF check on the tags it
276 * finds.
277 */
278 public static final int FLAG_READER_SKIP_NDEF_CHECK = 0x80;
279
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700280 /**
281 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
282 * <p>
283 * Setting this flag allows the caller to prevent the
284 * platform from playing sounds when it discovers a tag.
285 */
286 public static final int FLAG_READER_NO_PLATFORM_SOUNDS = 0x100;
287
288 /**
289 * Int Extra for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
290 * <p>
291 * Setting this integer extra allows the calling application to specify
292 * the delay that the platform will use for performing presence checks
293 * on any discovered tag.
294 */
295 public static final String EXTRA_READER_PRESENCE_CHECK_DELAY = "presence";
296
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700297 /** @hide */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700298 @SystemApi
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800299 public static final int FLAG_NDEF_PUSH_NO_CONFIRM = 0x1;
300
301 /** @hide */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700302 public static final String ACTION_HANDOVER_TRANSFER_STARTED =
303 "android.nfc.action.HANDOVER_TRANSFER_STARTED";
304
305 /** @hide */
306 public static final String ACTION_HANDOVER_TRANSFER_DONE =
307 "android.nfc.action.HANDOVER_TRANSFER_DONE";
308
309 /** @hide */
310 public static final String EXTRA_HANDOVER_TRANSFER_STATUS =
311 "android.nfc.extra.HANDOVER_TRANSFER_STATUS";
312
313 /** @hide */
314 public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
315 /** @hide */
316 public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;
317
318 /** @hide */
319 public static final String EXTRA_HANDOVER_TRANSFER_URI =
320 "android.nfc.extra.HANDOVER_TRANSFER_URI";
321
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800322 // Guarded by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700323 static boolean sIsInitialized = false;
Steve Elliott04141ea2016-04-15 15:09:43 -0400324 static boolean sHasNfcFeature;
Nick Pelly590b73b2010-10-12 13:00:50 -0700325
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800326 // Final after first constructor, except for
327 // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
328 // recovery
Mathew Inwood57069dc2018-07-31 15:33:20 +0100329 @UnsupportedAppUsage
Nick Pellyc84c89a2011-08-22 22:27:11 -0700330 static INfcAdapter sService;
331 static INfcTag sTagService;
Martijn Coenena7397882013-07-30 20:07:47 -0700332 static INfcCardEmulation sCardEmulationService;
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900333 static INfcFCardEmulation sNfcFCardEmulationService;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700334
335 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600336 * The NfcAdapter object for each application context.
337 * There is a 1-1 relationship between application context and
338 * NfcAdapter object.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700339 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600340 static HashMap<Context, NfcAdapter> sNfcAdapters = new HashMap(); //guard by NfcAdapter.class
341
342 /**
343 * NfcAdapter used with a null context. This ctor was deprecated but we have
344 * to support it for backwards compatibility. New methods that require context
345 * might throw when called on the null-context NfcAdapter.
346 */
347 static NfcAdapter sNullContextNfcAdapter; // protected by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700348
349 final NfcActivityManager mNfcActivityManager;
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600350 final Context mContext;
Andres Moralesf9a97942014-08-14 15:43:30 -0700351 final HashMap<NfcUnlockHandler, INfcUnlockHandler> mNfcUnlockHandlers;
Andres Morales11d2e532014-07-23 12:39:55 -0700352 final Object mLock;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700353
Martijn Coenend55def82016-04-14 15:19:31 +0200354 ITagRemovedCallback mTagRemovedListener; // protected by mLock
355
Nick Pellyc84c89a2011-08-22 22:27:11 -0700356 /**
Scott Mainb38ad762013-10-17 11:57:06 -0700357 * A callback to be invoked when the system finds a tag while the foreground activity is
358 * operating in reader mode.
359 * <p>Register your {@code ReaderCallback} implementation with {@link
360 * NfcAdapter#enableReaderMode} and disable it with {@link
361 * NfcAdapter#disableReaderMode}.
362 * @see NfcAdapter#enableReaderMode
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700363 */
364 public interface ReaderCallback {
365 public void onTagDiscovered(Tag tag);
366 }
367
368 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700369 * A callback to be invoked when the system successfully delivers your {@link NdefMessage}
370 * to another device.
371 * @see #setOnNdefPushCompleteCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700372 */
373 public interface OnNdefPushCompleteCallback {
374 /**
375 * Called on successful NDEF push.
376 *
377 * <p>This callback is usually made on a binder thread (not the UI thread).
378 *
379 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
Scott Main2d68a6b2011-09-26 22:59:38 -0700380 * @see #setNdefPushMessageCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700381 */
382 public void onNdefPushComplete(NfcEvent event);
383 }
384
385 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700386 * A callback to be invoked when another NFC device capable of NDEF push (Android Beam)
387 * is within range.
388 * <p>Implement this interface and pass it to {@link
389 * NfcAdapter#setNdefPushMessageCallback setNdefPushMessageCallback()} in order to create an
390 * {@link NdefMessage} at the moment that another device is within range for NFC. Using this
391 * callback allows you to create a message with data that might vary based on the
392 * content currently visible to the user. Alternatively, you can call {@link
393 * #setNdefPushMessage setNdefPushMessage()} if the {@link NdefMessage} always contains the
394 * same data.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700395 */
396 public interface CreateNdefMessageCallback {
397 /**
398 * Called to provide a {@link NdefMessage} to push.
399 *
400 * <p>This callback is usually made on a binder thread (not the UI thread).
401 *
402 * <p>Called when this device is in range of another device
403 * that might support NDEF push. It allows the application to
404 * create the NDEF message only when it is required.
405 *
406 * <p>NDEF push cannot occur until this method returns, so do not
407 * block for too long.
408 *
409 * <p>The Android operating system will usually show a system UI
410 * on top of your activity during this time, so do not try to request
411 * input from the user to complete the callback, or provide custom NDEF
412 * push UI. The user probably will not see it.
413 *
414 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
415 * @return NDEF message to push, or null to not provide a message
416 */
417 public NdefMessage createNdefMessage(NfcEvent event);
418 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700419
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700420
421 // TODO javadoc
422 public interface CreateBeamUrisCallback {
423 public Uri[] createBeamUris(NfcEvent event);
424 }
425
Nick Pelly590b73b2010-10-12 13:00:50 -0700426 /**
Martijn Coenend55def82016-04-14 15:19:31 +0200427 * A callback that is invoked when a tag is removed from the field.
Martijn Coenen61367302016-05-17 09:09:15 +0200428 * @see NfcAdapter#ignore
Martijn Coenend55def82016-04-14 15:19:31 +0200429 */
430 public interface OnTagRemovedListener {
431 void onTagRemoved();
432 }
433
434 /**
Andres Morales11d2e532014-07-23 12:39:55 -0700435 * A callback to be invoked when an application has registered as a
436 * handler to unlock the device given an NFC tag at the lockscreen.
437 * @hide
438 */
439 @SystemApi
440 public interface NfcUnlockHandler {
441 /**
442 * Called at the lock screen to attempt to unlock the device with the given tag.
443 * @param tag the detected tag, to be used to unlock the device
444 * @return true if the device was successfully unlocked
445 */
446 public boolean onUnlockAttempted(Tag tag);
447 }
448
449
450 /**
Nick Pellyfdf90862010-10-18 11:19:41 -0700451 * Helper to check if this device has FEATURE_NFC, but without using
452 * a context.
453 * Equivalent to
454 * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)
455 */
456 private static boolean hasNfcFeature() {
457 IPackageManager pm = ActivityThread.getPackageManager();
458 if (pm == null) {
459 Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
460 return false;
461 }
462 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700463 return pm.hasSystemFeature(PackageManager.FEATURE_NFC, 0);
Nick Pellyfdf90862010-10-18 11:19:41 -0700464 } catch (RemoteException e) {
465 Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
466 return false;
467 }
468 }
469
Nick Pellyc84c89a2011-08-22 22:27:11 -0700470 /**
Steve Elliott04141ea2016-04-15 15:09:43 -0400471 * Helper to check if this device is NFC HCE capable, by checking for
472 * FEATURE_NFC_HOST_CARD_EMULATION and/or FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
473 * but without using a context.
474 */
475 private static boolean hasNfcHceFeature() {
476 IPackageManager pm = ActivityThread.getPackageManager();
477 if (pm == null) {
478 Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
479 return false;
480 }
481 try {
482 return pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION, 0)
483 || pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF, 0);
484 } catch (RemoteException e) {
485 Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
486 return false;
487 }
488 }
489
490 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600491 * Returns the NfcAdapter for application context,
492 * or throws if NFC is not available.
493 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700494 */
Mathew Inwood57069dc2018-07-31 15:33:20 +0100495 @UnsupportedAppUsage
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600496 public static synchronized NfcAdapter getNfcAdapter(Context context) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800497 if (!sIsInitialized) {
Steve Elliott04141ea2016-04-15 15:09:43 -0400498 sHasNfcFeature = hasNfcFeature();
499 boolean hasHceFeature = hasNfcHceFeature();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800500 /* is this device meant to have NFC */
Steve Elliott04141ea2016-04-15 15:09:43 -0400501 if (!sHasNfcFeature && !hasHceFeature) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800502 Log.v(TAG, "this device does not have NFC support");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700503 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800504 }
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800505 sService = getServiceInterface();
506 if (sService == null) {
507 Log.e(TAG, "could not retrieve NFC service");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700508 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800509 }
Steve Elliott04141ea2016-04-15 15:09:43 -0400510 if (sHasNfcFeature) {
511 try {
512 sTagService = sService.getNfcTagInterface();
513 } catch (RemoteException e) {
514 Log.e(TAG, "could not retrieve NFC Tag service");
515 throw new UnsupportedOperationException();
516 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100517 }
Steve Elliott04141ea2016-04-15 15:09:43 -0400518 if (hasHceFeature) {
519 try {
520 sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface();
521 } catch (RemoteException e) {
522 Log.e(TAG, "could not retrieve NFC-F card emulation service");
523 throw new UnsupportedOperationException();
524 }
525 try {
526 sCardEmulationService = sService.getNfcCardEmulationInterface();
527 } catch (RemoteException e) {
528 Log.e(TAG, "could not retrieve card emulation service");
529 throw new UnsupportedOperationException();
530 }
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900531 }
532
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600533 sIsInitialized = true;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800534 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600535 if (context == null) {
536 if (sNullContextNfcAdapter == null) {
537 sNullContextNfcAdapter = new NfcAdapter(null);
538 }
539 return sNullContextNfcAdapter;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700540 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600541 NfcAdapter adapter = sNfcAdapters.get(context);
542 if (adapter == null) {
543 adapter = new NfcAdapter(context);
544 sNfcAdapters.put(context, adapter);
545 }
546 return adapter;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800547 }
548
Nick Pelly6d55e132010-10-27 01:14:43 -0700549 /** get handle to NFC service interface */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800550 private static INfcAdapter getServiceInterface() {
Nick Pelly6d55e132010-10-27 01:14:43 -0700551 /* get a handle to NFC service */
552 IBinder b = ServiceManager.getService("nfc");
553 if (b == null) {
554 return null;
555 }
556 return INfcAdapter.Stub.asInterface(b);
557 }
558
Nick Pellyfdf90862010-10-18 11:19:41 -0700559 /**
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800560 * Helper to get the default NFC Adapter.
561 * <p>
562 * Most Android devices will only have one NFC Adapter (NFC Controller).
563 * <p>
564 * This helper is the equivalent of:
Scott Main97e0a1c2012-06-25 11:22:10 -0700565 * <pre>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800566 * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Scott Main97e0a1c2012-06-25 11:22:10 -0700567 * NfcAdapter adapter = manager.getDefaultAdapter();</pre>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800568 * @param context the calling application's context
569 *
570 * @return the default NFC adapter, or null if no NFC adapter exists
571 */
572 public static NfcAdapter getDefaultAdapter(Context context) {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600573 if (context == null) {
574 throw new IllegalArgumentException("context cannot be null");
575 }
576 context = context.getApplicationContext();
Nick Pellyb04cce02011-11-21 17:02:02 -0800577 if (context == null) {
578 throw new IllegalArgumentException(
579 "context not associated with any application (using a mock context?)");
580 }
581 /* use getSystemService() for consistency */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800582 NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Nick Pellya5193b242011-11-16 16:46:27 -0800583 if (manager == null) {
584 // NFC not available
585 return null;
586 }
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800587 return manager.getDefaultAdapter();
588 }
589
590 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600591 * Legacy NfcAdapter getter, always use {@link #getDefaultAdapter(Context)} instead.<p>
592 * This method was deprecated at API level 10 (Gingerbread MR1) because a context is required
593 * for many NFC API methods. Those methods will fail when called on an NfcAdapter
594 * object created from this method.<p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800595 * @deprecated use {@link #getDefaultAdapter(Context)}
Nick Pellya356bf12011-12-13 15:36:31 -0800596 * @hide
Nick Pelly590b73b2010-10-12 13:00:50 -0700597 */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800598 @Deprecated
Mathew Inwood57069dc2018-07-31 15:33:20 +0100599 @UnsupportedAppUsage
Nick Pelly590b73b2010-10-12 13:00:50 -0700600 public static NfcAdapter getDefaultAdapter() {
Nick Pellyc97a5522012-01-05 15:13:01 +1100601 // introduced in API version 9 (GB 2.3)
Nick Pellya356bf12011-12-13 15:36:31 -0800602 // deprecated in API version 10 (GB 2.3.3)
603 // removed from public API in version 16 (ICS MR2)
Nick Pellyc97a5522012-01-05 15:13:01 +1100604 // should maintain as a hidden API for binary compatibility for a little longer
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800605 Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " +
606 "NfcAdapter.getDefaultAdapter(Context) instead", new Exception());
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600607
608 return NfcAdapter.getNfcAdapter(null);
609 }
610
611 NfcAdapter(Context context) {
612 mContext = context;
613 mNfcActivityManager = new NfcActivityManager(this);
Andres Moralesf9a97942014-08-14 15:43:30 -0700614 mNfcUnlockHandlers = new HashMap<NfcUnlockHandler, INfcUnlockHandler>();
Martijn Coenend55def82016-04-14 15:19:31 +0200615 mTagRemovedListener = null;
Andres Morales11d2e532014-07-23 12:39:55 -0700616 mLock = new Object();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800617 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700618
Nick Pellyc84c89a2011-08-22 22:27:11 -0700619 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600620 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700621 */
Mathew Inwood57069dc2018-07-31 15:33:20 +0100622 @UnsupportedAppUsage
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600623 public Context getContext() {
624 return mContext;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800625 }
626
627 /**
628 * Returns the binder interface to the service.
629 * @hide
630 */
Mathew Inwood57069dc2018-07-31 15:33:20 +0100631 @UnsupportedAppUsage
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800632 public INfcAdapter getService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800633 isEnabled(); // NOP call to recover sService if it is stale
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800634 return sService;
Nick Pelly590b73b2010-10-12 13:00:50 -0700635 }
636
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600637 /**
Nick Pelly3dd6c452011-01-10 18:14:41 +1100638 * Returns the binder interface to the tag service.
639 * @hide
640 */
641 public INfcTag getTagService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800642 isEnabled(); // NOP call to recover sTagService if it is stale
Nick Pelly3dd6c452011-01-10 18:14:41 +1100643 return sTagService;
644 }
645
646 /**
Martijn Coenena7397882013-07-30 20:07:47 -0700647 * Returns the binder interface to the card emulation service.
648 * @hide
649 */
650 public INfcCardEmulation getCardEmulationService() {
651 isEnabled();
652 return sCardEmulationService;
653 }
654
655 /**
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900656 * Returns the binder interface to the NFC-F card emulation service.
657 * @hide
658 */
659 public INfcFCardEmulation getNfcFCardEmulationService() {
660 isEnabled();
661 return sNfcFCardEmulationService;
662 }
663
664 /**
Love Khanna755a8fa2017-05-17 15:52:16 +0530665 * Returns the binder interface to the NFC-DTA test interface.
666 * @hide
667 */
668 public INfcDta getNfcDtaInterface() {
669 if (mContext == null) {
670 throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
671 + " NFC extras APIs");
672 }
673 try {
674 return sService.getNfcDtaInterface(mContext.getPackageName());
675 } catch (RemoteException e) {
676 attemptDeadServiceRecovery(e);
677 return null;
678 }
679 }
680
681 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600682 * NFC service dead - attempt best effort recovery
683 * @hide
684 */
Mathew Inwood57069dc2018-07-31 15:33:20 +0100685 @UnsupportedAppUsage
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600686 public void attemptDeadServiceRecovery(Exception e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700687 Log.e(TAG, "NFC service dead - attempting to recover", e);
688 INfcAdapter service = getServiceInterface();
689 if (service == null) {
690 Log.e(TAG, "could not retrieve NFC service during service recovery");
Nick Pelly3dd6c452011-01-10 18:14:41 +1100691 // nothing more can be done now, sService is still stale, we'll hit
692 // this recovery path again later
Nick Pelly6d55e132010-10-27 01:14:43 -0700693 return;
694 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100695 // assigning to sService is not thread-safe, but this is best-effort code
696 // and on a well-behaved system should never happen
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800697 sService = service;
Nick Pelly3dd6c452011-01-10 18:14:41 +1100698 try {
699 sTagService = service.getNfcTagInterface();
700 } catch (RemoteException ee) {
701 Log.e(TAG, "could not retrieve NFC tag service during service recovery");
702 // nothing more can be done now, sService is still stale, we'll hit
703 // this recovery path again later
Martijn Coenena7397882013-07-30 20:07:47 -0700704 return;
705 }
706
707 try {
708 sCardEmulationService = service.getNfcCardEmulationInterface();
709 } catch (RemoteException ee) {
710 Log.e(TAG, "could not retrieve NFC card emulation service during service recovery");
Nick Pelly3dd6c452011-01-10 18:14:41 +1100711 }
712
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900713 try {
714 sNfcFCardEmulationService = service.getNfcFCardEmulationInterface();
715 } catch (RemoteException ee) {
716 Log.e(TAG, "could not retrieve NFC-F card emulation service during service recovery");
717 }
718
Nick Pelly6d55e132010-10-27 01:14:43 -0700719 return;
720 }
721
Nick Pelly590b73b2010-10-12 13:00:50 -0700722 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700723 * Return true if this NFC Adapter has any features enabled.
Nick Pelly590b73b2010-10-12 13:00:50 -0700724 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800725 * <p>If this method returns false, the NFC hardware is guaranteed not to
Nick Pellycccf01d2011-10-31 14:49:40 -0700726 * generate or respond to any NFC communication over its NFC radio.
727 * <p>Applications can use this to check if NFC is enabled. Applications
728 * can request Settings UI allowing the user to toggle NFC using:
729 * <p><pre>startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))</pre>
Nick Pelly74fe6c62011-02-02 22:37:40 -0800730 *
Nick Pellycccf01d2011-10-31 14:49:40 -0700731 * @see android.provider.Settings#ACTION_NFC_SETTINGS
Nick Pelly74fe6c62011-02-02 22:37:40 -0800732 * @return true if this NFC Adapter has any features enabled
Nick Pelly590b73b2010-10-12 13:00:50 -0700733 */
Nick Pelly7ea5c452010-10-20 18:39:11 -0700734 public boolean isEnabled() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700735 try {
Nick Pelly8d32a012011-08-09 07:03:49 -0700736 return sService.getState() == STATE_ON;
Nick Pelly590b73b2010-10-12 13:00:50 -0700737 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700738 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700739 return false;
740 }
741 }
742
743 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700744 * Return the state of this NFC Adapter.
745 *
746 * <p>Returns one of {@link #STATE_ON}, {@link #STATE_TURNING_ON},
747 * {@link #STATE_OFF}, {@link #STATE_TURNING_OFF}.
748 *
749 * <p>{@link #isEnabled()} is equivalent to
750 * <code>{@link #getAdapterState()} == {@link #STATE_ON}</code>
751 *
752 * @return the current state of this NFC adapter
753 *
754 * @hide
755 */
Mathew Inwood57069dc2018-07-31 15:33:20 +0100756 @UnsupportedAppUsage
Nick Pelly8d32a012011-08-09 07:03:49 -0700757 public int getAdapterState() {
758 try {
759 return sService.getState();
760 } catch (RemoteException e) {
761 attemptDeadServiceRecovery(e);
762 return NfcAdapter.STATE_OFF;
763 }
764 }
765
766 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700767 * Enable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700768 *
769 * <p>This call is asynchronous. Listen for
770 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
771 * operation is complete.
772 *
773 * <p>If this returns true, then either NFC is already on, or
774 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
775 * to indicate a state transition. If this returns false, then
776 * there is some problem that prevents an attempt to turn
777 * NFC on (for example we are in airplane mode and NFC is not
778 * toggleable in airplane mode on this platform).
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700779 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700780 * @hide
781 */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700782 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600783 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Nick Pelly7ea5c452010-10-20 18:39:11 -0700784 public boolean enable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700785 try {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800786 return sService.enable();
Nick Pelly590b73b2010-10-12 13:00:50 -0700787 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700788 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700789 return false;
790 }
791 }
792
793 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700794 * Disable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700795 *
796 * <p>No NFC features will work after this call, and the hardware
Nick Pelly7ea5c452010-10-20 18:39:11 -0700797 * will not perform or respond to any NFC communication.
Nick Pelly8d32a012011-08-09 07:03:49 -0700798 *
799 * <p>This call is asynchronous. Listen for
800 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
801 * operation is complete.
802 *
803 * <p>If this returns true, then either NFC is already off, or
804 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
805 * to indicate a state transition. If this returns false, then
806 * there is some problem that prevents an attempt to turn
807 * NFC off.
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700808 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700809 * @hide
810 */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700811 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600812 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Nick Pelly7ea5c452010-10-20 18:39:11 -0700813 public boolean disable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700814 try {
Sunil Jogi3bba8d02012-04-10 13:12:26 -0700815 return sService.disable(true);
Nick Pelly590b73b2010-10-12 13:00:50 -0700816 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700817 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700818 return false;
819 }
820 }
821
Martijn Coenen2c103112012-05-15 10:32:15 -0700822 /**
Martijn Coenen56a3e672014-09-23 21:27:30 -0700823 * Disable NFC hardware.
824 * @hide
825 */
826 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600827 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Martijn Coenen56a3e672014-09-23 21:27:30 -0700828 public boolean disable(boolean persist) {
829 try {
830 return sService.disable(persist);
831 } catch (RemoteException e) {
832 attemptDeadServiceRecovery(e);
833 return false;
834 }
835 }
836
837 /**
Andres Morales9c4f4002014-09-08 17:09:16 -0700838 * Pauses polling for a {@code timeoutInMs} millis. If polling must be resumed before timeout,
839 * use {@link #resumePolling()}.
840 * @hide
841 */
842 public void pausePolling(int timeoutInMs) {
843 try {
844 sService.pausePolling(timeoutInMs);
845 } catch (RemoteException e) {
846 attemptDeadServiceRecovery(e);
847 }
848 }
849
850 /**
851 * Resumes default polling for the current device state if polling is paused. Calling
852 * this while polling is not paused is a no-op.
853 *
854 * @hide
855 */
856 public void resumePolling() {
857 try {
858 sService.resumePolling();
859 } catch (RemoteException e) {
860 attemptDeadServiceRecovery(e);
861 }
862 }
863
864 /**
Martijn Coenen2c103112012-05-15 10:32:15 -0700865 * Set one or more {@link Uri}s to send using Android Beam (TM). Every
866 * Uri you provide must have either scheme 'file' or scheme 'content'.
867 *
868 * <p>For the data provided through this method, Android Beam tries to
869 * switch to alternate transports such as Bluetooth to achieve a fast
870 * transfer speed. Hence this method is very suitable
871 * for transferring large files such as pictures or songs.
872 *
873 * <p>The receiving side will store the content of each Uri in
874 * a file and present a notification to the user to open the file
875 * with a {@link android.content.Intent} with action
876 * {@link android.content.Intent#ACTION_VIEW}.
877 * If multiple URIs are sent, the {@link android.content.Intent} will refer
878 * to the first of the stored files.
879 *
880 * <p>This method may be called at any time before {@link Activity#onDestroy},
881 * but the URI(s) are only made available for Android Beam when the
882 * specified activity(s) are in resumed (foreground) state. The recommended
883 * approach is to call this method during your Activity's
884 * {@link Activity#onCreate} - see sample
885 * code below. This method does not immediately perform any I/O or blocking work,
886 * so is safe to call on your main thread.
887 *
888 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
889 * have priority over both {@link #setNdefPushMessage} and
890 * {@link #setNdefPushMessageCallback}.
891 *
892 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
893 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
894 * then the Uri push will be completely disabled for the specified activity(s).
895 *
896 * <p>Code example:
897 * <pre>
898 * protected void onCreate(Bundle savedInstanceState) {
899 * super.onCreate(savedInstanceState);
900 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
901 * if (nfcAdapter == null) return; // NFC not available on this device
902 * nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700903 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700904 * And that is it. Only one call per activity is necessary. The Android
905 * OS will automatically release its references to the Uri(s) and the
906 * Activity object when it is destroyed if you follow this pattern.
907 *
908 * <p>If your Activity wants to dynamically supply Uri(s),
909 * then set a callback using {@link #setBeamPushUrisCallback} instead
910 * of using this method.
911 *
912 * <p class="note">Do not pass in an Activity that has already been through
913 * {@link Activity#onDestroy}. This is guaranteed if you call this API
914 * during {@link Activity#onCreate}.
915 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700916 * <p class="note">If this device does not support alternate transports
917 * such as Bluetooth or WiFI, calling this method does nothing.
918 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700919 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
920 *
921 * @param uris an array of Uri(s) to push over Android Beam
922 * @param activity activity for which the Uri(s) will be pushed
Steve Elliott04141ea2016-04-15 15:09:43 -0400923 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Martijn Coenen2c103112012-05-15 10:32:15 -0700924 */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700925 public void setBeamPushUris(Uri[] uris, Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -0400926 synchronized (NfcAdapter.class) {
927 if (!sHasNfcFeature) {
928 throw new UnsupportedOperationException();
929 }
930 }
Nick Pelly1d7e9062012-04-03 17:46:00 -0700931 if (activity == null) {
932 throw new NullPointerException("activity cannot be null");
933 }
Martijn Coenen2c103112012-05-15 10:32:15 -0700934 if (uris != null) {
935 for (Uri uri : uris) {
936 if (uri == null) throw new NullPointerException("Uri not " +
937 "allowed to be null");
938 String scheme = uri.getScheme();
939 if (scheme == null || (!scheme.equalsIgnoreCase("file") &&
940 !scheme.equalsIgnoreCase("content"))) {
941 throw new IllegalArgumentException("URI needs to have " +
942 "either scheme file or scheme content");
943 }
944 }
945 }
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700946 mNfcActivityManager.setNdefPushContentUri(activity, uris);
947 }
948
Martijn Coenen2c103112012-05-15 10:32:15 -0700949 /**
950 * Set a callback that will dynamically generate one or more {@link Uri}s
951 * to send using Android Beam (TM). Every Uri the callback provides
952 * must have either scheme 'file' or scheme 'content'.
953 *
954 * <p>For the data provided through this callback, Android Beam tries to
955 * switch to alternate transports such as Bluetooth to achieve a fast
956 * transfer speed. Hence this method is very suitable
957 * for transferring large files such as pictures or songs.
958 *
959 * <p>The receiving side will store the content of each Uri in
960 * a file and present a notification to the user to open the file
961 * with a {@link android.content.Intent} with action
962 * {@link android.content.Intent#ACTION_VIEW}.
963 * If multiple URIs are sent, the {@link android.content.Intent} will refer
964 * to the first of the stored files.
965 *
966 * <p>This method may be called at any time before {@link Activity#onDestroy},
967 * but the URI(s) are only made available for Android Beam when the
968 * specified activity(s) are in resumed (foreground) state. The recommended
969 * approach is to call this method during your Activity's
970 * {@link Activity#onCreate} - see sample
971 * code below. This method does not immediately perform any I/O or blocking work,
972 * so is safe to call on your main thread.
973 *
974 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
975 * have priority over both {@link #setNdefPushMessage} and
976 * {@link #setNdefPushMessageCallback}.
977 *
978 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
979 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
980 * then the Uri push will be completely disabled for the specified activity(s).
981 *
982 * <p>Code example:
983 * <pre>
984 * protected void onCreate(Bundle savedInstanceState) {
985 * super.onCreate(savedInstanceState);
986 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
987 * if (nfcAdapter == null) return; // NFC not available on this device
988 * nfcAdapter.setBeamPushUrisCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700989 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700990 * And that is it. Only one call per activity is necessary. The Android
991 * OS will automatically release its references to the Uri(s) and the
992 * Activity object when it is destroyed if you follow this pattern.
993 *
994 * <p class="note">Do not pass in an Activity that has already been through
995 * {@link Activity#onDestroy}. This is guaranteed if you call this API
996 * during {@link Activity#onCreate}.
997 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700998 * <p class="note">If this device does not support alternate transports
999 * such as Bluetooth or WiFI, calling this method does nothing.
1000 *
Martijn Coenen2c103112012-05-15 10:32:15 -07001001 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1002 *
1003 * @param callback callback, or null to disable
1004 * @param activity activity for which the Uri(s) will be pushed
Steve Elliott04141ea2016-04-15 15:09:43 -04001005 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Martijn Coenen2c103112012-05-15 10:32:15 -07001006 */
Martijn Coenen20e8dd92012-04-12 16:37:18 -07001007 public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001008 synchronized (NfcAdapter.class) {
1009 if (!sHasNfcFeature) {
1010 throw new UnsupportedOperationException();
1011 }
1012 }
Martijn Coenen20e8dd92012-04-12 16:37:18 -07001013 if (activity == null) {
1014 throw new NullPointerException("activity cannot be null");
1015 }
1016 mNfcActivityManager.setNdefPushContentUriCallback(activity, callback);
Nick Pelly1d7e9062012-04-03 17:46:00 -07001017 }
1018
Jeff Hamilton52d32032011-01-08 15:31:26 -06001019 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -07001020 * Set a static {@link NdefMessage} to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -07001021 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001022 * <p>This method may be called at any time before {@link Activity#onDestroy},
1023 * but the NDEF message is only made available for NDEF push when the
1024 * specified activity(s) are in resumed (foreground) state. The recommended
1025 * approach is to call this method during your Activity's
1026 * {@link Activity#onCreate} - see sample
1027 * code below. This method does not immediately perform any I/O or blocking work,
1028 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001029 *
1030 * <p>Only one NDEF message can be pushed by the currently resumed activity.
1031 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -07001032 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -07001033 * the callback will take priority.
1034 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001035 * <p>If neither {@link #setNdefPushMessage} or
1036 * {@link #setNdefPushMessageCallback} have been called for your activity, then
1037 * the Android OS may choose to send a default NDEF message on your behalf,
1038 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001039 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001040 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
1041 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
1042 * then NDEF push will be completely disabled for the specified activity(s).
1043 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -07001044 * otherwise sent on your behalf for those activity(s).
1045 *
1046 * <p>If you want to prevent the Android OS from sending default NDEF
1047 * messages completely (for all activities), you can include a
Neil Fuller9498e8a2015-11-30 09:51:33 +00001048 * {@code <meta-data>} element inside the {@code <application>}
Martijn Coenen854e0772012-04-02 14:52:10 -07001049 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -07001050 * <pre>
1051 * &lt;application ...>
1052 * &lt;meta-data android:name="android.nfc.disable_beam_default"
1053 * android:value="true" />
1054 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001055 *
1056 * <p>The API allows for multiple activities to be specified at a time,
1057 * but it is strongly recommended to just register one at a time,
1058 * and to do so during the activity's {@link Activity#onCreate}. For example:
1059 * <pre>
1060 * protected void onCreate(Bundle savedInstanceState) {
1061 * super.onCreate(savedInstanceState);
1062 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1063 * if (nfcAdapter == null) return; // NFC not available on this device
1064 * nfcAdapter.setNdefPushMessage(ndefMessage, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001065 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001066 * And that is it. Only one call per activity is necessary. The Android
1067 * OS will automatically release its references to the NDEF message and the
1068 * Activity object when it is destroyed if you follow this pattern.
1069 *
1070 * <p>If your Activity wants to dynamically generate an NDEF message,
1071 * then set a callback using {@link #setNdefPushMessageCallback} instead
1072 * of a static message.
1073 *
1074 * <p class="note">Do not pass in an Activity that has already been through
1075 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1076 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -07001077 *
Martijn Coenen2c103112012-05-15 10:32:15 -07001078 * <p class="note">For sending large content such as pictures and songs,
1079 * consider using {@link #setBeamPushUris}, which switches to alternate transports
1080 * such as Bluetooth to achieve a fast transfer rate.
1081 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001082 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1083 *
1084 * @param message NDEF message to push over NFC, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001085 * @param activity activity for which the NDEF message will be pushed
1086 * @param activities optional additional activities, however we strongly recommend
1087 * to only register one at a time, and to do so in that activity's
1088 * {@link Activity#onCreate}
Steve Elliott04141ea2016-04-15 15:09:43 -04001089 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001090 */
Nick Pelly82328bf2011-08-30 09:37:25 -07001091 public void setNdefPushMessage(NdefMessage message, Activity activity,
1092 Activity ... activities) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001093 synchronized (NfcAdapter.class) {
1094 if (!sHasNfcFeature) {
1095 throw new UnsupportedOperationException();
1096 }
1097 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001098 int targetSdkVersion = getSdkVersion();
1099 try {
1100 if (activity == null) {
1101 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001102 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001103 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001104 for (Activity a : activities) {
1105 if (a == null) {
1106 throw new NullPointerException("activities cannot contain null");
1107 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001108 mNfcActivityManager.setNdefPushMessage(a, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001109 }
1110 } catch (IllegalStateException e) {
1111 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1112 // Less strict on old applications - just log the error
1113 Log.e(TAG, "Cannot call API with Activity that has already " +
1114 "been destroyed", e);
1115 } else {
1116 // Prevent new applications from making this mistake, re-throw
1117 throw(e);
1118 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001119 }
1120 }
1121
1122 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001123 * @hide
1124 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001125 @SystemApi
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001126 public void setNdefPushMessage(NdefMessage message, Activity activity, int flags) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001127 synchronized (NfcAdapter.class) {
1128 if (!sHasNfcFeature) {
1129 throw new UnsupportedOperationException();
1130 }
1131 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001132 if (activity == null) {
1133 throw new NullPointerException("activity cannot be null");
1134 }
1135 mNfcActivityManager.setNdefPushMessage(activity, message, flags);
1136 }
1137
1138 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -07001139 * Set a callback that dynamically generates NDEF messages to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -07001140 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001141 * <p>This method may be called at any time before {@link Activity#onDestroy},
1142 * but the NDEF message callback can only occur when the
1143 * specified activity(s) are in resumed (foreground) state. The recommended
1144 * approach is to call this method during your Activity's
1145 * {@link Activity#onCreate} - see sample
1146 * code below. This method does not immediately perform any I/O or blocking work,
1147 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001148 *
1149 * <p>Only one NDEF message can be pushed by the currently resumed activity.
1150 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -07001151 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -07001152 * the callback will take priority.
1153 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001154 * <p>If neither {@link #setNdefPushMessage} or
1155 * {@link #setNdefPushMessageCallback} have been called for your activity, then
1156 * the Android OS may choose to send a default NDEF message on your behalf,
1157 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001158 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001159 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
1160 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
1161 * then NDEF push will be completely disabled for the specified activity(s).
1162 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -07001163 * otherwise sent on your behalf for those activity(s).
1164 *
1165 * <p>If you want to prevent the Android OS from sending default NDEF
1166 * messages completely (for all activities), you can include a
Neil Fuller9498e8a2015-11-30 09:51:33 +00001167 * {@code <meta-data>} element inside the {@code <application>}
Martijn Coenen854e0772012-04-02 14:52:10 -07001168 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -07001169 * <pre>
1170 * &lt;application ...>
1171 * &lt;meta-data android:name="android.nfc.disable_beam_default"
1172 * android:value="true" />
1173 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001174 *
1175 * <p>The API allows for multiple activities to be specified at a time,
1176 * but it is strongly recommended to just register one at a time,
1177 * and to do so during the activity's {@link Activity#onCreate}. For example:
1178 * <pre>
1179 * protected void onCreate(Bundle savedInstanceState) {
1180 * super.onCreate(savedInstanceState);
1181 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1182 * if (nfcAdapter == null) return; // NFC not available on this device
1183 * nfcAdapter.setNdefPushMessageCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001184 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001185 * And that is it. Only one call per activity is necessary. The Android
1186 * OS will automatically release its references to the callback and the
1187 * Activity object when it is destroyed if you follow this pattern.
1188 *
1189 * <p class="note">Do not pass in an Activity that has already been through
1190 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1191 * during {@link Activity#onCreate}.
Martijn Coenen2c103112012-05-15 10:32:15 -07001192 * <p class="note">For sending large content such as pictures and songs,
1193 * consider using {@link #setBeamPushUris}, which switches to alternate transports
1194 * such as Bluetooth to achieve a fast transfer rate.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001195 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1196 *
1197 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001198 * @param activity activity for which the NDEF message will be pushed
1199 * @param activities optional additional activities, however we strongly recommend
1200 * to only register one at a time, and to do so in that activity's
1201 * {@link Activity#onCreate}
Steve Elliott04141ea2016-04-15 15:09:43 -04001202 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001203 */
Nick Pelly82328bf2011-08-30 09:37:25 -07001204 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
Nick Pellyc84c89a2011-08-22 22:27:11 -07001205 Activity ... activities) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001206 synchronized (NfcAdapter.class) {
1207 if (!sHasNfcFeature) {
1208 throw new UnsupportedOperationException();
1209 }
1210 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001211 int targetSdkVersion = getSdkVersion();
1212 try {
1213 if (activity == null) {
1214 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001215 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001216 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001217 for (Activity a : activities) {
1218 if (a == null) {
1219 throw new NullPointerException("activities cannot contain null");
1220 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001221 mNfcActivityManager.setNdefPushMessageCallback(a, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001222 }
1223 } catch (IllegalStateException e) {
1224 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1225 // Less strict on old applications - just log the error
1226 Log.e(TAG, "Cannot call API with Activity that has already " +
1227 "been destroyed", e);
1228 } else {
1229 // Prevent new applications from making this mistake, re-throw
1230 throw(e);
1231 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001232 }
1233 }
1234
1235 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001236 * @hide
1237 */
Mathew Inwood57069dc2018-07-31 15:33:20 +01001238 @UnsupportedAppUsage
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001239 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
1240 int flags) {
1241 if (activity == null) {
1242 throw new NullPointerException("activity cannot be null");
1243 }
1244 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, flags);
1245 }
1246
1247 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -07001248 * Set a callback on successful Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -07001249 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001250 * <p>This method may be called at any time before {@link Activity#onDestroy},
1251 * but the callback can only occur when the
1252 * specified activity(s) are in resumed (foreground) state. The recommended
1253 * approach is to call this method during your Activity's
1254 * {@link Activity#onCreate} - see sample
1255 * code below. This method does not immediately perform any I/O or blocking work,
1256 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001257 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001258 * <p>The API allows for multiple activities to be specified at a time,
1259 * but it is strongly recommended to just register one at a time,
1260 * and to do so during the activity's {@link Activity#onCreate}. For example:
1261 * <pre>
1262 * protected void onCreate(Bundle savedInstanceState) {
1263 * super.onCreate(savedInstanceState);
1264 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1265 * if (nfcAdapter == null) return; // NFC not available on this device
1266 * nfcAdapter.setOnNdefPushCompleteCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001267 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001268 * And that is it. Only one call per activity is necessary. The Android
1269 * OS will automatically release its references to the callback and the
1270 * Activity object when it is destroyed if you follow this pattern.
1271 *
1272 * <p class="note">Do not pass in an Activity that has already been through
1273 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1274 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -07001275 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001276 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1277 *
1278 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001279 * @param activity activity for which the NDEF message will be pushed
1280 * @param activities optional additional activities, however we strongly recommend
1281 * to only register one at a time, and to do so in that activity's
1282 * {@link Activity#onCreate}
Steve Elliott04141ea2016-04-15 15:09:43 -04001283 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001284 */
1285 public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback,
Nick Pelly82328bf2011-08-30 09:37:25 -07001286 Activity activity, Activity ... activities) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001287 synchronized (NfcAdapter.class) {
1288 if (!sHasNfcFeature) {
1289 throw new UnsupportedOperationException();
1290 }
1291 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001292 int targetSdkVersion = getSdkVersion();
1293 try {
1294 if (activity == null) {
1295 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001296 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001297 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, callback);
1298 for (Activity a : activities) {
1299 if (a == null) {
1300 throw new NullPointerException("activities cannot contain null");
1301 }
1302 mNfcActivityManager.setOnNdefPushCompleteCallback(a, callback);
1303 }
1304 } catch (IllegalStateException e) {
1305 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1306 // Less strict on old applications - just log the error
1307 Log.e(TAG, "Cannot call API with Activity that has already " +
1308 "been destroyed", e);
1309 } else {
1310 // Prevent new applications from making this mistake, re-throw
1311 throw(e);
1312 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001313 }
1314 }
1315
1316 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001317 * Enable foreground dispatch to the given Activity.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001318 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001319 * <p>This will give give priority to the foreground activity when
1320 * dispatching a discovered {@link Tag} to an application.
1321 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001322 * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents
1323 * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and
1324 * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED}
1325 * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled
1326 * by passing in the tech lists separately. Each first level entry in the tech list represents
1327 * an array of technologies that must all be present to match. If any of the first level sets
1328 * match then the dispatch is routed through the given PendingIntent. In other words, the second
1329 * level is ANDed together and the first level entries are ORed together.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001330 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001331 * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters
1332 * that acts a wild card and will cause the foreground activity to receive all tags via the
1333 * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001334 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001335 * <p>This method must be called from the main thread, and only when the activity is in the
1336 * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before
1337 * the completion of their {@link Activity#onPause} callback to disable foreground dispatch
1338 * after it has been enabled.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001339 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001340 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1341 *
Jeff Hamilton52d32032011-01-08 15:31:26 -06001342 * @param activity the Activity to dispatch to
1343 * @param intent the PendingIntent to start for the dispatch
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001344 * @param filters the IntentFilters to override dispatching for, or null to always dispatch
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001345 * @param techLists the tech lists used to perform matching for dispatching of the
1346 * {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent
Nick Pelly74fe6c62011-02-02 22:37:40 -08001347 * @throws IllegalStateException if the Activity is not currently in the foreground
Steve Elliott04141ea2016-04-15 15:09:43 -04001348 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001349 */
1350 public void enableForegroundDispatch(Activity activity, PendingIntent intent,
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001351 IntentFilter[] filters, String[][] techLists) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001352 synchronized (NfcAdapter.class) {
1353 if (!sHasNfcFeature) {
1354 throw new UnsupportedOperationException();
1355 }
1356 }
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001357 if (activity == null || intent == null) {
Jeff Hamilton52d32032011-01-08 15:31:26 -06001358 throw new NullPointerException();
1359 }
1360 if (!activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001361 throw new IllegalStateException("Foreground dispatch can only be enabled " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001362 "when your activity is resumed");
1363 }
1364 try {
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001365 TechListParcel parcel = null;
1366 if (techLists != null && techLists.length > 0) {
1367 parcel = new TechListParcel(techLists);
1368 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06001369 ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001370 mForegroundDispatchListener);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001371 sService.setForegroundDispatch(intent, filters, parcel);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001372 } catch (RemoteException e) {
1373 attemptDeadServiceRecovery(e);
1374 }
1375 }
1376
1377 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001378 * Disable foreground dispatch to the given activity.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001379 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001380 * <p>After calling {@link #enableForegroundDispatch}, an activity
1381 * must call this method before its {@link Activity#onPause} callback
1382 * completes.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001383 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001384 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001385 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001386 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1387 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001388 * @param activity the Activity to disable dispatch to
1389 * @throws IllegalStateException if the Activity has already been paused
Steve Elliott04141ea2016-04-15 15:09:43 -04001390 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001391 */
1392 public void disableForegroundDispatch(Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001393 synchronized (NfcAdapter.class) {
1394 if (!sHasNfcFeature) {
1395 throw new UnsupportedOperationException();
1396 }
1397 }
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001398 ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,
1399 mForegroundDispatchListener);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001400 disableForegroundDispatchInternal(activity, false);
1401 }
1402
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001403 OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001404 @Override
1405 public void onPaused(Activity activity) {
1406 disableForegroundDispatchInternal(activity, true);
1407 }
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001408 };
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001409
Jeff Hamilton52d32032011-01-08 15:31:26 -06001410 void disableForegroundDispatchInternal(Activity activity, boolean force) {
1411 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001412 sService.setForegroundDispatch(null, null, null);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001413 if (!force && !activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001414 throw new IllegalStateException("You must disable foreground dispatching " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001415 "while your activity is still resumed");
1416 }
1417 } catch (RemoteException e) {
1418 attemptDeadServiceRecovery(e);
1419 }
1420 }
1421
Nick Pelly590b73b2010-10-12 13:00:50 -07001422 /**
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001423 * Limit the NFC controller to reader mode while this Activity is in the foreground.
1424 *
1425 * <p>In this mode the NFC controller will only act as an NFC tag reader/writer,
1426 * thus disabling any peer-to-peer (Android Beam) and card-emulation modes of
1427 * the NFC adapter on this device.
1428 *
1429 * <p>Use {@link #FLAG_READER_SKIP_NDEF_CHECK} to prevent the platform from
1430 * performing any NDEF checks in reader mode. Note that this will prevent the
1431 * {@link Ndef} tag technology from being enumerated on the tag, and that
1432 * NDEF-based tag dispatch will not be functional.
1433 *
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001434 * <p>For interacting with tags that are emulated on another Android device
1435 * using Android's host-based card-emulation, the recommended flags are
1436 * {@link #FLAG_READER_NFC_A} and {@link #FLAG_READER_SKIP_NDEF_CHECK}.
1437 *
1438 * @param activity the Activity that requests the adapter to be in reader mode
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001439 * @param callback the callback to be called when a tag is discovered
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001440 * @param flags Flags indicating poll technologies and other optional parameters
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001441 * @param extras Additional extras for configuring reader mode.
Steve Elliott04141ea2016-04-15 15:09:43 -04001442 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001443 */
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001444 public void enableReaderMode(Activity activity, ReaderCallback callback, int flags,
1445 Bundle extras) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001446 synchronized (NfcAdapter.class) {
1447 if (!sHasNfcFeature) {
1448 throw new UnsupportedOperationException();
1449 }
1450 }
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001451 mNfcActivityManager.enableReaderMode(activity, callback, flags, extras);
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001452 }
1453
1454 /**
1455 * Restore the NFC adapter to normal mode of operation: supporting
1456 * peer-to-peer (Android Beam), card emulation, and polling for
1457 * all supported tag technologies.
1458 *
1459 * @param activity the Activity that currently has reader mode enabled
Steve Elliott04141ea2016-04-15 15:09:43 -04001460 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001461 */
1462 public void disableReaderMode(Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001463 synchronized (NfcAdapter.class) {
1464 if (!sHasNfcFeature) {
1465 throw new UnsupportedOperationException();
1466 }
1467 }
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001468 mNfcActivityManager.disableReaderMode(activity);
1469 }
1470
1471 /**
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001472 * Manually invoke Android Beam to share data.
1473 *
1474 * <p>The Android Beam animation is normally only shown when two NFC-capable
1475 * devices come into range.
1476 * By calling this method, an Activity can invoke the Beam animation directly
1477 * even if no other NFC device is in range yet. The Beam animation will then
1478 * prompt the user to tap another NFC-capable device to complete the data
1479 * transfer.
1480 *
1481 * <p>The main advantage of using this method is that it avoids the need for the
1482 * user to tap the screen to complete the transfer, as this method already
1483 * establishes the direction of the transfer and the consent of the user to
1484 * share data. Callers are responsible for making sure that the user has
1485 * consented to sharing data on NFC tap.
1486 *
1487 * <p>Note that to use this method, the passed in Activity must have already
1488 * set data to share over Beam by using method calls such as
Martijn Coenen6924c4e2014-02-25 11:37:17 -08001489 * {@link #setNdefPushMessageCallback} or
1490 * {@link #setBeamPushUrisCallback}.
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001491 *
1492 * @param activity the current foreground Activity that has registered data to share
1493 * @return whether the Beam animation was successfully invoked
Steve Elliott04141ea2016-04-15 15:09:43 -04001494 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001495 */
1496 public boolean invokeBeam(Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001497 synchronized (NfcAdapter.class) {
1498 if (!sHasNfcFeature) {
1499 throw new UnsupportedOperationException();
1500 }
1501 }
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001502 if (activity == null) {
1503 throw new NullPointerException("activity may not be null.");
1504 }
1505 enforceResumed(activity);
1506 try {
1507 sService.invokeBeam();
1508 return true;
1509 } catch (RemoteException e) {
1510 Log.e(TAG, "invokeBeam: NFC process has died.");
1511 attemptDeadServiceRecovery(e);
1512 return false;
1513 }
1514 }
1515
1516 /**
Martijn Coenendbedb4c2014-07-01 15:52:19 -07001517 * @hide
1518 */
1519 public boolean invokeBeam(BeamShareData shareData) {
1520 try {
1521 Log.e(TAG, "invokeBeamInternal()");
1522 sService.invokeBeamInternal(shareData);
1523 return true;
1524 } catch (RemoteException e) {
1525 Log.e(TAG, "invokeBeam: NFC process has died.");
1526 attemptDeadServiceRecovery(e);
1527 return false;
1528 }
1529 }
1530
1531 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001532 * Enable NDEF message push over NFC while this Activity is in the foreground.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001533 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001534 * <p>You must explicitly call this method every time the activity is
1535 * resumed, and you must call {@link #disableForegroundNdefPush} before
1536 * your activity completes {@link Activity#onPause}.
1537 *
1538 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1539 * instead: it automatically hooks into your activity life-cycle,
1540 * so you do not need to call enable/disable in your onResume/onPause.
1541 *
1542 * <p>For NDEF push to function properly the other NFC device must
1543 * support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or
1544 * Android's "com.android.npp" (Ndef Push Protocol). This was optional
1545 * on Gingerbread level Android NFC devices, but SNEP is mandatory on
1546 * Ice-Cream-Sandwich and beyond.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001547 *
1548 * <p>This method must be called from the main thread.
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001549 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001550 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1551 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001552 * @param activity foreground activity
1553 * @param message a NDEF Message to push over NFC
1554 * @throws IllegalStateException if the activity is not currently in the foreground
Steve Elliott04141ea2016-04-15 15:09:43 -04001555 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001556 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001557 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001558 @Deprecated
1559 public void enableForegroundNdefPush(Activity activity, NdefMessage message) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001560 synchronized (NfcAdapter.class) {
1561 if (!sHasNfcFeature) {
1562 throw new UnsupportedOperationException();
1563 }
1564 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001565 if (activity == null || message == null) {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001566 throw new NullPointerException();
1567 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001568 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001569 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Jason parks01425362011-05-24 02:57:37 -07001570 }
1571
1572 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001573 * Disable NDEF message push over P2P.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001574 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001575 * <p>After calling {@link #enableForegroundNdefPush}, an activity
1576 * must call this method before its {@link Activity#onPause} callback
1577 * completes.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001578 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001579 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1580 * instead: it automatically hooks into your activity life-cycle,
1581 * so you do not need to call enable/disable in your onResume/onPause.
1582 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001583 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001584 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001585 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1586 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001587 * @param activity the Foreground activity
1588 * @throws IllegalStateException if the Activity has already been paused
Steve Elliott04141ea2016-04-15 15:09:43 -04001589 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001590 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001591 */
Nick Pellyc97a5522012-01-05 15:13:01 +11001592 @Deprecated
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001593 public void disableForegroundNdefPush(Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001594 synchronized (NfcAdapter.class) {
1595 if (!sHasNfcFeature) {
1596 throw new UnsupportedOperationException();
1597 }
1598 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001599 if (activity == null) {
1600 throw new NullPointerException();
1601 }
1602 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001603 mNfcActivityManager.setNdefPushMessage(activity, null, 0);
1604 mNfcActivityManager.setNdefPushMessageCallback(activity, null, 0);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001605 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, null);
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001606 }
1607
Nick Pellyc84c89a2011-08-22 22:27:11 -07001608 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001609 * Enable NDEF Push feature.
1610 * <p>This API is for the Settings application.
1611 * @hide
1612 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001613 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001614 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Nick Pellyc84c89a2011-08-22 22:27:11 -07001615 public boolean enableNdefPush() {
Steve Elliott04141ea2016-04-15 15:09:43 -04001616 if (!sHasNfcFeature) {
1617 throw new UnsupportedOperationException();
1618 }
Martijn Coenen6d748942011-07-21 09:34:25 +02001619 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001620 return sService.enableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001621 } catch (RemoteException e) {
1622 attemptDeadServiceRecovery(e);
1623 return false;
1624 }
1625 }
1626
1627 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001628 * Disable NDEF Push feature.
1629 * <p>This API is for the Settings application.
Martijn Coenen6d748942011-07-21 09:34:25 +02001630 * @hide
1631 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001632 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001633 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Nick Pellyc84c89a2011-08-22 22:27:11 -07001634 public boolean disableNdefPush() {
Steve Elliott04141ea2016-04-15 15:09:43 -04001635 synchronized (NfcAdapter.class) {
1636 if (!sHasNfcFeature) {
1637 throw new UnsupportedOperationException();
1638 }
1639 }
Martijn Coenen6d748942011-07-21 09:34:25 +02001640 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001641 return sService.disableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001642 } catch (RemoteException e) {
1643 attemptDeadServiceRecovery(e);
1644 return false;
1645 }
1646 }
1647
1648 /**
Nick Pellycccf01d2011-10-31 14:49:40 -07001649 * Return true if the NDEF Push (Android Beam) feature is enabled.
1650 * <p>This function will return true only if both NFC is enabled, and the
1651 * NDEF Push feature is enabled.
1652 * <p>Note that if NFC is enabled but NDEF Push is disabled then this
1653 * device can still <i>receive</i> NDEF messages, it just cannot send them.
1654 * <p>Applications cannot directly toggle the NDEF Push feature, but they
1655 * can request Settings UI allowing the user to toggle NDEF Push using
1656 * <code>startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))</code>
1657 * <p>Example usage in an Activity that requires NDEF Push:
1658 * <p><pre>
1659 * protected void onResume() {
1660 * super.onResume();
1661 * if (!nfcAdapter.isEnabled()) {
1662 * startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
1663 * } else if (!nfcAdapter.isNdefPushEnabled()) {
1664 * startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
1665 * }
Scott Main97e0a1c2012-06-25 11:22:10 -07001666 * }</pre>
Martijn Coenen6d748942011-07-21 09:34:25 +02001667 *
Nick Pellycccf01d2011-10-31 14:49:40 -07001668 * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS
Nick Pellyc84c89a2011-08-22 22:27:11 -07001669 * @return true if NDEF Push feature is enabled
Steve Elliott04141ea2016-04-15 15:09:43 -04001670 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Martijn Coenen6d748942011-07-21 09:34:25 +02001671 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001672 public boolean isNdefPushEnabled() {
Steve Elliott04141ea2016-04-15 15:09:43 -04001673 synchronized (NfcAdapter.class) {
1674 if (!sHasNfcFeature) {
1675 throw new UnsupportedOperationException();
1676 }
1677 }
Martijn Coenen6d748942011-07-21 09:34:25 +02001678 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001679 return sService.isNdefPushEnabled();
Martijn Coenen6d748942011-07-21 09:34:25 +02001680 } catch (RemoteException e) {
1681 attemptDeadServiceRecovery(e);
1682 return false;
1683 }
1684 }
1685
1686 /**
Martijn Coenend55def82016-04-14 15:19:31 +02001687 * Signals that you are no longer interested in communicating with an NFC tag
1688 * for as long as it remains in range.
1689 *
1690 * All future attempted communication to this tag will fail with {@link IOException}.
1691 * The NFC controller will be put in a low-power polling mode, allowing the device
1692 * to save power in cases where it's "attached" to a tag all the time (e.g. a tag in
1693 * car dock).
1694 *
1695 * Additionally the debounceMs parameter allows you to specify for how long the tag needs
1696 * to have gone out of range, before it will be dispatched again.
1697 *
1698 * Note: the NFC controller typically polls at a pretty slow interval (100 - 500 ms).
1699 * This means that if the tag repeatedly goes in and out of range (for example, in
1700 * case of a flaky connection), and the controller happens to poll every time the
1701 * tag is out of range, it *will* re-dispatch the tag after debounceMs, despite the tag
1702 * having been "in range" during the interval.
1703 *
1704 * Note 2: if a tag with another UID is detected after this API is called, its effect
1705 * will be cancelled; if this tag shows up before the amount of time specified in
1706 * debounceMs, it will be dispatched again.
1707 *
1708 * Note 3: some tags have a random UID, in which case this API won't work reliably.
1709 *
1710 * @param tag the {@link android.nfc.Tag Tag} to ignore.
1711 * @param debounceMs minimum amount of time the tag needs to be out of range before being
1712 * dispatched again.
1713 * @param tagRemovedListener listener to be called when the tag is removed from the field.
1714 * Note that this will only be called if the tag has been out of range
1715 * for at least debounceMs, or if another tag came into range before
1716 * debounceMs. May be null in case you don't want a callback.
1717 * @param handler the {@link android.os.Handler Handler} that will be used for delivering
1718 * the callback. if the handler is null, then the thread used for delivering
1719 * the callback is unspecified.
1720 * @return false if the tag couldn't be found (or has already gone out of range), true otherwise
1721 */
1722 public boolean ignore(final Tag tag, int debounceMs,
1723 final OnTagRemovedListener tagRemovedListener, final Handler handler) {
1724 ITagRemovedCallback.Stub iListener = null;
1725 if (tagRemovedListener != null) {
1726 iListener = new ITagRemovedCallback.Stub() {
1727 @Override
1728 public void onTagRemoved() throws RemoteException {
1729 if (handler != null) {
1730 handler.post(new Runnable() {
1731 @Override
1732 public void run() {
1733 tagRemovedListener.onTagRemoved();
1734 }
1735 });
1736 } else {
1737 tagRemovedListener.onTagRemoved();
1738 }
1739 synchronized (mLock) {
1740 mTagRemovedListener = null;
1741 }
1742 }
1743 };
1744 }
1745 synchronized (mLock) {
1746 mTagRemovedListener = iListener;
1747 }
1748 try {
1749 return sService.ignore(tag.getServiceHandle(), debounceMs, iListener);
1750 } catch (RemoteException e) {
1751 return false;
1752 }
1753 }
1754
1755 /**
Nick Pellyc97a5522012-01-05 15:13:01 +11001756 * Inject a mock NFC tag.<p>
1757 * Used for testing purposes.
1758 * <p class="note">Requires the
1759 * {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
1760 * @hide
1761 */
Nick Pelly1f5badc2012-01-24 13:22:58 -08001762 public void dispatch(Tag tag) {
Nick Pellyc97a5522012-01-05 15:13:01 +11001763 if (tag == null) {
1764 throw new NullPointerException("tag cannot be null");
1765 }
1766 try {
Nick Pelly1f5badc2012-01-24 13:22:58 -08001767 sService.dispatch(tag);
Nick Pellyc97a5522012-01-05 15:13:01 +11001768 } catch (RemoteException e) {
1769 attemptDeadServiceRecovery(e);
1770 }
1771 }
1772
1773 /**
Daniel Tomas90245642010-11-17 10:07:52 +01001774 * @hide
1775 */
Martijn Coenen188cddb2012-01-31 22:16:15 -08001776 public void setP2pModes(int initiatorModes, int targetModes) {
1777 try {
1778 sService.setP2pModes(initiatorModes, targetModes);
1779 } catch (RemoteException e) {
1780 attemptDeadServiceRecovery(e);
1781 }
1782 }
1783
1784 /**
Andres Morales11d2e532014-07-23 12:39:55 -07001785 * Registers a new NFC unlock handler with the NFC service.
1786 *
1787 * <p />NFC unlock handlers are intended to unlock the keyguard in the presence of a trusted
1788 * NFC device. The handler should return true if it successfully authenticates the user and
1789 * unlocks the keyguard.
1790 *
1791 * <p /> The parameter {@code tagTechnologies} determines which Tag technologies will be polled for
1792 * at the lockscreen. Polling for less tag technologies reduces latency, and so it is
1793 * strongly recommended to only provide the Tag technologies that the handler is expected to
Andres Moralesf9a97942014-08-14 15:43:30 -07001794 * receive. There must be at least one tag technology provided, otherwise the unlock handler
1795 * is ignored.
Andres Morales11d2e532014-07-23 12:39:55 -07001796 *
1797 * @hide
1798 */
1799 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001800 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Andres Morales11d2e532014-07-23 12:39:55 -07001801 public boolean addNfcUnlockHandler(final NfcUnlockHandler unlockHandler,
1802 String[] tagTechnologies) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001803 synchronized (NfcAdapter.class) {
1804 if (!sHasNfcFeature) {
1805 throw new UnsupportedOperationException();
1806 }
1807 }
Andres Moralesf9a97942014-08-14 15:43:30 -07001808 // If there are no tag technologies, don't bother adding unlock handler
1809 if (tagTechnologies.length == 0) {
1810 return false;
1811 }
Andres Morales11d2e532014-07-23 12:39:55 -07001812
Andres Moralesf9a97942014-08-14 15:43:30 -07001813 try {
Andres Morales11d2e532014-07-23 12:39:55 -07001814 synchronized (mLock) {
1815 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001816 // update the tag technologies
1817 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.get(unlockHandler));
1818 mNfcUnlockHandlers.remove(unlockHandler);
Andres Morales11d2e532014-07-23 12:39:55 -07001819 }
Andres Moralesf9a97942014-08-14 15:43:30 -07001820
1821 INfcUnlockHandler.Stub iHandler = new INfcUnlockHandler.Stub() {
1822 @Override
1823 public boolean onUnlockAttempted(Tag tag) throws RemoteException {
1824 return unlockHandler.onUnlockAttempted(tag);
1825 }
1826 };
1827
1828 sService.addNfcUnlockHandler(iHandler,
1829 Tag.getTechCodesFromStrings(tagTechnologies));
1830 mNfcUnlockHandlers.put(unlockHandler, iHandler);
Andres Morales11d2e532014-07-23 12:39:55 -07001831 }
1832 } catch (RemoteException e) {
1833 attemptDeadServiceRecovery(e);
1834 return false;
1835 } catch (IllegalArgumentException e) {
1836 Log.e(TAG, "Unable to register LockscreenDispatch", e);
1837 return false;
1838 }
1839
1840 return true;
1841 }
1842
1843 /**
1844 * Removes a previously registered unlock handler. Also removes the tag technologies
1845 * associated with the removed unlock handler.
1846 *
1847 * @hide
1848 */
1849 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001850 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Andres Morales11d2e532014-07-23 12:39:55 -07001851 public boolean removeNfcUnlockHandler(NfcUnlockHandler unlockHandler) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001852 synchronized (NfcAdapter.class) {
1853 if (!sHasNfcFeature) {
1854 throw new UnsupportedOperationException();
1855 }
1856 }
Andres Morales11d2e532014-07-23 12:39:55 -07001857 try {
1858 synchronized (mLock) {
1859 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001860 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.remove(unlockHandler));
Andres Morales11d2e532014-07-23 12:39:55 -07001861 }
1862
1863 return true;
1864 }
1865 } catch (RemoteException e) {
1866 attemptDeadServiceRecovery(e);
1867 return false;
1868 }
1869 }
1870
1871 /**
Martijn Coenen188cddb2012-01-31 22:16:15 -08001872 * @hide
1873 */
Mathew Inwood57069dc2018-07-31 15:33:20 +01001874 @UnsupportedAppUsage
Nick Pelly367f41f2011-03-08 11:43:30 -08001875 public INfcAdapterExtras getNfcAdapterExtrasInterface() {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06001876 if (mContext == null) {
1877 throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
1878 + " NFC extras APIs");
1879 }
Daniel Tomas90245642010-11-17 10:07:52 +01001880 try {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06001881 return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
Daniel Tomas90245642010-11-17 10:07:52 +01001882 } catch (RemoteException e) {
Nick Pelly367f41f2011-03-08 11:43:30 -08001883 attemptDeadServiceRecovery(e);
Daniel Tomas90245642010-11-17 10:07:52 +01001884 return null;
1885 }
1886 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001887
1888 void enforceResumed(Activity activity) {
1889 if (!activity.isResumed()) {
1890 throw new IllegalStateException("API cannot be called while activity is paused");
1891 }
1892 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001893
1894 int getSdkVersion() {
1895 if (mContext == null) {
1896 return android.os.Build.VERSION_CODES.GINGERBREAD; // best guess
1897 } else {
1898 return mContext.getApplicationInfo().targetSdkVersion;
1899 }
1900 }
Nick Pelly590b73b2010-10-12 13:00:50 -07001901}