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