blob: 300301b5e7fbf265d8b96e02b22d73c1e2a1cf26 [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 /**
Andres Morales9c4f4002014-09-08 17:09:16 -0700705 * Pauses polling for a {@code timeoutInMs} millis. If polling must be resumed before timeout,
706 * use {@link #resumePolling()}.
707 * @hide
708 */
709 public void pausePolling(int timeoutInMs) {
710 try {
711 sService.pausePolling(timeoutInMs);
712 } catch (RemoteException e) {
713 attemptDeadServiceRecovery(e);
714 }
715 }
716
717 /**
718 * Resumes default polling for the current device state if polling is paused. Calling
719 * this while polling is not paused is a no-op.
720 *
721 * @hide
722 */
723 public void resumePolling() {
724 try {
725 sService.resumePolling();
726 } catch (RemoteException e) {
727 attemptDeadServiceRecovery(e);
728 }
729 }
730
731 /**
Martijn Coenen2c103112012-05-15 10:32:15 -0700732 * Set one or more {@link Uri}s to send using Android Beam (TM). Every
733 * Uri you provide must have either scheme 'file' or scheme 'content'.
734 *
735 * <p>For the data provided through this method, Android Beam tries to
736 * switch to alternate transports such as Bluetooth to achieve a fast
737 * transfer speed. Hence this method is very suitable
738 * for transferring large files such as pictures or songs.
739 *
740 * <p>The receiving side will store the content of each Uri in
741 * a file and present a notification to the user to open the file
742 * with a {@link android.content.Intent} with action
743 * {@link android.content.Intent#ACTION_VIEW}.
744 * If multiple URIs are sent, the {@link android.content.Intent} will refer
745 * to the first of the stored files.
746 *
747 * <p>This method may be called at any time before {@link Activity#onDestroy},
748 * but the URI(s) are only made available for Android Beam when the
749 * specified activity(s) are in resumed (foreground) state. The recommended
750 * approach is to call this method during your Activity's
751 * {@link Activity#onCreate} - see sample
752 * code below. This method does not immediately perform any I/O or blocking work,
753 * so is safe to call on your main thread.
754 *
755 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
756 * have priority over both {@link #setNdefPushMessage} and
757 * {@link #setNdefPushMessageCallback}.
758 *
759 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
760 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
761 * then the Uri push will be completely disabled for the specified activity(s).
762 *
763 * <p>Code example:
764 * <pre>
765 * protected void onCreate(Bundle savedInstanceState) {
766 * super.onCreate(savedInstanceState);
767 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
768 * if (nfcAdapter == null) return; // NFC not available on this device
769 * nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700770 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700771 * And that is it. Only one call per activity is necessary. The Android
772 * OS will automatically release its references to the Uri(s) and the
773 * Activity object when it is destroyed if you follow this pattern.
774 *
775 * <p>If your Activity wants to dynamically supply Uri(s),
776 * then set a callback using {@link #setBeamPushUrisCallback} instead
777 * of using this method.
778 *
779 * <p class="note">Do not pass in an Activity that has already been through
780 * {@link Activity#onDestroy}. This is guaranteed if you call this API
781 * during {@link Activity#onCreate}.
782 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700783 * <p class="note">If this device does not support alternate transports
784 * such as Bluetooth or WiFI, calling this method does nothing.
785 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700786 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
787 *
788 * @param uris an array of Uri(s) to push over Android Beam
789 * @param activity activity for which the Uri(s) will be pushed
790 */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700791 public void setBeamPushUris(Uri[] uris, Activity activity) {
Nick Pelly1d7e9062012-04-03 17:46:00 -0700792 if (activity == null) {
793 throw new NullPointerException("activity cannot be null");
794 }
Martijn Coenen2c103112012-05-15 10:32:15 -0700795 if (uris != null) {
796 for (Uri uri : uris) {
797 if (uri == null) throw new NullPointerException("Uri not " +
798 "allowed to be null");
799 String scheme = uri.getScheme();
800 if (scheme == null || (!scheme.equalsIgnoreCase("file") &&
801 !scheme.equalsIgnoreCase("content"))) {
802 throw new IllegalArgumentException("URI needs to have " +
803 "either scheme file or scheme content");
804 }
805 }
806 }
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700807 mNfcActivityManager.setNdefPushContentUri(activity, uris);
808 }
809
Martijn Coenen2c103112012-05-15 10:32:15 -0700810 /**
811 * Set a callback that will dynamically generate one or more {@link Uri}s
812 * to send using Android Beam (TM). Every Uri the callback provides
813 * must have either scheme 'file' or scheme 'content'.
814 *
815 * <p>For the data provided through this callback, Android Beam tries to
816 * switch to alternate transports such as Bluetooth to achieve a fast
817 * transfer speed. Hence this method is very suitable
818 * for transferring large files such as pictures or songs.
819 *
820 * <p>The receiving side will store the content of each Uri in
821 * a file and present a notification to the user to open the file
822 * with a {@link android.content.Intent} with action
823 * {@link android.content.Intent#ACTION_VIEW}.
824 * If multiple URIs are sent, the {@link android.content.Intent} will refer
825 * to the first of the stored files.
826 *
827 * <p>This method may be called at any time before {@link Activity#onDestroy},
828 * but the URI(s) are only made available for Android Beam when the
829 * specified activity(s) are in resumed (foreground) state. The recommended
830 * approach is to call this method during your Activity's
831 * {@link Activity#onCreate} - see sample
832 * code below. This method does not immediately perform any I/O or blocking work,
833 * so is safe to call on your main thread.
834 *
835 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
836 * have priority over both {@link #setNdefPushMessage} and
837 * {@link #setNdefPushMessageCallback}.
838 *
839 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
840 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
841 * then the Uri push will be completely disabled for the specified activity(s).
842 *
843 * <p>Code example:
844 * <pre>
845 * protected void onCreate(Bundle savedInstanceState) {
846 * super.onCreate(savedInstanceState);
847 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
848 * if (nfcAdapter == null) return; // NFC not available on this device
849 * nfcAdapter.setBeamPushUrisCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700850 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700851 * And that is it. Only one call per activity is necessary. The Android
852 * OS will automatically release its references to the Uri(s) and the
853 * Activity object when it is destroyed if you follow this pattern.
854 *
855 * <p class="note">Do not pass in an Activity that has already been through
856 * {@link Activity#onDestroy}. This is guaranteed if you call this API
857 * during {@link Activity#onCreate}.
858 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700859 * <p class="note">If this device does not support alternate transports
860 * such as Bluetooth or WiFI, calling this method does nothing.
861 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700862 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
863 *
864 * @param callback callback, or null to disable
865 * @param activity activity for which the Uri(s) will be pushed
866 */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700867 public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) {
868 if (activity == null) {
869 throw new NullPointerException("activity cannot be null");
870 }
871 mNfcActivityManager.setNdefPushContentUriCallback(activity, callback);
Nick Pelly1d7e9062012-04-03 17:46:00 -0700872 }
873
Jeff Hamilton52d32032011-01-08 15:31:26 -0600874 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -0700875 * Set a static {@link NdefMessage} to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -0700876 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700877 * <p>This method may be called at any time before {@link Activity#onDestroy},
878 * but the NDEF message is only made available for NDEF push 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.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700884 *
885 * <p>Only one NDEF message can be pushed by the currently resumed activity.
886 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -0700887 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -0700888 * the callback will take priority.
889 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700890 * <p>If neither {@link #setNdefPushMessage} or
891 * {@link #setNdefPushMessageCallback} have been called for your activity, then
892 * the Android OS may choose to send a default NDEF message on your behalf,
893 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700894 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700895 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
896 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
897 * then NDEF push will be completely disabled for the specified activity(s).
898 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -0700899 * otherwise sent on your behalf for those activity(s).
900 *
901 * <p>If you want to prevent the Android OS from sending default NDEF
902 * messages completely (for all activities), you can include a
Scott Main97e0a1c2012-06-25 11:22:10 -0700903 * {@code &lt;meta-data>} element inside the {@code &lt;application>}
Martijn Coenen854e0772012-04-02 14:52:10 -0700904 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -0700905 * <pre>
906 * &lt;application ...>
907 * &lt;meta-data android:name="android.nfc.disable_beam_default"
908 * android:value="true" />
909 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700910 *
911 * <p>The API allows for multiple activities to be specified at a time,
912 * but it is strongly recommended to just register one at a time,
913 * and to do so during the activity's {@link Activity#onCreate}. For example:
914 * <pre>
915 * protected void onCreate(Bundle savedInstanceState) {
916 * super.onCreate(savedInstanceState);
917 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
918 * if (nfcAdapter == null) return; // NFC not available on this device
919 * nfcAdapter.setNdefPushMessage(ndefMessage, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700920 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700921 * And that is it. Only one call per activity is necessary. The Android
922 * OS will automatically release its references to the NDEF message and the
923 * Activity object when it is destroyed if you follow this pattern.
924 *
925 * <p>If your Activity wants to dynamically generate an NDEF message,
926 * then set a callback using {@link #setNdefPushMessageCallback} instead
927 * of a static message.
928 *
929 * <p class="note">Do not pass in an Activity that has already been through
930 * {@link Activity#onDestroy}. This is guaranteed if you call this API
931 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -0700932 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700933 * <p class="note">For sending large content such as pictures and songs,
934 * consider using {@link #setBeamPushUris}, which switches to alternate transports
935 * such as Bluetooth to achieve a fast transfer rate.
936 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700937 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
938 *
939 * @param message NDEF message to push over NFC, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -0700940 * @param activity activity for which the NDEF message will be pushed
941 * @param activities optional additional activities, however we strongly recommend
942 * to only register one at a time, and to do so in that activity's
943 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -0700944 */
Nick Pelly82328bf2011-08-30 09:37:25 -0700945 public void setNdefPushMessage(NdefMessage message, Activity activity,
946 Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -0700947 int targetSdkVersion = getSdkVersion();
948 try {
949 if (activity == null) {
950 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -0700951 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800952 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -0700953 for (Activity a : activities) {
954 if (a == null) {
955 throw new NullPointerException("activities cannot contain null");
956 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800957 mNfcActivityManager.setNdefPushMessage(a, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -0700958 }
959 } catch (IllegalStateException e) {
960 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
961 // Less strict on old applications - just log the error
962 Log.e(TAG, "Cannot call API with Activity that has already " +
963 "been destroyed", e);
964 } else {
965 // Prevent new applications from making this mistake, re-throw
966 throw(e);
967 }
Nick Pellyc84c89a2011-08-22 22:27:11 -0700968 }
969 }
970
971 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800972 * @hide
973 */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700974 @SystemApi
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800975 public void setNdefPushMessage(NdefMessage message, Activity activity, int flags) {
976 if (activity == null) {
977 throw new NullPointerException("activity cannot be null");
978 }
979 mNfcActivityManager.setNdefPushMessage(activity, message, flags);
980 }
981
982 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -0700983 * Set a callback that dynamically generates NDEF messages to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -0700984 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700985 * <p>This method may be called at any time before {@link Activity#onDestroy},
986 * but the NDEF message callback can only occur when the
987 * specified activity(s) are in resumed (foreground) state. The recommended
988 * approach is to call this method during your Activity's
989 * {@link Activity#onCreate} - see sample
990 * code below. This method does not immediately perform any I/O or blocking work,
991 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700992 *
993 * <p>Only one NDEF message can be pushed by the currently resumed activity.
994 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -0700995 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -0700996 * the callback will take priority.
997 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700998 * <p>If neither {@link #setNdefPushMessage} or
999 * {@link #setNdefPushMessageCallback} have been called for your activity, then
1000 * the Android OS may choose to send a default NDEF message on your behalf,
1001 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001002 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001003 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
1004 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
1005 * then NDEF push will be completely disabled for the specified activity(s).
1006 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -07001007 * otherwise sent on your behalf for those activity(s).
1008 *
1009 * <p>If you want to prevent the Android OS from sending default NDEF
1010 * messages completely (for all activities), you can include a
Scott Main97e0a1c2012-06-25 11:22:10 -07001011 * {@code &lt;meta-data>} element inside the {@code &lt;application>}
Martijn Coenen854e0772012-04-02 14:52:10 -07001012 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -07001013 * <pre>
1014 * &lt;application ...>
1015 * &lt;meta-data android:name="android.nfc.disable_beam_default"
1016 * android:value="true" />
1017 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001018 *
1019 * <p>The API allows for multiple activities to be specified at a time,
1020 * but it is strongly recommended to just register one at a time,
1021 * and to do so during the activity's {@link Activity#onCreate}. For example:
1022 * <pre>
1023 * protected void onCreate(Bundle savedInstanceState) {
1024 * super.onCreate(savedInstanceState);
1025 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1026 * if (nfcAdapter == null) return; // NFC not available on this device
1027 * nfcAdapter.setNdefPushMessageCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001028 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001029 * And that is it. Only one call per activity is necessary. The Android
1030 * OS will automatically release its references to the callback and the
1031 * Activity object when it is destroyed if you follow this pattern.
1032 *
1033 * <p class="note">Do not pass in an Activity that has already been through
1034 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1035 * during {@link Activity#onCreate}.
Martijn Coenen2c103112012-05-15 10:32:15 -07001036 * <p class="note">For sending large content such as pictures and songs,
1037 * consider using {@link #setBeamPushUris}, which switches to alternate transports
1038 * such as Bluetooth to achieve a fast transfer rate.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001039 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1040 *
1041 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001042 * @param activity activity for which the NDEF message will be pushed
1043 * @param activities optional additional activities, however we strongly recommend
1044 * to only register one at a time, and to do so in that activity's
1045 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -07001046 */
Nick Pelly82328bf2011-08-30 09:37:25 -07001047 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
Nick Pellyc84c89a2011-08-22 22:27:11 -07001048 Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -07001049 int targetSdkVersion = getSdkVersion();
1050 try {
1051 if (activity == null) {
1052 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001053 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001054 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001055 for (Activity a : activities) {
1056 if (a == null) {
1057 throw new NullPointerException("activities cannot contain null");
1058 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001059 mNfcActivityManager.setNdefPushMessageCallback(a, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001060 }
1061 } catch (IllegalStateException e) {
1062 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1063 // Less strict on old applications - just log the error
1064 Log.e(TAG, "Cannot call API with Activity that has already " +
1065 "been destroyed", e);
1066 } else {
1067 // Prevent new applications from making this mistake, re-throw
1068 throw(e);
1069 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001070 }
1071 }
1072
1073 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001074 * @hide
1075 */
1076 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
1077 int flags) {
1078 if (activity == null) {
1079 throw new NullPointerException("activity cannot be null");
1080 }
1081 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, flags);
1082 }
1083
1084 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -07001085 * Set a callback on successful Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -07001086 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001087 * <p>This method may be called at any time before {@link Activity#onDestroy},
1088 * but the callback can only occur when the
1089 * specified activity(s) are in resumed (foreground) state. The recommended
1090 * approach is to call this method during your Activity's
1091 * {@link Activity#onCreate} - see sample
1092 * code below. This method does not immediately perform any I/O or blocking work,
1093 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001094 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001095 * <p>The API allows for multiple activities to be specified at a time,
1096 * but it is strongly recommended to just register one at a time,
1097 * and to do so during the activity's {@link Activity#onCreate}. For example:
1098 * <pre>
1099 * protected void onCreate(Bundle savedInstanceState) {
1100 * super.onCreate(savedInstanceState);
1101 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1102 * if (nfcAdapter == null) return; // NFC not available on this device
1103 * nfcAdapter.setOnNdefPushCompleteCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001104 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001105 * And that is it. Only one call per activity is necessary. The Android
1106 * OS will automatically release its references to the callback and the
1107 * Activity object when it is destroyed if you follow this pattern.
1108 *
1109 * <p class="note">Do not pass in an Activity that has already been through
1110 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1111 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -07001112 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001113 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1114 *
1115 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001116 * @param activity activity for which the NDEF message will be pushed
1117 * @param activities optional additional activities, however we strongly recommend
1118 * to only register one at a time, and to do so in that activity's
1119 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -07001120 */
1121 public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback,
Nick Pelly82328bf2011-08-30 09:37:25 -07001122 Activity activity, Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -07001123 int targetSdkVersion = getSdkVersion();
1124 try {
1125 if (activity == null) {
1126 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001127 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001128 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, callback);
1129 for (Activity a : activities) {
1130 if (a == null) {
1131 throw new NullPointerException("activities cannot contain null");
1132 }
1133 mNfcActivityManager.setOnNdefPushCompleteCallback(a, callback);
1134 }
1135 } catch (IllegalStateException e) {
1136 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1137 // Less strict on old applications - just log the error
1138 Log.e(TAG, "Cannot call API with Activity that has already " +
1139 "been destroyed", e);
1140 } else {
1141 // Prevent new applications from making this mistake, re-throw
1142 throw(e);
1143 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001144 }
1145 }
1146
1147 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001148 * Enable foreground dispatch to the given Activity.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001149 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001150 * <p>This will give give priority to the foreground activity when
1151 * dispatching a discovered {@link Tag} to an application.
1152 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001153 * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents
1154 * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and
1155 * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED}
1156 * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled
1157 * by passing in the tech lists separately. Each first level entry in the tech list represents
1158 * an array of technologies that must all be present to match. If any of the first level sets
1159 * match then the dispatch is routed through the given PendingIntent. In other words, the second
1160 * level is ANDed together and the first level entries are ORed together.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001161 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001162 * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters
1163 * that acts a wild card and will cause the foreground activity to receive all tags via the
1164 * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001165 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001166 * <p>This method must be called from the main thread, and only when the activity is in the
1167 * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before
1168 * the completion of their {@link Activity#onPause} callback to disable foreground dispatch
1169 * after it has been enabled.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001170 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001171 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1172 *
Jeff Hamilton52d32032011-01-08 15:31:26 -06001173 * @param activity the Activity to dispatch to
1174 * @param intent the PendingIntent to start for the dispatch
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001175 * @param filters the IntentFilters to override dispatching for, or null to always dispatch
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001176 * @param techLists the tech lists used to perform matching for dispatching of the
1177 * {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent
Nick Pelly74fe6c62011-02-02 22:37:40 -08001178 * @throws IllegalStateException if the Activity is not currently in the foreground
Jeff Hamilton52d32032011-01-08 15:31:26 -06001179 */
1180 public void enableForegroundDispatch(Activity activity, PendingIntent intent,
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001181 IntentFilter[] filters, String[][] techLists) {
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001182 if (activity == null || intent == null) {
Jeff Hamilton52d32032011-01-08 15:31:26 -06001183 throw new NullPointerException();
1184 }
1185 if (!activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001186 throw new IllegalStateException("Foreground dispatch can only be enabled " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001187 "when your activity is resumed");
1188 }
1189 try {
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001190 TechListParcel parcel = null;
1191 if (techLists != null && techLists.length > 0) {
1192 parcel = new TechListParcel(techLists);
1193 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06001194 ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001195 mForegroundDispatchListener);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001196 sService.setForegroundDispatch(intent, filters, parcel);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001197 } catch (RemoteException e) {
1198 attemptDeadServiceRecovery(e);
1199 }
1200 }
1201
1202 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001203 * Disable foreground dispatch to the given activity.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001204 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001205 * <p>After calling {@link #enableForegroundDispatch}, an activity
1206 * must call this method before its {@link Activity#onPause} callback
1207 * completes.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001208 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001209 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001210 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001211 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1212 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001213 * @param activity the Activity to disable dispatch to
1214 * @throws IllegalStateException if the Activity has already been paused
Jeff Hamilton52d32032011-01-08 15:31:26 -06001215 */
1216 public void disableForegroundDispatch(Activity activity) {
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001217 ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,
1218 mForegroundDispatchListener);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001219 disableForegroundDispatchInternal(activity, false);
1220 }
1221
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001222 OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001223 @Override
1224 public void onPaused(Activity activity) {
1225 disableForegroundDispatchInternal(activity, true);
1226 }
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001227 };
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001228
Jeff Hamilton52d32032011-01-08 15:31:26 -06001229 void disableForegroundDispatchInternal(Activity activity, boolean force) {
1230 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001231 sService.setForegroundDispatch(null, null, null);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001232 if (!force && !activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001233 throw new IllegalStateException("You must disable foreground dispatching " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001234 "while your activity is still resumed");
1235 }
1236 } catch (RemoteException e) {
1237 attemptDeadServiceRecovery(e);
1238 }
1239 }
1240
Nick Pelly590b73b2010-10-12 13:00:50 -07001241 /**
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001242 * Limit the NFC controller to reader mode while this Activity is in the foreground.
1243 *
1244 * <p>In this mode the NFC controller will only act as an NFC tag reader/writer,
1245 * thus disabling any peer-to-peer (Android Beam) and card-emulation modes of
1246 * the NFC adapter on this device.
1247 *
1248 * <p>Use {@link #FLAG_READER_SKIP_NDEF_CHECK} to prevent the platform from
1249 * performing any NDEF checks in reader mode. Note that this will prevent the
1250 * {@link Ndef} tag technology from being enumerated on the tag, and that
1251 * NDEF-based tag dispatch will not be functional.
1252 *
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001253 * <p>For interacting with tags that are emulated on another Android device
1254 * using Android's host-based card-emulation, the recommended flags are
1255 * {@link #FLAG_READER_NFC_A} and {@link #FLAG_READER_SKIP_NDEF_CHECK}.
1256 *
1257 * @param activity the Activity that requests the adapter to be in reader mode
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001258 * @param callback the callback to be called when a tag is discovered
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001259 * @param flags Flags indicating poll technologies and other optional parameters
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001260 * @param extras Additional extras for configuring reader mode.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001261 */
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001262 public void enableReaderMode(Activity activity, ReaderCallback callback, int flags,
1263 Bundle extras) {
1264 mNfcActivityManager.enableReaderMode(activity, callback, flags, extras);
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001265 }
1266
1267 /**
1268 * Restore the NFC adapter to normal mode of operation: supporting
1269 * peer-to-peer (Android Beam), card emulation, and polling for
1270 * all supported tag technologies.
1271 *
1272 * @param activity the Activity that currently has reader mode enabled
1273 */
1274 public void disableReaderMode(Activity activity) {
1275 mNfcActivityManager.disableReaderMode(activity);
1276 }
1277
1278 /**
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001279 * Manually invoke Android Beam to share data.
1280 *
1281 * <p>The Android Beam animation is normally only shown when two NFC-capable
1282 * devices come into range.
1283 * By calling this method, an Activity can invoke the Beam animation directly
1284 * even if no other NFC device is in range yet. The Beam animation will then
1285 * prompt the user to tap another NFC-capable device to complete the data
1286 * transfer.
1287 *
1288 * <p>The main advantage of using this method is that it avoids the need for the
1289 * user to tap the screen to complete the transfer, as this method already
1290 * establishes the direction of the transfer and the consent of the user to
1291 * share data. Callers are responsible for making sure that the user has
1292 * consented to sharing data on NFC tap.
1293 *
1294 * <p>Note that to use this method, the passed in Activity must have already
1295 * set data to share over Beam by using method calls such as
Martijn Coenen6924c4e2014-02-25 11:37:17 -08001296 * {@link #setNdefPushMessageCallback} or
1297 * {@link #setBeamPushUrisCallback}.
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001298 *
1299 * @param activity the current foreground Activity that has registered data to share
1300 * @return whether the Beam animation was successfully invoked
1301 */
1302 public boolean invokeBeam(Activity activity) {
1303 if (activity == null) {
1304 throw new NullPointerException("activity may not be null.");
1305 }
1306 enforceResumed(activity);
1307 try {
1308 sService.invokeBeam();
1309 return true;
1310 } catch (RemoteException e) {
1311 Log.e(TAG, "invokeBeam: NFC process has died.");
1312 attemptDeadServiceRecovery(e);
1313 return false;
1314 }
1315 }
1316
1317 /**
Martijn Coenendbedb4c2014-07-01 15:52:19 -07001318 * @hide
1319 */
1320 public boolean invokeBeam(BeamShareData shareData) {
1321 try {
1322 Log.e(TAG, "invokeBeamInternal()");
1323 sService.invokeBeamInternal(shareData);
1324 return true;
1325 } catch (RemoteException e) {
1326 Log.e(TAG, "invokeBeam: NFC process has died.");
1327 attemptDeadServiceRecovery(e);
1328 return false;
1329 }
1330 }
1331
1332 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001333 * Enable NDEF message push over NFC while this Activity is in the foreground.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001334 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001335 * <p>You must explicitly call this method every time the activity is
1336 * resumed, and you must call {@link #disableForegroundNdefPush} before
1337 * your activity completes {@link Activity#onPause}.
1338 *
1339 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1340 * instead: it automatically hooks into your activity life-cycle,
1341 * so you do not need to call enable/disable in your onResume/onPause.
1342 *
1343 * <p>For NDEF push to function properly the other NFC device must
1344 * support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or
1345 * Android's "com.android.npp" (Ndef Push Protocol). This was optional
1346 * on Gingerbread level Android NFC devices, but SNEP is mandatory on
1347 * Ice-Cream-Sandwich and beyond.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001348 *
1349 * <p>This method must be called from the main thread.
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001350 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001351 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1352 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001353 * @param activity foreground activity
1354 * @param message a NDEF Message to push over NFC
1355 * @throws IllegalStateException if the activity is not currently in the foreground
1356 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001357 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001358 @Deprecated
1359 public void enableForegroundNdefPush(Activity activity, NdefMessage message) {
1360 if (activity == null || message == null) {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001361 throw new NullPointerException();
1362 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001363 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001364 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Jason parks01425362011-05-24 02:57:37 -07001365 }
1366
1367 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001368 * Disable NDEF message push over P2P.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001369 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001370 * <p>After calling {@link #enableForegroundNdefPush}, an activity
1371 * must call this method before its {@link Activity#onPause} callback
1372 * completes.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001373 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001374 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1375 * instead: it automatically hooks into your activity life-cycle,
1376 * so you do not need to call enable/disable in your onResume/onPause.
1377 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001378 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001379 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001380 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1381 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001382 * @param activity the Foreground activity
1383 * @throws IllegalStateException if the Activity has already been paused
Nick Pellyc84c89a2011-08-22 22:27:11 -07001384 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001385 */
Nick Pellyc97a5522012-01-05 15:13:01 +11001386 @Deprecated
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001387 public void disableForegroundNdefPush(Activity activity) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001388 if (activity == null) {
1389 throw new NullPointerException();
1390 }
1391 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001392 mNfcActivityManager.setNdefPushMessage(activity, null, 0);
1393 mNfcActivityManager.setNdefPushMessageCallback(activity, null, 0);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001394 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, null);
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001395 }
1396
Nick Pellyc84c89a2011-08-22 22:27:11 -07001397 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001398 * Enable NDEF Push feature.
1399 * <p>This API is for the Settings application.
1400 * @hide
1401 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001402 @SystemApi
Nick Pellyc84c89a2011-08-22 22:27:11 -07001403 public boolean enableNdefPush() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001404 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001405 return sService.enableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001406 } catch (RemoteException e) {
1407 attemptDeadServiceRecovery(e);
1408 return false;
1409 }
1410 }
1411
1412 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001413 * Disable NDEF Push feature.
1414 * <p>This API is for the Settings application.
Martijn Coenen6d748942011-07-21 09:34:25 +02001415 * @hide
1416 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001417 @SystemApi
Nick Pellyc84c89a2011-08-22 22:27:11 -07001418 public boolean disableNdefPush() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001419 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001420 return sService.disableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001421 } catch (RemoteException e) {
1422 attemptDeadServiceRecovery(e);
1423 return false;
1424 }
1425 }
1426
1427 /**
Nick Pellycccf01d2011-10-31 14:49:40 -07001428 * Return true if the NDEF Push (Android Beam) feature is enabled.
1429 * <p>This function will return true only if both NFC is enabled, and the
1430 * NDEF Push feature is enabled.
1431 * <p>Note that if NFC is enabled but NDEF Push is disabled then this
1432 * device can still <i>receive</i> NDEF messages, it just cannot send them.
1433 * <p>Applications cannot directly toggle the NDEF Push feature, but they
1434 * can request Settings UI allowing the user to toggle NDEF Push using
1435 * <code>startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))</code>
1436 * <p>Example usage in an Activity that requires NDEF Push:
1437 * <p><pre>
1438 * protected void onResume() {
1439 * super.onResume();
1440 * if (!nfcAdapter.isEnabled()) {
1441 * startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
1442 * } else if (!nfcAdapter.isNdefPushEnabled()) {
1443 * startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
1444 * }
Scott Main97e0a1c2012-06-25 11:22:10 -07001445 * }</pre>
Martijn Coenen6d748942011-07-21 09:34:25 +02001446 *
Nick Pellycccf01d2011-10-31 14:49:40 -07001447 * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS
Nick Pellyc84c89a2011-08-22 22:27:11 -07001448 * @return true if NDEF Push feature is enabled
Martijn Coenen6d748942011-07-21 09:34:25 +02001449 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001450 public boolean isNdefPushEnabled() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001451 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001452 return sService.isNdefPushEnabled();
Martijn Coenen6d748942011-07-21 09:34:25 +02001453 } catch (RemoteException e) {
1454 attemptDeadServiceRecovery(e);
1455 return false;
1456 }
1457 }
1458
1459 /**
Nick Pellyc97a5522012-01-05 15:13:01 +11001460 * Inject a mock NFC tag.<p>
1461 * Used for testing purposes.
1462 * <p class="note">Requires the
1463 * {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
1464 * @hide
1465 */
Nick Pelly1f5badc2012-01-24 13:22:58 -08001466 public void dispatch(Tag tag) {
Nick Pellyc97a5522012-01-05 15:13:01 +11001467 if (tag == null) {
1468 throw new NullPointerException("tag cannot be null");
1469 }
1470 try {
Nick Pelly1f5badc2012-01-24 13:22:58 -08001471 sService.dispatch(tag);
Nick Pellyc97a5522012-01-05 15:13:01 +11001472 } catch (RemoteException e) {
1473 attemptDeadServiceRecovery(e);
1474 }
1475 }
1476
1477 /**
Daniel Tomas90245642010-11-17 10:07:52 +01001478 * @hide
1479 */
Martijn Coenen188cddb2012-01-31 22:16:15 -08001480 public void setP2pModes(int initiatorModes, int targetModes) {
1481 try {
1482 sService.setP2pModes(initiatorModes, targetModes);
1483 } catch (RemoteException e) {
1484 attemptDeadServiceRecovery(e);
1485 }
1486 }
1487
1488 /**
Andres Morales11d2e532014-07-23 12:39:55 -07001489 * Registers a new NFC unlock handler with the NFC service.
1490 *
1491 * <p />NFC unlock handlers are intended to unlock the keyguard in the presence of a trusted
1492 * NFC device. The handler should return true if it successfully authenticates the user and
1493 * unlocks the keyguard.
1494 *
1495 * <p /> The parameter {@code tagTechnologies} determines which Tag technologies will be polled for
1496 * at the lockscreen. Polling for less tag technologies reduces latency, and so it is
1497 * strongly recommended to only provide the Tag technologies that the handler is expected to
Andres Moralesf9a97942014-08-14 15:43:30 -07001498 * receive. There must be at least one tag technology provided, otherwise the unlock handler
1499 * is ignored.
Andres Morales11d2e532014-07-23 12:39:55 -07001500 *
1501 * @hide
1502 */
1503 @SystemApi
1504 public boolean addNfcUnlockHandler(final NfcUnlockHandler unlockHandler,
1505 String[] tagTechnologies) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001506 // If there are no tag technologies, don't bother adding unlock handler
1507 if (tagTechnologies.length == 0) {
1508 return false;
1509 }
Andres Morales11d2e532014-07-23 12:39:55 -07001510
Andres Moralesf9a97942014-08-14 15:43:30 -07001511 try {
Andres Morales11d2e532014-07-23 12:39:55 -07001512 synchronized (mLock) {
1513 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001514 // update the tag technologies
1515 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.get(unlockHandler));
1516 mNfcUnlockHandlers.remove(unlockHandler);
Andres Morales11d2e532014-07-23 12:39:55 -07001517 }
Andres Moralesf9a97942014-08-14 15:43:30 -07001518
1519 INfcUnlockHandler.Stub iHandler = new INfcUnlockHandler.Stub() {
1520 @Override
1521 public boolean onUnlockAttempted(Tag tag) throws RemoteException {
1522 return unlockHandler.onUnlockAttempted(tag);
1523 }
1524 };
1525
1526 sService.addNfcUnlockHandler(iHandler,
1527 Tag.getTechCodesFromStrings(tagTechnologies));
1528 mNfcUnlockHandlers.put(unlockHandler, iHandler);
Andres Morales11d2e532014-07-23 12:39:55 -07001529 }
1530 } catch (RemoteException e) {
1531 attemptDeadServiceRecovery(e);
1532 return false;
1533 } catch (IllegalArgumentException e) {
1534 Log.e(TAG, "Unable to register LockscreenDispatch", e);
1535 return false;
1536 }
1537
1538 return true;
1539 }
1540
1541 /**
1542 * Removes a previously registered unlock handler. Also removes the tag technologies
1543 * associated with the removed unlock handler.
1544 *
1545 * @hide
1546 */
1547 @SystemApi
1548 public boolean removeNfcUnlockHandler(NfcUnlockHandler unlockHandler) {
1549 try {
1550 synchronized (mLock) {
1551 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001552 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.remove(unlockHandler));
Andres Morales11d2e532014-07-23 12:39:55 -07001553 }
1554
1555 return true;
1556 }
1557 } catch (RemoteException e) {
1558 attemptDeadServiceRecovery(e);
1559 return false;
1560 }
1561 }
1562
1563 /**
Martijn Coenen188cddb2012-01-31 22:16:15 -08001564 * @hide
1565 */
Nick Pelly367f41f2011-03-08 11:43:30 -08001566 public INfcAdapterExtras getNfcAdapterExtrasInterface() {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06001567 if (mContext == null) {
1568 throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
1569 + " NFC extras APIs");
1570 }
Daniel Tomas90245642010-11-17 10:07:52 +01001571 try {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06001572 return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
Daniel Tomas90245642010-11-17 10:07:52 +01001573 } catch (RemoteException e) {
Nick Pelly367f41f2011-03-08 11:43:30 -08001574 attemptDeadServiceRecovery(e);
Daniel Tomas90245642010-11-17 10:07:52 +01001575 return null;
1576 }
1577 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001578
1579 void enforceResumed(Activity activity) {
1580 if (!activity.isResumed()) {
1581 throw new IllegalStateException("API cannot be called while activity is paused");
1582 }
1583 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001584
1585 int getSdkVersion() {
1586 if (mContext == null) {
1587 return android.os.Build.VERSION_CODES.GINGERBREAD; // best guess
1588 } else {
1589 return mContext.getApplicationInfo().targetSdkVersion;
1590 }
1591 }
Nick Pelly590b73b2010-10-12 13:00:50 -07001592}