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