am 0c8d57d1: Merge "Fix Activity ref-leak in NFC dispatch API."

* commit '0c8d57d124d917721890b0aa4a152f9c9c7d317f':
  Fix Activity ref-leak in NFC dispatch API.
This commit is contained in:
Martijn Coenen
2011-09-02 12:52:03 -07:00
committed by Android Git Automerger
2 changed files with 21 additions and 3 deletions

View File

@@ -20,7 +20,7 @@ import android.app.Activity;
import android.os.RemoteException;
import android.util.Log;
import java.util.HashMap;
import java.util.WeakHashMap;
/**
* Manages NFC API's that are coupled to the life-cycle of an Activity.
@@ -38,7 +38,7 @@ public final class NfcActivityManager extends INdefPushCallback.Stub {
static final Boolean DBG = false;
final NfcAdapter mAdapter;
final HashMap<Activity, NfcActivityState> mNfcState; // contents protected by this
final WeakHashMap<Activity, NfcActivityState> mNfcState; // contents protected by this
final NfcEvent mDefaultEvent; // can re-use one NfcEvent because it just contains adapter
/**
@@ -60,7 +60,7 @@ public final class NfcActivityManager extends INdefPushCallback.Stub {
public NfcActivityManager(NfcAdapter adapter) {
mAdapter = adapter;
mNfcState = new HashMap<Activity, NfcActivityState>();
mNfcState = new WeakHashMap<Activity, NfcActivityState>();
mDefaultEvent = new NfcEvent(mAdapter);
}
@@ -88,6 +88,13 @@ public final class NfcActivityManager extends INdefPushCallback.Stub {
}
}
/**
* onDestroy hook from fragment attached to activity
*/
public void onDestroy(Activity activity) {
mNfcState.remove(activity);
}
public synchronized void setNdefPushMessage(Activity activity, NdefMessage message) {
NfcActivityState state = getOrCreateState(activity, message != null);
if (state == null || state.ndefMessage == message) {
@@ -214,4 +221,5 @@ public final class NfcActivityManager extends INdefPushCallback.Stub {
callback.onNdefPushComplete(mDefaultEvent);
}
}
}

View File

@@ -80,4 +80,14 @@ public final class NfcFragment extends Fragment {
sNfcActivityManager.onPause(getActivity());
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (sNfcActivityManager != null) {
sNfcActivityManager.onDestroy(getActivity());
}
}
}