summaryrefslogtreecommitdiffstats
path: root/core/java/android/nfc/NfcAdapter.java
diff options
context:
space:
mode:
authorNick Pelly <npelly@google.com>2010-12-07 22:40:28 -0800
committerNick Pelly <npelly@google.com>2010-12-10 17:12:33 -0800
commit50b4d8f643f31b37e9872f562fb869059cf79c8a (patch)
tree6f866534d961355448a91f04ee5b3bec2b3b7dd5 /core/java/android/nfc/NfcAdapter.java
parenta218e01437c3c47c49ced7ab363bf98fe92cf1e3 (diff)
downloadframeworks_base-50b4d8f643f31b37e9872f562fb869059cf79c8a.zip
frameworks_base-50b4d8f643f31b37e9872f562fb869059cf79c8a.tar.gz
frameworks_base-50b4d8f643f31b37e9872f562fb869059cf79c8a.tar.bz2
Make getSystemService(NFC_SERVICE) the NFC entry point.
This gives NFC service a handle to the application context. Deprecate NfcAdapter.getDefaultAdapter(), it does not provide a context. Using this method will print a warning, and will later throw an exception if a method that requires a context is called. No 2.3 API's will fail, but new API's that do require a context might fail. Also add helper NfcAdapter.getDefaultAdapter(Context). Change-Id: I9a6378de4ef4b61ad922f8d53e64e2a1a1d5d60c
Diffstat (limited to 'core/java/android/nfc/NfcAdapter.java')
-rw-r--r--core/java/android/nfc/NfcAdapter.java127
1 files changed, 78 insertions, 49 deletions
diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java
index a1c22bf..d71fdd5 100644
--- a/core/java/android/nfc/NfcAdapter.java
+++ b/core/java/android/nfc/NfcAdapter.java
@@ -19,6 +19,7 @@ package android.nfc;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.app.ActivityThread;
+import android.content.Context;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.os.IBinder;
@@ -29,11 +30,12 @@ import android.util.Log;
/**
* Represents the device's local NFC adapter.
* <p>
- * Use the static {@link #getDefaultAdapter} method to get the default NFC
- * Adapter for this Android device. Most Android devices will have only one NFC
- * Adapter, and {@link #getDefaultAdapter} returns the singleton object.
+ * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC
+ * adapter for this Android device.
*/
public final class NfcAdapter {
+ private static final String TAG = "NFC";
+
/**
* Intent to start an activity when a tag is discovered.
*/
@@ -161,30 +163,18 @@ public final class NfcAdapter {
*/
private static final int DISCOVERY_MODE_CARD_EMULATION = 2;
- private static final String TAG = "NFC";
- // Both guarded by NfcAdapter.class:
+ // Guarded by NfcAdapter.class
private static boolean sIsInitialized = false;
- private static NfcAdapter sAdapter;
- // Final after construction, except for attemptDeadServiceRecovery()
- // when NFC crashes.
- // Not locked - we accept a best effort attempt when NFC crashes.
- /*package*/ INfcAdapter mService;
+ // Final after first constructor, except for
+ // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
+ // recovery
+ private static INfcAdapter sService;
- private NfcAdapter(INfcAdapter service) {
- mService = service;
- }
+ private final Context mContext;
/**
- * Returns the binder interface to the service.
- * @hide
- */
- public INfcAdapter getService() {
- return mService;
- }
-
- /**
* Helper to check if this device has FEATURE_NFC, but without using
* a context.
* Equivalent to
@@ -204,8 +194,27 @@ public final class NfcAdapter {
}
}
+ private static synchronized INfcAdapter setupService() {
+ if (!sIsInitialized) {
+ sIsInitialized = true;
+
+ /* is this device meant to have NFC */
+ if (!hasNfcFeature()) {
+ Log.v(TAG, "this device does not have NFC support");
+ return null;
+ }
+
+ sService = getServiceInterface();
+ if (sService == null) {
+ Log.e(TAG, "could not retrieve NFC service");
+ return null;
+ }
+ }
+ return sService;
+ }
+
/** get handle to NFC service interface */
- private static synchronized INfcAdapter getServiceInterface() {
+ private static INfcAdapter getServiceInterface() {
/* get a handle to NFC service */
IBinder b = ServiceManager.getService("nfc");
if (b == null) {
@@ -215,34 +224,54 @@ public final class NfcAdapter {
}
/**
+ * Helper to get the default NFC Adapter.
+ * <p>
+ * Most Android devices will only have one NFC Adapter (NFC Controller).
+ * <p>
+ * This helper is the equivalent of:
+ * <pre>{@code
+ * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
+ * NfcAdapter adapter = manager.getDefaultAdapter();
+ * }</pre>
+ * @param context the calling application's context
+ *
+ * @return the default NFC adapter, or null if no NFC adapter exists
+ */
+ public static NfcAdapter getDefaultAdapter(Context context) {
+ /* use getSystemService() instead of just instantiating to take
+ * advantage of the context's cached NfcManager & NfcAdapter */
+ NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
+ return manager.getDefaultAdapter();
+ }
+
+ /**
* Get a handle to the default NFC Adapter on this Android device.
* <p>
* Most Android devices will only have one NFC Adapter (NFC Controller).
*
* @return the default NFC adapter, or null if no NFC adapter exists
+ * @deprecated use {@link #getDefaultAdapter(Context)}
*/
+ @Deprecated
public static NfcAdapter getDefaultAdapter() {
- synchronized (NfcAdapter.class) {
- if (sIsInitialized) {
- return sAdapter;
- }
- sIsInitialized = true;
-
- /* is this device meant to have NFC */
- if (!hasNfcFeature()) {
- Log.v(TAG, "this device does not have NFC support");
- return null;
- }
-
- INfcAdapter service = getServiceInterface();
- if (service == null) {
- Log.e(TAG, "could not retrieve NFC service");
- return null;
- }
+ Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " +
+ "NfcAdapter.getDefaultAdapter(Context) instead", new Exception());
+ return new NfcAdapter(null);
+ }
- sAdapter = new NfcAdapter(service);
- return sAdapter;
+ /*package*/ NfcAdapter(Context context) {
+ if (setupService() == null) {
+ throw new UnsupportedOperationException();
}
+ mContext = context;
+ }
+
+ /**
+ * Returns the binder interface to the service.
+ * @hide
+ */
+ public INfcAdapter getService() {
+ return sService;
}
/**
@@ -256,9 +285,9 @@ public final class NfcAdapter {
Log.e(TAG, "could not retrieve NFC service during service recovery");
return;
}
- /* assigning to mService is not thread-safe, but this is best-effort code
+ /* assigning to sService is not thread-safe, but this is best-effort code
* and on a well-behaved system should never happen */
- mService = service;
+ sService = service;
return;
}
@@ -275,7 +304,7 @@ public final class NfcAdapter {
*/
public boolean isEnabled() {
try {
- return mService.isEnabled();
+ return sService.isEnabled();
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
return false;
@@ -292,7 +321,7 @@ public final class NfcAdapter {
*/
public boolean enable() {
try {
- return mService.enable();
+ return sService.enable();
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
return false;
@@ -311,7 +340,7 @@ public final class NfcAdapter {
*/
public boolean disable() {
try {
- return mService.disable();
+ return sService.disable();
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
return false;
@@ -338,7 +367,7 @@ public final class NfcAdapter {
*/
public void setLocalNdefMessage(NdefMessage message) {
try {
- mService.localSet(message);
+ sService.localSet(message);
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
}
@@ -353,7 +382,7 @@ public final class NfcAdapter {
*/
public NdefMessage getLocalNdefMessage() {
try {
- return mService.localGet();
+ return sService.localGet();
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
return null;
@@ -366,7 +395,7 @@ public final class NfcAdapter {
*/
public NfcSecureElement createNfcSecureElementConnection() {
try {
- return new NfcSecureElement(mService.getNfcSecureElementInterface());
+ return new NfcSecureElement(sService.getNfcSecureElementInterface());
} catch (RemoteException e) {
Log.e(TAG, "createNfcSecureElementConnection failed", e);
return null;