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; |
Jorim Jaggi | ad5d284 | 2016-11-01 18:22:53 -0700 | [diff] [blame] | 21 | import static android.view.Display.DEFAULT_DISPLAY; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 22 | |
| 23 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 24 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 25 | |
| 26 | import android.animation.ValueAnimator; |
| 27 | import android.content.res.Resources; |
| 28 | import android.graphics.Point; |
| 29 | import android.graphics.Rect; |
| 30 | import android.os.Handler; |
| 31 | import android.os.IBinder; |
| 32 | import android.os.RemoteException; |
| 33 | import android.util.DisplayMetrics; |
| 34 | import android.util.Log; |
| 35 | import android.util.Size; |
| 36 | import android.util.Slog; |
| 37 | import android.util.TypedValue; |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 38 | import android.view.DisplayInfo; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 39 | import android.view.Gravity; |
| 40 | import android.view.IPinnedStackController; |
| 41 | import android.view.IPinnedStackListener; |
| 42 | |
| 43 | import com.android.internal.os.BackgroundThread; |
| 44 | import com.android.internal.policy.PipMotionHelper; |
| 45 | import com.android.internal.policy.PipSnapAlgorithm; |
| 46 | |
| 47 | import java.io.PrintWriter; |
| 48 | |
| 49 | /** |
| 50 | * Holds the common state of the pinned stack between the system and SystemUI. |
| 51 | */ |
| 52 | class PinnedStackController { |
| 53 | |
| 54 | private static final String TAG = TAG_WITH_CLASS_NAME ? "PinnedStackController" : TAG_WM; |
| 55 | |
| 56 | private final WindowManagerService mService; |
| 57 | private final DisplayContent mDisplayContent; |
| 58 | private final Handler mHandler = new Handler(); |
| 59 | |
| 60 | private IPinnedStackListener mPinnedStackListener; |
| 61 | private final PinnedStackListenerDeathHandler mPinnedStackListenerDeathHandler = |
| 62 | new PinnedStackListenerDeathHandler(); |
| 63 | |
| 64 | private final PinnedStackControllerCallback mCallbacks = new PinnedStackControllerCallback(); |
| 65 | private final PipSnapAlgorithm mSnapAlgorithm; |
| 66 | private final PipMotionHelper mMotionHelper; |
| 67 | |
| 68 | // States that affect how the PIP can be manipulated |
| 69 | private boolean mInInteractiveMode; |
| 70 | private boolean mIsImeShowing; |
| 71 | private int mImeHeight; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 72 | private ValueAnimator mBoundsAnimator = null; |
| 73 | |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 74 | // Used to calculate stack bounds across rotations |
| 75 | private final DisplayInfo mDisplayInfo = new DisplayInfo(); |
| 76 | |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 77 | // The size and position information that describes where the pinned stack will go by default. |
| 78 | private int mDefaultStackGravity; |
| 79 | private Size mDefaultStackSize; |
| 80 | private Point mScreenEdgeInsets; |
| 81 | |
| 82 | // Temp vars for calculation |
| 83 | private final DisplayMetrics mTmpMetrics = new DisplayMetrics(); |
| 84 | private final Rect mTmpInsets = new Rect(); |
Jorim Jaggi | ad5d284 | 2016-11-01 18:22:53 -0700 | [diff] [blame] | 85 | private final Rect mTmpRect = new Rect(); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * The callback object passed to listeners for them to notify the controller of state changes. |
| 89 | */ |
| 90 | private class PinnedStackControllerCallback extends IPinnedStackController.Stub { |
| 91 | |
| 92 | @Override |
| 93 | public void setInInteractiveMode(final boolean inInteractiveMode) { |
| 94 | mHandler.post(() -> { |
| 95 | // Cancel any existing animations on the PIP once the user starts dragging it |
| 96 | if (mBoundsAnimator != null && inInteractiveMode) { |
| 97 | mBoundsAnimator.cancel(); |
| 98 | } |
| 99 | mInInteractiveMode = inInteractiveMode; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 100 | }); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Handler for the case where the listener dies. |
| 106 | */ |
| 107 | private class PinnedStackListenerDeathHandler implements IBinder.DeathRecipient { |
| 108 | |
| 109 | @Override |
| 110 | public void binderDied() { |
| 111 | // Clean up the state if the listener dies |
| 112 | mInInteractiveMode = false; |
| 113 | mPinnedStackListener = null; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | PinnedStackController(WindowManagerService service, DisplayContent displayContent) { |
| 118 | mService = service; |
| 119 | mDisplayContent = displayContent; |
| 120 | mSnapAlgorithm = new PipSnapAlgorithm(service.mContext); |
| 121 | mMotionHelper = new PipMotionHelper(BackgroundThread.getHandler()); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 122 | mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo()); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 123 | reloadResources(); |
| 124 | } |
| 125 | |
| 126 | void onConfigurationChanged() { |
| 127 | reloadResources(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Reloads all the resources for the current configuration. |
| 132 | */ |
| 133 | void reloadResources() { |
| 134 | final Resources res = mService.mContext.getResources(); |
| 135 | final Size defaultSizeDp = Size.parseSize(res.getString( |
| 136 | com.android.internal.R.string.config_defaultPictureInPictureSize)); |
| 137 | final Size screenEdgeInsetsDp = Size.parseSize(res.getString( |
| 138 | com.android.internal.R.string.config_defaultPictureInPictureScreenEdgeInsets)); |
| 139 | mDefaultStackGravity = res.getInteger( |
| 140 | com.android.internal.R.integer.config_defaultPictureInPictureGravity); |
| 141 | mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics); |
| 142 | mDefaultStackSize = new Size(dpToPx(defaultSizeDp.getWidth(), mTmpMetrics), |
| 143 | dpToPx(defaultSizeDp.getHeight(), mTmpMetrics)); |
| 144 | mScreenEdgeInsets = new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics), |
| 145 | dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics)); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Registers a pinned stack listener. |
| 150 | */ |
| 151 | void registerPinnedStackListener(IPinnedStackListener listener) { |
| 152 | try { |
| 153 | listener.asBinder().linkToDeath(mPinnedStackListenerDeathHandler, 0); |
| 154 | listener.onListenerRegistered(mCallbacks); |
| 155 | mPinnedStackListener = listener; |
| 156 | notifyBoundsChanged(mIsImeShowing); |
| 157 | } catch (RemoteException e) { |
| 158 | Log.e(TAG, "Failed to register pinned stack listener", e); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @return the default bounds to show the PIP when there is no active PIP. |
| 164 | */ |
| 165 | Rect getDefaultBounds() { |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 166 | final Rect insetBounds = new Rect(); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 167 | getInsetBounds(insetBounds); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 168 | |
| 169 | final Rect defaultBounds = new Rect(); |
| 170 | Gravity.apply(mDefaultStackGravity, mDefaultStackSize.getWidth(), |
| 171 | mDefaultStackSize.getHeight(), insetBounds, 0, 0, defaultBounds); |
| 172 | return defaultBounds; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @return the movement bounds for the given {@param stackBounds} and the current state of the |
| 177 | * controller. |
| 178 | */ |
| 179 | Rect getMovementBounds(Rect stackBounds) { |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 180 | return getMovementBounds(stackBounds, true /* adjustForIme */); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return the movement bounds for the given {@param stackBounds} and the current state of the |
| 185 | * controller. |
| 186 | */ |
| 187 | Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) { |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 188 | final Rect movementBounds = new Rect(); |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 189 | getInsetBounds(movementBounds); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 190 | |
| 191 | // Adjust the right/bottom to ensure the stack bounds never goes offscreen |
| 192 | movementBounds.right = Math.max(movementBounds.left, movementBounds.right - |
| 193 | stackBounds.width()); |
| 194 | movementBounds.bottom = Math.max(movementBounds.top, movementBounds.bottom - |
| 195 | stackBounds.height()); |
| 196 | |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 197 | // Apply the movement bounds adjustments based on the current state |
| 198 | if (adjustForIme) { |
| 199 | if (mIsImeShowing) { |
| 200 | movementBounds.bottom -= mImeHeight; |
| 201 | } |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 202 | } |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 203 | return movementBounds; |
| 204 | } |
| 205 | |
| 206 | /** |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 207 | * @return the repositioned PIP bounds given it's pre-change bounds, and the new display info. |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 208 | */ |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 209 | Rect onDisplayChanged(Rect preChangeStackBounds, DisplayInfo displayInfo) { |
| 210 | final Rect postChangeStackBounds = new Rect(preChangeStackBounds); |
| 211 | if (!mDisplayInfo.equals(displayInfo)) { |
| 212 | // Calculate the snap fraction of the current stack along the old movement bounds, and |
| 213 | // then update the stack bounds to the same fraction along the rotated movement bounds. |
| 214 | final Rect preChangeMovementBounds = getMovementBounds(preChangeStackBounds); |
| 215 | final float snapFraction = mSnapAlgorithm.getSnapFraction(preChangeStackBounds, |
| 216 | preChangeMovementBounds); |
| 217 | mDisplayInfo.copyFrom(displayInfo); |
| 218 | |
| 219 | final Rect postChangeMovementBounds = getMovementBounds(preChangeStackBounds, |
| 220 | false /* adjustForIme */); |
| 221 | mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds, |
| 222 | snapFraction); |
| 223 | } |
| 224 | return postChangeStackBounds; |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Sets the Ime state and height. |
| 229 | */ |
| 230 | void setAdjustedForIme(boolean adjustedForIme, int imeHeight) { |
| 231 | // Return early if there is no state change |
| 232 | if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | final Rect stackBounds = new Rect(); |
| 237 | mService.getStackBounds(PINNED_STACK_ID, stackBounds); |
| 238 | final Rect prevMovementBounds = getMovementBounds(stackBounds); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 239 | mIsImeShowing = adjustedForIme; |
| 240 | mImeHeight = imeHeight; |
| 241 | if (mInInteractiveMode) { |
| 242 | // If the user is currently interacting with the PIP and the ime state changes, then |
| 243 | // don't adjust the bounds and defer that to after the interaction |
| 244 | notifyBoundsChanged(adjustedForIme /* adjustedForIme */); |
| 245 | } else { |
| 246 | // Otherwise, we can move the PIP to a sane location to ensure that it does not block |
| 247 | // the user from interacting with the IME |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 248 | final Rect movementBounds = getMovementBounds(stackBounds); |
| 249 | final Rect toBounds = new Rect(stackBounds); |
| 250 | if (adjustedForIme) { |
| 251 | // IME visible |
| 252 | toBounds.offset(0, Math.min(0, movementBounds.bottom - stackBounds.top)); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 253 | } else { |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 254 | // IME hidden |
| 255 | if (stackBounds.top == prevMovementBounds.bottom) { |
| 256 | // If the PIP is resting on top of the IME, then adjust it with the hiding IME |
| 257 | toBounds.offsetTo(toBounds.left, movementBounds.bottom); |
| 258 | } |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 259 | } |
| 260 | if (!toBounds.equals(stackBounds)) { |
| 261 | if (mBoundsAnimator != null) { |
| 262 | mBoundsAnimator.cancel(); |
| 263 | } |
| 264 | mBoundsAnimator = mMotionHelper.createAnimationToBounds(stackBounds, toBounds); |
| 265 | mBoundsAnimator.start(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 271 | * Sends a broadcast that the PIP movement bounds have changed. |
| 272 | */ |
| 273 | private void notifyBoundsChanged(boolean adjustedForIme) { |
| 274 | if (mPinnedStackListener != null) { |
| 275 | try { |
| 276 | mPinnedStackListener.onBoundsChanged(adjustedForIme); |
| 277 | } catch (RemoteException e) { |
| 278 | Slog.e(TAG_WM, "Error delivering bounds changed event.", e); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @return the bounds on the screen that the PIP can be visible in. |
| 285 | */ |
Winson Chung | 14fefc2 | 2016-11-02 10:02:29 -0700 | [diff] [blame] | 286 | private void getInsetBounds(Rect outRect) { |
| 287 | mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth, |
| 288 | mDisplayInfo.logicalHeight, mTmpInsets); |
| 289 | outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y, |
| 290 | mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x, |
| 291 | mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
| 295 | * @return the pixels for a given dp value. |
| 296 | */ |
| 297 | private int dpToPx(float dpValue, DisplayMetrics dm) { |
| 298 | return (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, dpValue, dm); |
| 299 | } |
| 300 | |
| 301 | void dump(String prefix, PrintWriter pw) { |
| 302 | pw.println(prefix + "PinnedStackController"); |
Jorim Jaggi | ad5d284 | 2016-11-01 18:22:53 -0700 | [diff] [blame] | 303 | pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw); |
| 304 | pw.println(); |
| 305 | mService.getStackBounds(PINNED_STACK_ID, mTmpRect); |
| 306 | pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw); |
| 307 | pw.println(); |
Winson Chung | 655332c | 2016-10-31 13:14:28 -0700 | [diff] [blame] | 308 | pw.println(prefix + " mIsImeShowing=" + mIsImeShowing); |
| 309 | pw.println(prefix + " mInInteractiveMode=" + mInInteractiveMode); |
| 310 | } |
| 311 | } |