blob: 2a20a70b6ee9e1203f0ab5ba0e2c72df0e6af3f2 [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 Chung47f2bf62017-02-16 18:58:12 -0800233 * Updates the display info, calculating and returning the new stack and movement bounds in the
234 * new orientation of the device if necessary.
Winson Chung655332c2016-10-31 13:14:28 -0700235 */
Winson Chung47f2bf62017-02-16 18:58:12 -0800236 void onTaskStackBoundsChanged(Rect targetBounds, Rect outBounds) {
237 final DisplayInfo displayInfo = mDisplayContent.getDisplayInfo();
238 if (mDisplayInfo.equals(displayInfo)) {
239 return;
Winson Chung14fefc22016-11-02 10:02:29 -0700240 }
Winson Chung47f2bf62017-02-16 18:58:12 -0800241
242 mTmpRect.set(targetBounds);
243 final Rect postChangeStackBounds = mTmpRect;
244
245 // Calculate the snap fraction of the current stack along the old movement bounds
246 final Rect preChangeMovementBounds = getMovementBounds(postChangeStackBounds);
247 final float snapFraction = mSnapAlgorithm.getSnapFraction(postChangeStackBounds,
248 preChangeMovementBounds);
249 mDisplayInfo.copyFrom(displayInfo);
250
251 // Calculate the stack bounds in the new orientation to the same same fraction along the
252 // rotated movement bounds.
253 final Rect postChangeMovementBounds = getMovementBounds(postChangeStackBounds,
254 false /* adjustForIme */);
255 mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds,
256 snapFraction);
257 if (mIsMinimized) {
258 applyMinimizedOffset(postChangeStackBounds, postChangeMovementBounds);
259 }
260
261 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
262
263 outBounds.set(postChangeStackBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700264 }
265
266 /**
267 * Sets the Ime state and height.
268 */
269 void setAdjustedForIme(boolean adjustedForIme, int imeHeight) {
270 // Return early if there is no state change
271 if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) {
272 return;
273 }
274
Winson Chung655332c2016-10-31 13:14:28 -0700275 mIsImeShowing = adjustedForIme;
276 mImeHeight = imeHeight;
Winson Chung2a82fe52017-02-02 14:43:34 -0800277 notifyImeVisibilityChanged(adjustedForIme, imeHeight);
278 notifyMovementBoundsChanged(true /* fromImeAdjustment */);
279 }
280
281 /**
282 * Sets the current aspect ratio.
283 */
284 void setAspectRatio(float aspectRatio) {
285 if (Float.compare(mAspectRatio, aspectRatio) != 0) {
286 mAspectRatio = aspectRatio;
287 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chung655332c2016-10-31 13:14:28 -0700288 }
289 }
290
291 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800292 * Sets the current set of actions.
293 */
294 void setActions(List<RemoteAction> actions) {
295 mActions.clear();
Winson Chungc2baac02017-01-11 13:34:47 -0800296 if (actions != null) {
297 mActions.addAll(actions);
298 }
Winson Chunga29eb982016-12-14 12:01:27 -0800299 notifyActionsChanged(mActions);
300 }
301
302 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800303 * Notifies listeners that the PIP needs to be adjusted for the IME.
Winson Chung655332c2016-10-31 13:14:28 -0700304 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800305 private void notifyImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Winson Chung655332c2016-10-31 13:14:28 -0700306 if (mPinnedStackListener != null) {
307 try {
Winson Chung2a82fe52017-02-02 14:43:34 -0800308 mPinnedStackListener.onImeVisibilityChanged(imeVisible, imeHeight);
Winson Chung655332c2016-10-31 13:14:28 -0700309 } catch (RemoteException e) {
310 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
311 }
312 }
313 }
314
315 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800316 * Notifies listeners that the PIP minimized state has changed.
317 */
318 private void notifyMinimizeChanged(boolean isMinimized) {
319 if (mPinnedStackListener != null) {
320 try {
321 mPinnedStackListener.onMinimizedStateChanged(isMinimized);
322 } catch (RemoteException e) {
323 Slog.e(TAG_WM, "Error delivering minimize changed event.", e);
324 }
325 }
326 }
327
328 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800329 * Notifies listeners that the PIP actions have changed.
330 */
331 private void notifyActionsChanged(List<RemoteAction> actions) {
332 if (mPinnedStackListener != null) {
333 try {
334 mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions));
335 } catch (RemoteException e) {
336 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
337 }
338 }
339 }
340
341 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800342 * Notifies listeners that the PIP movement bounds have changed.
343 */
344 private void notifyMovementBoundsChanged(boolean fromImeAdjustement) {
345 if (mPinnedStackListener != null) {
346 try {
347 Rect insetBounds = new Rect();
348 getInsetBounds(insetBounds);
349 Rect normalBounds = getDefaultBounds();
350 if (isValidPictureInPictureAspectRatio(mAspectRatio)) {
351 transformBoundsToAspectRatio(normalBounds, mAspectRatio);
352 }
353 mPinnedStackListener.onMovementBoundsChanged(insetBounds, normalBounds,
354 fromImeAdjustement);
355 } catch (RemoteException e) {
356 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
357 }
358 }
359 }
360
361 /**
Winson Chung655332c2016-10-31 13:14:28 -0700362 * @return the bounds on the screen that the PIP can be visible in.
363 */
Winson Chung14fefc22016-11-02 10:02:29 -0700364 private void getInsetBounds(Rect outRect) {
365 mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth,
366 mDisplayInfo.logicalHeight, mTmpInsets);
367 outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y,
368 mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x,
369 mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y);
Winson Chung655332c2016-10-31 13:14:28 -0700370 }
371
372 /**
Winson Chung55893332017-02-17 17:13:10 -0800373 * @return the movement bounds for the given {@param stackBounds} and the current state of the
374 * controller.
375 */
376 private Rect getMovementBounds(Rect stackBounds) {
377 return getMovementBounds(stackBounds, true /* adjustForIme */);
378 }
379
380 /**
381 * @return the movement bounds for the given {@param stackBounds} and the current state of the
382 * controller.
383 */
384 private Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
385 final Rect movementBounds = new Rect();
386 getInsetBounds(movementBounds);
387
388 // Apply the movement bounds adjustments based on the current state
389 mSnapAlgorithm.getMovementBounds(stackBounds, movementBounds, movementBounds,
390 (adjustForIme && mIsImeShowing) ? mImeHeight : 0);
391 return movementBounds;
392 }
393
394 /**
Winson Chungf1f72f62017-02-14 17:15:48 -0800395 * Applies the minimized offsets to the given stack bounds.
396 */
397 private void applyMinimizedOffset(Rect stackBounds, Rect movementBounds) {
398 mTmpDisplaySize.set(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
399 mService.getStableInsetsLocked(mDisplayContent.getDisplayId(), mStableInsets);
400 mSnapAlgorithm.applyMinimizedOffset(stackBounds, movementBounds, mTmpDisplaySize,
401 mStableInsets);
402 }
403
404 /**
Winson Chung655332c2016-10-31 13:14:28 -0700405 * @return the pixels for a given dp value.
406 */
407 private int dpToPx(float dpValue, DisplayMetrics dm) {
408 return (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, dpValue, dm);
409 }
410
411 void dump(String prefix, PrintWriter pw) {
412 pw.println(prefix + "PinnedStackController");
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700413 pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw);
414 pw.println();
415 mService.getStackBounds(PINNED_STACK_ID, mTmpRect);
416 pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw);
417 pw.println();
Winson Chung655332c2016-10-31 13:14:28 -0700418 pw.println(prefix + " mIsImeShowing=" + mIsImeShowing);
Winson Chungfa7053782016-11-08 15:45:10 -0800419 pw.println(prefix + " mIsMinimized=" + mIsMinimized);
Winson Chunga29eb982016-12-14 12:01:27 -0800420 if (mActions.isEmpty()) {
421 pw.println(prefix + " mActions=[]");
422 } else {
423 pw.println(prefix + " mActions=[");
424 for (int i = 0; i < mActions.size(); i++) {
425 RemoteAction action = mActions.get(i);
426 pw.print(prefix + " Action[" + i + "]: ");
427 action.dump("", pw);
428 }
429 pw.println(prefix + " ]");
430 }
Winson Chung655332c2016-10-31 13:14:28 -0700431 }
432}