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 |
| 276 | wct.setWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
| 277 | wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
| 278 | WindowOrganizer.applyTransaction(wct); |
Hongwei Wang | 3d11e1f | 2020-05-28 15:48:06 -0700 | [diff] [blame] | 279 | // Send finished callback though animation is ignored. |
| 280 | sendOnPipTransitionFinished(direction); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 281 | mInPip = false; |
| 282 | } else { |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 283 | final SurfaceControl.Transaction tx = |
| 284 | mSurfaceControlTransactionFactory.getTransaction(); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 285 | mSurfaceTransactionHelper.scale(tx, mLeash, destinationBounds, |
| 286 | mLastReportedBounds); |
| 287 | tx.setWindowCrop(mLeash, destinationBounds.width(), destinationBounds.height()); |
| 288 | wct.setActivityWindowingMode(mToken, direction == TRANSITION_DIRECTION_TO_SPLIT_SCREEN |
| 289 | ? WINDOWING_MODE_SPLIT_SCREEN_SECONDARY |
| 290 | : WINDOWING_MODE_FULLSCREEN); |
| 291 | wct.setBounds(mToken, destinationBounds); |
| 292 | wct.setBoundsChangeTransaction(mToken, tx); |
| 293 | applySyncTransaction(wct, new WindowContainerTransactionCallback() { |
| 294 | @Override |
| 295 | public void onTransactionReady(int id, SurfaceControl.Transaction t) { |
| 296 | t.apply(); |
| 297 | scheduleAnimateResizePip(mLastReportedBounds, destinationBounds, |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 298 | direction, animationDurationMs, null /* updateBoundsCallback */); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 299 | mInPip = false; |
| 300 | } |
| 301 | }); |
| 302 | } |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 303 | mExitingPip = true; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Removes PiP immediately. |
| 308 | */ |
| 309 | public void removePip() { |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 310 | if (!mInPip || mExitingPip || mToken == null) { |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 311 | Log.wtf(TAG, "Not allowed to removePip in current state" |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 312 | + " mInPip=" + mInPip + " mExitingPip=" + mExitingPip + " mToken=" + mToken); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | getUpdateHandler().post(() -> { |
| 316 | try { |
Winson Chung | 9bd523d | 2020-05-29 11:09:52 -0700 | [diff] [blame] | 317 | // Reset the task bounds first to ensure the activity configuration is reset as well |
| 318 | final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 319 | wct.setBounds(mToken, null); |
| 320 | WindowOrganizer.applyTransaction(wct); |
| 321 | |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 322 | ActivityTaskManager.getService().removeStacksInWindowingModes( |
| 323 | new int[]{ WINDOWING_MODE_PINNED }); |
| 324 | } catch (RemoteException e) { |
| 325 | Log.e(TAG, "Failed to remove PiP", e); |
| 326 | } |
| 327 | }); |
| 328 | mInitialState.remove(mToken.asBinder()); |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 329 | mExitingPip = true; |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 332 | @Override |
chaviw | 7de5000 | 2020-04-27 12:33:30 -0700 | [diff] [blame] | 333 | public void onTaskAppeared(ActivityManager.RunningTaskInfo info, SurfaceControl leash) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 334 | Objects.requireNonNull(info, "Requires RunningTaskInfo"); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 335 | mTaskInfo = info; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 336 | mToken = mTaskInfo.token; |
| 337 | mInPip = true; |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 338 | mExitingPip = false; |
chaviw | 7de5000 | 2020-04-27 12:33:30 -0700 | [diff] [blame] | 339 | mLeash = leash; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 340 | mInitialState.put(mToken.asBinder(), new Configuration(mTaskInfo.configuration)); |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 341 | mPictureInPictureParams = mTaskInfo.pictureInPictureParams; |
| 342 | |
| 343 | if (mShouldDeferEnteringPip) { |
| 344 | if (DEBUG) Log.d(TAG, "Defer entering PiP animation, fixed rotation is ongoing"); |
| 345 | // if deferred, hide the surface till fixed rotation is completed |
| 346 | final SurfaceControl.Transaction tx = |
| 347 | mSurfaceControlTransactionFactory.getTransaction(); |
| 348 | tx.setAlpha(mLeash, 0f); |
Winson Chung | 14a657c | 2020-05-28 10:47:40 -0700 | [diff] [blame] | 349 | tx.show(mLeash); |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 350 | tx.apply(); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | final Rect destinationBounds = mPipBoundsHandler.getDestinationBounds( |
| 355 | mTaskInfo.topActivity, getAspectRatioOrDefault(mPictureInPictureParams), |
| 356 | null /* bounds */, getMinimalSize(mTaskInfo.topActivityInfo)); |
| 357 | Objects.requireNonNull(destinationBounds, "Missing destination bounds"); |
| 358 | final Rect currentBounds = mTaskInfo.configuration.windowConfiguration.getBounds(); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 359 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 360 | if (mOneShotAnimationType == ANIM_TYPE_BOUNDS) { |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 361 | scheduleAnimateResizePip(currentBounds, destinationBounds, |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 362 | TRANSITION_DIRECTION_TO_PIP, mEnterExitAnimationDuration, |
| 363 | null /* updateBoundsCallback */); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 364 | } else if (mOneShotAnimationType == ANIM_TYPE_ALPHA) { |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 365 | enterPipWithAlphaAnimation(destinationBounds, mEnterExitAnimationDuration); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 366 | mOneShotAnimationType = ANIM_TYPE_BOUNDS; |
| 367 | } else { |
| 368 | throw new RuntimeException("Unrecognized animation type: " + mOneShotAnimationType); |
| 369 | } |
| 370 | } |
| 371 | |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 372 | private void enterPipWithAlphaAnimation(Rect destinationBounds, long durationMs) { |
| 373 | // If we are fading the PIP in, then we should move the pip to the final location as |
| 374 | // soon as possible, but set the alpha immediately since the transaction can take a |
| 375 | // while to process |
| 376 | final SurfaceControl.Transaction tx = |
| 377 | mSurfaceControlTransactionFactory.getTransaction(); |
| 378 | tx.setAlpha(mLeash, 0f); |
| 379 | tx.apply(); |
| 380 | final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 381 | wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
| 382 | wct.setBounds(mToken, destinationBounds); |
| 383 | wct.scheduleFinishEnterPip(mToken, destinationBounds); |
| 384 | applySyncTransaction(wct, new WindowContainerTransactionCallback() { |
| 385 | @Override |
| 386 | public void onTransactionReady(int id, SurfaceControl.Transaction t) { |
| 387 | t.apply(); |
| 388 | mUpdateHandler.post(() -> mPipAnimationController |
| 389 | .getAnimator(mLeash, destinationBounds, 0f, 1f) |
| 390 | .setTransitionDirection(TRANSITION_DIRECTION_TO_PIP) |
| 391 | .setPipAnimationCallback(mPipAnimationCallback) |
| 392 | .setDuration(durationMs) |
| 393 | .start()); |
| 394 | } |
| 395 | }); |
| 396 | } |
| 397 | |
Hongwei Wang | 3d11e1f | 2020-05-28 15:48:06 -0700 | [diff] [blame] | 398 | private void sendOnPipTransitionStarted( |
| 399 | @PipAnimationController.TransitionDirection int direction) { |
| 400 | mMainHandler.post(() -> { |
| 401 | for (int i = mPipTransitionCallbacks.size() - 1; i >= 0; i--) { |
| 402 | final PipTransitionCallback callback = mPipTransitionCallbacks.get(i); |
| 403 | callback.onPipTransitionStarted(mTaskInfo.baseActivity, direction); |
| 404 | } |
| 405 | }); |
| 406 | } |
| 407 | |
| 408 | private void sendOnPipTransitionFinished( |
| 409 | @PipAnimationController.TransitionDirection int direction) { |
| 410 | mMainHandler.post(() -> { |
| 411 | for (int i = mPipTransitionCallbacks.size() - 1; i >= 0; i--) { |
| 412 | final PipTransitionCallback callback = mPipTransitionCallbacks.get(i); |
| 413 | callback.onPipTransitionFinished(mTaskInfo.baseActivity, direction); |
| 414 | } |
| 415 | }); |
| 416 | } |
| 417 | |
| 418 | private void sendOnPipTransitionCancelled( |
| 419 | @PipAnimationController.TransitionDirection int direction) { |
| 420 | mMainHandler.post(() -> { |
| 421 | for (int i = mPipTransitionCallbacks.size() - 1; i >= 0; i--) { |
| 422 | final PipTransitionCallback callback = mPipTransitionCallbacks.get(i); |
| 423 | callback.onPipTransitionCanceled(mTaskInfo.baseActivity, direction); |
| 424 | } |
| 425 | }); |
| 426 | } |
| 427 | |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 428 | /** |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 429 | * 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] | 430 | * Meanwhile this callback is invoked whenever the task is removed. For instance: |
| 431 | * - as a result of removeStacksInWindowingModes from WM |
| 432 | * - activity itself is died |
Hongwei Wang | 180162e | 2020-04-22 14:40:32 -0700 | [diff] [blame] | 433 | * Nevertheless, we simply update the internal state here as all the heavy lifting should |
| 434 | * have been done in WM. |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 435 | */ |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 436 | @Override |
Wale Ogunwale | dec3408 | 2020-03-22 09:45:00 -0700 | [diff] [blame] | 437 | public void onTaskVanished(ActivityManager.RunningTaskInfo info) { |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 438 | if (!mInPip) { |
| 439 | return; |
| 440 | } |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 441 | final WindowContainerToken token = info.token; |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 442 | Objects.requireNonNull(token, "Requires valid WindowContainerToken"); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 443 | if (token.asBinder() != mToken.asBinder()) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 444 | Log.wtf(TAG, "Unrecognized token: " + token); |
| 445 | return; |
| 446 | } |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 447 | mShouldDeferEnteringPip = false; |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 448 | mPictureInPictureParams = null; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 449 | mInPip = false; |
Winson Chung | c46cbae | 2020-05-27 21:36:09 -0700 | [diff] [blame] | 450 | mExitingPip = false; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | @Override |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 454 | public void onTaskInfoChanged(ActivityManager.RunningTaskInfo info) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 455 | Objects.requireNonNull(mToken, "onTaskInfoChanged requires valid existing mToken"); |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 456 | final PictureInPictureParams newParams = info.pictureInPictureParams; |
Winson Chung | 6d8a1956 | 2020-05-21 11:27:28 -0700 | [diff] [blame] | 457 | if (newParams == null || !applyPictureInPictureParams(newParams)) { |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 458 | Log.d(TAG, "Ignored onTaskInfoChanged with PiP param: " + newParams); |
| 459 | return; |
| 460 | } |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 461 | final Rect destinationBounds = mPipBoundsHandler.getDestinationBounds( |
Hongwei Wang | 309cffa | 2020-04-06 11:11:01 -0700 | [diff] [blame] | 462 | info.topActivity, getAspectRatioOrDefault(newParams), |
Hongwei Wang | b5333e9 | 2020-05-29 12:51:52 -0700 | [diff] [blame] | 463 | mLastReportedBounds, getMinimalSize(info.topActivityInfo), |
| 464 | true /* userCurrentMinEdgeSize */); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 465 | Objects.requireNonNull(destinationBounds, "Missing destination bounds"); |
Hongwei Wang | d39583a | 2020-03-04 11:14:32 -0800 | [diff] [blame] | 466 | scheduleAnimateResizePip(destinationBounds, mEnterExitAnimationDuration, |
| 467 | null /* updateBoundsCallback */); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 470 | @Override |
| 471 | public void onBackPressedOnTaskRoot(ActivityManager.RunningTaskInfo taskInfo) { |
| 472 | // Do nothing |
| 473 | } |
| 474 | |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 475 | @Override |
| 476 | public void onFixedRotationStarted(int displayId, int newRotation) { |
| 477 | mShouldDeferEnteringPip = true; |
| 478 | } |
| 479 | |
| 480 | @Override |
| 481 | public void onFixedRotationFinished(int displayId) { |
| 482 | if (mShouldDeferEnteringPip && mInPip) { |
| 483 | final Rect destinationBounds = mPipBoundsHandler.getDestinationBounds( |
| 484 | mTaskInfo.topActivity, getAspectRatioOrDefault(mPictureInPictureParams), |
| 485 | null /* bounds */, getMinimalSize(mTaskInfo.topActivityInfo)); |
| 486 | // schedule a regular animation to ensure all the callbacks are still being sent |
| 487 | enterPipWithAlphaAnimation(destinationBounds, 0 /* durationMs */); |
| 488 | } |
| 489 | mShouldDeferEnteringPip = false; |
| 490 | } |
| 491 | |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 492 | /** |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 493 | * TODO(b/152809058): consolidate the display info handling logic in SysUI |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 494 | * |
| 495 | * @param destinationBoundsOut the current destination bounds will be populated to this param |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 496 | */ |
| 497 | @SuppressWarnings("unchecked") |
Hongwei Wang | e0412d1 | 2020-04-13 18:27:25 -0700 | [diff] [blame] | 498 | public void onMovementBoundsChanged(Rect destinationBoundsOut, boolean fromRotation, |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 499 | boolean fromImeAdjustment, boolean fromShelfAdjustment) { |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 500 | final PipAnimationController.PipTransitionAnimator animator = |
| 501 | mPipAnimationController.getCurrentAnimator(); |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 502 | if (animator == null || !animator.isRunning() |
| 503 | || animator.getTransitionDirection() != TRANSITION_DIRECTION_TO_PIP) { |
Hongwei Wang | e0412d1 | 2020-04-13 18:27:25 -0700 | [diff] [blame] | 504 | if (mInPip && fromRotation) { |
| 505 | // this could happen if rotation finishes before the animation |
| 506 | mLastReportedBounds.set(destinationBoundsOut); |
| 507 | scheduleFinishResizePip(mLastReportedBounds); |
Ben Lin | ba4b76b | 2020-06-05 16:39:17 -0700 | [diff] [blame] | 508 | } else { |
| 509 | // There could be an animation on-going. If there is one on-going, last-reported |
| 510 | // bounds isn't yet updated. We'll use the animator's bounds instead. |
| 511 | if (animator != null && animator.isRunning()) { |
| 512 | if (!animator.getDestinationBounds().isEmpty()) { |
| 513 | destinationBoundsOut.set(animator.getDestinationBounds()); |
| 514 | } |
| 515 | } else { |
| 516 | if (!mLastReportedBounds.isEmpty()) { |
| 517 | destinationBoundsOut.set(mLastReportedBounds); |
| 518 | } |
| 519 | } |
Hongwei Wang | e0412d1 | 2020-04-13 18:27:25 -0700 | [diff] [blame] | 520 | } |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 521 | return; |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 522 | } |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 523 | |
| 524 | final Rect currentDestinationBounds = animator.getDestinationBounds(); |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 525 | destinationBoundsOut.set(currentDestinationBounds); |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 526 | if (!fromImeAdjustment && !fromShelfAdjustment |
| 527 | && mPipBoundsHandler.getDisplayBounds().contains(currentDestinationBounds)) { |
| 528 | // no need to update the destination bounds, bail early |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | final Rect newDestinationBounds = mPipBoundsHandler.getDestinationBounds( |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 533 | mTaskInfo.topActivity, getAspectRatioOrDefault(mPictureInPictureParams), |
Hongwei Wang | 6e68af5 | 2020-04-06 12:34:06 -0700 | [diff] [blame] | 534 | null /* bounds */, getMinimalSize(mTaskInfo.topActivityInfo)); |
| 535 | if (newDestinationBounds.equals(currentDestinationBounds)) return; |
| 536 | if (animator.getAnimationType() == ANIM_TYPE_BOUNDS) { |
| 537 | animator.updateEndValue(newDestinationBounds); |
| 538 | } |
| 539 | animator.setDestinationBounds(newDestinationBounds); |
Hongwei Wang | 3c981f6 | 2020-04-07 16:16:26 -0700 | [diff] [blame] | 540 | destinationBoundsOut.set(newDestinationBounds); |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /** |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 544 | * @return {@code true} if the aspect ratio is changed since no other parameters within |
| 545 | * {@link PictureInPictureParams} would affect the bounds. |
| 546 | */ |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 547 | private boolean applyPictureInPictureParams(@NonNull PictureInPictureParams params) { |
| 548 | final boolean changed = (mPictureInPictureParams == null) ? true : !Objects.equals( |
| 549 | mPictureInPictureParams.getAspectRatioRational(), params.getAspectRatioRational()); |
| 550 | if (changed) { |
| 551 | mPictureInPictureParams = params; |
| 552 | mPipBoundsHandler.onAspectRatioChanged(params.getAspectRatio()); |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 553 | } |
Hongwei Wang | 8c95ce5 | 2020-04-30 15:06:12 -0700 | [diff] [blame] | 554 | return changed; |
Hongwei Wang | 8e8a8ae | 2020-03-03 11:06:56 -0800 | [diff] [blame] | 555 | } |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 556 | |
| 557 | /** |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 558 | * Animates resizing of the pinned stack given the duration. |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 559 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 560 | public void scheduleAnimateResizePip(Rect toBounds, int duration, |
| 561 | Consumer<Rect> updateBoundsCallback) { |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 562 | if (mShouldDeferEnteringPip) { |
| 563 | Log.d(TAG, "skip scheduleAnimateResizePip, entering pip deferred"); |
| 564 | return; |
| 565 | } |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 566 | scheduleAnimateResizePip(mLastReportedBounds, toBounds, |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 567 | TRANSITION_DIRECTION_NONE, duration, updateBoundsCallback); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 570 | private void scheduleAnimateResizePip(Rect currentBounds, Rect destinationBounds, |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 571 | @PipAnimationController.TransitionDirection int direction, int durationMs, |
| 572 | Consumer<Rect> updateBoundsCallback) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 573 | if (!mInPip) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 574 | // 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] | 575 | return; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 576 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 577 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 578 | SomeArgs args = SomeArgs.obtain(); |
| 579 | args.arg1 = updateBoundsCallback; |
| 580 | args.arg2 = currentBounds; |
| 581 | args.arg3 = destinationBounds; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 582 | args.argi1 = direction; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 583 | args.argi2 = durationMs; |
| 584 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_RESIZE_ANIMATE, args)); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Directly perform manipulation/resize on the leash. This will not perform any |
| 589 | * {@link WindowContainerTransaction} until {@link #scheduleFinishResizePip} is called. |
| 590 | */ |
| 591 | public void scheduleResizePip(Rect toBounds, Consumer<Rect> updateBoundsCallback) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 592 | SomeArgs args = SomeArgs.obtain(); |
| 593 | args.arg1 = updateBoundsCallback; |
| 594 | args.arg2 = toBounds; |
| 595 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_RESIZE_IMMEDIATE, args)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 598 | /** |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 599 | * Directly perform a scaled matrix transformation on the leash. This will not perform any |
| 600 | * {@link WindowContainerTransaction} until {@link #scheduleFinishResizePip} is called. |
| 601 | */ |
| 602 | public void scheduleUserResizePip(Rect startBounds, Rect toBounds, |
| 603 | Consumer<Rect> updateBoundsCallback) { |
| 604 | SomeArgs args = SomeArgs.obtain(); |
| 605 | args.arg1 = updateBoundsCallback; |
| 606 | args.arg2 = startBounds; |
| 607 | args.arg3 = toBounds; |
| 608 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_RESIZE_USER, args)); |
| 609 | } |
| 610 | |
| 611 | /** |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 612 | * Finish an intermediate resize operation. This is expected to be called after |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 613 | * {@link #scheduleResizePip}. |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 614 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 615 | public void scheduleFinishResizePip(Rect destinationBounds) { |
Ben Lin | 4a8e257 | 2020-05-08 15:55:15 -0700 | [diff] [blame] | 616 | scheduleFinishResizePip(destinationBounds, null); |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * Same as {@link #scheduleFinishResizePip} but with a callback. |
| 621 | */ |
| 622 | public void scheduleFinishResizePip(Rect destinationBounds, |
| 623 | Consumer<Rect> updateBoundsCallback) { |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 624 | final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 625 | mSurfaceTransactionHelper |
| 626 | .crop(tx, mLeash, destinationBounds) |
Ben Lin | f603b27 | 2020-03-23 15:39:07 -0700 | [diff] [blame] | 627 | .resetScale(tx, mLeash, destinationBounds) |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 628 | .round(tx, mLeash, mInPip); |
Ben Lin | 4a8e257 | 2020-05-08 15:55:15 -0700 | [diff] [blame] | 629 | scheduleFinishResizePip(tx, destinationBounds, TRANSITION_DIRECTION_NONE, |
| 630 | updateBoundsCallback); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 633 | private void scheduleFinishResizePip(SurfaceControl.Transaction tx, |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 634 | Rect destinationBounds, @PipAnimationController.TransitionDirection int direction, |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 635 | Consumer<Rect> updateBoundsCallback) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 636 | if (!mInPip) { |
| 637 | // can be initiated in other component, ignore if we are no longer in PIP |
| 638 | return; |
| 639 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 640 | SomeArgs args = SomeArgs.obtain(); |
| 641 | args.arg1 = updateBoundsCallback; |
| 642 | args.arg2 = tx; |
| 643 | args.arg3 = destinationBounds; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 644 | args.argi1 = direction; |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 645 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_FINISH_RESIZE, args)); |
| 646 | } |
| 647 | |
| 648 | /** |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 649 | * 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] | 650 | */ |
| 651 | public void scheduleOffsetPip(Rect originalBounds, int offset, int duration, |
| 652 | Consumer<Rect> updateBoundsCallback) { |
| 653 | if (!mInPip) { |
Hongwei Wang | e8e3286 | 2020-04-08 13:23:45 -0700 | [diff] [blame] | 654 | // 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] | 655 | return; |
| 656 | } |
Hongwei Wang | f4e4bab | 2020-05-19 11:28:32 -0700 | [diff] [blame] | 657 | if (mShouldDeferEnteringPip) { |
| 658 | Log.d(TAG, "skip scheduleOffsetPip, entering pip deferred"); |
| 659 | return; |
| 660 | } |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 661 | SomeArgs args = SomeArgs.obtain(); |
| 662 | args.arg1 = updateBoundsCallback; |
| 663 | args.arg2 = originalBounds; |
| 664 | // offset would be zero if triggered from screen rotation. |
| 665 | args.argi1 = offset; |
| 666 | args.argi2 = duration; |
| 667 | mUpdateHandler.sendMessage(mUpdateHandler.obtainMessage(MSG_OFFSET_ANIMATE, args)); |
| 668 | } |
| 669 | |
| 670 | private void offsetPip(Rect originalBounds, int xOffset, int yOffset, int durationMs) { |
| 671 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 672 | throw new RuntimeException("Callers should call scheduleOffsetPip() instead of this " |
| 673 | + "directly"); |
| 674 | } |
| 675 | if (mTaskInfo == null) { |
| 676 | Log.w(TAG, "mTaskInfo is not set"); |
| 677 | return; |
| 678 | } |
| 679 | final Rect destinationBounds = new Rect(originalBounds); |
| 680 | destinationBounds.offset(xOffset, yOffset); |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 681 | animateResizePip(originalBounds, destinationBounds, TRANSITION_DIRECTION_SAME, durationMs); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | private void resizePip(Rect destinationBounds) { |
| 685 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 686 | throw new RuntimeException("Callers should call scheduleResizePip() instead of this " |
| 687 | + "directly"); |
| 688 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 689 | // Could happen when exitPip |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 690 | if (mToken == null || mLeash == null) { |
| 691 | Log.w(TAG, "Abort animation, invalid leash"); |
| 692 | return; |
| 693 | } |
Ben Lin | 5400de3 | 2020-05-06 19:28:09 -0700 | [diff] [blame] | 694 | mLastReportedBounds.set(destinationBounds); |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 695 | final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 696 | mSurfaceTransactionHelper |
| 697 | .crop(tx, mLeash, destinationBounds) |
| 698 | .round(tx, mLeash, mInPip); |
| 699 | tx.apply(); |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 702 | private void userResizePip(Rect startBounds, Rect destinationBounds) { |
| 703 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 704 | throw new RuntimeException("Callers should call scheduleUserResizePip() instead of " |
| 705 | + "this directly"); |
| 706 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 707 | // Could happen when exitPip |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 708 | if (mToken == null || mLeash == null) { |
| 709 | Log.w(TAG, "Abort animation, invalid leash"); |
| 710 | return; |
| 711 | } |
Josh Tsuji | a8ed97e | 2020-06-16 17:00:59 -0400 | [diff] [blame] | 712 | |
| 713 | if (startBounds.isEmpty() || destinationBounds.isEmpty()) { |
| 714 | Log.w(TAG, "Attempted to user resize PIP to or from empty bounds, aborting."); |
| 715 | return; |
| 716 | } |
| 717 | |
Ben Lin | 75ba9c3 | 2020-03-19 17:55:12 -0700 | [diff] [blame] | 718 | final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); |
| 719 | mSurfaceTransactionHelper.scale(tx, mLeash, startBounds, destinationBounds); |
| 720 | tx.apply(); |
| 721 | } |
| 722 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 723 | private void finishResize(SurfaceControl.Transaction tx, Rect destinationBounds, |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 724 | @PipAnimationController.TransitionDirection int direction, |
| 725 | @PipAnimationController.AnimationType int type) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 726 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 727 | throw new RuntimeException("Callers should call scheduleResizePip() instead of this " |
| 728 | + "directly"); |
| 729 | } |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 730 | mLastReportedBounds.set(destinationBounds); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 731 | if (isInPipDirection(direction) && type == ANIM_TYPE_ALPHA) { |
| 732 | return; |
| 733 | } |
| 734 | |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 735 | final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 736 | final Rect taskBounds; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 737 | if (isInPipDirection(direction)) { |
| 738 | // If we are animating from fullscreen using a bounds animation, then reset the |
| 739 | // activity windowing mode set by WM, and set the task bounds to the final bounds |
| 740 | taskBounds = destinationBounds; |
| 741 | wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
| 742 | wct.scheduleFinishEnterPip(mToken, destinationBounds); |
| 743 | } else if (isOutPipDirection(direction)) { |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 744 | // 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] | 745 | // on the task to ensure that the task "matches" the parent's bounds. |
| 746 | taskBounds = (direction == TRANSITION_DIRECTION_TO_FULLSCREEN) |
| 747 | ? null : destinationBounds; |
jorgegil@google.com | 79aae3c | 2020-06-11 10:37:52 -0700 | [diff] [blame] | 748 | // Reset the final windowing mode. |
| 749 | wct.setWindowingMode(mToken, getOutPipWindowingMode()); |
| 750 | // Simply reset the activity mode set prior to the animation running. |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 751 | wct.setActivityWindowingMode(mToken, WINDOWING_MODE_UNDEFINED); |
Evan Rosky | dc1d29f | 2020-05-04 13:46:14 -0700 | [diff] [blame] | 752 | if (mSplitDivider != null && direction == TRANSITION_DIRECTION_TO_SPLIT_SCREEN) { |
| 753 | wct.reparent(mToken, mSplitDivider.getSecondaryRoot(), true /* onTop */); |
| 754 | } |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 755 | } else { |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 756 | // Just a resize in PIP |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 757 | taskBounds = destinationBounds; |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 758 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 759 | |
| 760 | wct.setBounds(mToken, taskBounds); |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 761 | wct.setBoundsChangeTransaction(mToken, tx); |
jorgegil@google.com | 79aae3c | 2020-06-11 10:37:52 -0700 | [diff] [blame] | 762 | applyFinishBoundsResize(wct, direction); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Applies the window container transaction to finish a bounds resize. |
| 767 | * |
| 768 | * Called by {@link #finishResize(SurfaceControl.Transaction, Rect, int, int)}} once it has |
| 769 | * finished preparing the transaction. It allows subclasses to modify the transaction before |
| 770 | * applying it. |
| 771 | */ |
| 772 | public void applyFinishBoundsResize(@NonNull WindowContainerTransaction wct, |
| 773 | @PipAnimationController.TransitionDirection int direction) { |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 774 | WindowOrganizer.applyTransaction(wct); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 775 | } |
| 776 | |
jorgegil@google.com | 79aae3c | 2020-06-11 10:37:52 -0700 | [diff] [blame] | 777 | /** |
| 778 | * The windowing mode to restore to when resizing out of PIP direction. Defaults to undefined |
| 779 | * and can be overridden to restore to an alternate windowing mode. |
| 780 | */ |
| 781 | public int getOutPipWindowingMode() { |
| 782 | // By default, simply reset the windowing mode to undefined. |
| 783 | return WINDOWING_MODE_UNDEFINED; |
| 784 | } |
| 785 | |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 786 | private void animateResizePip(Rect currentBounds, Rect destinationBounds, |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 787 | @PipAnimationController.TransitionDirection int direction, int durationMs) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 788 | if (Looper.myLooper() != mUpdateHandler.getLooper()) { |
| 789 | throw new RuntimeException("Callers should call scheduleAnimateResizePip() instead of " |
| 790 | + "this directly"); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 791 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 792 | // Could happen when exitPip |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 793 | if (mToken == null || mLeash == null) { |
| 794 | Log.w(TAG, "Abort animation, invalid leash"); |
| 795 | return; |
| 796 | } |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 797 | mPipAnimationController |
Winson Chung | d730254 | 2020-06-08 17:09:45 +0000 | [diff] [blame] | 798 | .getAnimator(mLeash, currentBounds, destinationBounds) |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 799 | .setTransitionDirection(direction) |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 800 | .setPipAnimationCallback(mPipAnimationCallback) |
| 801 | .setDuration(durationMs) |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 802 | .start(); |
Ben Lin | 7d6b8e7 | 2020-02-27 17:48:16 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 805 | private Size getMinimalSize(ActivityInfo activityInfo) { |
| 806 | if (activityInfo == null || activityInfo.windowLayout == null) { |
| 807 | return null; |
| 808 | } |
| 809 | final ActivityInfo.WindowLayout windowLayout = activityInfo.windowLayout; |
Hongwei Wang | 907fd63 | 2020-04-02 17:13:08 -0700 | [diff] [blame] | 810 | // -1 will be populated if an activity specifies defaultWidth/defaultHeight in <layout> |
| 811 | // without minWidth/minHeight |
| 812 | if (windowLayout.minWidth > 0 && windowLayout.minHeight > 0) { |
| 813 | return new Size(windowLayout.minWidth, windowLayout.minHeight); |
| 814 | } |
| 815 | return null; |
Hongwei Wang | 2e725be | 2020-03-10 11:01:28 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 818 | private float getAspectRatioOrDefault(@Nullable PictureInPictureParams params) { |
| 819 | return params == null |
| 820 | ? mPipBoundsHandler.getDefaultAspectRatio() |
| 821 | : params.getAspectRatio(); |
| 822 | } |
| 823 | |
| 824 | /** |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 825 | * Sync with {@link #mSplitDivider} on destination bounds if PiP is going to split screen. |
| 826 | * |
| 827 | * @param destinationBoundsOut contain the updated destination bounds if applicable |
| 828 | * @return {@code true} if destinationBounds is altered for split screen |
| 829 | */ |
| 830 | private boolean syncWithSplitScreenBounds(Rect destinationBoundsOut) { |
Evan Rosky | 90d5b6d | 2020-05-13 19:39:05 -0700 | [diff] [blame] | 831 | if (mSplitDivider == null || !mSplitDivider.isDividerVisible()) { |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 832 | // bail early if system is not in split screen mode |
| 833 | return false; |
| 834 | } |
| 835 | // PiP window will go to split-secondary mode instead of fullscreen, populates the |
| 836 | // split screen bounds here. |
| 837 | destinationBoundsOut.set( |
| 838 | mSplitDivider.getView().getNonMinimizedSplitScreenSecondaryBounds()); |
| 839 | return true; |
| 840 | } |
| 841 | |
| 842 | /** |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 843 | * Dumps internal states. |
| 844 | */ |
| 845 | public void dump(PrintWriter pw, String prefix) { |
| 846 | final String innerPrefix = prefix + " "; |
| 847 | pw.println(prefix + TAG); |
| 848 | pw.println(innerPrefix + "mTaskInfo=" + mTaskInfo); |
| 849 | pw.println(innerPrefix + "mToken=" + mToken |
| 850 | + " binder=" + (mToken != null ? mToken.asBinder() : null)); |
| 851 | pw.println(innerPrefix + "mLeash=" + mLeash); |
| 852 | pw.println(innerPrefix + "mInPip=" + mInPip); |
| 853 | pw.println(innerPrefix + "mOneShotAnimationType=" + mOneShotAnimationType); |
| 854 | pw.println(innerPrefix + "mPictureInPictureParams=" + mPictureInPictureParams); |
| 855 | pw.println(innerPrefix + "mLastReportedBounds=" + mLastReportedBounds); |
| 856 | pw.println(innerPrefix + "mInitialState:"); |
| 857 | for (Map.Entry<IBinder, Configuration> e : mInitialState.entrySet()) { |
| 858 | pw.println(innerPrefix + " binder=" + e.getKey() |
| 859 | + " winConfig=" + e.getValue().windowConfiguration); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /** |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 864 | * Callback interface for PiP transitions (both from and to PiP mode) |
| 865 | */ |
| 866 | public interface PipTransitionCallback { |
| 867 | /** |
| 868 | * Callback when the pip transition is started. |
| 869 | */ |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 870 | void onPipTransitionStarted(ComponentName activity, int direction); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 871 | |
| 872 | /** |
| 873 | * Callback when the pip transition is finished. |
| 874 | */ |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 875 | void onPipTransitionFinished(ComponentName activity, int direction); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 876 | |
| 877 | /** |
| 878 | * Callback when the pip transition is cancelled. |
| 879 | */ |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 880 | void onPipTransitionCanceled(ComponentName activity, int direction); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 881 | } |
| 882 | } |