From d98dcab679ce477c3acba298feb8fc2302e69333 Mon Sep 17 00:00:00 2001 From: Jaewan Kim Date: Tue, 3 May 2016 02:52:18 +0900 Subject: [PATCH] Fix crash when device doesn't support picture-in-picture Bug: 28512987 Change-Id: Ic8287d70fed491d7493d443ba7d9633b7d9aaea9 --- core/java/android/app/Activity.java | 12 ++++++++++++ core/java/android/view/Window.java | 18 ++++++------------ .../android/internal/policy/PhoneWindow.java | 16 ++++++++++++++-- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index 0149952ae2cd4..b5490cf239eaa 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -2974,6 +2974,18 @@ public class Activity extends ContextThemeWrapper return ActivityManagerNative.getDefault().getActivityStackId(mToken); } + /** + * Puts the activity in picture-in-picture mode if the activity supports. + * @see android.R.attr#supportsPictureInPicture + * @hide + */ + @Override + public void enterPictureInPictureModeIfPossible() { + if (mActivityInfo.resizeMode == ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE) { + enterPictureInPictureMode(); + } + } + /** * Called to process key events. You can override this to intercept all * key events before they are dispatched to the window. Be sure to call diff --git a/core/java/android/view/Window.java b/core/java/android/view/Window.java index e598113bb3b1f..85c16b8bc80f2 100644 --- a/core/java/android/view/Window.java +++ b/core/java/android/view/Window.java @@ -593,6 +593,12 @@ public abstract class Window { */ void exitFreeformMode() throws RemoteException; + /** + * Puts the activity in picture-in-picture mode if the activity supports. + * @see android.R.attr#supportsPictureInPicture + */ + void enterPictureInPictureModeIfPossible(); + /** Returns the current stack Id for the window. */ int getWindowStackId() throws RemoteException; } @@ -1256,18 +1262,6 @@ public abstract class Window { return getDecorView().findViewById(id); } - /** - * Puts the activity in picture-in-picture mode. - * @see android.R.attr#supportsPictureInPicture - * @hide - */ - protected void enterPictureInPictureMode() { - try { - ActivityManagerNative.getDefault().enterPictureInPictureMode(mAppToken); - } catch (IllegalArgumentException|RemoteException e) { - } - } - /** * Convenience for * {@link #setContentView(View, android.view.ViewGroup.LayoutParams)} diff --git a/core/java/com/android/internal/policy/PhoneWindow.java b/core/java/com/android/internal/policy/PhoneWindow.java index 16198433d99f0..5bb89693e9f25 100644 --- a/core/java/com/android/internal/policy/PhoneWindow.java +++ b/core/java/com/android/internal/policy/PhoneWindow.java @@ -16,6 +16,7 @@ package com.android.internal.policy; +import static android.provider.Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES; import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; import static android.view.WindowManager.LayoutParams.*; @@ -80,6 +81,7 @@ import android.os.Parcel; import android.os.Parcelable; import android.os.RemoteException; import android.os.ServiceManager; +import android.provider.Settings; import android.transition.Scene; import android.transition.Transition; import android.transition.TransitionInflater; @@ -204,6 +206,10 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback { */ int mPanelChordingKey; + // This stores if the system supports Picture-in-Picture + // to see if KEYCODE_WINDOW should be handled here or not. + private boolean mSupportsPictureInPicture; + private ImageView mLeftIconView; private ImageView mRightIconView; @@ -313,6 +319,12 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback { // the token will not be updated as for a new window. getAttributes().token = preservedWindow.getAttributes().token; } + // Even though the device doesn't support picture-in-picture mode, + // an user can force using it through developer options. + boolean forceResizable = Settings.Global.getInt(context.getContentResolver(), + DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, 0) != 0; + mSupportsPictureInPicture = forceResizable || context.getPackageManager().hasSystemFeature( + PackageManager.FEATURE_PICTURE_IN_PICTURE); } @Override @@ -2004,8 +2016,8 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback { } case KeyEvent.KEYCODE_WINDOW: { - if (!event.isCanceled()) { - enterPictureInPictureMode(); + if (mSupportsPictureInPicture && !event.isCanceled()) { + getWindowControllerCallback().enterPictureInPictureModeIfPossible(); } return true; }