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