From 4e06c094468d0e84b8f5a3ddd95ead850f4f22d9 Mon Sep 17 00:00:00 2001 From: "tedchoc@chromium.org" Date: Thu, 20 Feb 2014 20:05:50 +0000 Subject: Expose activity from WindowAndroid. Changes ActivityWindow to only keep a WeakReference to the Activity to avoid it preventing an Activity from being GC'd. BUG=341231 Review URL: https://codereview.chromium.org/171803002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@252360 0039d316-1c4b-4281-b951-d872f2087c98 --- .../chromium/ui/base/ActivityWindowAndroid.java | 31 +++++++++++++++++----- .../src/org/chromium/ui/base/WindowAndroid.java | 14 +++++++++- 2 files changed, 37 insertions(+), 8 deletions(-) (limited to 'ui/android') diff --git a/ui/android/java/src/org/chromium/ui/base/ActivityWindowAndroid.java b/ui/android/java/src/org/chromium/ui/base/ActivityWindowAndroid.java index 0d689a0..bba7fae 100644 --- a/ui/android/java/src/org/chromium/ui/base/ActivityWindowAndroid.java +++ b/ui/android/java/src/org/chromium/ui/base/ActivityWindowAndroid.java @@ -18,6 +18,7 @@ import android.view.View; import org.chromium.ui.UiUtils; import java.io.ByteArrayOutputStream; +import java.lang.ref.WeakReference; /** * The class provides the WindowAndroid's implementation which requires @@ -30,20 +31,23 @@ public class ActivityWindowAndroid extends WindowAndroid { private static final int REQUEST_CODE_RANGE_SIZE = 100; private static final String TAG = "ActivityWindowAndroid"; - private final Activity mActivity; + private final WeakReference mActivityRef; private int mNextRequestCode = 0; public ActivityWindowAndroid(Activity activity) { super(activity.getApplicationContext()); - mActivity = activity; + mActivityRef = new WeakReference(activity); } @Override public int showCancelableIntent(PendingIntent intent, IntentCallback callback, int errorId) { + Activity activity = mActivityRef.get(); + if (activity == null) return START_INTENT_FAILURE; + int requestCode = generateNextRequestCode(); try { - mActivity.startIntentSenderForResult( + activity.startIntentSenderForResult( intent.getIntentSender(), requestCode, new Intent(), 0, 0, 0); } catch (SendIntentException e) { return START_INTENT_FAILURE; @@ -55,10 +59,13 @@ public class ActivityWindowAndroid extends WindowAndroid { @Override public int showCancelableIntent(Intent intent, IntentCallback callback, int errorId) { + Activity activity = mActivityRef.get(); + if (activity == null) return START_INTENT_FAILURE; + int requestCode = generateNextRequestCode(); try { - mActivity.startActivityForResult(intent, requestCode); + activity.startActivityForResult(intent, requestCode); } catch (ActivityNotFoundException e) { return START_INTENT_FAILURE; } @@ -69,7 +76,9 @@ public class ActivityWindowAndroid extends WindowAndroid { @Override public void cancelIntent(int requestCode) { - mActivity.finishActivity(requestCode); + Activity activity = mActivityRef.get(); + if (activity == null) return; + activity.finishActivity(requestCode); } @Override @@ -94,7 +103,13 @@ public class ActivityWindowAndroid extends WindowAndroid { @Override @Deprecated public Context getContext() { - return mActivity; + return mActivityRef.get(); + } + + @Override + public WeakReference getActivity() { + // Return a new WeakReference to prevent clients from releasing our internal WeakReference. + return new WeakReference(mActivityRef.get()); } /** @@ -103,10 +118,12 @@ public class ActivityWindowAndroid extends WindowAndroid { */ @Override public byte[] grabSnapshot(int windowX, int windowY, int width, int height) { + Activity activity = mActivityRef.get(); + if (activity == null) return null; try { // Take a screenshot of the root activity view. This generally includes UI // controls such as the URL bar and OS windows such as the status bar. - View rootView = mActivity.findViewById(android.R.id.content).getRootView(); + View rootView = activity.findViewById(android.R.id.content).getRootView(); Bitmap bitmap = UiUtils.generateScaledScreenshot(rootView, 0, Bitmap.Config.ARGB_8888); if (bitmap == null) return null; diff --git a/ui/android/java/src/org/chromium/ui/base/WindowAndroid.java b/ui/android/java/src/org/chromium/ui/base/WindowAndroid.java index f7938cd..e87f144 100644 --- a/ui/android/java/src/org/chromium/ui/base/WindowAndroid.java +++ b/ui/android/java/src/org/chromium/ui/base/WindowAndroid.java @@ -4,6 +4,7 @@ package org.chromium.ui.base; +import android.app.Activity; import android.app.PendingIntent; import android.content.ContentResolver; import android.content.Context; @@ -16,6 +17,7 @@ import android.widget.Toast; import org.chromium.base.CalledByNative; import org.chromium.base.JNINamespace; +import java.lang.ref.WeakReference; import java.util.HashMap; /** @@ -156,7 +158,8 @@ public class WindowAndroid { } /** - * TODO(nileshagrawal): Stop returning Activity Context crbug.com/233440. + * TODO(tedchoc): Delete this method. Use getActivity() below. + * * @return Activity context, it could be null. Note, in most cases, you probably * just need Application Context returned by getApplicationContext(). * @see #getApplicationContext() @@ -167,6 +170,15 @@ public class WindowAndroid { } /** + * @return A reference to owning Activity. The returned WeakReference will never be null, but + * the contained Activity can be null (either if it has been garbage collected or if + * this is in the context of a WebView that was not created using an Activity). + */ + public WeakReference getActivity() { + return new WeakReference(null); + } + + /** * @return The application context for this activity. */ public Context getApplicationContext() { -- cgit v1.1