blob: 1e7140a12d016bc55df2b3dbed8629e0543e7ae5 [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;
Steven Timotiusaf03df62017-07-18 16:56:43 -070024import static com.android.server.wm.proto.PinnedStackControllerProto.DEFAULT_BOUNDS;
25import static com.android.server.wm.proto.PinnedStackControllerProto.MOVEMENT_BOUNDS;
Winson Chung655332c2016-10-31 13:14:28 -070026
Winson Chunga29eb982016-12-14 12:01:27 -080027import android.app.RemoteAction;
28import android.content.pm.ParceledListSlice;
Winson Chung655332c2016-10-31 13:14:28 -070029import android.content.res.Resources;
30import android.graphics.Point;
31import android.graphics.Rect;
32import android.os.Handler;
33import android.os.IBinder;
34import android.os.RemoteException;
35import android.util.DisplayMetrics;
36import android.util.Log;
37import android.util.Size;
38import android.util.Slog;
39import android.util.TypedValue;
Steven Timotiusaf03df62017-07-18 16:56:43 -070040import android.util.proto.ProtoOutputStream;
Winson Chung14fefc22016-11-02 10:02:29 -070041import android.view.DisplayInfo;
Winson Chung655332c2016-10-31 13:14:28 -070042import android.view.Gravity;
43import android.view.IPinnedStackController;
44import android.view.IPinnedStackListener;
45
Winson Chung655332c2016-10-31 13:14:28 -070046import com.android.internal.policy.PipSnapAlgorithm;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070047import com.android.server.UiThread;
Winson Chung655332c2016-10-31 13:14:28 -070048
49import java.io.PrintWriter;
Winson Chunga29eb982016-12-14 12:01:27 -080050import java.util.ArrayList;
51import java.util.List;
Winson Chung655332c2016-10-31 13:14:28 -070052
53/**
Winson Chung2a82fe52017-02-02 14:43:34 -080054 * 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 Chung655332c2016-10-31 13:14:28 -070068 */
69class 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 Ogunwaleb783fd82016-11-04 09:51:54 -070075 private final Handler mHandler = UiThread.getHandler();
Winson Chung655332c2016-10-31 13:14:28 -070076
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 Chung655332c2016-10-31 13:14:28 -070083
84 // States that affect how the PIP can be manipulated
Winson Chungfa7053782016-11-08 15:45:10 -080085 private boolean mIsMinimized;
Winson Chung655332c2016-10-31 13:14:28 -070086 private boolean mIsImeShowing;
87 private int mImeHeight;
Winson Chung655332c2016-10-31 13:14:28 -070088
Winson Chung2a82fe52017-02-02 14:43:34 -080089 // The set of actions and aspect-ratio for the that are currently allowed on the PiP activity
Winson Chunga29eb982016-12-14 12:01:27 -080090 private ArrayList<RemoteAction> mActions = new ArrayList<>();
Winson Chung2a82fe52017-02-02 14:43:34 -080091 private float mAspectRatio = -1f;
Winson Chunga29eb982016-12-14 12:01:27 -080092
Winson Chung14fefc22016-11-02 10:02:29 -070093 // Used to calculate stack bounds across rotations
94 private final DisplayInfo mDisplayInfo = new DisplayInfo();
Winson Chung114aeea2017-01-09 16:08:07 -080095 private final Rect mStableInsets = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -070096
Winson Chung655332c2016-10-31 13:14:28 -070097 // The size and position information that describes where the pinned stack will go by default.
Winson Chunga71febe2017-05-22 11:14:22 -070098 private int mDefaultMinSize;
Winson Chung655332c2016-10-31 13:14:28 -070099 private int mDefaultStackGravity;
Mady Mellora7f69742017-02-03 11:00:20 -0800100 private float mDefaultAspectRatio;
Winson Chung655332c2016-10-31 13:14:28 -0700101 private Point mScreenEdgeInsets;
Winson Chunga71febe2017-05-22 11:14:22 -0700102 private int mCurrentMinSize;
Winson Chung655332c2016-10-31 13:14:28 -0700103
Winson Chung2a82fe52017-02-02 14:43:34 -0800104 // The aspect ratio bounds of the PIP.
105 private float mMinAspectRatio;
106 private float mMaxAspectRatio;
107
Winson Chung655332c2016-10-31 13:14:28 -0700108 // Temp vars for calculation
109 private final DisplayMetrics mTmpMetrics = new DisplayMetrics();
110 private final Rect mTmpInsets = new Rect();
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700111 private final Rect mTmpRect = new Rect();
Winson Chung34a07f42017-03-10 18:06:25 -0800112 private final Rect mTmpAnimatingBoundsRect = new Rect();
Winson Chungf1f72f62017-02-14 17:15:48 -0800113 private final Point mTmpDisplaySize = new Point();
Winson Chung655332c2016-10-31 13:14:28 -0700114
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 Chungfa7053782016-11-08 15:45:10 -0800121 public void setIsMinimized(final boolean isMinimized) {
122 mHandler.post(() -> {
123 mIsMinimized = isMinimized;
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800124 mSnapAlgorithm.setMinimized(isMinimized);
Winson Chungfa7053782016-11-08 15:45:10 -0800125 });
126 }
Winson Chungef4dc812017-04-11 13:31:44 -0700127
128 @Override
Winson Chunga71febe2017-05-22 11:14:22 -0700129 public void setMinEdgeSize(int minEdgeSize) {
130 mHandler.post(() -> {
131 mCurrentMinSize = Math.max(mDefaultMinSize, minEdgeSize);
132 });
133 }
134
135 @Override
Winson Chungef4dc812017-04-11 13:31:44 -0700136 public int getDisplayRotation() {
137 synchronized (mService.mWindowMap) {
138 return mDisplayInfo.rotation;
139 }
140 }
Winson Chung655332c2016-10-31 13:14:28 -0700141 }
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 Chung655332c2016-10-31 13:14:28 -0700151 mPinnedStackListener = null;
152 }
153 }
154
155 PinnedStackController(WindowManagerService service, DisplayContent displayContent) {
156 mService = service;
157 mDisplayContent = displayContent;
158 mSnapAlgorithm = new PipSnapAlgorithm(service.mContext);
Winson Chung14fefc22016-11-02 10:02:29 -0700159 mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo());
Winson Chung655332c2016-10-31 13:14:28 -0700160 reloadResources();
Winson Chung401f78e2017-06-16 10:52:40 -0700161 // 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 Chung655332c2016-10-31 13:14:28 -0700165 }
166
167 void onConfigurationChanged() {
168 reloadResources();
169 }
170
171 /**
172 * Reloads all the resources for the current configuration.
173 */
Wale Ogunwale027f4752017-05-12 10:37:16 -0700174 private void reloadResources() {
Winson Chung655332c2016-10-31 13:14:28 -0700175 final Resources res = mService.mContext.getResources();
Winson Chunga71febe2017-05-22 11:14:22 -0700176 mDefaultMinSize = res.getDimensionPixelSize(
Mady Mellora7f69742017-02-03 11:00:20 -0800177 com.android.internal.R.dimen.default_minimal_size_pip_resizable_task);
Winson Chunga71febe2017-05-22 11:14:22 -0700178 mCurrentMinSize = mDefaultMinSize;
Mady Mellora7f69742017-02-03 11:00:20 -0800179 mDefaultAspectRatio = res.getFloat(
180 com.android.internal.R.dimen.config_pictureInPictureDefaultAspectRatio);
Wale Ogunwale027f4752017-05-12 10:37:16 -0700181 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 Chung655332c2016-10-31 13:14:28 -0700186 mDefaultStackGravity = res.getInteger(
187 com.android.internal.R.integer.config_defaultPictureInPictureGravity);
188 mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics);
Wale Ogunwale027f4752017-05-12 10:37:16 -0700189 mScreenEdgeInsets = screenEdgeInsetsDp == null ? new Point()
190 : new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics),
191 dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics));
Winson Chung2a82fe52017-02-02 14:43:34 -0800192 mMinAspectRatio = res.getFloat(
193 com.android.internal.R.dimen.config_pictureInPictureMinAspectRatio);
194 mMaxAspectRatio = res.getFloat(
195 com.android.internal.R.dimen.config_pictureInPictureMaxAspectRatio);
Winson Chung655332c2016-10-31 13:14:28 -0700196 }
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 Chung2a82fe52017-02-02 14:43:34 -0800206 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 Chunga29eb982016-12-14 12:01:27 -0800210 notifyActionsChanged(mActions);
Winson Chung2a82fe52017-02-02 14:43:34 -0800211 notifyMinimizeChanged(mIsMinimized);
Winson Chung655332c2016-10-31 13:14:28 -0700212 } catch (RemoteException e) {
213 Log.e(TAG, "Failed to register pinned stack listener", e);
214 }
215 }
216
217 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800218 * @return whether the given {@param aspectRatio} is valid.
219 */
220 public boolean isValidPictureInPictureAspectRatio(float aspectRatio) {
Winson Chungbd041962017-03-06 15:07:25 -0800221 return Float.compare(mMinAspectRatio, aspectRatio) <= 0 &&
222 Float.compare(aspectRatio, mMaxAspectRatio) <= 0;
Winson Chung2a82fe52017-02-02 14:43:34 -0800223 }
224
225 /**
Winson Chung84a38342016-11-08 16:15:10 -0800226 * Returns the current bounds (or the default bounds if there are no current bounds) with the
227 * specified aspect ratio.
228 */
Winson Chunga71febe2017-05-22 11:14:22 -0700229 Rect transformBoundsToAspectRatio(Rect stackBounds, float aspectRatio,
230 boolean useCurrentMinEdgeSize) {
Mady Mellora7f69742017-02-03 11:00:20 -0800231 // Save the snap fraction, calculate the aspect ratio based on screen size
Winson Chung84a38342016-11-08 16:15:10 -0800232 final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds,
233 getMovementBounds(stackBounds));
Winson Chunga71febe2017-05-22 11:14:22 -0700234
235 final int minEdgeSize = useCurrentMinEdgeSize ? mCurrentMinSize : mDefaultMinSize;
236 final Size size = mSnapAlgorithm.getSizeForAspectRatio(aspectRatio, minEdgeSize,
237 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
Mady Mellora7f69742017-02-03 11:00:20 -0800238 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 Chung84a38342016-11-08 16:15:10 -0800241 mSnapAlgorithm.applySnapFraction(stackBounds, getMovementBounds(stackBounds), snapFraction);
Winson Chungf1f72f62017-02-14 17:15:48 -0800242 if (mIsMinimized) {
243 applyMinimizedOffset(stackBounds, getMovementBounds(stackBounds));
244 }
Winson Chung84a38342016-11-08 16:15:10 -0800245 return stackBounds;
246 }
247
248 /**
Winson Chung655332c2016-10-31 13:14:28 -0700249 * @return the default bounds to show the PIP when there is no active PIP.
250 */
251 Rect getDefaultBounds() {
Winson Chungef4dc812017-04-11 13:31:44 -0700252 synchronized (mService.mWindowMap) {
253 final Rect insetBounds = new Rect();
254 getInsetBounds(insetBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700255
Winson Chungef4dc812017-04-11 13:31:44 -0700256 final Rect defaultBounds = new Rect();
Winson Chunga71febe2017-05-22 11:14:22 -0700257 final Size size = mSnapAlgorithm.getSizeForAspectRatio(mDefaultAspectRatio,
258 mDefaultMinSize, mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
Winson Chungef4dc812017-04-11 13:31:44 -0700259 Gravity.apply(mDefaultStackGravity, size.getWidth(), size.getHeight(), insetBounds,
260 0, mIsImeShowing ? mImeHeight : 0, defaultBounds);
261 return defaultBounds;
262 }
Winson Chung655332c2016-10-31 13:14:28 -0700263 }
264
265 /**
Winson Chung32c566f2017-04-11 18:31:21 -0700266 * 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 Chung47f2bf62017-02-16 18:58:12 -0800276 * Updates the display info, calculating and returning the new stack and movement bounds in the
277 * new orientation of the device if necessary.
Winson Chung655332c2016-10-31 13:14:28 -0700278 */
Winson Chung19953ca2017-04-11 11:19:23 -0700279 boolean onTaskStackBoundsChanged(Rect targetBounds, Rect outBounds) {
Winson Chungef4dc812017-04-11 13:31:44 -0700280 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 Chung19953ca2017-04-11 11:19:23 -0700301 mDisplayInfo.copyFrom(displayInfo);
Winson Chungef4dc812017-04-11 13:31:44 -0700302
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 Chung14fefc22016-11-02 10:02:29 -0700317 }
Winson Chung655332c2016-10-31 13:14:28 -0700318 }
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 Chung655332c2016-10-31 13:14:28 -0700329 mIsImeShowing = adjustedForIme;
330 mImeHeight = imeHeight;
Winson Chung2a82fe52017-02-02 14:43:34 -0800331 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 Chung655332c2016-10-31 13:14:28 -0700342 }
343 }
344
345 /**
Winson Chungc95ff842017-03-21 10:20:20 -0700346 * @return the current aspect ratio.
347 */
348 float getAspectRatio() {
349 return mAspectRatio;
350 }
351
352 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800353 * Sets the current set of actions.
354 */
355 void setActions(List<RemoteAction> actions) {
356 mActions.clear();
Winson Chungc2baac02017-01-11 13:34:47 -0800357 if (actions != null) {
358 mActions.addAll(actions);
359 }
Winson Chunga29eb982016-12-14 12:01:27 -0800360 notifyActionsChanged(mActions);
361 }
362
363 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800364 * Notifies listeners that the PIP needs to be adjusted for the IME.
Winson Chung655332c2016-10-31 13:14:28 -0700365 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800366 private void notifyImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Winson Chung655332c2016-10-31 13:14:28 -0700367 if (mPinnedStackListener != null) {
368 try {
Winson Chung2a82fe52017-02-02 14:43:34 -0800369 mPinnedStackListener.onImeVisibilityChanged(imeVisible, imeHeight);
Winson Chung655332c2016-10-31 13:14:28 -0700370 } catch (RemoteException e) {
371 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
372 }
373 }
374 }
375
376 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800377 * 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 Chunga29eb982016-12-14 12:01:27 -0800390 * 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 Chung2a82fe52017-02-02 14:43:34 -0800403 * Notifies listeners that the PIP movement bounds have changed.
404 */
405 private void notifyMovementBoundsChanged(boolean fromImeAdjustement) {
Winson Chungef4dc812017-04-11 13:31:44 -0700406 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 Chunga71febe2017-05-22 11:14:22 -0700413 transformBoundsToAspectRatio(normalBounds, mAspectRatio,
414 false /* useCurrentMinEdgeSize */);
Winson Chungef4dc812017-04-11 13:31:44 -0700415 }
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 Chung2a82fe52017-02-02 14:43:34 -0800427 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800428 }
429 }
430 }
431
432 /**
Winson Chung655332c2016-10-31 13:14:28 -0700433 * @return the bounds on the screen that the PIP can be visible in.
434 */
Winson Chung14fefc22016-11-02 10:02:29 -0700435 private void getInsetBounds(Rect outRect) {
Winson Chungef4dc812017-04-11 13:31:44 -0700436 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 Chung655332c2016-10-31 13:14:28 -0700443 }
444
445 /**
Winson Chung55893332017-02-17 17:13:10 -0800446 * @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 Chungef4dc812017-04-11 13:31:44 -0700450 synchronized (mService.mWindowMap) {
451 return getMovementBounds(stackBounds, true /* adjustForIme */);
452 }
Winson Chung55893332017-02-17 17:13:10 -0800453 }
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 Chungef4dc812017-04-11 13:31:44 -0700460 synchronized (mService.mWindowMap) {
461 final Rect movementBounds = new Rect();
462 getInsetBounds(movementBounds);
Winson Chung55893332017-02-17 17:13:10 -0800463
Winson Chungef4dc812017-04-11 13:31:44 -0700464 // 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 Chung55893332017-02-17 17:13:10 -0800469 }
470
471 /**
Winson Chungf1f72f62017-02-14 17:15:48 -0800472 * Applies the minimized offsets to the given stack bounds.
473 */
474 private void applyMinimizedOffset(Rect stackBounds, Rect movementBounds) {
Winson Chungef4dc812017-04-11 13:31:44 -0700475 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 Chungf1f72f62017-02-14 17:15:48 -0800481 }
482
483 /**
Winson Chung655332c2016-10-31 13:14:28 -0700484 * @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 Jaggiad5d2842016-11-01 18:22:53 -0700492 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 Chung655332c2016-10-31 13:14:28 -0700497 pw.println(prefix + " mIsImeShowing=" + mIsImeShowing);
Winson Chungfa7053782016-11-08 15:45:10 -0800498 pw.println(prefix + " mIsMinimized=" + mIsMinimized);
Winson Chunga29eb982016-12-14 12:01:27 -0800499 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 Chung655332c2016-10-31 13:14:28 -0700510 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700511
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 Chung655332c2016-10-31 13:14:28 -0700519}