Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.server.wm; |
| 18 | |
| 19 | import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID; |
Winson Chung | 9c84441 | 2017-04-27 17:36:41 -0700 | [diff] [blame] | 20 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 21 | import static com.android.server.wm.BoundsAnimationController.NO_PIP_MODE_CHANGED_CALLBACKS; |
| 22 | import static com.android.server.wm.BoundsAnimationController.SCHEDULE_PIP_MODE_CHANGED_ON_END; |
| 23 | import static com.android.server.wm.BoundsAnimationController.SCHEDULE_PIP_MODE_CHANGED_ON_START; |
| 24 | import static com.android.server.wm.BoundsAnimationController.SchedulePipModeChangedState; |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 25 | |
| 26 | import android.app.RemoteAction; |
| 27 | import android.graphics.Rect; |
| 28 | |
| 29 | import com.android.server.UiThread; |
| 30 | |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * Controller for the pinned stack container. See {@link StackWindowController}. |
| 35 | */ |
| 36 | public class PinnedStackWindowController extends StackWindowController { |
| 37 | |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 38 | private Rect mTmpFromBounds = new Rect(); |
| 39 | private Rect mTmpToBounds = new Rect(); |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 40 | |
Winson Chung | 3a68287 | 2017-04-10 14:38:05 -0700 | [diff] [blame] | 41 | public PinnedStackWindowController(int stackId, PinnedStackWindowListener listener, |
| 42 | int displayId, boolean onTop, Rect outBounds) { |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 43 | super(stackId, listener, displayId, onTop, outBounds, WindowManagerService.getInstance()); |
| 44 | } |
| 45 | |
| 46 | /** |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 47 | * @return the {@param currentStackBounds} transformed to the give {@param aspectRatio}. If |
| 48 | * {@param currentStackBounds} is null, then the {@param aspectRatio} is applied to the |
| 49 | * default bounds. |
Winson Chung | 9c84441 | 2017-04-27 17:36:41 -0700 | [diff] [blame] | 50 | */ |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 51 | public Rect getPictureInPictureBounds(float aspectRatio, Rect stackBounds) { |
Winson Chung | 9c84441 | 2017-04-27 17:36:41 -0700 | [diff] [blame] | 52 | synchronized (mWindowMap) { |
| 53 | if (!mService.mSupportsPictureInPicture || mContainer == null) { |
| 54 | return null; |
| 55 | } |
| 56 | |
Winson Chung | 9c84441 | 2017-04-27 17:36:41 -0700 | [diff] [blame] | 57 | final DisplayContent displayContent = mContainer.getDisplayContent(); |
| 58 | if (displayContent == null) { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | final PinnedStackController pinnedStackController = |
| 63 | displayContent.getPinnedStackController(); |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 64 | if (stackBounds == null) { |
| 65 | // Calculate the aspect ratio bounds from the default bounds |
Winson Chung | 9c84441 | 2017-04-27 17:36:41 -0700 | [diff] [blame] | 66 | stackBounds = pinnedStackController.getDefaultBounds(); |
| 67 | } |
| 68 | |
| 69 | if (pinnedStackController.isValidPictureInPictureAspectRatio(aspectRatio)) { |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 70 | return pinnedStackController.transformBoundsToAspectRatio(stackBounds, aspectRatio, |
| 71 | true /* useCurrentMinEdgeSize */); |
Winson Chung | 9c84441 | 2017-04-27 17:36:41 -0700 | [diff] [blame] | 72 | } else { |
| 73 | return stackBounds; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 79 | * Animates the pinned stack. |
| 80 | */ |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 81 | public void animateResizePinnedStack(Rect toBounds, Rect sourceHintBounds, |
Winson Chung | e7ba686 | 2017-05-24 12:13:33 -0700 | [diff] [blame] | 82 | int animationDuration, boolean fromFullscreen) { |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 83 | synchronized (mWindowMap) { |
| 84 | if (mContainer == null) { |
| 85 | throw new IllegalArgumentException("Pinned stack container not found :("); |
| 86 | } |
| 87 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 88 | // Get the from-bounds |
| 89 | final Rect fromBounds = new Rect(); |
| 90 | mContainer.getBounds(fromBounds); |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 91 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 92 | // Get non-null fullscreen to-bounds for animating if the bounds are null |
| 93 | @SchedulePipModeChangedState int schedulePipModeChangedState = |
| 94 | NO_PIP_MODE_CHANGED_CALLBACKS; |
| 95 | final boolean toFullscreen = toBounds == null; |
| 96 | if (toFullscreen) { |
Winson Chung | e7ba686 | 2017-05-24 12:13:33 -0700 | [diff] [blame] | 97 | if (fromFullscreen) { |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 98 | throw new IllegalArgumentException("Should not defer scheduling PiP mode" |
| 99 | + " change on animation to fullscreen."); |
| 100 | } |
| 101 | schedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_START; |
| 102 | |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 103 | mService.getStackBounds(FULLSCREEN_WORKSPACE_STACK_ID, mTmpToBounds); |
| 104 | if (!mTmpToBounds.isEmpty()) { |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 105 | // If there is a fullscreen bounds, use that |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 106 | toBounds = new Rect(mTmpToBounds); |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 107 | } else { |
| 108 | // Otherwise, use the display bounds |
| 109 | toBounds = new Rect(); |
| 110 | mContainer.getDisplayContent().getLogicalDisplayRect(toBounds); |
| 111 | } |
Winson Chung | e7ba686 | 2017-05-24 12:13:33 -0700 | [diff] [blame] | 112 | } else if (fromFullscreen) { |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 113 | schedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_END; |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 116 | mContainer.setAnimationFinalBounds(sourceHintBounds, toBounds, toFullscreen); |
| 117 | |
| 118 | final Rect finalToBounds = toBounds; |
| 119 | final @SchedulePipModeChangedState int finalSchedulePipModeChangedState = |
| 120 | schedulePipModeChangedState; |
Winson Chung | e7ba686 | 2017-05-24 12:13:33 -0700 | [diff] [blame] | 121 | mService.mBoundsAnimationController.getHandler().post(() -> { |
Winson Chung | bb25054 | 2017-03-20 20:06:46 -0700 | [diff] [blame] | 122 | if (mContainer == null) { |
| 123 | return; |
| 124 | } |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 125 | mService.mBoundsAnimationController.animateBounds(mContainer, fromBounds, |
| 126 | finalToBounds, animationDuration, finalSchedulePipModeChangedState, |
Winson Chung | e7ba686 | 2017-05-24 12:13:33 -0700 | [diff] [blame] | 127 | fromFullscreen, toFullscreen); |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 128 | }); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Sets the current picture-in-picture aspect ratio. |
| 134 | */ |
| 135 | public void setPictureInPictureAspectRatio(float aspectRatio) { |
| 136 | synchronized (mWindowMap) { |
| 137 | if (!mService.mSupportsPictureInPicture || mContainer == null) { |
| 138 | return; |
| 139 | } |
| 140 | |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 141 | final PinnedStackController pinnedStackController = |
| 142 | mContainer.getDisplayContent().getPinnedStackController(); |
Winson Chung | c95ff84 | 2017-03-21 10:20:20 -0700 | [diff] [blame] | 143 | |
| 144 | if (Float.compare(aspectRatio, pinnedStackController.getAspectRatio()) != 0) { |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 145 | mContainer.getAnimationOrCurrentBounds(mTmpFromBounds); |
| 146 | mTmpToBounds.set(mTmpFromBounds); |
| 147 | getPictureInPictureBounds(aspectRatio, mTmpToBounds); |
| 148 | if (!mTmpToBounds.equals(mTmpFromBounds)) { |
| 149 | animateResizePinnedStack(mTmpToBounds, null /* sourceHintBounds */, |
Winson Chung | e7ba686 | 2017-05-24 12:13:33 -0700 | [diff] [blame] | 150 | -1 /* duration */, false /* fromFullscreen */); |
Winson Chung | 08f8189 | 2017-03-02 15:40:51 -0800 | [diff] [blame] | 151 | } |
Winson Chung | c95ff84 | 2017-03-21 10:20:20 -0700 | [diff] [blame] | 152 | pinnedStackController.setAspectRatio( |
| 153 | pinnedStackController.isValidPictureInPictureAspectRatio(aspectRatio) |
| 154 | ? aspectRatio : -1f); |
| 155 | } |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets the current picture-in-picture actions. |
| 161 | */ |
| 162 | public void setPictureInPictureActions(List<RemoteAction> actions) { |
| 163 | synchronized (mWindowMap) { |
| 164 | if (!mService.mSupportsPictureInPicture || mContainer == null) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | mContainer.getDisplayContent().getPinnedStackController().setActions(actions); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 173 | * @return whether the multi-window mode change should be deferred as a part of a transition |
| 174 | * from fullscreen to non-fullscreen bounds. |
Winson Chung | 02e7193 | 2017-03-10 10:08:17 -0800 | [diff] [blame] | 175 | */ |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 176 | public boolean deferScheduleMultiWindowModeChanged() { |
| 177 | synchronized(mWindowMap) { |
| 178 | return mContainer.deferScheduleMultiWindowModeChanged(); |
| 179 | } |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Winson Chung | 02e7193 | 2017-03-10 10:08:17 -0800 | [diff] [blame] | 182 | /** |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 183 | * @return whether the bounds are currently animating to fullscreen. |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 184 | */ |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 185 | public boolean isAnimatingBoundsToFullscreen() { |
| 186 | synchronized (mWindowMap) { |
| 187 | return mContainer.isAnimatingBoundsToFullscreen(); |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 188 | } |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @return whether the stack can be resized from the bounds animation. |
| 193 | */ |
| 194 | public boolean pinnedStackResizeDisallowed() { |
| 195 | synchronized (mWindowMap) { |
| 196 | return mContainer.pinnedStackResizeDisallowed(); |
| 197 | } |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 198 | } |
Winson Chung | 3a68287 | 2017-04-10 14:38:05 -0700 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * The following calls are made from WM to AM. |
| 202 | */ |
| 203 | |
| 204 | /** Calls directly into activity manager so window manager lock shouldn't held. */ |
Winson Chung | ab76bbc | 2017-08-14 13:33:51 -0700 | [diff] [blame] | 205 | public void updatePictureInPictureModeForPinnedStackAnimation(Rect targetStackBounds, |
| 206 | boolean forceUpdate) { |
Winson Chung | 3a68287 | 2017-04-10 14:38:05 -0700 | [diff] [blame] | 207 | if (mListener != null) { |
| 208 | PinnedStackWindowListener listener = (PinnedStackWindowListener) mListener; |
Winson Chung | ab76bbc | 2017-08-14 13:33:51 -0700 | [diff] [blame] | 209 | listener.updatePictureInPictureModeForPinnedStackAnimation(targetStackBounds, |
| 210 | forceUpdate); |
Winson Chung | 3a68287 | 2017-04-10 14:38:05 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 213 | } |