blob: 9558068972cbc1a725d5d3c3473d039f14b6e69a [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;
Winson Chung84a38342016-11-08 16:15:10 -080029import android.graphics.PointF;
Winson Chung655332c2016-10-31 13:14:28 -070030import android.graphics.Rect;
31import android.os.Handler;
32import android.os.IBinder;
33import android.os.RemoteException;
34import android.util.DisplayMetrics;
35import android.util.Log;
36import android.util.Size;
37import android.util.Slog;
38import android.util.TypedValue;
Winson Chung14fefc22016-11-02 10:02:29 -070039import android.view.DisplayInfo;
Winson Chung655332c2016-10-31 13:14:28 -070040import android.view.Gravity;
41import android.view.IPinnedStackController;
42import android.view.IPinnedStackListener;
43
Winson Chung655332c2016-10-31 13:14:28 -070044import com.android.internal.policy.PipSnapAlgorithm;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070045import com.android.server.UiThread;
Winson Chung655332c2016-10-31 13:14:28 -070046
47import java.io.PrintWriter;
Winson Chunga29eb982016-12-14 12:01:27 -080048import java.util.ArrayList;
49import java.util.List;
Winson Chung655332c2016-10-31 13:14:28 -070050
51/**
Winson Chung2a82fe52017-02-02 14:43:34 -080052 * Holds the common state of the pinned stack between the system and SystemUI. If SystemUI ever
53 * needs to be restarted, it will be notified with the last known state.
54 *
55 * Changes to the pinned stack also flow through this controller, and generally, the system only
56 * changes the pinned stack bounds through this controller in two ways:
57 *
58 * 1) When first entering PiP: the controller returns the valid bounds given, taking aspect ratio
59 * and IME state into account.
60 * 2) When rotating the device: the controller calculates the new bounds in the new orientation,
61 * taking the minimized and IME state into account. In this case, we currently ignore the
62 * SystemUI adjustments (ie. expanded for menu, interaction, etc).
63 *
64 * Other changes in the system, including adjustment of IME, configuration change, and more are
65 * handled by SystemUI (similar to the docked stack divider).
Winson Chung655332c2016-10-31 13:14:28 -070066 */
67class PinnedStackController {
68
69 private static final String TAG = TAG_WITH_CLASS_NAME ? "PinnedStackController" : TAG_WM;
70
71 private final WindowManagerService mService;
72 private final DisplayContent mDisplayContent;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070073 private final Handler mHandler = UiThread.getHandler();
Winson Chung655332c2016-10-31 13:14:28 -070074
75 private IPinnedStackListener mPinnedStackListener;
76 private final PinnedStackListenerDeathHandler mPinnedStackListenerDeathHandler =
77 new PinnedStackListenerDeathHandler();
78
79 private final PinnedStackControllerCallback mCallbacks = new PinnedStackControllerCallback();
80 private final PipSnapAlgorithm mSnapAlgorithm;
Winson Chung655332c2016-10-31 13:14:28 -070081
82 // States that affect how the PIP can be manipulated
Winson Chungfa7053782016-11-08 15:45:10 -080083 private boolean mIsMinimized;
Winson Chung655332c2016-10-31 13:14:28 -070084 private boolean mIsImeShowing;
85 private int mImeHeight;
Winson Chung655332c2016-10-31 13:14:28 -070086
Winson Chung2a82fe52017-02-02 14:43:34 -080087 // The set of actions and aspect-ratio for the that are currently allowed on the PiP activity
Winson Chunga29eb982016-12-14 12:01:27 -080088 private ArrayList<RemoteAction> mActions = new ArrayList<>();
Winson Chung2a82fe52017-02-02 14:43:34 -080089 private float mAspectRatio = -1f;
Winson Chunga29eb982016-12-14 12:01:27 -080090
Winson Chung14fefc22016-11-02 10:02:29 -070091 // Used to calculate stack bounds across rotations
92 private final DisplayInfo mDisplayInfo = new DisplayInfo();
Winson Chung114aeea2017-01-09 16:08:07 -080093 private final Rect mStableInsets = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -070094
Winson Chung655332c2016-10-31 13:14:28 -070095 // The size and position information that describes where the pinned stack will go by default.
96 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;
99
Winson Chung2a82fe52017-02-02 14:43:34 -0800100 // The aspect ratio bounds of the PIP.
101 private float mMinAspectRatio;
102 private float mMaxAspectRatio;
103
Mady Mellora7f69742017-02-03 11:00:20 -0800104 // The minimum edge size of the normal PiP bounds.
105 private int mMinSize;
106
Winson Chung655332c2016-10-31 13:14:28 -0700107 // Temp vars for calculation
108 private final DisplayMetrics mTmpMetrics = new DisplayMetrics();
109 private final Rect mTmpInsets = new Rect();
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700110 private final Rect mTmpRect = new Rect();
Winson Chungf1f72f62017-02-14 17:15:48 -0800111 private final Point mTmpDisplaySize = new Point();
Winson Chung655332c2016-10-31 13:14:28 -0700112
113 /**
114 * The callback object passed to listeners for them to notify the controller of state changes.
115 */
116 private class PinnedStackControllerCallback extends IPinnedStackController.Stub {
117
118 @Override
Winson Chungfa7053782016-11-08 15:45:10 -0800119 public void setIsMinimized(final boolean isMinimized) {
120 mHandler.post(() -> {
121 mIsMinimized = isMinimized;
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800122 mSnapAlgorithm.setMinimized(isMinimized);
Winson Chungfa7053782016-11-08 15:45:10 -0800123 });
124 }
Winson Chung655332c2016-10-31 13:14:28 -0700125 }
126
127 /**
128 * Handler for the case where the listener dies.
129 */
130 private class PinnedStackListenerDeathHandler implements IBinder.DeathRecipient {
131
132 @Override
133 public void binderDied() {
134 // Clean up the state if the listener dies
Winson Chung655332c2016-10-31 13:14:28 -0700135 mPinnedStackListener = null;
136 }
137 }
138
139 PinnedStackController(WindowManagerService service, DisplayContent displayContent) {
140 mService = service;
141 mDisplayContent = displayContent;
142 mSnapAlgorithm = new PipSnapAlgorithm(service.mContext);
Winson Chung14fefc22016-11-02 10:02:29 -0700143 mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo());
Winson Chung655332c2016-10-31 13:14:28 -0700144 reloadResources();
145 }
146
147 void onConfigurationChanged() {
148 reloadResources();
Winson Chung2a82fe52017-02-02 14:43:34 -0800149 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chung655332c2016-10-31 13:14:28 -0700150 }
151
152 /**
153 * Reloads all the resources for the current configuration.
154 */
155 void reloadResources() {
156 final Resources res = mService.mContext.getResources();
Mady Mellora7f69742017-02-03 11:00:20 -0800157 mMinSize = res.getDimensionPixelSize(
158 com.android.internal.R.dimen.default_minimal_size_pip_resizable_task);
159 mDefaultAspectRatio = res.getFloat(
160 com.android.internal.R.dimen.config_pictureInPictureDefaultAspectRatio);
Winson Chung655332c2016-10-31 13:14:28 -0700161 final Size screenEdgeInsetsDp = Size.parseSize(res.getString(
162 com.android.internal.R.string.config_defaultPictureInPictureScreenEdgeInsets));
163 mDefaultStackGravity = res.getInteger(
164 com.android.internal.R.integer.config_defaultPictureInPictureGravity);
165 mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics);
Winson Chung655332c2016-10-31 13:14:28 -0700166 mScreenEdgeInsets = new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics),
167 dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics));
Winson Chung2a82fe52017-02-02 14:43:34 -0800168 mMinAspectRatio = res.getFloat(
169 com.android.internal.R.dimen.config_pictureInPictureMinAspectRatio);
170 mMaxAspectRatio = res.getFloat(
171 com.android.internal.R.dimen.config_pictureInPictureMaxAspectRatio);
Winson Chung655332c2016-10-31 13:14:28 -0700172 }
173
174 /**
175 * Registers a pinned stack listener.
176 */
177 void registerPinnedStackListener(IPinnedStackListener listener) {
178 try {
179 listener.asBinder().linkToDeath(mPinnedStackListenerDeathHandler, 0);
180 listener.onListenerRegistered(mCallbacks);
181 mPinnedStackListener = listener;
Winson Chung2a82fe52017-02-02 14:43:34 -0800182 notifyImeVisibilityChanged(mIsImeShowing, mImeHeight);
183 // The movement bounds notification needs to be sent before the minimized state, since
184 // SystemUI may use the bounds to retore the minimized position
185 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chunga29eb982016-12-14 12:01:27 -0800186 notifyActionsChanged(mActions);
Winson Chung2a82fe52017-02-02 14:43:34 -0800187 notifyMinimizeChanged(mIsMinimized);
Winson Chung655332c2016-10-31 13:14:28 -0700188 } catch (RemoteException e) {
189 Log.e(TAG, "Failed to register pinned stack listener", e);
190 }
191 }
192
193 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800194 * @return whether the given {@param aspectRatio} is valid.
195 */
196 public boolean isValidPictureInPictureAspectRatio(float aspectRatio) {
197 return mMinAspectRatio <= aspectRatio && aspectRatio <= mMaxAspectRatio;
198 }
199
200 /**
Winson Chung84a38342016-11-08 16:15:10 -0800201 * Returns the current bounds (or the default bounds if there are no current bounds) with the
202 * specified aspect ratio.
203 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800204 Rect transformBoundsToAspectRatio(Rect stackBounds, float aspectRatio) {
Mady Mellora7f69742017-02-03 11:00:20 -0800205 // Save the snap fraction, calculate the aspect ratio based on screen size
Winson Chung84a38342016-11-08 16:15:10 -0800206 final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds,
207 getMovementBounds(stackBounds));
Mady Mellora7f69742017-02-03 11:00:20 -0800208 final Size size = getSize(aspectRatio);
209 final int left = (int) (stackBounds.centerX() - size.getWidth() / 2f);
210 final int top = (int) (stackBounds.centerY() - size.getHeight() / 2f);
211 stackBounds.set(left, top, left + size.getWidth(), top + size.getHeight());
Winson Chung84a38342016-11-08 16:15:10 -0800212 mSnapAlgorithm.applySnapFraction(stackBounds, getMovementBounds(stackBounds), snapFraction);
Winson Chungf1f72f62017-02-14 17:15:48 -0800213 if (mIsMinimized) {
214 applyMinimizedOffset(stackBounds, getMovementBounds(stackBounds));
215 }
Winson Chung84a38342016-11-08 16:15:10 -0800216 return stackBounds;
217 }
218
219 /**
Mady Mellora7f69742017-02-03 11:00:20 -0800220 * @return the size of the PIP based on the given {@param aspectRatio}.
221 */
222 Size getSize(float aspectRatio) {
223 return mSnapAlgorithm.getSizeForAspectRatio(aspectRatio, mMinSize,
224 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
225 }
226
227 /**
Winson Chung655332c2016-10-31 13:14:28 -0700228 * @return the default bounds to show the PIP when there is no active PIP.
229 */
230 Rect getDefaultBounds() {
Winson Chung655332c2016-10-31 13:14:28 -0700231 final Rect insetBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700232 getInsetBounds(insetBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700233
234 final Rect defaultBounds = new Rect();
Mady Mellora7f69742017-02-03 11:00:20 -0800235 final Size size = getSize(mDefaultAspectRatio);
236 Gravity.apply(mDefaultStackGravity, size.getWidth(), size.getHeight(), insetBounds, 0, 0,
237 defaultBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700238 return defaultBounds;
239 }
240
241 /**
Winson Chung47f2bf62017-02-16 18:58:12 -0800242 * Updates the display info, calculating and returning the new stack and movement bounds in the
243 * new orientation of the device if necessary.
Winson Chung655332c2016-10-31 13:14:28 -0700244 */
Winson Chung47f2bf62017-02-16 18:58:12 -0800245 void onTaskStackBoundsChanged(Rect targetBounds, Rect outBounds) {
246 final DisplayInfo displayInfo = mDisplayContent.getDisplayInfo();
247 if (mDisplayInfo.equals(displayInfo)) {
248 return;
Winson Chung14fefc22016-11-02 10:02:29 -0700249 }
Winson Chung47f2bf62017-02-16 18:58:12 -0800250
251 mTmpRect.set(targetBounds);
252 final Rect postChangeStackBounds = mTmpRect;
253
254 // Calculate the snap fraction of the current stack along the old movement bounds
255 final Rect preChangeMovementBounds = getMovementBounds(postChangeStackBounds);
256 final float snapFraction = mSnapAlgorithm.getSnapFraction(postChangeStackBounds,
257 preChangeMovementBounds);
258 mDisplayInfo.copyFrom(displayInfo);
259
260 // Calculate the stack bounds in the new orientation to the same same fraction along the
261 // rotated movement bounds.
262 final Rect postChangeMovementBounds = getMovementBounds(postChangeStackBounds,
263 false /* adjustForIme */);
264 mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds,
265 snapFraction);
266 if (mIsMinimized) {
267 applyMinimizedOffset(postChangeStackBounds, postChangeMovementBounds);
268 }
269
270 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
271
272 outBounds.set(postChangeStackBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700273 }
274
275 /**
276 * Sets the Ime state and height.
277 */
278 void setAdjustedForIme(boolean adjustedForIme, int imeHeight) {
279 // Return early if there is no state change
280 if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) {
281 return;
282 }
283
Winson Chung655332c2016-10-31 13:14:28 -0700284 mIsImeShowing = adjustedForIme;
285 mImeHeight = imeHeight;
Winson Chung2a82fe52017-02-02 14:43:34 -0800286 notifyImeVisibilityChanged(adjustedForIme, imeHeight);
287 notifyMovementBoundsChanged(true /* fromImeAdjustment */);
288 }
289
290 /**
291 * Sets the current aspect ratio.
292 */
293 void setAspectRatio(float aspectRatio) {
294 if (Float.compare(mAspectRatio, aspectRatio) != 0) {
295 mAspectRatio = aspectRatio;
296 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chung655332c2016-10-31 13:14:28 -0700297 }
298 }
299
300 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800301 * Sets the current set of actions.
302 */
303 void setActions(List<RemoteAction> actions) {
304 mActions.clear();
Winson Chungc2baac02017-01-11 13:34:47 -0800305 if (actions != null) {
306 mActions.addAll(actions);
307 }
Winson Chunga29eb982016-12-14 12:01:27 -0800308 notifyActionsChanged(mActions);
309 }
310
311 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800312 * Notifies listeners that the PIP needs to be adjusted for the IME.
Winson Chung655332c2016-10-31 13:14:28 -0700313 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800314 private void notifyImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Winson Chung655332c2016-10-31 13:14:28 -0700315 if (mPinnedStackListener != null) {
316 try {
Winson Chung2a82fe52017-02-02 14:43:34 -0800317 mPinnedStackListener.onImeVisibilityChanged(imeVisible, imeHeight);
Winson Chung655332c2016-10-31 13:14:28 -0700318 } catch (RemoteException e) {
319 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
320 }
321 }
322 }
323
324 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800325 * Notifies listeners that the PIP minimized state has changed.
326 */
327 private void notifyMinimizeChanged(boolean isMinimized) {
328 if (mPinnedStackListener != null) {
329 try {
330 mPinnedStackListener.onMinimizedStateChanged(isMinimized);
331 } catch (RemoteException e) {
332 Slog.e(TAG_WM, "Error delivering minimize changed event.", e);
333 }
334 }
335 }
336
337 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800338 * Notifies listeners that the PIP actions have changed.
339 */
340 private void notifyActionsChanged(List<RemoteAction> actions) {
341 if (mPinnedStackListener != null) {
342 try {
343 mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions));
344 } catch (RemoteException e) {
345 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
346 }
347 }
348 }
349
350 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800351 * Notifies listeners that the PIP movement bounds have changed.
352 */
353 private void notifyMovementBoundsChanged(boolean fromImeAdjustement) {
354 if (mPinnedStackListener != null) {
355 try {
356 Rect insetBounds = new Rect();
357 getInsetBounds(insetBounds);
358 Rect normalBounds = getDefaultBounds();
359 if (isValidPictureInPictureAspectRatio(mAspectRatio)) {
360 transformBoundsToAspectRatio(normalBounds, mAspectRatio);
361 }
362 mPinnedStackListener.onMovementBoundsChanged(insetBounds, normalBounds,
363 fromImeAdjustement);
364 } catch (RemoteException e) {
365 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
366 }
367 }
368 }
369
370 /**
Winson Chung655332c2016-10-31 13:14:28 -0700371 * @return the bounds on the screen that the PIP can be visible in.
372 */
Winson Chung14fefc22016-11-02 10:02:29 -0700373 private void getInsetBounds(Rect outRect) {
374 mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth,
375 mDisplayInfo.logicalHeight, mTmpInsets);
376 outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y,
377 mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x,
378 mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y);
Winson Chung655332c2016-10-31 13:14:28 -0700379 }
380
381 /**
Winson Chung55893332017-02-17 17:13:10 -0800382 * @return the movement bounds for the given {@param stackBounds} and the current state of the
383 * controller.
384 */
385 private Rect getMovementBounds(Rect stackBounds) {
386 return getMovementBounds(stackBounds, true /* adjustForIme */);
387 }
388
389 /**
390 * @return the movement bounds for the given {@param stackBounds} and the current state of the
391 * controller.
392 */
393 private Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
394 final Rect movementBounds = new Rect();
395 getInsetBounds(movementBounds);
396
397 // Apply the movement bounds adjustments based on the current state
398 mSnapAlgorithm.getMovementBounds(stackBounds, movementBounds, movementBounds,
399 (adjustForIme && mIsImeShowing) ? mImeHeight : 0);
400 return movementBounds;
401 }
402
403 /**
Winson Chungf1f72f62017-02-14 17:15:48 -0800404 * Applies the minimized offsets to the given stack bounds.
405 */
406 private void applyMinimizedOffset(Rect stackBounds, Rect movementBounds) {
407 mTmpDisplaySize.set(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
408 mService.getStableInsetsLocked(mDisplayContent.getDisplayId(), mStableInsets);
409 mSnapAlgorithm.applyMinimizedOffset(stackBounds, movementBounds, mTmpDisplaySize,
410 mStableInsets);
411 }
412
413 /**
Winson Chung655332c2016-10-31 13:14:28 -0700414 * @return the pixels for a given dp value.
415 */
416 private int dpToPx(float dpValue, DisplayMetrics dm) {
417 return (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, dpValue, dm);
418 }
419
420 void dump(String prefix, PrintWriter pw) {
421 pw.println(prefix + "PinnedStackController");
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700422 pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw);
423 pw.println();
424 mService.getStackBounds(PINNED_STACK_ID, mTmpRect);
425 pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw);
426 pw.println();
Winson Chung655332c2016-10-31 13:14:28 -0700427 pw.println(prefix + " mIsImeShowing=" + mIsImeShowing);
Winson Chungfa7053782016-11-08 15:45:10 -0800428 pw.println(prefix + " mIsMinimized=" + mIsMinimized);
Winson Chunga29eb982016-12-14 12:01:27 -0800429 if (mActions.isEmpty()) {
430 pw.println(prefix + " mActions=[]");
431 } else {
432 pw.println(prefix + " mActions=[");
433 for (int i = 0; i < mActions.size(); i++) {
434 RemoteAction action = mActions.get(i);
435 pw.print(prefix + " Action[" + i + "]: ");
436 action.dump("", pw);
437 }
438 pw.println(prefix + " ]");
439 }
Winson Chung655332c2016-10-31 13:14:28 -0700440 }
441}