am a26cf301: Merge "Enforce 1-1 relationship between context and NfcAdapterExtras." into ics-mr1

* commit 'a26cf3011157eb94fa2766e76fa0096628c16c18':
  Enforce 1-1 relationship between context and NfcAdapterExtras.
This commit is contained in:
Nick Pelly
2011-11-14 19:40:46 -08:00
committed by Android Git Automerger

View File

@@ -16,6 +16,8 @@
package com.android.nfc_extras; package com.android.nfc_extras;
import java.util.HashMap;
import android.content.Context; import android.content.Context;
import android.nfc.INfcAdapterExtras; import android.nfc.INfcAdapterExtras;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
@@ -57,20 +59,22 @@ public final class NfcAdapterExtras {
// protected by NfcAdapterExtras.class, and final after first construction, // protected by NfcAdapterExtras.class, and final after first construction,
// except for attemptDeadServiceRecovery() when NFC crashes - we accept a // except for attemptDeadServiceRecovery() when NFC crashes - we accept a
// best effort recovery // best effort recovery
private static NfcAdapter sAdapter;
private static INfcAdapterExtras sService; private static INfcAdapterExtras sService;
private static final CardEmulationRoute ROUTE_OFF = private static final CardEmulationRoute ROUTE_OFF =
new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null); new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null);
// contents protected by NfcAdapterExtras.class
private static final HashMap<NfcAdapter, NfcAdapterExtras> sNfcExtras = new HashMap();
private final NfcExecutionEnvironment mEmbeddedEe; private final NfcExecutionEnvironment mEmbeddedEe;
private final CardEmulationRoute mRouteOnWhenScreenOn; private final CardEmulationRoute mRouteOnWhenScreenOn;
final Context mContext; private final NfcAdapter mAdapter;
final String mPackageName; final String mPackageName;
/** get service handles */ /** get service handles */
private static void initService() { private static void initService(NfcAdapter adapter) {
final INfcAdapterExtras service = sAdapter.getNfcAdapterExtrasInterface(); final INfcAdapterExtras service = adapter.getNfcAdapterExtrasInterface();
if (service != null) { if (service != null) {
// Leave stale rather than receive a null value. // Leave stale rather than receive a null value.
sService = service; sService = service;
@@ -95,23 +99,20 @@ public final class NfcAdapterExtras {
synchronized (NfcAdapterExtras.class) { synchronized (NfcAdapterExtras.class) {
if (sService == null) { if (sService == null) {
try { initService(adapter);
sAdapter = adapter;
initService();
} finally {
if (sService == null) {
sAdapter = null;
}
}
} }
NfcAdapterExtras extras = sNfcExtras.get(adapter);
if (extras == null) {
extras = new NfcAdapterExtras(adapter);
sNfcExtras.put(adapter, extras);
}
return extras;
} }
return new NfcAdapterExtras(context);
} }
private NfcAdapterExtras(Context context) { private NfcAdapterExtras(NfcAdapter adapter) {
mContext = context.getApplicationContext(); mAdapter = adapter;
mPackageName = context.getPackageName(); mPackageName = adapter.getContext().getPackageName();
mEmbeddedEe = new NfcExecutionEnvironment(this); mEmbeddedEe = new NfcExecutionEnvironment(this);
mRouteOnWhenScreenOn = new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, mRouteOnWhenScreenOn = new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON,
mEmbeddedEe); mEmbeddedEe);
@@ -160,8 +161,8 @@ public final class NfcAdapterExtras {
*/ */
void attemptDeadServiceRecovery(Exception e) { void attemptDeadServiceRecovery(Exception e) {
Log.e(TAG, "NFC Adapter Extras dead - attempting to recover"); Log.e(TAG, "NFC Adapter Extras dead - attempting to recover");
sAdapter.attemptDeadServiceRecovery(e); mAdapter.attemptDeadServiceRecovery(e);
initService(); initService(mAdapter);
} }
INfcAdapterExtras getService() { INfcAdapterExtras getService() {