blob: 53a0341f562aa66d75505b9d604653841724ff24 [file] [log] [blame]
Nick Pelly590b73b2010-10-12 13:00:50 -07001/*
Daniel Tomas90245642010-11-17 10:07:52 +01002 * 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 Pelly590b73b2010-10-12 13:00:50 -070015 */
16
17package android.nfc;
18
Jeff Hamiltonbb951c82011-11-08 16:55:13 -060019import java.util.HashMap;
20
Nick Pelly590b73b2010-10-12 13:00:50 -070021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Jeff Hamilton52d32032011-01-08 15:31:26 -060023import android.app.Activity;
Nick Pellyfdf90862010-10-18 11:19:41 -070024import android.app.ActivityThread;
Jeff Hamilton52d32032011-01-08 15:31:26 -060025import android.app.OnActivityPausedListener;
26import android.app.PendingIntent;
Nick Pelly50b4d8f2010-12-07 22:40:28 -080027import android.content.Context;
Jeff Hamilton52d32032011-01-08 15:31:26 -060028import android.content.IntentFilter;
Nick Pellyfdf90862010-10-18 11:19:41 -070029import android.content.pm.IPackageManager;
30import android.content.pm.PackageManager;
Jeff Hamilton28319c02011-02-09 17:26:47 +090031import android.nfc.tech.MifareClassic;
32import android.nfc.tech.Ndef;
33import android.nfc.tech.NfcA;
34import android.nfc.tech.NfcF;
Nick Pelly590b73b2010-10-12 13:00:50 -070035import android.os.IBinder;
36import android.os.RemoteException;
37import android.os.ServiceManager;
38import android.util.Log;
39
Nick Pelly590b73b2010-10-12 13:00:50 -070040/**
Nick Pelly74fe6c62011-02-02 22:37:40 -080041 * Represents the local NFC adapter.
Nick Pelly590b73b2010-10-12 13:00:50 -070042 * <p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -080043 * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC
44 * adapter for this Android device.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080045 *
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 Pelly590b73b2010-10-12 13:00:50 -070051 */
52public final class NfcAdapter {
Nick Pellyc84c89a2011-08-22 22:27:11 -070053 static final String TAG = "NFC";
Nick Pelly50b4d8f2010-12-07 22:40:28 -080054
Nick Pelly590b73b2010-10-12 13:00:50 -070055 /**
Jeff Hamilton641dd622010-12-02 09:16:22 -060056 * Intent to start an activity when a tag with NDEF payload is discovered.
Jeff Hamilton641dd622010-12-02 09:16:22 -060057 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090058 * <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 Pellyf003e262011-01-31 23:27:37 -080068 * {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started.
Jeff Hamilton641dd622010-12-02 09:16:22 -060069 */
70 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
71 public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED";
72
73 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +090074 * Intent to start an activity when a tag is discovered and activities are registered for the
75 * specific technologies on the tag.
Jeff Hamilton641dd622010-12-02 09:16:22 -060076 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090077 * <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 * &lt;activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"&gt;
82 * &lt;!-- Add a technology filter --&gt;
83 * &lt;intent-filter&gt;
84 * &lt;action android:name="android.nfc.action.TECH_DISCOVERED" /&gt;
85 * &lt;/intent-filter&gt;
Jeff Hamilton641dd622010-12-02 09:16:22 -060086 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090087 * &lt;meta-data android:name="android.nfc.action.TECH_DISCOVERED"
88 * android:resource="@xml/filter_nfc"
89 * /&gt;
90 * &lt;/activity&gt;
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 * &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
107 * &lt;!-- capture anything using NfcF --&gt;
108 * &lt;tech-list&gt;
109 * &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
110 * &lt;/tech-list&gt;
111 *
112 * &lt;!-- OR --&gt;
113 *
114 * &lt;!-- capture all MIFARE Classics with NDEF payloads --&gt;
115 * &lt;tech-list&gt;
116 * &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
117 * &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
118 * &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
119 * &lt;/tech-list&gt;
120 * &lt;/resources&gt;
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 Hamilton641dd622010-12-02 09:16:22 -0600127 */
128 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Nick Pellyf003e262011-01-31 23:27:37 -0800129 public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED";
Jeff Hamilton641dd622010-12-02 09:16:22 -0600130
131 /**
Nick Pelly11b075e2010-10-28 13:39:37 -0700132 * Intent to start an activity when a tag is discovered.
Jeff Hamilton28319c02011-02-09 17:26:47 +0900133 *
134 * <p>This intent will not be started when a tag is discovered if any activities respond to
Jason parks01425362011-05-24 02:57:37 -0700135 * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag.
Nick Pelly590b73b2010-10-12 13:00:50 -0700136 */
137 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
138 public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED";
139
140 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600141 * 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 Hamilton28319c02011-02-09 17:26:47 +0900147 * 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 Pelly590b73b2010-10-12 13:00:50 -0700150 */
151 public static final String EXTRA_TAG = "android.nfc.extra.TAG";
152
153 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900154 * 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 Pelly11b075e2010-10-28 13:39:37 -0700157 */
158 public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES";
159
160 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900161 * 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 Pelly11b075e2010-10-28 13:39:37 -0700164 */
165 public static final String EXTRA_ID = "android.nfc.extra.ID";
166
167 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700168 * 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 Fitzpatrickafb082d2010-10-18 14:02:58 -0700172 * @hide
173 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700174 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
175 public static final String ACTION_ADAPTER_STATE_CHANGED =
176 "android.nfc.action.ADAPTER_STATE_CHANGED";
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700177
178 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700179 * 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 Fitzpatrickafb082d2010-10-18 14:02:58 -0700185 * @hide
186 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700187 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 Fitzpatrickafb082d2010-10-18 14:02:58 -0700197
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800198 // Guarded by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700199 static boolean sIsInitialized = false;
Nick Pelly590b73b2010-10-12 13:00:50 -0700200
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800201 // Final after first constructor, except for
202 // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
203 // recovery
Nick Pellyc84c89a2011-08-22 22:27:11 -0700204 static INfcAdapter sService;
205 static INfcTag sTagService;
206
207 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600208 * The NfcAdapter object for each application context.
209 * There is a 1-1 relationship between application context and
210 * NfcAdapter object.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700211 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600212 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 Pellyc84c89a2011-08-22 22:27:11 -0700220
221 final NfcActivityManager mNfcActivityManager;
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600222 final Context mContext;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700223
224 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700225 * A callback to be invoked when the system successfully delivers your {@link NdefMessage}
226 * to another device.
227 * @see #setOnNdefPushCompleteCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700228 */
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 Main2d68a6b2011-09-26 22:59:38 -0700236 * @see #setNdefPushMessageCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700237 */
238 public void onNdefPushComplete(NfcEvent event);
239 }
240
241 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700242 * 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 Pellyc84c89a2011-08-22 22:27:11 -0700251 */
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 Pelly590b73b2010-10-12 13:00:50 -0700275
Nick Pelly590b73b2010-10-12 13:00:50 -0700276 /**
Nick Pellyfdf90862010-10-18 11:19:41 -0700277 * 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 Pellyc84c89a2011-08-22 22:27:11 -0700296 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600297 * Returns the NfcAdapter for application context,
298 * or throws if NFC is not available.
299 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700300 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600301 public static synchronized NfcAdapter getNfcAdapter(Context context) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800302 if (!sIsInitialized) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800303 /* is this device meant to have NFC */
304 if (!hasNfcFeature()) {
305 Log.v(TAG, "this device does not have NFC support");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700306 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800307 }
308
309 sService = getServiceInterface();
310 if (sService == null) {
311 Log.e(TAG, "could not retrieve NFC service");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700312 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800313 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100314 try {
315 sTagService = sService.getNfcTagInterface();
316 } catch (RemoteException e) {
317 Log.e(TAG, "could not retrieve NFC Tag service");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700318 throw new UnsupportedOperationException();
Nick Pelly3dd6c452011-01-10 18:14:41 +1100319 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600320
321 sIsInitialized = true;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800322 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600323 if (context == null) {
324 if (sNullContextNfcAdapter == null) {
325 sNullContextNfcAdapter = new NfcAdapter(null);
326 }
327 return sNullContextNfcAdapter;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700328 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600329 NfcAdapter adapter = sNfcAdapters.get(context);
330 if (adapter == null) {
331 adapter = new NfcAdapter(context);
332 sNfcAdapters.put(context, adapter);
333 }
334 return adapter;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800335 }
336
Nick Pelly6d55e132010-10-27 01:14:43 -0700337 /** get handle to NFC service interface */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800338 private static INfcAdapter getServiceInterface() {
Nick Pelly6d55e132010-10-27 01:14:43 -0700339 /* 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 Pellyfdf90862010-10-18 11:19:41 -0700347 /**
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800348 * 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 Hamiltonbb951c82011-11-08 16:55:13 -0600362 if (context == null) {
363 throw new IllegalArgumentException("context cannot be null");
364 }
365 context = context.getApplicationContext();
Nick Pellyb04cce02011-11-21 17:02:02 -0800366 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 Pelly50b4d8f2010-12-07 22:40:28 -0800371 NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Nick Pellya5193b242011-11-16 16:46:27 -0800372 if (manager == null) {
373 // NFC not available
374 return null;
375 }
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800376 return manager.getDefaultAdapter();
377 }
378
379 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600380 * 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 Pelly50b4d8f2010-12-07 22:40:28 -0800384 * @deprecated use {@link #getDefaultAdapter(Context)}
Nick Pellya356bf12011-12-13 15:36:31 -0800385 * @hide
Nick Pelly590b73b2010-10-12 13:00:50 -0700386 */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800387 @Deprecated
Nick Pelly590b73b2010-10-12 13:00:50 -0700388 public static NfcAdapter getDefaultAdapter() {
Nick Pellya356bf12011-12-13 15:36:31 -0800389 // 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 Pelly50b4d8f2010-12-07 22:40:28 -0800393 Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " +
394 "NfcAdapter.getDefaultAdapter(Context) instead", new Exception());
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600395
396 return NfcAdapter.getNfcAdapter(null);
397 }
398
399 NfcAdapter(Context context) {
400 mContext = context;
401 mNfcActivityManager = new NfcActivityManager(this);
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800402 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700403
Nick Pellyc84c89a2011-08-22 22:27:11 -0700404 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600405 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700406 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600407 public Context getContext() {
408 return mContext;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800409 }
410
411 /**
412 * Returns the binder interface to the service.
413 * @hide
414 */
415 public INfcAdapter getService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800416 isEnabled(); // NOP call to recover sService if it is stale
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800417 return sService;
Nick Pelly590b73b2010-10-12 13:00:50 -0700418 }
419
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600420 /**
Nick Pelly3dd6c452011-01-10 18:14:41 +1100421 * Returns the binder interface to the tag service.
422 * @hide
423 */
424 public INfcTag getTagService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800425 isEnabled(); // NOP call to recover sTagService if it is stale
Nick Pelly3dd6c452011-01-10 18:14:41 +1100426 return sTagService;
427 }
428
429 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600430 * NFC service dead - attempt best effort recovery
431 * @hide
432 */
433 public void attemptDeadServiceRecovery(Exception e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700434 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 Pelly3dd6c452011-01-10 18:14:41 +1100438 // nothing more can be done now, sService is still stale, we'll hit
439 // this recovery path again later
Nick Pelly6d55e132010-10-27 01:14:43 -0700440 return;
441 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100442 // assigning to sService is not thread-safe, but this is best-effort code
443 // and on a well-behaved system should never happen
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800444 sService = service;
Nick Pelly3dd6c452011-01-10 18:14:41 +1100445 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 Pelly6d55e132010-10-27 01:14:43 -0700453 return;
454 }
455
Nick Pelly590b73b2010-10-12 13:00:50 -0700456 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700457 * Return true if this NFC Adapter has any features enabled.
Nick Pelly590b73b2010-10-12 13:00:50 -0700458 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800459 * <p>If this method returns false, the NFC hardware is guaranteed not to
Nick Pellycccf01d2011-10-31 14:49:40 -0700460 * 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 Pelly74fe6c62011-02-02 22:37:40 -0800464 *
Nick Pellycccf01d2011-10-31 14:49:40 -0700465 * @see android.provider.Settings#ACTION_NFC_SETTINGS
Nick Pelly74fe6c62011-02-02 22:37:40 -0800466 * @return true if this NFC Adapter has any features enabled
Nick Pelly590b73b2010-10-12 13:00:50 -0700467 */
Nick Pelly7ea5c452010-10-20 18:39:11 -0700468 public boolean isEnabled() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700469 try {
Nick Pelly8d32a012011-08-09 07:03:49 -0700470 return sService.getState() == STATE_ON;
Nick Pelly590b73b2010-10-12 13:00:50 -0700471 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700472 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700473 return false;
474 }
475 }
476
477 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700478 * 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 Pelly7ea5c452010-10-20 18:39:11 -0700500 * Enable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700501 *
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 Fitzpatrickafb082d2010-10-18 14:02:58 -0700512 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700513 * @hide
514 */
Nick Pelly7ea5c452010-10-20 18:39:11 -0700515 public boolean enable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700516 try {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800517 return sService.enable();
Nick Pelly590b73b2010-10-12 13:00:50 -0700518 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700519 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700520 return false;
521 }
522 }
523
524 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700525 * Disable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700526 *
527 * <p>No NFC features will work after this call, and the hardware
Nick Pelly7ea5c452010-10-20 18:39:11 -0700528 * will not perform or respond to any NFC communication.
Nick Pelly8d32a012011-08-09 07:03:49 -0700529 *
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 Fitzpatrickafb082d2010-10-18 14:02:58 -0700539 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700540 * @hide
541 */
Nick Pelly7ea5c452010-10-20 18:39:11 -0700542 public boolean disable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700543 try {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800544 return sService.disable();
Nick Pelly590b73b2010-10-12 13:00:50 -0700545 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700546 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700547 return false;
548 }
549 }
550
Jeff Hamilton52d32032011-01-08 15:31:26 -0600551 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -0700552 * 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 Main2d68a6b2011-09-26 22:59:38 -0700566 * <p>At least one activity must be specified, and usually only one is necessary.
Nick Pelly82328bf2011-08-30 09:37:25 -0700567 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700568 * <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 Main2d68a6b2011-09-26 22:59:38 -0700571 * @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 Pellyc84c89a2011-08-22 22:27:11 -0700575 */
Nick Pelly82328bf2011-08-30 09:37:25 -0700576 public void setNdefPushMessage(NdefMessage message, Activity activity,
577 Activity ... activities) {
578 if (activity == null) {
579 throw new NullPointerException("activity cannot be null");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700580 }
Nick Pelly82328bf2011-08-30 09:37:25 -0700581 mNfcActivityManager.setNdefPushMessage(activity, message);
Nick Pellyc84c89a2011-08-22 22:27:11 -0700582 for (Activity a : activities) {
Nick Pelly82328bf2011-08-30 09:37:25 -0700583 if (a == null) {
584 throw new NullPointerException("activities cannot contain null");
585 }
Nick Pellyc84c89a2011-08-22 22:27:11 -0700586 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 Main2d68a6b2011-09-26 22:59:38 -0700605 * <p>At least one activity must be specified, and usually only one is necessary.
Nick Pelly82328bf2011-08-30 09:37:25 -0700606 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700607 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
608 *
609 * @param callback callback, or null to disable
Scott Main2d68a6b2011-09-26 22:59:38 -0700610 * @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 Pellyc84c89a2011-08-22 22:27:11 -0700614 */
Nick Pelly82328bf2011-08-30 09:37:25 -0700615 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
Nick Pellyc84c89a2011-08-22 22:27:11 -0700616 Activity ... activities) {
Nick Pelly82328bf2011-08-30 09:37:25 -0700617 if (activity == null) {
618 throw new NullPointerException("activity cannot be null");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700619 }
Nick Pelly82328bf2011-08-30 09:37:25 -0700620 mNfcActivityManager.setNdefPushMessageCallback(activity, callback);
Nick Pellyc84c89a2011-08-22 22:27:11 -0700621 for (Activity a : activities) {
Nick Pelly82328bf2011-08-30 09:37:25 -0700622 if (a == null) {
623 throw new NullPointerException("activities cannot contain null");
624 }
Nick Pellyc84c89a2011-08-22 22:27:11 -0700625 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 Pelly82328bf2011-08-30 09:37:25 -0700636 * <p>One or more activities must be specified.
637 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700638 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
639 *
640 * @param callback callback, or null to disable
Nick Pelly82328bf2011-08-30 09:37:25 -0700641 * @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 Pellyc84c89a2011-08-22 22:27:11 -0700643 */
644 public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback,
Nick Pelly82328bf2011-08-30 09:37:25 -0700645 Activity activity, Activity ... activities) {
646 if (activity == null) {
647 throw new NullPointerException("activity cannot be null");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700648 }
Nick Pelly82328bf2011-08-30 09:37:25 -0700649 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, callback);
Nick Pellyc84c89a2011-08-22 22:27:11 -0700650 for (Activity a : activities) {
Nick Pelly82328bf2011-08-30 09:37:25 -0700651 if (a == null) {
652 throw new NullPointerException("activities cannot contain null");
653 }
Nick Pellyc84c89a2011-08-22 22:27:11 -0700654 mNfcActivityManager.setOnNdefPushCompleteCallback(a, callback);
655 }
656 }
657
658 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -0800659 * Enable foreground dispatch to the given Activity.
Jeff Hamilton52d32032011-01-08 15:31:26 -0600660 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800661 * <p>This will give give priority to the foreground activity when
662 * dispatching a discovered {@link Tag} to an application.
663 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +0900664 * <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 Pelly74fe6c62011-02-02 22:37:40 -0800672 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +0900673 * <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 Pelly74fe6c62011-02-02 22:37:40 -0800676 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +0900677 * <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 Hamilton52d32032011-01-08 15:31:26 -0600681 *
Nick Pelly39cf3a42011-02-07 17:04:21 +0900682 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
683 *
Jeff Hamilton52d32032011-01-08 15:31:26 -0600684 * @param activity the Activity to dispatch to
685 * @param intent the PendingIntent to start for the dispatch
Jeff Hamilton9f20cd72011-01-23 12:14:42 -0600686 * @param filters the IntentFilters to override dispatching for, or null to always dispatch
Jeff Hamilton167d9e42011-02-09 12:27:01 +0900687 * @param techLists the tech lists used to perform matching for dispatching of the
688 * {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent
Nick Pelly74fe6c62011-02-02 22:37:40 -0800689 * @throws IllegalStateException if the Activity is not currently in the foreground
Jeff Hamilton52d32032011-01-08 15:31:26 -0600690 */
691 public void enableForegroundDispatch(Activity activity, PendingIntent intent,
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -0600692 IntentFilter[] filters, String[][] techLists) {
Jeff Hamilton9f20cd72011-01-23 12:14:42 -0600693 if (activity == null || intent == null) {
Jeff Hamilton52d32032011-01-08 15:31:26 -0600694 throw new NullPointerException();
695 }
696 if (!activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -0700697 throw new IllegalStateException("Foreground dispatch can only be enabled " +
Jeff Hamilton52d32032011-01-08 15:31:26 -0600698 "when your activity is resumed");
699 }
700 try {
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -0600701 TechListParcel parcel = null;
702 if (techLists != null && techLists.length > 0) {
703 parcel = new TechListParcel(techLists);
704 }
Jeff Hamilton52d32032011-01-08 15:31:26 -0600705 ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
Jeff Hamiltonce3224c2011-01-17 11:05:03 -0800706 mForegroundDispatchListener);
Nick Pellyc84c89a2011-08-22 22:27:11 -0700707 sService.setForegroundDispatch(intent, filters, parcel);
Jeff Hamilton52d32032011-01-08 15:31:26 -0600708 } catch (RemoteException e) {
709 attemptDeadServiceRecovery(e);
710 }
711 }
712
713 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -0800714 * Disable foreground dispatch to the given activity.
Jeff Hamilton33ff2402011-01-17 07:59:03 -0800715 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800716 * <p>After calling {@link #enableForegroundDispatch}, an activity
717 * must call this method before its {@link Activity#onPause} callback
718 * completes.
Jeff Hamilton52d32032011-01-08 15:31:26 -0600719 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -0800720 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -0800721 *
Nick Pelly39cf3a42011-02-07 17:04:21 +0900722 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
723 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800724 * @param activity the Activity to disable dispatch to
725 * @throws IllegalStateException if the Activity has already been paused
Jeff Hamilton52d32032011-01-08 15:31:26 -0600726 */
727 public void disableForegroundDispatch(Activity activity) {
Jeff Hamiltonce3224c2011-01-17 11:05:03 -0800728 ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,
729 mForegroundDispatchListener);
Jeff Hamilton52d32032011-01-08 15:31:26 -0600730 disableForegroundDispatchInternal(activity, false);
731 }
732
Jeff Hamiltonce3224c2011-01-17 11:05:03 -0800733 OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() {
Jeff Hamilton33ff2402011-01-17 07:59:03 -0800734 @Override
735 public void onPaused(Activity activity) {
736 disableForegroundDispatchInternal(activity, true);
737 }
Jeff Hamiltonce3224c2011-01-17 11:05:03 -0800738 };
Jeff Hamilton33ff2402011-01-17 07:59:03 -0800739
Jeff Hamilton52d32032011-01-08 15:31:26 -0600740 void disableForegroundDispatchInternal(Activity activity, boolean force) {
741 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -0700742 sService.setForegroundDispatch(null, null, null);
Jeff Hamilton52d32032011-01-08 15:31:26 -0600743 if (!force && !activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -0700744 throw new IllegalStateException("You must disable foreground dispatching " +
Jeff Hamilton52d32032011-01-08 15:31:26 -0600745 "while your activity is still resumed");
746 }
747 } catch (RemoteException e) {
748 attemptDeadServiceRecovery(e);
749 }
750 }
751
Nick Pelly590b73b2010-10-12 13:00:50 -0700752 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -0700753 * Enable NDEF message push over NFC while this Activity is in the foreground.
Nick Pelly74fe6c62011-02-02 22:37:40 -0800754 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700755 * <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 Pelly74fe6c62011-02-02 22:37:40 -0800768 *
769 * <p>This method must be called from the main thread.
Jeff Hamilton9f20cd72011-01-23 12:14:42 -0600770 *
Nick Pelly39cf3a42011-02-07 17:04:21 +0900771 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
772 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700773 * @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 Hamilton33ff2402011-01-17 07:59:03 -0800777 */
Nick Pellyc84c89a2011-08-22 22:27:11 -0700778 @Deprecated
779 public void enableForegroundNdefPush(Activity activity, NdefMessage message) {
780 if (activity == null || message == null) {
Jeff Hamilton33ff2402011-01-17 07:59:03 -0800781 throw new NullPointerException();
782 }
Nick Pellyc84c89a2011-08-22 22:27:11 -0700783 enforceResumed(activity);
784 mNfcActivityManager.setNdefPushMessage(activity, message);
Jason parks01425362011-05-24 02:57:37 -0700785 }
786
787 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -0800788 * Disable NDEF message push over P2P.
Jeff Hamilton33ff2402011-01-17 07:59:03 -0800789 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800790 * <p>After calling {@link #enableForegroundNdefPush}, an activity
791 * must call this method before its {@link Activity#onPause} callback
792 * completes.
Jeff Hamilton33ff2402011-01-17 07:59:03 -0800793 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700794 * <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 Hamilton33ff2402011-01-17 07:59:03 -0800798 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -0800799 *
Nick Pelly39cf3a42011-02-07 17:04:21 +0900800 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
801 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800802 * @param activity the Foreground activity
803 * @throws IllegalStateException if the Activity has already been paused
Nick Pellyc84c89a2011-08-22 22:27:11 -0700804 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -0800805 */
Jeff Hamiltonce3224c2011-01-17 11:05:03 -0800806 public void disableForegroundNdefPush(Activity activity) {
Nick Pellyc84c89a2011-08-22 22:27:11 -0700807 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 Hamilton33ff2402011-01-17 07:59:03 -0800814 }
815
Nick Pellyc84c89a2011-08-22 22:27:11 -0700816 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -0700817 * Enable NDEF Push feature.
818 * <p>This API is for the Settings application.
819 * @hide
820 */
821 public boolean enableNdefPush() {
Martijn Coenen6d748942011-07-21 09:34:25 +0200822 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -0700823 return sService.enableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +0200824 } catch (RemoteException e) {
825 attemptDeadServiceRecovery(e);
826 return false;
827 }
828 }
829
830 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -0700831 * Disable NDEF Push feature.
832 * <p>This API is for the Settings application.
Martijn Coenen6d748942011-07-21 09:34:25 +0200833 * @hide
834 */
Nick Pellyc84c89a2011-08-22 22:27:11 -0700835 public boolean disableNdefPush() {
Martijn Coenen6d748942011-07-21 09:34:25 +0200836 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -0700837 return sService.disableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +0200838 } catch (RemoteException e) {
839 attemptDeadServiceRecovery(e);
840 return false;
841 }
842 }
843
844 /**
Nick Pellycccf01d2011-10-31 14:49:40 -0700845 * 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 Coenen6d748942011-07-21 09:34:25 +0200864 *
Nick Pellycccf01d2011-10-31 14:49:40 -0700865 * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS
Nick Pellyc84c89a2011-08-22 22:27:11 -0700866 * @return true if NDEF Push feature is enabled
Martijn Coenen6d748942011-07-21 09:34:25 +0200867 */
Nick Pellyc84c89a2011-08-22 22:27:11 -0700868 public boolean isNdefPushEnabled() {
Martijn Coenen6d748942011-07-21 09:34:25 +0200869 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -0700870 return sService.isNdefPushEnabled();
Martijn Coenen6d748942011-07-21 09:34:25 +0200871 } catch (RemoteException e) {
872 attemptDeadServiceRecovery(e);
873 return false;
874 }
875 }
876
877 /**
Daniel Tomas90245642010-11-17 10:07:52 +0100878 * @hide
879 */
Nick Pelly367f41f2011-03-08 11:43:30 -0800880 public INfcAdapterExtras getNfcAdapterExtrasInterface() {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600881 if (mContext == null) {
882 throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
883 + " NFC extras APIs");
884 }
Daniel Tomas90245642010-11-17 10:07:52 +0100885 try {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600886 return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
Daniel Tomas90245642010-11-17 10:07:52 +0100887 } catch (RemoteException e) {
Nick Pelly367f41f2011-03-08 11:43:30 -0800888 attemptDeadServiceRecovery(e);
Daniel Tomas90245642010-11-17 10:07:52 +0100889 return null;
890 }
891 }
Nick Pellyc84c89a2011-08-22 22:27:11 -0700892
893 void enforceResumed(Activity activity) {
894 if (!activity.isResumed()) {
895 throw new IllegalStateException("API cannot be called while activity is paused");
896 }
897 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700898}