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