blob: 6a8417dc440b704b5c8afeed6ea6ab96f067e224 [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 /**
233 * @return the movement bounds for the given {@param stackBounds} and the current state of the
234 * controller.
235 */
236 Rect getMovementBounds(Rect stackBounds) {
Winson Chung14fefc22016-11-02 10:02:29 -0700237 return getMovementBounds(stackBounds, true /* adjustForIme */);
238 }
239
240 /**
241 * @return the movement bounds for the given {@param stackBounds} and the current state of the
242 * controller.
243 */
244 Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
Winson Chung655332c2016-10-31 13:14:28 -0700245 final Rect movementBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700246 getInsetBounds(movementBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700247
Winson Chung14fefc22016-11-02 10:02:29 -0700248 // Apply the movement bounds adjustments based on the current state
Winson Chung2a82fe52017-02-02 14:43:34 -0800249 mSnapAlgorithm.getMovementBounds(stackBounds, movementBounds, movementBounds,
250 (adjustForIme && mIsImeShowing) ? mImeHeight : 0);
Winson Chung655332c2016-10-31 13:14:28 -0700251 return movementBounds;
252 }
253
254 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800255 * @param preChangeTargetBounds The final bounds of the stack if it is currently animating
Winson Chung114aeea2017-01-09 16:08:07 -0800256 * @return the repositioned PIP bounds given it's pre-change bounds, and the new display
257 * content.
Winson Chung655332c2016-10-31 13:14:28 -0700258 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800259 Rect onDisplayChanged(Rect preChangeStackBounds, Rect preChangeTargetBounds,
260 DisplayContent displayContent) {
261 final Rect postChangeStackBounds = new Rect(preChangeTargetBounds);
Winson Chung114aeea2017-01-09 16:08:07 -0800262 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
Winson Chung14fefc22016-11-02 10:02:29 -0700263 if (!mDisplayInfo.equals(displayInfo)) {
264 // Calculate the snap fraction of the current stack along the old movement bounds, and
265 // then update the stack bounds to the same fraction along the rotated movement bounds.
266 final Rect preChangeMovementBounds = getMovementBounds(preChangeStackBounds);
267 final float snapFraction = mSnapAlgorithm.getSnapFraction(preChangeStackBounds,
268 preChangeMovementBounds);
269 mDisplayInfo.copyFrom(displayInfo);
270
271 final Rect postChangeMovementBounds = getMovementBounds(preChangeStackBounds,
272 false /* adjustForIme */);
273 mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds,
274 snapFraction);
Winson Chungd5a01592016-11-11 16:25:04 -0800275 if (mIsMinimized) {
Winson Chungf1f72f62017-02-14 17:15:48 -0800276 applyMinimizedOffset(postChangeStackBounds, postChangeMovementBounds);
Winson Chungd5a01592016-11-11 16:25:04 -0800277 }
Winson Chung2a82fe52017-02-02 14:43:34 -0800278 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chung14fefc22016-11-02 10:02:29 -0700279 }
280 return postChangeStackBounds;
Winson Chung655332c2016-10-31 13:14:28 -0700281 }
282
283 /**
284 * Sets the Ime state and height.
285 */
286 void setAdjustedForIme(boolean adjustedForIme, int imeHeight) {
287 // Return early if there is no state change
288 if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) {
289 return;
290 }
291
Winson Chung655332c2016-10-31 13:14:28 -0700292 mIsImeShowing = adjustedForIme;
293 mImeHeight = imeHeight;
Winson Chung2a82fe52017-02-02 14:43:34 -0800294 notifyImeVisibilityChanged(adjustedForIme, imeHeight);
295 notifyMovementBoundsChanged(true /* fromImeAdjustment */);
296 }
297
298 /**
299 * Sets the current aspect ratio.
300 */
301 void setAspectRatio(float aspectRatio) {
302 if (Float.compare(mAspectRatio, aspectRatio) != 0) {
303 mAspectRatio = aspectRatio;
304 notifyMovementBoundsChanged(false /* fromImeAdjustment */);
Winson Chung655332c2016-10-31 13:14:28 -0700305 }
306 }
307
308 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800309 * Sets the current set of actions.
310 */
311 void setActions(List<RemoteAction> actions) {
312 mActions.clear();
Winson Chungc2baac02017-01-11 13:34:47 -0800313 if (actions != null) {
314 mActions.addAll(actions);
315 }
Winson Chunga29eb982016-12-14 12:01:27 -0800316 notifyActionsChanged(mActions);
317 }
318
319 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800320 * Notifies listeners that the PIP needs to be adjusted for the IME.
Winson Chung655332c2016-10-31 13:14:28 -0700321 */
Winson Chung2a82fe52017-02-02 14:43:34 -0800322 private void notifyImeVisibilityChanged(boolean imeVisible, int imeHeight) {
Winson Chung655332c2016-10-31 13:14:28 -0700323 if (mPinnedStackListener != null) {
324 try {
Winson Chung2a82fe52017-02-02 14:43:34 -0800325 mPinnedStackListener.onImeVisibilityChanged(imeVisible, imeHeight);
Winson Chung655332c2016-10-31 13:14:28 -0700326 } catch (RemoteException e) {
327 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
328 }
329 }
330 }
331
332 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800333 * Notifies listeners that the PIP minimized state has changed.
334 */
335 private void notifyMinimizeChanged(boolean isMinimized) {
336 if (mPinnedStackListener != null) {
337 try {
338 mPinnedStackListener.onMinimizedStateChanged(isMinimized);
339 } catch (RemoteException e) {
340 Slog.e(TAG_WM, "Error delivering minimize changed event.", e);
341 }
342 }
343 }
344
345 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800346 * Notifies listeners that the PIP actions have changed.
347 */
348 private void notifyActionsChanged(List<RemoteAction> actions) {
349 if (mPinnedStackListener != null) {
350 try {
351 mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions));
352 } catch (RemoteException e) {
353 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
354 }
355 }
356 }
357
358 /**
Winson Chung2a82fe52017-02-02 14:43:34 -0800359 * Notifies listeners that the PIP movement bounds have changed.
360 */
361 private void notifyMovementBoundsChanged(boolean fromImeAdjustement) {
362 if (mPinnedStackListener != null) {
363 try {
364 Rect insetBounds = new Rect();
365 getInsetBounds(insetBounds);
366 Rect normalBounds = getDefaultBounds();
367 if (isValidPictureInPictureAspectRatio(mAspectRatio)) {
368 transformBoundsToAspectRatio(normalBounds, mAspectRatio);
369 }
370 mPinnedStackListener.onMovementBoundsChanged(insetBounds, normalBounds,
371 fromImeAdjustement);
372 } catch (RemoteException e) {
373 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
374 }
375 }
376 }
377
378 /**
Winson Chung655332c2016-10-31 13:14:28 -0700379 * @return the bounds on the screen that the PIP can be visible in.
380 */
Winson Chung14fefc22016-11-02 10:02:29 -0700381 private void getInsetBounds(Rect outRect) {
382 mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth,
383 mDisplayInfo.logicalHeight, mTmpInsets);
384 outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y,
385 mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x,
386 mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y);
Winson Chung655332c2016-10-31 13:14:28 -0700387 }
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}