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