1616
1717package com .android .nfc_extras ;
1818
19+ import java .util .HashMap ;
20+
1921import android .content .Context ;
2022import android .nfc .INfcAdapterExtras ;
2123import android .nfc .NfcAdapter ;
@@ -57,20 +59,22 @@ public final class NfcAdapterExtras {
5759 // protected by NfcAdapterExtras.class, and final after first construction,
5860 // except for attemptDeadServiceRecovery() when NFC crashes - we accept a
5961 // best effort recovery
60- private static NfcAdapter sAdapter ;
6162 private static INfcAdapterExtras sService ;
6263 private static final CardEmulationRoute ROUTE_OFF =
6364 new CardEmulationRoute (CardEmulationRoute .ROUTE_OFF , null );
6465
66+ // contents protected by NfcAdapterExtras.class
67+ private static final HashMap <NfcAdapter , NfcAdapterExtras > sNfcExtras = new HashMap ();
68+
6569 private final NfcExecutionEnvironment mEmbeddedEe ;
6670 private final CardEmulationRoute mRouteOnWhenScreenOn ;
6771
68- final Context mContext ;
72+ private final NfcAdapter mAdapter ;
6973 final String mPackageName ;
7074
7175 /** get service handles */
72- private static void initService () {
73- final INfcAdapterExtras service = sAdapter .getNfcAdapterExtrasInterface ();
76+ private static void initService (NfcAdapter adapter ) {
77+ final INfcAdapterExtras service = adapter .getNfcAdapterExtrasInterface ();
7478 if (service != null ) {
7579 // Leave stale rather than receive a null value.
7680 sService = service ;
@@ -95,23 +99,20 @@ public static NfcAdapterExtras get(NfcAdapter adapter) {
9599
96100 synchronized (NfcAdapterExtras .class ) {
97101 if (sService == null ) {
98- try {
99- sAdapter = adapter ;
100- initService ();
101- } finally {
102- if (sService == null ) {
103- sAdapter = null ;
104- }
105- }
102+ initService (adapter );
106103 }
104+ NfcAdapterExtras extras = sNfcExtras .get (adapter );
105+ if (extras == null ) {
106+ extras = new NfcAdapterExtras (adapter );
107+ sNfcExtras .put (adapter , extras );
108+ }
109+ return extras ;
107110 }
108-
109- return new NfcAdapterExtras (context );
110111 }
111112
112- private NfcAdapterExtras (Context context ) {
113- mContext = context . getApplicationContext () ;
114- mPackageName = context .getPackageName ();
113+ private NfcAdapterExtras (NfcAdapter adapter ) {
114+ mAdapter = adapter ;
115+ mPackageName = adapter . getContext () .getPackageName ();
115116 mEmbeddedEe = new NfcExecutionEnvironment (this );
116117 mRouteOnWhenScreenOn = new CardEmulationRoute (CardEmulationRoute .ROUTE_ON_WHEN_SCREEN_ON ,
117118 mEmbeddedEe );
@@ -160,8 +161,8 @@ public CardEmulationRoute(int route, NfcExecutionEnvironment nfcEe) {
160161 */
161162 void attemptDeadServiceRecovery (Exception e ) {
162163 Log .e (TAG , "NFC Adapter Extras dead - attempting to recover" );
163- sAdapter .attemptDeadServiceRecovery (e );
164- initService ();
164+ mAdapter .attemptDeadServiceRecovery (e );
165+ initService (mAdapter );
165166 }
166167
167168 INfcAdapterExtras getService () {
0 commit comments