blob: a7d2ee98b45cd80afaf187faf6651ffffda43951 [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
Ruchi Kandoiebf267a2018-10-16 17:29:16 -070019import android.annotation.NonNull;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060020import android.annotation.RequiresPermission;
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;
Mathew Inwood1961e1e2018-07-31 16:04:15 +010024import android.annotation.UnsupportedAppUsage;
Jeff Hamilton52d32032011-01-08 15:31:26 -060025import android.app.Activity;
Nick Pellyfdf90862010-10-18 11:19:41 -070026import android.app.ActivityThread;
Jeff Hamilton52d32032011-01-08 15:31:26 -060027import android.app.OnActivityPausedListener;
28import android.app.PendingIntent;
Nick Pelly50b4d8f2010-12-07 22:40:28 -080029import android.content.Context;
Jeff Hamilton52d32032011-01-08 15:31:26 -060030import android.content.IntentFilter;
Nick Pellyfdf90862010-10-18 11:19:41 -070031import android.content.pm.IPackageManager;
32import android.content.pm.PackageManager;
Nick Pelly1d7e9062012-04-03 17:46:00 -070033import android.net.Uri;
Jeff Hamilton28319c02011-02-09 17:26:47 +090034import android.nfc.tech.MifareClassic;
35import android.nfc.tech.Ndef;
36import android.nfc.tech.NfcA;
37import android.nfc.tech.NfcF;
Martijn Coenen5b1e0322013-09-02 20:38:47 -070038import android.os.Bundle;
Martijn Coenend55def82016-04-14 15:19:31 +020039import android.os.Handler;
Nick Pelly590b73b2010-10-12 13:00:50 -070040import android.os.IBinder;
41import android.os.RemoteException;
42import android.os.ServiceManager;
43import android.util.Log;
44
Martijn Coenend55def82016-04-14 15:19:31 +020045import java.io.IOException;
Ruchi Kandoiebf267a2018-10-16 17:29:16 -070046import java.util.ArrayList;
Love Khanna755a8fa2017-05-17 15:52:16 +053047import java.util.HashMap;
Ruchi Kandoiebf267a2018-10-16 17:29:16 -070048import java.util.List;
Martijn Coenend55def82016-04-14 15:19:31 +020049
Nick Pelly590b73b2010-10-12 13:00:50 -070050/**
Nick Pelly74fe6c62011-02-02 22:37:40 -080051 * Represents the local NFC adapter.
Nick Pelly590b73b2010-10-12 13:00:50 -070052 * <p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -080053 * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC
54 * adapter for this Android device.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080055 *
56 * <div class="special reference">
57 * <h3>Developer Guides</h3>
58 * <p>For more information about using NFC, read the
59 * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p>
Scott Mainb38ad762013-10-17 11:57:06 -070060 * <p>To perform basic file sharing between devices, read
61 * <a href="{@docRoot}training/beam-files/index.html">Sharing Files with NFC</a>.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080062 * </div>
Nick Pelly590b73b2010-10-12 13:00:50 -070063 */
64public final class NfcAdapter {
Nick Pellyc84c89a2011-08-22 22:27:11 -070065 static final String TAG = "NFC";
Nick Pelly50b4d8f2010-12-07 22:40:28 -080066
Nick Pelly590b73b2010-10-12 13:00:50 -070067 /**
Jeff Hamilton641dd622010-12-02 09:16:22 -060068 * Intent to start an activity when a tag with NDEF payload is discovered.
Jeff Hamilton641dd622010-12-02 09:16:22 -060069 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090070 * <p>The system inspects the first {@link NdefRecord} in the first {@link NdefMessage} and
71 * looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the
72 * intent will contain the URI in its data field. If a MIME record is found the intent will
73 * contain the MIME type in its type field. This allows activities to register
74 * {@link IntentFilter}s targeting specific content on tags. Activities should register the
75 * most specific intent filters possible to avoid the activity chooser dialog, which can
76 * disrupt the interaction with the tag as the user interacts with the screen.
77 *
78 * <p>If the tag has an NDEF payload this intent is started before
79 * {@link #ACTION_TECH_DISCOVERED}. If any activities respond to this intent neither
Nick Pellyf003e262011-01-31 23:27:37 -080080 * {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started.
Nick Pellyc97a5522012-01-05 15:13:01 +110081 *
82 * <p>The MIME type or data URI of this intent are normalized before dispatch -
83 * so that MIME, URI scheme and URI host are always lower-case.
Jeff Hamilton641dd622010-12-02 09:16:22 -060084 */
85 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
86 public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED";
87
88 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +090089 * Intent to start an activity when a tag is discovered and activities are registered for the
90 * specific technologies on the tag.
Jeff Hamilton641dd622010-12-02 09:16:22 -060091 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090092 * <p>To receive this intent an activity must include an intent filter
93 * for this action and specify the desired tech types in a
94 * manifest <code>meta-data</code> entry. Here is an example manfiest entry:
95 * <pre>
Scott Main97e0a1c2012-06-25 11:22:10 -070096 * &lt;activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"&gt;
97 * &lt;!-- Add a technology filter --&gt;
98 * &lt;intent-filter&gt;
99 * &lt;action android:name="android.nfc.action.TECH_DISCOVERED" /&gt;
100 * &lt;/intent-filter&gt;
Jeff Hamilton641dd622010-12-02 09:16:22 -0600101 *
Scott Main97e0a1c2012-06-25 11:22:10 -0700102 * &lt;meta-data android:name="android.nfc.action.TECH_DISCOVERED"
103 * android:resource="@xml/filter_nfc"
104 * /&gt;
105 * &lt;/activity&gt;</pre>
Jeff Hamilton28319c02011-02-09 17:26:47 +0900106 *
107 * <p>The meta-data XML file should contain one or more <code>tech-list</code> entries
108 * each consisting or one or more <code>tech</code> entries. The <code>tech</code> entries refer
109 * to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA".
110 *
111 * <p>A tag matches if any of the
112 * <code>tech-list</code> sets is a subset of {@link Tag#getTechList() Tag.getTechList()}. Each
113 * of the <code>tech-list</code>s is considered independently and the
114 * activity is considered a match is any single <code>tech-list</code> matches the tag that was
115 * discovered. This provides AND and OR semantics for filtering desired techs. Here is an
116 * example that will match any tag using {@link NfcF} or any tag using {@link NfcA},
117 * {@link MifareClassic}, and {@link Ndef}:
118 *
119 * <pre>
120 * &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
121 * &lt;!-- capture anything using NfcF --&gt;
122 * &lt;tech-list&gt;
123 * &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
124 * &lt;/tech-list&gt;
125 *
126 * &lt;!-- OR --&gt;
127 *
128 * &lt;!-- capture all MIFARE Classics with NDEF payloads --&gt;
129 * &lt;tech-list&gt;
130 * &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
131 * &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
132 * &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
133 * &lt;/tech-list&gt;
Scott Main97e0a1c2012-06-25 11:22:10 -0700134 * &lt;/resources&gt;</pre>
Jeff Hamilton28319c02011-02-09 17:26:47 +0900135 *
136 * <p>This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before
137 * {@link #ACTION_TAG_DISCOVERED}. If any activities respond to {@link #ACTION_NDEF_DISCOVERED}
138 * this intent will not be started. If any activities respond to this intent
139 * {@link #ACTION_TAG_DISCOVERED} will not be started.
Jeff Hamilton641dd622010-12-02 09:16:22 -0600140 */
141 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Nick Pellyf003e262011-01-31 23:27:37 -0800142 public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED";
Jeff Hamilton641dd622010-12-02 09:16:22 -0600143
144 /**
Nick Pelly11b075e2010-10-28 13:39:37 -0700145 * Intent to start an activity when a tag is discovered.
Jeff Hamilton28319c02011-02-09 17:26:47 +0900146 *
147 * <p>This intent will not be started when a tag is discovered if any activities respond to
Jason parks01425362011-05-24 02:57:37 -0700148 * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag.
Nick Pelly590b73b2010-10-12 13:00:50 -0700149 */
150 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
151 public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED";
152
153 /**
Ruchi Kandoia0771612018-02-02 09:14:39 -0800154 * Broadcast Action: Intent to notify an application that an transaction event has occurred
155 * on the Secure Element.
156 *
157 * <p>This intent will only be sent if the application has requested permission for
158 * {@link android.Manifest.permission#NFC_TRANSACTION_EVENT} and if the application has the
159 * necessary access to Secure Element which witnessed the particular event.
160 */
161 @RequiresPermission(android.Manifest.permission.NFC_TRANSACTION_EVENT)
162 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
163 public static final String ACTION_TRANSACTION_DETECTED =
164 "android.nfc.action.TRANSACTION_DETECTED";
165
166 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600167 * Broadcast to only the activity that handles ACTION_TAG_DISCOVERED
168 * @hide
169 */
170 public static final String ACTION_TAG_LEFT_FIELD = "android.nfc.action.TAG_LOST";
171
172 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900173 * Mandatory extra containing the {@link Tag} that was discovered for the
174 * {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
175 * {@link #ACTION_TAG_DISCOVERED} intents.
Nick Pelly590b73b2010-10-12 13:00:50 -0700176 */
177 public static final String EXTRA_TAG = "android.nfc.extra.TAG";
178
179 /**
Nick Pellyc97a5522012-01-05 15:13:01 +1100180 * Extra containing an array of {@link NdefMessage} present on the discovered tag.<p>
181 * This extra is mandatory for {@link #ACTION_NDEF_DISCOVERED} intents,
182 * and optional for {@link #ACTION_TECH_DISCOVERED}, and
183 * {@link #ACTION_TAG_DISCOVERED} intents.<p>
184 * When this extra is present there will always be at least one
185 * {@link NdefMessage} element. Most NDEF tags have only one NDEF message,
186 * but we use an array for future compatibility.
Nick Pelly11b075e2010-10-28 13:39:37 -0700187 */
188 public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES";
189
190 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900191 * Optional extra containing a byte array containing the ID of the discovered tag for
192 * the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
193 * {@link #ACTION_TAG_DISCOVERED} intents.
Nick Pelly11b075e2010-10-28 13:39:37 -0700194 */
195 public static final String EXTRA_ID = "android.nfc.extra.ID";
196
197 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700198 * Broadcast Action: The state of the local NFC adapter has been
199 * changed.
200 * <p>For example, NFC has been turned on or off.
Martijn Coenen4ba5eaf2013-01-08 12:49:47 -0800201 * <p>Always contains the extra field {@link #EXTRA_ADAPTER_STATE}
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700202 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700203 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
204 public static final String ACTION_ADAPTER_STATE_CHANGED =
205 "android.nfc.action.ADAPTER_STATE_CHANGED";
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700206
207 /**
Martijn Coenen4ba5eaf2013-01-08 12:49:47 -0800208 * Used as an int extra field in {@link #ACTION_ADAPTER_STATE_CHANGED}
Nick Pelly8d32a012011-08-09 07:03:49 -0700209 * intents to request the current power state. Possible values are:
210 * {@link #STATE_OFF},
211 * {@link #STATE_TURNING_ON},
212 * {@link #STATE_ON},
213 * {@link #STATE_TURNING_OFF},
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700214 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700215 public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE";
216
Ruchi Kandoia0771612018-02-02 09:14:39 -0800217 /**
218 * Mandatory byte[] extra field in {@link #ACTION_TRANSACTION_DETECTED}
219 */
220 public static final String EXTRA_AID = "android.nfc.extra.AID";
221
222 /**
223 * Optional byte[] extra field in {@link #ACTION_TRANSACTION_DETECTED}
224 */
225 public static final String EXTRA_DATA = "android.nfc.extra.DATA";
226
227 /**
228 * Mandatory String extra field in {@link #ACTION_TRANSACTION_DETECTED}
229 * Indicates the Secure Element on which the transaction occurred.
230 * eSE1...eSEn for Embedded Secure Elements, SIM1...SIMn for UICC, etc.
231 */
Ruchi Kandoi9a33aefd2018-03-15 15:15:18 -0700232 public static final String EXTRA_SECURE_ELEMENT_NAME = "android.nfc.extra.SECURE_ELEMENT_NAME";
Ruchi Kandoia0771612018-02-02 09:14:39 -0800233
Nick Pelly8d32a012011-08-09 07:03:49 -0700234 public static final int STATE_OFF = 1;
Nick Pelly8d32a012011-08-09 07:03:49 -0700235 public static final int STATE_TURNING_ON = 2;
Nick Pelly8d32a012011-08-09 07:03:49 -0700236 public static final int STATE_ON = 3;
Nick Pelly8d32a012011-08-09 07:03:49 -0700237 public static final int STATE_TURNING_OFF = 4;
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700238
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700239 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700240 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700241 * <p>
242 * Setting this flag enables polling for Nfc-A technology.
243 */
244 public static final int FLAG_READER_NFC_A = 0x1;
245
246 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700247 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700248 * <p>
249 * Setting this flag enables polling for Nfc-B technology.
250 */
251 public static final int FLAG_READER_NFC_B = 0x2;
252
253 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700254 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700255 * <p>
256 * Setting this flag enables polling for Nfc-F technology.
257 */
258 public static final int FLAG_READER_NFC_F = 0x4;
259
260 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700261 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700262 * <p>
263 * Setting this flag enables polling for Nfc-V (ISO15693) technology.
264 */
265 public static final int FLAG_READER_NFC_V = 0x8;
266
267 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700268 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700269 * <p>
Martijn Coenenb523bfa2013-09-12 13:44:39 +0200270 * Setting this flag enables polling for NfcBarcode technology.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700271 */
Martijn Coenenb523bfa2013-09-12 13:44:39 +0200272 public static final int FLAG_READER_NFC_BARCODE = 0x10;
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700273
274 /**
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700275 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -0700276 * <p>
277 * Setting this flag allows the caller to prevent the
278 * platform from performing an NDEF check on the tags it
279 * finds.
280 */
281 public static final int FLAG_READER_SKIP_NDEF_CHECK = 0x80;
282
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700283 /**
284 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
285 * <p>
286 * Setting this flag allows the caller to prevent the
287 * platform from playing sounds when it discovers a tag.
288 */
289 public static final int FLAG_READER_NO_PLATFORM_SOUNDS = 0x100;
290
291 /**
292 * Int Extra for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}.
293 * <p>
294 * Setting this integer extra allows the calling application to specify
295 * the delay that the platform will use for performing presence checks
296 * on any discovered tag.
297 */
298 public static final String EXTRA_READER_PRESENCE_CHECK_DELAY = "presence";
299
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700300 /** @hide */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700301 @SystemApi
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800302 public static final int FLAG_NDEF_PUSH_NO_CONFIRM = 0x1;
303
304 /** @hide */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700305 public static final String ACTION_HANDOVER_TRANSFER_STARTED =
306 "android.nfc.action.HANDOVER_TRANSFER_STARTED";
307
308 /** @hide */
309 public static final String ACTION_HANDOVER_TRANSFER_DONE =
310 "android.nfc.action.HANDOVER_TRANSFER_DONE";
311
312 /** @hide */
313 public static final String EXTRA_HANDOVER_TRANSFER_STATUS =
314 "android.nfc.extra.HANDOVER_TRANSFER_STATUS";
315
316 /** @hide */
317 public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
318 /** @hide */
319 public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;
320
321 /** @hide */
322 public static final String EXTRA_HANDOVER_TRANSFER_URI =
323 "android.nfc.extra.HANDOVER_TRANSFER_URI";
324
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800325 // Guarded by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700326 static boolean sIsInitialized = false;
Steve Elliott04141ea2016-04-15 15:09:43 -0400327 static boolean sHasNfcFeature;
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700328 static boolean sHasBeamFeature;
Nick Pelly590b73b2010-10-12 13:00:50 -0700329
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800330 // Final after first constructor, except for
331 // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
332 // recovery
Mathew Inwood1961e1e2018-07-31 16:04:15 +0100333 @UnsupportedAppUsage
Nick Pellyc84c89a2011-08-22 22:27:11 -0700334 static INfcAdapter sService;
335 static INfcTag sTagService;
Martijn Coenena7397882013-07-30 20:07:47 -0700336 static INfcCardEmulation sCardEmulationService;
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900337 static INfcFCardEmulation sNfcFCardEmulationService;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700338
339 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600340 * The NfcAdapter object for each application context.
341 * There is a 1-1 relationship between application context and
342 * NfcAdapter object.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700343 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600344 static HashMap<Context, NfcAdapter> sNfcAdapters = new HashMap(); //guard by NfcAdapter.class
345
346 /**
347 * NfcAdapter used with a null context. This ctor was deprecated but we have
348 * to support it for backwards compatibility. New methods that require context
349 * might throw when called on the null-context NfcAdapter.
350 */
351 static NfcAdapter sNullContextNfcAdapter; // protected by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700352
353 final NfcActivityManager mNfcActivityManager;
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600354 final Context mContext;
Andres Moralesf9a97942014-08-14 15:43:30 -0700355 final HashMap<NfcUnlockHandler, INfcUnlockHandler> mNfcUnlockHandlers;
Andres Morales11d2e532014-07-23 12:39:55 -0700356 final Object mLock;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700357
Martijn Coenend55def82016-04-14 15:19:31 +0200358 ITagRemovedCallback mTagRemovedListener; // protected by mLock
359
Nick Pellyc84c89a2011-08-22 22:27:11 -0700360 /**
Scott Mainb38ad762013-10-17 11:57:06 -0700361 * A callback to be invoked when the system finds a tag while the foreground activity is
362 * operating in reader mode.
363 * <p>Register your {@code ReaderCallback} implementation with {@link
364 * NfcAdapter#enableReaderMode} and disable it with {@link
365 * NfcAdapter#disableReaderMode}.
366 * @see NfcAdapter#enableReaderMode
Martijn Coenen5b1e0322013-09-02 20:38:47 -0700367 */
368 public interface ReaderCallback {
369 public void onTagDiscovered(Tag tag);
370 }
371
372 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700373 * A callback to be invoked when the system successfully delivers your {@link NdefMessage}
374 * to another device.
375 * @see #setOnNdefPushCompleteCallback
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700376 * @deprecated this feature is deprecated.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700377 */
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700378 @java.lang.Deprecated
Nick Pellyc84c89a2011-08-22 22:27:11 -0700379 public interface OnNdefPushCompleteCallback {
380 /**
381 * Called on successful NDEF push.
382 *
383 * <p>This callback is usually made on a binder thread (not the UI thread).
384 *
385 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
Scott Main2d68a6b2011-09-26 22:59:38 -0700386 * @see #setNdefPushMessageCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700387 */
388 public void onNdefPushComplete(NfcEvent event);
389 }
390
391 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700392 * A callback to be invoked when another NFC device capable of NDEF push (Android Beam)
393 * is within range.
394 * <p>Implement this interface and pass it to {@link
395 * NfcAdapter#setNdefPushMessageCallback setNdefPushMessageCallback()} in order to create an
396 * {@link NdefMessage} at the moment that another device is within range for NFC. Using this
397 * callback allows you to create a message with data that might vary based on the
398 * content currently visible to the user. Alternatively, you can call {@link
399 * #setNdefPushMessage setNdefPushMessage()} if the {@link NdefMessage} always contains the
400 * same data.
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700401 * @deprecated this feature is deprecated.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700402 */
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700403 @java.lang.Deprecated
Nick Pellyc84c89a2011-08-22 22:27:11 -0700404 public interface CreateNdefMessageCallback {
405 /**
406 * Called to provide a {@link NdefMessage} to push.
407 *
408 * <p>This callback is usually made on a binder thread (not the UI thread).
409 *
410 * <p>Called when this device is in range of another device
411 * that might support NDEF push. It allows the application to
412 * create the NDEF message only when it is required.
413 *
414 * <p>NDEF push cannot occur until this method returns, so do not
415 * block for too long.
416 *
417 * <p>The Android operating system will usually show a system UI
418 * on top of your activity during this time, so do not try to request
419 * input from the user to complete the callback, or provide custom NDEF
420 * push UI. The user probably will not see it.
421 *
422 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
423 * @return NDEF message to push, or null to not provide a message
424 */
425 public NdefMessage createNdefMessage(NfcEvent event);
426 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700427
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700428
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700429 /**
430 * @deprecated this feature is deprecated.
431 */
432 @java.lang.Deprecated
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700433 public interface CreateBeamUrisCallback {
434 public Uri[] createBeamUris(NfcEvent event);
435 }
436
Nick Pelly590b73b2010-10-12 13:00:50 -0700437 /**
Martijn Coenend55def82016-04-14 15:19:31 +0200438 * A callback that is invoked when a tag is removed from the field.
Martijn Coenen61367302016-05-17 09:09:15 +0200439 * @see NfcAdapter#ignore
Martijn Coenend55def82016-04-14 15:19:31 +0200440 */
441 public interface OnTagRemovedListener {
442 void onTagRemoved();
443 }
444
445 /**
Andres Morales11d2e532014-07-23 12:39:55 -0700446 * A callback to be invoked when an application has registered as a
447 * handler to unlock the device given an NFC tag at the lockscreen.
448 * @hide
449 */
450 @SystemApi
451 public interface NfcUnlockHandler {
452 /**
453 * Called at the lock screen to attempt to unlock the device with the given tag.
454 * @param tag the detected tag, to be used to unlock the device
455 * @return true if the device was successfully unlocked
456 */
457 public boolean onUnlockAttempted(Tag tag);
458 }
459
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700460 /**
461 * Helper to check if this device has FEATURE_NFC_BEAM, but without using
462 * a context.
463 * Equivalent to
464 * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC_BEAM)
465 */
466 private static boolean hasBeamFeature() {
467 IPackageManager pm = ActivityThread.getPackageManager();
468 if (pm == null) {
469 Log.e(TAG, "Cannot get package manager, assuming no Android Beam feature");
470 return false;
471 }
472 try {
473 return pm.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM, 0);
474 } catch (RemoteException e) {
475 Log.e(TAG, "Package manager query failed, assuming no Android Beam feature", e);
476 return false;
477 }
478 }
Andres Morales11d2e532014-07-23 12:39:55 -0700479
480 /**
Nick Pellyfdf90862010-10-18 11:19:41 -0700481 * Helper to check if this device has FEATURE_NFC, but without using
482 * a context.
483 * Equivalent to
484 * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)
485 */
486 private static boolean hasNfcFeature() {
487 IPackageManager pm = ActivityThread.getPackageManager();
488 if (pm == null) {
489 Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
490 return false;
491 }
492 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700493 return pm.hasSystemFeature(PackageManager.FEATURE_NFC, 0);
Nick Pellyfdf90862010-10-18 11:19:41 -0700494 } catch (RemoteException e) {
495 Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
496 return false;
497 }
498 }
499
Nick Pellyc84c89a2011-08-22 22:27:11 -0700500 /**
Steve Elliott04141ea2016-04-15 15:09:43 -0400501 * Helper to check if this device is NFC HCE capable, by checking for
502 * FEATURE_NFC_HOST_CARD_EMULATION and/or FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
503 * but without using a context.
504 */
505 private static boolean hasNfcHceFeature() {
506 IPackageManager pm = ActivityThread.getPackageManager();
507 if (pm == null) {
508 Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
509 return false;
510 }
511 try {
512 return pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION, 0)
513 || pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF, 0);
514 } catch (RemoteException e) {
515 Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
516 return false;
517 }
518 }
519
520 /**
Ruchi Kandoiebf267a2018-10-16 17:29:16 -0700521 * Return list of Secure Elements which support off host card emulation.
522 *
523 * @return List<String> containing secure elements on the device which supports
524 * off host card emulation. eSE for Embedded secure element,
525 * SIM for UICC and so on.
526 */
527 public @NonNull List<String> getSupportedOffHostSecureElements() {
528 List<String> offHostSE = new ArrayList<String>();
529 IPackageManager pm = ActivityThread.getPackageManager();
530 if (pm == null) {
531 Log.e(TAG, "Cannot get package manager, assuming no off-host CE feature");
532 return offHostSE;
533 }
534 try {
535 if (pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC, 0)) {
536 offHostSE.add("SIM");
537 }
538 if (pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE, 0)) {
539 offHostSE.add("eSE");
540 }
541 } catch (RemoteException e) {
542 Log.e(TAG, "Package manager query failed, assuming no off-host CE feature", e);
543 offHostSE.clear();
544 return offHostSE;
545 }
546 return offHostSE;
547 }
548
549 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600550 * Returns the NfcAdapter for application context,
551 * or throws if NFC is not available.
552 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700553 */
Mathew Inwood1961e1e2018-07-31 16:04:15 +0100554 @UnsupportedAppUsage
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600555 public static synchronized NfcAdapter getNfcAdapter(Context context) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800556 if (!sIsInitialized) {
Steve Elliott04141ea2016-04-15 15:09:43 -0400557 sHasNfcFeature = hasNfcFeature();
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700558 sHasBeamFeature = hasBeamFeature();
Steve Elliott04141ea2016-04-15 15:09:43 -0400559 boolean hasHceFeature = hasNfcHceFeature();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800560 /* is this device meant to have NFC */
Steve Elliott04141ea2016-04-15 15:09:43 -0400561 if (!sHasNfcFeature && !hasHceFeature) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800562 Log.v(TAG, "this device does not have NFC support");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700563 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800564 }
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800565 sService = getServiceInterface();
566 if (sService == null) {
567 Log.e(TAG, "could not retrieve NFC service");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700568 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800569 }
Steve Elliott04141ea2016-04-15 15:09:43 -0400570 if (sHasNfcFeature) {
571 try {
572 sTagService = sService.getNfcTagInterface();
573 } catch (RemoteException e) {
574 Log.e(TAG, "could not retrieve NFC Tag service");
575 throw new UnsupportedOperationException();
576 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100577 }
Steve Elliott04141ea2016-04-15 15:09:43 -0400578 if (hasHceFeature) {
579 try {
580 sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface();
581 } catch (RemoteException e) {
582 Log.e(TAG, "could not retrieve NFC-F card emulation service");
583 throw new UnsupportedOperationException();
584 }
585 try {
586 sCardEmulationService = sService.getNfcCardEmulationInterface();
587 } catch (RemoteException e) {
588 Log.e(TAG, "could not retrieve card emulation service");
589 throw new UnsupportedOperationException();
590 }
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900591 }
592
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600593 sIsInitialized = true;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800594 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600595 if (context == null) {
596 if (sNullContextNfcAdapter == null) {
597 sNullContextNfcAdapter = new NfcAdapter(null);
598 }
599 return sNullContextNfcAdapter;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700600 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600601 NfcAdapter adapter = sNfcAdapters.get(context);
602 if (adapter == null) {
603 adapter = new NfcAdapter(context);
604 sNfcAdapters.put(context, adapter);
605 }
606 return adapter;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800607 }
608
Nick Pelly6d55e132010-10-27 01:14:43 -0700609 /** get handle to NFC service interface */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800610 private static INfcAdapter getServiceInterface() {
Nick Pelly6d55e132010-10-27 01:14:43 -0700611 /* get a handle to NFC service */
612 IBinder b = ServiceManager.getService("nfc");
613 if (b == null) {
614 return null;
615 }
616 return INfcAdapter.Stub.asInterface(b);
617 }
618
Nick Pellyfdf90862010-10-18 11:19:41 -0700619 /**
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800620 * Helper to get the default NFC Adapter.
621 * <p>
622 * Most Android devices will only have one NFC Adapter (NFC Controller).
623 * <p>
624 * This helper is the equivalent of:
Scott Main97e0a1c2012-06-25 11:22:10 -0700625 * <pre>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800626 * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Scott Main97e0a1c2012-06-25 11:22:10 -0700627 * NfcAdapter adapter = manager.getDefaultAdapter();</pre>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800628 * @param context the calling application's context
629 *
630 * @return the default NFC adapter, or null if no NFC adapter exists
631 */
632 public static NfcAdapter getDefaultAdapter(Context context) {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600633 if (context == null) {
634 throw new IllegalArgumentException("context cannot be null");
635 }
636 context = context.getApplicationContext();
Nick Pellyb04cce02011-11-21 17:02:02 -0800637 if (context == null) {
638 throw new IllegalArgumentException(
639 "context not associated with any application (using a mock context?)");
640 }
641 /* use getSystemService() for consistency */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800642 NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Nick Pellya5193b242011-11-16 16:46:27 -0800643 if (manager == null) {
644 // NFC not available
645 return null;
646 }
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800647 return manager.getDefaultAdapter();
648 }
649
650 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600651 * Legacy NfcAdapter getter, always use {@link #getDefaultAdapter(Context)} instead.<p>
652 * This method was deprecated at API level 10 (Gingerbread MR1) because a context is required
653 * for many NFC API methods. Those methods will fail when called on an NfcAdapter
654 * object created from this method.<p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800655 * @deprecated use {@link #getDefaultAdapter(Context)}
Nick Pellya356bf12011-12-13 15:36:31 -0800656 * @hide
Nick Pelly590b73b2010-10-12 13:00:50 -0700657 */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800658 @Deprecated
Mathew Inwood1961e1e2018-07-31 16:04:15 +0100659 @UnsupportedAppUsage
Nick Pelly590b73b2010-10-12 13:00:50 -0700660 public static NfcAdapter getDefaultAdapter() {
Nick Pellyc97a5522012-01-05 15:13:01 +1100661 // introduced in API version 9 (GB 2.3)
Nick Pellya356bf12011-12-13 15:36:31 -0800662 // deprecated in API version 10 (GB 2.3.3)
663 // removed from public API in version 16 (ICS MR2)
Nick Pellyc97a5522012-01-05 15:13:01 +1100664 // should maintain as a hidden API for binary compatibility for a little longer
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800665 Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " +
666 "NfcAdapter.getDefaultAdapter(Context) instead", new Exception());
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600667
668 return NfcAdapter.getNfcAdapter(null);
669 }
670
671 NfcAdapter(Context context) {
672 mContext = context;
673 mNfcActivityManager = new NfcActivityManager(this);
Andres Moralesf9a97942014-08-14 15:43:30 -0700674 mNfcUnlockHandlers = new HashMap<NfcUnlockHandler, INfcUnlockHandler>();
Martijn Coenend55def82016-04-14 15:19:31 +0200675 mTagRemovedListener = null;
Andres Morales11d2e532014-07-23 12:39:55 -0700676 mLock = new Object();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800677 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700678
Nick Pellyc84c89a2011-08-22 22:27:11 -0700679 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600680 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700681 */
Mathew Inwood1961e1e2018-07-31 16:04:15 +0100682 @UnsupportedAppUsage
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600683 public Context getContext() {
684 return mContext;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800685 }
686
687 /**
688 * Returns the binder interface to the service.
689 * @hide
690 */
Mathew Inwood1961e1e2018-07-31 16:04:15 +0100691 @UnsupportedAppUsage
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800692 public INfcAdapter getService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800693 isEnabled(); // NOP call to recover sService if it is stale
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800694 return sService;
Nick Pelly590b73b2010-10-12 13:00:50 -0700695 }
696
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600697 /**
Nick Pelly3dd6c452011-01-10 18:14:41 +1100698 * Returns the binder interface to the tag service.
699 * @hide
700 */
701 public INfcTag getTagService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800702 isEnabled(); // NOP call to recover sTagService if it is stale
Nick Pelly3dd6c452011-01-10 18:14:41 +1100703 return sTagService;
704 }
705
706 /**
Martijn Coenena7397882013-07-30 20:07:47 -0700707 * Returns the binder interface to the card emulation service.
708 * @hide
709 */
710 public INfcCardEmulation getCardEmulationService() {
711 isEnabled();
712 return sCardEmulationService;
713 }
714
715 /**
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900716 * Returns the binder interface to the NFC-F card emulation service.
717 * @hide
718 */
719 public INfcFCardEmulation getNfcFCardEmulationService() {
720 isEnabled();
721 return sNfcFCardEmulationService;
722 }
723
724 /**
Love Khanna755a8fa2017-05-17 15:52:16 +0530725 * Returns the binder interface to the NFC-DTA test interface.
726 * @hide
727 */
728 public INfcDta getNfcDtaInterface() {
729 if (mContext == null) {
730 throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
731 + " NFC extras APIs");
732 }
733 try {
734 return sService.getNfcDtaInterface(mContext.getPackageName());
735 } catch (RemoteException e) {
736 attemptDeadServiceRecovery(e);
737 return null;
738 }
739 }
740
741 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600742 * NFC service dead - attempt best effort recovery
743 * @hide
744 */
Mathew Inwood1961e1e2018-07-31 16:04:15 +0100745 @UnsupportedAppUsage
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600746 public void attemptDeadServiceRecovery(Exception e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700747 Log.e(TAG, "NFC service dead - attempting to recover", e);
748 INfcAdapter service = getServiceInterface();
749 if (service == null) {
750 Log.e(TAG, "could not retrieve NFC service during service recovery");
Nick Pelly3dd6c452011-01-10 18:14:41 +1100751 // nothing more can be done now, sService is still stale, we'll hit
752 // this recovery path again later
Nick Pelly6d55e132010-10-27 01:14:43 -0700753 return;
754 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100755 // assigning to sService is not thread-safe, but this is best-effort code
756 // and on a well-behaved system should never happen
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800757 sService = service;
Nick Pelly3dd6c452011-01-10 18:14:41 +1100758 try {
759 sTagService = service.getNfcTagInterface();
760 } catch (RemoteException ee) {
761 Log.e(TAG, "could not retrieve NFC tag service during service recovery");
762 // nothing more can be done now, sService is still stale, we'll hit
763 // this recovery path again later
Martijn Coenena7397882013-07-30 20:07:47 -0700764 return;
765 }
766
767 try {
768 sCardEmulationService = service.getNfcCardEmulationInterface();
769 } catch (RemoteException ee) {
770 Log.e(TAG, "could not retrieve NFC card emulation service during service recovery");
Nick Pelly3dd6c452011-01-10 18:14:41 +1100771 }
772
Yoshinobu Itoc52adfe2016-01-22 18:14:18 +0900773 try {
774 sNfcFCardEmulationService = service.getNfcFCardEmulationInterface();
775 } catch (RemoteException ee) {
776 Log.e(TAG, "could not retrieve NFC-F card emulation service during service recovery");
777 }
778
Nick Pelly6d55e132010-10-27 01:14:43 -0700779 return;
780 }
781
Nick Pelly590b73b2010-10-12 13:00:50 -0700782 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700783 * Return true if this NFC Adapter has any features enabled.
Nick Pelly590b73b2010-10-12 13:00:50 -0700784 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800785 * <p>If this method returns false, the NFC hardware is guaranteed not to
Nick Pellycccf01d2011-10-31 14:49:40 -0700786 * generate or respond to any NFC communication over its NFC radio.
787 * <p>Applications can use this to check if NFC is enabled. Applications
788 * can request Settings UI allowing the user to toggle NFC using:
789 * <p><pre>startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))</pre>
Nick Pelly74fe6c62011-02-02 22:37:40 -0800790 *
Nick Pellycccf01d2011-10-31 14:49:40 -0700791 * @see android.provider.Settings#ACTION_NFC_SETTINGS
Nick Pelly74fe6c62011-02-02 22:37:40 -0800792 * @return true if this NFC Adapter has any features enabled
Nick Pelly590b73b2010-10-12 13:00:50 -0700793 */
Nick Pelly7ea5c452010-10-20 18:39:11 -0700794 public boolean isEnabled() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700795 try {
Nick Pelly8d32a012011-08-09 07:03:49 -0700796 return sService.getState() == STATE_ON;
Nick Pelly590b73b2010-10-12 13:00:50 -0700797 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700798 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700799 return false;
800 }
801 }
802
803 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700804 * Return the state of this NFC Adapter.
805 *
806 * <p>Returns one of {@link #STATE_ON}, {@link #STATE_TURNING_ON},
807 * {@link #STATE_OFF}, {@link #STATE_TURNING_OFF}.
808 *
809 * <p>{@link #isEnabled()} is equivalent to
810 * <code>{@link #getAdapterState()} == {@link #STATE_ON}</code>
811 *
812 * @return the current state of this NFC adapter
813 *
814 * @hide
815 */
Mathew Inwood1961e1e2018-07-31 16:04:15 +0100816 @UnsupportedAppUsage
Nick Pelly8d32a012011-08-09 07:03:49 -0700817 public int getAdapterState() {
818 try {
819 return sService.getState();
820 } catch (RemoteException e) {
821 attemptDeadServiceRecovery(e);
822 return NfcAdapter.STATE_OFF;
823 }
824 }
825
826 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700827 * Enable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700828 *
829 * <p>This call is asynchronous. Listen for
830 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
831 * operation is complete.
832 *
833 * <p>If this returns true, then either NFC is already on, or
834 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
835 * to indicate a state transition. If this returns false, then
836 * there is some problem that prevents an attempt to turn
837 * NFC on (for example we are in airplane mode and NFC is not
838 * toggleable in airplane mode on this platform).
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700839 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700840 * @hide
841 */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700842 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600843 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Nick Pelly7ea5c452010-10-20 18:39:11 -0700844 public boolean enable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700845 try {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800846 return sService.enable();
Nick Pelly590b73b2010-10-12 13:00:50 -0700847 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700848 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700849 return false;
850 }
851 }
852
853 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700854 * Disable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700855 *
856 * <p>No NFC features will work after this call, and the hardware
Nick Pelly7ea5c452010-10-20 18:39:11 -0700857 * will not perform or respond to any NFC communication.
Nick Pelly8d32a012011-08-09 07:03:49 -0700858 *
859 * <p>This call is asynchronous. Listen for
860 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
861 * operation is complete.
862 *
863 * <p>If this returns true, then either NFC is already off, or
864 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
865 * to indicate a state transition. If this returns false, then
866 * there is some problem that prevents an attempt to turn
867 * NFC off.
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700868 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700869 * @hide
870 */
Martijn Coenen29c2e372014-07-29 13:54:21 -0700871 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600872 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Nick Pelly7ea5c452010-10-20 18:39:11 -0700873 public boolean disable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700874 try {
Sunil Jogi3bba8d02012-04-10 13:12:26 -0700875 return sService.disable(true);
Nick Pelly590b73b2010-10-12 13:00:50 -0700876 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700877 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700878 return false;
879 }
880 }
881
Martijn Coenen2c103112012-05-15 10:32:15 -0700882 /**
Martijn Coenen56a3e672014-09-23 21:27:30 -0700883 * Disable NFC hardware.
884 * @hide
885 */
886 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600887 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Martijn Coenen56a3e672014-09-23 21:27:30 -0700888 public boolean disable(boolean persist) {
889 try {
890 return sService.disable(persist);
891 } catch (RemoteException e) {
892 attemptDeadServiceRecovery(e);
893 return false;
894 }
895 }
896
897 /**
Andres Morales9c4f4002014-09-08 17:09:16 -0700898 * Pauses polling for a {@code timeoutInMs} millis. If polling must be resumed before timeout,
899 * use {@link #resumePolling()}.
900 * @hide
901 */
902 public void pausePolling(int timeoutInMs) {
903 try {
904 sService.pausePolling(timeoutInMs);
905 } catch (RemoteException e) {
906 attemptDeadServiceRecovery(e);
907 }
908 }
909
910 /**
911 * Resumes default polling for the current device state if polling is paused. Calling
912 * this while polling is not paused is a no-op.
913 *
914 * @hide
915 */
916 public void resumePolling() {
917 try {
918 sService.resumePolling();
919 } catch (RemoteException e) {
920 attemptDeadServiceRecovery(e);
921 }
922 }
923
924 /**
Martijn Coenen2c103112012-05-15 10:32:15 -0700925 * Set one or more {@link Uri}s to send using Android Beam (TM). Every
926 * Uri you provide must have either scheme 'file' or scheme 'content'.
927 *
928 * <p>For the data provided through this method, Android Beam tries to
929 * switch to alternate transports such as Bluetooth to achieve a fast
930 * transfer speed. Hence this method is very suitable
931 * for transferring large files such as pictures or songs.
932 *
933 * <p>The receiving side will store the content of each Uri in
934 * a file and present a notification to the user to open the file
935 * with a {@link android.content.Intent} with action
936 * {@link android.content.Intent#ACTION_VIEW}.
937 * If multiple URIs are sent, the {@link android.content.Intent} will refer
938 * to the first of the stored files.
939 *
940 * <p>This method may be called at any time before {@link Activity#onDestroy},
941 * but the URI(s) are only made available for Android Beam when the
942 * specified activity(s) are in resumed (foreground) state. The recommended
943 * approach is to call this method during your Activity's
944 * {@link Activity#onCreate} - see sample
945 * code below. This method does not immediately perform any I/O or blocking work,
946 * so is safe to call on your main thread.
947 *
948 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
949 * have priority over both {@link #setNdefPushMessage} and
950 * {@link #setNdefPushMessageCallback}.
951 *
952 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
953 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
954 * then the Uri push will be completely disabled for the specified activity(s).
955 *
956 * <p>Code example:
957 * <pre>
958 * protected void onCreate(Bundle savedInstanceState) {
959 * super.onCreate(savedInstanceState);
960 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
961 * if (nfcAdapter == null) return; // NFC not available on this device
962 * nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700963 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700964 * And that is it. Only one call per activity is necessary. The Android
965 * OS will automatically release its references to the Uri(s) and the
966 * Activity object when it is destroyed if you follow this pattern.
967 *
968 * <p>If your Activity wants to dynamically supply Uri(s),
969 * then set a callback using {@link #setBeamPushUrisCallback} instead
970 * of using this method.
971 *
972 * <p class="note">Do not pass in an Activity that has already been through
973 * {@link Activity#onDestroy}. This is guaranteed if you call this API
974 * during {@link Activity#onCreate}.
975 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700976 * <p class="note">If this device does not support alternate transports
977 * such as Bluetooth or WiFI, calling this method does nothing.
978 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700979 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
980 *
981 * @param uris an array of Uri(s) to push over Android Beam
982 * @param activity activity for which the Uri(s) will be pushed
Steve Elliott04141ea2016-04-15 15:09:43 -0400983 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700984 * @deprecated this feature is deprecated.
Martijn Coenen2c103112012-05-15 10:32:15 -0700985 */
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700986 @java.lang.Deprecated
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700987 public void setBeamPushUris(Uri[] uris, Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -0400988 synchronized (NfcAdapter.class) {
989 if (!sHasNfcFeature) {
990 throw new UnsupportedOperationException();
991 }
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -0700992 if (!sHasBeamFeature) {
993 return;
994 }
Steve Elliott04141ea2016-04-15 15:09:43 -0400995 }
Nick Pelly1d7e9062012-04-03 17:46:00 -0700996 if (activity == null) {
997 throw new NullPointerException("activity cannot be null");
998 }
Martijn Coenen2c103112012-05-15 10:32:15 -0700999 if (uris != null) {
1000 for (Uri uri : uris) {
1001 if (uri == null) throw new NullPointerException("Uri not " +
1002 "allowed to be null");
1003 String scheme = uri.getScheme();
1004 if (scheme == null || (!scheme.equalsIgnoreCase("file") &&
1005 !scheme.equalsIgnoreCase("content"))) {
1006 throw new IllegalArgumentException("URI needs to have " +
1007 "either scheme file or scheme content");
1008 }
1009 }
1010 }
Martijn Coenen20e8dd92012-04-12 16:37:18 -07001011 mNfcActivityManager.setNdefPushContentUri(activity, uris);
1012 }
1013
Martijn Coenen2c103112012-05-15 10:32:15 -07001014 /**
1015 * Set a callback that will dynamically generate one or more {@link Uri}s
1016 * to send using Android Beam (TM). Every Uri the callback provides
1017 * must have either scheme 'file' or scheme 'content'.
1018 *
1019 * <p>For the data provided through this callback, Android Beam tries to
1020 * switch to alternate transports such as Bluetooth to achieve a fast
1021 * transfer speed. Hence this method is very suitable
1022 * for transferring large files such as pictures or songs.
1023 *
1024 * <p>The receiving side will store the content of each Uri in
1025 * a file and present a notification to the user to open the file
1026 * with a {@link android.content.Intent} with action
1027 * {@link android.content.Intent#ACTION_VIEW}.
1028 * If multiple URIs are sent, the {@link android.content.Intent} will refer
1029 * to the first of the stored files.
1030 *
1031 * <p>This method may be called at any time before {@link Activity#onDestroy},
1032 * but the URI(s) are only made available for Android Beam when the
1033 * specified activity(s) are in resumed (foreground) state. The recommended
1034 * approach is to call this method during your Activity's
1035 * {@link Activity#onCreate} - see sample
1036 * code below. This method does not immediately perform any I/O or blocking work,
1037 * so is safe to call on your main thread.
1038 *
1039 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
1040 * have priority over both {@link #setNdefPushMessage} and
1041 * {@link #setNdefPushMessageCallback}.
1042 *
1043 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
1044 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
1045 * then the Uri push will be completely disabled for the specified activity(s).
1046 *
1047 * <p>Code example:
1048 * <pre>
1049 * protected void onCreate(Bundle savedInstanceState) {
1050 * super.onCreate(savedInstanceState);
1051 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1052 * if (nfcAdapter == null) return; // NFC not available on this device
1053 * nfcAdapter.setBeamPushUrisCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001054 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -07001055 * And that is it. Only one call per activity is necessary. The Android
1056 * OS will automatically release its references to the Uri(s) and the
1057 * Activity object when it is destroyed if you follow this pattern.
1058 *
1059 * <p class="note">Do not pass in an Activity that has already been through
1060 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1061 * during {@link Activity#onCreate}.
1062 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -07001063 * <p class="note">If this device does not support alternate transports
1064 * such as Bluetooth or WiFI, calling this method does nothing.
1065 *
Martijn Coenen2c103112012-05-15 10:32:15 -07001066 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1067 *
1068 * @param callback callback, or null to disable
1069 * @param activity activity for which the Uri(s) will be pushed
Steve Elliott04141ea2016-04-15 15:09:43 -04001070 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001071 * @deprecated this feature is deprecated.
Martijn Coenen2c103112012-05-15 10:32:15 -07001072 */
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001073 @java.lang.Deprecated
Martijn Coenen20e8dd92012-04-12 16:37:18 -07001074 public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001075 synchronized (NfcAdapter.class) {
1076 if (!sHasNfcFeature) {
1077 throw new UnsupportedOperationException();
1078 }
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001079 if (!sHasBeamFeature) {
1080 return;
1081 }
Steve Elliott04141ea2016-04-15 15:09:43 -04001082 }
Martijn Coenen20e8dd92012-04-12 16:37:18 -07001083 if (activity == null) {
1084 throw new NullPointerException("activity cannot be null");
1085 }
1086 mNfcActivityManager.setNdefPushContentUriCallback(activity, callback);
Nick Pelly1d7e9062012-04-03 17:46:00 -07001087 }
1088
Jeff Hamilton52d32032011-01-08 15:31:26 -06001089 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -07001090 * Set a static {@link NdefMessage} to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -07001091 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001092 * <p>This method may be called at any time before {@link Activity#onDestroy},
1093 * but the NDEF message is only made available for NDEF push when the
1094 * specified activity(s) are in resumed (foreground) state. The recommended
1095 * approach is to call this method during your Activity's
1096 * {@link Activity#onCreate} - see sample
1097 * code below. This method does not immediately perform any I/O or blocking work,
1098 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001099 *
1100 * <p>Only one NDEF message can be pushed by the currently resumed activity.
1101 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -07001102 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -07001103 * the callback will take priority.
1104 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001105 * <p>If neither {@link #setNdefPushMessage} or
1106 * {@link #setNdefPushMessageCallback} have been called for your activity, then
1107 * the Android OS may choose to send a default NDEF message on your behalf,
1108 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001109 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001110 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
1111 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
1112 * then NDEF push will be completely disabled for the specified activity(s).
1113 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -07001114 * otherwise sent on your behalf for those activity(s).
1115 *
1116 * <p>If you want to prevent the Android OS from sending default NDEF
1117 * messages completely (for all activities), you can include a
Neil Fuller9498e8a2015-11-30 09:51:33 +00001118 * {@code <meta-data>} element inside the {@code <application>}
Martijn Coenen854e0772012-04-02 14:52:10 -07001119 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -07001120 * <pre>
1121 * &lt;application ...>
1122 * &lt;meta-data android:name="android.nfc.disable_beam_default"
1123 * android:value="true" />
1124 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001125 *
1126 * <p>The API allows for multiple activities to be specified at a time,
1127 * but it is strongly recommended to just register one at a time,
1128 * and to do so during the activity's {@link Activity#onCreate}. For example:
1129 * <pre>
1130 * protected void onCreate(Bundle savedInstanceState) {
1131 * super.onCreate(savedInstanceState);
1132 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1133 * if (nfcAdapter == null) return; // NFC not available on this device
1134 * nfcAdapter.setNdefPushMessage(ndefMessage, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001135 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001136 * And that is it. Only one call per activity is necessary. The Android
1137 * OS will automatically release its references to the NDEF message and the
1138 * Activity object when it is destroyed if you follow this pattern.
1139 *
1140 * <p>If your Activity wants to dynamically generate an NDEF message,
1141 * then set a callback using {@link #setNdefPushMessageCallback} instead
1142 * of a static message.
1143 *
1144 * <p class="note">Do not pass in an Activity that has already been through
1145 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1146 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -07001147 *
Martijn Coenen2c103112012-05-15 10:32:15 -07001148 * <p class="note">For sending large content such as pictures and songs,
1149 * consider using {@link #setBeamPushUris}, which switches to alternate transports
1150 * such as Bluetooth to achieve a fast transfer rate.
1151 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001152 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1153 *
1154 * @param message NDEF message to push over NFC, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001155 * @param activity activity for which the NDEF message will be pushed
1156 * @param activities optional additional activities, however we strongly recommend
1157 * to only register one at a time, and to do so in that activity's
1158 * {@link Activity#onCreate}
Steve Elliott04141ea2016-04-15 15:09:43 -04001159 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001160 * @deprecated this feature is deprecated.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001161 */
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001162 @java.lang.Deprecated
Nick Pelly82328bf2011-08-30 09:37:25 -07001163 public void setNdefPushMessage(NdefMessage message, Activity activity,
1164 Activity ... activities) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001165 synchronized (NfcAdapter.class) {
1166 if (!sHasNfcFeature) {
1167 throw new UnsupportedOperationException();
1168 }
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001169 if (!sHasBeamFeature) {
1170 return;
1171 }
Steve Elliott04141ea2016-04-15 15:09:43 -04001172 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001173 int targetSdkVersion = getSdkVersion();
1174 try {
1175 if (activity == null) {
1176 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001177 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001178 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001179 for (Activity a : activities) {
1180 if (a == null) {
1181 throw new NullPointerException("activities cannot contain null");
1182 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001183 mNfcActivityManager.setNdefPushMessage(a, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001184 }
1185 } catch (IllegalStateException e) {
1186 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1187 // Less strict on old applications - just log the error
1188 Log.e(TAG, "Cannot call API with Activity that has already " +
1189 "been destroyed", e);
1190 } else {
1191 // Prevent new applications from making this mistake, re-throw
1192 throw(e);
1193 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001194 }
1195 }
1196
1197 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001198 * @hide
1199 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001200 @SystemApi
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001201 public void setNdefPushMessage(NdefMessage message, Activity activity, int flags) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001202 synchronized (NfcAdapter.class) {
1203 if (!sHasNfcFeature) {
1204 throw new UnsupportedOperationException();
1205 }
1206 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001207 if (activity == null) {
1208 throw new NullPointerException("activity cannot be null");
1209 }
1210 mNfcActivityManager.setNdefPushMessage(activity, message, flags);
1211 }
1212
1213 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -07001214 * Set a callback that dynamically generates NDEF messages to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -07001215 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001216 * <p>This method may be called at any time before {@link Activity#onDestroy},
1217 * but the NDEF message callback can only occur when the
1218 * specified activity(s) are in resumed (foreground) state. The recommended
1219 * approach is to call this method during your Activity's
1220 * {@link Activity#onCreate} - see sample
1221 * code below. This method does not immediately perform any I/O or blocking work,
1222 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001223 *
1224 * <p>Only one NDEF message can be pushed by the currently resumed activity.
1225 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -07001226 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -07001227 * the callback will take priority.
1228 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001229 * <p>If neither {@link #setNdefPushMessage} or
1230 * {@link #setNdefPushMessageCallback} have been called for your activity, then
1231 * the Android OS may choose to send a default NDEF message on your behalf,
1232 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001233 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001234 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
1235 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
1236 * then NDEF push will be completely disabled for the specified activity(s).
1237 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -07001238 * otherwise sent on your behalf for those activity(s).
1239 *
1240 * <p>If you want to prevent the Android OS from sending default NDEF
1241 * messages completely (for all activities), you can include a
Neil Fuller9498e8a2015-11-30 09:51:33 +00001242 * {@code <meta-data>} element inside the {@code <application>}
Martijn Coenen854e0772012-04-02 14:52:10 -07001243 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -07001244 * <pre>
1245 * &lt;application ...>
1246 * &lt;meta-data android:name="android.nfc.disable_beam_default"
1247 * android:value="true" />
1248 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001249 *
1250 * <p>The API allows for multiple activities to be specified at a time,
1251 * but it is strongly recommended to just register one at a time,
1252 * and to do so during the activity's {@link Activity#onCreate}. For example:
1253 * <pre>
1254 * protected void onCreate(Bundle savedInstanceState) {
1255 * super.onCreate(savedInstanceState);
1256 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1257 * if (nfcAdapter == null) return; // NFC not available on this device
1258 * nfcAdapter.setNdefPushMessageCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001259 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001260 * And that is it. Only one call per activity is necessary. The Android
1261 * OS will automatically release its references to the callback and the
1262 * Activity object when it is destroyed if you follow this pattern.
1263 *
1264 * <p class="note">Do not pass in an Activity that has already been through
1265 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1266 * during {@link Activity#onCreate}.
Martijn Coenen2c103112012-05-15 10:32:15 -07001267 * <p class="note">For sending large content such as pictures and songs,
1268 * consider using {@link #setBeamPushUris}, which switches to alternate transports
1269 * such as Bluetooth to achieve a fast transfer rate.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001270 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1271 *
1272 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001273 * @param activity activity for which the NDEF message will be pushed
1274 * @param activities optional additional activities, however we strongly recommend
1275 * to only register one at a time, and to do so in that activity's
1276 * {@link Activity#onCreate}
Steve Elliott04141ea2016-04-15 15:09:43 -04001277 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001278 * @deprecated this feature is deprecated.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001279 */
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001280 @java.lang.Deprecated
Nick Pelly82328bf2011-08-30 09:37:25 -07001281 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
Nick Pellyc84c89a2011-08-22 22:27:11 -07001282 Activity ... activities) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001283 synchronized (NfcAdapter.class) {
1284 if (!sHasNfcFeature) {
1285 throw new UnsupportedOperationException();
1286 }
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001287 if (!sHasBeamFeature) {
1288 return;
1289 }
Steve Elliott04141ea2016-04-15 15:09:43 -04001290 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001291 int targetSdkVersion = getSdkVersion();
1292 try {
1293 if (activity == null) {
1294 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001295 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001296 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001297 for (Activity a : activities) {
1298 if (a == null) {
1299 throw new NullPointerException("activities cannot contain null");
1300 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001301 mNfcActivityManager.setNdefPushMessageCallback(a, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -07001302 }
1303 } catch (IllegalStateException e) {
1304 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1305 // Less strict on old applications - just log the error
1306 Log.e(TAG, "Cannot call API with Activity that has already " +
1307 "been destroyed", e);
1308 } else {
1309 // Prevent new applications from making this mistake, re-throw
1310 throw(e);
1311 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001312 }
1313 }
1314
1315 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001316 * @hide
1317 */
Mathew Inwood1961e1e2018-07-31 16:04:15 +01001318 @UnsupportedAppUsage
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001319 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
1320 int flags) {
1321 if (activity == null) {
1322 throw new NullPointerException("activity cannot be null");
1323 }
1324 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, flags);
1325 }
1326
1327 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -07001328 * Set a callback on successful Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -07001329 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001330 * <p>This method may be called at any time before {@link Activity#onDestroy},
1331 * but the callback can only occur when the
1332 * specified activity(s) are in resumed (foreground) state. The recommended
1333 * approach is to call this method during your Activity's
1334 * {@link Activity#onCreate} - see sample
1335 * code below. This method does not immediately perform any I/O or blocking work,
1336 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001337 *
Nick Pelly8ce7a272012-03-21 15:14:09 -07001338 * <p>The API allows for multiple activities to be specified at a time,
1339 * but it is strongly recommended to just register one at a time,
1340 * and to do so during the activity's {@link Activity#onCreate}. For example:
1341 * <pre>
1342 * protected void onCreate(Bundle savedInstanceState) {
1343 * super.onCreate(savedInstanceState);
1344 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
1345 * if (nfcAdapter == null) return; // NFC not available on this device
1346 * nfcAdapter.setOnNdefPushCompleteCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -07001347 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -07001348 * And that is it. Only one call per activity is necessary. The Android
1349 * OS will automatically release its references to the callback and the
1350 * Activity object when it is destroyed if you follow this pattern.
1351 *
1352 * <p class="note">Do not pass in an Activity that has already been through
1353 * {@link Activity#onDestroy}. This is guaranteed if you call this API
1354 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -07001355 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001356 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1357 *
1358 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -07001359 * @param activity activity for which the NDEF message will be pushed
1360 * @param activities optional additional activities, however we strongly recommend
1361 * to only register one at a time, and to do so in that activity's
1362 * {@link Activity#onCreate}
Steve Elliott04141ea2016-04-15 15:09:43 -04001363 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001364 * @deprecated this feature is deprecated.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001365 */
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001366 @java.lang.Deprecated
Nick Pellyc84c89a2011-08-22 22:27:11 -07001367 public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback,
Nick Pelly82328bf2011-08-30 09:37:25 -07001368 Activity activity, Activity ... activities) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001369 synchronized (NfcAdapter.class) {
1370 if (!sHasNfcFeature) {
1371 throw new UnsupportedOperationException();
1372 }
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001373 if (!sHasBeamFeature) {
1374 return;
1375 }
Steve Elliott04141ea2016-04-15 15:09:43 -04001376 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001377 int targetSdkVersion = getSdkVersion();
1378 try {
1379 if (activity == null) {
1380 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -07001381 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001382 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, callback);
1383 for (Activity a : activities) {
1384 if (a == null) {
1385 throw new NullPointerException("activities cannot contain null");
1386 }
1387 mNfcActivityManager.setOnNdefPushCompleteCallback(a, callback);
1388 }
1389 } catch (IllegalStateException e) {
1390 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
1391 // Less strict on old applications - just log the error
1392 Log.e(TAG, "Cannot call API with Activity that has already " +
1393 "been destroyed", e);
1394 } else {
1395 // Prevent new applications from making this mistake, re-throw
1396 throw(e);
1397 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001398 }
1399 }
1400
1401 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001402 * Enable foreground dispatch to the given Activity.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001403 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001404 * <p>This will give give priority to the foreground activity when
1405 * dispatching a discovered {@link Tag} to an application.
1406 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001407 * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents
1408 * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and
1409 * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED}
1410 * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled
1411 * by passing in the tech lists separately. Each first level entry in the tech list represents
1412 * an array of technologies that must all be present to match. If any of the first level sets
1413 * match then the dispatch is routed through the given PendingIntent. In other words, the second
1414 * level is ANDed together and the first level entries are ORed together.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001415 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001416 * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters
1417 * that acts a wild card and will cause the foreground activity to receive all tags via the
1418 * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001419 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001420 * <p>This method must be called from the main thread, and only when the activity is in the
1421 * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before
1422 * the completion of their {@link Activity#onPause} callback to disable foreground dispatch
1423 * after it has been enabled.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001424 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001425 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1426 *
Jeff Hamilton52d32032011-01-08 15:31:26 -06001427 * @param activity the Activity to dispatch to
1428 * @param intent the PendingIntent to start for the dispatch
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001429 * @param filters the IntentFilters to override dispatching for, or null to always dispatch
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001430 * @param techLists the tech lists used to perform matching for dispatching of the
1431 * {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent
Nick Pelly74fe6c62011-02-02 22:37:40 -08001432 * @throws IllegalStateException if the Activity is not currently in the foreground
Steve Elliott04141ea2016-04-15 15:09:43 -04001433 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001434 */
1435 public void enableForegroundDispatch(Activity activity, PendingIntent intent,
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001436 IntentFilter[] filters, String[][] techLists) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001437 synchronized (NfcAdapter.class) {
1438 if (!sHasNfcFeature) {
1439 throw new UnsupportedOperationException();
1440 }
1441 }
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001442 if (activity == null || intent == null) {
Jeff Hamilton52d32032011-01-08 15:31:26 -06001443 throw new NullPointerException();
1444 }
1445 if (!activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001446 throw new IllegalStateException("Foreground dispatch can only be enabled " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001447 "when your activity is resumed");
1448 }
1449 try {
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001450 TechListParcel parcel = null;
1451 if (techLists != null && techLists.length > 0) {
1452 parcel = new TechListParcel(techLists);
1453 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06001454 ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001455 mForegroundDispatchListener);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001456 sService.setForegroundDispatch(intent, filters, parcel);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001457 } catch (RemoteException e) {
1458 attemptDeadServiceRecovery(e);
1459 }
1460 }
1461
1462 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001463 * Disable foreground dispatch to the given activity.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001464 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001465 * <p>After calling {@link #enableForegroundDispatch}, an activity
1466 * must call this method before its {@link Activity#onPause} callback
1467 * completes.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001468 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001469 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001470 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001471 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1472 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001473 * @param activity the Activity to disable dispatch to
1474 * @throws IllegalStateException if the Activity has already been paused
Steve Elliott04141ea2016-04-15 15:09:43 -04001475 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001476 */
1477 public void disableForegroundDispatch(Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001478 synchronized (NfcAdapter.class) {
1479 if (!sHasNfcFeature) {
1480 throw new UnsupportedOperationException();
1481 }
1482 }
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001483 ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,
1484 mForegroundDispatchListener);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001485 disableForegroundDispatchInternal(activity, false);
1486 }
1487
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001488 OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001489 @Override
1490 public void onPaused(Activity activity) {
1491 disableForegroundDispatchInternal(activity, true);
1492 }
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001493 };
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001494
Jeff Hamilton52d32032011-01-08 15:31:26 -06001495 void disableForegroundDispatchInternal(Activity activity, boolean force) {
1496 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001497 sService.setForegroundDispatch(null, null, null);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001498 if (!force && !activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001499 throw new IllegalStateException("You must disable foreground dispatching " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001500 "while your activity is still resumed");
1501 }
1502 } catch (RemoteException e) {
1503 attemptDeadServiceRecovery(e);
1504 }
1505 }
1506
Nick Pelly590b73b2010-10-12 13:00:50 -07001507 /**
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001508 * Limit the NFC controller to reader mode while this Activity is in the foreground.
1509 *
1510 * <p>In this mode the NFC controller will only act as an NFC tag reader/writer,
1511 * thus disabling any peer-to-peer (Android Beam) and card-emulation modes of
1512 * the NFC adapter on this device.
1513 *
1514 * <p>Use {@link #FLAG_READER_SKIP_NDEF_CHECK} to prevent the platform from
1515 * performing any NDEF checks in reader mode. Note that this will prevent the
1516 * {@link Ndef} tag technology from being enumerated on the tag, and that
1517 * NDEF-based tag dispatch will not be functional.
1518 *
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001519 * <p>For interacting with tags that are emulated on another Android device
1520 * using Android's host-based card-emulation, the recommended flags are
1521 * {@link #FLAG_READER_NFC_A} and {@link #FLAG_READER_SKIP_NDEF_CHECK}.
1522 *
1523 * @param activity the Activity that requests the adapter to be in reader mode
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001524 * @param callback the callback to be called when a tag is discovered
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001525 * @param flags Flags indicating poll technologies and other optional parameters
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001526 * @param extras Additional extras for configuring reader mode.
Steve Elliott04141ea2016-04-15 15:09:43 -04001527 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001528 */
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001529 public void enableReaderMode(Activity activity, ReaderCallback callback, int flags,
1530 Bundle extras) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001531 synchronized (NfcAdapter.class) {
1532 if (!sHasNfcFeature) {
1533 throw new UnsupportedOperationException();
1534 }
1535 }
Martijn Coenen5b1e0322013-09-02 20:38:47 -07001536 mNfcActivityManager.enableReaderMode(activity, callback, flags, extras);
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001537 }
1538
1539 /**
1540 * Restore the NFC adapter to normal mode of operation: supporting
1541 * peer-to-peer (Android Beam), card emulation, and polling for
1542 * all supported tag technologies.
1543 *
1544 * @param activity the Activity that currently has reader mode enabled
Steve Elliott04141ea2016-04-15 15:09:43 -04001545 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001546 */
1547 public void disableReaderMode(Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001548 synchronized (NfcAdapter.class) {
1549 if (!sHasNfcFeature) {
1550 throw new UnsupportedOperationException();
1551 }
1552 }
Martijn Coenenc20ed2f2013-08-27 14:32:53 -07001553 mNfcActivityManager.disableReaderMode(activity);
1554 }
1555
1556 /**
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001557 * Manually invoke Android Beam to share data.
1558 *
1559 * <p>The Android Beam animation is normally only shown when two NFC-capable
1560 * devices come into range.
1561 * By calling this method, an Activity can invoke the Beam animation directly
1562 * even if no other NFC device is in range yet. The Beam animation will then
1563 * prompt the user to tap another NFC-capable device to complete the data
1564 * transfer.
1565 *
1566 * <p>The main advantage of using this method is that it avoids the need for the
1567 * user to tap the screen to complete the transfer, as this method already
1568 * establishes the direction of the transfer and the consent of the user to
1569 * share data. Callers are responsible for making sure that the user has
1570 * consented to sharing data on NFC tap.
1571 *
1572 * <p>Note that to use this method, the passed in Activity must have already
1573 * set data to share over Beam by using method calls such as
Martijn Coenen6924c4e2014-02-25 11:37:17 -08001574 * {@link #setNdefPushMessageCallback} or
1575 * {@link #setBeamPushUrisCallback}.
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001576 *
1577 * @param activity the current foreground Activity that has registered data to share
1578 * @return whether the Beam animation was successfully invoked
Steve Elliott04141ea2016-04-15 15:09:43 -04001579 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001580 * @deprecated this feature is deprecated.
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001581 */
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001582 @java.lang.Deprecated
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001583 public boolean invokeBeam(Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001584 synchronized (NfcAdapter.class) {
1585 if (!sHasNfcFeature) {
1586 throw new UnsupportedOperationException();
1587 }
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001588 if (!sHasBeamFeature) {
1589 return false;
1590 }
Steve Elliott04141ea2016-04-15 15:09:43 -04001591 }
Martijn Coenen7fe9fa12014-01-29 17:28:04 -08001592 if (activity == null) {
1593 throw new NullPointerException("activity may not be null.");
1594 }
1595 enforceResumed(activity);
1596 try {
1597 sService.invokeBeam();
1598 return true;
1599 } catch (RemoteException e) {
1600 Log.e(TAG, "invokeBeam: NFC process has died.");
1601 attemptDeadServiceRecovery(e);
1602 return false;
1603 }
1604 }
1605
1606 /**
Martijn Coenendbedb4c2014-07-01 15:52:19 -07001607 * @hide
1608 */
1609 public boolean invokeBeam(BeamShareData shareData) {
1610 try {
1611 Log.e(TAG, "invokeBeamInternal()");
1612 sService.invokeBeamInternal(shareData);
1613 return true;
1614 } catch (RemoteException e) {
1615 Log.e(TAG, "invokeBeam: NFC process has died.");
1616 attemptDeadServiceRecovery(e);
1617 return false;
1618 }
1619 }
1620
1621 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001622 * Enable NDEF message push over NFC while this Activity is in the foreground.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001623 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001624 * <p>You must explicitly call this method every time the activity is
1625 * resumed, and you must call {@link #disableForegroundNdefPush} before
1626 * your activity completes {@link Activity#onPause}.
1627 *
1628 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1629 * instead: it automatically hooks into your activity life-cycle,
1630 * so you do not need to call enable/disable in your onResume/onPause.
1631 *
1632 * <p>For NDEF push to function properly the other NFC device must
1633 * support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or
1634 * Android's "com.android.npp" (Ndef Push Protocol). This was optional
1635 * on Gingerbread level Android NFC devices, but SNEP is mandatory on
1636 * Ice-Cream-Sandwich and beyond.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001637 *
1638 * <p>This method must be called from the main thread.
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001639 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001640 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1641 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001642 * @param activity foreground activity
1643 * @param message a NDEF Message to push over NFC
1644 * @throws IllegalStateException if the activity is not currently in the foreground
Steve Elliott04141ea2016-04-15 15:09:43 -04001645 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001646 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001647 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001648 @Deprecated
1649 public void enableForegroundNdefPush(Activity activity, NdefMessage message) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001650 synchronized (NfcAdapter.class) {
1651 if (!sHasNfcFeature) {
1652 throw new UnsupportedOperationException();
1653 }
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001654 if (!sHasBeamFeature) {
1655 return;
1656 }
Steve Elliott04141ea2016-04-15 15:09:43 -04001657 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001658 if (activity == null || message == null) {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001659 throw new NullPointerException();
1660 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001661 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001662 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Jason parks01425362011-05-24 02:57:37 -07001663 }
1664
1665 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001666 * Disable NDEF message push over P2P.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001667 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001668 * <p>After calling {@link #enableForegroundNdefPush}, an activity
1669 * must call this method before its {@link Activity#onPause} callback
1670 * completes.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001671 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001672 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1673 * instead: it automatically hooks into your activity life-cycle,
1674 * so you do not need to call enable/disable in your onResume/onPause.
1675 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001676 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001677 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001678 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1679 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001680 * @param activity the Foreground activity
1681 * @throws IllegalStateException if the Activity has already been paused
Steve Elliott04141ea2016-04-15 15:09:43 -04001682 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Nick Pellyc84c89a2011-08-22 22:27:11 -07001683 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001684 */
Nick Pellyc97a5522012-01-05 15:13:01 +11001685 @Deprecated
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001686 public void disableForegroundNdefPush(Activity activity) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001687 synchronized (NfcAdapter.class) {
1688 if (!sHasNfcFeature) {
1689 throw new UnsupportedOperationException();
1690 }
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001691 if (!sHasBeamFeature) {
1692 return;
1693 }
Steve Elliott04141ea2016-04-15 15:09:43 -04001694 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001695 if (activity == null) {
1696 throw new NullPointerException();
1697 }
1698 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001699 mNfcActivityManager.setNdefPushMessage(activity, null, 0);
1700 mNfcActivityManager.setNdefPushMessageCallback(activity, null, 0);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001701 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, null);
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001702 }
1703
Nick Pellyc84c89a2011-08-22 22:27:11 -07001704 /**
Ruchi Kandoid97bb092019-01-08 14:16:25 -08001705 * Sets Secure NFC feature.
1706 * <p>This API is for the Settings application.
1707 * @hide
1708 */
1709 @SystemApi
1710 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
1711 public boolean setNfcSecure(boolean enable) {
1712 if (!sHasNfcFeature) {
1713 throw new UnsupportedOperationException();
1714 }
1715 try {
1716 return sService.setNfcSecure(enable);
1717 } catch (RemoteException e) {
1718 attemptDeadServiceRecovery(e);
1719 return false;
1720 }
1721 }
1722
1723 /**
1724 * Checks if the device supports Secure NFC functionality.
1725 *
1726 * @return True if device supports Secure NFC, false otherwise
1727 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1728 */
1729 public boolean deviceSupportsNfcSecure() {
1730 if (!sHasNfcFeature) {
1731 throw new UnsupportedOperationException();
1732 }
1733 try {
1734 return sService.deviceSupportsNfcSecure();
1735 } catch (RemoteException e) {
1736 attemptDeadServiceRecovery(e);
1737 return false;
1738 }
1739 }
1740
1741 /**
1742 * Checks Secure NFC feature is enabled.
1743 *
1744 * @return True if device supports Secure NFC is enabled, false otherwise
1745 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
1746 * @throws UnsupportedOperationException if device doesn't support
1747 * Secure NFC functionality. {@link #deviceSupportsNfcSecure}
1748 */
1749 public boolean isNfcSecureEnabled() {
1750 if (!sHasNfcFeature) {
1751 throw new UnsupportedOperationException();
1752 }
1753 try {
1754 return sService.isNfcSecureEnabled();
1755 } catch (RemoteException e) {
1756 attemptDeadServiceRecovery(e);
1757 return false;
1758 }
1759 }
1760
1761 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001762 * Enable NDEF Push feature.
1763 * <p>This API is for the Settings application.
1764 * @hide
1765 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001766 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001767 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Nick Pellyc84c89a2011-08-22 22:27:11 -07001768 public boolean enableNdefPush() {
Steve Elliott04141ea2016-04-15 15:09:43 -04001769 if (!sHasNfcFeature) {
1770 throw new UnsupportedOperationException();
1771 }
Martijn Coenen6d748942011-07-21 09:34:25 +02001772 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001773 return sService.enableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001774 } catch (RemoteException e) {
1775 attemptDeadServiceRecovery(e);
1776 return false;
1777 }
1778 }
1779
1780 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001781 * Disable NDEF Push feature.
1782 * <p>This API is for the Settings application.
Martijn Coenen6d748942011-07-21 09:34:25 +02001783 * @hide
1784 */
Martijn Coenen29c2e372014-07-29 13:54:21 -07001785 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001786 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Nick Pellyc84c89a2011-08-22 22:27:11 -07001787 public boolean disableNdefPush() {
Steve Elliott04141ea2016-04-15 15:09:43 -04001788 synchronized (NfcAdapter.class) {
1789 if (!sHasNfcFeature) {
1790 throw new UnsupportedOperationException();
1791 }
1792 }
Martijn Coenen6d748942011-07-21 09:34:25 +02001793 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001794 return sService.disableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001795 } catch (RemoteException e) {
1796 attemptDeadServiceRecovery(e);
1797 return false;
1798 }
1799 }
1800
1801 /**
Nick Pellycccf01d2011-10-31 14:49:40 -07001802 * Return true if the NDEF Push (Android Beam) feature is enabled.
1803 * <p>This function will return true only if both NFC is enabled, and the
1804 * NDEF Push feature is enabled.
1805 * <p>Note that if NFC is enabled but NDEF Push is disabled then this
1806 * device can still <i>receive</i> NDEF messages, it just cannot send them.
1807 * <p>Applications cannot directly toggle the NDEF Push feature, but they
1808 * can request Settings UI allowing the user to toggle NDEF Push using
1809 * <code>startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))</code>
1810 * <p>Example usage in an Activity that requires NDEF Push:
1811 * <p><pre>
1812 * protected void onResume() {
1813 * super.onResume();
1814 * if (!nfcAdapter.isEnabled()) {
1815 * startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
1816 * } else if (!nfcAdapter.isNdefPushEnabled()) {
1817 * startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
1818 * }
Scott Main97e0a1c2012-06-25 11:22:10 -07001819 * }</pre>
Martijn Coenen6d748942011-07-21 09:34:25 +02001820 *
Nick Pellycccf01d2011-10-31 14:49:40 -07001821 * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS
Nick Pellyc84c89a2011-08-22 22:27:11 -07001822 * @return true if NDEF Push feature is enabled
Steve Elliott04141ea2016-04-15 15:09:43 -04001823 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001824 * @deprecated this feature is deprecated.
Martijn Coenen6d748942011-07-21 09:34:25 +02001825 */
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001826 @java.lang.Deprecated
1827
Nick Pellyc84c89a2011-08-22 22:27:11 -07001828 public boolean isNdefPushEnabled() {
Steve Elliott04141ea2016-04-15 15:09:43 -04001829 synchronized (NfcAdapter.class) {
1830 if (!sHasNfcFeature) {
1831 throw new UnsupportedOperationException();
1832 }
Ruchi Kandoi96fe50b2018-10-23 14:40:29 -07001833 if (!sHasBeamFeature) {
1834 return false;
1835 }
Steve Elliott04141ea2016-04-15 15:09:43 -04001836 }
Martijn Coenen6d748942011-07-21 09:34:25 +02001837 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001838 return sService.isNdefPushEnabled();
Martijn Coenen6d748942011-07-21 09:34:25 +02001839 } catch (RemoteException e) {
1840 attemptDeadServiceRecovery(e);
1841 return false;
1842 }
1843 }
1844
1845 /**
Martijn Coenend55def82016-04-14 15:19:31 +02001846 * Signals that you are no longer interested in communicating with an NFC tag
1847 * for as long as it remains in range.
1848 *
1849 * All future attempted communication to this tag will fail with {@link IOException}.
1850 * The NFC controller will be put in a low-power polling mode, allowing the device
1851 * to save power in cases where it's "attached" to a tag all the time (e.g. a tag in
1852 * car dock).
1853 *
1854 * Additionally the debounceMs parameter allows you to specify for how long the tag needs
1855 * to have gone out of range, before it will be dispatched again.
1856 *
1857 * Note: the NFC controller typically polls at a pretty slow interval (100 - 500 ms).
1858 * This means that if the tag repeatedly goes in and out of range (for example, in
1859 * case of a flaky connection), and the controller happens to poll every time the
1860 * tag is out of range, it *will* re-dispatch the tag after debounceMs, despite the tag
1861 * having been "in range" during the interval.
1862 *
1863 * Note 2: if a tag with another UID is detected after this API is called, its effect
1864 * will be cancelled; if this tag shows up before the amount of time specified in
1865 * debounceMs, it will be dispatched again.
1866 *
1867 * Note 3: some tags have a random UID, in which case this API won't work reliably.
1868 *
1869 * @param tag the {@link android.nfc.Tag Tag} to ignore.
1870 * @param debounceMs minimum amount of time the tag needs to be out of range before being
1871 * dispatched again.
1872 * @param tagRemovedListener listener to be called when the tag is removed from the field.
1873 * Note that this will only be called if the tag has been out of range
1874 * for at least debounceMs, or if another tag came into range before
1875 * debounceMs. May be null in case you don't want a callback.
1876 * @param handler the {@link android.os.Handler Handler} that will be used for delivering
1877 * the callback. if the handler is null, then the thread used for delivering
1878 * the callback is unspecified.
1879 * @return false if the tag couldn't be found (or has already gone out of range), true otherwise
1880 */
1881 public boolean ignore(final Tag tag, int debounceMs,
1882 final OnTagRemovedListener tagRemovedListener, final Handler handler) {
1883 ITagRemovedCallback.Stub iListener = null;
1884 if (tagRemovedListener != null) {
1885 iListener = new ITagRemovedCallback.Stub() {
1886 @Override
1887 public void onTagRemoved() throws RemoteException {
1888 if (handler != null) {
1889 handler.post(new Runnable() {
1890 @Override
1891 public void run() {
1892 tagRemovedListener.onTagRemoved();
1893 }
1894 });
1895 } else {
1896 tagRemovedListener.onTagRemoved();
1897 }
1898 synchronized (mLock) {
1899 mTagRemovedListener = null;
1900 }
1901 }
1902 };
1903 }
1904 synchronized (mLock) {
1905 mTagRemovedListener = iListener;
1906 }
1907 try {
1908 return sService.ignore(tag.getServiceHandle(), debounceMs, iListener);
1909 } catch (RemoteException e) {
1910 return false;
1911 }
1912 }
1913
1914 /**
Nick Pellyc97a5522012-01-05 15:13:01 +11001915 * Inject a mock NFC tag.<p>
1916 * Used for testing purposes.
1917 * <p class="note">Requires the
1918 * {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
1919 * @hide
1920 */
Nick Pelly1f5badc2012-01-24 13:22:58 -08001921 public void dispatch(Tag tag) {
Nick Pellyc97a5522012-01-05 15:13:01 +11001922 if (tag == null) {
1923 throw new NullPointerException("tag cannot be null");
1924 }
1925 try {
Nick Pelly1f5badc2012-01-24 13:22:58 -08001926 sService.dispatch(tag);
Nick Pellyc97a5522012-01-05 15:13:01 +11001927 } catch (RemoteException e) {
1928 attemptDeadServiceRecovery(e);
1929 }
1930 }
1931
1932 /**
Daniel Tomas90245642010-11-17 10:07:52 +01001933 * @hide
1934 */
Martijn Coenen188cddb2012-01-31 22:16:15 -08001935 public void setP2pModes(int initiatorModes, int targetModes) {
1936 try {
1937 sService.setP2pModes(initiatorModes, targetModes);
1938 } catch (RemoteException e) {
1939 attemptDeadServiceRecovery(e);
1940 }
1941 }
1942
1943 /**
Andres Morales11d2e532014-07-23 12:39:55 -07001944 * Registers a new NFC unlock handler with the NFC service.
1945 *
1946 * <p />NFC unlock handlers are intended to unlock the keyguard in the presence of a trusted
1947 * NFC device. The handler should return true if it successfully authenticates the user and
1948 * unlocks the keyguard.
1949 *
1950 * <p /> The parameter {@code tagTechnologies} determines which Tag technologies will be polled for
1951 * at the lockscreen. Polling for less tag technologies reduces latency, and so it is
1952 * strongly recommended to only provide the Tag technologies that the handler is expected to
Andres Moralesf9a97942014-08-14 15:43:30 -07001953 * receive. There must be at least one tag technology provided, otherwise the unlock handler
1954 * is ignored.
Andres Morales11d2e532014-07-23 12:39:55 -07001955 *
1956 * @hide
1957 */
1958 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001959 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Andres Morales11d2e532014-07-23 12:39:55 -07001960 public boolean addNfcUnlockHandler(final NfcUnlockHandler unlockHandler,
1961 String[] tagTechnologies) {
Steve Elliott04141ea2016-04-15 15:09:43 -04001962 synchronized (NfcAdapter.class) {
1963 if (!sHasNfcFeature) {
1964 throw new UnsupportedOperationException();
1965 }
1966 }
Andres Moralesf9a97942014-08-14 15:43:30 -07001967 // If there are no tag technologies, don't bother adding unlock handler
1968 if (tagTechnologies.length == 0) {
1969 return false;
1970 }
Andres Morales11d2e532014-07-23 12:39:55 -07001971
Andres Moralesf9a97942014-08-14 15:43:30 -07001972 try {
Andres Morales11d2e532014-07-23 12:39:55 -07001973 synchronized (mLock) {
1974 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
Andres Moralesf9a97942014-08-14 15:43:30 -07001975 // update the tag technologies
1976 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.get(unlockHandler));
1977 mNfcUnlockHandlers.remove(unlockHandler);
Andres Morales11d2e532014-07-23 12:39:55 -07001978 }
Andres Moralesf9a97942014-08-14 15:43:30 -07001979
1980 INfcUnlockHandler.Stub iHandler = new INfcUnlockHandler.Stub() {
1981 @Override
1982 public boolean onUnlockAttempted(Tag tag) throws RemoteException {
1983 return unlockHandler.onUnlockAttempted(tag);
1984 }
1985 };
1986
1987 sService.addNfcUnlockHandler(iHandler,
1988 Tag.getTechCodesFromStrings(tagTechnologies));
1989 mNfcUnlockHandlers.put(unlockHandler, iHandler);
Andres Morales11d2e532014-07-23 12:39:55 -07001990 }
1991 } catch (RemoteException e) {
1992 attemptDeadServiceRecovery(e);
1993 return false;
1994 } catch (IllegalArgumentException e) {
1995 Log.e(TAG, "Unable to register LockscreenDispatch", e);
1996 return false;
1997 }
1998
1999 return true;
2000 }
2001
2002 /**
2003 * Removes a previously registered unlock handler. Also removes the tag technologies
2004 * associated with the removed unlock handler.
2005 *
2006 * @hide
2007 */
2008 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002009 @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
Andres Morales11d2e532014-07-23 12:39:55 -07002010 public boolean removeNfcUnlockHandler(NfcUnlockHandler unlockHandler) {
Steve Elliott04141ea2016-04-15 15:09:43 -04002011 synchronized (NfcAdapter.class) {
2012 if (!sHasNfcFeature) {
2013 throw new UnsupportedOperationException();
2014 }
2015 }
Andres Morales11d2e532014-07-23 12:39:55 -07002016 try {
2017 synchronized (mLock) {
2018 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
Andres Moralesf9a97942014-08-14 15:43:30 -07002019 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.remove(unlockHandler));
Andres Morales11d2e532014-07-23 12:39:55 -07002020 }
2021
2022 return true;
2023 }
2024 } catch (RemoteException e) {
2025 attemptDeadServiceRecovery(e);
2026 return false;
2027 }
2028 }
2029
2030 /**
Martijn Coenen188cddb2012-01-31 22:16:15 -08002031 * @hide
2032 */
Mathew Inwood1961e1e2018-07-31 16:04:15 +01002033 @UnsupportedAppUsage
Nick Pelly367f41f2011-03-08 11:43:30 -08002034 public INfcAdapterExtras getNfcAdapterExtrasInterface() {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06002035 if (mContext == null) {
2036 throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
2037 + " NFC extras APIs");
2038 }
Daniel Tomas90245642010-11-17 10:07:52 +01002039 try {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06002040 return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
Daniel Tomas90245642010-11-17 10:07:52 +01002041 } catch (RemoteException e) {
Nick Pelly367f41f2011-03-08 11:43:30 -08002042 attemptDeadServiceRecovery(e);
Daniel Tomas90245642010-11-17 10:07:52 +01002043 return null;
2044 }
2045 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07002046
2047 void enforceResumed(Activity activity) {
2048 if (!activity.isResumed()) {
2049 throw new IllegalStateException("API cannot be called while activity is paused");
2050 }
2051 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07002052
2053 int getSdkVersion() {
2054 if (mContext == null) {
2055 return android.os.Build.VERSION_CODES.GINGERBREAD; // best guess
2056 } else {
2057 return mContext.getApplicationInfo().targetSdkVersion;
2058 }
2059 }
Nick Pelly590b73b2010-10-12 13:00:50 -07002060}