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