Merge "Fix Activity ref-leak in NFC dispatch API."
This commit is contained in:
committed by
Android (Google) Code Review
commit
0c8d57d124
@@ -20,7 +20,7 @@ import android.app.Activity;
|
|||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
import android.util.Log;
|
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.
|
* 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;
|
static final Boolean DBG = false;
|
||||||
|
|
||||||
final NfcAdapter mAdapter;
|
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
|
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) {
|
public NfcActivityManager(NfcAdapter adapter) {
|
||||||
mAdapter = adapter;
|
mAdapter = adapter;
|
||||||
mNfcState = new HashMap<Activity, NfcActivityState>();
|
mNfcState = new WeakHashMap<Activity, NfcActivityState>();
|
||||||
mDefaultEvent = new NfcEvent(mAdapter);
|
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) {
|
public synchronized void setNdefPushMessage(Activity activity, NdefMessage message) {
|
||||||
NfcActivityState state = getOrCreateState(activity, message != null);
|
NfcActivityState state = getOrCreateState(activity, message != null);
|
||||||
if (state == null || state.ndefMessage == message) {
|
if (state == null || state.ndefMessage == message) {
|
||||||
@@ -214,4 +221,5 @@ public final class NfcActivityManager extends INdefPushCallback.Stub {
|
|||||||
callback.onNdefPushComplete(mDefaultEvent);
|
callback.onNdefPushComplete(mDefaultEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,4 +80,14 @@ public final class NfcFragment extends Fragment {
|
|||||||
sNfcActivityManager.onPause(getActivity());
|
sNfcActivityManager.onPause(getActivity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
if (sNfcActivityManager != null) {
|
||||||
|
sNfcActivityManager.onDestroy(getActivity());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user