blob: 8406bcfa8184da8f750020a5e1aecbc97e211f1a [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 Hamiltonbb951c82011-11-08 16:55:13 -060019import java.util.HashMap;
20
Nick Pelly590b73b2010-10-12 13:00:50 -070021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Martijn Coenen29c2e372014-07-29 13:54:21 -070023import android.annotation.SystemApi;
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;
45
Nick Pelly590b73b2010-10-12 13:00:50 -070046/**
Nick Pelly74fe6c62011-02-02 22:37:40 -080047 * Represents the local NFC adapter.
Nick Pelly590b73b2010-10-12 13:00:50 -070048 * <p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -080049 * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC
50 * adapter for this Android device.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080051 *
52 * <div class="special reference">
53 * <h3>Developer Guides</h3>
54 * <p>For more information about using NFC, read the
55 * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p>
Scott Mainb38ad762013-10-17 11:57:06 -070056 * <p>To perform basic file sharing between devices, read
57 * <a href="{@docRoot}training/beam-files/index.html">Sharing Files with NFC</a>.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080058 * </div>
Nick Pelly590b73b2010-10-12 13:00:50 -070059 */
60public final class NfcAdapter {
Nick Pellyc84c89a2011-08-22 22:27:11 -070061 static final String TAG = "NFC";
Nick Pelly50b4d8f2010-12-07 22:40:28 -080062
Nick Pelly590b73b2010-10-12 13:00:50 -070063 /**
Jeff Hamilton641dd622010-12-02 09:16:22 -060064 * Intent to start an activity when a tag with NDEF payload is discovered.
Jeff Hamilton641dd622010-12-02 09:16:22 -060065 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090066 * <p>The system inspects the first {@link NdefRecord} in the first {@link NdefMessage} and
67 * looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the
68 * intent will contain the URI in its data field. If a MIME record is found the intent will
69 * contain the MIME type in its type field. This allows activities to register
70 * {@link IntentFilter}s targeting specific content on tags. Activities should register the
71 * most specific intent filters possible to avoid the activity chooser dialog, which can
72 * disrupt the interaction with the tag as the user interacts with the screen.
73 *
74 * <p>If the tag has an NDEF payload this intent is started before
75 * {@link #ACTION_TECH_DISCOVERED}. If any activities respond to this intent neither
Nick Pellyf003e262011-01-31 23:27:37 -080076 * {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started.
Nick Pellyc97a5522012-01-05 15:13:01 +110077 *
78 * <p>The MIME type or data URI of this intent are normalized before dispatch -
79 * so that MIME, URI scheme and URI host are always lower-case.
Jeff Hamilton641dd622010-12-02 09:16:22 -060080 */
81 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
82 public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED";
83
84 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +090085 * Intent to start an activity when a tag is discovered and activities are registered for the
86 * specific technologies on the tag.
Jeff Hamilton641dd622010-12-02 09:16:22 -060087 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090088 * <p>To receive this intent an activity must include an intent filter
89 * for this action and specify the desired tech types in a
90 * manifest <code>meta-data</code> entry. Here is an example manfiest entry:
91 * <pre>
Scott Main97e0a1c2012-06-25 11:22:10 -070092 * &lt;activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"&gt;
93 * &lt;!-- Add a technology filter --&gt;
94 * &lt;intent-filter&gt;
95 * &lt;action android:name="android.nfc.action.TECH_DISCOVERED" /&gt;
96 * &lt;/intent-filter&gt;
Jeff Hamilton641dd622010-12-02 09:16:22 -060097 *
Scott Main97e0a1c2012-06-25 11:22:10 -070098 * &lt;meta-data android:name="android.nfc.action.TECH_DISCOVERED"
99 * android:resource="@xml/filter_nfc"
100 * /&gt;
101 * &lt;/activity&gt;</pre>
Jeff Hamilton28319c02011-02-09 17:26:47 +0900102 *
103 * <p>The meta-data XML file should contain one or more <code>tech-list</code> entries
104 * each consisting or one or more <code>tech</code> entries. The <code>tech</code> entries refer
105 * to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA".
106 *
107 * <p>A tag matches if any of the
108 * <code>tech-list</code> sets is a subset of {@link Tag#getTechList() Tag.getTechList()}. Each
109 * of the <code>tech-list</code>s is considered independently and the
110 * activity is considered a match is any single <code>tech-list</code> matches the tag that was
111 * discovered. This provides AND and OR semantics for filtering desired techs. Here is an
112 * example that will match any tag using {@link NfcF} or any tag using {@link NfcA},
113 * {@link MifareClassic}, and {@link Ndef}:
114 *
115 * <pre>
116 * &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
117 * &lt;!-- capture anything using NfcF --&gt;
118 * &lt;tech-list&gt;
119 * &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
120 * &lt;/tech-list&gt;
121 *
122 * &lt;!-- OR --&gt;
123 *
124 * &lt;!-- capture all MIFARE Classics with NDEF payloads --&gt;
125 * &lt;tech-list&gt;
126 * &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
127 * &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
128 * &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
129 * &lt;/tech-list&gt;
Scott Main97e0a1c2012-06-25 11:22:10 -0700130 * &lt;/resources&gt;</pre>
Jeff Hamilton28319c02011-02-09 17:26:47 +0900131 *
132 * <p>This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before
133 * {@link #ACTION_TAG_DISCOVERED}. If any activities respond to {@link #ACTION_NDEF_DISCOVERED}
134 * this intent will not be started. If any activities respond to this intent
135 * {@link #ACTION_TAG_DISCOVERED} will not be started.
Jeff Hamilton641dd622010-12-02 09:16:22 -0600136 */
137 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Nick Pellyf003e262011-01-31 23:27:37 -0800138 public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED";
Jeff Hamilton641dd622010-12-02 09:16:22 -0600139
140 /**
Nick Pelly11b075e2010-10-28 13:39:37 -0700141 * Intent to start an activity when a tag is discovered.
Jeff Hamilton28319c02011-02-09 17:26:47 +0900142 *
143 * <p>This intent will not be started when a tag is discovered if any activities respond to
Jason parks01425362011-05-24 02:57:37 -0700144 * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag.
Nick Pelly590b73b2010-10-12 13:00:50 -0700145 */
146 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
147 public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED";
148
149 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600150 * Broadcast to only the activity that handles ACTION_TAG_DISCOVERED
151 * @hide
152 */
153 public static final String ACTION_TAG_LEFT_FIELD = "android.nfc.action.TAG_LOST";
154
155 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900156 * Mandatory extra containing the {@link Tag} that was discovered for the
157 * {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
158 * {@link #ACTION_TAG_DISCOVERED} intents.
Nick Pelly590b73b2010-10-12 13:00:50 -0700159 */
160 public static final String EXTRA_TAG = "android.nfc.extra.TAG";
161
162 /**
Nick Pellyc97a5522012-01-05 15:13:01 +1100163 * Extra containing an array of {@link NdefMessage} present on the discovered tag.<p>
164 * This extra is mandatory for {@link #ACTION_NDEF_DISCOVERED} intents,
165 * and optional for {@link #ACTION_TECH_DISCOVERED}, and
166 * {@link #ACTION_TAG_DISCOVERED} intents.<p>
167 * When this extra is present there will always be at least one
168 * {@link NdefMessage} element. Most NDEF tags have only one NDEF message,
169 * but we use an array for future compatibility.
Nick Pelly11b075e2010-10-28 13:39:37 -0700170 */
171 public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES";
172
173 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900174 * Optional extra containing a byte array containing the ID of the discovered tag for
175 * the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
176 * {@link #ACTION_TAG_DISCOVERED} intents.
Nick Pelly11b075e2010-10-28 13:39:37 -0700177 */
178 public static final String EXTRA_ID = "android.nfc.extra.ID";
179
180 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700181 * Broadcast Action: The state of the local NFC adapter has been
182 * changed.
183 * <p>For example, NFC has been turned on or off.
Martijn Coenen4ba5eaf2013-01-08 12:49:47 -0800184 * <p>Always contains the extra field {@link #EXTRA_ADAPTER_STATE}
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700185 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700186 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
187 public static final String ACTION_ADAPTER_STATE_CHANGED =
188 "android.nfc.action.ADAPTER_STATE_CHANGED";
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700189
190 /**
Martijn Coenen4ba5eaf2013-01-08 12:49:47 -0800191 * Used as an int extra field in {@link #ACTION_ADAPTER_STATE_CHANGED}
Nick Pelly8d32a012011-08-09 07:03:49 -0700192 * intents to request the current power state. Possible values are:
193 * {@link #STATE_OFF},
194 * {@link #STATE_TURNING_ON},
195 * {@link #STATE_ON},
196 * {@link #STATE_TURNING_OFF},
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700197 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700198 public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE";
199
Nick Pelly8d32a012011-08-09 07:03:49 -0700200 public static final int STATE_OFF = 1;
Nick Pelly8d32a012011-08-09 07:03:49 -0700201 public static final int STATE_TURNING_ON = 2;
Nick Pelly8d32a012011-08-09 07:03:49 -0700202 public static final int STATE_ON = 3;
Nick Pelly8d32a012011-08-09 07:03:49 -0700203 public static final int STATE_TURNING_OFF = 4;
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700204
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700205 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700206 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700207 * <p>
208 * Setting this flag enables polling for Nfc-A technology.
209 */
210 public static final int FLAG_READER_NFC_A = 0x1;
211
212 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700213 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700214 * <p>
215 * Setting this flag enables polling for Nfc-B technology.
216 */
217 public static final int FLAG_READER_NFC_B = 0x2;
218
219 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700220 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700221 * <p>
222 * Setting this flag enables polling for Nfc-F technology.
223 */
224 public static final int FLAG_READER_NFC_F = 0x4;
225
226 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700227 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700228 * <p>
229 * Setting this flag enables polling for Nfc-V (ISO15693) technology.
230 */
231 public static final int FLAG_READER_NFC_V = 0x8;
232
233 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700234 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700235 * <p>
Martijn Coenenb523bfa2013-09-12 13:44:39 +0200236 * Setting this flag enables polling for NfcBarcode technology.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700237 */
Martijn Coenenb523bfa2013-09-12 13:44:39 +0200238 public static final int FLAG_READER_NFC_BARCODE = 0x10;
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700239
240 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700241 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700242 * <p>
243 * Setting this flag allows the caller to prevent the
244 * platform from performing an NDEF check on the tags it
245 * finds.
246 */
247 public static final int FLAG_READER_SKIP_NDEF_CHECK = 0x80;
248
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700249 /**
250 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
251 * <p>
252 * Setting this flag allows the caller to prevent the
253 * platform from playing sounds when it discovers a tag.
254 */
255 public static final int FLAG_READER_NO_PLATFORM_SOUNDS = 0x100;
256
257 /**
258 * Int Extra for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
259 * <p>
260 * Setting this integer extra allows the calling application to specify
261 * the delay that the platform will use for performing presence checks
262 * on any discovered tag.
263 */
264 public static final String EXTRA_READER_PRESENCE_CHECK_DELAY = "presence";
265
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700266 /** @hide */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700267 @SystemApi
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800268 public static final int FLAG_NDEF_PUSH_NO_CONFIRM = 0x1;
269
270 /** @hide */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700271 public static final String ACTION_HANDOVER_TRANSFER_STARTED =
272 "android.nfc.action.HANDOVER_TRANSFER_STARTED";
273
274 /** @hide */
275 public static final String ACTION_HANDOVER_TRANSFER_DONE =
276 "android.nfc.action.HANDOVER_TRANSFER_DONE";
277
278 /** @hide */
279 public static final String EXTRA_HANDOVER_TRANSFER_STATUS =
280 "android.nfc.extra.HANDOVER_TRANSFER_STATUS";
281
282 /** @hide */
283 public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
284 /** @hide */
285 public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;
286
287 /** @hide */
288 public static final String EXTRA_HANDOVER_TRANSFER_URI =
289 "android.nfc.extra.HANDOVER_TRANSFER_URI";
290
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800291 // Guarded by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700292 static boolean sIsInitialized = false;
Nick Pelly590b73b2010-10-12 13:00:50 -0700293
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800294 // Final after first constructor, except for
295 // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
296 // recovery
Nick Pellyc84c89a2011-08-22 22:27:11 -0700297 static INfcAdapter sService;
298 static INfcTag sTagService;
Martijn Coenena7397882013-07-30 20:07:47 -0700299 static INfcCardEmulation sCardEmulationService;
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900300 static INfcFCardEmulation sNfcFCardEmulationService;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700301
302 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600303 * The NfcAdapter object for each application context.
304 * There is a 1-1 relationship between application context and
305 * NfcAdapter object.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700306 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600307 static HashMap<Context, NfcAdapter> sNfcAdapters = new HashMap(); //guard by NfcAdapter.class
308
309 /**
310 * NfcAdapter used with a null context. This ctor was deprecated but we have
311 * to support it for backwards compatibility. New methods that require context
312 * might throw when called on the null-context NfcAdapter.
313 */
314 static NfcAdapter sNullContextNfcAdapter; // protected by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700315
316 final NfcActivityManager mNfcActivityManager;
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600317 final Context mContext;
Andres Moralesf9a97942014-08-14 15:43:30 -0700318 final HashMap<NfcUnlockHandler, INfcUnlockHandler> mNfcUnlockHandlers;
Andres Morales11d2e532014-07-23 12:39:55 -0700319 final Object mLock;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700320
Martijn Coenend55def82016-04-14 15:19:31 +0200321 ITagRemovedCallback mTagRemovedListener; // protected by mLock
322
Nick Pellyc84c89a2011-08-22 22:27:11 -0700323 /**
Scott Mainb38ad762013-10-17 11:57:06 -0700324 * A callback to be invoked when the system finds a tag while the foreground activity is
325 * operating in reader mode.
326 * <p>Register your {@code ReaderCallback} implementation with {@link
327 * NfcAdapter#enableReaderMode} and disable it with {@link
328 * NfcAdapter#disableReaderMode}.
329 * @see NfcAdapter#enableReaderMode
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700330 */
331 public interface ReaderCallback {
332 public void onTagDiscovered(Tag tag);
333 }
334
335 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700336 * A callback to be invoked when the system successfully delivers your {@link NdefMessage}
337 * to another device.
338 * @see #setOnNdefPushCompleteCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700339 */
340 public interface OnNdefPushCompleteCallback {
341 /**
342 * Called on successful NDEF push.
343 *
344 * <p>This callback is usually made on a binder thread (not the UI thread).
345 *
346 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
Scott Main2d68a6b2011-09-26 22:59:38 -0700347 * @see #setNdefPushMessageCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700348 */
349 public void onNdefPushComplete(NfcEvent event);
350 }
351
352 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700353 * A callback to be invoked when another NFC device capable of NDEF push (Android Beam)
354 * is within range.
355 * <p>Implement this interface and pass it to {@link
356 * NfcAdapter#setNdefPushMessageCallback setNdefPushMessageCallback()} in order to create an
357 * {@link NdefMessage} at the moment that another device is within range for NFC. Using this
358 * callback allows you to create a message with data that might vary based on the
359 * content currently visible to the user. Alternatively, you can call {@link
360 * #setNdefPushMessage setNdefPushMessage()} if the {@link NdefMessage} always contains the
361 * same data.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700362 */
363 public interface CreateNdefMessageCallback {
364 /**
365 * Called to provide a {@link NdefMessage} to push.
366 *
367 * <p>This callback is usually made on a binder thread (not the UI thread).
368 *
369 * <p>Called when this device is in range of another device
370 * that might support NDEF push. It allows the application to
371 * create the NDEF message only when it is required.
372 *
373 * <p>NDEF push cannot occur until this method returns, so do not
374 * block for too long.
375 *
376 * <p>The Android operating system will usually show a system UI
377 * on top of your activity during this time, so do not try to request
378 * input from the user to complete the callback, or provide custom NDEF
379 * push UI. The user probably will not see it.
380 *
381 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
382 * @return NDEF message to push, or null to not provide a message
383 */
384 public NdefMessage createNdefMessage(NfcEvent event);
385 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700386
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700387
388 // TODO javadoc
389 public interface CreateBeamUrisCallback {
390 public Uri[] createBeamUris(NfcEvent event);
391 }
392
Nick Pelly590b73b2010-10-12 13:00:50 -0700393 /**
Martijn Coenend55def82016-04-14 15:19:31 +0200394 * A callback that is invoked when a tag is removed from the field.
395 */
396 public interface OnTagRemovedListener {
397 void onTagRemoved();
398 }
399
400 /**
Andres Morales11d2e532014-07-23 12:39:55 -0700401 * A callback to be invoked when an application has registered as a
402 * handler to unlock the device given an NFC tag at the lockscreen.
403 * @hide
404 */
405 @SystemApi
406 public interface NfcUnlockHandler {
407 /**
408 * Called at the lock screen to attempt to unlock the device with the given tag.
409 * @param tag the detected tag, to be used to unlock the device
410 * @return true if the device was successfully unlocked
411 */
412 public boolean onUnlockAttempted(Tag tag);
413 }
414
415
416 /**
Nick Pellyfdf90862010-10-18 11:19:41 -0700417 * Helper to check if this device has FEATURE_NFC, but without using
418 * a context.
419 * Equivalent to
420 * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)
421 */
422 private static boolean hasNfcFeature() {
423 IPackageManager pm = ActivityThread.getPackageManager();
424 if (pm == null) {
425 Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
426 return false;
427 }
428 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700429 return pm.hasSystemFeature(PackageManager.FEATURE_NFC, 0);
Nick Pellyfdf90862010-10-18 11:19:41 -0700430 } catch (RemoteException e) {
431 Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
432 return false;
433 }
434 }
435
Nick Pellyc84c89a2011-08-22 22:27:11 -0700436 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600437 * Returns the NfcAdapter for application context,
438 * or throws if NFC is not available.
439 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700440 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600441 public static synchronized NfcAdapter getNfcAdapter(Context context) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800442 if (!sIsInitialized) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800443 /* is this device meant to have NFC */
444 if (!hasNfcFeature()) {
445 Log.v(TAG, "this device does not have NFC support");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700446 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800447 }
448
449 sService = getServiceInterface();
450 if (sService == null) {
451 Log.e(TAG, "could not retrieve NFC service");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700452 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800453 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100454 try {
455 sTagService = sService.getNfcTagInterface();
456 } catch (RemoteException e) {
457 Log.e(TAG, "could not retrieve NFC Tag service");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700458 throw new UnsupportedOperationException();
Nick Pelly3dd6c452011-01-10 18:14:41 +1100459 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600460
Martijn Coenena7397882013-07-30 20:07:47 -0700461 try {
462 sCardEmulationService = sService.getNfcCardEmulationInterface();
463 } catch (RemoteException e) {
464 Log.e(TAG, "could not retrieve card emulation service");
465 throw new UnsupportedOperationException();
466 }
467
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900468 try {
469 sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface();
470 } catch (RemoteException e) {
471 Log.e(TAG, "could not retrieve NFC-F card emulation service");
472 throw new UnsupportedOperationException();
473 }
474
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600475 sIsInitialized = true;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800476 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600477 if (context == null) {
478 if (sNullContextNfcAdapter == null) {
479 sNullContextNfcAdapter = new NfcAdapter(null);
480 }
481 return sNullContextNfcAdapter;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700482 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600483 NfcAdapter adapter = sNfcAdapters.get(context);
484 if (adapter == null) {
485 adapter = new NfcAdapter(context);
486 sNfcAdapters.put(context, adapter);
487 }
488 return adapter;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800489 }
490
Nick Pelly6d55e132010-10-27 01:14:43 -0700491 /** get handle to NFC service interface */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800492 private static INfcAdapter getServiceInterface() {
Nick Pelly6d55e132010-10-27 01:14:43 -0700493 /* get a handle to NFC service */
494 IBinder b = ServiceManager.getService("nfc");
495 if (b == null) {
496 return null;
497 }
498 return INfcAdapter.Stub.asInterface(b);
499 }
500
Nick Pellyfdf90862010-10-18 11:19:41 -0700501 /**
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800502 * Helper to get the default NFC Adapter.
503 * <p>
504 * Most Android devices will only have one NFC Adapter (NFC Controller).
505 * <p>
506 * This helper is the equivalent of:
Scott Main97e0a1c2012-06-25 11:22:10 -0700507 * <pre>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800508 * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Scott Main97e0a1c2012-06-25 11:22:10 -0700509 * NfcAdapter adapter = manager.getDefaultAdapter();</pre>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800510 * @param context the calling application's context
511 *
512 * @return the default NFC adapter, or null if no NFC adapter exists
513 */
514 public static NfcAdapter getDefaultAdapter(Context context) {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600515 if (context == null) {
516 throw new IllegalArgumentException("context cannot be null");
517 }
518 context = context.getApplicationContext();
Nick Pellyb04cce02011-11-21 17:02:02 -0800519 if (context == null) {
520 throw new IllegalArgumentException(
521 "context not associated with any application (using a mock context?)");
522 }
523 /* use getSystemService() for consistency */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800524 NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Nick Pellya5193b242011-11-16 16:46:27 -0800525 if (manager == null) {
526 // NFC not available
527 return null;
528 }
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800529 return manager.getDefaultAdapter();
530 }
531
532 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600533 * Legacy NfcAdapter getter, always use {@link #getDefaultAdapter(Context)} instead.<p>
534 * This method was deprecated at API level 10 (Gingerbread MR1) because a context is required
535 * for many NFC API methods. Those methods will fail when called on an NfcAdapter
536 * object created from this method.<p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800537 * @deprecated use {@link #getDefaultAdapter(Context)}
Nick Pellya356bf12011-12-13 15:36:31 -0800538 * @hide
Nick Pelly590b73b2010-10-12 13:00:50 -0700539 */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800540 @Deprecated
Nick Pelly590b73b2010-10-12 13:00:50 -0700541 public static NfcAdapter getDefaultAdapter() {
Nick Pellyc97a5522012-01-05 15:13:01 +1100542 // introduced in API version 9 (GB 2.3)
Nick Pellya356bf12011-12-13 15:36:31 -0800543 // deprecated in API version 10 (GB 2.3.3)
544 // removed from public API in version 16 (ICS MR2)
Nick Pellyc97a5522012-01-05 15:13:01 +1100545 // should maintain as a hidden API for binary compatibility for a little longer
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800546 Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " +
547 "NfcAdapter.getDefaultAdapter(Context) instead", new Exception());
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600548
549 return NfcAdapter.getNfcAdapter(null);
550 }
551
552 NfcAdapter(Context context) {
553 mContext = context;
554 mNfcActivityManager = new NfcActivityManager(this);
Andres Moralesf9a97942014-08-14 15:43:30 -0700555 mNfcUnlockHandlers = new HashMap<NfcUnlockHandler, INfcUnlockHandler>();
Martijn Coenend55def82016-04-14 15:19:31 +0200556 mTagRemovedListener = null;
Andres Morales11d2e532014-07-23 12:39:55 -0700557 mLock = new Object();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800558 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700559
Nick Pellyc84c89a2011-08-22 22:27:11 -0700560 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600561 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700562 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600563 public Context getContext() {
564 return mContext;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800565 }
566
567 /**
568 * Returns the binder interface to the service.
569 * @hide
570 */
571 public INfcAdapter getService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800572 isEnabled(); // NOP call to recover sService if it is stale
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800573 return sService;
Nick Pelly590b73b2010-10-12 13:00:50 -0700574 }
575
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600576 /**
Nick Pelly3dd6c452011-01-10 18:14:41 +1100577 * Returns the binder interface to the tag service.
578 * @hide
579 */
580 public INfcTag getTagService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800581 isEnabled(); // NOP call to recover sTagService if it is stale
Nick Pelly3dd6c452011-01-10 18:14:41 +1100582 return sTagService;
583 }
584
585 /**
Martijn Coenena7397882013-07-30 20:07:47 -0700586 * Returns the binder interface to the card emulation service.
587 * @hide
588 */
589 public INfcCardEmulation getCardEmulationService() {
590 isEnabled();
591 return sCardEmulationService;
592 }
593
594 /**
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900595 * Returns the binder interface to the NFC-F card emulation service.
596 * @hide
597 */
598 public INfcFCardEmulation getNfcFCardEmulationService() {
599 isEnabled();
600 return sNfcFCardEmulationService;
601 }
602
603 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600604 * NFC service dead - attempt best effort recovery
605 * @hide
606 */
607 public void attemptDeadServiceRecovery(Exception e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700608 Log.e(TAG, "NFC service dead - attempting to recover", e);
609 INfcAdapter service = getServiceInterface();
610 if (service == null) {
611 Log.e(TAG, "could not retrieve NFC service during service recovery");
Nick Pelly3dd6c452011-01-10 18:14:41 +1100612 // nothing more can be done now, sService is still stale, we'll hit
613 // this recovery path again later
Nick Pelly6d55e132010-10-27 01:14:43 -0700614 return;
615 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100616 // assigning to sService is not thread-safe, but this is best-effort code
617 // and on a well-behaved system should never happen
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800618 sService = service;
Nick Pelly3dd6c452011-01-10 18:14:41 +1100619 try {
620 sTagService = service.getNfcTagInterface();
621 } catch (RemoteException ee) {
622 Log.e(TAG, "could not retrieve NFC tag service during service recovery");
623 // nothing more can be done now, sService is still stale, we'll hit
624 // this recovery path again later
Martijn Coenena7397882013-07-30 20:07:47 -0700625 return;
626 }
627
628 try {
629 sCardEmulationService = service.getNfcCardEmulationInterface();
630 } catch (RemoteException ee) {
631 Log.e(TAG, "could not retrieve NFC card emulation service during service recovery");
Nick Pelly3dd6c452011-01-10 18:14:41 +1100632 }
633
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900634 try {
635 sNfcFCardEmulationService = service.getNfcFCardEmulationInterface();
636 } catch (RemoteException ee) {
637 Log.e(TAG, "could not retrieve NFC-F card emulation service during service recovery");
638 }
639
Nick Pelly6d55e132010-10-27 01:14:43 -0700640 return;
641 }
642
Nick Pelly590b73b2010-10-12 13:00:50 -0700643 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700644 * Return true if this NFC Adapter has any features enabled.
Nick Pelly590b73b2010-10-12 13:00:50 -0700645 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800646 * <p>If this method returns false, the NFC hardware is guaranteed not to
Nick Pellycccf01d2011-10-31 14:49:40 -0700647 * generate or respond to any NFC communication over its NFC radio.
648 * <p>Applications can use this to check if NFC is enabled. Applications
649 * can request Settings UI allowing the user to toggle NFC using:
650 * <p><pre>startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))</pre>
Nick Pelly74fe6c62011-02-02 22:37:40 -0800651 *
Nick Pellycccf01d2011-10-31 14:49:40 -0700652 * @see android.provider.Settings#ACTION_NFC_SETTINGS
Nick Pelly74fe6c62011-02-02 22:37:40 -0800653 * @return true if this NFC Adapter has any features enabled
Nick Pelly590b73b2010-10-12 13:00:50 -0700654 */
Nick Pelly7ea5c452010-10-20 18:39:11 -0700655 public boolean isEnabled() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700656 try {
Nick Pelly8d32a012011-08-09 07:03:49 -0700657 return sService.getState() == STATE_ON;
Nick Pelly590b73b2010-10-12 13:00:50 -0700658 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700659 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700660 return false;
661 }
662 }
663
664 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700665 * Return the state of this NFC Adapter.
666 *
667 * <p>Returns one of {@link #STATE_ON}, {@link #STATE_TURNING_ON},
668 * {@link #STATE_OFF}, {@link #STATE_TURNING_OFF}.
669 *
670 * <p>{@link #isEnabled()} is equivalent to
671 * <code>{@link #getAdapterState()} == {@link #STATE_ON}</code>
672 *
673 * @return the current state of this NFC adapter
674 *
675 * @hide
676 */
677 public int getAdapterState() {
678 try {
679 return sService.getState();
680 } catch (RemoteException e) {
681 attemptDeadServiceRecovery(e);
682 return NfcAdapter.STATE_OFF;
683 }
684 }
685
686 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700687 * Enable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700688 *
689 * <p>This call is asynchronous. Listen for
690 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
691 * operation is complete.
692 *
693 * <p>If this returns true, then either NFC is already on, or
694 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
695 * to indicate a state transition. If this returns false, then
696 * there is some problem that prevents an attempt to turn
697 * NFC on (for example we are in airplane mode and NFC is not
698 * toggleable in airplane mode on this platform).
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700699 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700700 * @hide
701 */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700702 @SystemApi
Nick Pelly7ea5c452010-10-20 18:39:11 -0700703 public boolean enable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700704 try {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800705 return sService.enable();
Nick Pelly590b73b2010-10-12 13:00:50 -0700706 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700707 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700708 return false;
709 }
710 }
711
712 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700713 * Disable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700714 *
715 * <p>No NFC features will work after this call, and the hardware
Nick Pelly7ea5c452010-10-20 18:39:11 -0700716 * will not perform or respond to any NFC communication.
Nick Pelly8d32a012011-08-09 07:03:49 -0700717 *
718 * <p>This call is asynchronous. Listen for
719 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
720 * operation is complete.
721 *
722 * <p>If this returns true, then either NFC is already off, or
723 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
724 * to indicate a state transition. If this returns false, then
725 * there is some problem that prevents an attempt to turn
726 * NFC off.
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700727 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700728 * @hide
729 */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700730 @SystemApi
Nick Pelly7ea5c452010-10-20 18:39:11 -0700731 public boolean disable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700732 try {
Sunil Jogi3bba8d02012-04-10 13:12:26 -0700733 return sService.disable(true);
Nick Pelly590b73b2010-10-12 13:00:50 -0700734 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700735 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700736 return false;
737 }
738 }
739
Martijn Coenen2c103112012-05-15 10:32:15 -0700740 /**
Martijn Coenen56a3e672014-09-23 21:27:30 -0700741 * Disable NFC hardware.
742 * @hide
743 */
744 @SystemApi
745 public boolean disable(boolean persist) {
746 try {
747 return sService.disable(persist);
748 } catch (RemoteException e) {
749 attemptDeadServiceRecovery(e);
750 return false;
751 }
752 }
753
754 /**
Andres Morales9c4f4002014-09-08 17:09:16 -0700755 * Pauses polling for a {@code timeoutInMs} millis. If polling must be resumed before timeout,
756 * use {@link #resumePolling()}.
757 * @hide
758 */
759 public void pausePolling(int timeoutInMs) {
760 try {
761 sService.pausePolling(timeoutInMs);
762 } catch (RemoteException e) {
763 attemptDeadServiceRecovery(e);
764 }
765 }
766
767 /**
768 * Resumes default polling for the current device state if polling is paused. Calling
769 * this while polling is not paused is a no-op.
770 *
771 * @hide
772 */
773 public void resumePolling() {
774 try {
775 sService.resumePolling();
776 } catch (RemoteException e) {
777 attemptDeadServiceRecovery(e);
778 }
779 }
780
781 /**
Martijn Coenen2c103112012-05-15 10:32:15 -0700782 * Set one or more {@link Uri}s to send using Android Beam (TM). Every
783 * Uri you provide must have either scheme 'file' or scheme 'content'.
784 *
785 * <p>For the data provided through this method, Android Beam tries to
786 * switch to alternate transports such as Bluetooth to achieve a fast
787 * transfer speed. Hence this method is very suitable
788 * for transferring large files such as pictures or songs.
789 *
790 * <p>The receiving side will store the content of each Uri in
791 * a file and present a notification to the user to open the file
792 * with a {@link android.content.Intent} with action
793 * {@link android.content.Intent#ACTION_VIEW}.
794 * If multiple URIs are sent, the {@link android.content.Intent} will refer
795 * to the first of the stored files.
796 *
797 * <p>This method may be called at any time before {@link Activity#onDestroy},
798 * but the URI(s) are only made available for Android Beam when the
799 * specified activity(s) are in resumed (foreground) state. The recommended
800 * approach is to call this method during your Activity's
801 * {@link Activity#onCreate} - see sample
802 * code below. This method does not immediately perform any I/O or blocking work,
803 * so is safe to call on your main thread.
804 *
805 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
806 * have priority over both {@link #setNdefPushMessage} and
807 * {@link #setNdefPushMessageCallback}.
808 *
809 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
810 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
811 * then the Uri push will be completely disabled for the specified activity(s).
812 *
813 * <p>Code example:
814 * <pre>
815 * protected void onCreate(Bundle savedInstanceState) {
816 * super.onCreate(savedInstanceState);
817 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
818 * if (nfcAdapter == null) return; // NFC not available on this device
819 * nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700820 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700821 * And that is it. Only one call per activity is necessary. The Android
822 * OS will automatically release its references to the Uri(s) and the
823 * Activity object when it is destroyed if you follow this pattern.
824 *
825 * <p>If your Activity wants to dynamically supply Uri(s),
826 * then set a callback using {@link #setBeamPushUrisCallback} instead
827 * of using this method.
828 *
829 * <p class="note">Do not pass in an Activity that has already been through
830 * {@link Activity#onDestroy}. This is guaranteed if you call this API
831 * during {@link Activity#onCreate}.
832 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700833 * <p class="note">If this device does not support alternate transports
834 * such as Bluetooth or WiFI, calling this method does nothing.
835 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700836 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
837 *
838 * @param uris an array of Uri(s) to push over Android Beam
839 * @param activity activity for which the Uri(s) will be pushed
840 */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700841 public void setBeamPushUris(Uri[] uris, Activity activity) {
Nick Pelly1d7e9062012-04-03 17:46:00 -0700842 if (activity == null) {
843 throw new NullPointerException("activity cannot be null");
844 }
Martijn Coenen2c103112012-05-15 10:32:15 -0700845 if (uris != null) {
846 for (Uri uri : uris) {
847 if (uri == null) throw new NullPointerException("Uri not " +
848 "allowed to be null");
849 String scheme = uri.getScheme();
850 if (scheme == null || (!scheme.equalsIgnoreCase("file") &&
851 !scheme.equalsIgnoreCase("content"))) {
852 throw new IllegalArgumentException("URI needs to have " +
853 "either scheme file or scheme content");
854 }
855 }
856 }
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700857 mNfcActivityManager.setNdefPushContentUri(activity, uris);
858 }
859
Martijn Coenen2c103112012-05-15 10:32:15 -0700860 /**
861 * Set a callback that will dynamically generate one or more {@link Uri}s
862 * to send using Android Beam (TM). Every Uri the callback provides
863 * must have either scheme 'file' or scheme 'content'.
864 *
865 * <p>For the data provided through this callback, Android Beam tries to
866 * switch to alternate transports such as Bluetooth to achieve a fast
867 * transfer speed. Hence this method is very suitable
868 * for transferring large files such as pictures or songs.
869 *
870 * <p>The receiving side will store the content of each Uri in
871 * a file and present a notification to the user to open the file
872 * with a {@link android.content.Intent} with action
873 * {@link android.content.Intent#ACTION_VIEW}.
874 * If multiple URIs are sent, the {@link android.content.Intent} will refer
875 * to the first of the stored files.
876 *
877 * <p>This method may be called at any time before {@link Activity#onDestroy},
878 * but the URI(s) are only made available for Android Beam when the
879 * specified activity(s) are in resumed (foreground) state. The recommended
880 * approach is to call this method during your Activity's
881 * {@link Activity#onCreate} - see sample
882 * code below. This method does not immediately perform any I/O or blocking work,
883 * so is safe to call on your main thread.
884 *
885 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
886 * have priority over both {@link #setNdefPushMessage} and
887 * {@link #setNdefPushMessageCallback}.
888 *
889 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
890 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
891 * then the Uri push will be completely disabled for the specified activity(s).
892 *
893 * <p>Code example:
894 * <pre>
895 * protected void onCreate(Bundle savedInstanceState) {
896 * super.onCreate(savedInstanceState);
897 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
898 * if (nfcAdapter == null) return; // NFC not available on this device
899 * nfcAdapter.setBeamPushUrisCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700900 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700901 * And that is it. Only one call per activity is necessary. The Android
902 * OS will automatically release its references to the Uri(s) and the
903 * Activity object when it is destroyed if you follow this pattern.
904 *
905 * <p class="note">Do not pass in an Activity that has already been through
906 * {@link Activity#onDestroy}. This is guaranteed if you call this API
907 * during {@link Activity#onCreate}.
908 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700909 * <p class="note">If this device does not support alternate transports
910 * such as Bluetooth or WiFI, calling this method does nothing.
911 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700912 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
913 *
914 * @param callback callback, or null to disable
915 * @param activity activity for which the Uri(s) will be pushed
916 */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700917 public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) {
918 if (activity == null) {
919 throw new NullPointerException("activity cannot be null");
920 }
921 mNfcActivityManager.setNdefPushContentUriCallback(activity, callback);
Nick Pelly1d7e9062012-04-03 17:46:00 -0700922 }
923
Jeff Hamilton52d32032011-01-08 15:31:26 -0600924 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -0700925 * Set a static {@link NdefMessage} to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -0700926 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700927 * <p>This method may be called at any time before {@link Activity#onDestroy},
928 * but the NDEF message is only made available for NDEF push when the
929 * specified activity(s) are in resumed (foreground) state. The recommended
930 * approach is to call this method during your Activity's
931 * {@link Activity#onCreate} - see sample
932 * code below. This method does not immediately perform any I/O or blocking work,
933 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700934 *
935 * <p>Only one NDEF message can be pushed by the currently resumed activity.
936 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -0700937 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -0700938 * the callback will take priority.
939 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700940 * <p>If neither {@link #setNdefPushMessage} or
941 * {@link #setNdefPushMessageCallback} have been called for your activity, then
942 * the Android OS may choose to send a default NDEF message on your behalf,
943 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700944 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700945 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
946 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
947 * then NDEF push will be completely disabled for the specified activity(s).
948 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -0700949 * otherwise sent on your behalf for those activity(s).
950 *
951 * <p>If you want to prevent the Android OS from sending default NDEF
952 * messages completely (for all activities), you can include a
Neil Fuller9498e8a2015-11-30 09:51:33 +0000953 * {@code <meta-data>} element inside the {@code <application>}
Martijn Coenen854e0772012-04-02 14:52:10 -0700954 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -0700955 * <pre>
956 * &lt;application ...>
957 * &lt;meta-data android:name="android.nfc.disable_beam_default"
958 * android:value="true" />
959 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700960 *
961 * <p>The API allows for multiple activities to be specified at a time,
962 * but it is strongly recommended to just register one at a time,
963 * and to do so during the activity's {@link Activity#onCreate}. For example:
964 * <pre>
965 * protected void onCreate(Bundle savedInstanceState) {
966 * super.onCreate(savedInstanceState);
967 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
968 * if (nfcAdapter == null) return; // NFC not available on this device
969 * nfcAdapter.setNdefPushMessage(ndefMessage, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700970 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700971 * And that is it. Only one call per activity is necessary. The Android
972 * OS will automatically release its references to the NDEF message and the
973 * Activity object when it is destroyed if you follow this pattern.
974 *
975 * <p>If your Activity wants to dynamically generate an NDEF message,
976 * then set a callback using {@link #setNdefPushMessageCallback} instead
977 * of a static message.
978 *
979 * <p class="note">Do not pass in an Activity that has already been through
980 * {@link Activity#onDestroy}. This is guaranteed if you call this API
981 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -0700982 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700983 * <p class="note">For sending large content such as pictures and songs,
984 * consider using {@link #setBeamPushUris}, which switches to alternate transports
985 * such as Bluetooth to achieve a fast transfer rate.
986 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700987 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
988 *
989 * @param message NDEF message to push over NFC, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -0700990 * @param activity activity for which the NDEF message will be pushed
991 * @param activities optional additional activities, however we strongly recommend
992 * to only register one at a time, and to do so in that activity's
993 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -0700994 */
Nick Pelly82328bf2011-08-30 09:37:25 -0700995 public void setNdefPushMessage(NdefMessage message, Activity activity,
996 Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -0700997 int targetSdkVersion = getSdkVersion();
998 try {
999 if (activity == null) {
1000 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001001 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001002 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001003 for (Activity a : activities) {
1004 if (a == null) {
1005 throw new NullPointerException("activities cannot contain null");
1006 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001007 mNfcActivityManager.setNdefPushMessage(a, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001008 }
1009 } catch (IllegalStateException e) {
1010 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1011 // Less strict on old applications - just log the error
1012 Log.e(TAG, "Cannot call API with Activity that has already " +
1013 "been destroyed", e);
1014 } else {
1015 // Prevent new applications from making this mistake, re-throw
1016 throw(e);
1017 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001018 }
1019 }
1020
1021 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001022 * @hide
1023 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001024 @SystemApi
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001025 public void setNdefPushMessage(NdefMessage message, Activity activity, int flags) {
1026 if (activity == null) {
1027 throw new NullPointerException("activity cannot be null");
1028 }
1029 mNfcActivityManager.setNdefPushMessage(activity, message, flags);
1030 }
1031
1032 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -07001033 * Set a callback that dynamically generates NDEF messages to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -07001034 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001035 * <p>This method may be called at any time before {@link Activity#onDestroy},
1036 * but the NDEF message callback can only occur when the
1037 * specified activity(s) are in resumed (foreground) state. The recommended
1038 * approach is to call this method during your Activity's
1039 * {@link Activity#onCreate} - see sample
1040 * code below. This method does not immediately perform any I/O or blocking work,
1041 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001042 *
1043 * <p>Only one NDEF message can be pushed by the currently resumed activity.
1044 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -07001045 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -07001046 * the callback will take priority.
1047 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001048 * <p>If neither {@link #setNdefPushMessage} or
1049 * {@link #setNdefPushMessageCallback} have been called for your activity, then
1050 * the Android OS may choose to send a default NDEF message on your behalf,
1051 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001052 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001053 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
1054 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
1055 * then NDEF push will be completely disabled for the specified activity(s).
1056 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -07001057 * otherwise sent on your behalf for those activity(s).
1058 *
1059 * <p>If you want to prevent the Android OS from sending default NDEF
1060 * messages completely (for all activities), you can include a
Neil Fuller9498e8a2015-11-30 09:51:33 +00001061 * {@code <meta-data>} element inside the {@code <application>}
Martijn Coenen854e0772012-04-02 14:52:10 -07001062 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -07001063 * <pre>
1064 * &lt;application ...>
1065 * &lt;meta-data android:name="android.nfc.disable_beam_default"
1066 * android:value="true" />
1067 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001068 *
1069 * <p>The API allows for multiple activities to be specified at a time,
1070 * but it is strongly recommended to just register one at a time,
1071 * and to do so during the activity's {@link Activity#onCreate}. For example:
1072 * <pre>
1073 * protected void onCreate(Bundle savedInstanceState) {
1074 * super.onCreate(savedInstanceState);
1075 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1076 * if (nfcAdapter == null) return; // NFC not available on this device
1077 * nfcAdapter.setNdefPushMessageCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001078 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001079 * And that is it. Only one call per activity is necessary. The Android
1080 * OS will automatically release its references to the callback and the
1081 * Activity object when it is destroyed if you follow this pattern.
1082 *
1083 * <p class="note">Do not pass in an Activity that has already been through
1084 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1085 * during {@link Activity#onCreate}.
Martijn Coenen2c103112012-05-15 10:32:15 -07001086 * <p class="note">For sending large content such as pictures and songs,
1087 * consider using {@link #setBeamPushUris}, which switches to alternate transports
1088 * such as Bluetooth to achieve a fast transfer rate.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001089 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1090 *
1091 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001092 * @param activity activity for which the NDEF message will be pushed
1093 * @param activities optional additional activities, however we strongly recommend
1094 * to only register one at a time, and to do so in that activity's
1095 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -07001096 */
Nick Pelly82328bf2011-08-30 09:37:25 -07001097 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
Nick Pellyc84c89a2011-08-22 22:27:11 -07001098 Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -07001099 int targetSdkVersion = getSdkVersion();
1100 try {
1101 if (activity == null) {
1102 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001103 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001104 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001105 for (Activity a : activities) {
1106 if (a == null) {
1107 throw new NullPointerException("activities cannot contain null");
1108 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001109 mNfcActivityManager.setNdefPushMessageCallback(a, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001110 }
1111 } catch (IllegalStateException e) {
1112 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1113 // Less strict on old applications - just log the error
1114 Log.e(TAG, "Cannot call API with Activity that has already " +
1115 "been destroyed", e);
1116 } else {
1117 // Prevent new applications from making this mistake, re-throw
1118 throw(e);
1119 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001120 }
1121 }
1122
1123 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001124 * @hide
1125 */
1126 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
1127 int flags) {
1128 if (activity == null) {
1129 throw new NullPointerException("activity cannot be null");
1130 }
1131 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, flags);
1132 }
1133
1134 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -07001135 * Set a callback on successful Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -07001136 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001137 * <p>This method may be called at any time before {@link Activity#onDestroy},
1138 * but the callback can only occur when the
1139 * specified activity(s) are in resumed (foreground) state. The recommended
1140 * approach is to call this method during your Activity's
1141 * {@link Activity#onCreate} - see sample
1142 * code below. This method does not immediately perform any I/O or blocking work,
1143 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001144 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001145 * <p>The API allows for multiple activities to be specified at a time,
1146 * but it is strongly recommended to just register one at a time,
1147 * and to do so during the activity's {@link Activity#onCreate}. For example:
1148 * <pre>
1149 * protected void onCreate(Bundle savedInstanceState) {
1150 * super.onCreate(savedInstanceState);
1151 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1152 * if (nfcAdapter == null) return; // NFC not available on this device
1153 * nfcAdapter.setOnNdefPushCompleteCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001154 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001155 * And that is it. Only one call per activity is necessary. The Android
1156 * OS will automatically release its references to the callback and the
1157 * Activity object when it is destroyed if you follow this pattern.
1158 *
1159 * <p class="note">Do not pass in an Activity that has already been through
1160 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1161 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -07001162 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001163 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1164 *
1165 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001166 * @param activity activity for which the NDEF message will be pushed
1167 * @param activities optional additional activities, however we strongly recommend
1168 * to only register one at a time, and to do so in that activity's
1169 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -07001170 */
1171 public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback,
Nick Pelly82328bf2011-08-30 09:37:25 -07001172 Activity activity, Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -07001173 int targetSdkVersion = getSdkVersion();
1174 try {
1175 if (activity == null) {
1176 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001177 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001178 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, callback);
1179 for (Activity a : activities) {
1180 if (a == null) {
1181 throw new NullPointerException("activities cannot contain null");
1182 }
1183 mNfcActivityManager.setOnNdefPushCompleteCallback(a, callback);
1184 }
1185 } catch (IllegalStateException e) {
1186 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1187 // Less strict on old applications - just log the error
1188 Log.e(TAG, "Cannot call API with Activity that has already " +
1189 "been destroyed", e);
1190 } else {
1191 // Prevent new applications from making this mistake, re-throw
1192 throw(e);
1193 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001194 }
1195 }
1196
1197 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001198 * Enable foreground dispatch to the given Activity.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001199 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001200 * <p>This will give give priority to the foreground activity when
1201 * dispatching a discovered {@link Tag} to an application.
1202 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001203 * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents
1204 * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and
1205 * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED}
1206 * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled
1207 * by passing in the tech lists separately. Each first level entry in the tech list represents
1208 * an array of technologies that must all be present to match. If any of the first level sets
1209 * match then the dispatch is routed through the given PendingIntent. In other words, the second
1210 * level is ANDed together and the first level entries are ORed together.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001211 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001212 * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters
1213 * that acts a wild card and will cause the foreground activity to receive all tags via the
1214 * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001215 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001216 * <p>This method must be called from the main thread, and only when the activity is in the
1217 * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before
1218 * the completion of their {@link Activity#onPause} callback to disable foreground dispatch
1219 * after it has been enabled.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001220 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001221 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1222 *
Jeff Hamilton52d32032011-01-08 15:31:26 -06001223 * @param activity the Activity to dispatch to
1224 * @param intent the PendingIntent to start for the dispatch
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001225 * @param filters the IntentFilters to override dispatching for, or null to always dispatch
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001226 * @param techLists the tech lists used to perform matching for dispatching of the
1227 * {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent
Nick Pelly74fe6c62011-02-02 22:37:40 -08001228 * @throws IllegalStateException if the Activity is not currently in the foreground
Jeff Hamilton52d32032011-01-08 15:31:26 -06001229 */
1230 public void enableForegroundDispatch(Activity activity, PendingIntent intent,
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001231 IntentFilter[] filters, String[][] techLists) {
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001232 if (activity == null || intent == null) {
Jeff Hamilton52d32032011-01-08 15:31:26 -06001233 throw new NullPointerException();
1234 }
1235 if (!activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001236 throw new IllegalStateException("Foreground dispatch can only be enabled " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001237 "when your activity is resumed");
1238 }
1239 try {
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001240 TechListParcel parcel = null;
1241 if (techLists != null && techLists.length > 0) {
1242 parcel = new TechListParcel(techLists);
1243 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06001244 ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001245 mForegroundDispatchListener);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001246 sService.setForegroundDispatch(intent, filters, parcel);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001247 } catch (RemoteException e) {
1248 attemptDeadServiceRecovery(e);
1249 }
1250 }
1251
1252 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001253 * Disable foreground dispatch to the given activity.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001254 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001255 * <p>After calling {@link #enableForegroundDispatch}, an activity
1256 * must call this method before its {@link Activity#onPause} callback
1257 * completes.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001258 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001259 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001260 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001261 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1262 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001263 * @param activity the Activity to disable dispatch to
1264 * @throws IllegalStateException if the Activity has already been paused
Jeff Hamilton52d32032011-01-08 15:31:26 -06001265 */
1266 public void disableForegroundDispatch(Activity activity) {
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001267 ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,
1268 mForegroundDispatchListener);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001269 disableForegroundDispatchInternal(activity, false);
1270 }
1271
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001272 OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001273 @Override
1274 public void onPaused(Activity activity) {
1275 disableForegroundDispatchInternal(activity, true);
1276 }
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001277 };
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001278
Jeff Hamilton52d32032011-01-08 15:31:26 -06001279 void disableForegroundDispatchInternal(Activity activity, boolean force) {
1280 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001281 sService.setForegroundDispatch(null, null, null);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001282 if (!force && !activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001283 throw new IllegalStateException("You must disable foreground dispatching " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001284 "while your activity is still resumed");
1285 }
1286 } catch (RemoteException e) {
1287 attemptDeadServiceRecovery(e);
1288 }
1289 }
1290
Nick Pelly590b73b2010-10-12 13:00:50 -07001291 /**
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001292 * Limit the NFC controller to reader mode while this Activity is in the foreground.
1293 *
1294 * <p>In this mode the NFC controller will only act as an NFC tag reader/writer,
1295 * thus disabling any peer-to-peer (Android Beam) and card-emulation modes of
1296 * the NFC adapter on this device.
1297 *
1298 * <p>Use {@link #FLAG_READER_SKIP_NDEF_CHECK} to prevent the platform from
1299 * performing any NDEF checks in reader mode. Note that this will prevent the
1300 * {@link Ndef} tag technology from being enumerated on the tag, and that
1301 * NDEF-based tag dispatch will not be functional.
1302 *
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001303 * <p>For interacting with tags that are emulated on another Android device
1304 * using Android's host-based card-emulation, the recommended flags are
1305 * {@link #FLAG_READER_NFC_A} and {@link #FLAG_READER_SKIP_NDEF_CHECK}.
1306 *
1307 * @param activity the Activity that requests the adapter to be in reader mode
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001308 * @param callback the callback to be called when a tag is discovered
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001309 * @param flags Flags indicating poll technologies and other optional parameters
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001310 * @param extras Additional extras for configuring reader mode.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001311 */
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001312 public void enableReaderMode(Activity activity, ReaderCallback callback, int flags,
1313 Bundle extras) {
1314 mNfcActivityManager.enableReaderMode(activity, callback, flags, extras);
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001315 }
1316
1317 /**
1318 * Restore the NFC adapter to normal mode of operation: supporting
1319 * peer-to-peer (Android Beam), card emulation, and polling for
1320 * all supported tag technologies.
1321 *
1322 * @param activity the Activity that currently has reader mode enabled
1323 */
1324 public void disableReaderMode(Activity activity) {
1325 mNfcActivityManager.disableReaderMode(activity);
1326 }
1327
1328 /**
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001329 * Manually invoke Android Beam to share data.
1330 *
1331 * <p>The Android Beam animation is normally only shown when two NFC-capable
1332 * devices come into range.
1333 * By calling this method, an Activity can invoke the Beam animation directly
1334 * even if no other NFC device is in range yet. The Beam animation will then
1335 * prompt the user to tap another NFC-capable device to complete the data
1336 * transfer.
1337 *
1338 * <p>The main advantage of using this method is that it avoids the need for the
1339 * user to tap the screen to complete the transfer, as this method already
1340 * establishes the direction of the transfer and the consent of the user to
1341 * share data. Callers are responsible for making sure that the user has
1342 * consented to sharing data on NFC tap.
1343 *
1344 * <p>Note that to use this method, the passed in Activity must have already
1345 * set data to share over Beam by using method calls such as
Martijn Coenen6924c4e2014-02-25 11:37:17 -08001346 * {@link #setNdefPushMessageCallback} or
1347 * {@link #setBeamPushUrisCallback}.
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001348 *
1349 * @param activity the current foreground Activity that has registered data to share
1350 * @return whether the Beam animation was successfully invoked
1351 */
1352 public boolean invokeBeam(Activity activity) {
1353 if (activity == null) {
1354 throw new NullPointerException("activity may not be null.");
1355 }
1356 enforceResumed(activity);
1357 try {
1358 sService.invokeBeam();
1359 return true;
1360 } catch (RemoteException e) {
1361 Log.e(TAG, "invokeBeam: NFC process has died.");
1362 attemptDeadServiceRecovery(e);
1363 return false;
1364 }
1365 }
1366
1367 /**
Martijn Coenendbedb4c2014-07-01 15:52:19 -07001368 * @hide
1369 */
1370 public boolean invokeBeam(BeamShareData shareData) {
1371 try {
1372 Log.e(TAG, "invokeBeamInternal()");
1373 sService.invokeBeamInternal(shareData);
1374 return true;
1375 } catch (RemoteException e) {
1376 Log.e(TAG, "invokeBeam: NFC process has died.");
1377 attemptDeadServiceRecovery(e);
1378 return false;
1379 }
1380 }
1381
1382 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001383 * Enable NDEF message push over NFC while this Activity is in the foreground.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001384 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001385 * <p>You must explicitly call this method every time the activity is
1386 * resumed, and you must call {@link #disableForegroundNdefPush} before
1387 * your activity completes {@link Activity#onPause}.
1388 *
1389 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1390 * instead: it automatically hooks into your activity life-cycle,
1391 * so you do not need to call enable/disable in your onResume/onPause.
1392 *
1393 * <p>For NDEF push to function properly the other NFC device must
1394 * support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or
1395 * Android's "com.android.npp" (Ndef Push Protocol). This was optional
1396 * on Gingerbread level Android NFC devices, but SNEP is mandatory on
1397 * Ice-Cream-Sandwich and beyond.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001398 *
1399 * <p>This method must be called from the main thread.
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001400 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001401 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1402 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001403 * @param activity foreground activity
1404 * @param message a NDEF Message to push over NFC
1405 * @throws IllegalStateException if the activity is not currently in the foreground
1406 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001407 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001408 @Deprecated
1409 public void enableForegroundNdefPush(Activity activity, NdefMessage message) {
1410 if (activity == null || message == null) {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001411 throw new NullPointerException();
1412 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001413 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001414 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Jason parks01425362011-05-24 02:57:37 -07001415 }
1416
1417 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001418 * Disable NDEF message push over P2P.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001419 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001420 * <p>After calling {@link #enableForegroundNdefPush}, an activity
1421 * must call this method before its {@link Activity#onPause} callback
1422 * completes.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001423 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001424 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1425 * instead: it automatically hooks into your activity life-cycle,
1426 * so you do not need to call enable/disable in your onResume/onPause.
1427 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001428 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001429 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001430 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1431 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001432 * @param activity the Foreground activity
1433 * @throws IllegalStateException if the Activity has already been paused
Nick Pellyc84c89a2011-08-22 22:27:11 -07001434 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001435 */
Nick Pellyc97a5522012-01-05 15:13:01 +11001436 @Deprecated
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001437 public void disableForegroundNdefPush(Activity activity) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001438 if (activity == null) {
1439 throw new NullPointerException();
1440 }
1441 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001442 mNfcActivityManager.setNdefPushMessage(activity, null, 0);
1443 mNfcActivityManager.setNdefPushMessageCallback(activity, null, 0);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001444 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, null);
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001445 }
1446
Nick Pellyc84c89a2011-08-22 22:27:11 -07001447 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001448 * Enable NDEF Push feature.
1449 * <p>This API is for the Settings application.
1450 * @hide
1451 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001452 @SystemApi
Nick Pellyc84c89a2011-08-22 22:27:11 -07001453 public boolean enableNdefPush() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001454 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001455 return sService.enableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001456 } catch (RemoteException e) {
1457 attemptDeadServiceRecovery(e);
1458 return false;
1459 }
1460 }
1461
1462 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001463 * Disable NDEF Push feature.
1464 * <p>This API is for the Settings application.
Martijn Coenen6d748942011-07-21 09:34:25 +02001465 * @hide
1466 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001467 @SystemApi
Nick Pellyc84c89a2011-08-22 22:27:11 -07001468 public boolean disableNdefPush() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001469 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001470 return sService.disableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001471 } catch (RemoteException e) {
1472 attemptDeadServiceRecovery(e);
1473 return false;
1474 }
1475 }
1476
1477 /**
Nick Pellycccf01d2011-10-31 14:49:40 -07001478 * Return true if the NDEF Push (Android Beam) feature is enabled.
1479 * <p>This function will return true only if both NFC is enabled, and the
1480 * NDEF Push feature is enabled.
1481 * <p>Note that if NFC is enabled but NDEF Push is disabled then this
1482 * device can still <i>receive</i> NDEF messages, it just cannot send them.
1483 * <p>Applications cannot directly toggle the NDEF Push feature, but they
1484 * can request Settings UI allowing the user to toggle NDEF Push using
1485 * <code>startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))</code>
1486 * <p>Example usage in an Activity that requires NDEF Push:
1487 * <p><pre>
1488 * protected void onResume() {
1489 * super.onResume();
1490 * if (!nfcAdapter.isEnabled()) {
1491 * startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
1492 * } else if (!nfcAdapter.isNdefPushEnabled()) {
1493 * startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
1494 * }
Scott Main97e0a1c2012-06-25 11:22:10 -07001495 * }</pre>
Martijn Coenen6d748942011-07-21 09:34:25 +02001496 *
Nick Pellycccf01d2011-10-31 14:49:40 -07001497 * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS
Nick Pellyc84c89a2011-08-22 22:27:11 -07001498 * @return true if NDEF Push feature is enabled
Martijn Coenen6d748942011-07-21 09:34:25 +02001499 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001500 public boolean isNdefPushEnabled() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001501 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001502 return sService.isNdefPushEnabled();
Martijn Coenen6d748942011-07-21 09:34:25 +02001503 } catch (RemoteException e) {
1504 attemptDeadServiceRecovery(e);
1505 return false;
1506 }
1507 }
1508
1509 /**
Martijn Coenend55def82016-04-14 15:19:31 +02001510 * Signals that you are no longer interested in communicating with an NFC tag
1511 * for as long as it remains in range.
1512 *
1513 * All future attempted communication to this tag will fail with {@link IOException}.
1514 * The NFC controller will be put in a low-power polling mode, allowing the device
1515 * to save power in cases where it's "attached" to a tag all the time (e.g. a tag in
1516 * car dock).
1517 *
1518 * Additionally the debounceMs parameter allows you to specify for how long the tag needs
1519 * to have gone out of range, before it will be dispatched again.
1520 *
1521 * Note: the NFC controller typically polls at a pretty slow interval (100 - 500 ms).
1522 * This means that if the tag repeatedly goes in and out of range (for example, in
1523 * case of a flaky connection), and the controller happens to poll every time the
1524 * tag is out of range, it *will* re-dispatch the tag after debounceMs, despite the tag
1525 * having been "in range" during the interval.
1526 *
1527 * Note 2: if a tag with another UID is detected after this API is called, its effect
1528 * will be cancelled; if this tag shows up before the amount of time specified in
1529 * debounceMs, it will be dispatched again.
1530 *
1531 * Note 3: some tags have a random UID, in which case this API won't work reliably.
1532 *
1533 * @param tag the {@link android.nfc.Tag Tag} to ignore.
1534 * @param debounceMs minimum amount of time the tag needs to be out of range before being
1535 * dispatched again.
1536 * @param tagRemovedListener listener to be called when the tag is removed from the field.
1537 * Note that this will only be called if the tag has been out of range
1538 * for at least debounceMs, or if another tag came into range before
1539 * debounceMs. May be null in case you don't want a callback.
1540 * @param handler the {@link android.os.Handler Handler} that will be used for delivering
1541 * the callback. if the handler is null, then the thread used for delivering
1542 * the callback is unspecified.
1543 * @return false if the tag couldn't be found (or has already gone out of range), true otherwise
1544 */
1545 public boolean ignore(final Tag tag, int debounceMs,
1546 final OnTagRemovedListener tagRemovedListener, final Handler handler) {
1547 ITagRemovedCallback.Stub iListener = null;
1548 if (tagRemovedListener != null) {
1549 iListener = new ITagRemovedCallback.Stub() {
1550 @Override
1551 public void onTagRemoved() throws RemoteException {
1552 if (handler != null) {
1553 handler.post(new Runnable() {
1554 @Override
1555 public void run() {
1556 tagRemovedListener.onTagRemoved();
1557 }
1558 });
1559 } else {
1560 tagRemovedListener.onTagRemoved();
1561 }
1562 synchronized (mLock) {
1563 mTagRemovedListener = null;
1564 }
1565 }
1566 };
1567 }
1568 synchronized (mLock) {
1569 mTagRemovedListener = iListener;
1570 }
1571 try {
1572 return sService.ignore(tag.getServiceHandle(), debounceMs, iListener);
1573 } catch (RemoteException e) {
1574 return false;
1575 }
1576 }
1577
1578 /**
Nick Pellyc97a5522012-01-05 15:13:01 +11001579 * Inject a mock NFC tag.<p>
1580 * Used for testing purposes.
1581 * <p class="note">Requires the
1582 * {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
1583 * @hide
1584 */
Nick Pelly1f5badc2012-01-24 13:22:58 -08001585 public void dispatch(Tag tag) {
Nick Pellyc97a5522012-01-05 15:13:01 +11001586 if (tag == null) {
1587 throw new NullPointerException("tag cannot be null");
1588 }
1589 try {
Nick Pelly1f5badc2012-01-24 13:22:58 -08001590 sService.dispatch(tag);
Nick Pellyc97a5522012-01-05 15:13:01 +11001591 } catch (RemoteException e) {
1592 attemptDeadServiceRecovery(e);
1593 }
1594 }
1595
1596 /**
Daniel Tomas90245642010-11-17 10:07:52 +01001597 * @hide
1598 */
Martijn Coenen188cddb2012-01-31 22:16:15 -08001599 public void setP2pModes(int initiatorModes, int targetModes) {
1600 try {
1601 sService.setP2pModes(initiatorModes, targetModes);
1602 } catch (RemoteException e) {
1603 attemptDeadServiceRecovery(e);
1604 }
1605 }
1606
1607 /**
Andres Morales11d2e532014-07-23 12:39:55 -07001608 * Registers a new NFC unlock handler with the NFC service.
1609 *
1610 * <p />NFC unlock handlers are intended to unlock the keyguard in the presence of a trusted
1611 * NFC device. The handler should return true if it successfully authenticates the user and
1612 * unlocks the keyguard.
1613 *
1614 * <p /> The parameter {@code tagTechnologies} determines which Tag technologies will be polled for
1615 * at the lockscreen. Polling for less tag technologies reduces latency, and so it is
1616 * strongly recommended to only provide the Tag technologies that the handler is expected to
Andres Moralesf9a97942014-08-14 15:43:30 -07001617 * receive. There must be at least one tag technology provided, otherwise the unlock handler
1618 * is ignored.
Andres Morales11d2e532014-07-23 12:39:55 -07001619 *
1620 * @hide
1621 */
1622 @SystemApi
1623 public boolean addNfcUnlockHandler(final NfcUnlockHandler unlockHandler,
1624 String[] tagTechnologies) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001625 // If there are no tag technologies, don't bother adding unlock handler
1626 if (tagTechnologies.length == 0) {
1627 return false;
1628 }
Andres Morales11d2e532014-07-23 12:39:55 -07001629
Andres Moralesf9a97942014-08-14 15:43:30 -07001630 try {
Andres Morales11d2e532014-07-23 12:39:55 -07001631 synchronized (mLock) {
1632 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001633 // update the tag technologies
1634 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.get(unlockHandler));
1635 mNfcUnlockHandlers.remove(unlockHandler);
Andres Morales11d2e532014-07-23 12:39:55 -07001636 }
Andres Moralesf9a97942014-08-14 15:43:30 -07001637
1638 INfcUnlockHandler.Stub iHandler = new INfcUnlockHandler.Stub() {
1639 @Override
1640 public boolean onUnlockAttempted(Tag tag) throws RemoteException {
1641 return unlockHandler.onUnlockAttempted(tag);
1642 }
1643 };
1644
1645 sService.addNfcUnlockHandler(iHandler,
1646 Tag.getTechCodesFromStrings(tagTechnologies));
1647 mNfcUnlockHandlers.put(unlockHandler, iHandler);
Andres Morales11d2e532014-07-23 12:39:55 -07001648 }
1649 } catch (RemoteException e) {
1650 attemptDeadServiceRecovery(e);
1651 return false;
1652 } catch (IllegalArgumentException e) {
1653 Log.e(TAG, "Unable to register LockscreenDispatch", e);
1654 return false;
1655 }
1656
1657 return true;
1658 }
1659
1660 /**
1661 * Removes a previously registered unlock handler. Also removes the tag technologies
1662 * associated with the removed unlock handler.
1663 *
1664 * @hide
1665 */
1666 @SystemApi
1667 public boolean removeNfcUnlockHandler(NfcUnlockHandler unlockHandler) {
1668 try {
1669 synchronized (mLock) {
1670 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001671 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.remove(unlockHandler));
Andres Morales11d2e532014-07-23 12:39:55 -07001672 }
1673
1674 return true;
1675 }
1676 } catch (RemoteException e) {
1677 attemptDeadServiceRecovery(e);
1678 return false;
1679 }
1680 }
1681
1682 /**
Martijn Coenen188cddb2012-01-31 22:16:15 -08001683 * @hide
1684 */
Nick Pelly367f41f2011-03-08 11:43:30 -08001685 public INfcAdapterExtras getNfcAdapterExtrasInterface() {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06001686 if (mContext == null) {
1687 throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
1688 + " NFC extras APIs");
1689 }
Daniel Tomas90245642010-11-17 10:07:52 +01001690 try {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06001691 return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
Daniel Tomas90245642010-11-17 10:07:52 +01001692 } catch (RemoteException e) {
Nick Pelly367f41f2011-03-08 11:43:30 -08001693 attemptDeadServiceRecovery(e);
Daniel Tomas90245642010-11-17 10:07:52 +01001694 return null;
1695 }
1696 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001697
1698 void enforceResumed(Activity activity) {
1699 if (!activity.isResumed()) {
1700 throw new IllegalStateException("API cannot be called while activity is paused");
1701 }
1702 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001703
1704 int getSdkVersion() {
1705 if (mContext == null) {
1706 return android.os.Build.VERSION_CODES.GINGERBREAD; // best guess
1707 } else {
1708 return mContext.getApplicationInfo().targetSdkVersion;
1709 }
1710 }
Nick Pelly590b73b2010-10-12 13:00:50 -07001711}