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