blob: d8726bfc2c20a3c566429a237b531b8bea89d4a8 [file] [log] [blame]
Winson Chung655332c2016-10-31 13:14:28 -07001/*
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
17package com.android.server.wm;
18
Wale Ogunwale68278562017-09-23 17:13:55 -070019import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070020import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Winson Chung655332c2016-10-31 13:14:28 -070021import static android.util.TypedValue.COMPLEX_UNIT_DIP;
22
23import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
24import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Steven Timotiusaf03df62017-07-18 16:56:43 -070025import static com.android.server.wm.proto.PinnedStackControllerProto.DEFAULT_BOUNDS;
26import static com.android.server.wm.proto.PinnedStackControllerProto.MOVEMENT_BOUNDS;
Winson Chung655332c2016-10-31 13:14:28 -070027
Winson Chunga29eb982016-12-14 12:01:27 -080028import android.app.RemoteAction;
29import android.content.pm.ParceledListSlice;
Winson Chung655332c2016-10-31 13:14:28 -070030import android.content.res.Resources;
31import android.graphics.Point;
32import android.graphics.Rect;
33import android.os.Handler;
34import android.os.IBinder;
35import android.os.RemoteException;
36import android.util.DisplayMetrics;
37import android.util.Log;
38import android.util.Size;
39import android.util.Slog;
40import android.util.TypedValue;
Steven Timotiusaf03df62017-07-18 16:56:43 -070041import android.util.proto.ProtoOutputStream;
Winson Chung14fefc22016-11-02 10:02:29 -070042import android.view.DisplayInfo;
Winson Chung655332c2016-10-31 13:14:28 -070043import android.view.Gravity;
44import android.view.IPinnedStackController;
45import android.view.IPinnedStackListener;
46
Winson Chung655332c2016-10-31 13:14:28 -070047import com.android.internal.policy.PipSnapAlgorithm;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070048import com.android.server.UiThread;
Winson Chung655332c2016-10-31 13:14:28 -070049
50import java.io.PrintWriter;
Winson Chunga29eb982016-12-14 12:01:27 -080051import java.util.ArrayList;
52import java.util.List;
Winson Chung655332c2016-10-31 13:14:28 -070053
54/**
Winson Chung2a82fe52017-02-02 14:43:34 -080055 * 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 Chung655332c2016-10-31 13:14:28 -070069 */
70class 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 Ogunwaleb783fd82016-11-04 09:51:54 -070076 private final Handler mHandler = UiThread.getHandler();
Winson Chung655332c2016-10-31 13:14:28 -070077
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 Chung655332c2016-10-31 13:14:28 -070084
85 // States that affect how the PIP can be manipulated
Winson Chungfa7053782016-11-08 15:45:10 -080086 private boolean mIsMinimized;
Winson Chung655332c2016-10-31 13:14:28 -070087 private boolean mIsImeShowing;
88 private int mImeHeight;
Winson Chung655332c2016-10-31 13:14:28 -070089
Winson Chung2a82fe52017-02-02 14:43:34 -080090 // The set of actions and aspect-ratio for the that are currently allowed on the PiP activity
Winson Chunga29eb982016-12-14 12:01:27 -080091 private ArrayList<RemoteAction> mActions = new ArrayList<>();
Winson Chung2a82fe52017-02-02 14:43:34 -080092 private float mAspectRatio = -1f;
Winson Chunga29eb982016-12-14 12:01:27 -080093
Winson Chung14fefc22016-11-02 10:02:29 -070094 // Used to calculate stack bounds across rotations
95 private final DisplayInfo mDisplayInfo = new DisplayInfo();
Winson Chung114aeea2017-01-09 16:08:07 -080096 private final Rect mStableInsets = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -070097
Winson Chung655332c2016-10-31 13:14:28 -070098 // The size and position information that describes where the pinned stack will go by default.
Winson Chunga71febe2017-05-22 11:14:22 -070099 private int mDefaultMinSize;
Winson Chung655332c2016-10-31 13:14:28 -0700100 private int mDefaultStackGravity;
Mady Mellora7f69742017-02-03 11:00:20 -0800101 private float mDefaultAspectRatio;
Winson Chung655332c2016-10-31 13:14:28 -0700102 private Point mScreenEdgeInsets;
Winson Chunga71febe2017-05-22 11:14:22 -0700103 private int mCurrentMinSize;
Winson Chung655332c2016-10-31 13:14:28 -0700104
Winson Chung2a82fe52017-02-02 14:43:34 -0800105 // The aspect ratio bounds of the PIP.
106 private float mMinAspectRatio;
107 private float mMaxAspectRatio;
108
Winson Chung655332c2016-10-31 13:14:28 -0700109 // Temp vars for calculation
110 private final DisplayMetrics mTmpMetrics = new DisplayMetrics();
111 private final Rect mTmpInsets = new Rect();
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700112 private final Rect mTmpRect = new Rect();
Winson Chung34a07f42017-03-10 18:06:25 -0800113 private final Rect mTmpAnimatingBoundsRect = new Rect();
Winson Chungf1f72f62017-02-14 17:15:48 -0800114 private final Point mTmpDisplaySize = new Point();
Winson Chung655332c2016-10-31 13:14:28 -0700115
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 Chungfa7053782016-11-08 15:45:10 -0800122 public void setIsMinimized(final boolean isMinimized) {
123 mHandler.post(() -> {
124 mIsMinimized = isMinimized;
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800125 mSnapAlgorithm.setMinimized(isMinimized);
Winson Chungfa7053782016-11-08 15:45:10 -0800126 });
127 }
Winson Chungef4dc812017-04-11 13:31:44 -0700128
129 @Override
Winson Chunga71febe2017-05-22 11:14:22 -0700130 public void setMinEdgeSize(int minEdgeSize) {
131 mHandler.post(() -> {
132 mCurrentMinSize = Math.max(mDefaultMinSize, minEdgeSize);
133 });
134 }
135
136 @Override
Winson Chungef4dc812017-04-11 13:31:44 -0700137 public int getDisplayRotation() {
138 synchronized (mService.mWindowMap) {
139 return mDisplayInfo.rotation;
140 }
141 }
Winson Chung655332c2016-10-31 13:14:28 -0700142 }
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 Chung397967f2017-11-01 16:21:35 -0700152 if (mPinnedStackListener != null) {
153 mPinnedStackListener.asBinder().unlinkToDeath(mPinnedStackListenerDeathHandler, 0);
154 }
Winson Chung655332c2016-10-31 13:14:28 -0700155 mPinnedStackListener = null;
156 }
157 }
158
159 PinnedStackController(WindowManagerService service, DisplayContent displayContent) {
160 mService = service;
161 mDisplayContent = displayContent;
162 mSnapAlgorithm = new PipSnapAlgorithm(service.mContext);
Winson Chung14fefc22016-11-02 10:02:29 -0700163 mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo());
Winson Chung655332c2016-10-31 13:14:28 -0700164 reloadResources();
Winson Chung401f78e2017-06-16 10:52:40 -0700165 // 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 Chung655332c2016-10-31 13:14:28 -0700169 }
170
171 void onConfigurationChanged() {
172 reloadResources();
173 }
174
175 /**
176 * Reloads all the resources for the current configuration.
177 */
Wale Ogunwale027f4752017-05-12 10:37:16 -0700178 private void reloadResources() {
Winson Chung655332c2016-10-31 13:14:28 -0700179 final Resources res = mService.mContext.getResources();
Winson Chunga71febe2017-05-22 11:14:22 -0700180 mDefaultMinSize = res.getDimensionPixelSize(
Mady Mellora7f69742017-02-03 11:00:20 -0800181 com.android.internal.R.dimen.default_minimal_size_pip_resizable_task);
Winson Chunga71febe2017-05-22 11:14:22 -0700182 mCurrentMinSize = mDefaultMinSize;
Mady Mellora7f69742017-02-03 11:00:20 -0800183 mDefaultAspectRatio = res.getFloat(
184 com.android.internal.R.dimen.config_pictureInPictureDefaultAspectRatio);
Wale Ogunwale027f4752017-05-12 10:37:16 -0700185 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 Chung655332c2016-10-31 13:14:28 -0700190 mDefaultStackGravity = res.getInteger(
191 com.android.internal.R.integer.config_defaultPictureInPictureGravity);
192 mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics);
Wale Ogunwale027f4752017-05-12 10:37:16 -0700193 mScreenEdgeInsets = screenEdgeInsetsDp == null ? new Point()
194 : new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics),
195 dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics));
Winson Chung2a82fe52017-02-02 14:43:34 -0800196 mMinAspectRatio = res.getFloat(
197 com.android.internal.R.dimen.config_pictureInPictureMinAspectRatio);
198 mMaxAspectRatio = res.getFloat(
199 com.android.internal.R.dimen.config_pictureInPictureMaxAspectRatio);
Winson Chung655332c2016-10-31 13:14:28 -0700200 }
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 Chung2a82fe52017-02-02 14:43:34 -0800210 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 Chunga29eb982016-12-14 12:01:27 -0800214 notifyActionsChanged(mActions);
Winson Chung2a82fe52017-02-02 14:43:34 -0800215 notifyMinimizeChanged(mIsMinimized);
Winson Chung655332c2016-10-31 13:14:28 -0700216 } catch (RemoteException e) {
217 Log.e(TAG, "Failed to register pinned stack listener", e);
218 }
219 }
220
221 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800222 * @return whether the given {@param aspectRatio} is valid.
223 */
224 public boolean isValidPictureInPictureAspectRatio(float aspectRatio) {
Winson Chungbd041962017-03-06 15:07:25 -0800225 return Float.compare(mMinAspectRatio, aspectRatio) <= 0 &&
226 Float.compare(aspectRatio, mMaxAspectRatio) <= 0;
Winson Chung2a82fe52017-02-02 14:43:34 -0800227 }
228
229 /**
Winson Chung84a38342016-11-08 16:15:10 -0800230 * Returns the current bounds (or the default bounds if there are no current bounds) with the
231 * specified aspect ratio.
232 */
Winson Chunga71febe2017-05-22 11:14:22 -0700233 Rect transformBoundsToAspectRatio(Rect stackBounds, float aspectRatio,
234 boolean useCurrentMinEdgeSize) {
Mady Mellora7f69742017-02-03 11:00:20 -0800235 // Save the snap fraction, calculate the aspect ratio based on screen size
Winson Chung84a38342016-11-08 16:15:10 -0800236 final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds,
237 getMovementBounds(stackBounds));
Winson Chunga71febe2017-05-22 11:14:22 -0700238
239 final int minEdgeSize = useCurrentMinEdgeSize ? mCurrentMinSize : mDefaultMinSize;
240 final Size size = mSnapAlgorithm.getSizeForAspectRatio(aspectRatio, minEdgeSize,
241 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
Mady Mellora7f69742017-02-03 11:00:20 -0800242 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 Chung84a38342016-11-08 16:15:10 -0800245 mSnapAlgorithm.applySnapFraction(stackBounds, getMovementBounds(stackBounds), snapFraction);
Winson Chungf1f72f62017-02-14 17:15:48 -0800246 if (mIsMinimized) {
247 applyMinimizedOffset(stackBounds, getMovementBounds(stackBounds));
248 }
Winson Chung84a38342016-11-08 16:15:10 -0800249 return stackBounds;
250 }
251
252 /**
Winson Chung655332c2016-10-31 13:14:28 -0700253 * @return the default bounds to show the PIP when there is no active PIP.
254 */
255 Rect getDefaultBounds() {
Winson Chungef4dc812017-04-11 13:31:44 -0700256 synchronized (mService.mWindowMap) {
257 final Rect insetBounds = new Rect();
258 getInsetBounds(insetBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700259
Winson Chungef4dc812017-04-11 13:31:44 -0700260 final Rect defaultBounds = new Rect();
Winson Chunga71febe2017-05-22 11:14:22 -0700261 final Size size = mSnapAlgorithm.getSizeForAspectRatio(mDefaultAspectRatio,
262 mDefaultMinSize, mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
Winson Chungef4dc812017-04-11 13:31:44 -0700263 Gravity.apply(mDefaultStackGravity, size.getWidth(), size.getHeight(), insetBounds,
264 0, mIsImeShowing ? mImeHeight : 0, defaultBounds);
265 return defaultBounds;
266 }
Winson Chung655332c2016-10-31 13:14:28 -0700267 }
268
269 /**
Winson Chung32c566f2017-04-11 18:31:21 -0700270 * 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 Chung47f2bf62017-02-16 18:58:12 -0800280 * Updates the display info, calculating and returning the new stack and movement bounds in the
281 * new orientation of the device if necessary.
Winson Chung655332c2016-10-31 13:14:28 -0700282 */
Winson Chung19953ca2017-04-11 11:19:23 -0700283 boolean onTaskStackBoundsChanged(Rect targetBounds, Rect outBounds) {
Winson Chungef4dc812017-04-11 13:31:44 -0700284 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 Chung19953ca2017-04-11 11:19:23 -0700305 mDisplayInfo.copyFrom(displayInfo);
Winson Chungef4dc812017-04-11 13:31:44 -0700306
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 Chung14fefc22016-11-02 10:02:29 -0700321 }
Winson Chung655332c2016-10-31 13:14:28 -0700322 }
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 Chung655332c2016-10-31 13:14:28 -0700333 mIsImeShowing = adjustedForIme;
334 mImeHeight = imeHeight;
Winson Chung2a82fe52017-02-02 14:43:34 -0800335 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 Chung655332c2016-10-31 13:14:28 -0700346 }
347 }
348
349 /**
Winson Chungc95ff842017-03-21 10:20:20 -0700350 * @return the current aspect ratio.
351 */
352 float getAspectRatio() {
353 return mAspectRatio;
354 }
355
356 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800357 * Sets the current set of actions.
358 */
359 void setActions(List<RemoteAction> actions) {
360 mActions.clear();
Winson Chungc2baac02017-01-11 13:34:47 -0800361 if (actions != null) {
362 mActions.addAll(actions);
363 }
Winson Chunga29eb982016-12-14 12:01:27 -0800364 notifyActionsChanged(mActions);
365 }
366
367 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800368 * Notifies listeners that the PIP needs to be adjusted for the IME.
Winson Chung655332c2016-10-31 13:14:28 -0700369 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800370 private void notifyImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Winson Chung655332c2016-10-31 13:14:28 -0700371 if (mPinnedStackListener != null) {
372 try {
Winson Chung2a82fe52017-02-02 14:43:34 -0800373 mPinnedStackListener.onImeVisibilityChanged(imeVisible, imeHeight);
Winson Chung655332c2016-10-31 13:14:28 -0700374 } catch (RemoteException e) {
375 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
376 }
377 }
378 }
379
380 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800381 * 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 Chunga29eb982016-12-14 12:01:27 -0800394 * 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 Chung2a82fe52017-02-02 14:43:34 -0800407 * Notifies listeners that the PIP movement bounds have changed.
408 */
409 private void notifyMovementBoundsChanged(boolean fromImeAdjustement) {
Winson Chungef4dc812017-04-11 13:31:44 -0700410 synchronized (mService.mWindowMap) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -0700411 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 Chung2a82fe52017-02-02 14:43:34 -0800421 }
Wale Ogunwaleb62139d2017-09-20 15:37:35 -0700422 final Rect animatingBounds = mTmpAnimatingBoundsRect;
Wale Ogunwale61911492017-10-11 08:50:50 -0700423 final TaskStack pinnedStack = mDisplayContent.getPinnedStack();
Wale Ogunwaleb62139d2017-09-20 15:37:35 -0700424 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 Chung2a82fe52017-02-02 14:43:34 -0800433 }
434 }
435 }
436
437 /**
Winson Chung655332c2016-10-31 13:14:28 -0700438 * @return the bounds on the screen that the PIP can be visible in.
439 */
Winson Chung14fefc22016-11-02 10:02:29 -0700440 private void getInsetBounds(Rect outRect) {
Winson Chungef4dc812017-04-11 13:31:44 -0700441 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 Chung655332c2016-10-31 13:14:28 -0700448 }
449
450 /**
Winson Chung55893332017-02-17 17:13:10 -0800451 * @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 Chungef4dc812017-04-11 13:31:44 -0700455 synchronized (mService.mWindowMap) {
456 return getMovementBounds(stackBounds, true /* adjustForIme */);
457 }
Winson Chung55893332017-02-17 17:13:10 -0800458 }
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 Chungef4dc812017-04-11 13:31:44 -0700465 synchronized (mService.mWindowMap) {
466 final Rect movementBounds = new Rect();
467 getInsetBounds(movementBounds);
Winson Chung55893332017-02-17 17:13:10 -0800468
Winson Chungef4dc812017-04-11 13:31:44 -0700469 // 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 Chung55893332017-02-17 17:13:10 -0800474 }
475
476 /**
Winson Chungf1f72f62017-02-14 17:15:48 -0800477 * Applies the minimized offsets to the given stack bounds.
478 */
479 private void applyMinimizedOffset(Rect stackBounds, Rect movementBounds) {
Winson Chungef4dc812017-04-11 13:31:44 -0700480 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 Chungf1f72f62017-02-14 17:15:48 -0800486 }
487
488 /**
Winson Chung655332c2016-10-31 13:14:28 -0700489 * @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 Jaggiad5d2842016-11-01 18:22:53 -0700497 pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw);
498 pw.println();
Wale Ogunwale68278562017-09-23 17:13:55 -0700499 mService.getStackBounds(WINDOWING_MODE_PINNED, ACTIVITY_TYPE_STANDARD, mTmpRect);
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700500 pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw);
501 pw.println();
Winson Chung655332c2016-10-31 13:14:28 -0700502 pw.println(prefix + " mIsImeShowing=" + mIsImeShowing);
Winson Chungfa7053782016-11-08 15:45:10 -0800503 pw.println(prefix + " mIsMinimized=" + mIsMinimized);
Winson Chunga29eb982016-12-14 12:01:27 -0800504 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 Chung655332c2016-10-31 13:14:28 -0700515 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700516
517 void writeToProto(ProtoOutputStream proto, long fieldId) {
518 final long token = proto.start(fieldId);
519 getDefaultBounds().writeToProto(proto, DEFAULT_BOUNDS);
Wale Ogunwale68278562017-09-23 17:13:55 -0700520 mService.getStackBounds(WINDOWING_MODE_PINNED, ACTIVITY_TYPE_STANDARD, mTmpRect);
Steven Timotiusaf03df62017-07-18 16:56:43 -0700521 getMovementBounds(mTmpRect).writeToProto(proto, MOVEMENT_BOUNDS);
522 proto.end(token);
523 }
Winson Chung655332c2016-10-31 13:14:28 -0700524}