From 5b3f5ed7d9ffa02c2255a68e6a4be969f0116104 Mon Sep 17 00:00:00 2001 From: Yorke Lee Date: Wed, 30 Jul 2014 15:36:49 -0700 Subject: Fix activity leaks in InCallUI We were holding a reference to anonymous inner classes in CallCardPresenter in an static instance of ContactInfoCache, which was causing activity leaks. Refactor ContactInfoCacheCallback into a static inner class so that this fixes the problem. Bug: 16657866 Change-Id: I4da5cecd556f80fafd27f919b0aaa7f00b2a96f5 --- .../com/android/incallui/CallCardPresenter.java | 74 +++++++++++++++------- .../src/com/android/incallui/ContactInfoCache.java | 2 +- 2 files changed, 52 insertions(+), 24 deletions(-) (limited to 'InCallUI') diff --git a/InCallUI/src/com/android/incallui/CallCardPresenter.java b/InCallUI/src/com/android/incallui/CallCardPresenter.java index 7018db9d1..bb6edfb7a 100644 --- a/InCallUI/src/com/android/incallui/CallCardPresenter.java +++ b/InCallUI/src/com/android/incallui/CallCardPresenter.java @@ -41,6 +41,9 @@ import com.android.incallui.InCallPresenter.InCallEventListener; import com.android.incallui.InCallPresenter.InCallState; import com.android.incallui.InCallPresenter.InCallStateListener; import com.android.incallui.InCallPresenter.IncomingCallListener; + +import java.lang.ref.WeakReference; + import com.google.common.base.Preconditions; /** @@ -63,6 +66,33 @@ public class CallCardPresenter extends Presenter private Context mContext; private TelecommManager mTelecommManager; + public static class ContactLookupCallback implements ContactInfoCacheCallback { + private final WeakReference mCallCardPresenter; + private final boolean mIsPrimary; + + public ContactLookupCallback(CallCardPresenter callCardPresenter, boolean isPrimary) { + mCallCardPresenter = new WeakReference(callCardPresenter); + mIsPrimary = isPrimary; + } + + @Override + public void onContactInfoComplete(String callId, ContactCacheEntry entry) { + CallCardPresenter presenter = mCallCardPresenter.get(); + if (presenter != null) { + presenter.onContactInfoComplete(callId, entry, mIsPrimary); + } + } + + @Override + public void onImageLoadComplete(String callId, ContactCacheEntry entry) { + CallCardPresenter presenter = mCallCardPresenter.get(); + if (presenter != null) { + presenter.onImageLoadComplete(callId, entry); + } + } + + } + public CallCardPresenter() { // create the call timer mCallTimer = new CallTimer(new Runnable() { @@ -347,30 +377,28 @@ public class CallCardPresenter extends Presenter boolean isIncoming) { final ContactInfoCache cache = ContactInfoCache.getInstance(mContext); - cache.findInfo(call, isIncoming, new ContactInfoCacheCallback() { - @Override - public void onContactInfoComplete(String callId, ContactCacheEntry entry) { - updateContactEntry(entry, isPrimary, false); - if (entry.name != null) { - Log.d(TAG, "Contact found: " + entry); - } - if (entry.contactUri != null) { - CallerInfoUtils.sendViewNotification(mContext, entry.contactUri); - } - } + cache.findInfo(call, isIncoming, new ContactLookupCallback(this, isPrimary)); + } - @Override - public void onImageLoadComplete(String callId, ContactCacheEntry entry) { - if (getUi() == null) { - return; - } - if (entry.photo != null) { - if (mPrimary != null && callId.equals(mPrimary.getId())) { - getUi().setPrimaryImage(entry.photo); - } - } - } - }); + private void onContactInfoComplete(String callId, ContactCacheEntry entry, boolean isPrimary) { + updateContactEntry(entry, isPrimary, false); + if (entry.name != null) { + Log.d(TAG, "Contact found: " + entry); + } + if (entry.contactUri != null) { + CallerInfoUtils.sendViewNotification(mContext, entry.contactUri); + } + } + + private void onImageLoadComplete(String callId, ContactCacheEntry entry) { + if (getUi() == null) { + return; + } + if (entry.photo != null) { + if (mPrimary != null && callId.equals(mPrimary.getId())) { + getUi().setPrimaryImage(entry.photo); + } + } } private static boolean isConference(Call call) { diff --git a/InCallUI/src/com/android/incallui/ContactInfoCache.java b/InCallUI/src/com/android/incallui/ContactInfoCache.java index ba1dca706..d02c8c837 100644 --- a/InCallUI/src/com/android/incallui/ContactInfoCache.java +++ b/InCallUI/src/com/android/incallui/ContactInfoCache.java @@ -59,7 +59,7 @@ public class ContactInfoCache implements ContactsAsyncHelper.OnImageLoadComplete public static synchronized ContactInfoCache getInstance(Context mContext) { if (sCache == null) { - sCache = new ContactInfoCache(mContext); + sCache = new ContactInfoCache(mContext.getApplicationContext()); } return sCache; } -- cgit v1.2.3