Chromium Code Reviews| Index: ui/android/java/src/org/chromium/ui/base/ActivityWindowAndroid.java |
| 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 ad02b45884a08c9467fb8c7ead4a291f31a0a9e4..663c64454a8106619435c9d741bd7edbfa0383c1 100644 |
| --- a/ui/android/java/src/org/chromium/ui/base/ActivityWindowAndroid.java |
| +++ b/ui/android/java/src/org/chromium/ui/base/ActivityWindowAndroid.java |
| @@ -7,6 +7,7 @@ package org.chromium.ui.base; |
| import android.app.Activity; |
| import android.app.PendingIntent; |
| import android.content.ActivityNotFoundException; |
| +import android.content.Context; |
| import android.content.Intent; |
| import android.content.IntentSender.SendIntentException; |
| import android.content.SharedPreferences; |
| @@ -41,7 +42,6 @@ public class ActivityWindowAndroid |
| private static final String PERMISSION_QUERIED_KEY_PREFIX = "HasRequestedAndroidPermission::"; |
| - private final WeakReference<Activity> mActivityRef; |
| private final Handler mHandler; |
| private final SparseArray<PermissionCallback> mOutstandingPermissionRequests; |
| @@ -63,8 +63,7 @@ public class ActivityWindowAndroid |
| * @param listenToActivityState Whether to listen to activity state changes. |
| */ |
| public ActivityWindowAndroid(Activity activity, boolean listenToActivityState) { |
| - super(activity.getApplicationContext()); |
| - mActivityRef = new WeakReference<Activity>(activity); |
| + super(activity); |
| mHandler = new Handler(); |
| mOutstandingPermissionRequests = new SparseArray<PermissionCallback>(); |
| if (listenToActivityState) { |
| @@ -76,16 +75,16 @@ public class ActivityWindowAndroid |
| @Override |
| protected void registerKeyboardVisibilityCallbacks() { |
| - Activity activity = mActivityRef.get(); |
| + Activity activity = getActivity().get(); |
| if (activity == null) return; |
| View content = activity.findViewById(android.R.id.content); |
| - mIsKeyboardShowing = UiUtils.isKeyboardShowing(mActivityRef.get(), content); |
| + mIsKeyboardShowing = UiUtils.isKeyboardShowing(getActivity().get(), content); |
| content.addOnLayoutChangeListener(this); |
| } |
| @Override |
| protected void unregisterKeyboardVisibilityCallbacks() { |
| - Activity activity = mActivityRef.get(); |
| + Activity activity = getActivity().get(); |
| if (activity == null) return; |
| activity.findViewById(android.R.id.content).removeOnLayoutChangeListener(this); |
| } |
| @@ -93,7 +92,7 @@ public class ActivityWindowAndroid |
| @Override |
| public int showCancelableIntent( |
| PendingIntent intent, IntentCallback callback, Integer errorId) { |
| - Activity activity = mActivityRef.get(); |
| + Activity activity = getActivity().get(); |
| if (activity == null) return START_INTENT_FAILURE; |
| int requestCode = generateNextRequestCode(); |
| @@ -111,7 +110,7 @@ public class ActivityWindowAndroid |
| @Override |
| public int showCancelableIntent(Intent intent, IntentCallback callback, Integer errorId) { |
| - Activity activity = mActivityRef.get(); |
| + Activity activity = getActivity().get(); |
| if (activity == null) return START_INTENT_FAILURE; |
| int requestCode = generateNextRequestCode(); |
| @@ -128,7 +127,7 @@ public class ActivityWindowAndroid |
| @Override |
| public void cancelIntent(int requestCode) { |
| - Activity activity = mActivityRef.get(); |
| + Activity activity = getActivity().get(); |
| if (activity == null) return; |
| activity.finishActivity(requestCode); |
| } |
| @@ -190,7 +189,7 @@ public class ActivityWindowAndroid |
| */ |
| public boolean onRequestPermissionsResult(int requestCode, String[] permissions, |
| int[] grantResults) { |
| - Activity activity = mActivityRef.get(); |
| + Activity activity = getActivity().get(); |
| assert activity != null; |
| SharedPreferences.Editor editor = |
| @@ -209,8 +208,9 @@ public class ActivityWindowAndroid |
| @Override |
| public WeakReference<Activity> getActivity() { |
| - // Return a new WeakReference to prevent clients from releasing our internal WeakReference. |
| - return new WeakReference<Activity>(mActivityRef.get()); |
| + Context context = getContext().get(); |
| + if (context instanceof Activity) return new WeakReference<Activity>((Activity) context); |
|
Ted C
2015/11/11 00:19:45
I would just add a warning suppression and do the
boliu
2015/11/11 00:34:08
Hmm, not as simple as that. What if context is nul
Ted C
2015/11/11 00:44:25
We specifically don't care about the null case tho
boliu
2015/11/11 00:46:29
Sure. But in that case, should getActivity return
Ted C
2015/11/11 00:48:41
Definitely the latter.
But my original suggestion
boliu
2015/11/11 01:01:20
that sgtm
gsennton
2015/11/13 20:00:04
Done.
|
| + return null; // Should never happen |
| } |
| @Override |
| @@ -225,7 +225,7 @@ public class ActivityWindowAndroid |
| @Override |
| public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, |
| int oldTop, int oldRight, int oldBottom) { |
| - keyboardVisibilityPossiblyChanged(UiUtils.isKeyboardShowing(mActivityRef.get(), v)); |
| + keyboardVisibilityPossiblyChanged(UiUtils.isKeyboardShowing(getActivity().get(), v)); |
| } |
| private int generateNextRequestCode() { |
| @@ -251,7 +251,7 @@ public class ActivityWindowAndroid |
| public boolean canRequestPermission(String permission) { |
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false; |
| - Activity activity = mActivityRef.get(); |
| + Activity activity = getActivity().get(); |
| if (activity == null) return false; |
| if (isPermissionRevokedByPolicy(permission)) { |
| @@ -275,7 +275,7 @@ public class ActivityWindowAndroid |
| public boolean isPermissionRevokedByPolicy(String permission) { |
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false; |
| - Activity activity = mActivityRef.get(); |
| + Activity activity = getActivity().get(); |
| if (activity == null) return false; |
| return activity.getPackageManager().isPermissionRevokedByPolicy( |
| @@ -311,7 +311,7 @@ public class ActivityWindowAndroid |
| private boolean requestPermissionsInternal( |
| String[] permissions, PermissionCallback callback) { |
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false; |
| - Activity activity = mActivityRef.get(); |
| + Activity activity = getActivity().get(); |
| if (activity == null) return false; |
| int requestCode = generateNextRequestCode(); |