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 | |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
| 20 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 21 | import static com.android.systemui.pip.PipAnimationController.ANIM_TYPE_ALPHA; |
| 22 | import static com.android.systemui.pip.PipAnimationController.ANIM_TYPE_BOUNDS; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 23 | import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_NONE; |
| 24 | import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_SAME; |
| 25 | import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_TO_FULLSCREEN; |
| 26 | import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_TO_PIP; |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 27 | import static com.android.systemui.pip.PipAnimationController.TRANSITION_DIRECTION_TO_SPLIT_SCREEN; |
| 28 | import static com.android.systemui.pip.PipAnimationController.isOutPipDirection; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 29 | |
| 30 | import android.annotation.NonNull; |
| 31 | import android.annotation.Nullable; |
| 32 | import android.app.ActivityManager; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 33 | import android.app.PictureInPictureParams; |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 34 | import android.content.ComponentName; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 35 | import android.content.Context; |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 36 | import android.content.pm.ActivityInfo; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 37 | import android.graphics.Rect; |
| 38 | import android.os.Handler; |
Hongwei Wang | fbc25fe | 2020-03-16 11:59:28 -0700 | [diff] [blame] | 39 | import android.os.IBinder; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 40 | import android.os.Looper; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 41 | import android.util.Log; |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 42 | import android.util.Size; |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 43 | import android.view.SurfaceControl; |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 44 | import android.window.TaskOrganizer; |
| 45 | import android.window.WindowContainerToken; |
Wale Ogunwale | 5794658 | 2020-03-21 14:29:07 -0700 | [diff] [blame] | 46 | import android.window.WindowContainerTransaction; |
Wale Ogunwale | 568f9f41 | 2020-03-21 22:27:35 -0700 | [diff] [blame] | 47 | import android.window.WindowOrganizer; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 48 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 49 | import com.android.internal.os.SomeArgs; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 50 | import com.android.systemui.R; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 51 | import com.android.systemui.pip.phone.PipUpdateThread; |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 52 | import com.android.systemui.stackdivider.Divider; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 53 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 54 | import java.util.ArrayList; |
Hongwei Wang | fbc25fe | 2020-03-16 11:59:28 -0700 | [diff] [blame] | 55 | import java.util.HashMap; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 56 | import java.util.List; |
Hongwei Wang | fbc25fe | 2020-03-16 11:59:28 -0700 | [diff] [blame] | 57 | import java.util.Map; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 58 | import java.util.Objects; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 59 | import java.util.function.Consumer; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 60 | |
Ben Lin | 6189fa4 | 2020-04-29 14:59:16 -0700 | [diff] [blame] | 61 | import javax.inject.Inject; |
| 62 | import javax.inject.Singleton; |
| 63 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 64 | /** |
| 65 | * Manages PiP tasks such as resize and offset. |
| 66 | * |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 67 | * This class listens on {@link TaskOrganizer} callbacks for windowing mode change |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 68 | * both to and from PiP and issues corresponding animation if applicable. |
| 69 | * Normally, we apply series of {@link SurfaceControl.Transaction} when the animator is running |
| 70 | * and files a final {@link WindowContainerTransaction} at the end of the transition. |
| 71 | * |
| 72 | * This class is also responsible for general resize/offset PiP operations within SysUI component, |
| 73 | * see also {@link com.android.systemui.pip.phone.PipMotionHelper}. |
| 74 | */ |
Ben Lin | 6189fa4 | 2020-04-29 14:59:16 -0700 | [diff] [blame] | 75 | @Singleton |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 76 | public class PipTaskOrganizer extends TaskOrganizer { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 77 | private static final String TAG = PipTaskOrganizer.class.getSimpleName(); |
| 78 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 79 | private static final int MSG_RESIZE_IMMEDIATE = 1; |
| 80 | private static final int MSG_RESIZE_ANIMATE = 2; |
| 81 | private static final int MSG_OFFSET_ANIMATE = 3; |
| 82 | private static final int MSG_FINISH_RESIZE = 4; |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 83 | private static final int MSG_RESIZE_USER = 5; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 84 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 85 | private final Handler mMainHandler; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 86 | private final Handler mUpdateHandler; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 87 | private final PipBoundsHandler mPipBoundsHandler; |
| 88 | private final PipAnimationController mPipAnimationController; |
| 89 | private final List<PipTransitionCallback> mPipTransitionCallbacks = new ArrayList<>(); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 90 | private final Rect mLastReportedBounds = new Rect(); |
Ben Lin | ede9a60 | 2020-02-26 12:16:09 -0800 | [diff] [blame] | 91 | private final int mEnterExitAnimationDuration; |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 92 | private final PipSurfaceTransactionHelper mSurfaceTransactionHelper; |
Hongwei Wang | fbc25fe | 2020-03-16 11:59:28 -0700 | [diff] [blame] | 93 | private final Map<IBinder, Rect> mBoundsToRestore = new HashMap<>(); |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 94 | private final Divider mSplitDivider; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 95 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 96 | // These callbacks are called on the update thread |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 97 | private final PipAnimationController.PipAnimationCallback mPipAnimationCallback = |
| 98 | new PipAnimationController.PipAnimationCallback() { |
| 99 | @Override |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 100 | public void onPipAnimationStart(PipAnimationController.PipTransitionAnimator animator) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 101 | mMainHandler.post(() -> { |
| 102 | for (int i = mPipTransitionCallbacks.size() - 1; i >= 0; i--) { |
| 103 | final PipTransitionCallback callback = mPipTransitionCallbacks.get(i); |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 104 | callback.onPipTransitionStarted(mTaskInfo.baseActivity, |
| 105 | animator.getTransitionDirection()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 106 | } |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | @Override |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 111 | public void onPipAnimationEnd(SurfaceControl.Transaction tx, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 112 | PipAnimationController.PipTransitionAnimator animator) { |
Ben Lin | 3cd7bc3 | 2020-04-07 17:15:21 -0700 | [diff] [blame] | 113 | finishResize(tx, animator.getDestinationBounds(), animator.getTransitionDirection()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 114 | mMainHandler.post(() -> { |
| 115 | for (int i = mPipTransitionCallbacks.size() - 1; i >= 0; i--) { |
| 116 | final PipTransitionCallback callback = mPipTransitionCallbacks.get(i); |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 117 | callback.onPipTransitionFinished(mTaskInfo.baseActivity, |
| 118 | animator.getTransitionDirection()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 119 | } |
| 120 | }); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 124 | public void onPipAnimationCancel(PipAnimationController.PipTransitionAnimator animator) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 125 | mMainHandler.post(() -> { |
| 126 | for (int i = mPipTransitionCallbacks.size() - 1; i >= 0; i--) { |
| 127 | final PipTransitionCallback callback = mPipTransitionCallbacks.get(i); |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 128 | callback.onPipTransitionCanceled(mTaskInfo.baseActivity, |
| 129 | animator.getTransitionDirection()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 130 | } |
| 131 | }); |
| 132 | } |
| 133 | }; |
| 134 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 135 | @SuppressWarnings("unchecked") |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 136 | private final Handler.Callback mUpdateCallbacks = (msg) -> { |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 137 | SomeArgs args = (SomeArgs) msg.obj; |
| 138 | Consumer<Rect> updateBoundsCallback = (Consumer<Rect>) args.arg1; |
| 139 | switch (msg.what) { |
| 140 | case MSG_RESIZE_IMMEDIATE: { |
| 141 | Rect toBounds = (Rect) args.arg2; |
| 142 | resizePip(toBounds); |
| 143 | if (updateBoundsCallback != null) { |
| 144 | updateBoundsCallback.accept(toBounds); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 145 | } |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 146 | break; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 147 | } |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 148 | case MSG_RESIZE_ANIMATE: { |
| 149 | Rect currentBounds = (Rect) args.arg2; |
| 150 | Rect toBounds = (Rect) args.arg3; |
| 151 | int duration = args.argi2; |
| 152 | animateResizePip(currentBounds, toBounds, args.argi1 /* direction */, duration); |
| 153 | if (updateBoundsCallback != null) { |
| 154 | updateBoundsCallback.accept(toBounds); |
| 155 | } |
| 156 | break; |
| 157 | } |
| 158 | case MSG_OFFSET_ANIMATE: { |
| 159 | Rect originalBounds = (Rect) args.arg2; |
| 160 | final int offset = args.argi1; |
| 161 | final int duration = args.argi2; |
| 162 | offsetPip(originalBounds, 0 /* xOffset */, offset, duration); |
| 163 | Rect toBounds = new Rect(originalBounds); |
| 164 | toBounds.offset(0, offset); |
| 165 | if (updateBoundsCallback != null) { |
| 166 | updateBoundsCallback.accept(toBounds); |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | case MSG_FINISH_RESIZE: { |
| 171 | SurfaceControl.Transaction tx = (SurfaceControl.Transaction) args.arg2; |
| 172 | Rect toBounds = (Rect) args.arg3; |
| 173 | finishResize(tx, toBounds, args.argi1 /* direction */); |
| 174 | if (updateBoundsCallback != null) { |
| 175 | updateBoundsCallback.accept(toBounds); |
| 176 | } |
| 177 | break; |
| 178 | } |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 179 | case MSG_RESIZE_USER: { |
| 180 | Rect startBounds = (Rect) args.arg2; |
| 181 | Rect toBounds = (Rect) args.arg3; |
| 182 | userResizePip(startBounds, toBounds); |
| 183 | break; |
| 184 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 185 | } |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 186 | args.recycle(); |
| 187 | return true; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 188 | }; |
| 189 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 190 | private ActivityManager.RunningTaskInfo mTaskInfo; |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 191 | private WindowContainerToken mToken; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 192 | private SurfaceControl mLeash; |
| 193 | private boolean mInPip; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 194 | private @PipAnimationController.AnimationType int mOneShotAnimationType = ANIM_TYPE_BOUNDS; |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 195 | private PipSurfaceTransactionHelper.SurfaceControlTransactionFactory |
| 196 | mSurfaceControlTransactionFactory; |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 197 | private PictureInPictureParams mPictureInPictureParams; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 198 | |
Ben Lin | 6189fa4 | 2020-04-29 14:59:16 -0700 | [diff] [blame] | 199 | @Inject |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 200 | public PipTaskOrganizer(Context context, @NonNull PipBoundsHandler boundsHandler, |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 201 | @NonNull PipSurfaceTransactionHelper surfaceTransactionHelper, |
| 202 | @Nullable Divider divider) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 203 | mMainHandler = new Handler(Looper.getMainLooper()); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 204 | mUpdateHandler = new Handler(PipUpdateThread.get().getLooper(), mUpdateCallbacks); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 205 | mPipBoundsHandler = boundsHandler; |
Ben Lin | ede9a60 | 2020-02-26 12:16:09 -0800 | [diff] [blame] | 206 | mEnterExitAnimationDuration = context.getResources() |
| 207 | .getInteger(R.integer.config_pipResizeAnimationDuration); |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 208 | mSurfaceTransactionHelper = surfaceTransactionHelper; |
| 209 | mPipAnimationController = new PipAnimationController(context, surfaceTransactionHelper); |
| 210 | mSurfaceControlTransactionFactory = SurfaceControl.Transaction::new; |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 211 | mSplitDivider = divider; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 214 | public Handler getUpdateHandler() { |
| 215 | return mUpdateHandler; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 218 | public Rect getLastReportedBounds() { |
| 219 | return new Rect(mLastReportedBounds); |
| 220 | } |
| 221 | |
Joshua Tsuji | 0d4cbeb | 2020-05-01 12:45:41 -0400 | [diff] [blame] | 222 | public boolean isInPip() { |
| 223 | return mInPip; |
| 224 | } |
| 225 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 226 | /** |
| 227 | * Registers {@link PipTransitionCallback} to receive transition callbacks. |
| 228 | */ |
| 229 | public void registerPipTransitionCallback(PipTransitionCallback callback) { |
| 230 | mPipTransitionCallbacks.add(callback); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Sets the preferred animation type for one time. |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 235 | * This is typically used to set the animation type to |
| 236 | * {@link PipAnimationController#ANIM_TYPE_ALPHA}. |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 237 | */ |
| 238 | public void setOneShotAnimationType(@PipAnimationController.AnimationType int animationType) { |
| 239 | mOneShotAnimationType = animationType; |
| 240 | } |
| 241 | |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 242 | /** |
| 243 | * Dismiss PiP, this is done in two phases using {@link WindowContainerTransaction} |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 244 | * - setActivityWindowingMode to undefined at beginning of the transaction. without changing |
| 245 | * the windowing mode of the Task itself. This makes sure the activity render it's final |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 246 | * configuration while the Task is still in PiP. |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 247 | * - setWindowingMode to undefined at the end of transition |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 248 | * @param animationDurationMs duration in millisecond for the exiting PiP transition |
| 249 | */ |
| 250 | public void dismissPip(int animationDurationMs) { |
Hongwei Wang | 1273c95 | 2020-04-30 13:28:29 -0700 | [diff] [blame] | 251 | if (!mInPip || mToken == null) { |
| 252 | Log.wtf(TAG, "Not allowed to dismissPip in current state" |
| 253 | + " mInPip=" + mInPip + " mToken=" + mToken); |
| 254 | return; |
| 255 | } |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 256 | final WindowContainerTransaction wct = new WindowContainerTransaction(); |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 257 | wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 258 | WindowOrganizer.applyTransaction(wct); |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 259 | final Rect destinationBounds = mBoundsToRestore.remove(mToken.asBinder()); |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 260 | final int direction = syncWithSplitScreenBounds(destinationBounds) |
| 261 | ? TRANSITION_DIRECTION_TO_SPLIT_SCREEN : TRANSITION_DIRECTION_TO_FULLSCREEN; |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 262 | scheduleAnimateResizePip(mLastReportedBounds, destinationBounds, |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 263 | direction, animationDurationMs, null /* updateBoundsCallback */); |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 264 | mInPip = false; |
| 265 | } |
| 266 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 267 | @Override |
chaviw | 7de5000 | 2020-04-27 12:33:30 -0700 | [diff] [blame] | 268 | public void onTaskAppeared(ActivityManager.RunningTaskInfo info, SurfaceControl leash) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 269 | Objects.requireNonNull(info, "Requires RunningTaskInfo"); |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 270 | mPictureInPictureParams = info.pictureInPictureParams; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 271 | final Rect destinationBounds = mPipBoundsHandler.getDestinationBounds( |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 272 | info.topActivity, getAspectRatioOrDefault(mPictureInPictureParams), |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 273 | null /* bounds */, getMinimalSize(info.topActivityInfo)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 274 | Objects.requireNonNull(destinationBounds, "Missing destination bounds"); |
| 275 | mTaskInfo = info; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 276 | mToken = mTaskInfo.token; |
| 277 | mInPip = true; |
chaviw | 7de5000 | 2020-04-27 12:33:30 -0700 | [diff] [blame] | 278 | mLeash = leash; |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 279 | |
Hongwei Wang | 89f18ff | 2020-03-10 10:24:35 -0700 | [diff] [blame] | 280 | final Rect currentBounds = mTaskInfo.configuration.windowConfiguration.getBounds(); |
Hongwei Wang | fbc25fe | 2020-03-16 11:59:28 -0700 | [diff] [blame] | 281 | mBoundsToRestore.put(mToken.asBinder(), currentBounds); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 282 | if (mOneShotAnimationType == ANIM_TYPE_BOUNDS) { |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 283 | scheduleAnimateResizePip(currentBounds, destinationBounds, |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 284 | TRANSITION_DIRECTION_TO_PIP, mEnterExitAnimationDuration, |
| 285 | null /* updateBoundsCallback */); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 286 | } else if (mOneShotAnimationType == ANIM_TYPE_ALPHA) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 287 | mUpdateHandler.post(() -> mPipAnimationController |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 288 | .getAnimator(mLeash, destinationBounds, 0f, 1f) |
| 289 | .setTransitionDirection(TRANSITION_DIRECTION_TO_PIP) |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 290 | .setPipAnimationCallback(mPipAnimationCallback) |
Ben Lin | ede9a60 | 2020-02-26 12:16:09 -0800 | [diff] [blame] | 291 | .setDuration(mEnterExitAnimationDuration) |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 292 | .start()); |
| 293 | mOneShotAnimationType = ANIM_TYPE_BOUNDS; |
| 294 | } else { |
| 295 | throw new RuntimeException("Unrecognized animation type: " + mOneShotAnimationType); |
| 296 | } |
| 297 | } |
| 298 | |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 299 | /** |
| 300 | * Note that dismissing PiP is now originated from SystemUI, see {@link #dismissPip(int)}. |
| 301 | * Meanwhile this callback is invoked whenever the task is removed. For instance: |
| 302 | * - as a result of removeStacksInWindowingModes from WM |
| 303 | * - activity itself is died |
Hongwei Wang | 180162e | 2020-04-22 14:40:32 -0700 | [diff] [blame] | 304 | * Nevertheless, we simply update the internal state here as all the heavy lifting should |
| 305 | * have been done in WM. |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 306 | */ |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 307 | @Override |
Wale Ogunwale | dec3408 | 2020-03-22 09:45:00 -0700 | [diff] [blame] | 308 | public void onTaskVanished(ActivityManager.RunningTaskInfo info) { |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 309 | if (!mInPip) { |
| 310 | return; |
| 311 | } |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 312 | final WindowContainerToken token = info.token; |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 313 | Objects.requireNonNull(token, "Requires valid WindowContainerToken"); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 314 | if (token.asBinder() != mToken.asBinder()) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 315 | Log.wtf(TAG, "Unrecognized token: " + token); |
| 316 | return; |
| 317 | } |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 318 | mPictureInPictureParams = null; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 319 | mInPip = false; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | @Override |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 323 | public void onTaskInfoChanged(ActivityManager.RunningTaskInfo info) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 324 | Objects.requireNonNull(mToken, "onTaskInfoChanged requires valid existing mToken"); |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 325 | final PictureInPictureParams newParams = info.pictureInPictureParams; |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 326 | if (!applyPictureInPictureParams(newParams)) { |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 327 | Log.d(TAG, "Ignored onTaskInfoChanged with PiP param: " + newParams); |
| 328 | return; |
| 329 | } |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 330 | final Rect destinationBounds = mPipBoundsHandler.getDestinationBounds( |
Hongwei Wang | 309cffa | 2020-04-06 11:11:01 -0700 | [diff] [blame] | 331 | info.topActivity, getAspectRatioOrDefault(newParams), |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 332 | null /* bounds */, getMinimalSize(info.topActivityInfo)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 333 | Objects.requireNonNull(destinationBounds, "Missing destination bounds"); |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 334 | scheduleAnimateResizePip(destinationBounds, mEnterExitAnimationDuration, |
| 335 | null /* updateBoundsCallback */); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 338 | @Override |
| 339 | public void onBackPressedOnTaskRoot(ActivityManager.RunningTaskInfo taskInfo) { |
| 340 | // Do nothing |
| 341 | } |
| 342 | |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 343 | /** |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 344 | * TODO(b/152809058): consolidate the display info handling logic in SysUI |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 345 | * |
| 346 | * @param destinationBoundsOut the current destination bounds will be populated to this param |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 347 | */ |
| 348 | @SuppressWarnings("unchecked") |
Hongwei Wang | e0412d1 | 2020-04-13 18:27:25 -0700 | [diff] [blame] | 349 | public void onMovementBoundsChanged(Rect destinationBoundsOut, boolean fromRotation, |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 350 | boolean fromImeAdjustment, boolean fromShelfAdjustment) { |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 351 | final PipAnimationController.PipTransitionAnimator animator = |
| 352 | mPipAnimationController.getCurrentAnimator(); |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 353 | if (animator == null || !animator.isRunning() |
| 354 | || animator.getTransitionDirection() != TRANSITION_DIRECTION_TO_PIP) { |
Hongwei Wang | e0412d1 | 2020-04-13 18:27:25 -0700 | [diff] [blame] | 355 | if (mInPip && fromRotation) { |
| 356 | // this could happen if rotation finishes before the animation |
| 357 | mLastReportedBounds.set(destinationBoundsOut); |
| 358 | scheduleFinishResizePip(mLastReportedBounds); |
| 359 | } else if (!mLastReportedBounds.isEmpty()) { |
| 360 | destinationBoundsOut.set(mLastReportedBounds); |
| 361 | } |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 362 | return; |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 363 | } |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 364 | |
| 365 | final Rect currentDestinationBounds = animator.getDestinationBounds(); |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 366 | destinationBoundsOut.set(currentDestinationBounds); |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 367 | if (!fromImeAdjustment && !fromShelfAdjustment |
| 368 | && mPipBoundsHandler.getDisplayBounds().contains(currentDestinationBounds)) { |
| 369 | // no need to update the destination bounds, bail early |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | final Rect newDestinationBounds = mPipBoundsHandler.getDestinationBounds( |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 374 | mTaskInfo.topActivity, getAspectRatioOrDefault(mPictureInPictureParams), |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 375 | null /* bounds */, getMinimalSize(mTaskInfo.topActivityInfo)); |
| 376 | if (newDestinationBounds.equals(currentDestinationBounds)) return; |
| 377 | if (animator.getAnimationType() == ANIM_TYPE_BOUNDS) { |
| 378 | animator.updateEndValue(newDestinationBounds); |
| 379 | } |
| 380 | animator.setDestinationBounds(newDestinationBounds); |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 381 | destinationBoundsOut.set(newDestinationBounds); |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 385 | * @return {@code true} if the aspect ratio is changed since no other parameters within |
| 386 | * {@link PictureInPictureParams} would affect the bounds. |
| 387 | */ |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 388 | private boolean applyPictureInPictureParams(@NonNull PictureInPictureParams params) { |
| 389 | final boolean changed = (mPictureInPictureParams == null) ? true : !Objects.equals( |
| 390 | mPictureInPictureParams.getAspectRatioRational(), params.getAspectRatioRational()); |
| 391 | if (changed) { |
| 392 | mPictureInPictureParams = params; |
| 393 | mPipBoundsHandler.onAspectRatioChanged(params.getAspectRatio()); |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 394 | } |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 395 | return changed; |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 396 | } |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 397 | |
| 398 | /** |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 399 | * Animates resizing of the pinned stack given the duration. |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 400 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 401 | public void scheduleAnimateResizePip(Rect toBounds, int duration, |
| 402 | Consumer<Rect> updateBoundsCallback) { |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 403 | scheduleAnimateResizePip(mLastReportedBounds, toBounds, |
| 404 | TRANSITION_DIRECTION_NONE, duration, updateBoundsCallback); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 407 | private void scheduleAnimateResizePip(Rect currentBounds, Rect destinationBounds, |
| 408 | @PipAnimationController.TransitionDirection int direction, int durationMs, |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 409 | Consumer<Rect> updateBoundsCallback) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 410 | if (!mInPip) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 411 | // 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] | 412 | return; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 413 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 414 | SomeArgs args = SomeArgs.obtain(); |
| 415 | args.arg1 = updateBoundsCallback; |
| 416 | args.arg2 = currentBounds; |
| 417 | args.arg3 = destinationBounds; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 418 | args.argi1 = direction; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 419 | args.argi2 = durationMs; |
| 420 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_RESIZE_ANIMATE, args)); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Directly perform manipulation/resize on the leash. This will not perform any |
| 425 | * {@link WindowContainerTransaction} until {@link #scheduleFinishResizePip} is called. |
| 426 | */ |
| 427 | public void scheduleResizePip(Rect toBounds, Consumer<Rect> updateBoundsCallback) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 428 | SomeArgs args = SomeArgs.obtain(); |
| 429 | args.arg1 = updateBoundsCallback; |
| 430 | args.arg2 = toBounds; |
| 431 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_RESIZE_IMMEDIATE, args)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 434 | /** |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 435 | * Directly perform a scaled matrix transformation on the leash. This will not perform any |
| 436 | * {@link WindowContainerTransaction} until {@link #scheduleFinishResizePip} is called. |
| 437 | */ |
| 438 | public void scheduleUserResizePip(Rect startBounds, Rect toBounds, |
| 439 | Consumer<Rect> updateBoundsCallback) { |
| 440 | SomeArgs args = SomeArgs.obtain(); |
| 441 | args.arg1 = updateBoundsCallback; |
| 442 | args.arg2 = startBounds; |
| 443 | args.arg3 = toBounds; |
| 444 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_RESIZE_USER, args)); |
| 445 | } |
| 446 | |
| 447 | /** |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 448 | * Finish an intermediate resize operation. This is expected to be called after |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 449 | * {@link #scheduleResizePip}. |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 450 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 451 | public void scheduleFinishResizePip(Rect destinationBounds) { |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 452 | final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 453 | mSurfaceTransactionHelper |
| 454 | .crop(tx, mLeash, destinationBounds) |
Ben Lin | f603b27 | 2020-03-23 15:39:07 -0700 | [diff] [blame] | 455 | .resetScale(tx, mLeash, destinationBounds) |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 456 | .round(tx, mLeash, mInPip); |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 457 | scheduleFinishResizePip(tx, destinationBounds, TRANSITION_DIRECTION_NONE, null); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 460 | private void scheduleFinishResizePip(SurfaceControl.Transaction tx, |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 461 | Rect destinationBounds, @PipAnimationController.TransitionDirection int direction, |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 462 | Consumer<Rect> updateBoundsCallback) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 463 | if (!mInPip) { |
| 464 | // can be initiated in other component, ignore if we are no longer in PIP |
| 465 | return; |
| 466 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 467 | SomeArgs args = SomeArgs.obtain(); |
| 468 | args.arg1 = updateBoundsCallback; |
| 469 | args.arg2 = tx; |
| 470 | args.arg3 = destinationBounds; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 471 | args.argi1 = direction; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 472 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_FINISH_RESIZE, args)); |
| 473 | } |
| 474 | |
| 475 | /** |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 476 | * 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] | 477 | */ |
| 478 | public void scheduleOffsetPip(Rect originalBounds, int offset, int duration, |
| 479 | Consumer<Rect> updateBoundsCallback) { |
| 480 | if (!mInPip) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 481 | // 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] | 482 | return; |
| 483 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 484 | SomeArgs args = SomeArgs.obtain(); |
| 485 | args.arg1 = updateBoundsCallback; |
| 486 | args.arg2 = originalBounds; |
| 487 | // offset would be zero if triggered from screen rotation. |
| 488 | args.argi1 = offset; |
| 489 | args.argi2 = duration; |
| 490 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_OFFSET_ANIMATE, args)); |
| 491 | } |
| 492 | |
| 493 | private void offsetPip(Rect originalBounds, int xOffset, int yOffset, int durationMs) { |
| 494 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 495 | throw new RuntimeException("Callers should call scheduleOffsetPip() instead of this " |
| 496 | + "directly"); |
| 497 | } |
| 498 | if (mTaskInfo == null) { |
| 499 | Log.w(TAG, "mTaskInfo is not set"); |
| 500 | return; |
| 501 | } |
| 502 | final Rect destinationBounds = new Rect(originalBounds); |
| 503 | destinationBounds.offset(xOffset, yOffset); |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 504 | animateResizePip(originalBounds, destinationBounds, TRANSITION_DIRECTION_SAME, durationMs); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | private void resizePip(Rect destinationBounds) { |
| 508 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 509 | throw new RuntimeException("Callers should call scheduleResizePip() instead of this " |
| 510 | + "directly"); |
| 511 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 512 | // Could happen when dismissPip |
| 513 | if (mToken == null || mLeash == null) { |
| 514 | Log.w(TAG, "Abort animation, invalid leash"); |
| 515 | return; |
| 516 | } |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 517 | final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 518 | mSurfaceTransactionHelper |
| 519 | .crop(tx, mLeash, destinationBounds) |
| 520 | .round(tx, mLeash, mInPip); |
| 521 | tx.apply(); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 524 | private void userResizePip(Rect startBounds, Rect destinationBounds) { |
| 525 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 526 | throw new RuntimeException("Callers should call scheduleUserResizePip() instead of " |
| 527 | + "this directly"); |
| 528 | } |
| 529 | // Could happen when dismissPip |
| 530 | if (mToken == null || mLeash == null) { |
| 531 | Log.w(TAG, "Abort animation, invalid leash"); |
| 532 | return; |
| 533 | } |
| 534 | final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 535 | mSurfaceTransactionHelper.scale(tx, mLeash, startBounds, destinationBounds); |
| 536 | tx.apply(); |
| 537 | } |
| 538 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 539 | private void finishResize(SurfaceControl.Transaction tx, Rect destinationBounds, |
| 540 | @PipAnimationController.TransitionDirection int direction) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 541 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 542 | throw new RuntimeException("Callers should call scheduleResizePip() instead of this " |
| 543 | + "directly"); |
| 544 | } |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 545 | mLastReportedBounds.set(destinationBounds); |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 546 | final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 547 | final Rect taskBounds; |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 548 | if (isOutPipDirection(direction)) { |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 549 | // 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] | 550 | // on the task to ensure that the task "matches" the parent's bounds. |
| 551 | taskBounds = (direction == TRANSITION_DIRECTION_TO_FULLSCREEN) |
| 552 | ? null : destinationBounds; |
| 553 | // As for the final windowing mode, simply reset it to undefined. |
| 554 | wct.setWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 555 | } else { |
| 556 | taskBounds = destinationBounds; |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 557 | } |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 558 | if (direction == TRANSITION_DIRECTION_TO_PIP) { |
| 559 | wct.scheduleFinishEnterPip(mToken, taskBounds); |
| 560 | } else { |
| 561 | wct.setBounds(mToken, taskBounds); |
| 562 | } |
| 563 | wct.setBoundsChangeTransaction(mToken, tx); |
| 564 | WindowOrganizer.applyTransaction(wct); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 567 | private void animateResizePip(Rect currentBounds, Rect destinationBounds, |
| 568 | @PipAnimationController.TransitionDirection int direction, int durationMs) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 569 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 570 | throw new RuntimeException("Callers should call scheduleAnimateResizePip() instead of " |
| 571 | + "this directly"); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 572 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 573 | // Could happen when dismissPip |
| 574 | if (mToken == null || mLeash == null) { |
| 575 | Log.w(TAG, "Abort animation, invalid leash"); |
| 576 | return; |
| 577 | } |
| 578 | mUpdateHandler.post(() -> mPipAnimationController |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 579 | .getAnimator(mLeash, currentBounds, destinationBounds) |
| 580 | .setTransitionDirection(direction) |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 581 | .setPipAnimationCallback(mPipAnimationCallback) |
| 582 | .setDuration(durationMs) |
| 583 | .start()); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 586 | private Size getMinimalSize(ActivityInfo activityInfo) { |
| 587 | if (activityInfo == null || activityInfo.windowLayout == null) { |
| 588 | return null; |
| 589 | } |
| 590 | final ActivityInfo.WindowLayout windowLayout = activityInfo.windowLayout; |
Hongwei Wang | 907fd63 | 2020-04-02 17:13:08 -0700 | [diff] [blame] | 591 | // -1 will be populated if an activity specifies defaultWidth/defaultHeight in <layout> |
| 592 | // without minWidth/minHeight |
| 593 | if (windowLayout.minWidth > 0 && windowLayout.minHeight > 0) { |
| 594 | return new Size(windowLayout.minWidth, windowLayout.minHeight); |
| 595 | } |
| 596 | return null; |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 599 | private float getAspectRatioOrDefault(@Nullable PictureInPictureParams params) { |
| 600 | return params == null |
| 601 | ? mPipBoundsHandler.getDefaultAspectRatio() |
| 602 | : params.getAspectRatio(); |
| 603 | } |
| 604 | |
| 605 | /** |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 606 | * Sync with {@link #mSplitDivider} on destination bounds if PiP is going to split screen. |
| 607 | * |
| 608 | * @param destinationBoundsOut contain the updated destination bounds if applicable |
| 609 | * @return {@code true} if destinationBounds is altered for split screen |
| 610 | */ |
| 611 | private boolean syncWithSplitScreenBounds(Rect destinationBoundsOut) { |
| 612 | if (mSplitDivider == null || !mSplitDivider.inSplitMode()) { |
| 613 | // bail early if system is not in split screen mode |
| 614 | return false; |
| 615 | } |
| 616 | // PiP window will go to split-secondary mode instead of fullscreen, populates the |
| 617 | // split screen bounds here. |
| 618 | destinationBoundsOut.set( |
| 619 | mSplitDivider.getView().getNonMinimizedSplitScreenSecondaryBounds()); |
| 620 | return true; |
| 621 | } |
| 622 | |
| 623 | /** |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 624 | * Callback interface for PiP transitions (both from and to PiP mode) |
| 625 | */ |
| 626 | public interface PipTransitionCallback { |
| 627 | /** |
| 628 | * Callback when the pip transition is started. |
| 629 | */ |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 630 | void onPipTransitionStarted(ComponentName activity, int direction); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 631 | |
| 632 | /** |
| 633 | * Callback when the pip transition is finished. |
| 634 | */ |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 635 | void onPipTransitionFinished(ComponentName activity, int direction); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 636 | |
| 637 | /** |
| 638 | * Callback when the pip transition is cancelled. |
| 639 | */ |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 640 | void onPipTransitionCanceled(ComponentName activity, int direction); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 641 | } |
| 642 | } |