diff options
author | Nick Pelly <npelly@google.com> | 2011-11-14 14:22:52 -0800 |
---|---|---|
committer | Nick Pelly <npelly@google.com> | 2011-11-14 14:24:37 -0800 |
commit | 678a7025ddb250de2a26cabc51402ff4e67752a8 (patch) | |
tree | 7b7243e4c3856282287e9d1267f832356ad59e58 /nfc-extras | |
parent | bb951c893973691554f49d2e725985125f866b27 (diff) | |
download | frameworks_base-678a7025ddb250de2a26cabc51402ff4e67752a8.zip frameworks_base-678a7025ddb250de2a26cabc51402ff4e67752a8.tar.gz frameworks_base-678a7025ddb250de2a26cabc51402ff4e67752a8.tar.bz2 |
Enforce 1-1 relationship between context and NfcAdapterExtras.
This fixes a bug where NfcExecutionEnvironment.close() would NPE
if you called it on a different EE to the one you opened. We now
always return the same EE in the same contet.
Change-Id: I949998dc2ee738527ee281cae46ae50ea6950a2c
Diffstat (limited to 'nfc-extras')
-rw-r--r-- | nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java index 62213de..9c87c22 100644 --- a/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java +++ b/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java @@ -16,6 +16,8 @@ package com.android.nfc_extras; +import java.util.HashMap; + import android.content.Context; import android.nfc.INfcAdapterExtras; import android.nfc.NfcAdapter; @@ -57,20 +59,22 @@ public final class NfcAdapterExtras { // protected by NfcAdapterExtras.class, and final after first construction, // except for attemptDeadServiceRecovery() when NFC crashes - we accept a // best effort recovery - private static NfcAdapter sAdapter; private static INfcAdapterExtras sService; private static final CardEmulationRoute ROUTE_OFF = 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 CardEmulationRoute mRouteOnWhenScreenOn; - final Context mContext; + private final NfcAdapter mAdapter; final String mPackageName; /** get service handles */ - private static void initService() { - final INfcAdapterExtras service = sAdapter.getNfcAdapterExtrasInterface(); + private static void initService(NfcAdapter adapter) { + final INfcAdapterExtras service = adapter.getNfcAdapterExtrasInterface(); if (service != null) { // Leave stale rather than receive a null value. sService = service; @@ -95,23 +99,20 @@ public final class NfcAdapterExtras { synchronized (NfcAdapterExtras.class) { if (sService == null) { - try { - sAdapter = adapter; - initService(); - } finally { - if (sService == null) { - sAdapter = null; - } - } + initService(adapter); } + 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) { - mContext = context.getApplicationContext(); - mPackageName = context.getPackageName(); + private NfcAdapterExtras(NfcAdapter adapter) { + mAdapter = adapter; + mPackageName = adapter.getContext().getPackageName(); mEmbeddedEe = new NfcExecutionEnvironment(this); mRouteOnWhenScreenOn = new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, mEmbeddedEe); @@ -160,8 +161,8 @@ public final class NfcAdapterExtras { */ void attemptDeadServiceRecovery(Exception e) { Log.e(TAG, "NFC Adapter Extras dead - attempting to recover"); - sAdapter.attemptDeadServiceRecovery(e); - initService(); + mAdapter.attemptDeadServiceRecovery(e); + initService(mAdapter); } INfcAdapterExtras getService() { |