Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 1 | /* |
Daniel Tomas | 9024564 | 2010-11-17 10:07:52 +0100 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package android.nfc; |
| 18 | |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 19 | import java.util.HashMap; |
| 20 | |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 21 | import android.annotation.SdkConstant; |
| 22 | import android.annotation.SdkConstant.SdkConstantType; |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 23 | import android.app.Activity; |
Nick Pelly | fdf9086 | 2010-10-18 11:19:41 -0700 | [diff] [blame] | 24 | import android.app.ActivityThread; |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 25 | import android.app.OnActivityPausedListener; |
| 26 | import android.app.PendingIntent; |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 28 | import android.content.IntentFilter; |
Nick Pelly | fdf9086 | 2010-10-18 11:19:41 -0700 | [diff] [blame] | 29 | import android.content.pm.IPackageManager; |
| 30 | import android.content.pm.PackageManager; |
Jeff Hamilton | 28319c0 | 2011-02-09 17:26:47 +0900 | [diff] [blame] | 31 | import android.nfc.tech.MifareClassic; |
| 32 | import android.nfc.tech.Ndef; |
| 33 | import android.nfc.tech.NfcA; |
| 34 | import android.nfc.tech.NfcF; |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 35 | import android.os.IBinder; |
| 36 | import android.os.RemoteException; |
| 37 | import android.os.ServiceManager; |
| 38 | import android.util.Log; |
| 39 | |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 40 | /** |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 41 | * Represents the local NFC adapter. |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 42 | * <p> |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 43 | * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC |
| 44 | * adapter for this Android device. |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 45 | * |
| 46 | * <div class="special reference"> |
| 47 | * <h3>Developer Guides</h3> |
| 48 | * <p>For more information about using NFC, read the |
| 49 | * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p> |
| 50 | * </div> |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 51 | */ |
| 52 | public final class NfcAdapter { |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 53 | static final String TAG = "NFC"; |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 54 | |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 55 | /** |
Jeff Hamilton | 641dd62 | 2010-12-02 09:16:22 -0600 | [diff] [blame] | 56 | * 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] | 57 | * |
Jeff Hamilton | 28319c0 | 2011-02-09 17:26:47 +0900 | [diff] [blame] | 58 | * <p>The system inspects the first {@link NdefRecord} in the first {@link NdefMessage} and |
| 59 | * looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the |
| 60 | * intent will contain the URI in its data field. If a MIME record is found the intent will |
| 61 | * contain the MIME type in its type field. This allows activities to register |
| 62 | * {@link IntentFilter}s targeting specific content on tags. Activities should register the |
| 63 | * most specific intent filters possible to avoid the activity chooser dialog, which can |
| 64 | * disrupt the interaction with the tag as the user interacts with the screen. |
| 65 | * |
| 66 | * <p>If the tag has an NDEF payload this intent is started before |
| 67 | * {@link #ACTION_TECH_DISCOVERED}. If any activities respond to this intent neither |
Nick Pelly | f003e26 | 2011-01-31 23:27:37 -0800 | [diff] [blame] | 68 | * {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started. |
Jeff Hamilton | 641dd62 | 2010-12-02 09:16:22 -0600 | [diff] [blame] | 69 | */ |
| 70 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 71 | public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED"; |
| 72 | |
| 73 | /** |
Jeff Hamilton | 28319c0 | 2011-02-09 17:26:47 +0900 | [diff] [blame] | 74 | * Intent to start an activity when a tag is discovered and activities are registered for the |
| 75 | * specific technologies on the tag. |
Jeff Hamilton | 641dd62 | 2010-12-02 09:16:22 -0600 | [diff] [blame] | 76 | * |
Jeff Hamilton | 28319c0 | 2011-02-09 17:26:47 +0900 | [diff] [blame] | 77 | * <p>To receive this intent an activity must include an intent filter |
| 78 | * for this action and specify the desired tech types in a |
| 79 | * manifest <code>meta-data</code> entry. Here is an example manfiest entry: |
| 80 | * <pre> |
| 81 | * <activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"> |
| 82 | * <!-- Add a technology filter --> |
| 83 | * <intent-filter> |
| 84 | * <action android:name="android.nfc.action.TECH_DISCOVERED" /> |
| 85 | * </intent-filter> |
Jeff Hamilton | 641dd62 | 2010-12-02 09:16:22 -0600 | [diff] [blame] | 86 | * |
Jeff Hamilton | 28319c0 | 2011-02-09 17:26:47 +0900 | [diff] [blame] | 87 | * <meta-data android:name="android.nfc.action.TECH_DISCOVERED" |
| 88 | * android:resource="@xml/filter_nfc" |
| 89 | * /> |
| 90 | * </activity> |
| 91 | * </pre> |
| 92 | * |
| 93 | * <p>The meta-data XML file should contain one or more <code>tech-list</code> entries |
| 94 | * each consisting or one or more <code>tech</code> entries. The <code>tech</code> entries refer |
| 95 | * to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA". |
| 96 | * |
| 97 | * <p>A tag matches if any of the |
| 98 | * <code>tech-list</code> sets is a subset of {@link Tag#getTechList() Tag.getTechList()}. Each |
| 99 | * of the <code>tech-list</code>s is considered independently and the |
| 100 | * activity is considered a match is any single <code>tech-list</code> matches the tag that was |
| 101 | * discovered. This provides AND and OR semantics for filtering desired techs. Here is an |
| 102 | * example that will match any tag using {@link NfcF} or any tag using {@link NfcA}, |
| 103 | * {@link MifareClassic}, and {@link Ndef}: |
| 104 | * |
| 105 | * <pre> |
| 106 | * <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> |
| 107 | * <!-- capture anything using NfcF --> |
| 108 | * <tech-list> |
| 109 | * <tech>android.nfc.tech.NfcF</tech> |
| 110 | * </tech-list> |
| 111 | * |
| 112 | * <!-- OR --> |
| 113 | * |
| 114 | * <!-- capture all MIFARE Classics with NDEF payloads --> |
| 115 | * <tech-list> |
| 116 | * <tech>android.nfc.tech.NfcA</tech> |
| 117 | * <tech>android.nfc.tech.MifareClassic</tech> |
| 118 | * <tech>android.nfc.tech.Ndef</tech> |
| 119 | * </tech-list> |
| 120 | * </resources> |
| 121 | * </pre> |
| 122 | * |
| 123 | * <p>This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before |
| 124 | * {@link #ACTION_TAG_DISCOVERED}. If any activities respond to {@link #ACTION_NDEF_DISCOVERED} |
| 125 | * this intent will not be started. If any activities respond to this intent |
| 126 | * {@link #ACTION_TAG_DISCOVERED} will not be started. |
Jeff Hamilton | 641dd62 | 2010-12-02 09:16:22 -0600 | [diff] [blame] | 127 | */ |
| 128 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
Nick Pelly | f003e26 | 2011-01-31 23:27:37 -0800 | [diff] [blame] | 129 | public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED"; |
Jeff Hamilton | 641dd62 | 2010-12-02 09:16:22 -0600 | [diff] [blame] | 130 | |
| 131 | /** |
Nick Pelly | 11b075e | 2010-10-28 13:39:37 -0700 | [diff] [blame] | 132 | * Intent to start an activity when a tag is discovered. |
Jeff Hamilton | 28319c0 | 2011-02-09 17:26:47 +0900 | [diff] [blame] | 133 | * |
| 134 | * <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] | 135 | * {@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] | 136 | */ |
| 137 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 138 | public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED"; |
| 139 | |
| 140 | /** |
Jeff Hamilton | 6be655c | 2010-11-12 12:28:16 -0600 | [diff] [blame] | 141 | * Broadcast to only the activity that handles ACTION_TAG_DISCOVERED |
| 142 | * @hide |
| 143 | */ |
| 144 | public static final String ACTION_TAG_LEFT_FIELD = "android.nfc.action.TAG_LOST"; |
| 145 | |
| 146 | /** |
Jeff Hamilton | 28319c0 | 2011-02-09 17:26:47 +0900 | [diff] [blame] | 147 | * Mandatory extra containing the {@link Tag} that was discovered for the |
| 148 | * {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and |
| 149 | * {@link #ACTION_TAG_DISCOVERED} intents. |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 150 | */ |
| 151 | public static final String EXTRA_TAG = "android.nfc.extra.TAG"; |
| 152 | |
| 153 | /** |
Jeff Hamilton | 28319c0 | 2011-02-09 17:26:47 +0900 | [diff] [blame] | 154 | * Optional extra containing an array of {@link NdefMessage} present on the discovered tag for |
| 155 | * the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and |
| 156 | * {@link #ACTION_TAG_DISCOVERED} intents. |
Nick Pelly | 11b075e | 2010-10-28 13:39:37 -0700 | [diff] [blame] | 157 | */ |
| 158 | public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES"; |
| 159 | |
| 160 | /** |
Jeff Hamilton | 28319c0 | 2011-02-09 17:26:47 +0900 | [diff] [blame] | 161 | * Optional extra containing a byte array containing the ID of the discovered tag for |
| 162 | * the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and |
| 163 | * {@link #ACTION_TAG_DISCOVERED} intents. |
Nick Pelly | 11b075e | 2010-10-28 13:39:37 -0700 | [diff] [blame] | 164 | */ |
| 165 | public static final String EXTRA_ID = "android.nfc.extra.ID"; |
| 166 | |
| 167 | /** |
Nick Pelly | 8d32a01 | 2011-08-09 07:03:49 -0700 | [diff] [blame] | 168 | * Broadcast Action: The state of the local NFC adapter has been |
| 169 | * changed. |
| 170 | * <p>For example, NFC has been turned on or off. |
| 171 | * <p>Always contains the extra field {@link #EXTRA_STATE} |
Brad Fitzpatrick | afb082d | 2010-10-18 14:02:58 -0700 | [diff] [blame] | 172 | * @hide |
| 173 | */ |
Nick Pelly | 8d32a01 | 2011-08-09 07:03:49 -0700 | [diff] [blame] | 174 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 175 | public static final String ACTION_ADAPTER_STATE_CHANGED = |
| 176 | "android.nfc.action.ADAPTER_STATE_CHANGED"; |
Brad Fitzpatrick | afb082d | 2010-10-18 14:02:58 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
Nick Pelly | 8d32a01 | 2011-08-09 07:03:49 -0700 | [diff] [blame] | 179 | * Used as an int extra field in {@link #ACTION_STATE_CHANGED} |
| 180 | * intents to request the current power state. Possible values are: |
| 181 | * {@link #STATE_OFF}, |
| 182 | * {@link #STATE_TURNING_ON}, |
| 183 | * {@link #STATE_ON}, |
| 184 | * {@link #STATE_TURNING_OFF}, |
Brad Fitzpatrick | afb082d | 2010-10-18 14:02:58 -0700 | [diff] [blame] | 185 | * @hide |
| 186 | */ |
Nick Pelly | 8d32a01 | 2011-08-09 07:03:49 -0700 | [diff] [blame] | 187 | public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE"; |
| 188 | |
| 189 | /** @hide */ |
| 190 | public static final int STATE_OFF = 1; |
| 191 | /** @hide */ |
| 192 | public static final int STATE_TURNING_ON = 2; |
| 193 | /** @hide */ |
| 194 | public static final int STATE_ON = 3; |
| 195 | /** @hide */ |
| 196 | public static final int STATE_TURNING_OFF = 4; |
Brad Fitzpatrick | afb082d | 2010-10-18 14:02:58 -0700 | [diff] [blame] | 197 | |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 198 | // Guarded by NfcAdapter.class |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 199 | static boolean sIsInitialized = false; |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 200 | |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 201 | // Final after first constructor, except for |
| 202 | // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort |
| 203 | // recovery |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 204 | static INfcAdapter sService; |
| 205 | static INfcTag sTagService; |
| 206 | |
| 207 | /** |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 208 | * The NfcAdapter object for each application context. |
| 209 | * There is a 1-1 relationship between application context and |
| 210 | * NfcAdapter object. |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 211 | */ |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 212 | static HashMap<Context, NfcAdapter> sNfcAdapters = new HashMap(); //guard by NfcAdapter.class |
| 213 | |
| 214 | /** |
| 215 | * NfcAdapter used with a null context. This ctor was deprecated but we have |
| 216 | * to support it for backwards compatibility. New methods that require context |
| 217 | * might throw when called on the null-context NfcAdapter. |
| 218 | */ |
| 219 | static NfcAdapter sNullContextNfcAdapter; // protected by NfcAdapter.class |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 220 | |
| 221 | final NfcActivityManager mNfcActivityManager; |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 222 | final Context mContext; |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 223 | |
| 224 | /** |
Scott Main | 2d68a6b | 2011-09-26 22:59:38 -0700 | [diff] [blame] | 225 | * A callback to be invoked when the system successfully delivers your {@link NdefMessage} |
| 226 | * to another device. |
| 227 | * @see #setOnNdefPushCompleteCallback |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 228 | */ |
| 229 | public interface OnNdefPushCompleteCallback { |
| 230 | /** |
| 231 | * Called on successful NDEF push. |
| 232 | * |
| 233 | * <p>This callback is usually made on a binder thread (not the UI thread). |
| 234 | * |
| 235 | * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set |
Scott Main | 2d68a6b | 2011-09-26 22:59:38 -0700 | [diff] [blame] | 236 | * @see #setNdefPushMessageCallback |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 237 | */ |
| 238 | public void onNdefPushComplete(NfcEvent event); |
| 239 | } |
| 240 | |
| 241 | /** |
Scott Main | 2d68a6b | 2011-09-26 22:59:38 -0700 | [diff] [blame] | 242 | * A callback to be invoked when another NFC device capable of NDEF push (Android Beam) |
| 243 | * is within range. |
| 244 | * <p>Implement this interface and pass it to {@link |
| 245 | * NfcAdapter#setNdefPushMessageCallback setNdefPushMessageCallback()} in order to create an |
| 246 | * {@link NdefMessage} at the moment that another device is within range for NFC. Using this |
| 247 | * callback allows you to create a message with data that might vary based on the |
| 248 | * content currently visible to the user. Alternatively, you can call {@link |
| 249 | * #setNdefPushMessage setNdefPushMessage()} if the {@link NdefMessage} always contains the |
| 250 | * same data. |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 251 | */ |
| 252 | public interface CreateNdefMessageCallback { |
| 253 | /** |
| 254 | * Called to provide a {@link NdefMessage} to push. |
| 255 | * |
| 256 | * <p>This callback is usually made on a binder thread (not the UI thread). |
| 257 | * |
| 258 | * <p>Called when this device is in range of another device |
| 259 | * that might support NDEF push. It allows the application to |
| 260 | * create the NDEF message only when it is required. |
| 261 | * |
| 262 | * <p>NDEF push cannot occur until this method returns, so do not |
| 263 | * block for too long. |
| 264 | * |
| 265 | * <p>The Android operating system will usually show a system UI |
| 266 | * on top of your activity during this time, so do not try to request |
| 267 | * input from the user to complete the callback, or provide custom NDEF |
| 268 | * push UI. The user probably will not see it. |
| 269 | * |
| 270 | * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set |
| 271 | * @return NDEF message to push, or null to not provide a message |
| 272 | */ |
| 273 | public NdefMessage createNdefMessage(NfcEvent event); |
| 274 | } |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 275 | |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 276 | /** |
Nick Pelly | fdf9086 | 2010-10-18 11:19:41 -0700 | [diff] [blame] | 277 | * Helper to check if this device has FEATURE_NFC, but without using |
| 278 | * a context. |
| 279 | * Equivalent to |
| 280 | * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC) |
| 281 | */ |
| 282 | private static boolean hasNfcFeature() { |
| 283 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 284 | if (pm == null) { |
| 285 | Log.e(TAG, "Cannot get package manager, assuming no NFC feature"); |
| 286 | return false; |
| 287 | } |
| 288 | try { |
| 289 | return pm.hasSystemFeature(PackageManager.FEATURE_NFC); |
| 290 | } catch (RemoteException e) { |
| 291 | Log.e(TAG, "Package manager query failed, assuming no NFC feature", e); |
| 292 | return false; |
| 293 | } |
| 294 | } |
| 295 | |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 296 | /** |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 297 | * Returns the NfcAdapter for application context, |
| 298 | * or throws if NFC is not available. |
| 299 | * @hide |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 300 | */ |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 301 | public static synchronized NfcAdapter getNfcAdapter(Context context) { |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 302 | if (!sIsInitialized) { |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 303 | /* is this device meant to have NFC */ |
| 304 | if (!hasNfcFeature()) { |
| 305 | Log.v(TAG, "this device does not have NFC support"); |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 306 | throw new UnsupportedOperationException(); |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | sService = getServiceInterface(); |
| 310 | if (sService == null) { |
| 311 | Log.e(TAG, "could not retrieve NFC service"); |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 312 | throw new UnsupportedOperationException(); |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 313 | } |
Nick Pelly | 3dd6c45 | 2011-01-10 18:14:41 +1100 | [diff] [blame] | 314 | try { |
| 315 | sTagService = sService.getNfcTagInterface(); |
| 316 | } catch (RemoteException e) { |
| 317 | Log.e(TAG, "could not retrieve NFC Tag service"); |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 318 | throw new UnsupportedOperationException(); |
Nick Pelly | 3dd6c45 | 2011-01-10 18:14:41 +1100 | [diff] [blame] | 319 | } |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 320 | |
| 321 | sIsInitialized = true; |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 322 | } |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 323 | if (context == null) { |
| 324 | if (sNullContextNfcAdapter == null) { |
| 325 | sNullContextNfcAdapter = new NfcAdapter(null); |
| 326 | } |
| 327 | return sNullContextNfcAdapter; |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 328 | } |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 329 | NfcAdapter adapter = sNfcAdapters.get(context); |
| 330 | if (adapter == null) { |
| 331 | adapter = new NfcAdapter(context); |
| 332 | sNfcAdapters.put(context, adapter); |
| 333 | } |
| 334 | return adapter; |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Nick Pelly | 6d55e13 | 2010-10-27 01:14:43 -0700 | [diff] [blame] | 337 | /** get handle to NFC service interface */ |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 338 | private static INfcAdapter getServiceInterface() { |
Nick Pelly | 6d55e13 | 2010-10-27 01:14:43 -0700 | [diff] [blame] | 339 | /* get a handle to NFC service */ |
| 340 | IBinder b = ServiceManager.getService("nfc"); |
| 341 | if (b == null) { |
| 342 | return null; |
| 343 | } |
| 344 | return INfcAdapter.Stub.asInterface(b); |
| 345 | } |
| 346 | |
Nick Pelly | fdf9086 | 2010-10-18 11:19:41 -0700 | [diff] [blame] | 347 | /** |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 348 | * Helper to get the default NFC Adapter. |
| 349 | * <p> |
| 350 | * Most Android devices will only have one NFC Adapter (NFC Controller). |
| 351 | * <p> |
| 352 | * This helper is the equivalent of: |
| 353 | * <pre>{@code |
| 354 | * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE); |
| 355 | * NfcAdapter adapter = manager.getDefaultAdapter(); |
| 356 | * }</pre> |
| 357 | * @param context the calling application's context |
| 358 | * |
| 359 | * @return the default NFC adapter, or null if no NFC adapter exists |
| 360 | */ |
| 361 | public static NfcAdapter getDefaultAdapter(Context context) { |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 362 | if (context == null) { |
| 363 | throw new IllegalArgumentException("context cannot be null"); |
| 364 | } |
| 365 | context = context.getApplicationContext(); |
Nick Pelly | b04cce0 | 2011-11-21 17:02:02 -0800 | [diff] [blame] | 366 | if (context == null) { |
| 367 | throw new IllegalArgumentException( |
| 368 | "context not associated with any application (using a mock context?)"); |
| 369 | } |
| 370 | /* use getSystemService() for consistency */ |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 371 | NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE); |
Nick Pelly | a5193b24 | 2011-11-16 16:46:27 -0800 | [diff] [blame] | 372 | if (manager == null) { |
| 373 | // NFC not available |
| 374 | return null; |
| 375 | } |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 376 | return manager.getDefaultAdapter(); |
| 377 | } |
| 378 | |
| 379 | /** |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 380 | * Legacy NfcAdapter getter, always use {@link #getDefaultAdapter(Context)} instead.<p> |
| 381 | * This method was deprecated at API level 10 (Gingerbread MR1) because a context is required |
| 382 | * for many NFC API methods. Those methods will fail when called on an NfcAdapter |
| 383 | * object created from this method.<p> |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 384 | * @deprecated use {@link #getDefaultAdapter(Context)} |
Nick Pelly | a356bf1 | 2011-12-13 15:36:31 -0800 | [diff] [blame] | 385 | * @hide |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 386 | */ |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 387 | @Deprecated |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 388 | public static NfcAdapter getDefaultAdapter() { |
Nick Pelly | a356bf1 | 2011-12-13 15:36:31 -0800 | [diff] [blame] | 389 | // introduce in API version 9 (GB 2.3) |
| 390 | // deprecated in API version 10 (GB 2.3.3) |
| 391 | // removed from public API in version 16 (ICS MR2) |
| 392 | // will need to maintain this as a hidden API for a while longer... |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 393 | Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " + |
| 394 | "NfcAdapter.getDefaultAdapter(Context) instead", new Exception()); |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 395 | |
| 396 | return NfcAdapter.getNfcAdapter(null); |
| 397 | } |
| 398 | |
| 399 | NfcAdapter(Context context) { |
| 400 | mContext = context; |
| 401 | mNfcActivityManager = new NfcActivityManager(this); |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 402 | } |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 403 | |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 404 | /** |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 405 | * @hide |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 406 | */ |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 407 | public Context getContext() { |
| 408 | return mContext; |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Returns the binder interface to the service. |
| 413 | * @hide |
| 414 | */ |
| 415 | public INfcAdapter getService() { |
Nick Pelly | 253c509 | 2011-01-13 09:22:57 -0800 | [diff] [blame] | 416 | isEnabled(); // NOP call to recover sService if it is stale |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 417 | return sService; |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Jeff Hamilton | 6be655c | 2010-11-12 12:28:16 -0600 | [diff] [blame] | 420 | /** |
Nick Pelly | 3dd6c45 | 2011-01-10 18:14:41 +1100 | [diff] [blame] | 421 | * Returns the binder interface to the tag service. |
| 422 | * @hide |
| 423 | */ |
| 424 | public INfcTag getTagService() { |
Nick Pelly | 253c509 | 2011-01-13 09:22:57 -0800 | [diff] [blame] | 425 | isEnabled(); // NOP call to recover sTagService if it is stale |
Nick Pelly | 3dd6c45 | 2011-01-10 18:14:41 +1100 | [diff] [blame] | 426 | return sTagService; |
| 427 | } |
| 428 | |
| 429 | /** |
Jeff Hamilton | 6be655c | 2010-11-12 12:28:16 -0600 | [diff] [blame] | 430 | * NFC service dead - attempt best effort recovery |
| 431 | * @hide |
| 432 | */ |
| 433 | public void attemptDeadServiceRecovery(Exception e) { |
Nick Pelly | 6d55e13 | 2010-10-27 01:14:43 -0700 | [diff] [blame] | 434 | Log.e(TAG, "NFC service dead - attempting to recover", e); |
| 435 | INfcAdapter service = getServiceInterface(); |
| 436 | if (service == null) { |
| 437 | Log.e(TAG, "could not retrieve NFC service during service recovery"); |
Nick Pelly | 3dd6c45 | 2011-01-10 18:14:41 +1100 | [diff] [blame] | 438 | // nothing more can be done now, sService is still stale, we'll hit |
| 439 | // this recovery path again later |
Nick Pelly | 6d55e13 | 2010-10-27 01:14:43 -0700 | [diff] [blame] | 440 | return; |
| 441 | } |
Nick Pelly | 3dd6c45 | 2011-01-10 18:14:41 +1100 | [diff] [blame] | 442 | // assigning to sService is not thread-safe, but this is best-effort code |
| 443 | // and on a well-behaved system should never happen |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 444 | sService = service; |
Nick Pelly | 3dd6c45 | 2011-01-10 18:14:41 +1100 | [diff] [blame] | 445 | try { |
| 446 | sTagService = service.getNfcTagInterface(); |
| 447 | } catch (RemoteException ee) { |
| 448 | Log.e(TAG, "could not retrieve NFC tag service during service recovery"); |
| 449 | // nothing more can be done now, sService is still stale, we'll hit |
| 450 | // this recovery path again later |
| 451 | } |
| 452 | |
Nick Pelly | 6d55e13 | 2010-10-27 01:14:43 -0700 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 456 | /** |
Nick Pelly | 7ea5c45 | 2010-10-20 18:39:11 -0700 | [diff] [blame] | 457 | * Return true if this NFC Adapter has any features enabled. |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 458 | * |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 459 | * <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] | 460 | * generate or respond to any NFC communication over its NFC radio. |
| 461 | * <p>Applications can use this to check if NFC is enabled. Applications |
| 462 | * can request Settings UI allowing the user to toggle NFC using: |
| 463 | * <p><pre>startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))</pre> |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 464 | * |
Nick Pelly | cccf01d | 2011-10-31 14:49:40 -0700 | [diff] [blame] | 465 | * @see android.provider.Settings#ACTION_NFC_SETTINGS |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 466 | * @return true if this NFC Adapter has any features enabled |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 467 | */ |
Nick Pelly | 7ea5c45 | 2010-10-20 18:39:11 -0700 | [diff] [blame] | 468 | public boolean isEnabled() { |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 469 | try { |
Nick Pelly | 8d32a01 | 2011-08-09 07:03:49 -0700 | [diff] [blame] | 470 | return sService.getState() == STATE_ON; |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 471 | } catch (RemoteException e) { |
Nick Pelly | 6d55e13 | 2010-10-27 01:14:43 -0700 | [diff] [blame] | 472 | attemptDeadServiceRecovery(e); |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /** |
Nick Pelly | 8d32a01 | 2011-08-09 07:03:49 -0700 | [diff] [blame] | 478 | * Return the state of this NFC Adapter. |
| 479 | * |
| 480 | * <p>Returns one of {@link #STATE_ON}, {@link #STATE_TURNING_ON}, |
| 481 | * {@link #STATE_OFF}, {@link #STATE_TURNING_OFF}. |
| 482 | * |
| 483 | * <p>{@link #isEnabled()} is equivalent to |
| 484 | * <code>{@link #getAdapterState()} == {@link #STATE_ON}</code> |
| 485 | * |
| 486 | * @return the current state of this NFC adapter |
| 487 | * |
| 488 | * @hide |
| 489 | */ |
| 490 | public int getAdapterState() { |
| 491 | try { |
| 492 | return sService.getState(); |
| 493 | } catch (RemoteException e) { |
| 494 | attemptDeadServiceRecovery(e); |
| 495 | return NfcAdapter.STATE_OFF; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /** |
Nick Pelly | 7ea5c45 | 2010-10-20 18:39:11 -0700 | [diff] [blame] | 500 | * Enable NFC hardware. |
Nick Pelly | 8d32a01 | 2011-08-09 07:03:49 -0700 | [diff] [blame] | 501 | * |
| 502 | * <p>This call is asynchronous. Listen for |
| 503 | * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the |
| 504 | * operation is complete. |
| 505 | * |
| 506 | * <p>If this returns true, then either NFC is already on, or |
| 507 | * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent |
| 508 | * to indicate a state transition. If this returns false, then |
| 509 | * there is some problem that prevents an attempt to turn |
| 510 | * NFC on (for example we are in airplane mode and NFC is not |
| 511 | * toggleable in airplane mode on this platform). |
Brad Fitzpatrick | afb082d | 2010-10-18 14:02:58 -0700 | [diff] [blame] | 512 | * |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 513 | * @hide |
| 514 | */ |
Nick Pelly | 7ea5c45 | 2010-10-20 18:39:11 -0700 | [diff] [blame] | 515 | public boolean enable() { |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 516 | try { |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 517 | return sService.enable(); |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 518 | } catch (RemoteException e) { |
Nick Pelly | 6d55e13 | 2010-10-27 01:14:43 -0700 | [diff] [blame] | 519 | attemptDeadServiceRecovery(e); |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 520 | return false; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | /** |
Nick Pelly | 7ea5c45 | 2010-10-20 18:39:11 -0700 | [diff] [blame] | 525 | * Disable NFC hardware. |
Nick Pelly | 8d32a01 | 2011-08-09 07:03:49 -0700 | [diff] [blame] | 526 | * |
| 527 | * <p>No NFC features will work after this call, and the hardware |
Nick Pelly | 7ea5c45 | 2010-10-20 18:39:11 -0700 | [diff] [blame] | 528 | * will not perform or respond to any NFC communication. |
Nick Pelly | 8d32a01 | 2011-08-09 07:03:49 -0700 | [diff] [blame] | 529 | * |
| 530 | * <p>This call is asynchronous. Listen for |
| 531 | * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the |
| 532 | * operation is complete. |
| 533 | * |
| 534 | * <p>If this returns true, then either NFC is already off, or |
| 535 | * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent |
| 536 | * to indicate a state transition. If this returns false, then |
| 537 | * there is some problem that prevents an attempt to turn |
| 538 | * NFC off. |
Brad Fitzpatrick | afb082d | 2010-10-18 14:02:58 -0700 | [diff] [blame] | 539 | * |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 540 | * @hide |
| 541 | */ |
Nick Pelly | 7ea5c45 | 2010-10-20 18:39:11 -0700 | [diff] [blame] | 542 | public boolean disable() { |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 543 | try { |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 544 | return sService.disable(); |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 545 | } catch (RemoteException e) { |
Nick Pelly | 6d55e13 | 2010-10-27 01:14:43 -0700 | [diff] [blame] | 546 | attemptDeadServiceRecovery(e); |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 547 | return false; |
| 548 | } |
| 549 | } |
| 550 | |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 551 | /** |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 552 | * Set the {@link NdefMessage} to push over NFC during the specified activities. |
| 553 | * |
| 554 | * <p>This method may be called at any time, but the NDEF message is |
| 555 | * only made available for NDEF push when one of the specified activities |
| 556 | * is in resumed (foreground) state. |
| 557 | * |
| 558 | * <p>Only one NDEF message can be pushed by the currently resumed activity. |
| 559 | * If both {@link #setNdefPushMessage} and |
| 560 | * {@link #setNdefPushMessageCallback} are set then |
| 561 | * the callback will take priority. |
| 562 | * |
| 563 | * <p>Pass a null NDEF message to disable foreground NDEF push in the |
| 564 | * specified activities. |
| 565 | * |
Scott Main | 2d68a6b | 2011-09-26 22:59:38 -0700 | [diff] [blame] | 566 | * <p>At least one activity must be specified, and usually only one is necessary. |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 567 | * |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 568 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 569 | * |
| 570 | * @param message NDEF message to push over NFC, or null to disable |
Scott Main | 2d68a6b | 2011-09-26 22:59:38 -0700 | [diff] [blame] | 571 | * @param activity an activity in which NDEF push should be enabled to share the provided |
| 572 | * NDEF message |
| 573 | * @param activities optional additional activities that should also enable NDEF push with |
| 574 | * the provided NDEF message |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 575 | */ |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 576 | public void setNdefPushMessage(NdefMessage message, Activity activity, |
| 577 | Activity ... activities) { |
| 578 | if (activity == null) { |
| 579 | throw new NullPointerException("activity cannot be null"); |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 580 | } |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 581 | mNfcActivityManager.setNdefPushMessage(activity, message); |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 582 | for (Activity a : activities) { |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 583 | if (a == null) { |
| 584 | throw new NullPointerException("activities cannot contain null"); |
| 585 | } |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 586 | mNfcActivityManager.setNdefPushMessage(a, message); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Set the callback to create a {@link NdefMessage} to push over NFC. |
| 592 | * |
| 593 | * <p>This method may be called at any time, but this callback is |
| 594 | * only made if one of the specified activities |
| 595 | * is in resumed (foreground) state. |
| 596 | * |
| 597 | * <p>Only one NDEF message can be pushed by the currently resumed activity. |
| 598 | * If both {@link #setNdefPushMessage} and |
| 599 | * {@link #setNdefPushMessageCallback} are set then |
| 600 | * the callback will take priority. |
| 601 | * |
| 602 | * <p>Pass a null callback to disable the callback in the |
| 603 | * specified activities. |
| 604 | * |
Scott Main | 2d68a6b | 2011-09-26 22:59:38 -0700 | [diff] [blame] | 605 | * <p>At least one activity must be specified, and usually only one is necessary. |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 606 | * |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 607 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 608 | * |
| 609 | * @param callback callback, or null to disable |
Scott Main | 2d68a6b | 2011-09-26 22:59:38 -0700 | [diff] [blame] | 610 | * @param activity an activity in which NDEF push should be enabled to share an NDEF message |
| 611 | * that's retrieved from the provided callback |
| 612 | * @param activities optional additional activities that should also enable NDEF push using |
| 613 | * the provided callback |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 614 | */ |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 615 | public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity, |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 616 | Activity ... activities) { |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 617 | if (activity == null) { |
| 618 | throw new NullPointerException("activity cannot be null"); |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 619 | } |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 620 | mNfcActivityManager.setNdefPushMessageCallback(activity, callback); |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 621 | for (Activity a : activities) { |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 622 | if (a == null) { |
| 623 | throw new NullPointerException("activities cannot contain null"); |
| 624 | } |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 625 | mNfcActivityManager.setNdefPushMessageCallback(a, callback); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Set the callback on a successful NDEF push over NFC. |
| 631 | * |
| 632 | * <p>This method may be called at any time, but NDEF push and this callback |
| 633 | * can only occur when one of the specified activities is in resumed |
| 634 | * (foreground) state. |
| 635 | * |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 636 | * <p>One or more activities must be specified. |
| 637 | * |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 638 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 639 | * |
| 640 | * @param callback callback, or null to disable |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 641 | * @param activity an activity to enable the callback (at least one is required) |
| 642 | * @param activities zero or more additional activities to enable to callback |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 643 | */ |
| 644 | public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback, |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 645 | Activity activity, Activity ... activities) { |
| 646 | if (activity == null) { |
| 647 | throw new NullPointerException("activity cannot be null"); |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 648 | } |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 649 | mNfcActivityManager.setOnNdefPushCompleteCallback(activity, callback); |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 650 | for (Activity a : activities) { |
Nick Pelly | 82328bf | 2011-08-30 09:37:25 -0700 | [diff] [blame] | 651 | if (a == null) { |
| 652 | throw new NullPointerException("activities cannot contain null"); |
| 653 | } |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 654 | mNfcActivityManager.setOnNdefPushCompleteCallback(a, callback); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | /** |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 659 | * Enable foreground dispatch to the given Activity. |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 660 | * |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 661 | * <p>This will give give priority to the foreground activity when |
| 662 | * dispatching a discovered {@link Tag} to an application. |
| 663 | * |
Jeff Hamilton | 167d9e4 | 2011-02-09 12:27:01 +0900 | [diff] [blame] | 664 | * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents |
| 665 | * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and |
| 666 | * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED} |
| 667 | * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled |
| 668 | * by passing in the tech lists separately. Each first level entry in the tech list represents |
| 669 | * an array of technologies that must all be present to match. If any of the first level sets |
| 670 | * match then the dispatch is routed through the given PendingIntent. In other words, the second |
| 671 | * level is ANDed together and the first level entries are ORed together. |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 672 | * |
Jeff Hamilton | 167d9e4 | 2011-02-09 12:27:01 +0900 | [diff] [blame] | 673 | * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters |
| 674 | * that acts a wild card and will cause the foreground activity to receive all tags via the |
| 675 | * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent. |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 676 | * |
Jeff Hamilton | 167d9e4 | 2011-02-09 12:27:01 +0900 | [diff] [blame] | 677 | * <p>This method must be called from the main thread, and only when the activity is in the |
| 678 | * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before |
| 679 | * the completion of their {@link Activity#onPause} callback to disable foreground dispatch |
| 680 | * after it has been enabled. |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 681 | * |
Nick Pelly | 39cf3a4 | 2011-02-07 17:04:21 +0900 | [diff] [blame] | 682 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 683 | * |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 684 | * @param activity the Activity to dispatch to |
| 685 | * @param intent the PendingIntent to start for the dispatch |
Jeff Hamilton | 9f20cd7 | 2011-01-23 12:14:42 -0600 | [diff] [blame] | 686 | * @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] | 687 | * @param techLists the tech lists used to perform matching for dispatching of the |
| 688 | * {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 689 | * @throws IllegalStateException if the Activity is not currently in the foreground |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 690 | */ |
| 691 | public void enableForegroundDispatch(Activity activity, PendingIntent intent, |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 692 | IntentFilter[] filters, String[][] techLists) { |
Jeff Hamilton | 9f20cd7 | 2011-01-23 12:14:42 -0600 | [diff] [blame] | 693 | if (activity == null || intent == null) { |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 694 | throw new NullPointerException(); |
| 695 | } |
| 696 | if (!activity.isResumed()) { |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 697 | throw new IllegalStateException("Foreground dispatch can only be enabled " + |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 698 | "when your activity is resumed"); |
| 699 | } |
| 700 | try { |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 701 | TechListParcel parcel = null; |
| 702 | if (techLists != null && techLists.length > 0) { |
| 703 | parcel = new TechListParcel(techLists); |
| 704 | } |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 705 | ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity, |
Jeff Hamilton | ce3224c | 2011-01-17 11:05:03 -0800 | [diff] [blame] | 706 | mForegroundDispatchListener); |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 707 | sService.setForegroundDispatch(intent, filters, parcel); |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 708 | } catch (RemoteException e) { |
| 709 | attemptDeadServiceRecovery(e); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /** |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 714 | * Disable foreground dispatch to the given activity. |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 715 | * |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 716 | * <p>After calling {@link #enableForegroundDispatch}, an activity |
| 717 | * must call this method before its {@link Activity#onPause} callback |
| 718 | * completes. |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 719 | * |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 720 | * <p>This method must be called from the main thread. |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 721 | * |
Nick Pelly | 39cf3a4 | 2011-02-07 17:04:21 +0900 | [diff] [blame] | 722 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 723 | * |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 724 | * @param activity the Activity to disable dispatch to |
| 725 | * @throws IllegalStateException if the Activity has already been paused |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 726 | */ |
| 727 | public void disableForegroundDispatch(Activity activity) { |
Jeff Hamilton | ce3224c | 2011-01-17 11:05:03 -0800 | [diff] [blame] | 728 | ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity, |
| 729 | mForegroundDispatchListener); |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 730 | disableForegroundDispatchInternal(activity, false); |
| 731 | } |
| 732 | |
Jeff Hamilton | ce3224c | 2011-01-17 11:05:03 -0800 | [diff] [blame] | 733 | OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() { |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 734 | @Override |
| 735 | public void onPaused(Activity activity) { |
| 736 | disableForegroundDispatchInternal(activity, true); |
| 737 | } |
Jeff Hamilton | ce3224c | 2011-01-17 11:05:03 -0800 | [diff] [blame] | 738 | }; |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 739 | |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 740 | void disableForegroundDispatchInternal(Activity activity, boolean force) { |
| 741 | try { |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 742 | sService.setForegroundDispatch(null, null, null); |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 743 | if (!force && !activity.isResumed()) { |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 744 | throw new IllegalStateException("You must disable foreground dispatching " + |
Jeff Hamilton | 52d3203 | 2011-01-08 15:31:26 -0600 | [diff] [blame] | 745 | "while your activity is still resumed"); |
| 746 | } |
| 747 | } catch (RemoteException e) { |
| 748 | attemptDeadServiceRecovery(e); |
| 749 | } |
| 750 | } |
| 751 | |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 752 | /** |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 753 | * 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] | 754 | * |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 755 | * <p>You must explicitly call this method every time the activity is |
| 756 | * resumed, and you must call {@link #disableForegroundNdefPush} before |
| 757 | * your activity completes {@link Activity#onPause}. |
| 758 | * |
| 759 | * <p>Strongly recommend to use the new {@link #setNdefPushMessage} |
| 760 | * instead: it automatically hooks into your activity life-cycle, |
| 761 | * so you do not need to call enable/disable in your onResume/onPause. |
| 762 | * |
| 763 | * <p>For NDEF push to function properly the other NFC device must |
| 764 | * support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or |
| 765 | * Android's "com.android.npp" (Ndef Push Protocol). This was optional |
| 766 | * on Gingerbread level Android NFC devices, but SNEP is mandatory on |
| 767 | * Ice-Cream-Sandwich and beyond. |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 768 | * |
| 769 | * <p>This method must be called from the main thread. |
Jeff Hamilton | 9f20cd7 | 2011-01-23 12:14:42 -0600 | [diff] [blame] | 770 | * |
Nick Pelly | 39cf3a4 | 2011-02-07 17:04:21 +0900 | [diff] [blame] | 771 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 772 | * |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 773 | * @param activity foreground activity |
| 774 | * @param message a NDEF Message to push over NFC |
| 775 | * @throws IllegalStateException if the activity is not currently in the foreground |
| 776 | * @deprecated use {@link #setNdefPushMessage} instead |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 777 | */ |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 778 | @Deprecated |
| 779 | public void enableForegroundNdefPush(Activity activity, NdefMessage message) { |
| 780 | if (activity == null || message == null) { |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 781 | throw new NullPointerException(); |
| 782 | } |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 783 | enforceResumed(activity); |
| 784 | mNfcActivityManager.setNdefPushMessage(activity, message); |
Jason parks | 0142536 | 2011-05-24 02:57:37 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 788 | * Disable NDEF message push over P2P. |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 789 | * |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 790 | * <p>After calling {@link #enableForegroundNdefPush}, an activity |
| 791 | * must call this method before its {@link Activity#onPause} callback |
| 792 | * completes. |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 793 | * |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 794 | * <p>Strongly recommend to use the new {@link #setNdefPushMessage} |
| 795 | * instead: it automatically hooks into your activity life-cycle, |
| 796 | * so you do not need to call enable/disable in your onResume/onPause. |
| 797 | * |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 798 | * <p>This method must be called from the main thread. |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 799 | * |
Nick Pelly | 39cf3a4 | 2011-02-07 17:04:21 +0900 | [diff] [blame] | 800 | * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. |
| 801 | * |
Nick Pelly | 74fe6c6 | 2011-02-02 22:37:40 -0800 | [diff] [blame] | 802 | * @param activity the Foreground activity |
| 803 | * @throws IllegalStateException if the Activity has already been paused |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 804 | * @deprecated use {@link #setNdefPushMessage} instead |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 805 | */ |
Jeff Hamilton | ce3224c | 2011-01-17 11:05:03 -0800 | [diff] [blame] | 806 | public void disableForegroundNdefPush(Activity activity) { |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 807 | if (activity == null) { |
| 808 | throw new NullPointerException(); |
| 809 | } |
| 810 | enforceResumed(activity); |
| 811 | mNfcActivityManager.setNdefPushMessage(activity, null); |
| 812 | mNfcActivityManager.setNdefPushMessageCallback(activity, null); |
| 813 | mNfcActivityManager.setOnNdefPushCompleteCallback(activity, null); |
Jeff Hamilton | 33ff240 | 2011-01-17 07:59:03 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 816 | /** |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 817 | * Enable NDEF Push feature. |
| 818 | * <p>This API is for the Settings application. |
| 819 | * @hide |
| 820 | */ |
| 821 | public boolean enableNdefPush() { |
Martijn Coenen | 6d74894 | 2011-07-21 09:34:25 +0200 | [diff] [blame] | 822 | try { |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 823 | return sService.enableNdefPush(); |
Martijn Coenen | 6d74894 | 2011-07-21 09:34:25 +0200 | [diff] [blame] | 824 | } catch (RemoteException e) { |
| 825 | attemptDeadServiceRecovery(e); |
| 826 | return false; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /** |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 831 | * Disable NDEF Push feature. |
| 832 | * <p>This API is for the Settings application. |
Martijn Coenen | 6d74894 | 2011-07-21 09:34:25 +0200 | [diff] [blame] | 833 | * @hide |
| 834 | */ |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 835 | public boolean disableNdefPush() { |
Martijn Coenen | 6d74894 | 2011-07-21 09:34:25 +0200 | [diff] [blame] | 836 | try { |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 837 | return sService.disableNdefPush(); |
Martijn Coenen | 6d74894 | 2011-07-21 09:34:25 +0200 | [diff] [blame] | 838 | } catch (RemoteException e) { |
| 839 | attemptDeadServiceRecovery(e); |
| 840 | return false; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /** |
Nick Pelly | cccf01d | 2011-10-31 14:49:40 -0700 | [diff] [blame] | 845 | * Return true if the NDEF Push (Android Beam) feature is enabled. |
| 846 | * <p>This function will return true only if both NFC is enabled, and the |
| 847 | * NDEF Push feature is enabled. |
| 848 | * <p>Note that if NFC is enabled but NDEF Push is disabled then this |
| 849 | * device can still <i>receive</i> NDEF messages, it just cannot send them. |
| 850 | * <p>Applications cannot directly toggle the NDEF Push feature, but they |
| 851 | * can request Settings UI allowing the user to toggle NDEF Push using |
| 852 | * <code>startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))</code> |
| 853 | * <p>Example usage in an Activity that requires NDEF Push: |
| 854 | * <p><pre> |
| 855 | * protected void onResume() { |
| 856 | * super.onResume(); |
| 857 | * if (!nfcAdapter.isEnabled()) { |
| 858 | * startActivity(new Intent(Settings.ACTION_NFC_SETTINGS)); |
| 859 | * } else if (!nfcAdapter.isNdefPushEnabled()) { |
| 860 | * startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS)); |
| 861 | * } |
| 862 | * } |
| 863 | * </pre> |
Martijn Coenen | 6d74894 | 2011-07-21 09:34:25 +0200 | [diff] [blame] | 864 | * |
Nick Pelly | cccf01d | 2011-10-31 14:49:40 -0700 | [diff] [blame] | 865 | * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 866 | * @return true if NDEF Push feature is enabled |
Martijn Coenen | 6d74894 | 2011-07-21 09:34:25 +0200 | [diff] [blame] | 867 | */ |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 868 | public boolean isNdefPushEnabled() { |
Martijn Coenen | 6d74894 | 2011-07-21 09:34:25 +0200 | [diff] [blame] | 869 | try { |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 870 | return sService.isNdefPushEnabled(); |
Martijn Coenen | 6d74894 | 2011-07-21 09:34:25 +0200 | [diff] [blame] | 871 | } catch (RemoteException e) { |
| 872 | attemptDeadServiceRecovery(e); |
| 873 | return false; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | /** |
Daniel Tomas | 9024564 | 2010-11-17 10:07:52 +0100 | [diff] [blame] | 878 | * @hide |
| 879 | */ |
Nick Pelly | 367f41f | 2011-03-08 11:43:30 -0800 | [diff] [blame] | 880 | public INfcAdapterExtras getNfcAdapterExtrasInterface() { |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 881 | if (mContext == null) { |
| 882 | throw new UnsupportedOperationException("You need a context on NfcAdapter to use the " |
| 883 | + " NFC extras APIs"); |
| 884 | } |
Daniel Tomas | 9024564 | 2010-11-17 10:07:52 +0100 | [diff] [blame] | 885 | try { |
Jeff Hamilton | bb951c8 | 2011-11-08 16:55:13 -0600 | [diff] [blame] | 886 | return sService.getNfcAdapterExtrasInterface(mContext.getPackageName()); |
Daniel Tomas | 9024564 | 2010-11-17 10:07:52 +0100 | [diff] [blame] | 887 | } catch (RemoteException e) { |
Nick Pelly | 367f41f | 2011-03-08 11:43:30 -0800 | [diff] [blame] | 888 | attemptDeadServiceRecovery(e); |
Daniel Tomas | 9024564 | 2010-11-17 10:07:52 +0100 | [diff] [blame] | 889 | return null; |
| 890 | } |
| 891 | } |
Nick Pelly | c84c89a | 2011-08-22 22:27:11 -0700 | [diff] [blame] | 892 | |
| 893 | void enforceResumed(Activity activity) { |
| 894 | if (!activity.isResumed()) { |
| 895 | throw new IllegalStateException("API cannot be called while activity is paused"); |
| 896 | } |
| 897 | } |
Nick Pelly | 590b73b | 2010-10-12 13:00:50 -0700 | [diff] [blame] | 898 | } |