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