blob: 6d33ce2941bc0489d4c9f9baff4e422f9190f4e7 [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
19import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
20import static android.util.TypedValue.COMPLEX_UNIT_DIP;
21
22import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
23import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
24
Winson Chunga29eb982016-12-14 12:01:27 -080025import android.app.RemoteAction;
26import android.content.pm.ParceledListSlice;
Winson Chung655332c2016-10-31 13:14:28 -070027import android.content.res.Resources;
28import android.graphics.Point;
29import android.graphics.Rect;
30import android.os.Handler;
31import android.os.IBinder;
32import android.os.RemoteException;
33import android.util.DisplayMetrics;
34import android.util.Log;
35import android.util.Size;
36import android.util.Slog;
37import android.util.TypedValue;
Winson Chung14fefc22016-11-02 10:02:29 -070038import android.view.DisplayInfo;
Winson Chung655332c2016-10-31 13:14:28 -070039import android.view.Gravity;
40import android.view.IPinnedStackController;
41import android.view.IPinnedStackListener;
42
Winson Chung655332c2016-10-31 13:14:28 -070043import com.android.internal.policy.PipSnapAlgorithm;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070044import com.android.server.UiThread;
Winson Chung655332c2016-10-31 13:14:28 -070045
46import java.io.PrintWriter;
Winson Chunga29eb982016-12-14 12:01:27 -080047import java.util.ArrayList;
48import java.util.List;
Winson Chung655332c2016-10-31 13:14:28 -070049
50/**
Winson Chung2a82fe52017-02-02 14:43:34 -080051 * Holds the common state of the pinned stack between the system and SystemUI. If SystemUI ever
52 * needs to be restarted, it will be notified with the last known state.
53 *
54 * Changes to the pinned stack also flow through this controller, and generally, the system only
55 * changes the pinned stack bounds through this controller in two ways:
56 *
57 * 1) When first entering PiP: the controller returns the valid bounds given, taking aspect ratio
58 * and IME state into account.
59 * 2) When rotating the device: the controller calculates the new bounds in the new orientation,
60 * taking the minimized and IME state into account. In this case, we currently ignore the
61 * SystemUI adjustments (ie. expanded for menu, interaction, etc).
62 *
63 * Other changes in the system, including adjustment of IME, configuration change, and more are
64 * handled by SystemUI (similar to the docked stack divider).
Winson Chung655332c2016-10-31 13:14:28 -070065 */
66class PinnedStackController {
67
68 private static final String TAG = TAG_WITH_CLASS_NAME ? "PinnedStackController" : TAG_WM;
69
70 private final WindowManagerService mService;
71 private final DisplayContent mDisplayContent;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070072 private final Handler mHandler = UiThread.getHandler();
Winson Chung655332c2016-10-31 13:14:28 -070073
74 private IPinnedStackListener mPinnedStackListener;
75 private final PinnedStackListenerDeathHandler mPinnedStackListenerDeathHandler =
76 new PinnedStackListenerDeathHandler();
77
78 private final PinnedStackControllerCallback mCallbacks = new PinnedStackControllerCallback();
79 private final PipSnapAlgorithm mSnapAlgorithm;
Winson Chung655332c2016-10-31 13:14:28 -070080
81 // States that affect how the PIP can be manipulated
Winson Chungfa7053782016-11-08 15:45:10 -080082 private boolean mIsMinimized;
Winson Chung655332c2016-10-31 13:14:28 -070083 private boolean mIsImeShowing;
84 private int mImeHeight;
Winson Chung655332c2016-10-31 13:14:28 -070085
Winson Chung2a82fe52017-02-02 14:43:34 -080086 // The set of actions and aspect-ratio for the that are currently allowed on the PiP activity
Winson Chunga29eb982016-12-14 12:01:27 -080087 private ArrayList<RemoteAction> mActions = new ArrayList<>();
Winson Chung2a82fe52017-02-02 14:43:34 -080088 private float mAspectRatio = -1f;
Winson Chunga29eb982016-12-14 12:01:27 -080089
Winson Chung14fefc22016-11-02 10:02:29 -070090 // Used to calculate stack bounds across rotations
91 private final DisplayInfo mDisplayInfo = new DisplayInfo();
Winson Chung114aeea2017-01-09 16:08:07 -080092 private final Rect mStableInsets = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -070093
Winson Chung655332c2016-10-31 13:14:28 -070094 // The size and position information that describes where the pinned stack will go by default.
Winson Chunga71febe2017-05-22 11:14:22 -070095 private int mDefaultMinSize;
Winson Chung655332c2016-10-31 13:14:28 -070096 private int mDefaultStackGravity;
Mady Mellora7f69742017-02-03 11:00:20 -080097 private float mDefaultAspectRatio;
Winson Chung655332c2016-10-31 13:14:28 -070098 private Point mScreenEdgeInsets;
Winson Chunga71febe2017-05-22 11:14:22 -070099 private int mCurrentMinSize;
Winson Chung655332c2016-10-31 13:14:28 -0700100
Winson Chung2a82fe52017-02-02 14:43:34 -0800101 // The aspect ratio bounds of the PIP.
102 private float mMinAspectRatio;
103 private float mMaxAspectRatio;
104
Winson Chung655332c2016-10-31 13:14:28 -0700105 // Temp vars for calculation
106 private final DisplayMetrics mTmpMetrics = new DisplayMetrics();
107 private final Rect mTmpInsets = new Rect();
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700108 private final Rect mTmpRect = new Rect();
Winson Chung34a07f42017-03-10 18:06:25 -0800109 private final Rect mTmpAnimatingBoundsRect = new Rect();
Winson Chungf1f72f62017-02-14 17:15:48 -0800110 private final Point mTmpDisplaySize = new Point();
Winson Chung655332c2016-10-31 13:14:28 -0700111
112 /**
113 * The callback object passed to listeners for them to notify the controller of state changes.
114 */
115 private class PinnedStackControllerCallback extends IPinnedStackController.Stub {
116
117 @Override
Winson Chungfa7053782016-11-08 15:45:10 -0800118 public void setIsMinimized(final boolean isMinimized) {
119 mHandler.post(() -> {
120 mIsMinimized = isMinimized;
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800121 mSnapAlgorithm.setMinimized(isMinimized);
Winson Chungfa7053782016-11-08 15:45:10 -0800122 });
123 }
Winson Chungef4dc812017-04-11 13:31:44 -0700124
125 @Override
Winson Chunga71febe2017-05-22 11:14:22 -0700126 public void setMinEdgeSize(int minEdgeSize) {
127 mHandler.post(() -> {
128 mCurrentMinSize = Math.max(mDefaultMinSize, minEdgeSize);
129 });
130 }
131
132 @Override
Winson Chungef4dc812017-04-11 13:31:44 -0700133 public int getDisplayRotation() {
134 synchronized (mService.mWindowMap) {
135 return mDisplayInfo.rotation;
136 }
137 }
Winson Chung655332c2016-10-31 13:14:28 -0700138 }
139
140 /**
141 * Handler for the case where the listener dies.
142 */
143 private class PinnedStackListenerDeathHandler implements IBinder.DeathRecipient {
144
145 @Override
146 public void binderDied() {
147 // Clean up the state if the listener dies
Winson Chung655332c2016-10-31 13:14:28 -0700148 mPinnedStackListener = null;
149 }
150 }
151
152 PinnedStackController(WindowManagerService service, DisplayContent displayContent) {
153 mService = service;
154 mDisplayContent = displayContent;
155 mSnapAlgorithm = new PipSnapAlgorithm(service.mContext);
Winson Chung14fefc22016-11-02 10:02:29 -0700156 mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo());
Winson Chung655332c2016-10-31 13:14:28 -0700157 reloadResources();
Winson Chung401f78e2017-06-16 10:52:40 -0700158 // Initialize the aspect ratio to the default aspect ratio. Don't do this in reload
159 // resources as it would clobber mAspectRatio when entering PiP from fullscreen which
160 // triggers a configuration change and the resources to be reloaded.
161 mAspectRatio = mDefaultAspectRatio;
Winson Chung655332c2016-10-31 13:14:28 -0700162 }
163
164 void onConfigurationChanged() {
165 reloadResources();
166 }
167
168 /**
169 * Reloads all the resources for the current configuration.
170 */
Wale Ogunwale027f4752017-05-12 10:37:16 -0700171 private void reloadResources() {
Winson Chung655332c2016-10-31 13:14:28 -0700172 final Resources res = mService.mContext.getResources();
Winson Chunga71febe2017-05-22 11:14:22 -0700173 mDefaultMinSize = res.getDimensionPixelSize(
Mady Mellora7f69742017-02-03 11:00:20 -0800174 com.android.internal.R.dimen.default_minimal_size_pip_resizable_task);
Winson Chunga71febe2017-05-22 11:14:22 -0700175 mCurrentMinSize = mDefaultMinSize;
Mady Mellora7f69742017-02-03 11:00:20 -0800176 mDefaultAspectRatio = res.getFloat(
177 com.android.internal.R.dimen.config_pictureInPictureDefaultAspectRatio);
Wale Ogunwale027f4752017-05-12 10:37:16 -0700178 final String screenEdgeInsetsDpString = res.getString(
179 com.android.internal.R.string.config_defaultPictureInPictureScreenEdgeInsets);
180 final Size screenEdgeInsetsDp = !screenEdgeInsetsDpString.isEmpty()
181 ? Size.parseSize(screenEdgeInsetsDpString)
182 : null;
Winson Chung655332c2016-10-31 13:14:28 -0700183 mDefaultStackGravity = res.getInteger(
184 com.android.internal.R.integer.config_defaultPictureInPictureGravity);
185 mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics);
Wale Ogunwale027f4752017-05-12 10:37:16 -0700186 mScreenEdgeInsets = screenEdgeInsetsDp == null ? new Point()
187 : new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics),
188 dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics));
Winson Chung2a82fe52017-02-02 14:43:34 -0800189 mMinAspectRatio = res.getFloat(
190 com.android.internal.R.dimen.config_pictureInPictureMinAspectRatio);
191 mMaxAspectRatio = res.getFloat(
192 com.android.internal.R.dimen.config_pictureInPictureMaxAspectRatio);
Winson Chung655332c2016-10-31 13:14:28 -0700193 }
194
195 /**
196 * Registers a pinned stack listener.
197 */
198 void registerPinnedStackListener(IPinnedStackListener listener) {
199 try {
200 listener.asBinder().linkToDeath(mPinnedStackListenerDeathHandler, 0);
201 listener.onListenerRegistered(mCallbacks);
202 mPinnedStackListener = listener;
Winson Chung2a82fe52017-02-02 14:43:34 -0800203 notifyImeVisibilityChanged(mIsImeShowing, mImeHeight);
204 // The movement bounds notification needs to be sent before the minimized state, since
205 // SystemUI may use the bounds to retore the minimized position
206 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chunga29eb982016-12-14 12:01:27 -0800207 notifyActionsChanged(mActions);
Winson Chung2a82fe52017-02-02 14:43:34 -0800208 notifyMinimizeChanged(mIsMinimized);
Winson Chung655332c2016-10-31 13:14:28 -0700209 } catch (RemoteException e) {
210 Log.e(TAG, "Failed to register pinned stack listener", e);
211 }
212 }
213
214 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800215 * @return whether the given {@param aspectRatio} is valid.
216 */
217 public boolean isValidPictureInPictureAspectRatio(float aspectRatio) {
Winson Chungbd041962017-03-06 15:07:25 -0800218 return Float.compare(mMinAspectRatio, aspectRatio) <= 0 &&
219 Float.compare(aspectRatio, mMaxAspectRatio) <= 0;
Winson Chung2a82fe52017-02-02 14:43:34 -0800220 }
221
222 /**
Winson Chung84a38342016-11-08 16:15:10 -0800223 * Returns the current bounds (or the default bounds if there are no current bounds) with the
224 * specified aspect ratio.
225 */
Winson Chunga71febe2017-05-22 11:14:22 -0700226 Rect transformBoundsToAspectRatio(Rect stackBounds, float aspectRatio,
227 boolean useCurrentMinEdgeSize) {
Mady Mellora7f69742017-02-03 11:00:20 -0800228 // Save the snap fraction, calculate the aspect ratio based on screen size
Winson Chung84a38342016-11-08 16:15:10 -0800229 final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds,
230 getMovementBounds(stackBounds));
Winson Chunga71febe2017-05-22 11:14:22 -0700231
232 final int minEdgeSize = useCurrentMinEdgeSize ? mCurrentMinSize : mDefaultMinSize;
233 final Size size = mSnapAlgorithm.getSizeForAspectRatio(aspectRatio, minEdgeSize,
234 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
Mady Mellora7f69742017-02-03 11:00:20 -0800235 final int left = (int) (stackBounds.centerX() - size.getWidth() / 2f);
236 final int top = (int) (stackBounds.centerY() - size.getHeight() / 2f);
237 stackBounds.set(left, top, left + size.getWidth(), top + size.getHeight());
Winson Chung84a38342016-11-08 16:15:10 -0800238 mSnapAlgorithm.applySnapFraction(stackBounds, getMovementBounds(stackBounds), snapFraction);
Winson Chungf1f72f62017-02-14 17:15:48 -0800239 if (mIsMinimized) {
240 applyMinimizedOffset(stackBounds, getMovementBounds(stackBounds));
241 }
Winson Chung84a38342016-11-08 16:15:10 -0800242 return stackBounds;
243 }
244
245 /**
Winson Chung655332c2016-10-31 13:14:28 -0700246 * @return the default bounds to show the PIP when there is no active PIP.
247 */
248 Rect getDefaultBounds() {
Winson Chungef4dc812017-04-11 13:31:44 -0700249 synchronized (mService.mWindowMap) {
250 final Rect insetBounds = new Rect();
251 getInsetBounds(insetBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700252
Winson Chungef4dc812017-04-11 13:31:44 -0700253 final Rect defaultBounds = new Rect();
Winson Chunga71febe2017-05-22 11:14:22 -0700254 final Size size = mSnapAlgorithm.getSizeForAspectRatio(mDefaultAspectRatio,
255 mDefaultMinSize, mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
Winson Chungef4dc812017-04-11 13:31:44 -0700256 Gravity.apply(mDefaultStackGravity, size.getWidth(), size.getHeight(), insetBounds,
257 0, mIsImeShowing ? mImeHeight : 0, defaultBounds);
258 return defaultBounds;
259 }
Winson Chung655332c2016-10-31 13:14:28 -0700260 }
261
262 /**
Winson Chung32c566f2017-04-11 18:31:21 -0700263 * In the case where the display rotation is changed but there is no stack, we can't depend on
264 * onTaskStackBoundsChanged() to be called. But we still should update our known display info
265 * with the new state so that we can update SystemUI.
266 */
267 synchronized void onDisplayInfoChanged() {
268 mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo());
269 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
270 }
271
272 /**
Winson Chung47f2bf62017-02-16 18:58:12 -0800273 * Updates the display info, calculating and returning the new stack and movement bounds in the
274 * new orientation of the device if necessary.
Winson Chung655332c2016-10-31 13:14:28 -0700275 */
Winson Chung19953ca2017-04-11 11:19:23 -0700276 boolean onTaskStackBoundsChanged(Rect targetBounds, Rect outBounds) {
Winson Chungef4dc812017-04-11 13:31:44 -0700277 synchronized (mService.mWindowMap) {
278 final DisplayInfo displayInfo = mDisplayContent.getDisplayInfo();
279 if (mDisplayInfo.equals(displayInfo)) {
280 // We are already in the right orientation, ignore
281 outBounds.setEmpty();
282 return false;
283 } else if (targetBounds.isEmpty()) {
284 // The stack is null, we are just initializing the stack, so just store the display
285 // info and ignore
286 mDisplayInfo.copyFrom(displayInfo);
287 outBounds.setEmpty();
288 return false;
289 }
290
291 mTmpRect.set(targetBounds);
292 final Rect postChangeStackBounds = mTmpRect;
293
294 // Calculate the snap fraction of the current stack along the old movement bounds
295 final Rect preChangeMovementBounds = getMovementBounds(postChangeStackBounds);
296 final float snapFraction = mSnapAlgorithm.getSnapFraction(postChangeStackBounds,
297 preChangeMovementBounds);
Winson Chung19953ca2017-04-11 11:19:23 -0700298 mDisplayInfo.copyFrom(displayInfo);
Winson Chungef4dc812017-04-11 13:31:44 -0700299
300 // Calculate the stack bounds in the new orientation to the same same fraction along the
301 // rotated movement bounds.
302 final Rect postChangeMovementBounds = getMovementBounds(postChangeStackBounds,
303 false /* adjustForIme */);
304 mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds,
305 snapFraction);
306 if (mIsMinimized) {
307 applyMinimizedOffset(postChangeStackBounds, postChangeMovementBounds);
308 }
309
310 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
311
312 outBounds.set(postChangeStackBounds);
313 return true;
Winson Chung14fefc22016-11-02 10:02:29 -0700314 }
Winson Chung655332c2016-10-31 13:14:28 -0700315 }
316
317 /**
318 * Sets the Ime state and height.
319 */
320 void setAdjustedForIme(boolean adjustedForIme, int imeHeight) {
321 // Return early if there is no state change
322 if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) {
323 return;
324 }
325
Winson Chung655332c2016-10-31 13:14:28 -0700326 mIsImeShowing = adjustedForIme;
327 mImeHeight = imeHeight;
Winson Chung2a82fe52017-02-02 14:43:34 -0800328 notifyImeVisibilityChanged(adjustedForIme, imeHeight);
329 notifyMovementBoundsChanged(true /* fromImeAdjustment */);
330 }
331
332 /**
333 * Sets the current aspect ratio.
334 */
335 void setAspectRatio(float aspectRatio) {
336 if (Float.compare(mAspectRatio, aspectRatio) != 0) {
337 mAspectRatio = aspectRatio;
338 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chung655332c2016-10-31 13:14:28 -0700339 }
340 }
341
342 /**
Winson Chungc95ff842017-03-21 10:20:20 -0700343 * @return the current aspect ratio.
344 */
345 float getAspectRatio() {
346 return mAspectRatio;
347 }
348
349 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800350 * Sets the current set of actions.
351 */
352 void setActions(List<RemoteAction> actions) {
353 mActions.clear();
Winson Chungc2baac02017-01-11 13:34:47 -0800354 if (actions != null) {
355 mActions.addAll(actions);
356 }
Winson Chunga29eb982016-12-14 12:01:27 -0800357 notifyActionsChanged(mActions);
358 }
359
360 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800361 * Notifies listeners that the PIP needs to be adjusted for the IME.
Winson Chung655332c2016-10-31 13:14:28 -0700362 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800363 private void notifyImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Winson Chung655332c2016-10-31 13:14:28 -0700364 if (mPinnedStackListener != null) {
365 try {
Winson Chung2a82fe52017-02-02 14:43:34 -0800366 mPinnedStackListener.onImeVisibilityChanged(imeVisible, imeHeight);
Winson Chung655332c2016-10-31 13:14:28 -0700367 } catch (RemoteException e) {
368 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
369 }
370 }
371 }
372
373 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800374 * Notifies listeners that the PIP minimized state has changed.
375 */
376 private void notifyMinimizeChanged(boolean isMinimized) {
377 if (mPinnedStackListener != null) {
378 try {
379 mPinnedStackListener.onMinimizedStateChanged(isMinimized);
380 } catch (RemoteException e) {
381 Slog.e(TAG_WM, "Error delivering minimize changed event.", e);
382 }
383 }
384 }
385
386 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800387 * Notifies listeners that the PIP actions have changed.
388 */
389 private void notifyActionsChanged(List<RemoteAction> actions) {
390 if (mPinnedStackListener != null) {
391 try {
392 mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions));
393 } catch (RemoteException e) {
394 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
395 }
396 }
397 }
398
399 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800400 * Notifies listeners that the PIP movement bounds have changed.
401 */
402 private void notifyMovementBoundsChanged(boolean fromImeAdjustement) {
Winson Chungef4dc812017-04-11 13:31:44 -0700403 synchronized (mService.mWindowMap) {
404 if (mPinnedStackListener != null) {
405 try {
406 final Rect insetBounds = new Rect();
407 getInsetBounds(insetBounds);
408 final Rect normalBounds = getDefaultBounds();
409 if (isValidPictureInPictureAspectRatio(mAspectRatio)) {
Winson Chunga71febe2017-05-22 11:14:22 -0700410 transformBoundsToAspectRatio(normalBounds, mAspectRatio,
411 false /* useCurrentMinEdgeSize */);
Winson Chungef4dc812017-04-11 13:31:44 -0700412 }
413 final Rect animatingBounds = mTmpAnimatingBoundsRect;
414 final TaskStack pinnedStack = mDisplayContent.getStackById(PINNED_STACK_ID);
415 if (pinnedStack != null) {
416 pinnedStack.getAnimationOrCurrentBounds(animatingBounds);
417 } else {
418 animatingBounds.set(normalBounds);
419 }
420 mPinnedStackListener.onMovementBoundsChanged(insetBounds, normalBounds,
421 animatingBounds, fromImeAdjustement, mDisplayInfo.rotation);
422 } catch (RemoteException e) {
423 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
Winson Chung2a82fe52017-02-02 14:43:34 -0800424 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800425 }
426 }
427 }
428
429 /**
Winson Chung655332c2016-10-31 13:14:28 -0700430 * @return the bounds on the screen that the PIP can be visible in.
431 */
Winson Chung14fefc22016-11-02 10:02:29 -0700432 private void getInsetBounds(Rect outRect) {
Winson Chungef4dc812017-04-11 13:31:44 -0700433 synchronized (mService.mWindowMap) {
434 mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth,
435 mDisplayInfo.logicalHeight, mTmpInsets);
436 outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y,
437 mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x,
438 mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y);
439 }
Winson Chung655332c2016-10-31 13:14:28 -0700440 }
441
442 /**
Winson Chung55893332017-02-17 17:13:10 -0800443 * @return the movement bounds for the given {@param stackBounds} and the current state of the
444 * controller.
445 */
446 private Rect getMovementBounds(Rect stackBounds) {
Winson Chungef4dc812017-04-11 13:31:44 -0700447 synchronized (mService.mWindowMap) {
448 return getMovementBounds(stackBounds, true /* adjustForIme */);
449 }
Winson Chung55893332017-02-17 17:13:10 -0800450 }
451
452 /**
453 * @return the movement bounds for the given {@param stackBounds} and the current state of the
454 * controller.
455 */
456 private Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
Winson Chungef4dc812017-04-11 13:31:44 -0700457 synchronized (mService.mWindowMap) {
458 final Rect movementBounds = new Rect();
459 getInsetBounds(movementBounds);
Winson Chung55893332017-02-17 17:13:10 -0800460
Winson Chungef4dc812017-04-11 13:31:44 -0700461 // Apply the movement bounds adjustments based on the current state
462 mSnapAlgorithm.getMovementBounds(stackBounds, movementBounds, movementBounds,
463 (adjustForIme && mIsImeShowing) ? mImeHeight : 0);
464 return movementBounds;
465 }
Winson Chung55893332017-02-17 17:13:10 -0800466 }
467
468 /**
Winson Chungf1f72f62017-02-14 17:15:48 -0800469 * Applies the minimized offsets to the given stack bounds.
470 */
471 private void applyMinimizedOffset(Rect stackBounds, Rect movementBounds) {
Winson Chungef4dc812017-04-11 13:31:44 -0700472 synchronized (mService.mWindowMap) {
473 mTmpDisplaySize.set(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
474 mService.getStableInsetsLocked(mDisplayContent.getDisplayId(), mStableInsets);
475 mSnapAlgorithm.applyMinimizedOffset(stackBounds, movementBounds, mTmpDisplaySize,
476 mStableInsets);
477 }
Winson Chungf1f72f62017-02-14 17:15:48 -0800478 }
479
480 /**
Winson Chung655332c2016-10-31 13:14:28 -0700481 * @return the pixels for a given dp value.
482 */
483 private int dpToPx(float dpValue, DisplayMetrics dm) {
484 return (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, dpValue, dm);
485 }
486
487 void dump(String prefix, PrintWriter pw) {
488 pw.println(prefix + "PinnedStackController");
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700489 pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw);
490 pw.println();
491 mService.getStackBounds(PINNED_STACK_ID, mTmpRect);
492 pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw);
493 pw.println();
Winson Chung655332c2016-10-31 13:14:28 -0700494 pw.println(prefix + " mIsImeShowing=" + mIsImeShowing);
Winson Chungfa7053782016-11-08 15:45:10 -0800495 pw.println(prefix + " mIsMinimized=" + mIsMinimized);
Winson Chunga29eb982016-12-14 12:01:27 -0800496 if (mActions.isEmpty()) {
497 pw.println(prefix + " mActions=[]");
498 } else {
499 pw.println(prefix + " mActions=[");
500 for (int i = 0; i < mActions.size(); i++) {
501 RemoteAction action = mActions.get(i);
502 pw.print(prefix + " Action[" + i + "]: ");
503 action.dump("", pw);
504 }
505 pw.println(prefix + " ]");
506 }
Winson Chung655332c2016-10-31 13:14:28 -0700507 }
508}