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; |
| 20 | |
| 21 | import android.app.RemoteAction; |
| 22 | import android.graphics.Rect; |
| 23 | |
| 24 | import com.android.server.UiThread; |
| 25 | |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * Controller for the pinned stack container. See {@link StackWindowController}. |
| 30 | */ |
| 31 | public class PinnedStackWindowController extends StackWindowController { |
| 32 | |
| 33 | private Rect mTmpBoundsRect = new Rect(); |
| 34 | |
| 35 | public PinnedStackWindowController(int stackId, StackWindowListener listener, int displayId, |
| 36 | boolean onTop, Rect outBounds) { |
| 37 | super(stackId, listener, displayId, onTop, outBounds, WindowManagerService.getInstance()); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Animates the pinned stack. |
| 42 | */ |
| 43 | public void animateResizePinnedStack(Rect bounds, int animationDuration) { |
| 44 | synchronized (mWindowMap) { |
| 45 | if (mContainer == null) { |
| 46 | throw new IllegalArgumentException("Pinned stack container not found :("); |
| 47 | } |
| 48 | |
| 49 | // Get non-null fullscreen bounds if the bounds are null |
| 50 | final boolean moveToFullscreen = bounds == null; |
| 51 | bounds = getPinnedStackAnimationBounds(bounds); |
| 52 | |
| 53 | // If the bounds are truly null, then there was no fullscreen stack at this time, so |
| 54 | // animate this to the full display bounds |
| 55 | final Rect toBounds; |
| 56 | if (bounds == null) { |
| 57 | toBounds = new Rect(); |
| 58 | mContainer.getDisplayContent().getLogicalDisplayRect(toBounds); |
| 59 | } else { |
| 60 | toBounds = bounds; |
| 61 | } |
| 62 | |
| 63 | final Rect originalBounds = new Rect(); |
| 64 | mContainer.getBounds(originalBounds); |
| 65 | mContainer.setAnimatingBounds(toBounds); |
| 66 | UiThread.getHandler().post(() -> { |
| 67 | mService.mBoundsAnimationController.animateBounds(mContainer, originalBounds, |
| 68 | toBounds, animationDuration, moveToFullscreen); |
| 69 | }); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Sets the current picture-in-picture aspect ratio. |
| 75 | */ |
| 76 | public void setPictureInPictureAspectRatio(float aspectRatio) { |
| 77 | synchronized (mWindowMap) { |
| 78 | if (!mService.mSupportsPictureInPicture || mContainer == null) { |
| 79 | return; |
| 80 | } |
| 81 | |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 82 | final PinnedStackController pinnedStackController = |
| 83 | mContainer.getDisplayContent().getPinnedStackController(); |
Winson Chung | c95ff84 | 2017-03-21 10:20:20 -0700 | [diff] [blame^] | 84 | |
| 85 | if (Float.compare(aspectRatio, pinnedStackController.getAspectRatio()) != 0) { |
| 86 | final int displayId = mContainer.getDisplayContent().getDisplayId(); |
| 87 | final Rect toBounds = mService.getPictureInPictureBounds(displayId, aspectRatio); |
| 88 | animateResizePinnedStack(toBounds, -1 /* duration */); |
| 89 | pinnedStackController.setAspectRatio( |
| 90 | pinnedStackController.isValidPictureInPictureAspectRatio(aspectRatio) |
| 91 | ? aspectRatio : -1f); |
| 92 | } |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Sets the current picture-in-picture actions. |
| 98 | */ |
| 99 | public void setPictureInPictureActions(List<RemoteAction> actions) { |
| 100 | synchronized (mWindowMap) { |
| 101 | if (!mService.mSupportsPictureInPicture || mContainer == null) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | mContainer.getDisplayContent().getPinnedStackController().setActions(actions); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
Winson Chung | 02e7193 | 2017-03-10 10:08:17 -0800 | [diff] [blame] | 110 | * @return whether the bounds are currently animating to fullscreen. |
| 111 | */ |
| 112 | public boolean isBoundsAnimatingToFullscreen() { |
| 113 | return mContainer.isBoundsAnimatingToFullscreen(); |
| 114 | } |
| 115 | |
| 116 | /** |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 117 | * Checks the {@param bounds} and retirms non-null fullscreen bounds for the pinned stack |
| 118 | * animation if necessary. |
| 119 | */ |
| 120 | private Rect getPinnedStackAnimationBounds(Rect bounds) { |
| 121 | mService.getStackBounds(FULLSCREEN_WORKSPACE_STACK_ID, mTmpBoundsRect); |
| 122 | if (bounds == null && !mTmpBoundsRect.isEmpty()) { |
| 123 | bounds = new Rect(mTmpBoundsRect); |
| 124 | } |
| 125 | return bounds; |
| 126 | } |
| 127 | } |