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; |
| 97 | private Size mDefaultStackSize; |
| 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 | |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 104 | // Temp vars for calculation |
| 105 | private final DisplayMetrics mTmpMetrics = new DisplayMetrics(); |
| 106 | private final Rect mTmpInsets = new Rect(); |
Jorim Jaggi | ad5d284 | 2016-11-01 18:22:53 -0700 | [diff] [blame] | 107 | private final Rect mTmpRect = new Rect(); |
Winson Chung | f1f72f6 | 2017-02-14 17:15:48 -0800 | [diff] [blame^] | 108 | private final Point mTmpDisplaySize = new Point(); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * The callback object passed to listeners for them to notify the controller of state changes. |
| 112 | */ |
| 113 | private class PinnedStackControllerCallback extends IPinnedStackController.Stub { |
| 114 | |
| 115 | @Override |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 116 | public void setIsMinimized(final boolean isMinimized) { |
| 117 | mHandler.post(() -> { |
| 118 | mIsMinimized = isMinimized; |
Mady Mellor | 3b10dcd | 2017-01-23 10:08:35 -0800 | [diff] [blame] | 119 | mSnapAlgorithm.setMinimized(isMinimized); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 120 | }); |
| 121 | } |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Handler for the case where the listener dies. |
| 126 | */ |
| 127 | private class PinnedStackListenerDeathHandler implements IBinder.DeathRecipient { |
| 128 | |
| 129 | @Override |
| 130 | public void binderDied() { |
| 131 | // Clean up the state if the listener dies |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 132 | mPinnedStackListener = null; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | PinnedStackController(WindowManagerService service, DisplayContent displayContent) { |
| 137 | mService = service; |
| 138 | mDisplayContent = displayContent; |
| 139 | mSnapAlgorithm = new PipSnapAlgorithm(service.mContext); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 140 | mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo()); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 141 | reloadResources(); |
| 142 | } |
| 143 | |
| 144 | void onConfigurationChanged() { |
| 145 | reloadResources(); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 146 | notifyMovementBoundsChanged(false /* fromImeAdjustment */); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Reloads all the resources for the current configuration. |
| 151 | */ |
| 152 | void reloadResources() { |
| 153 | final Resources res = mService.mContext.getResources(); |
| 154 | final Size defaultSizeDp = Size.parseSize(res.getString( |
| 155 | com.android.internal.R.string.config_defaultPictureInPictureSize)); |
| 156 | final Size screenEdgeInsetsDp = Size.parseSize(res.getString( |
| 157 | com.android.internal.R.string.config_defaultPictureInPictureScreenEdgeInsets)); |
| 158 | mDefaultStackGravity = res.getInteger( |
| 159 | com.android.internal.R.integer.config_defaultPictureInPictureGravity); |
| 160 | mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics); |
| 161 | mDefaultStackSize = new Size(dpToPx(defaultSizeDp.getWidth(), mTmpMetrics), |
| 162 | dpToPx(defaultSizeDp.getHeight(), mTmpMetrics)); |
| 163 | mScreenEdgeInsets = new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics), |
| 164 | dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics)); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 165 | mMinAspectRatio = res.getFloat( |
| 166 | com.android.internal.R.dimen.config_pictureInPictureMinAspectRatio); |
| 167 | mMaxAspectRatio = res.getFloat( |
| 168 | com.android.internal.R.dimen.config_pictureInPictureMaxAspectRatio); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Registers a pinned stack listener. |
| 173 | */ |
| 174 | void registerPinnedStackListener(IPinnedStackListener listener) { |
| 175 | try { |
| 176 | listener.asBinder().linkToDeath(mPinnedStackListenerDeathHandler, 0); |
| 177 | listener.onListenerRegistered(mCallbacks); |
| 178 | mPinnedStackListener = listener; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 179 | notifyImeVisibilityChanged(mIsImeShowing, mImeHeight); |
| 180 | // The movement bounds notification needs to be sent before the minimized state, since |
| 181 | // SystemUI may use the bounds to retore the minimized position |
| 182 | notifyMovementBoundsChanged(false /* fromImeAdjustment */); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 183 | notifyActionsChanged(mActions); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 184 | notifyMinimizeChanged(mIsMinimized); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 185 | } catch (RemoteException e) { |
| 186 | Log.e(TAG, "Failed to register pinned stack listener", e); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 191 | * @return whether the given {@param aspectRatio} is valid. |
| 192 | */ |
| 193 | public boolean isValidPictureInPictureAspectRatio(float aspectRatio) { |
| 194 | return mMinAspectRatio <= aspectRatio && aspectRatio <= mMaxAspectRatio; |
| 195 | } |
| 196 | |
| 197 | /** |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 198 | * Returns the current bounds (or the default bounds if there are no current bounds) with the |
| 199 | * specified aspect ratio. |
| 200 | */ |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 201 | Rect transformBoundsToAspectRatio(Rect stackBounds, float aspectRatio) { |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 202 | // Save the snap fraction, calculate the aspect ratio based on the current bounds |
| 203 | final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds, |
| 204 | getMovementBounds(stackBounds)); |
| 205 | final float radius = PointF.length(stackBounds.width(), stackBounds.height()); |
| 206 | final int height = (int) Math.round(Math.sqrt((radius * radius) / |
| 207 | (aspectRatio * aspectRatio + 1))); |
| 208 | final int width = Math.round(height * aspectRatio); |
| 209 | final int left = (int) (stackBounds.centerX() - width / 2f); |
| 210 | final int top = (int) (stackBounds.centerY() - height / 2f); |
| 211 | stackBounds.set(left, top, left + width, top + height); |
| 212 | mSnapAlgorithm.applySnapFraction(stackBounds, getMovementBounds(stackBounds), snapFraction); |
Winson Chung | f1f72f6 | 2017-02-14 17:15:48 -0800 | [diff] [blame^] | 213 | if (mIsMinimized) { |
| 214 | applyMinimizedOffset(stackBounds, getMovementBounds(stackBounds)); |
| 215 | } |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 216 | return stackBounds; |
| 217 | } |
| 218 | |
| 219 | /** |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 220 | * @return the default bounds to show the PIP when there is no active PIP. |
| 221 | */ |
| 222 | Rect getDefaultBounds() { |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 223 | final Rect insetBounds = new Rect(); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 224 | getInsetBounds(insetBounds); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 225 | |
| 226 | final Rect defaultBounds = new Rect(); |
| 227 | Gravity.apply(mDefaultStackGravity, mDefaultStackSize.getWidth(), |
| 228 | mDefaultStackSize.getHeight(), insetBounds, 0, 0, defaultBounds); |
| 229 | return defaultBounds; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * @return the movement bounds for the given {@param stackBounds} and the current state of the |
| 234 | * controller. |
| 235 | */ |
| 236 | Rect getMovementBounds(Rect stackBounds) { |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 237 | return getMovementBounds(stackBounds, true /* adjustForIme */); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @return the movement bounds for the given {@param stackBounds} and the current state of the |
| 242 | * controller. |
| 243 | */ |
| 244 | Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) { |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 245 | final Rect movementBounds = new Rect(); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 246 | getInsetBounds(movementBounds); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 247 | |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 248 | // Apply the movement bounds adjustments based on the current state |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 249 | mSnapAlgorithm.getMovementBounds(stackBounds, movementBounds, movementBounds, |
| 250 | (adjustForIme && mIsImeShowing) ? mImeHeight : 0); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 251 | return movementBounds; |
| 252 | } |
| 253 | |
| 254 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 255 | * @param preChangeTargetBounds The final bounds of the stack if it is currently animating |
Winson Chung | 114aeea | 2017-01-09 16:08:07 -0800 | [diff] [blame] | 256 | * @return the repositioned PIP bounds given it's pre-change bounds, and the new display |
| 257 | * content. |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 258 | */ |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 259 | Rect onDisplayChanged(Rect preChangeStackBounds, Rect preChangeTargetBounds, |
| 260 | DisplayContent displayContent) { |
| 261 | final Rect postChangeStackBounds = new Rect(preChangeTargetBounds); |
Winson Chung | 114aeea | 2017-01-09 16:08:07 -0800 | [diff] [blame] | 262 | final DisplayInfo displayInfo = displayContent.getDisplayInfo(); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 263 | if (!mDisplayInfo.equals(displayInfo)) { |
| 264 | // Calculate the snap fraction of the current stack along the old movement bounds, and |
| 265 | // then update the stack bounds to the same fraction along the rotated movement bounds. |
| 266 | final Rect preChangeMovementBounds = getMovementBounds(preChangeStackBounds); |
| 267 | final float snapFraction = mSnapAlgorithm.getSnapFraction(preChangeStackBounds, |
| 268 | preChangeMovementBounds); |
| 269 | mDisplayInfo.copyFrom(displayInfo); |
| 270 | |
| 271 | final Rect postChangeMovementBounds = getMovementBounds(preChangeStackBounds, |
| 272 | false /* adjustForIme */); |
| 273 | mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds, |
| 274 | snapFraction); |
Winson Chung | d5a0159 | 2016-11-11 16:25:04 -0800 | [diff] [blame] | 275 | if (mIsMinimized) { |
Winson Chung | f1f72f6 | 2017-02-14 17:15:48 -0800 | [diff] [blame^] | 276 | applyMinimizedOffset(postChangeStackBounds, postChangeMovementBounds); |
Winson Chung | d5a0159 | 2016-11-11 16:25:04 -0800 | [diff] [blame] | 277 | } |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 278 | notifyMovementBoundsChanged(false /* fromImeAdjustment */); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 279 | } |
| 280 | return postChangeStackBounds; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Sets the Ime state and height. |
| 285 | */ |
| 286 | void setAdjustedForIme(boolean adjustedForIme, int imeHeight) { |
| 287 | // Return early if there is no state change |
| 288 | if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) { |
| 289 | return; |
| 290 | } |
| 291 | |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 292 | mIsImeShowing = adjustedForIme; |
| 293 | mImeHeight = imeHeight; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 294 | notifyImeVisibilityChanged(adjustedForIme, imeHeight); |
| 295 | notifyMovementBoundsChanged(true /* fromImeAdjustment */); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Sets the current aspect ratio. |
| 300 | */ |
| 301 | void setAspectRatio(float aspectRatio) { |
| 302 | if (Float.compare(mAspectRatio, aspectRatio) != 0) { |
| 303 | mAspectRatio = aspectRatio; |
| 304 | notifyMovementBoundsChanged(false /* fromImeAdjustment */); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 309 | * Sets the current set of actions. |
| 310 | */ |
| 311 | void setActions(List<RemoteAction> actions) { |
| 312 | mActions.clear(); |
Winson Chung | c2baac0 | 2017-01-11 13:34:47 -0800 | [diff] [blame] | 313 | if (actions != null) { |
| 314 | mActions.addAll(actions); |
| 315 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 316 | notifyActionsChanged(mActions); |
| 317 | } |
| 318 | |
| 319 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 320 | * Notifies listeners that the PIP needs to be adjusted for the IME. |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 321 | */ |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 322 | private void notifyImeVisibilityChanged(boolean imeVisible, int imeHeight) { |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 323 | if (mPinnedStackListener != null) { |
| 324 | try { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 325 | mPinnedStackListener.onImeVisibilityChanged(imeVisible, imeHeight); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 326 | } catch (RemoteException e) { |
| 327 | Slog.e(TAG_WM, "Error delivering bounds changed event.", e); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 333 | * Notifies listeners that the PIP minimized state has changed. |
| 334 | */ |
| 335 | private void notifyMinimizeChanged(boolean isMinimized) { |
| 336 | if (mPinnedStackListener != null) { |
| 337 | try { |
| 338 | mPinnedStackListener.onMinimizedStateChanged(isMinimized); |
| 339 | } catch (RemoteException e) { |
| 340 | Slog.e(TAG_WM, "Error delivering minimize changed event.", e); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 346 | * Notifies listeners that the PIP actions have changed. |
| 347 | */ |
| 348 | private void notifyActionsChanged(List<RemoteAction> actions) { |
| 349 | if (mPinnedStackListener != null) { |
| 350 | try { |
| 351 | mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions)); |
| 352 | } catch (RemoteException e) { |
| 353 | Slog.e(TAG_WM, "Error delivering actions changed event.", e); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 359 | * Notifies listeners that the PIP movement bounds have changed. |
| 360 | */ |
| 361 | private void notifyMovementBoundsChanged(boolean fromImeAdjustement) { |
| 362 | if (mPinnedStackListener != null) { |
| 363 | try { |
| 364 | Rect insetBounds = new Rect(); |
| 365 | getInsetBounds(insetBounds); |
| 366 | Rect normalBounds = getDefaultBounds(); |
| 367 | if (isValidPictureInPictureAspectRatio(mAspectRatio)) { |
| 368 | transformBoundsToAspectRatio(normalBounds, mAspectRatio); |
| 369 | } |
| 370 | mPinnedStackListener.onMovementBoundsChanged(insetBounds, normalBounds, |
| 371 | fromImeAdjustement); |
| 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 | f1f72f6 | 2017-02-14 17:15:48 -0800 | [diff] [blame^] | 390 | * Applies the minimized offsets to the given stack bounds. |
| 391 | */ |
| 392 | private void applyMinimizedOffset(Rect stackBounds, Rect movementBounds) { |
| 393 | mTmpDisplaySize.set(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight); |
| 394 | mService.getStableInsetsLocked(mDisplayContent.getDisplayId(), mStableInsets); |
| 395 | mSnapAlgorithm.applyMinimizedOffset(stackBounds, movementBounds, mTmpDisplaySize, |
| 396 | mStableInsets); |
| 397 | } |
| 398 | |
| 399 | /** |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 400 | * @return the pixels for a given dp value. |
| 401 | */ |
| 402 | private int dpToPx(float dpValue, DisplayMetrics dm) { |
| 403 | return (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, dpValue, dm); |
| 404 | } |
| 405 | |
| 406 | void dump(String prefix, PrintWriter pw) { |
| 407 | pw.println(prefix + "PinnedStackController"); |
Jorim Jaggi | ad5d284 | 2016-11-01 18:22:53 -0700 | [diff] [blame] | 408 | pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw); |
| 409 | pw.println(); |
| 410 | mService.getStackBounds(PINNED_STACK_ID, mTmpRect); |
| 411 | pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw); |
| 412 | pw.println(); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 413 | pw.println(prefix + " mIsImeShowing=" + mIsImeShowing); |
Winson Chung | fa705378 | 2016-11-08 15:45:10 -0800 | [diff] [blame] | 414 | pw.println(prefix + " mIsMinimized=" + mIsMinimized); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 415 | if (mActions.isEmpty()) { |
| 416 | pw.println(prefix + " mActions=[]"); |
| 417 | } else { |
| 418 | pw.println(prefix + " mActions=["); |
| 419 | for (int i = 0; i < mActions.size(); i++) { |
| 420 | RemoteAction action = mActions.get(i); |
| 421 | pw.print(prefix + " Action[" + i + "]: "); |
| 422 | action.dump("", pw); |
| 423 | } |
| 424 | pw.println(prefix + " ]"); |
| 425 | } |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 426 | } |
| 427 | } |