blob: 5f41187c1cd99ba995ad5fbbb2097bd3c55d72e4 [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;
97 private Size mDefaultStackSize;
98 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
Winson Chung655332c2016-10-31 13:14:28 -0700104 // Temp vars for calculation
105 private final DisplayMetrics mTmpMetrics = new DisplayMetrics();
106 private final Rect mTmpInsets = new Rect();
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700107 private final Rect mTmpRect = new Rect();
Winson Chungf1f72f62017-02-14 17:15:48 -0800108 private final Point mTmpDisplaySize = new Point();
Winson Chung655332c2016-10-31 13:14:28 -0700109
110 /**
111 * The callback object passed to listeners for them to notify the controller of state changes.
112 */
113 private class PinnedStackControllerCallback extends IPinnedStackController.Stub {
114
115 @Override
Winson Chungfa7053782016-11-08 15:45:10 -0800116 public void setIsMinimized(final boolean isMinimized) {
117 mHandler.post(() -> {
118 mIsMinimized = isMinimized;
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800119 mSnapAlgorithm.setMinimized(isMinimized);
Winson Chungfa7053782016-11-08 15:45:10 -0800120 });
121 }
Winson Chung655332c2016-10-31 13:14:28 -0700122 }
123
124 /**
125 * Handler for the case where the listener dies.
126 */
127 private class PinnedStackListenerDeathHandler implements IBinder.DeathRecipient {
128
129 @Override
130 public void binderDied() {
131 // Clean up the state if the listener dies
Winson Chung655332c2016-10-31 13:14:28 -0700132 mPinnedStackListener = null;
133 }
134 }
135
136 PinnedStackController(WindowManagerService service, DisplayContent displayContent) {
137 mService = service;
138 mDisplayContent = displayContent;
139 mSnapAlgorithm = new PipSnapAlgorithm(service.mContext);
Winson Chung14fefc22016-11-02 10:02:29 -0700140 mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo());
Winson Chung655332c2016-10-31 13:14:28 -0700141 reloadResources();
142 }
143
144 void onConfigurationChanged() {
145 reloadResources();
Winson Chung2a82fe52017-02-02 14:43:34 -0800146 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chung655332c2016-10-31 13:14:28 -0700147 }
148
149 /**
150 * Reloads all the resources for the current configuration.
151 */
152 void reloadResources() {
153 final Resources res = mService.mContext.getResources();
154 final Size defaultSizeDp = Size.parseSize(res.getString(
155 com.android.internal.R.string.config_defaultPictureInPictureSize));
156 final Size screenEdgeInsetsDp = Size.parseSize(res.getString(
157 com.android.internal.R.string.config_defaultPictureInPictureScreenEdgeInsets));
158 mDefaultStackGravity = res.getInteger(
159 com.android.internal.R.integer.config_defaultPictureInPictureGravity);
160 mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics);
161 mDefaultStackSize = new Size(dpToPx(defaultSizeDp.getWidth(), mTmpMetrics),
162 dpToPx(defaultSizeDp.getHeight(), mTmpMetrics));
163 mScreenEdgeInsets = new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics),
164 dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics));
Winson Chung2a82fe52017-02-02 14:43:34 -0800165 mMinAspectRatio = res.getFloat(
166 com.android.internal.R.dimen.config_pictureInPictureMinAspectRatio);
167 mMaxAspectRatio = res.getFloat(
168 com.android.internal.R.dimen.config_pictureInPictureMaxAspectRatio);
Winson Chung655332c2016-10-31 13:14:28 -0700169 }
170
171 /**
172 * Registers a pinned stack listener.
173 */
174 void registerPinnedStackListener(IPinnedStackListener listener) {
175 try {
176 listener.asBinder().linkToDeath(mPinnedStackListenerDeathHandler, 0);
177 listener.onListenerRegistered(mCallbacks);
178 mPinnedStackListener = listener;
Winson Chung2a82fe52017-02-02 14:43:34 -0800179 notifyImeVisibilityChanged(mIsImeShowing, mImeHeight);
180 // The movement bounds notification needs to be sent before the minimized state, since
181 // SystemUI may use the bounds to retore the minimized position
182 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chunga29eb982016-12-14 12:01:27 -0800183 notifyActionsChanged(mActions);
Winson Chung2a82fe52017-02-02 14:43:34 -0800184 notifyMinimizeChanged(mIsMinimized);
Winson Chung655332c2016-10-31 13:14:28 -0700185 } catch (RemoteException e) {
186 Log.e(TAG, "Failed to register pinned stack listener", e);
187 }
188 }
189
190 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800191 * @return whether the given {@param aspectRatio} is valid.
192 */
193 public boolean isValidPictureInPictureAspectRatio(float aspectRatio) {
194 return mMinAspectRatio <= aspectRatio && aspectRatio <= mMaxAspectRatio;
195 }
196
197 /**
Winson Chung84a38342016-11-08 16:15:10 -0800198 * Returns the current bounds (or the default bounds if there are no current bounds) with the
199 * specified aspect ratio.
200 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800201 Rect transformBoundsToAspectRatio(Rect stackBounds, float aspectRatio) {
Winson Chung84a38342016-11-08 16:15:10 -0800202 // Save the snap fraction, calculate the aspect ratio based on the current bounds
203 final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds,
204 getMovementBounds(stackBounds));
205 final float radius = PointF.length(stackBounds.width(), stackBounds.height());
206 final int height = (int) Math.round(Math.sqrt((radius * radius) /
207 (aspectRatio * aspectRatio + 1)));
208 final int width = Math.round(height * aspectRatio);
209 final int left = (int) (stackBounds.centerX() - width / 2f);
210 final int top = (int) (stackBounds.centerY() - height / 2f);
211 stackBounds.set(left, top, left + width, top + height);
212 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 /**
Winson Chung655332c2016-10-31 13:14:28 -0700220 * @return the default bounds to show the PIP when there is no active PIP.
221 */
222 Rect getDefaultBounds() {
Winson Chung655332c2016-10-31 13:14:28 -0700223 final Rect insetBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700224 getInsetBounds(insetBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700225
226 final Rect defaultBounds = new Rect();
227 Gravity.apply(mDefaultStackGravity, mDefaultStackSize.getWidth(),
228 mDefaultStackSize.getHeight(), insetBounds, 0, 0, defaultBounds);
229 return defaultBounds;
230 }
231
232 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800233 * @param preChangeTargetBounds The final bounds of the stack if it is currently animating
Winson Chung114aeea2017-01-09 16:08:07 -0800234 * @return the repositioned PIP bounds given it's pre-change bounds, and the new display
235 * content.
Winson Chung655332c2016-10-31 13:14:28 -0700236 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800237 Rect onDisplayChanged(Rect preChangeStackBounds, Rect preChangeTargetBounds,
238 DisplayContent displayContent) {
239 final Rect postChangeStackBounds = new Rect(preChangeTargetBounds);
Winson Chung114aeea2017-01-09 16:08:07 -0800240 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
Winson Chung14fefc22016-11-02 10:02:29 -0700241 if (!mDisplayInfo.equals(displayInfo)) {
242 // Calculate the snap fraction of the current stack along the old movement bounds, and
243 // then update the stack bounds to the same fraction along the rotated movement bounds.
244 final Rect preChangeMovementBounds = getMovementBounds(preChangeStackBounds);
245 final float snapFraction = mSnapAlgorithm.getSnapFraction(preChangeStackBounds,
246 preChangeMovementBounds);
247 mDisplayInfo.copyFrom(displayInfo);
248
249 final Rect postChangeMovementBounds = getMovementBounds(preChangeStackBounds,
250 false /* adjustForIme */);
251 mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds,
252 snapFraction);
Winson Chungd5a01592016-11-11 16:25:04 -0800253 if (mIsMinimized) {
Winson Chungf1f72f62017-02-14 17:15:48 -0800254 applyMinimizedOffset(postChangeStackBounds, postChangeMovementBounds);
Winson Chungd5a01592016-11-11 16:25:04 -0800255 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800256 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chung14fefc22016-11-02 10:02:29 -0700257 }
258 return postChangeStackBounds;
Winson Chung655332c2016-10-31 13:14:28 -0700259 }
260
261 /**
262 * Sets the Ime state and height.
263 */
264 void setAdjustedForIme(boolean adjustedForIme, int imeHeight) {
265 // Return early if there is no state change
266 if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) {
267 return;
268 }
269
Winson Chung655332c2016-10-31 13:14:28 -0700270 mIsImeShowing = adjustedForIme;
271 mImeHeight = imeHeight;
Winson Chung2a82fe52017-02-02 14:43:34 -0800272 notifyImeVisibilityChanged(adjustedForIme, imeHeight);
273 notifyMovementBoundsChanged(true /* fromImeAdjustment */);
274 }
275
276 /**
277 * Sets the current aspect ratio.
278 */
279 void setAspectRatio(float aspectRatio) {
280 if (Float.compare(mAspectRatio, aspectRatio) != 0) {
281 mAspectRatio = aspectRatio;
282 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chung655332c2016-10-31 13:14:28 -0700283 }
284 }
285
286 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800287 * Sets the current set of actions.
288 */
289 void setActions(List<RemoteAction> actions) {
290 mActions.clear();
Winson Chungc2baac02017-01-11 13:34:47 -0800291 if (actions != null) {
292 mActions.addAll(actions);
293 }
Winson Chunga29eb982016-12-14 12:01:27 -0800294 notifyActionsChanged(mActions);
295 }
296
297 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800298 * Notifies listeners that the PIP needs to be adjusted for the IME.
Winson Chung655332c2016-10-31 13:14:28 -0700299 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800300 private void notifyImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Winson Chung655332c2016-10-31 13:14:28 -0700301 if (mPinnedStackListener != null) {
302 try {
Winson Chung2a82fe52017-02-02 14:43:34 -0800303 mPinnedStackListener.onImeVisibilityChanged(imeVisible, imeHeight);
Winson Chung655332c2016-10-31 13:14:28 -0700304 } catch (RemoteException e) {
305 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
306 }
307 }
308 }
309
310 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800311 * Notifies listeners that the PIP minimized state has changed.
312 */
313 private void notifyMinimizeChanged(boolean isMinimized) {
314 if (mPinnedStackListener != null) {
315 try {
316 mPinnedStackListener.onMinimizedStateChanged(isMinimized);
317 } catch (RemoteException e) {
318 Slog.e(TAG_WM, "Error delivering minimize changed event.", e);
319 }
320 }
321 }
322
323 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800324 * Notifies listeners that the PIP actions have changed.
325 */
326 private void notifyActionsChanged(List<RemoteAction> actions) {
327 if (mPinnedStackListener != null) {
328 try {
329 mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions));
330 } catch (RemoteException e) {
331 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
332 }
333 }
334 }
335
336 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800337 * Notifies listeners that the PIP movement bounds have changed.
338 */
339 private void notifyMovementBoundsChanged(boolean fromImeAdjustement) {
340 if (mPinnedStackListener != null) {
341 try {
342 Rect insetBounds = new Rect();
343 getInsetBounds(insetBounds);
344 Rect normalBounds = getDefaultBounds();
345 if (isValidPictureInPictureAspectRatio(mAspectRatio)) {
346 transformBoundsToAspectRatio(normalBounds, mAspectRatio);
347 }
348 mPinnedStackListener.onMovementBoundsChanged(insetBounds, normalBounds,
349 fromImeAdjustement);
350 } catch (RemoteException e) {
351 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
352 }
353 }
354 }
355
356 /**
Winson Chung655332c2016-10-31 13:14:28 -0700357 * @return the bounds on the screen that the PIP can be visible in.
358 */
Winson Chung14fefc22016-11-02 10:02:29 -0700359 private void getInsetBounds(Rect outRect) {
360 mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth,
361 mDisplayInfo.logicalHeight, mTmpInsets);
362 outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y,
363 mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x,
364 mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y);
Winson Chung655332c2016-10-31 13:14:28 -0700365 }
366
367 /**
Winson Chung55893332017-02-17 17:13:10 -0800368 * @return the movement bounds for the given {@param stackBounds} and the current state of the
369 * controller.
370 */
371 private Rect getMovementBounds(Rect stackBounds) {
372 return getMovementBounds(stackBounds, true /* adjustForIme */);
373 }
374
375 /**
376 * @return the movement bounds for the given {@param stackBounds} and the current state of the
377 * controller.
378 */
379 private Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
380 final Rect movementBounds = new Rect();
381 getInsetBounds(movementBounds);
382
383 // Apply the movement bounds adjustments based on the current state
384 mSnapAlgorithm.getMovementBounds(stackBounds, movementBounds, movementBounds,
385 (adjustForIme && mIsImeShowing) ? mImeHeight : 0);
386 return movementBounds;
387 }
388
389 /**
Winson Chungf1f72f62017-02-14 17:15:48 -0800390 * Applies the minimized offsets to the given stack bounds.
391 */
392 private void applyMinimizedOffset(Rect stackBounds, Rect movementBounds) {
393 mTmpDisplaySize.set(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
394 mService.getStableInsetsLocked(mDisplayContent.getDisplayId(), mStableInsets);
395 mSnapAlgorithm.applyMinimizedOffset(stackBounds, movementBounds, mTmpDisplaySize,
396 mStableInsets);
397 }
398
399 /**
Winson Chung655332c2016-10-31 13:14:28 -0700400 * @return the pixels for a given dp value.
401 */
402 private int dpToPx(float dpValue, DisplayMetrics dm) {
403 return (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, dpValue, dm);
404 }
405
406 void dump(String prefix, PrintWriter pw) {
407 pw.println(prefix + "PinnedStackController");
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700408 pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw);
409 pw.println();
410 mService.getStackBounds(PINNED_STACK_ID, mTmpRect);
411 pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw);
412 pw.println();
Winson Chung655332c2016-10-31 13:14:28 -0700413 pw.println(prefix + " mIsImeShowing=" + mIsImeShowing);
Winson Chungfa7053782016-11-08 15:45:10 -0800414 pw.println(prefix + " mIsMinimized=" + mIsMinimized);
Winson Chunga29eb982016-12-14 12:01:27 -0800415 if (mActions.isEmpty()) {
416 pw.println(prefix + " mActions=[]");
417 } else {
418 pw.println(prefix + " mActions=[");
419 for (int i = 0; i < mActions.size(); i++) {
420 RemoteAction action = mActions.get(i);
421 pw.print(prefix + " Action[" + i + "]: ");
422 action.dump("", pw);
423 }
424 pw.println(prefix + " ]");
425 }
Winson Chung655332c2016-10-31 13:14:28 -0700426 }
427}