Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.PINNED_STACK_ID; |
| 20 | import static android.util.TypedValue.COMPLEX_UNIT_DIP; |
| 21 | |
| 22 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 23 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 24 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 25 | import android.app.RemoteAction; |
| 26 | import android.content.pm.ParceledListSlice; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 27 | import android.content.res.Resources; |
| 28 | import android.graphics.Point; |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 29 | import android.graphics.PointF; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 30 | import android.graphics.Rect; |
| 31 | import android.os.Handler; |
| 32 | import android.os.IBinder; |
| 33 | import android.os.RemoteException; |
| 34 | import android.util.DisplayMetrics; |
| 35 | import android.util.Log; |
| 36 | import android.util.Size; |
| 37 | import android.util.Slog; |
| 38 | import android.util.TypedValue; |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 39 | import android.view.DisplayInfo; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 40 | import android.view.Gravity; |
| 41 | import android.view.IPinnedStackController; |
| 42 | import android.view.IPinnedStackListener; |
| 43 | |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 44 | import com.android.internal.policy.PipSnapAlgorithm; |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 45 | import com.android.server.UiThread; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 46 | |
| 47 | import java.io.PrintWriter; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 48 | import java.util.ArrayList; |
| 49 | import java.util.List; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 52 | * Holds the common state of the pinned stack between the system and SystemUI. If SystemUI ever |
| 53 | * needs to be restarted, it will be notified with the last known state. |
| 54 | * |
| 55 | * Changes to the pinned stack also flow through this controller, and generally, the system only |
| 56 | * changes the pinned stack bounds through this controller in two ways: |
| 57 | * |
| 58 | * 1) When first entering PiP: the controller returns the valid bounds given, taking aspect ratio |
| 59 | * and IME state into account. |
| 60 | * 2) When rotating the device: the controller calculates the new bounds in the new orientation, |
| 61 | * taking the minimized and IME state into account. In this case, we currently ignore the |
| 62 | * SystemUI adjustments (ie. expanded for menu, interaction, etc). |
| 63 | * |
| 64 | * Other changes in the system, including adjustment of IME, configuration change, and more are |
| 65 | * handled by SystemUI (similar to the docked stack divider). |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 66 | */ |
| 67 | class PinnedStackController { |
| 68 | |
| 69 | private static final String TAG = TAG_WITH_CLASS_NAME ? "PinnedStackController" : TAG_WM; |
| 70 | |
| 71 | private final WindowManagerService mService; |
| 72 | private final DisplayContent mDisplayContent; |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 73 | private final Handler mHandler = UiThread.getHandler(); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 74 | |
| 75 | private IPinnedStackListener mPinnedStackListener; |
| 76 | private final PinnedStackListenerDeathHandler mPinnedStackListenerDeathHandler = |
| 77 | new PinnedStackListenerDeathHandler(); |
| 78 | |
| 79 | private final PinnedStackControllerCallback mCallbacks = new PinnedStackControllerCallback(); |
| 80 | private final PipSnapAlgorithm mSnapAlgorithm; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 81 | |
| 82 | // States that affect how the PIP can be manipulated |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 83 | private boolean mIsMinimized; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 84 | private boolean mIsImeShowing; |
| 85 | private int mImeHeight; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 86 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 87 | // The set of actions and aspect-ratio for the that are currently allowed on the PiP activity |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 88 | private ArrayList<RemoteAction> mActions = new ArrayList<>(); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 89 | private float mAspectRatio = -1f; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 90 | |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 91 | // Used to calculate stack bounds across rotations |
| 92 | private final DisplayInfo mDisplayInfo = new DisplayInfo(); |
Winson Chung | 114aeea | 2017-01-09 16:08:07 -0800 | [diff] [blame] | 93 | private final Rect mStableInsets = new Rect(); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 94 | |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 95 | // The size and position information that describes where the pinned stack will go by default. |
| 96 | private int mDefaultStackGravity; |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 97 | private float mDefaultAspectRatio; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 98 | private Point mScreenEdgeInsets; |
| 99 | |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 100 | // The aspect ratio bounds of the PIP. |
| 101 | private float mMinAspectRatio; |
| 102 | private float mMaxAspectRatio; |
| 103 | |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 104 | // The minimum edge size of the normal PiP bounds. |
| 105 | private int mMinSize; |
| 106 | |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 107 | // Temp vars for calculation |
| 108 | private final DisplayMetrics mTmpMetrics = new DisplayMetrics(); |
| 109 | private final Rect mTmpInsets = new Rect(); |
Jorim Jaggi | ad5d284 | 2016-11-01 18:22:53 -0700 | [diff] [blame] | 110 | private final Rect mTmpRect = new Rect(); |
Winson Chung | 34a07f4 | 2017-03-10 18:06:25 -0800 | [diff] [blame] | 111 | private final Rect mTmpAnimatingBoundsRect = new Rect(); |
Winson Chung | f1f72f6 | 2017-02-14 17:15:48 -0800 | [diff] [blame] | 112 | private final Point mTmpDisplaySize = new Point(); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * The callback object passed to listeners for them to notify the controller of state changes. |
| 116 | */ |
| 117 | private class PinnedStackControllerCallback extends IPinnedStackController.Stub { |
| 118 | |
| 119 | @Override |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 120 | public void setIsMinimized(final boolean isMinimized) { |
| 121 | mHandler.post(() -> { |
| 122 | mIsMinimized = isMinimized; |
Mady Mellor | 3b10dcd | 2017-01-23 10:08:35 -0800 | [diff] [blame] | 123 | mSnapAlgorithm.setMinimized(isMinimized); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 124 | }); |
| 125 | } |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Handler for the case where the listener dies. |
| 130 | */ |
| 131 | private class PinnedStackListenerDeathHandler implements IBinder.DeathRecipient { |
| 132 | |
| 133 | @Override |
| 134 | public void binderDied() { |
| 135 | // Clean up the state if the listener dies |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 136 | mPinnedStackListener = null; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | PinnedStackController(WindowManagerService service, DisplayContent displayContent) { |
| 141 | mService = service; |
| 142 | mDisplayContent = displayContent; |
| 143 | mSnapAlgorithm = new PipSnapAlgorithm(service.mContext); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 144 | mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo()); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 145 | reloadResources(); |
| 146 | } |
| 147 | |
| 148 | void onConfigurationChanged() { |
| 149 | reloadResources(); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 150 | notifyMovementBoundsChanged(false /* fromImeAdjustment */); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Reloads all the resources for the current configuration. |
| 155 | */ |
| 156 | void reloadResources() { |
| 157 | final Resources res = mService.mContext.getResources(); |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 158 | mMinSize = res.getDimensionPixelSize( |
| 159 | com.android.internal.R.dimen.default_minimal_size_pip_resizable_task); |
| 160 | mDefaultAspectRatio = res.getFloat( |
| 161 | com.android.internal.R.dimen.config_pictureInPictureDefaultAspectRatio); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 162 | final Size screenEdgeInsetsDp = Size.parseSize(res.getString( |
| 163 | com.android.internal.R.string.config_defaultPictureInPictureScreenEdgeInsets)); |
| 164 | mDefaultStackGravity = res.getInteger( |
| 165 | com.android.internal.R.integer.config_defaultPictureInPictureGravity); |
| 166 | mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 167 | mScreenEdgeInsets = new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics), |
| 168 | dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics)); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 169 | mMinAspectRatio = res.getFloat( |
| 170 | com.android.internal.R.dimen.config_pictureInPictureMinAspectRatio); |
| 171 | mMaxAspectRatio = res.getFloat( |
| 172 | com.android.internal.R.dimen.config_pictureInPictureMaxAspectRatio); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Registers a pinned stack listener. |
| 177 | */ |
| 178 | void registerPinnedStackListener(IPinnedStackListener listener) { |
| 179 | try { |
| 180 | listener.asBinder().linkToDeath(mPinnedStackListenerDeathHandler, 0); |
| 181 | listener.onListenerRegistered(mCallbacks); |
| 182 | mPinnedStackListener = listener; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 183 | notifyImeVisibilityChanged(mIsImeShowing, mImeHeight); |
| 184 | // The movement bounds notification needs to be sent before the minimized state, since |
| 185 | // SystemUI may use the bounds to retore the minimized position |
| 186 | notifyMovementBoundsChanged(false /* fromImeAdjustment */); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 187 | notifyActionsChanged(mActions); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 188 | notifyMinimizeChanged(mIsMinimized); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 189 | } catch (RemoteException e) { |
| 190 | Log.e(TAG, "Failed to register pinned stack listener", e); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 195 | * @return whether the given {@param aspectRatio} is valid. |
| 196 | */ |
| 197 | public boolean isValidPictureInPictureAspectRatio(float aspectRatio) { |
| 198 | return mMinAspectRatio <= aspectRatio && aspectRatio <= mMaxAspectRatio; |
| 199 | } |
| 200 | |
| 201 | /** |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 202 | * Returns the current bounds (or the default bounds if there are no current bounds) with the |
| 203 | * specified aspect ratio. |
| 204 | */ |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 205 | Rect transformBoundsToAspectRatio(Rect stackBounds, float aspectRatio) { |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 206 | // Save the snap fraction, calculate the aspect ratio based on screen size |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 207 | final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds, |
| 208 | getMovementBounds(stackBounds)); |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 209 | final Size size = getSize(aspectRatio); |
| 210 | final int left = (int) (stackBounds.centerX() - size.getWidth() / 2f); |
| 211 | final int top = (int) (stackBounds.centerY() - size.getHeight() / 2f); |
| 212 | stackBounds.set(left, top, left + size.getWidth(), top + size.getHeight()); |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 213 | mSnapAlgorithm.applySnapFraction(stackBounds, getMovementBounds(stackBounds), snapFraction); |
Winson Chung | f1f72f6 | 2017-02-14 17:15:48 -0800 | [diff] [blame] | 214 | if (mIsMinimized) { |
| 215 | applyMinimizedOffset(stackBounds, getMovementBounds(stackBounds)); |
| 216 | } |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 217 | return stackBounds; |
| 218 | } |
| 219 | |
| 220 | /** |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 221 | * @return the size of the PIP based on the given {@param aspectRatio}. |
| 222 | */ |
| 223 | Size getSize(float aspectRatio) { |
| 224 | return mSnapAlgorithm.getSizeForAspectRatio(aspectRatio, mMinSize, |
| 225 | mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight); |
| 226 | } |
| 227 | |
| 228 | /** |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 229 | * @return the default bounds to show the PIP when there is no active PIP. |
| 230 | */ |
| 231 | Rect getDefaultBounds() { |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 232 | final Rect insetBounds = new Rect(); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 233 | getInsetBounds(insetBounds); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 234 | |
| 235 | final Rect defaultBounds = new Rect(); |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 236 | final Size size = getSize(mDefaultAspectRatio); |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 237 | Gravity.apply(mDefaultStackGravity, size.getWidth(), size.getHeight(), insetBounds, |
| 238 | 0, mIsImeShowing ? mImeHeight : 0, defaultBounds); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 239 | return defaultBounds; |
| 240 | } |
| 241 | |
| 242 | /** |
Winson Chung | 47f2bf6 | 2017-02-16 18:58:12 -0800 | [diff] [blame] | 243 | * Updates the display info, calculating and returning the new stack and movement bounds in the |
| 244 | * new orientation of the device if necessary. |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 245 | */ |
Winson Chung | 47f2bf6 | 2017-02-16 18:58:12 -0800 | [diff] [blame] | 246 | void onTaskStackBoundsChanged(Rect targetBounds, Rect outBounds) { |
| 247 | final DisplayInfo displayInfo = mDisplayContent.getDisplayInfo(); |
| 248 | if (mDisplayInfo.equals(displayInfo)) { |
| 249 | return; |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 250 | } |
Winson Chung | 47f2bf6 | 2017-02-16 18:58:12 -0800 | [diff] [blame] | 251 | |
| 252 | mTmpRect.set(targetBounds); |
| 253 | final Rect postChangeStackBounds = mTmpRect; |
| 254 | |
| 255 | // Calculate the snap fraction of the current stack along the old movement bounds |
| 256 | final Rect preChangeMovementBounds = getMovementBounds(postChangeStackBounds); |
| 257 | final float snapFraction = mSnapAlgorithm.getSnapFraction(postChangeStackBounds, |
| 258 | preChangeMovementBounds); |
| 259 | mDisplayInfo.copyFrom(displayInfo); |
| 260 | |
| 261 | // Calculate the stack bounds in the new orientation to the same same fraction along the |
| 262 | // rotated movement bounds. |
| 263 | final Rect postChangeMovementBounds = getMovementBounds(postChangeStackBounds, |
| 264 | false /* adjustForIme */); |
| 265 | mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds, |
| 266 | snapFraction); |
| 267 | if (mIsMinimized) { |
| 268 | applyMinimizedOffset(postChangeStackBounds, postChangeMovementBounds); |
| 269 | } |
| 270 | |
| 271 | notifyMovementBoundsChanged(false /* fromImeAdjustment */); |
| 272 | |
| 273 | outBounds.set(postChangeStackBounds); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Sets the Ime state and height. |
| 278 | */ |
| 279 | void setAdjustedForIme(boolean adjustedForIme, int imeHeight) { |
| 280 | // Return early if there is no state change |
| 281 | if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) { |
| 282 | return; |
| 283 | } |
| 284 | |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 285 | mIsImeShowing = adjustedForIme; |
| 286 | mImeHeight = imeHeight; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 287 | notifyImeVisibilityChanged(adjustedForIme, imeHeight); |
| 288 | notifyMovementBoundsChanged(true /* fromImeAdjustment */); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Sets the current aspect ratio. |
| 293 | */ |
| 294 | void setAspectRatio(float aspectRatio) { |
| 295 | if (Float.compare(mAspectRatio, aspectRatio) != 0) { |
| 296 | mAspectRatio = aspectRatio; |
| 297 | notifyMovementBoundsChanged(false /* fromImeAdjustment */); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 302 | * Sets the current set of actions. |
| 303 | */ |
| 304 | void setActions(List<RemoteAction> actions) { |
| 305 | mActions.clear(); |
Winson Chung | c2baac0 | 2017-01-11 13:34:47 -0800 | [diff] [blame] | 306 | if (actions != null) { |
| 307 | mActions.addAll(actions); |
| 308 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 309 | notifyActionsChanged(mActions); |
| 310 | } |
| 311 | |
| 312 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 313 | * Notifies listeners that the PIP needs to be adjusted for the IME. |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 314 | */ |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 315 | private void notifyImeVisibilityChanged(boolean imeVisible, int imeHeight) { |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 316 | if (mPinnedStackListener != null) { |
| 317 | try { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 318 | mPinnedStackListener.onImeVisibilityChanged(imeVisible, imeHeight); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 319 | } catch (RemoteException e) { |
| 320 | Slog.e(TAG_WM, "Error delivering bounds changed event.", e); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 326 | * Notifies listeners that the PIP minimized state has changed. |
| 327 | */ |
| 328 | private void notifyMinimizeChanged(boolean isMinimized) { |
| 329 | if (mPinnedStackListener != null) { |
| 330 | try { |
| 331 | mPinnedStackListener.onMinimizedStateChanged(isMinimized); |
| 332 | } catch (RemoteException e) { |
| 333 | Slog.e(TAG_WM, "Error delivering minimize changed event.", e); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 339 | * Notifies listeners that the PIP actions have changed. |
| 340 | */ |
| 341 | private void notifyActionsChanged(List<RemoteAction> actions) { |
| 342 | if (mPinnedStackListener != null) { |
| 343 | try { |
| 344 | mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions)); |
| 345 | } catch (RemoteException e) { |
| 346 | Slog.e(TAG_WM, "Error delivering actions changed event.", e); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 352 | * Notifies listeners that the PIP movement bounds have changed. |
| 353 | */ |
| 354 | private void notifyMovementBoundsChanged(boolean fromImeAdjustement) { |
| 355 | if (mPinnedStackListener != null) { |
| 356 | try { |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 357 | final Rect insetBounds = new Rect(); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 358 | getInsetBounds(insetBounds); |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 359 | final Rect normalBounds = getDefaultBounds(); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 360 | if (isValidPictureInPictureAspectRatio(mAspectRatio)) { |
| 361 | transformBoundsToAspectRatio(normalBounds, mAspectRatio); |
| 362 | } |
Winson Chung | 34a07f4 | 2017-03-10 18:06:25 -0800 | [diff] [blame] | 363 | final Rect animatingBounds = mTmpAnimatingBoundsRect; |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 364 | final TaskStack pinnedStack = mDisplayContent.getStackById(PINNED_STACK_ID); |
| 365 | if (pinnedStack != null) { |
| 366 | pinnedStack.getAnimatingBounds(animatingBounds); |
| 367 | } else { |
| 368 | animatingBounds.set(normalBounds); |
| 369 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 370 | mPinnedStackListener.onMovementBoundsChanged(insetBounds, normalBounds, |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 371 | animatingBounds, fromImeAdjustement); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 372 | } catch (RemoteException e) { |
| 373 | Slog.e(TAG_WM, "Error delivering actions changed event.", e); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 379 | * @return the bounds on the screen that the PIP can be visible in. |
| 380 | */ |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 381 | private void getInsetBounds(Rect outRect) { |
| 382 | mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth, |
| 383 | mDisplayInfo.logicalHeight, mTmpInsets); |
| 384 | outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y, |
| 385 | mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x, |
| 386 | mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
Winson Chung | 5589333 | 2017-02-17 17:13:10 -0800 | [diff] [blame] | 390 | * @return the movement bounds for the given {@param stackBounds} and the current state of the |
| 391 | * controller. |
| 392 | */ |
| 393 | private Rect getMovementBounds(Rect stackBounds) { |
| 394 | return getMovementBounds(stackBounds, true /* adjustForIme */); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * @return the movement bounds for the given {@param stackBounds} and the current state of the |
| 399 | * controller. |
| 400 | */ |
| 401 | private Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) { |
| 402 | final Rect movementBounds = new Rect(); |
| 403 | getInsetBounds(movementBounds); |
| 404 | |
| 405 | // Apply the movement bounds adjustments based on the current state |
| 406 | mSnapAlgorithm.getMovementBounds(stackBounds, movementBounds, movementBounds, |
| 407 | (adjustForIme && mIsImeShowing) ? mImeHeight : 0); |
| 408 | return movementBounds; |
| 409 | } |
| 410 | |
| 411 | /** |
Winson Chung | f1f72f6 | 2017-02-14 17:15:48 -0800 | [diff] [blame] | 412 | * Applies the minimized offsets to the given stack bounds. |
| 413 | */ |
| 414 | private void applyMinimizedOffset(Rect stackBounds, Rect movementBounds) { |
| 415 | mTmpDisplaySize.set(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight); |
| 416 | mService.getStableInsetsLocked(mDisplayContent.getDisplayId(), mStableInsets); |
| 417 | mSnapAlgorithm.applyMinimizedOffset(stackBounds, movementBounds, mTmpDisplaySize, |
| 418 | mStableInsets); |
| 419 | } |
| 420 | |
| 421 | /** |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 422 | * @return the pixels for a given dp value. |
| 423 | */ |
| 424 | private int dpToPx(float dpValue, DisplayMetrics dm) { |
| 425 | return (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, dpValue, dm); |
| 426 | } |
| 427 | |
| 428 | void dump(String prefix, PrintWriter pw) { |
| 429 | pw.println(prefix + "PinnedStackController"); |
Jorim Jaggi | ad5d284 | 2016-11-01 18:22:53 -0700 | [diff] [blame] | 430 | pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw); |
| 431 | pw.println(); |
| 432 | mService.getStackBounds(PINNED_STACK_ID, mTmpRect); |
| 433 | pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw); |
| 434 | pw.println(); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 435 | pw.println(prefix + " mIsImeShowing=" + mIsImeShowing); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 436 | pw.println(prefix + " mIsMinimized=" + mIsMinimized); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 437 | if (mActions.isEmpty()) { |
| 438 | pw.println(prefix + " mActions=[]"); |
| 439 | } else { |
| 440 | pw.println(prefix + " mActions=["); |
| 441 | for (int i = 0; i < mActions.size(); i++) { |
| 442 | RemoteAction action = mActions.get(i); |
| 443 | pw.print(prefix + " Action[" + i + "]: "); |
| 444 | action.dump("", pw); |
| 445 | } |
| 446 | pw.println(prefix + " ]"); |
| 447 | } |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 448 | } |
| 449 | } |