Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.pip; |
| 18 | |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
| 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
| 21 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY; |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 22 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
| 23 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 24 | import static com.android.systemui.pip.PipAnimationController.ANIM_TYPE_ALPHA; |
| 25 | import static com.android.systemui.pip.PipAnimationController.ANIM_TYPE_BOUNDS; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 26 | import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_NONE; |
| 27 | import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_SAME; |
| 28 | import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_TO_FULLSCREEN; |
| 29 | import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_TO_PIP; |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 30 | import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_TO_SPLIT_SCREEN; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 31 | import static com.android.systemui.pip.PipAnimationController.isInPipDirection; |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 32 | import static com.android.systemui.pip.PipAnimationController.isOutPipDirection; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 33 | |
| 34 | import android.annotation.NonNull; |
| 35 | import android.annotation.Nullable; |
| 36 | import android.app.ActivityManager; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 37 | import android.app.ActivityTaskManager; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 38 | import android.app.PictureInPictureParams; |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 39 | import android.content.ComponentName; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 40 | import android.content.Context; |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 41 | import android.content.pm.ActivityInfo; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 42 | import android.content.res.Configuration; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 43 | import android.graphics.Rect; |
| 44 | import android.os.Handler; |
Hongwei Wang | fbc25fe | 2020-03-16 11:59:28 -0700 | [diff] [blame] | 45 | import android.os.IBinder; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 46 | import android.os.Looper; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 47 | import android.os.RemoteException; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 48 | import android.util.Log; |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 49 | import android.util.Size; |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 50 | import android.view.SurfaceControl; |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 51 | import android.window.TaskOrganizer; |
| 52 | import android.window.WindowContainerToken; |
Wale Ogunwale | 5794658 | 2020-03-21 14:29:07 -0700 | [diff] [blame] | 53 | import android.window.WindowContainerTransaction; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 54 | import android.window.WindowContainerTransactionCallback; |
Wale Ogunwale | 568f9f41 | 2020-03-21 22:27:35 -0700 | [diff] [blame] | 55 | import android.window.WindowOrganizer; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 56 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 57 | import com.android.internal.os.SomeArgs; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 58 | import com.android.systemui.R; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 59 | import com.android.systemui.pip.phone.PipUpdateThread; |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 60 | import com.android.systemui.stackdivider.Divider; |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 61 | import com.android.systemui.wm.DisplayController; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 62 | |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 63 | import java.io.PrintWriter; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 64 | import java.util.ArrayList; |
Hongwei Wang | fbc25fe | 2020-03-16 11:59:28 -0700 | [diff] [blame] | 65 | import java.util.HashMap; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 66 | import java.util.List; |
Hongwei Wang | fbc25fe | 2020-03-16 11:59:28 -0700 | [diff] [blame] | 67 | import java.util.Map; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 68 | import java.util.Objects; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 69 | import java.util.function.Consumer; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 70 | |
Ben Lin | 6189fa4 | 2020-04-29 14:59:16 -0700 | [diff] [blame] | 71 | import javax.inject.Inject; |
| 72 | import javax.inject.Singleton; |
| 73 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Manages PiP tasks such as resize and offset. |
| 76 | * |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 77 | * This class listens on {@link TaskOrganizer} callbacks for windowing mode change |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 78 | * both to and from PiP and issues corresponding animation if applicable. |
| 79 | * Normally, we apply series of {@link SurfaceControl.Transaction} when the animator is running |
| 80 | * and files a final {@link WindowContainerTransaction} at the end of the transition. |
| 81 | * |
| 82 | * This class is also responsible for general resize/offset PiP operations within SysUI component, |
| 83 | * see also {@link com.android.systemui.pip.phone.PipMotionHelper}. |
| 84 | */ |
Ben Lin | 6189fa4 | 2020-04-29 14:59:16 -0700 | [diff] [blame] | 85 | @Singleton |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 86 | public class PipTaskOrganizer extends TaskOrganizer implements |
| 87 | DisplayController.OnDisplaysChangedListener { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 88 | private static final String TAG = PipTaskOrganizer.class.getSimpleName(); |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 89 | private static final boolean DEBUG = false; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 90 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 91 | private static final int MSG_RESIZE_IMMEDIATE = 1; |
| 92 | private static final int MSG_RESIZE_ANIMATE = 2; |
| 93 | private static final int MSG_OFFSET_ANIMATE = 3; |
| 94 | private static final int MSG_FINISH_RESIZE = 4; |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 95 | private static final int MSG_RESIZE_USER = 5; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 96 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 97 | private final Handler mMainHandler; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 98 | private final Handler mUpdateHandler; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 99 | private final PipBoundsHandler mPipBoundsHandler; |
| 100 | private final PipAnimationController mPipAnimationController; |
| 101 | private final List<PipTransitionCallback> mPipTransitionCallbacks = new ArrayList<>(); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 102 | private final Rect mLastReportedBounds = new Rect(); |
Ben Lin | ede9a60 | 2020-02-26 12:16:09 -0800 | [diff] [blame] | 103 | private final int mEnterExitAnimationDuration; |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 104 | private final PipSurfaceTransactionHelper mSurfaceTransactionHelper; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 105 | private final Map<IBinder, Configuration> mInitialState = new HashMap<>(); |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 106 | private final Divider mSplitDivider; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 107 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 108 | // These callbacks are called on the update thread |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 109 | private final PipAnimationController.PipAnimationCallback mPipAnimationCallback = |
| 110 | new PipAnimationController.PipAnimationCallback() { |
| 111 | @Override |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 112 | public void onPipAnimationStart(PipAnimationController.PipTransitionAnimator animator) { |
Hongwei Wang | 3d11e1f | 2020-05-28 15:48:06 -0700 | [diff] [blame] | 113 | sendOnPipTransitionStarted(animator.getTransitionDirection()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 117 | public void onPipAnimationEnd(SurfaceControl.Transaction tx, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 118 | PipAnimationController.PipTransitionAnimator animator) { |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 119 | finishResize(tx, animator.getDestinationBounds(), animator.getTransitionDirection(), |
| 120 | animator.getAnimationType()); |
Hongwei Wang | 3d11e1f | 2020-05-28 15:48:06 -0700 | [diff] [blame] | 121 | sendOnPipTransitionFinished(animator.getTransitionDirection()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 125 | public void onPipAnimationCancel(PipAnimationController.PipTransitionAnimator animator) { |
Hongwei Wang | 3d11e1f | 2020-05-28 15:48:06 -0700 | [diff] [blame] | 126 | sendOnPipTransitionCancelled(animator.getTransitionDirection()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 127 | } |
| 128 | }; |
| 129 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 130 | @SuppressWarnings("unchecked") |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 131 | private final Handler.Callback mUpdateCallbacks = (msg) -> { |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 132 | SomeArgs args = (SomeArgs) msg.obj; |
| 133 | Consumer<Rect> updateBoundsCallback = (Consumer<Rect>) args.arg1; |
| 134 | switch (msg.what) { |
| 135 | case MSG_RESIZE_IMMEDIATE: { |
| 136 | Rect toBounds = (Rect) args.arg2; |
| 137 | resizePip(toBounds); |
| 138 | if (updateBoundsCallback != null) { |
| 139 | updateBoundsCallback.accept(toBounds); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 140 | } |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 141 | break; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 142 | } |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 143 | case MSG_RESIZE_ANIMATE: { |
| 144 | Rect currentBounds = (Rect) args.arg2; |
| 145 | Rect toBounds = (Rect) args.arg3; |
| 146 | int duration = args.argi2; |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 147 | animateResizePip(currentBounds, toBounds, args.argi1 /* direction */, duration); |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 148 | if (updateBoundsCallback != null) { |
| 149 | updateBoundsCallback.accept(toBounds); |
| 150 | } |
| 151 | break; |
| 152 | } |
| 153 | case MSG_OFFSET_ANIMATE: { |
| 154 | Rect originalBounds = (Rect) args.arg2; |
| 155 | final int offset = args.argi1; |
| 156 | final int duration = args.argi2; |
| 157 | offsetPip(originalBounds, 0 /* xOffset */, offset, duration); |
| 158 | Rect toBounds = new Rect(originalBounds); |
| 159 | toBounds.offset(0, offset); |
| 160 | if (updateBoundsCallback != null) { |
| 161 | updateBoundsCallback.accept(toBounds); |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | case MSG_FINISH_RESIZE: { |
| 166 | SurfaceControl.Transaction tx = (SurfaceControl.Transaction) args.arg2; |
| 167 | Rect toBounds = (Rect) args.arg3; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 168 | finishResize(tx, toBounds, args.argi1 /* direction */, -1); |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 169 | if (updateBoundsCallback != null) { |
| 170 | updateBoundsCallback.accept(toBounds); |
| 171 | } |
| 172 | break; |
| 173 | } |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 174 | case MSG_RESIZE_USER: { |
| 175 | Rect startBounds = (Rect) args.arg2; |
| 176 | Rect toBounds = (Rect) args.arg3; |
| 177 | userResizePip(startBounds, toBounds); |
| 178 | break; |
| 179 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 180 | } |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 181 | args.recycle(); |
| 182 | return true; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 183 | }; |
| 184 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 185 | private ActivityManager.RunningTaskInfo mTaskInfo; |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 186 | private WindowContainerToken mToken; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 187 | private SurfaceControl mLeash; |
| 188 | private boolean mInPip; |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 189 | private boolean mExitingPip; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 190 | private @PipAnimationController.AnimationType int mOneShotAnimationType = ANIM_TYPE_BOUNDS; |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 191 | private PipSurfaceTransactionHelper.SurfaceControlTransactionFactory |
| 192 | mSurfaceControlTransactionFactory; |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 193 | private PictureInPictureParams mPictureInPictureParams; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 194 | |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 195 | /** |
| 196 | * If set to {@code true}, the entering animation will be skipped and we will wait for |
| 197 | * {@link #onFixedRotationFinished(int)} callback to actually enter PiP. |
| 198 | */ |
| 199 | private boolean mShouldDeferEnteringPip; |
| 200 | |
Ben Lin | 6189fa4 | 2020-04-29 14:59:16 -0700 | [diff] [blame] | 201 | @Inject |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 202 | public PipTaskOrganizer(Context context, @NonNull PipBoundsHandler boundsHandler, |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 203 | @NonNull PipSurfaceTransactionHelper surfaceTransactionHelper, |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 204 | @Nullable Divider divider, |
jorgegil@google.com | 0c449ec | 2020-06-16 16:30:07 -0700 | [diff] [blame] | 205 | @NonNull DisplayController displayController, |
| 206 | @NonNull PipAnimationController pipAnimationController) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 207 | mMainHandler = new Handler(Looper.getMainLooper()); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 208 | mUpdateHandler = new Handler(PipUpdateThread.get().getLooper(), mUpdateCallbacks); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 209 | mPipBoundsHandler = boundsHandler; |
Ben Lin | ede9a60 | 2020-02-26 12:16:09 -0800 | [diff] [blame] | 210 | mEnterExitAnimationDuration = context.getResources() |
| 211 | .getInteger(R.integer.config_pipResizeAnimationDuration); |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 212 | mSurfaceTransactionHelper = surfaceTransactionHelper; |
jorgegil@google.com | 0c449ec | 2020-06-16 16:30:07 -0700 | [diff] [blame] | 213 | mPipAnimationController = pipAnimationController; |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 214 | mSurfaceControlTransactionFactory = SurfaceControl.Transaction::new; |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 215 | mSplitDivider = divider; |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 216 | displayController.addDisplayWindowListener(this); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 219 | public Handler getUpdateHandler() { |
| 220 | return mUpdateHandler; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 223 | public Rect getLastReportedBounds() { |
| 224 | return new Rect(mLastReportedBounds); |
| 225 | } |
| 226 | |
Joshua Tsuji | 0d4cbeb | 2020-05-01 12:45:41 -0400 | [diff] [blame] | 227 | public boolean isInPip() { |
| 228 | return mInPip; |
| 229 | } |
| 230 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 231 | /** |
| 232 | * Registers {@link PipTransitionCallback} to receive transition callbacks. |
| 233 | */ |
| 234 | public void registerPipTransitionCallback(PipTransitionCallback callback) { |
| 235 | mPipTransitionCallbacks.add(callback); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Sets the preferred animation type for one time. |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 240 | * This is typically used to set the animation type to |
| 241 | * {@link PipAnimationController#ANIM_TYPE_ALPHA}. |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 242 | */ |
| 243 | public void setOneShotAnimationType(@PipAnimationController.AnimationType int animationType) { |
| 244 | mOneShotAnimationType = animationType; |
| 245 | } |
| 246 | |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 247 | /** |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 248 | * Expands PiP to the previous bounds, this is done in two phases using |
| 249 | * {@link WindowContainerTransaction} |
| 250 | * - setActivityWindowingMode to either fullscreen or split-secondary at beginning of the |
| 251 | * transaction. without changing the windowing mode of the Task itself. This makes sure the |
| 252 | * activity render it's final configuration while the Task is still in PiP. |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 253 | * - setWindowingMode to undefined at the end of transition |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 254 | * @param animationDurationMs duration in millisecond for the exiting PiP transition |
| 255 | */ |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 256 | public void exitPip(int animationDurationMs) { |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 257 | if (!mInPip || mExitingPip || mToken == null) { |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 258 | Log.wtf(TAG, "Not allowed to exitPip in current state" |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 259 | + " mInPip=" + mInPip + " mExitingPip=" + mExitingPip + " mToken=" + mToken); |
Hongwei Wang | 1273c95 | 2020-04-30 13:28:29 -0700 | [diff] [blame] | 260 | return; |
| 261 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 262 | |
| 263 | final Configuration initialConfig = mInitialState.remove(mToken.asBinder()); |
| 264 | final boolean orientationDiffers = initialConfig.windowConfiguration.getRotation() |
| 265 | != mPipBoundsHandler.getDisplayRotation(); |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 266 | final WindowContainerTransaction wct = new WindowContainerTransaction(); |
Hongwei Wang | 3d11e1f | 2020-05-28 15:48:06 -0700 | [diff] [blame] | 267 | final Rect destinationBounds = initialConfig.windowConfiguration.getBounds(); |
| 268 | final int direction = syncWithSplitScreenBounds(destinationBounds) |
| 269 | ? TRANSITION_DIRECTION_TO_SPLIT_SCREEN |
| 270 | : TRANSITION_DIRECTION_TO_FULLSCREEN; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 271 | if (orientationDiffers) { |
Hongwei Wang | 3d11e1f | 2020-05-28 15:48:06 -0700 | [diff] [blame] | 272 | // Send started callback though animation is ignored. |
| 273 | sendOnPipTransitionStarted(direction); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 274 | // Don't bother doing an animation if the display rotation differs or if it's in |
| 275 | // a non-supported windowing mode |
Hongwei Wang | 6ef225e | 2020-06-23 11:16:05 -0700 | [diff] [blame] | 276 | applyWindowingModeChangeOnExit(wct, direction); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 277 | WindowOrganizer.applyTransaction(wct); |
Hongwei Wang | 3d11e1f | 2020-05-28 15:48:06 -0700 | [diff] [blame] | 278 | // Send finished callback though animation is ignored. |
| 279 | sendOnPipTransitionFinished(direction); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 280 | mInPip = false; |
| 281 | } else { |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 282 | final SurfaceControl.Transaction tx = |
| 283 | mSurfaceControlTransactionFactory.getTransaction(); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 284 | mSurfaceTransactionHelper.scale(tx, mLeash, destinationBounds, |
| 285 | mLastReportedBounds); |
| 286 | tx.setWindowCrop(mLeash, destinationBounds.width(), destinationBounds.height()); |
| 287 | wct.setActivityWindowingMode(mToken, direction == TRANSITION_DIRECTION_TO_SPLIT_SCREEN |
| 288 | ? WINDOWING_MODE_SPLIT_SCREEN_SECONDARY |
| 289 | : WINDOWING_MODE_FULLSCREEN); |
| 290 | wct.setBounds(mToken, destinationBounds); |
| 291 | wct.setBoundsChangeTransaction(mToken, tx); |
| 292 | applySyncTransaction(wct, new WindowContainerTransactionCallback() { |
| 293 | @Override |
| 294 | public void onTransactionReady(int id, SurfaceControl.Transaction t) { |
| 295 | t.apply(); |
| 296 | scheduleAnimateResizePip(mLastReportedBounds, destinationBounds, |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 297 | direction, animationDurationMs, null /* updateBoundsCallback */); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 298 | mInPip = false; |
| 299 | } |
| 300 | }); |
| 301 | } |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 302 | mExitingPip = true; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Hongwei Wang | 6ef225e | 2020-06-23 11:16:05 -0700 | [diff] [blame] | 305 | private void applyWindowingModeChangeOnExit(WindowContainerTransaction wct, int direction) { |
| 306 | // Reset the final windowing mode. |
| 307 | wct.setWindowingMode(mToken, getOutPipWindowingMode()); |
| 308 | // Simply reset the activity mode set prior to the animation running. |
| 309 | wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
| 310 | if (mSplitDivider != null && direction == TRANSITION_DIRECTION_TO_SPLIT_SCREEN) { |
| 311 | wct.reparent(mToken, mSplitDivider.getSecondaryRoot(), true /* onTop */); |
| 312 | } |
| 313 | } |
| 314 | |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 315 | /** |
| 316 | * Removes PiP immediately. |
| 317 | */ |
| 318 | public void removePip() { |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 319 | if (!mInPip || mExitingPip || mToken == null) { |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 320 | Log.wtf(TAG, "Not allowed to removePip in current state" |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 321 | + " mInPip=" + mInPip + " mExitingPip=" + mExitingPip + " mToken=" + mToken); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | getUpdateHandler().post(() -> { |
| 325 | try { |
Winson Chung | 9bd523d | 2020-05-29 11:09:52 -0700 | [diff] [blame] | 326 | // Reset the task bounds first to ensure the activity configuration is reset as well |
| 327 | final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 328 | wct.setBounds(mToken, null); |
| 329 | WindowOrganizer.applyTransaction(wct); |
| 330 | |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 331 | ActivityTaskManager.getService().removeStacksInWindowingModes( |
| 332 | new int[]{ WINDOWING_MODE_PINNED }); |
| 333 | } catch (RemoteException e) { |
| 334 | Log.e(TAG, "Failed to remove PiP", e); |
| 335 | } |
| 336 | }); |
| 337 | mInitialState.remove(mToken.asBinder()); |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 338 | mExitingPip = true; |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 341 | @Override |
chaviw | 7de5000 | 2020-04-27 12:33:30 -0700 | [diff] [blame] | 342 | public void onTaskAppeared(ActivityManager.RunningTaskInfo info, SurfaceControl leash) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 343 | Objects.requireNonNull(info, "Requires RunningTaskInfo"); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 344 | mTaskInfo = info; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 345 | mToken = mTaskInfo.token; |
| 346 | mInPip = true; |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 347 | mExitingPip = false; |
chaviw | 7de5000 | 2020-04-27 12:33:30 -0700 | [diff] [blame] | 348 | mLeash = leash; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 349 | mInitialState.put(mToken.asBinder(), new Configuration(mTaskInfo.configuration)); |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 350 | mPictureInPictureParams = mTaskInfo.pictureInPictureParams; |
| 351 | |
| 352 | if (mShouldDeferEnteringPip) { |
| 353 | if (DEBUG) Log.d(TAG, "Defer entering PiP animation, fixed rotation is ongoing"); |
| 354 | // if deferred, hide the surface till fixed rotation is completed |
| 355 | final SurfaceControl.Transaction tx = |
| 356 | mSurfaceControlTransactionFactory.getTransaction(); |
| 357 | tx.setAlpha(mLeash, 0f); |
Winson Chung | 14a657c | 2020-05-28 10:47:40 -0700 | [diff] [blame] | 358 | tx.show(mLeash); |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 359 | tx.apply(); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | final Rect destinationBounds = mPipBoundsHandler.getDestinationBounds( |
| 364 | mTaskInfo.topActivity, getAspectRatioOrDefault(mPictureInPictureParams), |
| 365 | null /* bounds */, getMinimalSize(mTaskInfo.topActivityInfo)); |
| 366 | Objects.requireNonNull(destinationBounds, "Missing destination bounds"); |
| 367 | final Rect currentBounds = mTaskInfo.configuration.windowConfiguration.getBounds(); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 368 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 369 | if (mOneShotAnimationType == ANIM_TYPE_BOUNDS) { |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 370 | scheduleAnimateResizePip(currentBounds, destinationBounds, |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 371 | TRANSITION_DIRECTION_TO_PIP, mEnterExitAnimationDuration, |
| 372 | null /* updateBoundsCallback */); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 373 | } else if (mOneShotAnimationType == ANIM_TYPE_ALPHA) { |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 374 | enterPipWithAlphaAnimation(destinationBounds, mEnterExitAnimationDuration); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 375 | mOneShotAnimationType = ANIM_TYPE_BOUNDS; |
| 376 | } else { |
| 377 | throw new RuntimeException("Unrecognized animation type: " + mOneShotAnimationType); |
| 378 | } |
| 379 | } |
| 380 | |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 381 | private void enterPipWithAlphaAnimation(Rect destinationBounds, long durationMs) { |
| 382 | // If we are fading the PIP in, then we should move the pip to the final location as |
| 383 | // soon as possible, but set the alpha immediately since the transaction can take a |
| 384 | // while to process |
| 385 | final SurfaceControl.Transaction tx = |
| 386 | mSurfaceControlTransactionFactory.getTransaction(); |
| 387 | tx.setAlpha(mLeash, 0f); |
| 388 | tx.apply(); |
| 389 | final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 390 | wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
| 391 | wct.setBounds(mToken, destinationBounds); |
| 392 | wct.scheduleFinishEnterPip(mToken, destinationBounds); |
| 393 | applySyncTransaction(wct, new WindowContainerTransactionCallback() { |
| 394 | @Override |
| 395 | public void onTransactionReady(int id, SurfaceControl.Transaction t) { |
| 396 | t.apply(); |
| 397 | mUpdateHandler.post(() -> mPipAnimationController |
| 398 | .getAnimator(mLeash, destinationBounds, 0f, 1f) |
| 399 | .setTransitionDirection(TRANSITION_DIRECTION_TO_PIP) |
| 400 | .setPipAnimationCallback(mPipAnimationCallback) |
| 401 | .setDuration(durationMs) |
| 402 | .start()); |
| 403 | } |
| 404 | }); |
| 405 | } |
| 406 | |
Hongwei Wang | 3d11e1f | 2020-05-28 15:48:06 -0700 | [diff] [blame] | 407 | private void sendOnPipTransitionStarted( |
| 408 | @PipAnimationController.TransitionDirection int direction) { |
| 409 | mMainHandler.post(() -> { |
| 410 | for (int i = mPipTransitionCallbacks.size() - 1; i >= 0; i--) { |
| 411 | final PipTransitionCallback callback = mPipTransitionCallbacks.get(i); |
| 412 | callback.onPipTransitionStarted(mTaskInfo.baseActivity, direction); |
| 413 | } |
| 414 | }); |
| 415 | } |
| 416 | |
| 417 | private void sendOnPipTransitionFinished( |
| 418 | @PipAnimationController.TransitionDirection int direction) { |
| 419 | mMainHandler.post(() -> { |
| 420 | for (int i = mPipTransitionCallbacks.size() - 1; i >= 0; i--) { |
| 421 | final PipTransitionCallback callback = mPipTransitionCallbacks.get(i); |
| 422 | callback.onPipTransitionFinished(mTaskInfo.baseActivity, direction); |
| 423 | } |
| 424 | }); |
| 425 | } |
| 426 | |
| 427 | private void sendOnPipTransitionCancelled( |
| 428 | @PipAnimationController.TransitionDirection int direction) { |
| 429 | mMainHandler.post(() -> { |
| 430 | for (int i = mPipTransitionCallbacks.size() - 1; i >= 0; i--) { |
| 431 | final PipTransitionCallback callback = mPipTransitionCallbacks.get(i); |
| 432 | callback.onPipTransitionCanceled(mTaskInfo.baseActivity, direction); |
| 433 | } |
| 434 | }); |
| 435 | } |
| 436 | |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 437 | /** |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 438 | * Note that dismissing PiP is now originated from SystemUI, see {@link #exitPip(int)}. |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 439 | * Meanwhile this callback is invoked whenever the task is removed. For instance: |
| 440 | * - as a result of removeStacksInWindowingModes from WM |
| 441 | * - activity itself is died |
Hongwei Wang | 180162e | 2020-04-22 14:40:32 -0700 | [diff] [blame] | 442 | * Nevertheless, we simply update the internal state here as all the heavy lifting should |
| 443 | * have been done in WM. |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 444 | */ |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 445 | @Override |
Wale Ogunwale | dec3408 | 2020-03-22 09:45:00 -0700 | [diff] [blame] | 446 | public void onTaskVanished(ActivityManager.RunningTaskInfo info) { |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 447 | if (!mInPip) { |
| 448 | return; |
| 449 | } |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 450 | final WindowContainerToken token = info.token; |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 451 | Objects.requireNonNull(token, "Requires valid WindowContainerToken"); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 452 | if (token.asBinder() != mToken.asBinder()) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 453 | Log.wtf(TAG, "Unrecognized token: " + token); |
| 454 | return; |
| 455 | } |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 456 | mShouldDeferEnteringPip = false; |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 457 | mPictureInPictureParams = null; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 458 | mInPip = false; |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 459 | mExitingPip = false; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | @Override |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 463 | public void onTaskInfoChanged(ActivityManager.RunningTaskInfo info) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 464 | Objects.requireNonNull(mToken, "onTaskInfoChanged requires valid existing mToken"); |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 465 | final PictureInPictureParams newParams = info.pictureInPictureParams; |
Winson Chung | 6d8a1956 | 2020-05-21 11:27:28 -0700 | [diff] [blame] | 466 | if (newParams == null || !applyPictureInPictureParams(newParams)) { |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 467 | Log.d(TAG, "Ignored onTaskInfoChanged with PiP param: " + newParams); |
| 468 | return; |
| 469 | } |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 470 | final Rect destinationBounds = mPipBoundsHandler.getDestinationBounds( |
Hongwei Wang | 309cffa | 2020-04-06 11:11:01 -0700 | [diff] [blame] | 471 | info.topActivity, getAspectRatioOrDefault(newParams), |
Hongwei Wang | b5333e9 | 2020-05-29 12:51:52 -0700 | [diff] [blame] | 472 | mLastReportedBounds, getMinimalSize(info.topActivityInfo), |
| 473 | true /* userCurrentMinEdgeSize */); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 474 | Objects.requireNonNull(destinationBounds, "Missing destination bounds"); |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 475 | scheduleAnimateResizePip(destinationBounds, mEnterExitAnimationDuration, |
| 476 | null /* updateBoundsCallback */); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 479 | @Override |
| 480 | public void onBackPressedOnTaskRoot(ActivityManager.RunningTaskInfo taskInfo) { |
| 481 | // Do nothing |
| 482 | } |
| 483 | |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 484 | @Override |
| 485 | public void onFixedRotationStarted(int displayId, int newRotation) { |
| 486 | mShouldDeferEnteringPip = true; |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public void onFixedRotationFinished(int displayId) { |
| 491 | if (mShouldDeferEnteringPip && mInPip) { |
| 492 | final Rect destinationBounds = mPipBoundsHandler.getDestinationBounds( |
| 493 | mTaskInfo.topActivity, getAspectRatioOrDefault(mPictureInPictureParams), |
| 494 | null /* bounds */, getMinimalSize(mTaskInfo.topActivityInfo)); |
| 495 | // schedule a regular animation to ensure all the callbacks are still being sent |
| 496 | enterPipWithAlphaAnimation(destinationBounds, 0 /* durationMs */); |
| 497 | } |
| 498 | mShouldDeferEnteringPip = false; |
| 499 | } |
| 500 | |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 501 | /** |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 502 | * TODO(b/152809058): consolidate the display info handling logic in SysUI |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 503 | * |
| 504 | * @param destinationBoundsOut the current destination bounds will be populated to this param |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 505 | */ |
| 506 | @SuppressWarnings("unchecked") |
Hongwei Wang | e0412d1 | 2020-04-13 18:27:25 -0700 | [diff] [blame] | 507 | public void onMovementBoundsChanged(Rect destinationBoundsOut, boolean fromRotation, |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 508 | boolean fromImeAdjustment, boolean fromShelfAdjustment) { |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 509 | final PipAnimationController.PipTransitionAnimator animator = |
| 510 | mPipAnimationController.getCurrentAnimator(); |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 511 | if (animator == null || !animator.isRunning() |
| 512 | || animator.getTransitionDirection() != TRANSITION_DIRECTION_TO_PIP) { |
Hongwei Wang | e0412d1 | 2020-04-13 18:27:25 -0700 | [diff] [blame] | 513 | if (mInPip && fromRotation) { |
| 514 | // this could happen if rotation finishes before the animation |
| 515 | mLastReportedBounds.set(destinationBoundsOut); |
| 516 | scheduleFinishResizePip(mLastReportedBounds); |
Ben Lin | ba4b76b | 2020-06-05 16:39:17 -0700 | [diff] [blame] | 517 | } else { |
| 518 | // There could be an animation on-going. If there is one on-going, last-reported |
| 519 | // bounds isn't yet updated. We'll use the animator's bounds instead. |
| 520 | if (animator != null && animator.isRunning()) { |
| 521 | if (!animator.getDestinationBounds().isEmpty()) { |
| 522 | destinationBoundsOut.set(animator.getDestinationBounds()); |
| 523 | } |
| 524 | } else { |
| 525 | if (!mLastReportedBounds.isEmpty()) { |
| 526 | destinationBoundsOut.set(mLastReportedBounds); |
| 527 | } |
| 528 | } |
Hongwei Wang | e0412d1 | 2020-04-13 18:27:25 -0700 | [diff] [blame] | 529 | } |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 530 | return; |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 531 | } |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 532 | |
| 533 | final Rect currentDestinationBounds = animator.getDestinationBounds(); |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 534 | destinationBoundsOut.set(currentDestinationBounds); |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 535 | if (!fromImeAdjustment && !fromShelfAdjustment |
| 536 | && mPipBoundsHandler.getDisplayBounds().contains(currentDestinationBounds)) { |
| 537 | // no need to update the destination bounds, bail early |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | final Rect newDestinationBounds = mPipBoundsHandler.getDestinationBounds( |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 542 | mTaskInfo.topActivity, getAspectRatioOrDefault(mPictureInPictureParams), |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 543 | null /* bounds */, getMinimalSize(mTaskInfo.topActivityInfo)); |
| 544 | if (newDestinationBounds.equals(currentDestinationBounds)) return; |
| 545 | if (animator.getAnimationType() == ANIM_TYPE_BOUNDS) { |
| 546 | animator.updateEndValue(newDestinationBounds); |
| 547 | } |
| 548 | animator.setDestinationBounds(newDestinationBounds); |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 549 | destinationBoundsOut.set(newDestinationBounds); |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /** |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 553 | * @return {@code true} if the aspect ratio is changed since no other parameters within |
| 554 | * {@link PictureInPictureParams} would affect the bounds. |
| 555 | */ |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 556 | private boolean applyPictureInPictureParams(@NonNull PictureInPictureParams params) { |
| 557 | final boolean changed = (mPictureInPictureParams == null) ? true : !Objects.equals( |
| 558 | mPictureInPictureParams.getAspectRatioRational(), params.getAspectRatioRational()); |
| 559 | if (changed) { |
| 560 | mPictureInPictureParams = params; |
| 561 | mPipBoundsHandler.onAspectRatioChanged(params.getAspectRatio()); |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 562 | } |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 563 | return changed; |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 564 | } |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 565 | |
| 566 | /** |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 567 | * Animates resizing of the pinned stack given the duration. |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 568 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 569 | public void scheduleAnimateResizePip(Rect toBounds, int duration, |
| 570 | Consumer<Rect> updateBoundsCallback) { |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 571 | if (mShouldDeferEnteringPip) { |
| 572 | Log.d(TAG, "skip scheduleAnimateResizePip, entering pip deferred"); |
| 573 | return; |
| 574 | } |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 575 | scheduleAnimateResizePip(mLastReportedBounds, toBounds, |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 576 | TRANSITION_DIRECTION_NONE, duration, updateBoundsCallback); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 579 | private void scheduleAnimateResizePip(Rect currentBounds, Rect destinationBounds, |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 580 | @PipAnimationController.TransitionDirection int direction, int durationMs, |
| 581 | Consumer<Rect> updateBoundsCallback) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 582 | if (!mInPip) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 583 | // can be initiated in other component, ignore if we are no longer in PIP |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 584 | return; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 585 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 586 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 587 | SomeArgs args = SomeArgs.obtain(); |
| 588 | args.arg1 = updateBoundsCallback; |
| 589 | args.arg2 = currentBounds; |
| 590 | args.arg3 = destinationBounds; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 591 | args.argi1 = direction; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 592 | args.argi2 = durationMs; |
| 593 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_RESIZE_ANIMATE, args)); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Directly perform manipulation/resize on the leash. This will not perform any |
| 598 | * {@link WindowContainerTransaction} until {@link #scheduleFinishResizePip} is called. |
| 599 | */ |
| 600 | public void scheduleResizePip(Rect toBounds, Consumer<Rect> updateBoundsCallback) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 601 | SomeArgs args = SomeArgs.obtain(); |
| 602 | args.arg1 = updateBoundsCallback; |
| 603 | args.arg2 = toBounds; |
| 604 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_RESIZE_IMMEDIATE, args)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 607 | /** |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 608 | * Directly perform a scaled matrix transformation on the leash. This will not perform any |
| 609 | * {@link WindowContainerTransaction} until {@link #scheduleFinishResizePip} is called. |
| 610 | */ |
| 611 | public void scheduleUserResizePip(Rect startBounds, Rect toBounds, |
| 612 | Consumer<Rect> updateBoundsCallback) { |
| 613 | SomeArgs args = SomeArgs.obtain(); |
| 614 | args.arg1 = updateBoundsCallback; |
| 615 | args.arg2 = startBounds; |
| 616 | args.arg3 = toBounds; |
| 617 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_RESIZE_USER, args)); |
| 618 | } |
| 619 | |
| 620 | /** |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 621 | * Finish an intermediate resize operation. This is expected to be called after |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 622 | * {@link #scheduleResizePip}. |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 623 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 624 | public void scheduleFinishResizePip(Rect destinationBounds) { |
Ben Lin | 4a8e257 | 2020-05-08 15:55:15 -0700 | [diff] [blame] | 625 | scheduleFinishResizePip(destinationBounds, null); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Same as {@link #scheduleFinishResizePip} but with a callback. |
| 630 | */ |
| 631 | public void scheduleFinishResizePip(Rect destinationBounds, |
| 632 | Consumer<Rect> updateBoundsCallback) { |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 633 | final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 634 | mSurfaceTransactionHelper |
| 635 | .crop(tx, mLeash, destinationBounds) |
Ben Lin | f603b27 | 2020-03-23 15:39:07 -0700 | [diff] [blame] | 636 | .resetScale(tx, mLeash, destinationBounds) |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 637 | .round(tx, mLeash, mInPip); |
Ben Lin | 4a8e257 | 2020-05-08 15:55:15 -0700 | [diff] [blame] | 638 | scheduleFinishResizePip(tx, destinationBounds, TRANSITION_DIRECTION_NONE, |
| 639 | updateBoundsCallback); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 642 | private void scheduleFinishResizePip(SurfaceControl.Transaction tx, |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 643 | Rect destinationBounds, @PipAnimationController.TransitionDirection int direction, |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 644 | Consumer<Rect> updateBoundsCallback) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 645 | if (!mInPip) { |
| 646 | // can be initiated in other component, ignore if we are no longer in PIP |
| 647 | return; |
| 648 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 649 | SomeArgs args = SomeArgs.obtain(); |
| 650 | args.arg1 = updateBoundsCallback; |
| 651 | args.arg2 = tx; |
| 652 | args.arg3 = destinationBounds; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 653 | args.argi1 = direction; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 654 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_FINISH_RESIZE, args)); |
| 655 | } |
| 656 | |
| 657 | /** |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 658 | * Offset the PiP window by a given offset on Y-axis, triggered also from screen rotation. |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 659 | */ |
| 660 | public void scheduleOffsetPip(Rect originalBounds, int offset, int duration, |
| 661 | Consumer<Rect> updateBoundsCallback) { |
| 662 | if (!mInPip) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 663 | // can be initiated in other component, ignore if we are no longer in PIP |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 664 | return; |
| 665 | } |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 666 | if (mShouldDeferEnteringPip) { |
| 667 | Log.d(TAG, "skip scheduleOffsetPip, entering pip deferred"); |
| 668 | return; |
| 669 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 670 | SomeArgs args = SomeArgs.obtain(); |
| 671 | args.arg1 = updateBoundsCallback; |
| 672 | args.arg2 = originalBounds; |
| 673 | // offset would be zero if triggered from screen rotation. |
| 674 | args.argi1 = offset; |
| 675 | args.argi2 = duration; |
| 676 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_OFFSET_ANIMATE, args)); |
| 677 | } |
| 678 | |
| 679 | private void offsetPip(Rect originalBounds, int xOffset, int yOffset, int durationMs) { |
| 680 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 681 | throw new RuntimeException("Callers should call scheduleOffsetPip() instead of this " |
| 682 | + "directly"); |
| 683 | } |
| 684 | if (mTaskInfo == null) { |
| 685 | Log.w(TAG, "mTaskInfo is not set"); |
| 686 | return; |
| 687 | } |
| 688 | final Rect destinationBounds = new Rect(originalBounds); |
| 689 | destinationBounds.offset(xOffset, yOffset); |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 690 | animateResizePip(originalBounds, destinationBounds, TRANSITION_DIRECTION_SAME, durationMs); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | private void resizePip(Rect destinationBounds) { |
| 694 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 695 | throw new RuntimeException("Callers should call scheduleResizePip() instead of this " |
| 696 | + "directly"); |
| 697 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 698 | // Could happen when exitPip |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 699 | if (mToken == null || mLeash == null) { |
| 700 | Log.w(TAG, "Abort animation, invalid leash"); |
| 701 | return; |
| 702 | } |
Ben Lin | 5400de3 | 2020-05-06 19:28:09 -0700 | [diff] [blame] | 703 | mLastReportedBounds.set(destinationBounds); |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 704 | final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 705 | mSurfaceTransactionHelper |
| 706 | .crop(tx, mLeash, destinationBounds) |
| 707 | .round(tx, mLeash, mInPip); |
| 708 | tx.apply(); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 711 | private void userResizePip(Rect startBounds, Rect destinationBounds) { |
| 712 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 713 | throw new RuntimeException("Callers should call scheduleUserResizePip() instead of " |
| 714 | + "this directly"); |
| 715 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 716 | // Could happen when exitPip |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 717 | if (mToken == null || mLeash == null) { |
| 718 | Log.w(TAG, "Abort animation, invalid leash"); |
| 719 | return; |
| 720 | } |
Josh Tsuji | a8ed97e | 2020-06-16 17:00:59 -0400 | [diff] [blame] | 721 | |
| 722 | if (startBounds.isEmpty() || destinationBounds.isEmpty()) { |
| 723 | Log.w(TAG, "Attempted to user resize PIP to or from empty bounds, aborting."); |
| 724 | return; |
| 725 | } |
| 726 | |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 727 | final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 728 | mSurfaceTransactionHelper.scale(tx, mLeash, startBounds, destinationBounds); |
| 729 | tx.apply(); |
| 730 | } |
| 731 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 732 | private void finishResize(SurfaceControl.Transaction tx, Rect destinationBounds, |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 733 | @PipAnimationController.TransitionDirection int direction, |
| 734 | @PipAnimationController.AnimationType int type) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 735 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 736 | throw new RuntimeException("Callers should call scheduleResizePip() instead of this " |
| 737 | + "directly"); |
| 738 | } |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 739 | mLastReportedBounds.set(destinationBounds); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 740 | if (isInPipDirection(direction) && type == ANIM_TYPE_ALPHA) { |
| 741 | return; |
| 742 | } |
| 743 | |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 744 | final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 745 | final Rect taskBounds; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 746 | if (isInPipDirection(direction)) { |
| 747 | // If we are animating from fullscreen using a bounds animation, then reset the |
| 748 | // activity windowing mode set by WM, and set the task bounds to the final bounds |
| 749 | taskBounds = destinationBounds; |
| 750 | wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
| 751 | wct.scheduleFinishEnterPip(mToken, destinationBounds); |
| 752 | } else if (isOutPipDirection(direction)) { |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 753 | // If we are animating to fullscreen, then we need to reset the override bounds |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 754 | // on the task to ensure that the task "matches" the parent's bounds. |
| 755 | taskBounds = (direction == TRANSITION_DIRECTION_TO_FULLSCREEN) |
| 756 | ? null : destinationBounds; |
Hongwei Wang | 6ef225e | 2020-06-23 11:16:05 -0700 | [diff] [blame] | 757 | applyWindowingModeChangeOnExit(wct, direction); |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 758 | } else { |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 759 | // Just a resize in PIP |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 760 | taskBounds = destinationBounds; |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 761 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 762 | |
| 763 | wct.setBounds(mToken, taskBounds); |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 764 | wct.setBoundsChangeTransaction(mToken, tx); |
jorgegil@google.com | 79aae3c | 2020-06-11 10:37:52 -0700 | [diff] [blame] | 765 | applyFinishBoundsResize(wct, direction); |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * Applies the window container transaction to finish a bounds resize. |
| 770 | * |
| 771 | * Called by {@link #finishResize(SurfaceControl.Transaction, Rect, int, int)}} once it has |
| 772 | * finished preparing the transaction. It allows subclasses to modify the transaction before |
| 773 | * applying it. |
| 774 | */ |
| 775 | public void applyFinishBoundsResize(@NonNull WindowContainerTransaction wct, |
| 776 | @PipAnimationController.TransitionDirection int direction) { |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 777 | WindowOrganizer.applyTransaction(wct); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 778 | } |
| 779 | |
jorgegil@google.com | 79aae3c | 2020-06-11 10:37:52 -0700 | [diff] [blame] | 780 | /** |
| 781 | * The windowing mode to restore to when resizing out of PIP direction. Defaults to undefined |
| 782 | * and can be overridden to restore to an alternate windowing mode. |
| 783 | */ |
| 784 | public int getOutPipWindowingMode() { |
| 785 | // By default, simply reset the windowing mode to undefined. |
| 786 | return WINDOWING_MODE_UNDEFINED; |
| 787 | } |
| 788 | |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 789 | private void animateResizePip(Rect currentBounds, Rect destinationBounds, |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 790 | @PipAnimationController.TransitionDirection int direction, int durationMs) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 791 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 792 | throw new RuntimeException("Callers should call scheduleAnimateResizePip() instead of " |
| 793 | + "this directly"); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 794 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 795 | // Could happen when exitPip |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 796 | if (mToken == null || mLeash == null) { |
| 797 | Log.w(TAG, "Abort animation, invalid leash"); |
| 798 | return; |
| 799 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 800 | mPipAnimationController |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 801 | .getAnimator(mLeash, currentBounds, destinationBounds) |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 802 | .setTransitionDirection(direction) |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 803 | .setPipAnimationCallback(mPipAnimationCallback) |
| 804 | .setDuration(durationMs) |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 805 | .start(); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 808 | private Size getMinimalSize(ActivityInfo activityInfo) { |
| 809 | if (activityInfo == null || activityInfo.windowLayout == null) { |
| 810 | return null; |
| 811 | } |
| 812 | final ActivityInfo.WindowLayout windowLayout = activityInfo.windowLayout; |
Hongwei Wang | 907fd63 | 2020-04-02 17:13:08 -0700 | [diff] [blame] | 813 | // -1 will be populated if an activity specifies defaultWidth/defaultHeight in <layout> |
| 814 | // without minWidth/minHeight |
| 815 | if (windowLayout.minWidth > 0 && windowLayout.minHeight > 0) { |
| 816 | return new Size(windowLayout.minWidth, windowLayout.minHeight); |
| 817 | } |
| 818 | return null; |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 821 | private float getAspectRatioOrDefault(@Nullable PictureInPictureParams params) { |
| 822 | return params == null |
| 823 | ? mPipBoundsHandler.getDefaultAspectRatio() |
| 824 | : params.getAspectRatio(); |
| 825 | } |
| 826 | |
| 827 | /** |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 828 | * Sync with {@link #mSplitDivider} on destination bounds if PiP is going to split screen. |
| 829 | * |
| 830 | * @param destinationBoundsOut contain the updated destination bounds if applicable |
| 831 | * @return {@code true} if destinationBounds is altered for split screen |
| 832 | */ |
| 833 | private boolean syncWithSplitScreenBounds(Rect destinationBoundsOut) { |
Evan Rosky | 90d5b6d | 2020-05-13 19:39:05 -0700 | [diff] [blame] | 834 | if (mSplitDivider == null || !mSplitDivider.isDividerVisible()) { |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 835 | // bail early if system is not in split screen mode |
| 836 | return false; |
| 837 | } |
| 838 | // PiP window will go to split-secondary mode instead of fullscreen, populates the |
| 839 | // split screen bounds here. |
| 840 | destinationBoundsOut.set( |
| 841 | mSplitDivider.getView().getNonMinimizedSplitScreenSecondaryBounds()); |
| 842 | return true; |
| 843 | } |
| 844 | |
| 845 | /** |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 846 | * Dumps internal states. |
| 847 | */ |
| 848 | public void dump(PrintWriter pw, String prefix) { |
| 849 | final String innerPrefix = prefix + " "; |
| 850 | pw.println(prefix + TAG); |
| 851 | pw.println(innerPrefix + "mTaskInfo=" + mTaskInfo); |
| 852 | pw.println(innerPrefix + "mToken=" + mToken |
| 853 | + " binder=" + (mToken != null ? mToken.asBinder() : null)); |
| 854 | pw.println(innerPrefix + "mLeash=" + mLeash); |
| 855 | pw.println(innerPrefix + "mInPip=" + mInPip); |
| 856 | pw.println(innerPrefix + "mOneShotAnimationType=" + mOneShotAnimationType); |
| 857 | pw.println(innerPrefix + "mPictureInPictureParams=" + mPictureInPictureParams); |
| 858 | pw.println(innerPrefix + "mLastReportedBounds=" + mLastReportedBounds); |
| 859 | pw.println(innerPrefix + "mInitialState:"); |
| 860 | for (Map.Entry<IBinder, Configuration> e : mInitialState.entrySet()) { |
| 861 | pw.println(innerPrefix + " binder=" + e.getKey() |
| 862 | + " winConfig=" + e.getValue().windowConfiguration); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | /** |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 867 | * Callback interface for PiP transitions (both from and to PiP mode) |
| 868 | */ |
| 869 | public interface PipTransitionCallback { |
| 870 | /** |
| 871 | * Callback when the pip transition is started. |
| 872 | */ |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 873 | void onPipTransitionStarted(ComponentName activity, int direction); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 874 | |
| 875 | /** |
| 876 | * Callback when the pip transition is finished. |
| 877 | */ |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 878 | void onPipTransitionFinished(ComponentName activity, int direction); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 879 | |
| 880 | /** |
| 881 | * Callback when the pip transition is cancelled. |
| 882 | */ |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 883 | void onPipTransitionCanceled(ComponentName activity, int direction); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 884 | } |
| 885 | } |