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