blob: b492debdd7c8141b7e2fc3f557b0e01851b2b3d3 [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;
Nick Pelly590b73b2010-10-12 13:00:50 -070038import android.os.IBinder;
39import android.os.RemoteException;
40import android.os.ServiceManager;
41import android.util.Log;
42
Nick Pelly590b73b2010-10-12 13:00:50 -070043/**
Nick Pelly74fe6c62011-02-02 22:37:40 -080044 * Represents the local NFC adapter.
Nick Pelly590b73b2010-10-12 13:00:50 -070045 * <p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -080046 * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC
47 * adapter for this Android device.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080048 *
49 * <div class="special reference">
50 * <h3>Developer Guides</h3>
51 * <p>For more information about using NFC, read the
52 * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p>
Scott Mainb38ad762013-10-17 11:57:06 -070053 * <p>To perform basic file sharing between devices, read
54 * <a href="{@docRoot}training/beam-files/index.html">Sharing Files with NFC</a>.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080055 * </div>
Nick Pelly590b73b2010-10-12 13:00:50 -070056 */
57public final class NfcAdapter {
Nick Pellyc84c89a2011-08-22 22:27:11 -070058 static final String TAG = "NFC";
Nick Pelly50b4d8f2010-12-07 22:40:28 -080059
Nick Pelly590b73b2010-10-12 13:00:50 -070060 /**
Jeff Hamilton641dd622010-12-02 09:16:22 -060061 * Intent to start an activity when a tag with NDEF payload is discovered.
Jeff Hamilton641dd622010-12-02 09:16:22 -060062 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090063 * <p>The system inspects the first {@link NdefRecord} in the first {@link NdefMessage} and
64 * looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the
65 * intent will contain the URI in its data field. If a MIME record is found the intent will
66 * contain the MIME type in its type field. This allows activities to register
67 * {@link IntentFilter}s targeting specific content on tags. Activities should register the
68 * most specific intent filters possible to avoid the activity chooser dialog, which can
69 * disrupt the interaction with the tag as the user interacts with the screen.
70 *
71 * <p>If the tag has an NDEF payload this intent is started before
72 * {@link #ACTION_TECH_DISCOVERED}. If any activities respond to this intent neither
Nick Pellyf003e262011-01-31 23:27:37 -080073 * {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started.
Nick Pellyc97a5522012-01-05 15:13:01 +110074 *
75 * <p>The MIME type or data URI of this intent are normalized before dispatch -
76 * so that MIME, URI scheme and URI host are always lower-case.
Jeff Hamilton641dd622010-12-02 09:16:22 -060077 */
78 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
79 public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED";
80
81 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +090082 * Intent to start an activity when a tag is discovered and activities are registered for the
83 * specific technologies on the tag.
Jeff Hamilton641dd622010-12-02 09:16:22 -060084 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090085 * <p>To receive this intent an activity must include an intent filter
86 * for this action and specify the desired tech types in a
87 * manifest <code>meta-data</code> entry. Here is an example manfiest entry:
88 * <pre>
Scott Main97e0a1c2012-06-25 11:22:10 -070089 * &lt;activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"&gt;
90 * &lt;!-- Add a technology filter --&gt;
91 * &lt;intent-filter&gt;
92 * &lt;action android:name="android.nfc.action.TECH_DISCOVERED" /&gt;
93 * &lt;/intent-filter&gt;
Jeff Hamilton641dd622010-12-02 09:16:22 -060094 *
Scott Main97e0a1c2012-06-25 11:22:10 -070095 * &lt;meta-data android:name="android.nfc.action.TECH_DISCOVERED"
96 * android:resource="@xml/filter_nfc"
97 * /&gt;
98 * &lt;/activity&gt;</pre>
Jeff Hamilton28319c02011-02-09 17:26:47 +090099 *
100 * <p>The meta-data XML file should contain one or more <code>tech-list</code> entries
101 * each consisting or one or more <code>tech</code> entries. The <code>tech</code> entries refer
102 * to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA".
103 *
104 * <p>A tag matches if any of the
105 * <code>tech-list</code> sets is a subset of {@link Tag#getTechList() Tag.getTechList()}. Each
106 * of the <code>tech-list</code>s is considered independently and the
107 * activity is considered a match is any single <code>tech-list</code> matches the tag that was
108 * discovered. This provides AND and OR semantics for filtering desired techs. Here is an
109 * example that will match any tag using {@link NfcF} or any tag using {@link NfcA},
110 * {@link MifareClassic}, and {@link Ndef}:
111 *
112 * <pre>
113 * &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
114 * &lt;!-- capture anything using NfcF --&gt;
115 * &lt;tech-list&gt;
116 * &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
117 * &lt;/tech-list&gt;
118 *
119 * &lt;!-- OR --&gt;
120 *
121 * &lt;!-- capture all MIFARE Classics with NDEF payloads --&gt;
122 * &lt;tech-list&gt;
123 * &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
124 * &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
125 * &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
126 * &lt;/tech-list&gt;
Scott Main97e0a1c2012-06-25 11:22:10 -0700127 * &lt;/resources&gt;</pre>
Jeff Hamilton28319c02011-02-09 17:26:47 +0900128 *
129 * <p>This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before
130 * {@link #ACTION_TAG_DISCOVERED}. If any activities respond to {@link #ACTION_NDEF_DISCOVERED}
131 * this intent will not be started. If any activities respond to this intent
132 * {@link #ACTION_TAG_DISCOVERED} will not be started.
Jeff Hamilton641dd622010-12-02 09:16:22 -0600133 */
134 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Nick Pellyf003e262011-01-31 23:27:37 -0800135 public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED";
Jeff Hamilton641dd622010-12-02 09:16:22 -0600136
137 /**
Nick Pelly11b075e2010-10-28 13:39:37 -0700138 * Intent to start an activity when a tag is discovered.
Jeff Hamilton28319c02011-02-09 17:26:47 +0900139 *
140 * <p>This intent will not be started when a tag is discovered if any activities respond to
Jason parks01425362011-05-24 02:57:37 -0700141 * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag.
Nick Pelly590b73b2010-10-12 13:00:50 -0700142 */
143 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
144 public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED";
145
146 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600147 * Broadcast to only the activity that handles ACTION_TAG_DISCOVERED
148 * @hide
149 */
150 public static final String ACTION_TAG_LEFT_FIELD = "android.nfc.action.TAG_LOST";
151
152 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900153 * Mandatory extra containing the {@link Tag} that was discovered for the
154 * {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
155 * {@link #ACTION_TAG_DISCOVERED} intents.
Nick Pelly590b73b2010-10-12 13:00:50 -0700156 */
157 public static final String EXTRA_TAG = "android.nfc.extra.TAG";
158
159 /**
Nick Pellyc97a5522012-01-05 15:13:01 +1100160 * Extra containing an array of {@link NdefMessage} present on the discovered tag.<p>
161 * This extra is mandatory for {@link #ACTION_NDEF_DISCOVERED} intents,
162 * and optional for {@link #ACTION_TECH_DISCOVERED}, and
163 * {@link #ACTION_TAG_DISCOVERED} intents.<p>
164 * When this extra is present there will always be at least one
165 * {@link NdefMessage} element. Most NDEF tags have only one NDEF message,
166 * but we use an array for future compatibility.
Nick Pelly11b075e2010-10-28 13:39:37 -0700167 */
168 public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES";
169
170 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900171 * Optional extra containing a byte array containing the ID of the discovered tag for
172 * the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
173 * {@link #ACTION_TAG_DISCOVERED} intents.
Nick Pelly11b075e2010-10-28 13:39:37 -0700174 */
175 public static final String EXTRA_ID = "android.nfc.extra.ID";
176
177 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700178 * Broadcast Action: The state of the local NFC adapter has been
179 * changed.
180 * <p>For example, NFC has been turned on or off.
Martijn Coenen4ba5eaf2013-01-08 12:49:47 -0800181 * <p>Always contains the extra field {@link #EXTRA_ADAPTER_STATE}
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700182 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700183 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
184 public static final String ACTION_ADAPTER_STATE_CHANGED =
185 "android.nfc.action.ADAPTER_STATE_CHANGED";
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700186
187 /**
Martijn Coenen4ba5eaf2013-01-08 12:49:47 -0800188 * Used as an int extra field in {@link #ACTION_ADAPTER_STATE_CHANGED}
Nick Pelly8d32a012011-08-09 07:03:49 -0700189 * intents to request the current power state. Possible values are:
190 * {@link #STATE_OFF},
191 * {@link #STATE_TURNING_ON},
192 * {@link #STATE_ON},
193 * {@link #STATE_TURNING_OFF},
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700194 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700195 public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE";
196
Nick Pelly8d32a012011-08-09 07:03:49 -0700197 public static final int STATE_OFF = 1;
Nick Pelly8d32a012011-08-09 07:03:49 -0700198 public static final int STATE_TURNING_ON = 2;
Nick Pelly8d32a012011-08-09 07:03:49 -0700199 public static final int STATE_ON = 3;
Nick Pelly8d32a012011-08-09 07:03:49 -0700200 public static final int STATE_TURNING_OFF = 4;
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700201
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700202 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700203 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700204 * <p>
205 * Setting this flag enables polling for Nfc-A technology.
206 */
207 public static final int FLAG_READER_NFC_A = 0x1;
208
209 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700210 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700211 * <p>
212 * Setting this flag enables polling for Nfc-B technology.
213 */
214 public static final int FLAG_READER_NFC_B = 0x2;
215
216 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700217 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700218 * <p>
219 * Setting this flag enables polling for Nfc-F technology.
220 */
221 public static final int FLAG_READER_NFC_F = 0x4;
222
223 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700224 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700225 * <p>
226 * Setting this flag enables polling for Nfc-V (ISO15693) technology.
227 */
228 public static final int FLAG_READER_NFC_V = 0x8;
229
230 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700231 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700232 * <p>
Martijn Coenenb523bfa2013-09-12 13:44:39 +0200233 * Setting this flag enables polling for NfcBarcode technology.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700234 */
Martijn Coenenb523bfa2013-09-12 13:44:39 +0200235 public static final int FLAG_READER_NFC_BARCODE = 0x10;
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700236
237 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700238 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700239 * <p>
240 * Setting this flag allows the caller to prevent the
241 * platform from performing an NDEF check on the tags it
242 * finds.
243 */
244 public static final int FLAG_READER_SKIP_NDEF_CHECK = 0x80;
245
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700246 /**
247 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
248 * <p>
249 * Setting this flag allows the caller to prevent the
250 * platform from playing sounds when it discovers a tag.
251 */
252 public static final int FLAG_READER_NO_PLATFORM_SOUNDS = 0x100;
253
254 /**
255 * Int Extra for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
256 * <p>
257 * Setting this integer extra allows the calling application to specify
258 * the delay that the platform will use for performing presence checks
259 * on any discovered tag.
260 */
261 public static final String EXTRA_READER_PRESENCE_CHECK_DELAY = "presence";
262
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700263 /** @hide */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700264 @SystemApi
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800265 public static final int FLAG_NDEF_PUSH_NO_CONFIRM = 0x1;
266
267 /** @hide */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700268 public static final String ACTION_HANDOVER_TRANSFER_STARTED =
269 "android.nfc.action.HANDOVER_TRANSFER_STARTED";
270
271 /** @hide */
272 public static final String ACTION_HANDOVER_TRANSFER_DONE =
273 "android.nfc.action.HANDOVER_TRANSFER_DONE";
274
275 /** @hide */
276 public static final String EXTRA_HANDOVER_TRANSFER_STATUS =
277 "android.nfc.extra.HANDOVER_TRANSFER_STATUS";
278
279 /** @hide */
280 public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
281 /** @hide */
282 public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;
283
284 /** @hide */
285 public static final String EXTRA_HANDOVER_TRANSFER_URI =
286 "android.nfc.extra.HANDOVER_TRANSFER_URI";
287
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800288 // Guarded by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700289 static boolean sIsInitialized = false;
Nick Pelly590b73b2010-10-12 13:00:50 -0700290
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800291 // Final after first constructor, except for
292 // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
293 // recovery
Nick Pellyc84c89a2011-08-22 22:27:11 -0700294 static INfcAdapter sService;
295 static INfcTag sTagService;
Martijn Coenena7397882013-07-30 20:07:47 -0700296 static INfcCardEmulation sCardEmulationService;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700297
298 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600299 * The NfcAdapter object for each application context.
300 * There is a 1-1 relationship between application context and
301 * NfcAdapter object.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700302 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600303 static HashMap<Context, NfcAdapter> sNfcAdapters = new HashMap(); //guard by NfcAdapter.class
304
305 /**
306 * NfcAdapter used with a null context. This ctor was deprecated but we have
307 * to support it for backwards compatibility. New methods that require context
308 * might throw when called on the null-context NfcAdapter.
309 */
310 static NfcAdapter sNullContextNfcAdapter; // protected by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700311
312 final NfcActivityManager mNfcActivityManager;
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600313 final Context mContext;
Andres Moralesf9a97942014-08-14 15:43:30 -0700314 final HashMap<NfcUnlockHandler, INfcUnlockHandler> mNfcUnlockHandlers;
Andres Morales11d2e532014-07-23 12:39:55 -0700315 final Object mLock;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700316
317 /**
Scott Mainb38ad762013-10-17 11:57:06 -0700318 * A callback to be invoked when the system finds a tag while the foreground activity is
319 * operating in reader mode.
320 * <p>Register your {@code ReaderCallback} implementation with {@link
321 * NfcAdapter#enableReaderMode} and disable it with {@link
322 * NfcAdapter#disableReaderMode}.
323 * @see NfcAdapter#enableReaderMode
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700324 */
325 public interface ReaderCallback {
326 public void onTagDiscovered(Tag tag);
327 }
328
329 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700330 * A callback to be invoked when the system successfully delivers your {@link NdefMessage}
331 * to another device.
332 * @see #setOnNdefPushCompleteCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700333 */
334 public interface OnNdefPushCompleteCallback {
335 /**
336 * Called on successful NDEF push.
337 *
338 * <p>This callback is usually made on a binder thread (not the UI thread).
339 *
340 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
Scott Main2d68a6b2011-09-26 22:59:38 -0700341 * @see #setNdefPushMessageCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700342 */
343 public void onNdefPushComplete(NfcEvent event);
344 }
345
346 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700347 * A callback to be invoked when another NFC device capable of NDEF push (Android Beam)
348 * is within range.
349 * <p>Implement this interface and pass it to {@link
350 * NfcAdapter#setNdefPushMessageCallback setNdefPushMessageCallback()} in order to create an
351 * {@link NdefMessage} at the moment that another device is within range for NFC. Using this
352 * callback allows you to create a message with data that might vary based on the
353 * content currently visible to the user. Alternatively, you can call {@link
354 * #setNdefPushMessage setNdefPushMessage()} if the {@link NdefMessage} always contains the
355 * same data.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700356 */
357 public interface CreateNdefMessageCallback {
358 /**
359 * Called to provide a {@link NdefMessage} to push.
360 *
361 * <p>This callback is usually made on a binder thread (not the UI thread).
362 *
363 * <p>Called when this device is in range of another device
364 * that might support NDEF push. It allows the application to
365 * create the NDEF message only when it is required.
366 *
367 * <p>NDEF push cannot occur until this method returns, so do not
368 * block for too long.
369 *
370 * <p>The Android operating system will usually show a system UI
371 * on top of your activity during this time, so do not try to request
372 * input from the user to complete the callback, or provide custom NDEF
373 * push UI. The user probably will not see it.
374 *
375 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
376 * @return NDEF message to push, or null to not provide a message
377 */
378 public NdefMessage createNdefMessage(NfcEvent event);
379 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700380
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700381
382 // TODO javadoc
383 public interface CreateBeamUrisCallback {
384 public Uri[] createBeamUris(NfcEvent event);
385 }
386
Nick Pelly590b73b2010-10-12 13:00:50 -0700387 /**
Andres Morales11d2e532014-07-23 12:39:55 -0700388 * A callback to be invoked when an application has registered as a
389 * handler to unlock the device given an NFC tag at the lockscreen.
390 * @hide
391 */
392 @SystemApi
393 public interface NfcUnlockHandler {
394 /**
395 * Called at the lock screen to attempt to unlock the device with the given tag.
396 * @param tag the detected tag, to be used to unlock the device
397 * @return true if the device was successfully unlocked
398 */
399 public boolean onUnlockAttempted(Tag tag);
400 }
401
402
403 /**
Nick Pellyfdf90862010-10-18 11:19:41 -0700404 * Helper to check if this device has FEATURE_NFC, but without using
405 * a context.
406 * Equivalent to
407 * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)
408 */
409 private static boolean hasNfcFeature() {
410 IPackageManager pm = ActivityThread.getPackageManager();
411 if (pm == null) {
412 Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
413 return false;
414 }
415 try {
416 return pm.hasSystemFeature(PackageManager.FEATURE_NFC);
417 } catch (RemoteException e) {
418 Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
419 return false;
420 }
421 }
422
Nick Pellyc84c89a2011-08-22 22:27:11 -0700423 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600424 * Returns the NfcAdapter for application context,
425 * or throws if NFC is not available.
426 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700427 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600428 public static synchronized NfcAdapter getNfcAdapter(Context context) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800429 if (!sIsInitialized) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800430 /* is this device meant to have NFC */
431 if (!hasNfcFeature()) {
432 Log.v(TAG, "this device does not have NFC support");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700433 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800434 }
435
436 sService = getServiceInterface();
437 if (sService == null) {
438 Log.e(TAG, "could not retrieve NFC service");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700439 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800440 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100441 try {
442 sTagService = sService.getNfcTagInterface();
443 } catch (RemoteException e) {
444 Log.e(TAG, "could not retrieve NFC Tag service");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700445 throw new UnsupportedOperationException();
Nick Pelly3dd6c452011-01-10 18:14:41 +1100446 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600447
Martijn Coenena7397882013-07-30 20:07:47 -0700448 try {
449 sCardEmulationService = sService.getNfcCardEmulationInterface();
450 } catch (RemoteException e) {
451 Log.e(TAG, "could not retrieve card emulation service");
452 throw new UnsupportedOperationException();
453 }
454
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600455 sIsInitialized = true;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800456 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600457 if (context == null) {
458 if (sNullContextNfcAdapter == null) {
459 sNullContextNfcAdapter = new NfcAdapter(null);
460 }
461 return sNullContextNfcAdapter;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700462 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600463 NfcAdapter adapter = sNfcAdapters.get(context);
464 if (adapter == null) {
465 adapter = new NfcAdapter(context);
466 sNfcAdapters.put(context, adapter);
467 }
468 return adapter;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800469 }
470
Nick Pelly6d55e132010-10-27 01:14:43 -0700471 /** get handle to NFC service interface */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800472 private static INfcAdapter getServiceInterface() {
Nick Pelly6d55e132010-10-27 01:14:43 -0700473 /* get a handle to NFC service */
474 IBinder b = ServiceManager.getService("nfc");
475 if (b == null) {
476 return null;
477 }
478 return INfcAdapter.Stub.asInterface(b);
479 }
480
Nick Pellyfdf90862010-10-18 11:19:41 -0700481 /**
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800482 * Helper to get the default NFC Adapter.
483 * <p>
484 * Most Android devices will only have one NFC Adapter (NFC Controller).
485 * <p>
486 * This helper is the equivalent of:
Scott Main97e0a1c2012-06-25 11:22:10 -0700487 * <pre>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800488 * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Scott Main97e0a1c2012-06-25 11:22:10 -0700489 * NfcAdapter adapter = manager.getDefaultAdapter();</pre>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800490 * @param context the calling application's context
491 *
492 * @return the default NFC adapter, or null if no NFC adapter exists
493 */
494 public static NfcAdapter getDefaultAdapter(Context context) {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600495 if (context == null) {
496 throw new IllegalArgumentException("context cannot be null");
497 }
498 context = context.getApplicationContext();
Nick Pellyb04cce02011-11-21 17:02:02 -0800499 if (context == null) {
500 throw new IllegalArgumentException(
501 "context not associated with any application (using a mock context?)");
502 }
503 /* use getSystemService() for consistency */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800504 NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Nick Pellya5193b242011-11-16 16:46:27 -0800505 if (manager == null) {
506 // NFC not available
507 return null;
508 }
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800509 return manager.getDefaultAdapter();
510 }
511
512 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600513 * Legacy NfcAdapter getter, always use {@link #getDefaultAdapter(Context)} instead.<p>
514 * This method was deprecated at API level 10 (Gingerbread MR1) because a context is required
515 * for many NFC API methods. Those methods will fail when called on an NfcAdapter
516 * object created from this method.<p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800517 * @deprecated use {@link #getDefaultAdapter(Context)}
Nick Pellya356bf12011-12-13 15:36:31 -0800518 * @hide
Nick Pelly590b73b2010-10-12 13:00:50 -0700519 */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800520 @Deprecated
Nick Pelly590b73b2010-10-12 13:00:50 -0700521 public static NfcAdapter getDefaultAdapter() {
Nick Pellyc97a5522012-01-05 15:13:01 +1100522 // introduced in API version 9 (GB 2.3)
Nick Pellya356bf12011-12-13 15:36:31 -0800523 // deprecated in API version 10 (GB 2.3.3)
524 // removed from public API in version 16 (ICS MR2)
Nick Pellyc97a5522012-01-05 15:13:01 +1100525 // should maintain as a hidden API for binary compatibility for a little longer
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800526 Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " +
527 "NfcAdapter.getDefaultAdapter(Context) instead", new Exception());
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600528
529 return NfcAdapter.getNfcAdapter(null);
530 }
531
532 NfcAdapter(Context context) {
533 mContext = context;
534 mNfcActivityManager = new NfcActivityManager(this);
Andres Moralesf9a97942014-08-14 15:43:30 -0700535 mNfcUnlockHandlers = new HashMap<NfcUnlockHandler, INfcUnlockHandler>();
Andres Morales11d2e532014-07-23 12:39:55 -0700536 mLock = new Object();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800537 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700538
Nick Pellyc84c89a2011-08-22 22:27:11 -0700539 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600540 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700541 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600542 public Context getContext() {
543 return mContext;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800544 }
545
546 /**
547 * Returns the binder interface to the service.
548 * @hide
549 */
550 public INfcAdapter getService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800551 isEnabled(); // NOP call to recover sService if it is stale
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800552 return sService;
Nick Pelly590b73b2010-10-12 13:00:50 -0700553 }
554
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600555 /**
Nick Pelly3dd6c452011-01-10 18:14:41 +1100556 * Returns the binder interface to the tag service.
557 * @hide
558 */
559 public INfcTag getTagService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800560 isEnabled(); // NOP call to recover sTagService if it is stale
Nick Pelly3dd6c452011-01-10 18:14:41 +1100561 return sTagService;
562 }
563
564 /**
Martijn Coenena7397882013-07-30 20:07:47 -0700565 * Returns the binder interface to the card emulation service.
566 * @hide
567 */
568 public INfcCardEmulation getCardEmulationService() {
569 isEnabled();
570 return sCardEmulationService;
571 }
572
573 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600574 * NFC service dead - attempt best effort recovery
575 * @hide
576 */
577 public void attemptDeadServiceRecovery(Exception e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700578 Log.e(TAG, "NFC service dead - attempting to recover", e);
579 INfcAdapter service = getServiceInterface();
580 if (service == null) {
581 Log.e(TAG, "could not retrieve NFC service during service recovery");
Nick Pelly3dd6c452011-01-10 18:14:41 +1100582 // nothing more can be done now, sService is still stale, we'll hit
583 // this recovery path again later
Nick Pelly6d55e132010-10-27 01:14:43 -0700584 return;
585 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100586 // assigning to sService is not thread-safe, but this is best-effort code
587 // and on a well-behaved system should never happen
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800588 sService = service;
Nick Pelly3dd6c452011-01-10 18:14:41 +1100589 try {
590 sTagService = service.getNfcTagInterface();
591 } catch (RemoteException ee) {
592 Log.e(TAG, "could not retrieve NFC tag service during service recovery");
593 // nothing more can be done now, sService is still stale, we'll hit
594 // this recovery path again later
Martijn Coenena7397882013-07-30 20:07:47 -0700595 return;
596 }
597
598 try {
599 sCardEmulationService = service.getNfcCardEmulationInterface();
600 } catch (RemoteException ee) {
601 Log.e(TAG, "could not retrieve NFC card emulation service during service recovery");
Nick Pelly3dd6c452011-01-10 18:14:41 +1100602 }
603
Nick Pelly6d55e132010-10-27 01:14:43 -0700604 return;
605 }
606
Nick Pelly590b73b2010-10-12 13:00:50 -0700607 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700608 * Return true if this NFC Adapter has any features enabled.
Nick Pelly590b73b2010-10-12 13:00:50 -0700609 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800610 * <p>If this method returns false, the NFC hardware is guaranteed not to
Nick Pellycccf01d2011-10-31 14:49:40 -0700611 * generate or respond to any NFC communication over its NFC radio.
612 * <p>Applications can use this to check if NFC is enabled. Applications
613 * can request Settings UI allowing the user to toggle NFC using:
614 * <p><pre>startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))</pre>
Nick Pelly74fe6c62011-02-02 22:37:40 -0800615 *
Nick Pellycccf01d2011-10-31 14:49:40 -0700616 * @see android.provider.Settings#ACTION_NFC_SETTINGS
Nick Pelly74fe6c62011-02-02 22:37:40 -0800617 * @return true if this NFC Adapter has any features enabled
Nick Pelly590b73b2010-10-12 13:00:50 -0700618 */
Nick Pelly7ea5c452010-10-20 18:39:11 -0700619 public boolean isEnabled() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700620 try {
Nick Pelly8d32a012011-08-09 07:03:49 -0700621 return sService.getState() == STATE_ON;
Nick Pelly590b73b2010-10-12 13:00:50 -0700622 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700623 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700624 return false;
625 }
626 }
627
628 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700629 * Return the state of this NFC Adapter.
630 *
631 * <p>Returns one of {@link #STATE_ON}, {@link #STATE_TURNING_ON},
632 * {@link #STATE_OFF}, {@link #STATE_TURNING_OFF}.
633 *
634 * <p>{@link #isEnabled()} is equivalent to
635 * <code>{@link #getAdapterState()} == {@link #STATE_ON}</code>
636 *
637 * @return the current state of this NFC adapter
638 *
639 * @hide
640 */
641 public int getAdapterState() {
642 try {
643 return sService.getState();
644 } catch (RemoteException e) {
645 attemptDeadServiceRecovery(e);
646 return NfcAdapter.STATE_OFF;
647 }
648 }
649
650 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700651 * Enable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700652 *
653 * <p>This call is asynchronous. Listen for
654 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
655 * operation is complete.
656 *
657 * <p>If this returns true, then either NFC is already on, or
658 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
659 * to indicate a state transition. If this returns false, then
660 * there is some problem that prevents an attempt to turn
661 * NFC on (for example we are in airplane mode and NFC is not
662 * toggleable in airplane mode on this platform).
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700663 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700664 * @hide
665 */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700666 @SystemApi
Nick Pelly7ea5c452010-10-20 18:39:11 -0700667 public boolean enable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700668 try {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800669 return sService.enable();
Nick Pelly590b73b2010-10-12 13:00:50 -0700670 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700671 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700672 return false;
673 }
674 }
675
676 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700677 * Disable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700678 *
679 * <p>No NFC features will work after this call, and the hardware
Nick Pelly7ea5c452010-10-20 18:39:11 -0700680 * will not perform or respond to any NFC communication.
Nick Pelly8d32a012011-08-09 07:03:49 -0700681 *
682 * <p>This call is asynchronous. Listen for
683 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
684 * operation is complete.
685 *
686 * <p>If this returns true, then either NFC is already off, or
687 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
688 * to indicate a state transition. If this returns false, then
689 * there is some problem that prevents an attempt to turn
690 * NFC off.
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700691 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700692 * @hide
693 */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700694 @SystemApi
Nick Pelly7ea5c452010-10-20 18:39:11 -0700695 public boolean disable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700696 try {
Sunil Jogi3bba8d02012-04-10 13:12:26 -0700697 return sService.disable(true);
Nick Pelly590b73b2010-10-12 13:00:50 -0700698 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700699 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700700 return false;
701 }
702 }
703
Martijn Coenen2c103112012-05-15 10:32:15 -0700704 /**
Martijn Coenen56a3e672014-09-23 21:27:30 -0700705 * Disable NFC hardware.
706 * @hide
707 */
708 @SystemApi
709 public boolean disable(boolean persist) {
710 try {
711 return sService.disable(persist);
712 } catch (RemoteException e) {
713 attemptDeadServiceRecovery(e);
714 return false;
715 }
716 }
717
718 /**
Andres Morales9c4f4002014-09-08 17:09:16 -0700719 * Pauses polling for a {@code timeoutInMs} millis. If polling must be resumed before timeout,
720 * use {@link #resumePolling()}.
721 * @hide
722 */
723 public void pausePolling(int timeoutInMs) {
724 try {
725 sService.pausePolling(timeoutInMs);
726 } catch (RemoteException e) {
727 attemptDeadServiceRecovery(e);
728 }
729 }
730
731 /**
732 * Resumes default polling for the current device state if polling is paused. Calling
733 * this while polling is not paused is a no-op.
734 *
735 * @hide
736 */
737 public void resumePolling() {
738 try {
739 sService.resumePolling();
740 } catch (RemoteException e) {
741 attemptDeadServiceRecovery(e);
742 }
743 }
744
745 /**
Martijn Coenen2c103112012-05-15 10:32:15 -0700746 * Set one or more {@link Uri}s to send using Android Beam (TM). Every
747 * Uri you provide must have either scheme 'file' or scheme 'content'.
748 *
749 * <p>For the data provided through this method, Android Beam tries to
750 * switch to alternate transports such as Bluetooth to achieve a fast
751 * transfer speed. Hence this method is very suitable
752 * for transferring large files such as pictures or songs.
753 *
754 * <p>The receiving side will store the content of each Uri in
755 * a file and present a notification to the user to open the file
756 * with a {@link android.content.Intent} with action
757 * {@link android.content.Intent#ACTION_VIEW}.
758 * If multiple URIs are sent, the {@link android.content.Intent} will refer
759 * to the first of the stored files.
760 *
761 * <p>This method may be called at any time before {@link Activity#onDestroy},
762 * but the URI(s) are only made available for Android Beam when the
763 * specified activity(s) are in resumed (foreground) state. The recommended
764 * approach is to call this method during your Activity's
765 * {@link Activity#onCreate} - see sample
766 * code below. This method does not immediately perform any I/O or blocking work,
767 * so is safe to call on your main thread.
768 *
769 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
770 * have priority over both {@link #setNdefPushMessage} and
771 * {@link #setNdefPushMessageCallback}.
772 *
773 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
774 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
775 * then the Uri push will be completely disabled for the specified activity(s).
776 *
777 * <p>Code example:
778 * <pre>
779 * protected void onCreate(Bundle savedInstanceState) {
780 * super.onCreate(savedInstanceState);
781 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
782 * if (nfcAdapter == null) return; // NFC not available on this device
783 * nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700784 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700785 * And that is it. Only one call per activity is necessary. The Android
786 * OS will automatically release its references to the Uri(s) and the
787 * Activity object when it is destroyed if you follow this pattern.
788 *
789 * <p>If your Activity wants to dynamically supply Uri(s),
790 * then set a callback using {@link #setBeamPushUrisCallback} instead
791 * of using this method.
792 *
793 * <p class="note">Do not pass in an Activity that has already been through
794 * {@link Activity#onDestroy}. This is guaranteed if you call this API
795 * during {@link Activity#onCreate}.
796 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700797 * <p class="note">If this device does not support alternate transports
798 * such as Bluetooth or WiFI, calling this method does nothing.
799 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700800 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
801 *
802 * @param uris an array of Uri(s) to push over Android Beam
803 * @param activity activity for which the Uri(s) will be pushed
804 */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700805 public void setBeamPushUris(Uri[] uris, Activity activity) {
Nick Pelly1d7e9062012-04-03 17:46:00 -0700806 if (activity == null) {
807 throw new NullPointerException("activity cannot be null");
808 }
Martijn Coenen2c103112012-05-15 10:32:15 -0700809 if (uris != null) {
810 for (Uri uri : uris) {
811 if (uri == null) throw new NullPointerException("Uri not " +
812 "allowed to be null");
813 String scheme = uri.getScheme();
814 if (scheme == null || (!scheme.equalsIgnoreCase("file") &&
815 !scheme.equalsIgnoreCase("content"))) {
816 throw new IllegalArgumentException("URI needs to have " +
817 "either scheme file or scheme content");
818 }
819 }
820 }
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700821 mNfcActivityManager.setNdefPushContentUri(activity, uris);
822 }
823
Martijn Coenen2c103112012-05-15 10:32:15 -0700824 /**
825 * Set a callback that will dynamically generate one or more {@link Uri}s
826 * to send using Android Beam (TM). Every Uri the callback provides
827 * must have either scheme 'file' or scheme 'content'.
828 *
829 * <p>For the data provided through this callback, Android Beam tries to
830 * switch to alternate transports such as Bluetooth to achieve a fast
831 * transfer speed. Hence this method is very suitable
832 * for transferring large files such as pictures or songs.
833 *
834 * <p>The receiving side will store the content of each Uri in
835 * a file and present a notification to the user to open the file
836 * with a {@link android.content.Intent} with action
837 * {@link android.content.Intent#ACTION_VIEW}.
838 * If multiple URIs are sent, the {@link android.content.Intent} will refer
839 * to the first of the stored files.
840 *
841 * <p>This method may be called at any time before {@link Activity#onDestroy},
842 * but the URI(s) are only made available for Android Beam when the
843 * specified activity(s) are in resumed (foreground) state. The recommended
844 * approach is to call this method during your Activity's
845 * {@link Activity#onCreate} - see sample
846 * code below. This method does not immediately perform any I/O or blocking work,
847 * so is safe to call on your main thread.
848 *
849 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
850 * have priority over both {@link #setNdefPushMessage} and
851 * {@link #setNdefPushMessageCallback}.
852 *
853 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
854 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
855 * then the Uri push will be completely disabled for the specified activity(s).
856 *
857 * <p>Code example:
858 * <pre>
859 * protected void onCreate(Bundle savedInstanceState) {
860 * super.onCreate(savedInstanceState);
861 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
862 * if (nfcAdapter == null) return; // NFC not available on this device
863 * nfcAdapter.setBeamPushUrisCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700864 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700865 * And that is it. Only one call per activity is necessary. The Android
866 * OS will automatically release its references to the Uri(s) and the
867 * Activity object when it is destroyed if you follow this pattern.
868 *
869 * <p class="note">Do not pass in an Activity that has already been through
870 * {@link Activity#onDestroy}. This is guaranteed if you call this API
871 * during {@link Activity#onCreate}.
872 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700873 * <p class="note">If this device does not support alternate transports
874 * such as Bluetooth or WiFI, calling this method does nothing.
875 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700876 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
877 *
878 * @param callback callback, or null to disable
879 * @param activity activity for which the Uri(s) will be pushed
880 */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700881 public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) {
882 if (activity == null) {
883 throw new NullPointerException("activity cannot be null");
884 }
885 mNfcActivityManager.setNdefPushContentUriCallback(activity, callback);
Nick Pelly1d7e9062012-04-03 17:46:00 -0700886 }
887
Jeff Hamilton52d32032011-01-08 15:31:26 -0600888 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -0700889 * Set a static {@link NdefMessage} to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -0700890 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700891 * <p>This method may be called at any time before {@link Activity#onDestroy},
892 * but the NDEF message is only made available for NDEF push when the
893 * specified activity(s) are in resumed (foreground) state. The recommended
894 * approach is to call this method during your Activity's
895 * {@link Activity#onCreate} - see sample
896 * code below. This method does not immediately perform any I/O or blocking work,
897 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700898 *
899 * <p>Only one NDEF message can be pushed by the currently resumed activity.
900 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -0700901 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -0700902 * the callback will take priority.
903 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700904 * <p>If neither {@link #setNdefPushMessage} or
905 * {@link #setNdefPushMessageCallback} have been called for your activity, then
906 * the Android OS may choose to send a default NDEF message on your behalf,
907 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700908 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700909 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
910 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
911 * then NDEF push will be completely disabled for the specified activity(s).
912 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -0700913 * otherwise sent on your behalf for those activity(s).
914 *
915 * <p>If you want to prevent the Android OS from sending default NDEF
916 * messages completely (for all activities), you can include a
Scott Main97e0a1c2012-06-25 11:22:10 -0700917 * {@code &lt;meta-data>} element inside the {@code &lt;application>}
Martijn Coenen854e0772012-04-02 14:52:10 -0700918 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -0700919 * <pre>
920 * &lt;application ...>
921 * &lt;meta-data android:name="android.nfc.disable_beam_default"
922 * android:value="true" />
923 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700924 *
925 * <p>The API allows for multiple activities to be specified at a time,
926 * but it is strongly recommended to just register one at a time,
927 * and to do so during the activity's {@link Activity#onCreate}. For example:
928 * <pre>
929 * protected void onCreate(Bundle savedInstanceState) {
930 * super.onCreate(savedInstanceState);
931 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
932 * if (nfcAdapter == null) return; // NFC not available on this device
933 * nfcAdapter.setNdefPushMessage(ndefMessage, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700934 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700935 * And that is it. Only one call per activity is necessary. The Android
936 * OS will automatically release its references to the NDEF message and the
937 * Activity object when it is destroyed if you follow this pattern.
938 *
939 * <p>If your Activity wants to dynamically generate an NDEF message,
940 * then set a callback using {@link #setNdefPushMessageCallback} instead
941 * of a static message.
942 *
943 * <p class="note">Do not pass in an Activity that has already been through
944 * {@link Activity#onDestroy}. This is guaranteed if you call this API
945 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -0700946 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700947 * <p class="note">For sending large content such as pictures and songs,
948 * consider using {@link #setBeamPushUris}, which switches to alternate transports
949 * such as Bluetooth to achieve a fast transfer rate.
950 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700951 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
952 *
953 * @param message NDEF message to push over NFC, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -0700954 * @param activity activity for which the NDEF message will be pushed
955 * @param activities optional additional activities, however we strongly recommend
956 * to only register one at a time, and to do so in that activity's
957 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -0700958 */
Nick Pelly82328bf2011-08-30 09:37:25 -0700959 public void setNdefPushMessage(NdefMessage message, Activity activity,
960 Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -0700961 int targetSdkVersion = getSdkVersion();
962 try {
963 if (activity == null) {
964 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -0700965 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800966 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -0700967 for (Activity a : activities) {
968 if (a == null) {
969 throw new NullPointerException("activities cannot contain null");
970 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800971 mNfcActivityManager.setNdefPushMessage(a, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -0700972 }
973 } catch (IllegalStateException e) {
974 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
975 // Less strict on old applications - just log the error
976 Log.e(TAG, "Cannot call API with Activity that has already " +
977 "been destroyed", e);
978 } else {
979 // Prevent new applications from making this mistake, re-throw
980 throw(e);
981 }
Nick Pellyc84c89a2011-08-22 22:27:11 -0700982 }
983 }
984
985 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800986 * @hide
987 */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700988 @SystemApi
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800989 public void setNdefPushMessage(NdefMessage message, Activity activity, int flags) {
990 if (activity == null) {
991 throw new NullPointerException("activity cannot be null");
992 }
993 mNfcActivityManager.setNdefPushMessage(activity, message, flags);
994 }
995
996 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -0700997 * Set a callback that dynamically generates NDEF messages to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -0700998 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700999 * <p>This method may be called at any time before {@link Activity#onDestroy},
1000 * but the NDEF message callback can only occur when the
1001 * specified activity(s) are in resumed (foreground) state. The recommended
1002 * approach is to call this method during your Activity's
1003 * {@link Activity#onCreate} - see sample
1004 * code below. This method does not immediately perform any I/O or blocking work,
1005 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001006 *
1007 * <p>Only one NDEF message can be pushed by the currently resumed activity.
1008 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -07001009 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -07001010 * the callback will take priority.
1011 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001012 * <p>If neither {@link #setNdefPushMessage} or
1013 * {@link #setNdefPushMessageCallback} have been called for your activity, then
1014 * the Android OS may choose to send a default NDEF message on your behalf,
1015 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001016 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001017 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
1018 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
1019 * then NDEF push will be completely disabled for the specified activity(s).
1020 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -07001021 * otherwise sent on your behalf for those activity(s).
1022 *
1023 * <p>If you want to prevent the Android OS from sending default NDEF
1024 * messages completely (for all activities), you can include a
Scott Main97e0a1c2012-06-25 11:22:10 -07001025 * {@code &lt;meta-data>} element inside the {@code &lt;application>}
Martijn Coenen854e0772012-04-02 14:52:10 -07001026 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -07001027 * <pre>
1028 * &lt;application ...>
1029 * &lt;meta-data android:name="android.nfc.disable_beam_default"
1030 * android:value="true" />
1031 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001032 *
1033 * <p>The API allows for multiple activities to be specified at a time,
1034 * but it is strongly recommended to just register one at a time,
1035 * and to do so during the activity's {@link Activity#onCreate}. For example:
1036 * <pre>
1037 * protected void onCreate(Bundle savedInstanceState) {
1038 * super.onCreate(savedInstanceState);
1039 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1040 * if (nfcAdapter == null) return; // NFC not available on this device
1041 * nfcAdapter.setNdefPushMessageCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001042 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001043 * And that is it. Only one call per activity is necessary. The Android
1044 * OS will automatically release its references to the callback and the
1045 * Activity object when it is destroyed if you follow this pattern.
1046 *
1047 * <p class="note">Do not pass in an Activity that has already been through
1048 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1049 * during {@link Activity#onCreate}.
Martijn Coenen2c103112012-05-15 10:32:15 -07001050 * <p class="note">For sending large content such as pictures and songs,
1051 * consider using {@link #setBeamPushUris}, which switches to alternate transports
1052 * such as Bluetooth to achieve a fast transfer rate.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001053 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1054 *
1055 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001056 * @param activity activity for which the NDEF message will be pushed
1057 * @param activities optional additional activities, however we strongly recommend
1058 * to only register one at a time, and to do so in that activity's
1059 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -07001060 */
Nick Pelly82328bf2011-08-30 09:37:25 -07001061 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
Nick Pellyc84c89a2011-08-22 22:27:11 -07001062 Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -07001063 int targetSdkVersion = getSdkVersion();
1064 try {
1065 if (activity == null) {
1066 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001067 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001068 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001069 for (Activity a : activities) {
1070 if (a == null) {
1071 throw new NullPointerException("activities cannot contain null");
1072 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001073 mNfcActivityManager.setNdefPushMessageCallback(a, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001074 }
1075 } catch (IllegalStateException e) {
1076 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1077 // Less strict on old applications - just log the error
1078 Log.e(TAG, "Cannot call API with Activity that has already " +
1079 "been destroyed", e);
1080 } else {
1081 // Prevent new applications from making this mistake, re-throw
1082 throw(e);
1083 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001084 }
1085 }
1086
1087 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001088 * @hide
1089 */
1090 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
1091 int flags) {
1092 if (activity == null) {
1093 throw new NullPointerException("activity cannot be null");
1094 }
1095 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, flags);
1096 }
1097
1098 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -07001099 * Set a callback on successful Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -07001100 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001101 * <p>This method may be called at any time before {@link Activity#onDestroy},
1102 * but the callback can only occur when the
1103 * specified activity(s) are in resumed (foreground) state. The recommended
1104 * approach is to call this method during your Activity's
1105 * {@link Activity#onCreate} - see sample
1106 * code below. This method does not immediately perform any I/O or blocking work,
1107 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001108 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001109 * <p>The API allows for multiple activities to be specified at a time,
1110 * but it is strongly recommended to just register one at a time,
1111 * and to do so during the activity's {@link Activity#onCreate}. For example:
1112 * <pre>
1113 * protected void onCreate(Bundle savedInstanceState) {
1114 * super.onCreate(savedInstanceState);
1115 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1116 * if (nfcAdapter == null) return; // NFC not available on this device
1117 * nfcAdapter.setOnNdefPushCompleteCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001118 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001119 * And that is it. Only one call per activity is necessary. The Android
1120 * OS will automatically release its references to the callback and the
1121 * Activity object when it is destroyed if you follow this pattern.
1122 *
1123 * <p class="note">Do not pass in an Activity that has already been through
1124 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1125 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -07001126 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001127 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1128 *
1129 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001130 * @param activity activity for which the NDEF message will be pushed
1131 * @param activities optional additional activities, however we strongly recommend
1132 * to only register one at a time, and to do so in that activity's
1133 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -07001134 */
1135 public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback,
Nick Pelly82328bf2011-08-30 09:37:25 -07001136 Activity activity, Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -07001137 int targetSdkVersion = getSdkVersion();
1138 try {
1139 if (activity == null) {
1140 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001141 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001142 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, callback);
1143 for (Activity a : activities) {
1144 if (a == null) {
1145 throw new NullPointerException("activities cannot contain null");
1146 }
1147 mNfcActivityManager.setOnNdefPushCompleteCallback(a, callback);
1148 }
1149 } catch (IllegalStateException e) {
1150 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1151 // Less strict on old applications - just log the error
1152 Log.e(TAG, "Cannot call API with Activity that has already " +
1153 "been destroyed", e);
1154 } else {
1155 // Prevent new applications from making this mistake, re-throw
1156 throw(e);
1157 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001158 }
1159 }
1160
1161 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001162 * Enable foreground dispatch to the given Activity.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001163 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001164 * <p>This will give give priority to the foreground activity when
1165 * dispatching a discovered {@link Tag} to an application.
1166 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001167 * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents
1168 * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and
1169 * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED}
1170 * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled
1171 * by passing in the tech lists separately. Each first level entry in the tech list represents
1172 * an array of technologies that must all be present to match. If any of the first level sets
1173 * match then the dispatch is routed through the given PendingIntent. In other words, the second
1174 * level is ANDed together and the first level entries are ORed together.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001175 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001176 * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters
1177 * that acts a wild card and will cause the foreground activity to receive all tags via the
1178 * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001179 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001180 * <p>This method must be called from the main thread, and only when the activity is in the
1181 * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before
1182 * the completion of their {@link Activity#onPause} callback to disable foreground dispatch
1183 * after it has been enabled.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001184 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001185 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1186 *
Jeff Hamilton52d32032011-01-08 15:31:26 -06001187 * @param activity the Activity to dispatch to
1188 * @param intent the PendingIntent to start for the dispatch
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001189 * @param filters the IntentFilters to override dispatching for, or null to always dispatch
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001190 * @param techLists the tech lists used to perform matching for dispatching of the
1191 * {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent
Nick Pelly74fe6c62011-02-02 22:37:40 -08001192 * @throws IllegalStateException if the Activity is not currently in the foreground
Jeff Hamilton52d32032011-01-08 15:31:26 -06001193 */
1194 public void enableForegroundDispatch(Activity activity, PendingIntent intent,
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001195 IntentFilter[] filters, String[][] techLists) {
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001196 if (activity == null || intent == null) {
Jeff Hamilton52d32032011-01-08 15:31:26 -06001197 throw new NullPointerException();
1198 }
1199 if (!activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001200 throw new IllegalStateException("Foreground dispatch can only be enabled " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001201 "when your activity is resumed");
1202 }
1203 try {
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001204 TechListParcel parcel = null;
1205 if (techLists != null && techLists.length > 0) {
1206 parcel = new TechListParcel(techLists);
1207 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06001208 ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001209 mForegroundDispatchListener);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001210 sService.setForegroundDispatch(intent, filters, parcel);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001211 } catch (RemoteException e) {
1212 attemptDeadServiceRecovery(e);
1213 }
1214 }
1215
1216 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001217 * Disable foreground dispatch to the given activity.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001218 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001219 * <p>After calling {@link #enableForegroundDispatch}, an activity
1220 * must call this method before its {@link Activity#onPause} callback
1221 * completes.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001222 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001223 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001224 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001225 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1226 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001227 * @param activity the Activity to disable dispatch to
1228 * @throws IllegalStateException if the Activity has already been paused
Jeff Hamilton52d32032011-01-08 15:31:26 -06001229 */
1230 public void disableForegroundDispatch(Activity activity) {
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001231 ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,
1232 mForegroundDispatchListener);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001233 disableForegroundDispatchInternal(activity, false);
1234 }
1235
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001236 OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001237 @Override
1238 public void onPaused(Activity activity) {
1239 disableForegroundDispatchInternal(activity, true);
1240 }
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001241 };
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001242
Jeff Hamilton52d32032011-01-08 15:31:26 -06001243 void disableForegroundDispatchInternal(Activity activity, boolean force) {
1244 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001245 sService.setForegroundDispatch(null, null, null);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001246 if (!force && !activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001247 throw new IllegalStateException("You must disable foreground dispatching " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001248 "while your activity is still resumed");
1249 }
1250 } catch (RemoteException e) {
1251 attemptDeadServiceRecovery(e);
1252 }
1253 }
1254
Nick Pelly590b73b2010-10-12 13:00:50 -07001255 /**
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001256 * Limit the NFC controller to reader mode while this Activity is in the foreground.
1257 *
1258 * <p>In this mode the NFC controller will only act as an NFC tag reader/writer,
1259 * thus disabling any peer-to-peer (Android Beam) and card-emulation modes of
1260 * the NFC adapter on this device.
1261 *
1262 * <p>Use {@link #FLAG_READER_SKIP_NDEF_CHECK} to prevent the platform from
1263 * performing any NDEF checks in reader mode. Note that this will prevent the
1264 * {@link Ndef} tag technology from being enumerated on the tag, and that
1265 * NDEF-based tag dispatch will not be functional.
1266 *
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001267 * <p>For interacting with tags that are emulated on another Android device
1268 * using Android's host-based card-emulation, the recommended flags are
1269 * {@link #FLAG_READER_NFC_A} and {@link #FLAG_READER_SKIP_NDEF_CHECK}.
1270 *
1271 * @param activity the Activity that requests the adapter to be in reader mode
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001272 * @param callback the callback to be called when a tag is discovered
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001273 * @param flags Flags indicating poll technologies and other optional parameters
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001274 * @param extras Additional extras for configuring reader mode.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001275 */
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001276 public void enableReaderMode(Activity activity, ReaderCallback callback, int flags,
1277 Bundle extras) {
1278 mNfcActivityManager.enableReaderMode(activity, callback, flags, extras);
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001279 }
1280
1281 /**
1282 * Restore the NFC adapter to normal mode of operation: supporting
1283 * peer-to-peer (Android Beam), card emulation, and polling for
1284 * all supported tag technologies.
1285 *
1286 * @param activity the Activity that currently has reader mode enabled
1287 */
1288 public void disableReaderMode(Activity activity) {
1289 mNfcActivityManager.disableReaderMode(activity);
1290 }
1291
1292 /**
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001293 * Manually invoke Android Beam to share data.
1294 *
1295 * <p>The Android Beam animation is normally only shown when two NFC-capable
1296 * devices come into range.
1297 * By calling this method, an Activity can invoke the Beam animation directly
1298 * even if no other NFC device is in range yet. The Beam animation will then
1299 * prompt the user to tap another NFC-capable device to complete the data
1300 * transfer.
1301 *
1302 * <p>The main advantage of using this method is that it avoids the need for the
1303 * user to tap the screen to complete the transfer, as this method already
1304 * establishes the direction of the transfer and the consent of the user to
1305 * share data. Callers are responsible for making sure that the user has
1306 * consented to sharing data on NFC tap.
1307 *
1308 * <p>Note that to use this method, the passed in Activity must have already
1309 * set data to share over Beam by using method calls such as
Martijn Coenen6924c4e2014-02-25 11:37:17 -08001310 * {@link #setNdefPushMessageCallback} or
1311 * {@link #setBeamPushUrisCallback}.
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001312 *
1313 * @param activity the current foreground Activity that has registered data to share
1314 * @return whether the Beam animation was successfully invoked
1315 */
1316 public boolean invokeBeam(Activity activity) {
1317 if (activity == null) {
1318 throw new NullPointerException("activity may not be null.");
1319 }
1320 enforceResumed(activity);
1321 try {
1322 sService.invokeBeam();
1323 return true;
1324 } catch (RemoteException e) {
1325 Log.e(TAG, "invokeBeam: NFC process has died.");
1326 attemptDeadServiceRecovery(e);
1327 return false;
1328 }
1329 }
1330
1331 /**
Martijn Coenendbedb4c2014-07-01 15:52:19 -07001332 * @hide
1333 */
1334 public boolean invokeBeam(BeamShareData shareData) {
1335 try {
1336 Log.e(TAG, "invokeBeamInternal()");
1337 sService.invokeBeamInternal(shareData);
1338 return true;
1339 } catch (RemoteException e) {
1340 Log.e(TAG, "invokeBeam: NFC process has died.");
1341 attemptDeadServiceRecovery(e);
1342 return false;
1343 }
1344 }
1345
1346 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001347 * Enable NDEF message push over NFC while this Activity is in the foreground.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001348 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001349 * <p>You must explicitly call this method every time the activity is
1350 * resumed, and you must call {@link #disableForegroundNdefPush} before
1351 * your activity completes {@link Activity#onPause}.
1352 *
1353 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1354 * instead: it automatically hooks into your activity life-cycle,
1355 * so you do not need to call enable/disable in your onResume/onPause.
1356 *
1357 * <p>For NDEF push to function properly the other NFC device must
1358 * support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or
1359 * Android's "com.android.npp" (Ndef Push Protocol). This was optional
1360 * on Gingerbread level Android NFC devices, but SNEP is mandatory on
1361 * Ice-Cream-Sandwich and beyond.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001362 *
1363 * <p>This method must be called from the main thread.
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001364 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001365 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1366 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001367 * @param activity foreground activity
1368 * @param message a NDEF Message to push over NFC
1369 * @throws IllegalStateException if the activity is not currently in the foreground
1370 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001371 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001372 @Deprecated
1373 public void enableForegroundNdefPush(Activity activity, NdefMessage message) {
1374 if (activity == null || message == null) {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001375 throw new NullPointerException();
1376 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001377 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001378 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Jason parks01425362011-05-24 02:57:37 -07001379 }
1380
1381 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001382 * Disable NDEF message push over P2P.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001383 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001384 * <p>After calling {@link #enableForegroundNdefPush}, an activity
1385 * must call this method before its {@link Activity#onPause} callback
1386 * completes.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001387 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001388 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1389 * instead: it automatically hooks into your activity life-cycle,
1390 * so you do not need to call enable/disable in your onResume/onPause.
1391 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001392 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001393 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001394 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1395 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001396 * @param activity the Foreground activity
1397 * @throws IllegalStateException if the Activity has already been paused
Nick Pellyc84c89a2011-08-22 22:27:11 -07001398 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001399 */
Nick Pellyc97a5522012-01-05 15:13:01 +11001400 @Deprecated
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001401 public void disableForegroundNdefPush(Activity activity) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001402 if (activity == null) {
1403 throw new NullPointerException();
1404 }
1405 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001406 mNfcActivityManager.setNdefPushMessage(activity, null, 0);
1407 mNfcActivityManager.setNdefPushMessageCallback(activity, null, 0);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001408 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, null);
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001409 }
1410
Nick Pellyc84c89a2011-08-22 22:27:11 -07001411 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001412 * Enable NDEF Push feature.
1413 * <p>This API is for the Settings application.
1414 * @hide
1415 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001416 @SystemApi
Nick Pellyc84c89a2011-08-22 22:27:11 -07001417 public boolean enableNdefPush() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001418 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001419 return sService.enableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001420 } catch (RemoteException e) {
1421 attemptDeadServiceRecovery(e);
1422 return false;
1423 }
1424 }
1425
1426 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001427 * Disable NDEF Push feature.
1428 * <p>This API is for the Settings application.
Martijn Coenen6d748942011-07-21 09:34:25 +02001429 * @hide
1430 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001431 @SystemApi
Nick Pellyc84c89a2011-08-22 22:27:11 -07001432 public boolean disableNdefPush() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001433 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001434 return sService.disableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001435 } catch (RemoteException e) {
1436 attemptDeadServiceRecovery(e);
1437 return false;
1438 }
1439 }
1440
1441 /**
Nick Pellycccf01d2011-10-31 14:49:40 -07001442 * Return true if the NDEF Push (Android Beam) feature is enabled.
1443 * <p>This function will return true only if both NFC is enabled, and the
1444 * NDEF Push feature is enabled.
1445 * <p>Note that if NFC is enabled but NDEF Push is disabled then this
1446 * device can still <i>receive</i> NDEF messages, it just cannot send them.
1447 * <p>Applications cannot directly toggle the NDEF Push feature, but they
1448 * can request Settings UI allowing the user to toggle NDEF Push using
1449 * <code>startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))</code>
1450 * <p>Example usage in an Activity that requires NDEF Push:
1451 * <p><pre>
1452 * protected void onResume() {
1453 * super.onResume();
1454 * if (!nfcAdapter.isEnabled()) {
1455 * startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
1456 * } else if (!nfcAdapter.isNdefPushEnabled()) {
1457 * startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
1458 * }
Scott Main97e0a1c2012-06-25 11:22:10 -07001459 * }</pre>
Martijn Coenen6d748942011-07-21 09:34:25 +02001460 *
Nick Pellycccf01d2011-10-31 14:49:40 -07001461 * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS
Nick Pellyc84c89a2011-08-22 22:27:11 -07001462 * @return true if NDEF Push feature is enabled
Martijn Coenen6d748942011-07-21 09:34:25 +02001463 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001464 public boolean isNdefPushEnabled() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001465 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001466 return sService.isNdefPushEnabled();
Martijn Coenen6d748942011-07-21 09:34:25 +02001467 } catch (RemoteException e) {
1468 attemptDeadServiceRecovery(e);
1469 return false;
1470 }
1471 }
1472
1473 /**
Nick Pellyc97a5522012-01-05 15:13:01 +11001474 * Inject a mock NFC tag.<p>
1475 * Used for testing purposes.
1476 * <p class="note">Requires the
1477 * {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
1478 * @hide
1479 */
Nick Pelly1f5badc2012-01-24 13:22:58 -08001480 public void dispatch(Tag tag) {
Nick Pellyc97a5522012-01-05 15:13:01 +11001481 if (tag == null) {
1482 throw new NullPointerException("tag cannot be null");
1483 }
1484 try {
Nick Pelly1f5badc2012-01-24 13:22:58 -08001485 sService.dispatch(tag);
Nick Pellyc97a5522012-01-05 15:13:01 +11001486 } catch (RemoteException e) {
1487 attemptDeadServiceRecovery(e);
1488 }
1489 }
1490
1491 /**
Daniel Tomas90245642010-11-17 10:07:52 +01001492 * @hide
1493 */
Martijn Coenen188cddb2012-01-31 22:16:15 -08001494 public void setP2pModes(int initiatorModes, int targetModes) {
1495 try {
1496 sService.setP2pModes(initiatorModes, targetModes);
1497 } catch (RemoteException e) {
1498 attemptDeadServiceRecovery(e);
1499 }
1500 }
1501
1502 /**
Andres Morales11d2e532014-07-23 12:39:55 -07001503 * Registers a new NFC unlock handler with the NFC service.
1504 *
1505 * <p />NFC unlock handlers are intended to unlock the keyguard in the presence of a trusted
1506 * NFC device. The handler should return true if it successfully authenticates the user and
1507 * unlocks the keyguard.
1508 *
1509 * <p /> The parameter {@code tagTechnologies} determines which Tag technologies will be polled for
1510 * at the lockscreen. Polling for less tag technologies reduces latency, and so it is
1511 * strongly recommended to only provide the Tag technologies that the handler is expected to
Andres Moralesf9a97942014-08-14 15:43:30 -07001512 * receive. There must be at least one tag technology provided, otherwise the unlock handler
1513 * is ignored.
Andres Morales11d2e532014-07-23 12:39:55 -07001514 *
1515 * @hide
1516 */
1517 @SystemApi
1518 public boolean addNfcUnlockHandler(final NfcUnlockHandler unlockHandler,
1519 String[] tagTechnologies) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001520 // If there are no tag technologies, don't bother adding unlock handler
1521 if (tagTechnologies.length == 0) {
1522 return false;
1523 }
Andres Morales11d2e532014-07-23 12:39:55 -07001524
Andres Moralesf9a97942014-08-14 15:43:30 -07001525 try {
Andres Morales11d2e532014-07-23 12:39:55 -07001526 synchronized (mLock) {
1527 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001528 // update the tag technologies
1529 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.get(unlockHandler));
1530 mNfcUnlockHandlers.remove(unlockHandler);
Andres Morales11d2e532014-07-23 12:39:55 -07001531 }
Andres Moralesf9a97942014-08-14 15:43:30 -07001532
1533 INfcUnlockHandler.Stub iHandler = new INfcUnlockHandler.Stub() {
1534 @Override
1535 public boolean onUnlockAttempted(Tag tag) throws RemoteException {
1536 return unlockHandler.onUnlockAttempted(tag);
1537 }
1538 };
1539
1540 sService.addNfcUnlockHandler(iHandler,
1541 Tag.getTechCodesFromStrings(tagTechnologies));
1542 mNfcUnlockHandlers.put(unlockHandler, iHandler);
Andres Morales11d2e532014-07-23 12:39:55 -07001543 }
1544 } catch (RemoteException e) {
1545 attemptDeadServiceRecovery(e);
1546 return false;
1547 } catch (IllegalArgumentException e) {
1548 Log.e(TAG, "Unable to register LockscreenDispatch", e);
1549 return false;
1550 }
1551
1552 return true;
1553 }
1554
1555 /**
1556 * Removes a previously registered unlock handler. Also removes the tag technologies
1557 * associated with the removed unlock handler.
1558 *
1559 * @hide
1560 */
1561 @SystemApi
1562 public boolean removeNfcUnlockHandler(NfcUnlockHandler unlockHandler) {
1563 try {
1564 synchronized (mLock) {
1565 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001566 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.remove(unlockHandler));
Andres Morales11d2e532014-07-23 12:39:55 -07001567 }
1568
1569 return true;
1570 }
1571 } catch (RemoteException e) {
1572 attemptDeadServiceRecovery(e);
1573 return false;
1574 }
1575 }
1576
1577 /**
Martijn Coenen188cddb2012-01-31 22:16:15 -08001578 * @hide
1579 */
Nick Pelly367f41f2011-03-08 11:43:30 -08001580 public INfcAdapterExtras getNfcAdapterExtrasInterface() {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06001581 if (mContext == null) {
1582 throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
1583 + " NFC extras APIs");
1584 }
Daniel Tomas90245642010-11-17 10:07:52 +01001585 try {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06001586 return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
Daniel Tomas90245642010-11-17 10:07:52 +01001587 } catch (RemoteException e) {
Nick Pelly367f41f2011-03-08 11:43:30 -08001588 attemptDeadServiceRecovery(e);
Daniel Tomas90245642010-11-17 10:07:52 +01001589 return null;
1590 }
1591 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001592
1593 void enforceResumed(Activity activity) {
1594 if (!activity.isResumed()) {
1595 throw new IllegalStateException("API cannot be called while activity is paused");
1596 }
1597 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001598
1599 int getSdkVersion() {
1600 if (mContext == null) {
1601 return android.os.Build.VERSION_CODES.GINGERBREAD; // best guess
1602 } else {
1603 return mContext.getApplicationInfo().targetSdkVersion;
1604 }
1605 }
Nick Pelly590b73b2010-10-12 13:00:50 -07001606}