| 1 | /* |
| 2 | * 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. |
| 15 | */ |
| 16 | |
| 17 | package android.nfc; |
| 18 | |
| 19 | import android.annotation.CallbackExecutor; |
| 20 | import android.annotation.IntDef; |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
| 23 | import android.annotation.RequiresPermission; |
| 24 | import android.annotation.SdkConstant; |
| 25 | import android.annotation.SdkConstant.SdkConstantType; |
| 26 | import android.annotation.SystemApi; |
| 27 | import android.annotation.UserIdInt; |
| 28 | import android.app.Activity; |
| 29 | import android.app.ActivityThread; |
| 30 | import android.app.OnActivityPausedListener; |
| 31 | import android.app.PendingIntent; |
| 32 | import android.compat.annotation.UnsupportedAppUsage; |
| 33 | import android.content.Context; |
| 34 | import android.content.IntentFilter; |
| 35 | import android.content.pm.PackageManager; |
| 36 | import android.net.Uri; |
| 37 | import android.nfc.tech.MifareClassic; |
| 38 | import android.nfc.tech.Ndef; |
| 39 | import android.nfc.tech.NfcA; |
| 40 | import android.nfc.tech.NfcF; |
| 41 | import android.os.Build; |
| 42 | import android.os.Bundle; |
| 43 | import android.os.Handler; |
| 44 | import android.os.IBinder; |
| 45 | import android.os.RemoteException; |
| 46 | import android.util.Log; |
| 47 | |
| 48 | import java.io.IOException; |
| 49 | import java.lang.annotation.Retention; |
| 50 | import java.lang.annotation.RetentionPolicy; |
| 51 | import java.util.ArrayList; |
| 52 | import java.util.Collections; |
| 53 | import java.util.HashMap; |
| 54 | import java.util.List; |
| 55 | import java.util.Map; |
| 56 | import java.util.Objects; |
| 57 | import java.util.concurrent.Executor; |
| 58 | |
| 59 | /** |
| 60 | * Represents the local NFC adapter. |
| 61 | * <p> |
| 62 | * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC |
| 63 | * adapter for this Android device. |
| 64 | * |
| 65 | * <div class="special reference"> |
| 66 | * <h3>Developer Guides</h3> |
| 67 | * <p>For more information about using NFC, read the |
| 68 | * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p> |
| 69 | * <p>To perform basic file sharing between devices, read |
| 70 | * <a href="{@docRoot}training/beam-files/index.html">Sharing Files with NFC</a>. |
| 71 | * </div> |
| 72 | */ |
| 73 | public final class NfcAdapter { |
| 74 | static final String TAG = "NFC"; |
| 75 | |
| 76 | private final NfcControllerAlwaysOnListener mControllerAlwaysOnListener; |
| 77 | |
| 78 | /** |
| 79 | * Intent to start an activity when a tag with NDEF payload is discovered. |
| 80 | * |
| 81 | * <p>The system inspects the first {@link NdefRecord} in the first {@link NdefMessage} and |
| 82 | * looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the |
| 83 | * intent will contain the URI in its data field. If a MIME record is found the intent will |
| 84 | * contain the MIME type in its type field. This allows activities to register |
| 85 | * {@link IntentFilter}s targeting specific content on tags. Activities should register the |
| 86 | * most specific intent filters possible to avoid the activity chooser dialog, which can |
| 87 | * disrupt the interaction with the tag as the user interacts with the screen. |
| 88 | * |
| 89 | * <p>If the tag has an NDEF payload this intent is started before |
| 90 | * {@link #ACTION_TECH_DISCOVERED}. If any activities respond to this intent neither |
| 91 | * {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started. |
| 92 | * |
| 93 | * <p>The MIME type or data URI of this intent are normalized before dispatch - |
| 94 | * so that MIME, URI scheme and URI host are always lower-case. |
| 95 | */ |
| 96 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 97 | public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED"; |
| 98 | |
| 99 | /** |
| 100 | * Intent to start an activity when a tag is discovered and activities are registered for the |
| 101 | * specific technologies on the tag. |
| 102 | * |
| 103 | * <p>To receive this intent an activity must include an intent filter |
| 104 | * for this action and specify the desired tech types in a |
| 105 | * manifest <code>meta-data</code> entry. Here is an example manfiest entry: |
| 106 | * <pre> |
| 107 | * <activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"> |
| 108 | * <!-- Add a technology filter --> |
| 109 | * <intent-filter> |
| 110 | * <action android:name="android.nfc.action.TECH_DISCOVERED" /> |
| 111 | * </intent-filter> |
| 112 | * |
| 113 | * <meta-data android:name="android.nfc.action.TECH_DISCOVERED" |
| 114 | * android:resource="@xml/filter_nfc" |
| 115 | * /> |
| 116 | * </activity></pre> |
| 117 | * |
| 118 | * <p>The meta-data XML file should contain one or more <code>tech-list</code> entries |
| 119 | * each consisting or one or more <code>tech</code> entries. The <code>tech</code> entries refer |
| 120 | * to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA". |
| 121 | * |
| 122 | * <p>A tag matches if any of the |
| 123 | * <code>tech-list</code> sets is a subset of {@link Tag#getTechList() Tag.getTechList()}. Each |
| 124 | * of the <code>tech-list</code>s is considered independently and the |
| 125 | * activity is considered a match is any single <code>tech-list</code> matches the tag that was |
| 126 | * discovered. This provides AND and OR semantics for filtering desired techs. Here is an |
| 127 | * example that will match any tag using {@link NfcF} or any tag using {@link NfcA}, |
| 128 | * {@link MifareClassic}, and {@link Ndef}: |
| 129 | * |
| 130 | * <pre> |
| 131 | * <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> |
| 132 | * <!-- capture anything using NfcF --> |
| 133 | * <tech-list> |
| 134 | * <tech>android.nfc.tech.NfcF</tech> |
| 135 | * </tech-list> |
| 136 | * |
| 137 | * <!-- OR --> |
| 138 | * |
| 139 | * <!-- capture all MIFARE Classics with NDEF payloads --> |
| 140 | * <tech-list> |
| 141 | * <tech>android.nfc.tech.NfcA</tech> |
| 142 | * <tech>android.nfc.tech.MifareClassic</tech> |
| 143 | * <tech>android.nfc.tech.Ndef</tech> |
| 144 | * </tech-list> |
| 145 | * </resources></pre> |
| 146 | * |
| 147 | * <p>This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before |
| 148 | * {@link #ACTION_TAG_DISCOVERED}. If any activities respond to {@link #ACTION_NDEF_DISCOVERED} |
| 149 | * this intent will not be started. If any activities respond to this intent |
| 150 | * {@link #ACTION_TAG_DISCOVERED} will not be started. |
| 151 | */ |
| 152 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 153 | public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED"; |
| 154 | |
| 155 | /** |
| 156 | * Intent to start an activity when a tag is discovered. |
| 157 | * |
| 158 | * <p>This intent will not be started when a tag is discovered if any activities respond to |
| 159 | * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag. |
| 160 | */ |
| 161 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 162 | public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED"; |
| 163 | |
| 164 | /** |
| 165 | * Broadcast Action: Intent to notify an application that a transaction event has occurred |
| 166 | * on the Secure Element. |
| 167 | * |
| 168 | * <p>This intent will only be sent if the application has requested permission for |
| 169 | * {@link android.Manifest.permission#NFC_TRANSACTION_EVENT} and if the application has the |
| 170 | * necessary access to Secure Element which witnessed the particular event. |
| 171 | */ |
| 172 | @RequiresPermission(android.Manifest.permission.NFC_TRANSACTION_EVENT) |
| 173 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 174 | public static final String ACTION_TRANSACTION_DETECTED = |
| 175 | "android.nfc.action.TRANSACTION_DETECTED"; |
| 176 | |
| 177 | /** |
| 178 | * Broadcast Action: Intent to notify if the preferred payment service changed. |
| 179 | * |
| 180 | * <p>This intent will only be sent to the application has requested permission for |
| 181 | * {@link android.Manifest.permission#NFC_PREFERRED_PAYMENT_INFO} and if the application |
| 182 | * has the necessary access to Secure Element which witnessed the particular event. |
| 183 | */ |
| 184 | @RequiresPermission(android.Manifest.permission.NFC_PREFERRED_PAYMENT_INFO) |
| 185 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 186 | public static final String ACTION_PREFERRED_PAYMENT_CHANGED = |
| 187 | "android.nfc.action.PREFERRED_PAYMENT_CHANGED"; |
| 188 | |
| 189 | /** |
| 190 | * Broadcast to only the activity that handles ACTION_TAG_DISCOVERED |
| 191 | * @hide |
| 192 | */ |
| 193 | public static final String ACTION_TAG_LEFT_FIELD = "android.nfc.action.TAG_LOST"; |
| 194 | |
| 195 | /** |
| 196 | * Mandatory extra containing the {@link Tag} that was discovered for the |
| 197 | * {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and |
| 198 | * {@link #ACTION_TAG_DISCOVERED} intents. |
| 199 | */ |
| 200 | public static final String EXTRA_TAG = "android.nfc.extra.TAG"; |
| 201 | |
| 202 | /** |
| 203 | * Extra containing an array of {@link NdefMessage} present on the discovered tag.<p> |
| 204 | * This extra is mandatory for {@link #ACTION_NDEF_DISCOVERED} intents, |
| 205 | * and optional for {@link #ACTION_TECH_DISCOVERED}, and |
| 206 | * {@link #ACTION_TAG_DISCOVERED} intents.<p> |
| 207 | * When this extra is present there will always be at least one |
| 208 | * {@link NdefMessage} element. Most NDEF tags have only one NDEF message, |
| 209 | * but we use an array for future compatibility. |
| 210 | */ |
| 211 | public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES"; |
| 212 | |
| 213 | /** |
| 214 | * Optional extra containing a byte array containing the ID of the discovered tag for |
| 215 | * the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and |
| 216 | * {@link #ACTION_TAG_DISCOVERED} intents. |
| 217 | */ |
| 218 | public static final String EXTRA_ID = "android.nfc.extra.ID"; |
| 219 | |
| 220 | /** |
| 221 | * Broadcast Action: The state of the local NFC adapter has been |
| 222 | * changed. |
| 223 | * <p>For example, NFC has been turned on or off. |
| 224 | * <p>Always contains the extra field {@link #EXTRA_ADAPTER_STATE} |
| 225 | */ |
| 226 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 227 | public static final String ACTION_ADAPTER_STATE_CHANGED = |
| 228 | "android.nfc.action.ADAPTER_STATE_CHANGED"; |
| 229 | |
| 230 | /** |
| 231 | * Used as an int extra field in {@link #ACTION_ADAPTER_STATE_CHANGED} |
| 232 | * intents to request the current power state. Possible values are: |
| 233 | * {@link #STATE_OFF}, |
| 234 | * {@link #STATE_TURNING_ON}, |
| 235 | * {@link #STATE_ON}, |
| 236 | * {@link #STATE_TURNING_OFF}, |
| 237 | */ |
| 238 | public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE"; |
| 239 | |
| 240 | /** |
| 241 | * Mandatory byte[] extra field in {@link #ACTION_TRANSACTION_DETECTED} |
| 242 | */ |
| 243 | public static final String EXTRA_AID = "android.nfc.extra.AID"; |
| 244 | |
| 245 | /** |
| 246 | * Optional byte[] extra field in {@link #ACTION_TRANSACTION_DETECTED} |
| 247 | */ |
| 248 | public static final String EXTRA_DATA = "android.nfc.extra.DATA"; |
| 249 | |
| 250 | /** |
| 251 | * Mandatory String extra field in {@link #ACTION_TRANSACTION_DETECTED} |
| 252 | * Indicates the Secure Element on which the transaction occurred. |
| 253 | * eSE1...eSEn for Embedded Secure Elements, SIM1...SIMn for UICC, etc. |
| 254 | */ |
| 255 | public static final String EXTRA_SECURE_ELEMENT_NAME = "android.nfc.extra.SECURE_ELEMENT_NAME"; |
| 256 | |
| 257 | /** |
| 258 | * Mandatory String extra field in {@link #ACTION_PREFERRED_PAYMENT_CHANGED} |
| 259 | * Indicates the condition when trigger this event. Possible values are: |
| 260 | * {@link #PREFERRED_PAYMENT_LOADED}, |
| 261 | * {@link #PREFERRED_PAYMENT_CHANGED}, |
| 262 | * {@link #PREFERRED_PAYMENT_UPDATED}, |
| 263 | */ |
| 264 | public static final String EXTRA_PREFERRED_PAYMENT_CHANGED_REASON = |
| 265 | "android.nfc.extra.PREFERRED_PAYMENT_CHANGED_REASON"; |
| 266 | /** |
| 267 | * Nfc is enabled and the preferred payment aids are registered. |
| 268 | */ |
| 269 | public static final int PREFERRED_PAYMENT_LOADED = 1; |
| 270 | /** |
| 271 | * User selected another payment application as the preferred payment. |
| 272 | */ |
| 273 | public static final int PREFERRED_PAYMENT_CHANGED = 2; |
| 274 | /** |
| 275 | * Current preferred payment has issued an update (registered/unregistered new aids or has been |
| 276 | * updated itself). |
| 277 | */ |
| 278 | public static final int PREFERRED_PAYMENT_UPDATED = 3; |
| 279 | |
| 280 | public static final int STATE_OFF = 1; |
| 281 | public static final int STATE_TURNING_ON = 2; |
| 282 | public static final int STATE_ON = 3; |
| 283 | public static final int STATE_TURNING_OFF = 4; |
| 284 | |
| 285 | /** |
| 286 | * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. |
| 287 | * <p> |
| 288 | * Setting this flag enables polling for Nfc-A technology. |
| 289 | */ |
| 290 | public static final int FLAG_READER_NFC_A = 0x1; |
| 291 | |
| 292 | /** |
| 293 | * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. |
| 294 | * <p> |
| 295 | * Setting this flag enables polling for Nfc-B technology. |
| 296 | */ |
| 297 | public static final int FLAG_READER_NFC_B = 0x2; |
| 298 | |
| 299 | /** |
| 300 | * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. |
| 301 | * <p> |
| 302 | * Setting this flag enables polling for Nfc-F technology. |
| 303 | */ |
| 304 | public static final int FLAG_READER_NFC_F = 0x4; |
| 305 | |
| 306 | /** |
| 307 | * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. |
| 308 | * <p> |
| 309 | * Setting this flag enables polling for Nfc-V (ISO15693) technology. |
| 310 | */ |
| 311 | public static final int FLAG_READER_NFC_V = 0x8; |
| 312 | |
| 313 | /** |
| 314 | * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. |
| 315 | * <p> |
| 316 | * Setting this flag enables polling for NfcBarcode technology. |
| 317 | */ |
| 318 | public static final int FLAG_READER_NFC_BARCODE = 0x10; |
| 319 | |
| 320 | /** |
| 321 | * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. |
| 322 | * <p> |
| 323 | * Setting this flag allows the caller to prevent the |
| 324 | * platform from performing an NDEF check on the tags it |
| 325 | * finds. |
| 326 | */ |
| 327 | public static final int FLAG_READER_SKIP_NDEF_CHECK = 0x80; |
| 328 | |
| 329 | /** |
| 330 | * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. |
| 331 | * <p> |
| 332 | * Setting this flag allows the caller to prevent the |
| 333 | * platform from playing sounds when it discovers a tag. |
| 334 | */ |
| 335 | public static final int FLAG_READER_NO_PLATFORM_SOUNDS = 0x100; |
| 336 | |
| 337 | /** |
| 338 | * Int Extra for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. |
| 339 | * <p> |
| 340 | * Setting this integer extra allows the calling application to specify |
| 341 | * the delay that the platform will use for performing presence checks |
| 342 | * on any discovered tag. |
| 343 | */ |
| 344 | public static final String EXTRA_READER_PRESENCE_CHECK_DELAY = "presence"; |
| 345 | |
| 346 | /** |
| 347 | * @hide |
| 348 | * @removed |
| 349 | */ |
| 350 | @SystemApi |
| 351 | @UnsupportedAppUsage |
| 352 | public static final int FLAG_NDEF_PUSH_NO_CONFIRM = 0x1; |
| 353 | |
| 354 | /** @hide */ |
| 355 | public static final String ACTION_HANDOVER_TRANSFER_STARTED = |
| 356 | "android.nfc.action.HANDOVER_TRANSFER_STARTED"; |
| 357 | |
| 358 | /** @hide */ |
| 359 | public static final String ACTION_HANDOVER_TRANSFER_DONE = |
| 360 | "android.nfc.action.HANDOVER_TRANSFER_DONE"; |
| 361 | |
| 362 | /** @hide */ |
| 363 | public static final String EXTRA_HANDOVER_TRANSFER_STATUS = |
| 364 | "android.nfc.extra.HANDOVER_TRANSFER_STATUS"; |
| 365 | |
| 366 | /** @hide */ |
| 367 | public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0; |
| 368 | /** @hide */ |
| 369 | public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1; |
| 370 | |
| 371 | /** @hide */ |
| 372 | public static final String EXTRA_HANDOVER_TRANSFER_URI = |
| 373 | "android.nfc.extra.HANDOVER_TRANSFER_URI"; |
| 374 | |
| 375 | /** |
| 376 | * Broadcast Action: Notify possible NFC transaction blocked because device is locked. |
| 377 | * <p>An external NFC field detected when device locked and SecureNfc enabled. |
| 378 | * @hide |
| 379 | */ |
| 380 | public static final String ACTION_REQUIRE_UNLOCK_FOR_NFC = |
| 381 | "android.nfc.action.REQUIRE_UNLOCK_FOR_NFC"; |
| 382 | |
| 383 | /** |
| 384 | * The requested app is correctly added to the Tag intent app preference. |
| 385 | * |
| 386 | * @see #setTagIntentAppPreferenceForUser(int userId, String pkg, boolean allow) |
| 387 | * @hide |
| 388 | */ |
| 389 | @SystemApi |
| 390 | public static final int TAG_INTENT_APP_PREF_RESULT_SUCCESS = 0; |
| 391 | |
| 392 | /** |
| 393 | * The requested app is not installed on the device. |
| 394 | * |
| 395 | * @see #setTagIntentAppPreferenceForUser(int userId, String pkg, boolean allow) |
| 396 | * @hide |
| 397 | */ |
| 398 | @SystemApi |
| 399 | public static final int TAG_INTENT_APP_PREF_RESULT_PACKAGE_NOT_FOUND = -1; |
| 400 | |
| 401 | /** |
| 402 | * The NfcService is not available. |
| 403 | * |
| 404 | * @see #setTagIntentAppPreferenceForUser(int userId, String pkg, boolean allow) |
| 405 | * @hide |
| 406 | */ |
| 407 | @SystemApi |
| 408 | public static final int TAG_INTENT_APP_PREF_RESULT_UNAVAILABLE = -2; |
| 409 | |
| 410 | /** |
| 411 | * Possible response codes from {@link #setTagIntentAppPreferenceForUser}. |
| 412 | * |
| 413 | * @hide |
| 414 | */ |
| 415 | @IntDef(prefix = { "TAG_INTENT_APP_PREF_RESULT" }, value = { |
| 416 | TAG_INTENT_APP_PREF_RESULT_SUCCESS, |
| 417 | TAG_INTENT_APP_PREF_RESULT_PACKAGE_NOT_FOUND, |
| 418 | TAG_INTENT_APP_PREF_RESULT_UNAVAILABLE}) |
| 419 | @Retention(RetentionPolicy.SOURCE) |
| 420 | public @interface TagIntentAppPreferenceResult {} |
| 421 | |
| 422 | // Guarded by NfcAdapter.class |
| 423 | static boolean sIsInitialized = false; |
| 424 | static boolean sHasNfcFeature; |
| 425 | static boolean sHasCeFeature; |
| 426 | |
| 427 | // Final after first constructor, except for |
| 428 | // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort |
| 429 | // recovery |
| 430 | @UnsupportedAppUsage |
| 431 | static INfcAdapter sService; |
| 432 | static NfcServiceManager.ServiceRegisterer sServiceRegisterer; |
| 433 | static INfcTag sTagService; |
| 434 | static INfcCardEmulation sCardEmulationService; |
| 435 | static INfcFCardEmulation sNfcFCardEmulationService; |
| 436 | |
| 437 | /** |
| 438 | * The NfcAdapter object for each application context. |
| 439 | * There is a 1-1 relationship between application context and |
| 440 | * NfcAdapter object. |
| 441 | */ |
| 442 | static HashMap<Context, NfcAdapter> sNfcAdapters = new HashMap(); //guard by NfcAdapter.class |
| 443 | |
| 444 | /** |
| 445 | * NfcAdapter used with a null context. This ctor was deprecated but we have |
| 446 | * to support it for backwards compatibility. New methods that require context |
| 447 | * might throw when called on the null-context NfcAdapter. |
| 448 | */ |
| 449 | static NfcAdapter sNullContextNfcAdapter; // protected by NfcAdapter.class |
| 450 | |
| 451 | final NfcActivityManager mNfcActivityManager; |
| 452 | final Context mContext; |
| 453 | final HashMap<NfcUnlockHandler, INfcUnlockHandler> mNfcUnlockHandlers; |
| 454 | final Object mLock; |
| 455 | |
| 456 | ITagRemovedCallback mTagRemovedListener; // protected by mLock |
| 457 | |
| 458 | /** |
| 459 | * A callback to be invoked when the system finds a tag while the foreground activity is |
| 460 | * operating in reader mode. |
| 461 | * <p>Register your {@code ReaderCallback} implementation with {@link |
| 462 | * NfcAdapter#enableReaderMode} and disable it with {@link |
| 463 | * NfcAdapter#disableReaderMode}. |
| 464 | * @see NfcAdapter#enableReaderMode |
| 465 | */ |
| 466 | public interface ReaderCallback { |
| 467 | public void onTagDiscovered(Tag tag); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * A listener to be invoked when NFC controller always on state changes. |
| 472 | * <p>Register your {@code ControllerAlwaysOnListener} implementation with {@link |
| 473 | * NfcAdapter#registerControllerAlwaysOnListener} and disable it with {@link |
| 474 | * NfcAdapter#unregisterControllerAlwaysOnListener}. |
| 475 | * @see #registerControllerAlwaysOnListener |
| 476 | * @hide |
| 477 | */ |
| 478 | @SystemApi |
| 479 | public interface ControllerAlwaysOnListener { |
| 480 | /** |
| 481 | * Called on NFC controller always on state changes |
| 482 | */ |
| 483 | void onControllerAlwaysOnChanged(boolean isEnabled); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * A callback to be invoked when the system successfully delivers your {@link NdefMessage} |
| 488 | * to another device. |
| 489 | * @see #setOnNdefPushCompleteCallback |
| 490 | * @deprecated this feature is removed. File sharing can work using other technology like |
| 491 | * Bluetooth. |
| 492 | */ |
| 493 | @java.lang.Deprecated |
| 494 | public interface OnNdefPushCompleteCallback { |
| 495 | /** |
| 496 | * Called on successful NDEF push. |
| 497 | * |
| 498 | * <p>This callback is usually made on a binder thread (not the UI thread). |
| 499 | * |
| 500 | * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set |
| 501 | * @see #setNdefPushMessageCallback |
| 502 | */ |
| 503 | public void onNdefPushComplete(NfcEvent event); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * A callback to be invoked when another NFC device capable of NDEF push (Android Beam) |
| 508 | * is within range. |
| 509 | * <p>Implement this interface and pass it to {@link |
| 510 | * NfcAdapter#setNdefPushMessageCallback setNdefPushMessageCallback()} in order to create an |
| 511 | * {@link NdefMessage} at the moment that another device is within range for NFC. Using this |
| 512 | * callback allows you to create a message with data that might vary based on the |
| 513 | * content currently visible to the user. Alternatively, you can call {@link |
| 514 | * #setNdefPushMessage setNdefPushMessage()} if the {@link NdefMessage} always contains the |
| 515 | * same data. |
| 516 | * @deprecated this feature is removed. File sharing can work using other technology like |
| 517 | * Bluetooth. |
| 518 | */ |
| 519 | @java.lang.Deprecated |
| 520 | public interface CreateNdefMessageCallback { |
| 521 | /** |
| 522 | * Called to provide a {@link NdefMessage} to push. |
| 523 | * |
| 524 | * <p>This callback is usually made on a binder thread (not the UI thread). |
| 525 | * |
| 526 | * <p>Called when this device is in range of another device |
| 527 | * that might support NDEF push. It allows the application to |
| 528 | * create the NDEF message only when it is required. |
| 529 | * |
| 530 | * <p>NDEF push cannot occur until this method returns, so do not |
| 531 | * block for too long. |
| 532 | * |
| 533 | * <p>The Android operating system will usually show a system UI |
| 534 | * on top of your activity during this time, so do not try to request |
| 535 | * input from the user to complete the callback, or provide custom NDEF |
| 536 | * push UI. The user probably will not see it. |
| 537 | * |
| 538 | * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set |
| 539 | * @return NDEF message to push, or null to not provide a message |
| 540 | */ |
| 541 | public NdefMessage createNdefMessage(NfcEvent event); |
| 542 | } |
| 543 | |
| 544 | |
| 545 | /** |
| 546 | * @deprecated this feature is removed. File sharing can work using other technology like |
| 547 | * Bluetooth. |
| 548 | */ |
| 549 | @java.lang.Deprecated |
| 550 | public interface CreateBeamUrisCallback { |
| 551 | public Uri[] createBeamUris(NfcEvent event); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * A callback that is invoked when a tag is removed from the field. |
| 556 | * @see NfcAdapter#ignore |
| 557 | */ |
| 558 | public interface OnTagRemovedListener { |
| 559 | void onTagRemoved(); |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * A callback to be invoked when an application has registered as a |
| 564 | * handler to unlock the device given an NFC tag at the lockscreen. |
| 565 | * @hide |
| 566 | */ |
| 567 | @SystemApi |
| 568 | public interface NfcUnlockHandler { |
| 569 | /** |
| 570 | * Called at the lock screen to attempt to unlock the device with the given tag. |
| 571 | * @param tag the detected tag, to be used to unlock the device |
| 572 | * @return true if the device was successfully unlocked |
| 573 | */ |
| 574 | public boolean onUnlockAttempted(Tag tag); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Return list of Secure Elements which support off host card emulation. |
| 579 | * |
| 580 | * @return List<String> containing secure elements on the device which supports |
| 581 | * off host card emulation. eSE for Embedded secure element, |
| 582 | * SIM for UICC and so on. |
| 583 | * @hide |
| 584 | */ |
| 585 | public @NonNull List<String> getSupportedOffHostSecureElements() { |
| 586 | if (mContext == null) { |
| 587 | throw new UnsupportedOperationException("You need a context on NfcAdapter to use the " |
| 588 | + " getSupportedOffHostSecureElements APIs"); |
| 589 | } |
| 590 | List<String> offHostSE = new ArrayList<String>(); |
| 591 | PackageManager pm = mContext.getPackageManager(); |
| 592 | if (pm == null) { |
| 593 | Log.e(TAG, "Cannot get package manager, assuming no off-host CE feature"); |
| 594 | return offHostSE; |
| 595 | } |
| 596 | if (pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC)) { |
| 597 | offHostSE.add("SIM"); |
| 598 | } |
| 599 | if (pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE)) { |
| 600 | offHostSE.add("eSE"); |
| 601 | } |
| 602 | return offHostSE; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Returns the NfcAdapter for application context, |
| 607 | * or throws if NFC is not available. |
| 608 | * @hide |
| 609 | */ |
| 610 | @UnsupportedAppUsage |
| 611 | public static synchronized NfcAdapter getNfcAdapter(Context context) { |
| 612 | if (context == null) { |
| 613 | if (sNullContextNfcAdapter == null) { |
| 614 | sNullContextNfcAdapter = new NfcAdapter(null); |
| 615 | } |
| 616 | return sNullContextNfcAdapter; |
| 617 | } |
| 618 | if (!sIsInitialized) { |
| 619 | PackageManager pm; |
| 620 | pm = context.getPackageManager(); |
| 621 | sHasNfcFeature = pm.hasSystemFeature(PackageManager.FEATURE_NFC); |
| 622 | sHasCeFeature = |
| 623 | pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION) |
| 624 | || pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF) |
| 625 | || pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC) |
| 626 | || pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE); |
| 627 | /* is this device meant to have NFC */ |
| 628 | if (!sHasNfcFeature && !sHasCeFeature) { |
| 629 | Log.v(TAG, "this device does not have NFC support"); |
| 630 | throw new UnsupportedOperationException(); |
| 631 | } |
| 632 | NfcServiceManager manager = NfcFrameworkInitializer.getNfcServiceManager(); |
| 633 | if (manager == null) { |
| 634 | Log.e(TAG, "NfcServiceManager is null"); |
| 635 | throw new UnsupportedOperationException(); |
| 636 | } |
| 637 | sServiceRegisterer = manager.getNfcManagerServiceRegisterer(); |
| 638 | sService = getServiceInterface(); |
| 639 | if (sService == null) { |
| 640 | Log.e(TAG, "could not retrieve NFC service"); |
| 641 | throw new UnsupportedOperationException(); |
| 642 | } |
| 643 | if (sHasNfcFeature) { |
| 644 | try { |
| 645 | sTagService = sService.getNfcTagInterface(); |
| 646 | } catch (RemoteException e) { |
| 647 | Log.e(TAG, "could not retrieve NFC Tag service"); |
| 648 | throw new UnsupportedOperationException(); |
| 649 | } |
| 650 | } |
| 651 | if (sHasCeFeature) { |
| 652 | try { |
| 653 | sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface(); |
| 654 | } catch (RemoteException e) { |
| 655 | Log.e(TAG, "could not retrieve NFC-F card emulation service"); |
| 656 | throw new UnsupportedOperationException(); |
| 657 | } |
| 658 | try { |
| 659 | sCardEmulationService = sService.getNfcCardEmulationInterface(); |
| 660 | } catch (RemoteException e) { |
| 661 | Log.e(TAG, "could not retrieve card emulation service"); |
| 662 | throw new UnsupportedOperationException(); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | sIsInitialized = true; |
| 667 | } |
| 668 | NfcAdapter adapter = sNfcAdapters.get(context); |
| 669 | if (adapter == null) { |
| 670 | adapter = new NfcAdapter(context); |
| 671 | sNfcAdapters.put(context, adapter); |
| 672 | } |
| 673 | return adapter; |
| 674 | } |
| 675 | |
| 676 | /** get handle to NFC service interface */ |
| 677 | private static INfcAdapter getServiceInterface() { |
| 678 | /* get a handle to NFC service */ |
| 679 | IBinder b = sServiceRegisterer.get(); |
| 680 | if (b == null) { |
| 681 | return null; |
| 682 | } |
| 683 | return INfcAdapter.Stub.asInterface(b); |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Helper to get the default NFC Adapter. |
| 688 | * <p> |
| 689 | * Most Android devices will only have one NFC Adapter (NFC Controller). |
| 690 | * <p> |
| 691 | * This helper is the equivalent of: |
| 692 | * <pre> |
| 693 | * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE); |
| 694 | * NfcAdapter adapter = manager.getDefaultAdapter();</pre> |
| 695 | * @param context the calling application's context |
| 696 | * |
| 697 | * @return the default NFC adapter, or null if no NFC adapter exists |
| 698 | */ |
| 699 | public static NfcAdapter getDefaultAdapter(Context context) { |
| 700 | if (context == null) { |
| 701 | throw new IllegalArgumentException("context cannot be null"); |
| 702 | } |
| 703 | context = context.getApplicationContext(); |
| 704 | if (context == null) { |
| 705 | throw new IllegalArgumentException( |
| 706 | "context not associated with any application (using a mock context?)"); |
| 707 | } |
| 708 | |
| 709 | if (sIsInitialized && sServiceRegisterer.tryGet() == null) { |
| 710 | synchronized (NfcAdapter.class) { |
| 711 | /* Stale sService pointer */ |
| 712 | if (sIsInitialized) sIsInitialized = false; |
| 713 | } |
| 714 | } |
| 715 | /* Try to initialize the service */ |
| 716 | NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE); |
| 717 | if (manager == null) { |
| 718 | // NFC not available |
| 719 | return null; |
| 720 | } |
| 721 | return manager.getDefaultAdapter(); |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * Legacy NfcAdapter getter, always use {@link #getDefaultAdapter(Context)} instead.<p> |
| 726 | * This method was deprecated at API level 10 (Gingerbread MR1) because a context is required |
| 727 | * for many NFC API methods. Those methods will fail when called on an NfcAdapter |
| 728 | * object created from this method.<p> |
| 729 | * @deprecated use {@link #getDefaultAdapter(Context)} |
| 730 | * @hide |
| 731 | */ |
| 732 | @Deprecated |
| 733 | @UnsupportedAppUsage |
| 734 | public static NfcAdapter getDefaultAdapter() { |
| 735 | // introduced in API version 9 (GB 2.3) |
| 736 | // deprecated in API version 10 (GB 2.3.3) |
| 737 | // removed from public API in version 16 (ICS MR2) |
| 738 | // should maintain as a hidden API for binary compatibility for a little longer |
| 739 | Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " + |
| 740 | "NfcAdapter.getDefaultAdapter(Context) instead", new Exception()); |
| 741 | |
| 742 | return NfcAdapter.getNfcAdapter(null); |
| 743 | } |
| 744 | |
| 745 | NfcAdapter(Context context) { |
| 746 | mContext = context; |
| 747 | mNfcActivityManager = new NfcActivityManager(this); |
| 748 | mNfcUnlockHandlers = new HashMap<NfcUnlockHandler, INfcUnlockHandler>(); |
| 749 | mTagRemovedListener = null; |
| 750 | mLock = new Object(); |
| 751 | mControllerAlwaysOnListener = new NfcControllerAlwaysOnListener(getService()); |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * @hide |
| 756 | */ |
| 757 | @UnsupportedAppUsage |
| 758 | public Context getContext() { |
| 759 | return mContext; |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * Returns the binder interface to the service. |
| 764 | * @hide |
| 765 | */ |
| 766 | @UnsupportedAppUsage |
| 767 | public INfcAdapter getService() { |
| 768 | isEnabled(); // NOP call to recover sService if it is stale |
| 769 | return sService; |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Returns the binder interface to the tag service. |
| 774 | * @hide |
| 775 | */ |
| 776 | public INfcTag getTagService() { |
| 777 | isEnabled(); // NOP call to recover sTagService if it is stale |
| 778 | return sTagService; |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * Returns the binder interface to the card emulation service. |
| 783 | * @hide |
| 784 | */ |
| 785 | public INfcCardEmulation getCardEmulationService() { |
| 786 | isEnabled(); |
| 787 | return sCardEmulationService; |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Returns the binder interface to the NFC-F card emulation service. |
| 792 | * @hide |
| 793 | */ |
| 794 | public INfcFCardEmulation getNfcFCardEmulationService() { |
| 795 | isEnabled(); |
| 796 | return sNfcFCardEmulationService; |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Returns the binder interface to the NFC-DTA test interface. |
| 801 | * @hide |
| 802 | */ |
| 803 | public INfcDta getNfcDtaInterface() { |
| 804 | if (mContext == null) { |
| 805 | throw new UnsupportedOperationException("You need a context on NfcAdapter to use the " |
| 806 | + " NFC extras APIs"); |
| 807 | } |
| 808 | try { |
| 809 | return sService.getNfcDtaInterface(mContext.getPackageName()); |
| 810 | } catch (RemoteException e) { |
| 811 | attemptDeadServiceRecovery(e); |
| 812 | // Try one more time |
| 813 | if (sService == null) { |
| 814 | Log.e(TAG, "Failed to recover NFC Service."); |
| 815 | return null; |
| 816 | } |
| 817 | try { |
| 818 | return sService.getNfcDtaInterface(mContext.getPackageName()); |
| 819 | } catch (RemoteException ee) { |
| 820 | Log.e(TAG, "Failed to recover NFC Service."); |
| 821 | } |
| 822 | return null; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * NFC service dead - attempt best effort recovery |
| 828 | * @hide |
| 829 | */ |
| 830 | @UnsupportedAppUsage |
| 831 | public void attemptDeadServiceRecovery(Exception e) { |
| 832 | Log.e(TAG, "NFC service dead - attempting to recover", e); |
| 833 | INfcAdapter service = getServiceInterface(); |
| 834 | if (service == null) { |
| 835 | Log.e(TAG, "could not retrieve NFC service during service recovery"); |
| 836 | // nothing more can be done now, sService is still stale, we'll hit |
| 837 | // this recovery path again later |
| 838 | return; |
| 839 | } |
| 840 | // assigning to sService is not thread-safe, but this is best-effort code |
| 841 | // and on a well-behaved system should never happen |
| 842 | sService = service; |
| 843 | try { |
| 844 | sTagService = service.getNfcTagInterface(); |
| 845 | } catch (RemoteException ee) { |
| 846 | Log.e(TAG, "could not retrieve NFC tag service during service recovery"); |
| 847 | // nothing more can be done now, sService is still stale, we'll hit |
| 848 | // this recovery path again later |
| 849 | return; |
| 850 | } |
| 851 | |
| 852 | try { |
| 853 | sCardEmulationService = service.getNfcCardEmulationInterface(); |
| 854 | } catch (RemoteException ee) { |
| 855 | Log.e(TAG, "could not retrieve NFC card emulation service during service recovery"); |
| 856 | } |
| 857 | |
| 858 | try { |
| 859 | sNfcFCardEmulationService = service.getNfcFCardEmulationInterface(); |
| 860 | } catch (RemoteException ee) { |
| 861 | Log.e(TAG, "could not retrieve NFC-F card emulation service during service recovery"); |
| 862 | } |
| 863 | |
| 864 | return; |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * Return true if this NFC Adapter has any features enabled. |
| 869 | * |
| 870 | * <p>If this method returns false, the NFC hardware is guaranteed not to |
| 871 | * generate or respond to any NFC communication over its NFC radio. |
| 872 | * <p>Applications can use this to check if NFC is enabled. Applications |
| 873 | * can request Settings UI allowing the user to toggle NFC using: |
| 874 | * <p><pre>startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))</pre> |
| 875 | * |
| 876 | * @see android.provider.Settings#ACTION_NFC_SETTINGS |
| 877 | * @return true if this NFC Adapter has any features enabled |
| 878 | */ |
| 879 | public boolean isEnabled() { |
| 880 | try { |
| 881 | return sService.getState() == STATE_ON; |
| 882 | } catch (RemoteException e) { |
| 883 | attemptDeadServiceRecovery(e); |
| 884 | // Try one more time |
| 885 | if (sService == null) { |
| 886 | Log.e(TAG, "Failed to recover NFC Service."); |
| 887 | return false; |
| 888 | } |
| 889 | try { |
| 890 | return sService.getState() == STATE_ON; |
| 891 | } catch (RemoteException ee) { |
| 892 | Log.e(TAG, "Failed to recover NFC Service."); |
| 893 | } |
| 894 | return false; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * Return the state of this NFC Adapter. |
| 900 | * |
| 901 | * <p>Returns one of {@link #STATE_ON}, {@link #STATE_TURNING_ON}, |
| 902 | * {@link #STATE_OFF}, {@link #STATE_TURNING_OFF}. |
| 903 | * |
| 904 | * <p>{@link #isEnabled()} is equivalent to |
| 905 | * <code>{@link #getAdapterState()} == {@link #STATE_ON}</code> |
| 906 | * |
| 907 | * @return the current state of this NFC adapter |
| 908 | * |
| 909 | * @hide |
| 910 | */ |
| 911 | @UnsupportedAppUsage |
| 912 | public int getAdapterState() { |
| 913 | try { |
| 914 | return sService.getState(); |
| 915 | } catch (RemoteException e) { |
| 916 | attemptDeadServiceRecovery(e); |
| 917 | // Try one more time |
| 918 | if (sService == null) { |
| 919 | Log.e(TAG, "Failed to recover NFC Service."); |
| 920 | return NfcAdapter.STATE_OFF; |
| 921 | } |
| 922 | try { |
| 923 | return sService.getState(); |
| 924 | } catch (RemoteException ee) { |
| 925 | Log.e(TAG, "Failed to recover NFC Service."); |
| 926 | } |
| 927 | return NfcAdapter.STATE_OFF; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * Enable NFC hardware. |
| 933 | * |
| 934 | * <p>This call is asynchronous. Listen for |
| 935 | * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the |
| 936 | * operation is complete. |
| 937 | * |
| 938 | * <p>If this returns true, then either NFC is already on, or |
| 939 | * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent |
| 940 | * to indicate a state transition. If this returns false, then |
| 941 | * there is some problem that prevents an attempt to turn |
| 942 | * NFC on (for example we are in airplane mode and NFC is not |
| 943 | * toggleable in airplane mode on this platform). |
| 944 | * |
| 945 | * @hide |
| 946 | */ |
| 947 | @SystemApi |
| 948 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 949 | public boolean enable() { |
| 950 | try { |
| 951 | return sService.enable(); |
| 952 | } catch (RemoteException e) { |
| 953 | attemptDeadServiceRecovery(e); |
| 954 | // Try one more time |
| 955 | if (sService == null) { |
| 956 | Log.e(TAG, "Failed to recover NFC Service."); |
| 957 | return false; |
| 958 | } |
| 959 | try { |
| 960 | return sService.enable(); |
| 961 | } catch (RemoteException ee) { |
| 962 | Log.e(TAG, "Failed to recover NFC Service."); |
| 963 | } |
| 964 | return false; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * Disable NFC hardware. |
| 970 | * |
| 971 | * <p>No NFC features will work after this call, and the hardware |
| 972 | * will not perform or respond to any NFC communication. |
| 973 | * |
| 974 | * <p>This call is asynchronous. Listen for |
| 975 | * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the |
| 976 | * operation is complete. |
| 977 | * |
| 978 | * <p>If this returns true, then either NFC is already off, or |
| 979 | * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent |
| 980 | * to indicate a state transition. If this returns false, then |
| 981 | * there is some problem that prevents an attempt to turn |
| 982 | * NFC off. |
| 983 | * |
| 984 | * @hide |
| 985 | */ |
| 986 | @SystemApi |
| 987 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 988 | public boolean disable() { |
| 989 | try { |
| 990 | return sService.disable(true); |
| 991 | } catch (RemoteException e) { |
| 992 | attemptDeadServiceRecovery(e); |
| 993 | // Try one more time |
| 994 | if (sService == null) { |
| 995 | Log.e(TAG, "Failed to recover NFC Service."); |
| 996 | return false; |
| 997 | } |
| 998 | try { |
| 999 | return sService.disable(true); |
| 1000 | } catch (RemoteException ee) { |
| 1001 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1002 | } |
| 1003 | return false; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Disable NFC hardware. |
| 1009 | * @hide |
| 1010 | */ |
| 1011 | @SystemApi |
| 1012 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1013 | public boolean disable(boolean persist) { |
| 1014 | try { |
| 1015 | return sService.disable(persist); |
| 1016 | } catch (RemoteException e) { |
| 1017 | attemptDeadServiceRecovery(e); |
| 1018 | // Try one more time |
| 1019 | if (sService == null) { |
| 1020 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1021 | return false; |
| 1022 | } |
| 1023 | try { |
| 1024 | return sService.disable(persist); |
| 1025 | } catch (RemoteException ee) { |
| 1026 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1027 | } |
| 1028 | return false; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * Pauses polling for a {@code timeoutInMs} millis. If polling must be resumed before timeout, |
| 1034 | * use {@link #resumePolling()}. |
| 1035 | * @hide |
| 1036 | */ |
| 1037 | public void pausePolling(int timeoutInMs) { |
| 1038 | try { |
| 1039 | sService.pausePolling(timeoutInMs); |
| 1040 | } catch (RemoteException e) { |
| 1041 | attemptDeadServiceRecovery(e); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * Resumes default polling for the current device state if polling is paused. Calling |
| 1047 | * this while polling is not paused is a no-op. |
| 1048 | * |
| 1049 | * @hide |
| 1050 | */ |
| 1051 | public void resumePolling() { |
| 1052 | try { |
| 1053 | sService.resumePolling(); |
| 1054 | } catch (RemoteException e) { |
| 1055 | attemptDeadServiceRecovery(e); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * Set one or more {@link Uri}s to send using Android Beam (TM). Every |
| 1061 | * Uri you provide must have either scheme 'file' or scheme 'content'. |
| 1062 | * |
| 1063 | * <p>For the data provided through this method, Android Beam tries to |
| 1064 | * switch to alternate transports such as Bluetooth to achieve a fast |
| 1065 | * transfer speed. Hence this method is very suitable |
| 1066 | * for transferring large files such as pictures or songs. |
| 1067 | * |
| 1068 | * <p>The receiving side will store the content of each Uri in |
| 1069 | * a file and present a notification to the user to open the file |
| 1070 | * with a {@link android.content.Intent} with action |
| 1071 | * {@link android.content.Intent#ACTION_VIEW}. |
| 1072 | * If multiple URIs are sent, the {@link android.content.Intent} will refer |
| 1073 | * to the first of the stored files. |
| 1074 | * |
| 1075 | * <p>This method may be called at any time before {@link Activity#onDestroy}, |
| 1076 | * but the URI(s) are only made available for Android Beam when the |
| 1077 | * specified activity(s) are in resumed (foreground) state. The recommended |
| 1078 | * approach is to call this method during your Activity's |
| 1079 | * {@link Activity#onCreate} - see sample |
| 1080 | * code below. This method does not immediately perform any I/O or blocking work, |
| 1081 | * so is safe to call on your main thread. |
| 1082 | * |
| 1083 | * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback} |
| 1084 | * have priority over both {@link #setNdefPushMessage} and |
| 1085 | * {@link #setNdefPushMessageCallback}. |
| 1086 | * |
| 1087 | * <p>If {@link #setBeamPushUris} is called with a null Uri array, |
| 1088 | * and/or {@link #setBeamPushUrisCallback} is called with a null callback, |
| 1089 | * then the Uri push will be completely disabled for the specified activity(s). |
| 1090 | * |
| 1091 | * <p>Code example: |
| 1092 | * <pre> |
| 1093 | * protected void onCreate(Bundle savedInstanceState) { |
| 1094 | * super.onCreate(savedInstanceState); |
| 1095 | * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); |
| 1096 | * if (nfcAdapter == null) return; // NFC not available on this device |
| 1097 | * nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this); |
| 1098 | * }</pre> |
| 1099 | * And that is it. Only one call per activity is necessary. The Android |
| 1100 | * OS will automatically release its references to the Uri(s) and the |
| 1101 | * Activity object when it is destroyed if you follow this pattern. |
| 1102 | * |
| 1103 | * <p>If your Activity wants to dynamically supply Uri(s), |
| 1104 | * then set a callback using {@link #setBeamPushUrisCallback} instead |
| 1105 | * of using this method. |
| 1106 | * |
| 1107 | * <p class="note">Do not pass in an Activity that has already been through |
| 1108 | * {@link Activity#onDestroy}. This is guaranteed if you call this API |
| 1109 | * during {@link Activity#onCreate}. |
| 1110 | * |
| 1111 | * <p class="note">If this device does not support alternate transports |
| 1112 | * such as Bluetooth or WiFI, calling this method does nothing. |
| 1113 | * |
| 1114 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 1115 | * |
| 1116 | * @param uris an array of Uri(s) to push over Android Beam |
| 1117 | * @param activity activity for which the Uri(s) will be pushed |
| 1118 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1119 | * @removed this feature is removed. File sharing can work using other technology like |
| 1120 | * Bluetooth. |
| 1121 | */ |
| 1122 | @java.lang.Deprecated |
| 1123 | @UnsupportedAppUsage |
| 1124 | public void setBeamPushUris(Uri[] uris, Activity activity) { |
| 1125 | synchronized (NfcAdapter.class) { |
| 1126 | if (!sHasNfcFeature) { |
| 1127 | throw new UnsupportedOperationException(); |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * Set a callback that will dynamically generate one or more {@link Uri}s |
| 1134 | * to send using Android Beam (TM). Every Uri the callback provides |
| 1135 | * must have either scheme 'file' or scheme 'content'. |
| 1136 | * |
| 1137 | * <p>For the data provided through this callback, Android Beam tries to |
| 1138 | * switch to alternate transports such as Bluetooth to achieve a fast |
| 1139 | * transfer speed. Hence this method is very suitable |
| 1140 | * for transferring large files such as pictures or songs. |
| 1141 | * |
| 1142 | * <p>The receiving side will store the content of each Uri in |
| 1143 | * a file and present a notification to the user to open the file |
| 1144 | * with a {@link android.content.Intent} with action |
| 1145 | * {@link android.content.Intent#ACTION_VIEW}. |
| 1146 | * If multiple URIs are sent, the {@link android.content.Intent} will refer |
| 1147 | * to the first of the stored files. |
| 1148 | * |
| 1149 | * <p>This method may be called at any time before {@link Activity#onDestroy}, |
| 1150 | * but the URI(s) are only made available for Android Beam when the |
| 1151 | * specified activity(s) are in resumed (foreground) state. The recommended |
| 1152 | * approach is to call this method during your Activity's |
| 1153 | * {@link Activity#onCreate} - see sample |
| 1154 | * code below. This method does not immediately perform any I/O or blocking work, |
| 1155 | * so is safe to call on your main thread. |
| 1156 | * |
| 1157 | * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback} |
| 1158 | * have priority over both {@link #setNdefPushMessage} and |
| 1159 | * {@link #setNdefPushMessageCallback}. |
| 1160 | * |
| 1161 | * <p>If {@link #setBeamPushUris} is called with a null Uri array, |
| 1162 | * and/or {@link #setBeamPushUrisCallback} is called with a null callback, |
| 1163 | * then the Uri push will be completely disabled for the specified activity(s). |
| 1164 | * |
| 1165 | * <p>Code example: |
| 1166 | * <pre> |
| 1167 | * protected void onCreate(Bundle savedInstanceState) { |
| 1168 | * super.onCreate(savedInstanceState); |
| 1169 | * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); |
| 1170 | * if (nfcAdapter == null) return; // NFC not available on this device |
| 1171 | * nfcAdapter.setBeamPushUrisCallback(callback, this); |
| 1172 | * }</pre> |
| 1173 | * And that is it. Only one call per activity is necessary. The Android |
| 1174 | * OS will automatically release its references to the Uri(s) and the |
| 1175 | * Activity object when it is destroyed if you follow this pattern. |
| 1176 | * |
| 1177 | * <p class="note">Do not pass in an Activity that has already been through |
| 1178 | * {@link Activity#onDestroy}. This is guaranteed if you call this API |
| 1179 | * during {@link Activity#onCreate}. |
| 1180 | * |
| 1181 | * <p class="note">If this device does not support alternate transports |
| 1182 | * such as Bluetooth or WiFI, calling this method does nothing. |
| 1183 | * |
| 1184 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 1185 | * |
| 1186 | * @param callback callback, or null to disable |
| 1187 | * @param activity activity for which the Uri(s) will be pushed |
| 1188 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1189 | * @removed this feature is removed. File sharing can work using other technology like |
| 1190 | * Bluetooth. |
| 1191 | */ |
| 1192 | @java.lang.Deprecated |
| 1193 | @UnsupportedAppUsage |
| 1194 | public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) { |
| 1195 | synchronized (NfcAdapter.class) { |
| 1196 | if (!sHasNfcFeature) { |
| 1197 | throw new UnsupportedOperationException(); |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | /** |
| 1203 | * Set a static {@link NdefMessage} to send using Android Beam (TM). |
| 1204 | * |
| 1205 | * <p>This method may be called at any time before {@link Activity#onDestroy}, |
| 1206 | * but the NDEF message is only made available for NDEF push when the |
| 1207 | * specified activity(s) are in resumed (foreground) state. The recommended |
| 1208 | * approach is to call this method during your Activity's |
| 1209 | * {@link Activity#onCreate} - see sample |
| 1210 | * code below. This method does not immediately perform any I/O or blocking work, |
| 1211 | * so is safe to call on your main thread. |
| 1212 | * |
| 1213 | * <p>Only one NDEF message can be pushed by the currently resumed activity. |
| 1214 | * If both {@link #setNdefPushMessage} and |
| 1215 | * {@link #setNdefPushMessageCallback} are set, then |
| 1216 | * the callback will take priority. |
| 1217 | * |
| 1218 | * <p>If neither {@link #setNdefPushMessage} or |
| 1219 | * {@link #setNdefPushMessageCallback} have been called for your activity, then |
| 1220 | * the Android OS may choose to send a default NDEF message on your behalf, |
| 1221 | * such as a URI for your application. |
| 1222 | * |
| 1223 | * <p>If {@link #setNdefPushMessage} is called with a null NDEF message, |
| 1224 | * and/or {@link #setNdefPushMessageCallback} is called with a null callback, |
| 1225 | * then NDEF push will be completely disabled for the specified activity(s). |
| 1226 | * This also disables any default NDEF message the Android OS would have |
| 1227 | * otherwise sent on your behalf for those activity(s). |
| 1228 | * |
| 1229 | * <p>If you want to prevent the Android OS from sending default NDEF |
| 1230 | * messages completely (for all activities), you can include a |
| 1231 | * {@code <meta-data>} element inside the {@code <application>} |
| 1232 | * element of your AndroidManifest.xml file, like this: |
| 1233 | * <pre> |
| 1234 | * <application ...> |
| 1235 | * <meta-data android:name="android.nfc.disable_beam_default" |
| 1236 | * android:value="true" /> |
| 1237 | * </application></pre> |
| 1238 | * |
| 1239 | * <p>The API allows for multiple activities to be specified at a time, |
| 1240 | * but it is strongly recommended to just register one at a time, |
| 1241 | * and to do so during the activity's {@link Activity#onCreate}. For example: |
| 1242 | * <pre> |
| 1243 | * protected void onCreate(Bundle savedInstanceState) { |
| 1244 | * super.onCreate(savedInstanceState); |
| 1245 | * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); |
| 1246 | * if (nfcAdapter == null) return; // NFC not available on this device |
| 1247 | * nfcAdapter.setNdefPushMessage(ndefMessage, this); |
| 1248 | * }</pre> |
| 1249 | * And that is it. Only one call per activity is necessary. The Android |
| 1250 | * OS will automatically release its references to the NDEF message and the |
| 1251 | * Activity object when it is destroyed if you follow this pattern. |
| 1252 | * |
| 1253 | * <p>If your Activity wants to dynamically generate an NDEF message, |
| 1254 | * then set a callback using {@link #setNdefPushMessageCallback} instead |
| 1255 | * of a static message. |
| 1256 | * |
| 1257 | * <p class="note">Do not pass in an Activity that has already been through |
| 1258 | * {@link Activity#onDestroy}. This is guaranteed if you call this API |
| 1259 | * during {@link Activity#onCreate}. |
| 1260 | * |
| 1261 | * <p class="note">For sending large content such as pictures and songs, |
| 1262 | * consider using {@link #setBeamPushUris}, which switches to alternate transports |
| 1263 | * such as Bluetooth to achieve a fast transfer rate. |
| 1264 | * |
| 1265 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 1266 | * |
| 1267 | * @param message NDEF message to push over NFC, or null to disable |
| 1268 | * @param activity activity for which the NDEF message will be pushed |
| 1269 | * @param activities optional additional activities, however we strongly recommend |
| 1270 | * to only register one at a time, and to do so in that activity's |
| 1271 | * {@link Activity#onCreate} |
| 1272 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1273 | * @removed this feature is removed. File sharing can work using other technology like |
| 1274 | * Bluetooth. |
| 1275 | */ |
| 1276 | @java.lang.Deprecated |
| 1277 | @UnsupportedAppUsage |
| 1278 | public void setNdefPushMessage(NdefMessage message, Activity activity, |
| 1279 | Activity ... activities) { |
| 1280 | synchronized (NfcAdapter.class) { |
| 1281 | if (!sHasNfcFeature) { |
| 1282 | throw new UnsupportedOperationException(); |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * @hide |
| 1289 | * @removed |
| 1290 | */ |
| 1291 | @SystemApi |
| 1292 | @UnsupportedAppUsage |
| 1293 | public void setNdefPushMessage(NdefMessage message, Activity activity, int flags) { |
| 1294 | synchronized (NfcAdapter.class) { |
| 1295 | if (!sHasNfcFeature) { |
| 1296 | throw new UnsupportedOperationException(); |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | /** |
| 1302 | * Set a callback that dynamically generates NDEF messages to send using Android Beam (TM). |
| 1303 | * |
| 1304 | * <p>This method may be called at any time before {@link Activity#onDestroy}, |
| 1305 | * but the NDEF message callback can only occur when the |
| 1306 | * specified activity(s) are in resumed (foreground) state. The recommended |
| 1307 | * approach is to call this method during your Activity's |
| 1308 | * {@link Activity#onCreate} - see sample |
| 1309 | * code below. This method does not immediately perform any I/O or blocking work, |
| 1310 | * so is safe to call on your main thread. |
| 1311 | * |
| 1312 | * <p>Only one NDEF message can be pushed by the currently resumed activity. |
| 1313 | * If both {@link #setNdefPushMessage} and |
| 1314 | * {@link #setNdefPushMessageCallback} are set, then |
| 1315 | * the callback will take priority. |
| 1316 | * |
| 1317 | * <p>If neither {@link #setNdefPushMessage} or |
| 1318 | * {@link #setNdefPushMessageCallback} have been called for your activity, then |
| 1319 | * the Android OS may choose to send a default NDEF message on your behalf, |
| 1320 | * such as a URI for your application. |
| 1321 | * |
| 1322 | * <p>If {@link #setNdefPushMessage} is called with a null NDEF message, |
| 1323 | * and/or {@link #setNdefPushMessageCallback} is called with a null callback, |
| 1324 | * then NDEF push will be completely disabled for the specified activity(s). |
| 1325 | * This also disables any default NDEF message the Android OS would have |
| 1326 | * otherwise sent on your behalf for those activity(s). |
| 1327 | * |
| 1328 | * <p>If you want to prevent the Android OS from sending default NDEF |
| 1329 | * messages completely (for all activities), you can include a |
| 1330 | * {@code <meta-data>} element inside the {@code <application>} |
| 1331 | * element of your AndroidManifest.xml file, like this: |
| 1332 | * <pre> |
| 1333 | * <application ...> |
| 1334 | * <meta-data android:name="android.nfc.disable_beam_default" |
| 1335 | * android:value="true" /> |
| 1336 | * </application></pre> |
| 1337 | * |
| 1338 | * <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.setNdefPushMessageCallback(callback, this); |
| 1347 | * }</pre> |
| 1348 | * 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}. |
| 1355 | * <p class="note">For sending large content such as pictures and songs, |
| 1356 | * consider using {@link #setBeamPushUris}, which switches to alternate transports |
| 1357 | * such as Bluetooth to achieve a fast transfer rate. |
| 1358 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 1359 | * |
| 1360 | * @param callback callback, or null to disable |
| 1361 | * @param activity activity for which the NDEF message will be pushed |
| 1362 | * @param activities optional additional activities, however we strongly recommend |
| 1363 | * to only register one at a time, and to do so in that activity's |
| 1364 | * {@link Activity#onCreate} |
| 1365 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1366 | * @removed this feature is removed. File sharing can work using other technology like |
| 1367 | * Bluetooth. |
| 1368 | */ |
| 1369 | @java.lang.Deprecated |
| 1370 | @UnsupportedAppUsage |
| 1371 | public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity, |
| 1372 | Activity ... activities) { |
| 1373 | synchronized (NfcAdapter.class) { |
| 1374 | if (!sHasNfcFeature) { |
| 1375 | throw new UnsupportedOperationException(); |
| 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /** |
| 1381 | * Set a callback on successful Android Beam (TM). |
| 1382 | * |
| 1383 | * <p>This method may be called at any time before {@link Activity#onDestroy}, |
| 1384 | * but the callback can only occur when the |
| 1385 | * specified activity(s) are in resumed (foreground) state. The recommended |
| 1386 | * approach is to call this method during your Activity's |
| 1387 | * {@link Activity#onCreate} - see sample |
| 1388 | * code below. This method does not immediately perform any I/O or blocking work, |
| 1389 | * so is safe to call on your main thread. |
| 1390 | * |
| 1391 | * <p>The API allows for multiple activities to be specified at a time, |
| 1392 | * but it is strongly recommended to just register one at a time, |
| 1393 | * and to do so during the activity's {@link Activity#onCreate}. For example: |
| 1394 | * <pre> |
| 1395 | * protected void onCreate(Bundle savedInstanceState) { |
| 1396 | * super.onCreate(savedInstanceState); |
| 1397 | * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); |
| 1398 | * if (nfcAdapter == null) return; // NFC not available on this device |
| 1399 | * nfcAdapter.setOnNdefPushCompleteCallback(callback, this); |
| 1400 | * }</pre> |
| 1401 | * And that is it. Only one call per activity is necessary. The Android |
| 1402 | * OS will automatically release its references to the callback and the |
| 1403 | * Activity object when it is destroyed if you follow this pattern. |
| 1404 | * |
| 1405 | * <p class="note">Do not pass in an Activity that has already been through |
| 1406 | * {@link Activity#onDestroy}. This is guaranteed if you call this API |
| 1407 | * during {@link Activity#onCreate}. |
| 1408 | * |
| 1409 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 1410 | * |
| 1411 | * @param callback callback, or null to disable |
| 1412 | * @param activity activity for which the NDEF message will be pushed |
| 1413 | * @param activities optional additional activities, however we strongly recommend |
| 1414 | * to only register one at a time, and to do so in that activity's |
| 1415 | * {@link Activity#onCreate} |
| 1416 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1417 | * @removed this feature is removed. File sharing can work using other technology like |
| 1418 | * Bluetooth. |
| 1419 | */ |
| 1420 | @java.lang.Deprecated |
| 1421 | @UnsupportedAppUsage |
| 1422 | public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback, |
| 1423 | Activity activity, Activity ... activities) { |
| 1424 | synchronized (NfcAdapter.class) { |
| 1425 | if (!sHasNfcFeature) { |
| 1426 | throw new UnsupportedOperationException(); |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | /** |
| 1432 | * Enable foreground dispatch to the given Activity. |
| 1433 | * |
| 1434 | * <p>This will give priority to the foreground activity when |
| 1435 | * dispatching a discovered {@link Tag} to an application. |
| 1436 | * |
| 1437 | * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents |
| 1438 | * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and |
| 1439 | * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED} |
| 1440 | * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled |
| 1441 | * by passing in the tech lists separately. Each first level entry in the tech list represents |
| 1442 | * an array of technologies that must all be present to match. If any of the first level sets |
| 1443 | * match then the dispatch is routed through the given PendingIntent. In other words, the second |
| 1444 | * level is ANDed together and the first level entries are ORed together. |
| 1445 | * |
| 1446 | * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters |
| 1447 | * that acts a wild card and will cause the foreground activity to receive all tags via the |
| 1448 | * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent. |
| 1449 | * |
| 1450 | * <p>This method must be called from the main thread, and only when the activity is in the |
| 1451 | * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before |
| 1452 | * the completion of their {@link Activity#onPause} callback to disable foreground dispatch |
| 1453 | * after it has been enabled. |
| 1454 | * |
| 1455 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 1456 | * |
| 1457 | * @param activity the Activity to dispatch to |
| 1458 | * @param intent the PendingIntent to start for the dispatch |
| 1459 | * @param filters the IntentFilters to override dispatching for, or null to always dispatch |
| 1460 | * @param techLists the tech lists used to perform matching for dispatching of the |
| 1461 | * {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent |
| 1462 | * @throws IllegalStateException if the Activity is not currently in the foreground |
| 1463 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1464 | */ |
| 1465 | public void enableForegroundDispatch(Activity activity, PendingIntent intent, |
| 1466 | IntentFilter[] filters, String[][] techLists) { |
| 1467 | synchronized (NfcAdapter.class) { |
| 1468 | if (!sHasNfcFeature) { |
| 1469 | throw new UnsupportedOperationException(); |
| 1470 | } |
| 1471 | } |
| 1472 | if (activity == null || intent == null) { |
| 1473 | throw new NullPointerException(); |
| 1474 | } |
| 1475 | if (!activity.isResumed()) { |
| 1476 | throw new IllegalStateException("Foreground dispatch can only be enabled " + |
| 1477 | "when your activity is resumed"); |
| 1478 | } |
| 1479 | try { |
| 1480 | TechListParcel parcel = null; |
| 1481 | if (techLists != null && techLists.length > 0) { |
| 1482 | parcel = new TechListParcel(techLists); |
| 1483 | } |
| 1484 | ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity, |
| 1485 | mForegroundDispatchListener); |
| 1486 | sService.setForegroundDispatch(intent, filters, parcel); |
| 1487 | } catch (RemoteException e) { |
| 1488 | attemptDeadServiceRecovery(e); |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | /** |
| 1493 | * Disable foreground dispatch to the given activity. |
| 1494 | * |
| 1495 | * <p>After calling {@link #enableForegroundDispatch}, an activity |
| 1496 | * must call this method before its {@link Activity#onPause} callback |
| 1497 | * completes. |
| 1498 | * |
| 1499 | * <p>This method must be called from the main thread. |
| 1500 | * |
| 1501 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 1502 | * |
| 1503 | * @param activity the Activity to disable dispatch to |
| 1504 | * @throws IllegalStateException if the Activity has already been paused |
| 1505 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1506 | */ |
| 1507 | public void disableForegroundDispatch(Activity activity) { |
| 1508 | synchronized (NfcAdapter.class) { |
| 1509 | if (!sHasNfcFeature) { |
| 1510 | throw new UnsupportedOperationException(); |
| 1511 | } |
| 1512 | } |
| 1513 | ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity, |
| 1514 | mForegroundDispatchListener); |
| 1515 | disableForegroundDispatchInternal(activity, false); |
| 1516 | } |
| 1517 | |
| 1518 | OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() { |
| 1519 | @Override |
| 1520 | public void onPaused(Activity activity) { |
| 1521 | disableForegroundDispatchInternal(activity, true); |
| 1522 | } |
| 1523 | }; |
| 1524 | |
| 1525 | void disableForegroundDispatchInternal(Activity activity, boolean force) { |
| 1526 | try { |
| 1527 | sService.setForegroundDispatch(null, null, null); |
| 1528 | if (!force && !activity.isResumed()) { |
| 1529 | throw new IllegalStateException("You must disable foreground dispatching " + |
| 1530 | "while your activity is still resumed"); |
| 1531 | } |
| 1532 | } catch (RemoteException e) { |
| 1533 | attemptDeadServiceRecovery(e); |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | /** |
| 1538 | * Limit the NFC controller to reader mode while this Activity is in the foreground. |
| 1539 | * |
| 1540 | * <p>In this mode the NFC controller will only act as an NFC tag reader/writer, |
| 1541 | * thus disabling any peer-to-peer (Android Beam) and card-emulation modes of |
| 1542 | * the NFC adapter on this device. |
| 1543 | * |
| 1544 | * <p>Use {@link #FLAG_READER_SKIP_NDEF_CHECK} to prevent the platform from |
| 1545 | * performing any NDEF checks in reader mode. Note that this will prevent the |
| 1546 | * {@link Ndef} tag technology from being enumerated on the tag, and that |
| 1547 | * NDEF-based tag dispatch will not be functional. |
| 1548 | * |
| 1549 | * <p>For interacting with tags that are emulated on another Android device |
| 1550 | * using Android's host-based card-emulation, the recommended flags are |
| 1551 | * {@link #FLAG_READER_NFC_A} and {@link #FLAG_READER_SKIP_NDEF_CHECK}. |
| 1552 | * |
| 1553 | * @param activity the Activity that requests the adapter to be in reader mode |
| 1554 | * @param callback the callback to be called when a tag is discovered |
| 1555 | * @param flags Flags indicating poll technologies and other optional parameters |
| 1556 | * @param extras Additional extras for configuring reader mode. |
| 1557 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1558 | */ |
| 1559 | public void enableReaderMode(Activity activity, ReaderCallback callback, int flags, |
| 1560 | Bundle extras) { |
| 1561 | synchronized (NfcAdapter.class) { |
| 1562 | if (!sHasNfcFeature) { |
| 1563 | throw new UnsupportedOperationException(); |
| 1564 | } |
| 1565 | } |
| 1566 | mNfcActivityManager.enableReaderMode(activity, callback, flags, extras); |
| 1567 | } |
| 1568 | |
| 1569 | /** |
| 1570 | * Restore the NFC adapter to normal mode of operation: supporting |
| 1571 | * peer-to-peer (Android Beam), card emulation, and polling for |
| 1572 | * all supported tag technologies. |
| 1573 | * |
| 1574 | * @param activity the Activity that currently has reader mode enabled |
| 1575 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1576 | */ |
| 1577 | public void disableReaderMode(Activity activity) { |
| 1578 | synchronized (NfcAdapter.class) { |
| 1579 | if (!sHasNfcFeature) { |
| 1580 | throw new UnsupportedOperationException(); |
| 1581 | } |
| 1582 | } |
| 1583 | mNfcActivityManager.disableReaderMode(activity); |
| 1584 | } |
| 1585 | |
| 1586 | /** |
| 1587 | * Manually invoke Android Beam to share data. |
| 1588 | * |
| 1589 | * <p>The Android Beam animation is normally only shown when two NFC-capable |
| 1590 | * devices come into range. |
| 1591 | * By calling this method, an Activity can invoke the Beam animation directly |
| 1592 | * even if no other NFC device is in range yet. The Beam animation will then |
| 1593 | * prompt the user to tap another NFC-capable device to complete the data |
| 1594 | * transfer. |
| 1595 | * |
| 1596 | * <p>The main advantage of using this method is that it avoids the need for the |
| 1597 | * user to tap the screen to complete the transfer, as this method already |
| 1598 | * establishes the direction of the transfer and the consent of the user to |
| 1599 | * share data. Callers are responsible for making sure that the user has |
| 1600 | * consented to sharing data on NFC tap. |
| 1601 | * |
| 1602 | * <p>Note that to use this method, the passed in Activity must have already |
| 1603 | * set data to share over Beam by using method calls such as |
| 1604 | * {@link #setNdefPushMessageCallback} or |
| 1605 | * {@link #setBeamPushUrisCallback}. |
| 1606 | * |
| 1607 | * @param activity the current foreground Activity that has registered data to share |
| 1608 | * @return whether the Beam animation was successfully invoked |
| 1609 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1610 | * @removed this feature is removed. File sharing can work using other technology like |
| 1611 | * Bluetooth. |
| 1612 | */ |
| 1613 | @java.lang.Deprecated |
| 1614 | @UnsupportedAppUsage |
| 1615 | public boolean invokeBeam(Activity activity) { |
| 1616 | synchronized (NfcAdapter.class) { |
| 1617 | if (!sHasNfcFeature) { |
| 1618 | throw new UnsupportedOperationException(); |
| 1619 | } |
| 1620 | } |
| 1621 | return false; |
| 1622 | } |
| 1623 | |
| 1624 | /** |
| 1625 | * Enable NDEF message push over NFC while this Activity is in the foreground. |
| 1626 | * |
| 1627 | * <p>You must explicitly call this method every time the activity is |
| 1628 | * resumed, and you must call {@link #disableForegroundNdefPush} before |
| 1629 | * your activity completes {@link Activity#onPause}. |
| 1630 | * |
| 1631 | * <p>Strongly recommend to use the new {@link #setNdefPushMessage} |
| 1632 | * instead: it automatically hooks into your activity life-cycle, |
| 1633 | * so you do not need to call enable/disable in your onResume/onPause. |
| 1634 | * |
| 1635 | * <p>For NDEF push to function properly the other NFC device must |
| 1636 | * support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or |
| 1637 | * Android's "com.android.npp" (Ndef Push Protocol). This was optional |
| 1638 | * on Gingerbread level Android NFC devices, but SNEP is mandatory on |
| 1639 | * Ice-Cream-Sandwich and beyond. |
| 1640 | * |
| 1641 | * <p>This method must be called from the main thread. |
| 1642 | * |
| 1643 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 1644 | * |
| 1645 | * @param activity foreground activity |
| 1646 | * @param message a NDEF Message to push over NFC |
| 1647 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable |
| 1648 | * @removed this feature is removed. File sharing can work using other technology like |
| 1649 | * Bluetooth. |
| 1650 | */ |
| 1651 | @Deprecated |
| 1652 | @UnsupportedAppUsage |
| 1653 | public void enableForegroundNdefPush(Activity activity, NdefMessage message) { |
| 1654 | synchronized (NfcAdapter.class) { |
| 1655 | if (!sHasNfcFeature) { |
| 1656 | throw new UnsupportedOperationException(); |
| 1657 | } |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | /** |
| 1662 | * Disable NDEF message push over P2P. |
| 1663 | * |
| 1664 | * <p>After calling {@link #enableForegroundNdefPush}, an activity |
| 1665 | * must call this method before its {@link Activity#onPause} callback |
| 1666 | * completes. |
| 1667 | * |
| 1668 | * <p>Strongly recommend to use the new {@link #setNdefPushMessage} |
| 1669 | * instead: it automatically hooks into your activity life-cycle, |
| 1670 | * so you do not need to call enable/disable in your onResume/onPause. |
| 1671 | * |
| 1672 | * <p>This method must be called from the main thread. |
| 1673 | * |
| 1674 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 1675 | * |
| 1676 | * @param activity the Foreground activity |
| 1677 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable |
| 1678 | * @removed this feature is removed. File sharing can work using other technology like |
| 1679 | * Bluetooth. |
| 1680 | */ |
| 1681 | @Deprecated |
| 1682 | @UnsupportedAppUsage |
| 1683 | public void disableForegroundNdefPush(Activity activity) { |
| 1684 | synchronized (NfcAdapter.class) { |
| 1685 | if (!sHasNfcFeature) { |
| 1686 | throw new UnsupportedOperationException(); |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | /** |
| 1692 | * Sets Secure NFC feature. |
| 1693 | * <p>This API is for the Settings application. |
| 1694 | * @return True if successful |
| 1695 | * @hide |
| 1696 | */ |
| 1697 | @SystemApi |
| 1698 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1699 | public boolean enableSecureNfc(boolean enable) { |
| 1700 | if (!sHasNfcFeature && !sHasCeFeature) { |
| 1701 | throw new UnsupportedOperationException(); |
| 1702 | } |
| 1703 | try { |
| 1704 | return sService.setNfcSecure(enable); |
| 1705 | } catch (RemoteException e) { |
| 1706 | attemptDeadServiceRecovery(e); |
| 1707 | // Try one more time |
| 1708 | if (sService == null) { |
| 1709 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1710 | return false; |
| 1711 | } |
| 1712 | try { |
| 1713 | return sService.setNfcSecure(enable); |
| 1714 | } catch (RemoteException ee) { |
| 1715 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1716 | } |
| 1717 | return false; |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | /** |
| 1722 | * Checks if the device supports Secure NFC functionality. |
| 1723 | * |
| 1724 | * @return True if device supports Secure NFC, false otherwise |
| 1725 | * @throws UnsupportedOperationException if FEATURE_NFC, |
| 1726 | * FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF, |
| 1727 | * FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE |
| 1728 | * are unavailable |
| 1729 | */ |
| 1730 | public boolean isSecureNfcSupported() { |
| 1731 | if (!sHasNfcFeature && !sHasCeFeature) { |
| 1732 | throw new UnsupportedOperationException(); |
| 1733 | } |
| 1734 | try { |
| 1735 | return sService.deviceSupportsNfcSecure(); |
| 1736 | } catch (RemoteException e) { |
| 1737 | attemptDeadServiceRecovery(e); |
| 1738 | // Try one more time |
| 1739 | if (sService == null) { |
| 1740 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1741 | return false; |
| 1742 | } |
| 1743 | try { |
| 1744 | return sService.deviceSupportsNfcSecure(); |
| 1745 | } catch (RemoteException ee) { |
| 1746 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1747 | } |
| 1748 | return false; |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | /** |
| 1753 | * Returns information regarding Nfc antennas on the device |
| 1754 | * such as their relative positioning on the device. |
| 1755 | * |
| 1756 | * @return Information on the nfc antenna(s) on the device. |
| 1757 | * @throws UnsupportedOperationException if FEATURE_NFC, |
| 1758 | * FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF, |
| 1759 | * FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE |
| 1760 | * are unavailable |
| 1761 | */ |
| 1762 | @Nullable |
| 1763 | public NfcAntennaInfo getNfcAntennaInfo() { |
| 1764 | if (!sHasNfcFeature && !sHasCeFeature) { |
| 1765 | throw new UnsupportedOperationException(); |
| 1766 | } |
| 1767 | try { |
| 1768 | return sService.getNfcAntennaInfo(); |
| 1769 | } catch (RemoteException e) { |
| 1770 | attemptDeadServiceRecovery(e); |
| 1771 | // Try one more time |
| 1772 | if (sService == null) { |
| 1773 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1774 | return null; |
| 1775 | } |
| 1776 | try { |
| 1777 | return sService.getNfcAntennaInfo(); |
| 1778 | } catch (RemoteException ee) { |
| 1779 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1780 | } |
| 1781 | return null; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | /** |
| 1786 | * Checks Secure NFC feature is enabled. |
| 1787 | * |
| 1788 | * @return True if Secure NFC is enabled, false otherwise |
| 1789 | * @throws UnsupportedOperationException if FEATURE_NFC, |
| 1790 | * FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF, |
| 1791 | * FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE |
| 1792 | * are unavailable |
| 1793 | * @throws UnsupportedOperationException if device doesn't support |
| 1794 | * Secure NFC functionality. {@link #isSecureNfcSupported} |
| 1795 | */ |
| 1796 | public boolean isSecureNfcEnabled() { |
| 1797 | if (!sHasNfcFeature && !sHasCeFeature) { |
| 1798 | throw new UnsupportedOperationException(); |
| 1799 | } |
| 1800 | try { |
| 1801 | return sService.isNfcSecureEnabled(); |
| 1802 | } catch (RemoteException e) { |
| 1803 | attemptDeadServiceRecovery(e); |
| 1804 | // Try one more time |
| 1805 | if (sService == null) { |
| 1806 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1807 | return false; |
| 1808 | } |
| 1809 | try { |
| 1810 | return sService.isNfcSecureEnabled(); |
| 1811 | } catch (RemoteException ee) { |
| 1812 | Log.e(TAG, "Failed to recover NFC Service."); |
| 1813 | } |
| 1814 | return false; |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | /** |
| 1819 | * Enable NDEF Push feature. |
| 1820 | * <p>This API is for the Settings application. |
| 1821 | * @hide |
| 1822 | * @removed |
| 1823 | */ |
| 1824 | @SystemApi |
| 1825 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1826 | @UnsupportedAppUsage |
| 1827 | public boolean enableNdefPush() { |
| 1828 | return false; |
| 1829 | } |
| 1830 | |
| 1831 | /** |
| 1832 | * Disable NDEF Push feature. |
| 1833 | * <p>This API is for the Settings application. |
| 1834 | * @hide |
| 1835 | * @removed |
| 1836 | */ |
| 1837 | @SystemApi |
| 1838 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1839 | @UnsupportedAppUsage |
| 1840 | public boolean disableNdefPush() { |
| 1841 | return false; |
| 1842 | } |
| 1843 | |
| 1844 | /** |
| 1845 | * Return true if the NDEF Push (Android Beam) feature is enabled. |
| 1846 | * <p>This function will return true only if both NFC is enabled, and the |
| 1847 | * NDEF Push feature is enabled. |
| 1848 | * <p>Note that if NFC is enabled but NDEF Push is disabled then this |
| 1849 | * device can still <i>receive</i> NDEF messages, it just cannot send them. |
| 1850 | * <p>Applications cannot directly toggle the NDEF Push feature, but they |
| 1851 | * can request Settings UI allowing the user to toggle NDEF Push using |
| 1852 | * <code>startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))</code> |
| 1853 | * <p>Example usage in an Activity that requires NDEF Push: |
| 1854 | * <p><pre> |
| 1855 | * protected void onResume() { |
| 1856 | * super.onResume(); |
| 1857 | * if (!nfcAdapter.isEnabled()) { |
| 1858 | * startActivity(new Intent(Settings.ACTION_NFC_SETTINGS)); |
| 1859 | * } else if (!nfcAdapter.isNdefPushEnabled()) { |
| 1860 | * startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS)); |
| 1861 | * } |
| 1862 | * }</pre> |
| 1863 | * |
| 1864 | * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS |
| 1865 | * @return true if NDEF Push feature is enabled |
| 1866 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable. |
| 1867 | * @removed this feature is removed. File sharing can work using other technology like |
| 1868 | * Bluetooth. |
| 1869 | */ |
| 1870 | @java.lang.Deprecated |
| 1871 | @UnsupportedAppUsage |
| 1872 | public boolean isNdefPushEnabled() { |
| 1873 | synchronized (NfcAdapter.class) { |
| 1874 | if (!sHasNfcFeature) { |
| 1875 | throw new UnsupportedOperationException(); |
| 1876 | } |
| 1877 | } |
| 1878 | return false; |
| 1879 | } |
| 1880 | |
| 1881 | /** |
| 1882 | * Signals that you are no longer interested in communicating with an NFC tag |
| 1883 | * for as long as it remains in range. |
| 1884 | * |
| 1885 | * All future attempted communication to this tag will fail with {@link IOException}. |
| 1886 | * The NFC controller will be put in a low-power polling mode, allowing the device |
| 1887 | * to save power in cases where it's "attached" to a tag all the time (e.g. a tag in |
| 1888 | * car dock). |
| 1889 | * |
| 1890 | * Additionally the debounceMs parameter allows you to specify for how long the tag needs |
| 1891 | * to have gone out of range, before it will be dispatched again. |
| 1892 | * |
| 1893 | * Note: the NFC controller typically polls at a pretty slow interval (100 - 500 ms). |
| 1894 | * This means that if the tag repeatedly goes in and out of range (for example, in |
| 1895 | * case of a flaky connection), and the controller happens to poll every time the |
| 1896 | * tag is out of range, it *will* re-dispatch the tag after debounceMs, despite the tag |
| 1897 | * having been "in range" during the interval. |
| 1898 | * |
| 1899 | * Note 2: if a tag with another UID is detected after this API is called, its effect |
| 1900 | * will be cancelled; if this tag shows up before the amount of time specified in |
| 1901 | * debounceMs, it will be dispatched again. |
| 1902 | * |
| 1903 | * Note 3: some tags have a random UID, in which case this API won't work reliably. |
| 1904 | * |
| 1905 | * @param tag the {@link android.nfc.Tag Tag} to ignore. |
| 1906 | * @param debounceMs minimum amount of time the tag needs to be out of range before being |
| 1907 | * dispatched again. |
| 1908 | * @param tagRemovedListener listener to be called when the tag is removed from the field. |
| 1909 | * Note that this will only be called if the tag has been out of range |
| 1910 | * for at least debounceMs, or if another tag came into range before |
| 1911 | * debounceMs. May be null in case you don't want a callback. |
| 1912 | * @param handler the {@link android.os.Handler Handler} that will be used for delivering |
| 1913 | * the callback. if the handler is null, then the thread used for delivering |
| 1914 | * the callback is unspecified. |
| 1915 | * @return false if the tag couldn't be found (or has already gone out of range), true otherwise |
| 1916 | */ |
| 1917 | public boolean ignore(final Tag tag, int debounceMs, |
| 1918 | final OnTagRemovedListener tagRemovedListener, final Handler handler) { |
| 1919 | ITagRemovedCallback.Stub iListener = null; |
| 1920 | if (tagRemovedListener != null) { |
| 1921 | iListener = new ITagRemovedCallback.Stub() { |
| 1922 | @Override |
| 1923 | public void onTagRemoved() throws RemoteException { |
| 1924 | if (handler != null) { |
| 1925 | handler.post(new Runnable() { |
| 1926 | @Override |
| 1927 | public void run() { |
| 1928 | tagRemovedListener.onTagRemoved(); |
| 1929 | } |
| 1930 | }); |
| 1931 | } else { |
| 1932 | tagRemovedListener.onTagRemoved(); |
| 1933 | } |
| 1934 | synchronized (mLock) { |
| 1935 | mTagRemovedListener = null; |
| 1936 | } |
| 1937 | } |
| 1938 | }; |
| 1939 | } |
| 1940 | synchronized (mLock) { |
| 1941 | mTagRemovedListener = iListener; |
| 1942 | } |
| 1943 | try { |
| 1944 | return sService.ignore(tag.getServiceHandle(), debounceMs, iListener); |
| 1945 | } catch (RemoteException e) { |
| 1946 | return false; |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | /** |
| 1951 | * Inject a mock NFC tag.<p> |
| 1952 | * Used for testing purposes. |
| 1953 | * <p class="note">Requires the |
| 1954 | * {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. |
| 1955 | * @hide |
| 1956 | */ |
| 1957 | public void dispatch(Tag tag) { |
| 1958 | if (tag == null) { |
| 1959 | throw new NullPointerException("tag cannot be null"); |
| 1960 | } |
| 1961 | try { |
| 1962 | sService.dispatch(tag); |
| 1963 | } catch (RemoteException e) { |
| 1964 | attemptDeadServiceRecovery(e); |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * Registers a new NFC unlock handler with the NFC service. |
| 1970 | * |
| 1971 | * <p />NFC unlock handlers are intended to unlock the keyguard in the presence of a trusted |
| 1972 | * NFC device. The handler should return true if it successfully authenticates the user and |
| 1973 | * unlocks the keyguard. |
| 1974 | * |
| 1975 | * <p /> The parameter {@code tagTechnologies} determines which Tag technologies will be polled for |
| 1976 | * at the lockscreen. Polling for less tag technologies reduces latency, and so it is |
| 1977 | * strongly recommended to only provide the Tag technologies that the handler is expected to |
| 1978 | * receive. There must be at least one tag technology provided, otherwise the unlock handler |
| 1979 | * is ignored. |
| 1980 | * |
| 1981 | * @hide |
| 1982 | */ |
| 1983 | @SystemApi |
| 1984 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1985 | public boolean addNfcUnlockHandler(final NfcUnlockHandler unlockHandler, |
| 1986 | String[] tagTechnologies) { |
| 1987 | synchronized (NfcAdapter.class) { |
| 1988 | if (!sHasNfcFeature) { |
| 1989 | throw new UnsupportedOperationException(); |
| 1990 | } |
| 1991 | } |
| 1992 | // If there are no tag technologies, don't bother adding unlock handler |
| 1993 | if (tagTechnologies.length == 0) { |
| 1994 | return false; |
| 1995 | } |
| 1996 | |
| 1997 | try { |
| 1998 | synchronized (mLock) { |
| 1999 | if (mNfcUnlockHandlers.containsKey(unlockHandler)) { |
| 2000 | // update the tag technologies |
| 2001 | sService.removeNfcUnlockHandler(mNfcUnlockHandlers.get(unlockHandler)); |
| 2002 | mNfcUnlockHandlers.remove(unlockHandler); |
| 2003 | } |
| 2004 | |
| 2005 | INfcUnlockHandler.Stub iHandler = new INfcUnlockHandler.Stub() { |
| 2006 | @Override |
| 2007 | public boolean onUnlockAttempted(Tag tag) throws RemoteException { |
| 2008 | return unlockHandler.onUnlockAttempted(tag); |
| 2009 | } |
| 2010 | }; |
| 2011 | |
| 2012 | sService.addNfcUnlockHandler(iHandler, |
| 2013 | Tag.getTechCodesFromStrings(tagTechnologies)); |
| 2014 | mNfcUnlockHandlers.put(unlockHandler, iHandler); |
| 2015 | } |
| 2016 | } catch (RemoteException e) { |
| 2017 | attemptDeadServiceRecovery(e); |
| 2018 | return false; |
| 2019 | } catch (IllegalArgumentException e) { |
| 2020 | Log.e(TAG, "Unable to register LockscreenDispatch", e); |
| 2021 | return false; |
| 2022 | } |
| 2023 | |
| 2024 | return true; |
| 2025 | } |
| 2026 | |
| 2027 | /** |
| 2028 | * Removes a previously registered unlock handler. Also removes the tag technologies |
| 2029 | * associated with the removed unlock handler. |
| 2030 | * |
| 2031 | * @hide |
| 2032 | */ |
| 2033 | @SystemApi |
| 2034 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 2035 | public boolean removeNfcUnlockHandler(NfcUnlockHandler unlockHandler) { |
| 2036 | synchronized (NfcAdapter.class) { |
| 2037 | if (!sHasNfcFeature) { |
| 2038 | throw new UnsupportedOperationException(); |
| 2039 | } |
| 2040 | } |
| 2041 | try { |
| 2042 | synchronized (mLock) { |
| 2043 | if (mNfcUnlockHandlers.containsKey(unlockHandler)) { |
| 2044 | sService.removeNfcUnlockHandler(mNfcUnlockHandlers.remove(unlockHandler)); |
| 2045 | } |
| 2046 | |
| 2047 | return true; |
| 2048 | } |
| 2049 | } catch (RemoteException e) { |
| 2050 | attemptDeadServiceRecovery(e); |
| 2051 | return false; |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | /** |
| 2056 | * @hide |
| 2057 | */ |
| 2058 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2059 | public INfcAdapterExtras getNfcAdapterExtrasInterface() { |
| 2060 | if (mContext == null) { |
| 2061 | throw new UnsupportedOperationException("You need a context on NfcAdapter to use the " |
| 2062 | + " NFC extras APIs"); |
| 2063 | } |
| 2064 | try { |
| 2065 | return sService.getNfcAdapterExtrasInterface(mContext.getPackageName()); |
| 2066 | } catch (RemoteException e) { |
| 2067 | attemptDeadServiceRecovery(e); |
| 2068 | // Try one more time |
| 2069 | if (sService == null) { |
| 2070 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2071 | return null; |
| 2072 | } |
| 2073 | try { |
| 2074 | return sService.getNfcAdapterExtrasInterface(mContext.getPackageName()); |
| 2075 | } catch (RemoteException ee) { |
| 2076 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2077 | } |
| 2078 | return null; |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | void enforceResumed(Activity activity) { |
| 2083 | if (!activity.isResumed()) { |
| 2084 | throw new IllegalStateException("API cannot be called while activity is paused"); |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | int getSdkVersion() { |
| 2089 | if (mContext == null) { |
| 2090 | return android.os.Build.VERSION_CODES.GINGERBREAD; // best guess |
| 2091 | } else { |
| 2092 | return mContext.getApplicationInfo().targetSdkVersion; |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | /** |
| 2097 | * Sets NFC controller always on feature. |
| 2098 | * <p>This API is for the NFCC internal state management. It allows to discriminate |
| 2099 | * the controller function from the NFC function by keeping the NFC controller on without |
| 2100 | * any NFC RF enabled if necessary. |
| 2101 | * <p>This call is asynchronous. Register a listener {@link #ControllerAlwaysOnListener} |
| 2102 | * by {@link #registerControllerAlwaysOnListener} to find out when the operation is |
| 2103 | * complete. |
| 2104 | * <p>If this returns true, then either NFCC always on state has been set based on the value, |
| 2105 | * or a {@link ControllerAlwaysOnListener#onControllerAlwaysOnChanged(boolean)} will be invoked |
| 2106 | * to indicate the state change. |
| 2107 | * If this returns false, then there is some problem that prevents an attempt to turn NFCC |
| 2108 | * always on. |
| 2109 | * @param value if true the NFCC will be kept on (with no RF enabled if NFC adapter is |
| 2110 | * disabled), if false the NFCC will follow completely the Nfc adapter state. |
| 2111 | * @throws UnsupportedOperationException if FEATURE_NFC, |
| 2112 | * FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF, |
| 2113 | * FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE |
| 2114 | * are unavailable |
| 2115 | * @return void |
| 2116 | * @hide |
| 2117 | */ |
| 2118 | @SystemApi |
| 2119 | @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) |
| 2120 | public boolean setControllerAlwaysOn(boolean value) { |
| 2121 | if (!sHasNfcFeature && !sHasCeFeature) { |
| 2122 | throw new UnsupportedOperationException(); |
| 2123 | } |
| 2124 | try { |
| 2125 | return sService.setControllerAlwaysOn(value); |
| 2126 | } catch (RemoteException e) { |
| 2127 | attemptDeadServiceRecovery(e); |
| 2128 | // Try one more time |
| 2129 | if (sService == null) { |
| 2130 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2131 | return false; |
| 2132 | } |
| 2133 | try { |
| 2134 | return sService.setControllerAlwaysOn(value); |
| 2135 | } catch (RemoteException ee) { |
| 2136 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2137 | } |
| 2138 | return false; |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | /** |
| 2143 | * Checks NFC controller always on feature is enabled. |
| 2144 | * |
| 2145 | * @return True if NFC controller always on is enabled, false otherwise |
| 2146 | * @throws UnsupportedOperationException if FEATURE_NFC, |
| 2147 | * FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF, |
| 2148 | * FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE |
| 2149 | * are unavailable |
| 2150 | * @hide |
| 2151 | */ |
| 2152 | @SystemApi |
| 2153 | @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) |
| 2154 | public boolean isControllerAlwaysOn() { |
| 2155 | try { |
| 2156 | return sService.isControllerAlwaysOn(); |
| 2157 | } catch (RemoteException e) { |
| 2158 | attemptDeadServiceRecovery(e); |
| 2159 | // Try one more time |
| 2160 | if (sService == null) { |
| 2161 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2162 | return false; |
| 2163 | } |
| 2164 | try { |
| 2165 | return sService.isControllerAlwaysOn(); |
| 2166 | } catch (RemoteException ee) { |
| 2167 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2168 | } |
| 2169 | return false; |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | /** |
| 2174 | * Checks if the device supports NFC controller always on functionality. |
| 2175 | * |
| 2176 | * @return True if device supports NFC controller always on, false otherwise |
| 2177 | * @throws UnsupportedOperationException if FEATURE_NFC, |
| 2178 | * FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF, |
| 2179 | * FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE |
| 2180 | * are unavailable |
| 2181 | * @hide |
| 2182 | */ |
| 2183 | @SystemApi |
| 2184 | @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) |
| 2185 | public boolean isControllerAlwaysOnSupported() { |
| 2186 | if (!sHasNfcFeature && !sHasCeFeature) { |
| 2187 | throw new UnsupportedOperationException(); |
| 2188 | } |
| 2189 | try { |
| 2190 | return sService.isControllerAlwaysOnSupported(); |
| 2191 | } catch (RemoteException e) { |
| 2192 | attemptDeadServiceRecovery(e); |
| 2193 | // Try one more time |
| 2194 | if (sService == null) { |
| 2195 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2196 | return false; |
| 2197 | } |
| 2198 | try { |
| 2199 | return sService.isControllerAlwaysOnSupported(); |
| 2200 | } catch (RemoteException ee) { |
| 2201 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2202 | } |
| 2203 | return false; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | /** |
| 2208 | * Register a {@link ControllerAlwaysOnListener} to listen for NFC controller always on |
| 2209 | * state changes |
| 2210 | * <p>The provided listener will be invoked by the given {@link Executor}. |
| 2211 | * |
| 2212 | * @param executor an {@link Executor} to execute given listener |
| 2213 | * @param listener user implementation of the {@link ControllerAlwaysOnListener} |
| 2214 | * @hide |
| 2215 | */ |
| 2216 | @SystemApi |
| 2217 | @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) |
| 2218 | public void registerControllerAlwaysOnListener( |
| 2219 | @NonNull @CallbackExecutor Executor executor, |
| 2220 | @NonNull ControllerAlwaysOnListener listener) { |
| 2221 | mControllerAlwaysOnListener.register(executor, listener); |
| 2222 | } |
| 2223 | |
| 2224 | /** |
| 2225 | * Unregister the specified {@link ControllerAlwaysOnListener} |
| 2226 | * <p>The same {@link ControllerAlwaysOnListener} object used when calling |
| 2227 | * {@link #registerControllerAlwaysOnListener(Executor, ControllerAlwaysOnListener)} |
| 2228 | * must be used. |
| 2229 | * |
| 2230 | * <p>Listeners are automatically unregistered when application process goes away |
| 2231 | * |
| 2232 | * @param listener user implementation of the {@link ControllerAlwaysOnListener} |
| 2233 | * @hide |
| 2234 | */ |
| 2235 | @SystemApi |
| 2236 | @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) |
| 2237 | public void unregisterControllerAlwaysOnListener( |
| 2238 | @NonNull ControllerAlwaysOnListener listener) { |
| 2239 | mControllerAlwaysOnListener.unregister(listener); |
| 2240 | } |
| 2241 | |
| 2242 | |
| 2243 | /** |
| 2244 | * Sets whether we dispatch NFC Tag intents to the package. |
| 2245 | * |
| 2246 | * <p>{@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED} or |
| 2247 | * {@link #ACTION_TAG_DISCOVERED} will not be dispatched to an Activity if its package is |
| 2248 | * disallowed. |
| 2249 | * <p>An app is added to the preference list with the allowed flag set to {@code true} |
| 2250 | * when a Tag intent is dispatched to the package for the first time. This API is called |
| 2251 | * by settings to note that the user wants to change this default preference. |
| 2252 | * |
| 2253 | * @param userId the user to whom this package name will belong to |
| 2254 | * @param pkg the full name (i.e. com.google.android.tag) of the package that will be added to |
| 2255 | * the preference list |
| 2256 | * @param allow {@code true} to allow dispatching Tag intents to the package's activity, |
| 2257 | * {@code false} otherwise |
| 2258 | * @return the {@link #TagIntentAppPreferenceResult} value |
| 2259 | * @throws UnsupportedOperationException if {@link isTagIntentAppPreferenceSupported} returns |
| 2260 | * {@code false} |
| 2261 | * |
| 2262 | * @hide |
| 2263 | */ |
| 2264 | @SystemApi |
| 2265 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 2266 | @TagIntentAppPreferenceResult |
| 2267 | public int setTagIntentAppPreferenceForUser(@UserIdInt int userId, |
| 2268 | @NonNull String pkg, boolean allow) { |
| 2269 | Objects.requireNonNull(pkg, "pkg cannot be null"); |
| 2270 | if (!isTagIntentAppPreferenceSupported()) { |
| 2271 | Log.e(TAG, "TagIntentAppPreference is not supported"); |
| 2272 | throw new UnsupportedOperationException(); |
| 2273 | } |
| 2274 | try { |
| 2275 | return sService.setTagIntentAppPreferenceForUser(userId, pkg, allow); |
| 2276 | } catch (RemoteException e) { |
| 2277 | attemptDeadServiceRecovery(e); |
| 2278 | // Try one more time |
| 2279 | if (sService == null) { |
| 2280 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2281 | } |
| 2282 | try { |
| 2283 | return sService.setTagIntentAppPreferenceForUser(userId, pkg, allow); |
| 2284 | } catch (RemoteException ee) { |
| 2285 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2286 | } |
| 2287 | return TAG_INTENT_APP_PREF_RESULT_UNAVAILABLE; |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | |
| 2292 | /** |
| 2293 | * Get the Tag dispatch preference list of the UserId. |
| 2294 | * |
| 2295 | * <p>This returns a mapping of package names for this user id to whether we dispatch Tag |
| 2296 | * intents to the package. {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED} or |
| 2297 | * {@link #ACTION_TAG_DISCOVERED} will not be dispatched to an Activity if its package is |
| 2298 | * mapped to {@code false}. |
| 2299 | * <p>There are three different possible cases: |
| 2300 | * <p>A package not being in the preference list. |
| 2301 | * It does not contain any Tag intent filters or the user never triggers a Tag detection that |
| 2302 | * matches the intent filter of the package. |
| 2303 | * <p>A package being mapped to {@code true}. |
| 2304 | * When a package has been launched by a tag detection for the first time, the package name is |
| 2305 | * put to the map and by default mapped to {@code true}. The package will receive Tag intents as |
| 2306 | * usual. |
| 2307 | * <p>A package being mapped to {@code false}. |
| 2308 | * The user chooses to disable this package and it will not receive any Tag intents anymore. |
| 2309 | * |
| 2310 | * @param userId the user to whom this preference list will belong to |
| 2311 | * @return a map of the UserId which indicates the mapping from package name to |
| 2312 | * boolean(allow status), otherwise return an empty map |
| 2313 | * @throws UnsupportedOperationException if {@link isTagIntentAppPreferenceSupported} returns |
| 2314 | * {@code false} |
| 2315 | * |
| 2316 | * @hide |
| 2317 | */ |
| 2318 | @SystemApi |
| 2319 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 2320 | @NonNull |
| 2321 | public Map<String, Boolean> getTagIntentAppPreferenceForUser(@UserIdInt int userId) { |
| 2322 | if (!isTagIntentAppPreferenceSupported()) { |
| 2323 | Log.e(TAG, "TagIntentAppPreference is not supported"); |
| 2324 | throw new UnsupportedOperationException(); |
| 2325 | } |
| 2326 | try { |
| 2327 | Map<String, Boolean> result = (Map<String, Boolean>) sService |
| 2328 | .getTagIntentAppPreferenceForUser(userId); |
| 2329 | return result; |
| 2330 | } catch (RemoteException e) { |
| 2331 | attemptDeadServiceRecovery(e); |
| 2332 | // Try one more time |
| 2333 | if (sService == null) { |
| 2334 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2335 | return Collections.emptyMap(); |
| 2336 | } |
| 2337 | try { |
| 2338 | Map<String, Boolean> result = (Map<String, Boolean>) sService |
| 2339 | .getTagIntentAppPreferenceForUser(userId); |
| 2340 | return result; |
| 2341 | } catch (RemoteException ee) { |
| 2342 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2343 | } |
| 2344 | return Collections.emptyMap(); |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | /** |
| 2349 | * Checks if the device supports Tag application preference. |
| 2350 | * |
| 2351 | * @return {@code true} if the device supports Tag application preference, {@code false} |
| 2352 | * otherwise |
| 2353 | * @throws UnsupportedOperationException if FEATURE_NFC is unavailable |
| 2354 | * |
| 2355 | * @hide |
| 2356 | */ |
| 2357 | @SystemApi |
| 2358 | @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 2359 | public boolean isTagIntentAppPreferenceSupported() { |
| 2360 | if (!sHasNfcFeature) { |
| 2361 | throw new UnsupportedOperationException(); |
| 2362 | } |
| 2363 | try { |
| 2364 | return sService.isTagIntentAppPreferenceSupported(); |
| 2365 | } catch (RemoteException e) { |
| 2366 | attemptDeadServiceRecovery(e); |
| 2367 | // Try one more time |
| 2368 | if (sService == null) { |
| 2369 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2370 | return false; |
| 2371 | } |
| 2372 | try { |
| 2373 | return sService.isTagIntentAppPreferenceSupported(); |
| 2374 | } catch (RemoteException ee) { |
| 2375 | Log.e(TAG, "Failed to recover NFC Service."); |
| 2376 | } |
| 2377 | return false; |
| 2378 | } |
| 2379 | } |
| 2380 | } |