blob: 596c3d81dcfe895de2ff06bbd755309325dc1252 [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
25import android.animation.ValueAnimator;
Winson Chunga29eb982016-12-14 12:01:27 -080026import android.app.RemoteAction;
27import android.content.pm.ParceledListSlice;
Winson Chung655332c2016-10-31 13:14:28 -070028import android.content.res.Resources;
29import android.graphics.Point;
Winson Chung84a38342016-11-08 16:15:10 -080030import android.graphics.PointF;
Winson Chung655332c2016-10-31 13:14:28 -070031import 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;
Winson Chung14fefc22016-11-02 10:02:29 -070040import android.view.DisplayInfo;
Winson Chung655332c2016-10-31 13:14:28 -070041import android.view.Gravity;
42import android.view.IPinnedStackController;
43import android.view.IPinnedStackListener;
44
Winson Chung655332c2016-10-31 13:14:28 -070045import com.android.internal.policy.PipMotionHelper;
46import 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/**
54 * Holds the common state of the pinned stack between the system and SystemUI.
55 */
56class PinnedStackController {
57
58 private static final String TAG = TAG_WITH_CLASS_NAME ? "PinnedStackController" : TAG_WM;
59
60 private final WindowManagerService mService;
61 private final DisplayContent mDisplayContent;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070062 private final Handler mHandler = UiThread.getHandler();
Winson Chung655332c2016-10-31 13:14:28 -070063
64 private IPinnedStackListener mPinnedStackListener;
65 private final PinnedStackListenerDeathHandler mPinnedStackListenerDeathHandler =
66 new PinnedStackListenerDeathHandler();
67
68 private final PinnedStackControllerCallback mCallbacks = new PinnedStackControllerCallback();
69 private final PipSnapAlgorithm mSnapAlgorithm;
70 private final PipMotionHelper mMotionHelper;
71
72 // States that affect how the PIP can be manipulated
73 private boolean mInInteractiveMode;
Winson Chungfa7053782016-11-08 15:45:10 -080074 private boolean mIsMinimized;
Winson Chunga29eb982016-12-14 12:01:27 -080075 private boolean mIsSnappingToEdge;
Winson Chung655332c2016-10-31 13:14:28 -070076 private boolean mIsImeShowing;
77 private int mImeHeight;
Winson Chung655332c2016-10-31 13:14:28 -070078 private ValueAnimator mBoundsAnimator = null;
79
Winson Chunga29eb982016-12-14 12:01:27 -080080 // The set of actions that are currently allowed on the PiP activity
81 private ArrayList<RemoteAction> mActions = new ArrayList<>();
82
Winson Chung14fefc22016-11-02 10:02:29 -070083 // Used to calculate stack bounds across rotations
84 private final DisplayInfo mDisplayInfo = new DisplayInfo();
Winson Chung114aeea2017-01-09 16:08:07 -080085 private final Rect mStableInsets = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -070086
Winson Chung655332c2016-10-31 13:14:28 -070087 // The size and position information that describes where the pinned stack will go by default.
88 private int mDefaultStackGravity;
89 private Size mDefaultStackSize;
90 private Point mScreenEdgeInsets;
91
92 // Temp vars for calculation
93 private final DisplayMetrics mTmpMetrics = new DisplayMetrics();
94 private final Rect mTmpInsets = new Rect();
Jorim Jaggiad5d2842016-11-01 18:22:53 -070095 private final Rect mTmpRect = new Rect();
Winson Chung655332c2016-10-31 13:14:28 -070096
97 /**
98 * The callback object passed to listeners for them to notify the controller of state changes.
99 */
100 private class PinnedStackControllerCallback extends IPinnedStackController.Stub {
101
102 @Override
103 public void setInInteractiveMode(final boolean inInteractiveMode) {
104 mHandler.post(() -> {
105 // Cancel any existing animations on the PIP once the user starts dragging it
106 if (mBoundsAnimator != null && inInteractiveMode) {
107 mBoundsAnimator.cancel();
108 }
109 mInInteractiveMode = inInteractiveMode;
Winson Chung655332c2016-10-31 13:14:28 -0700110 });
111 }
Winson Chungdff5c082016-11-02 17:28:03 -0700112
113 @Override
Winson Chungfa7053782016-11-08 15:45:10 -0800114 public void setIsMinimized(final boolean isMinimized) {
115 mHandler.post(() -> {
116 mIsMinimized = isMinimized;
Mady Mellor3b10dcd2017-01-23 10:08:35 -0800117 mSnapAlgorithm.setMinimized(isMinimized);
Winson Chungfa7053782016-11-08 15:45:10 -0800118 });
119 }
120
121 @Override
Winson Chungdff5c082016-11-02 17:28:03 -0700122 public void setSnapToEdge(final boolean snapToEdge) {
123 mHandler.post(() -> {
Winson Chunga29eb982016-12-14 12:01:27 -0800124 mIsSnappingToEdge = snapToEdge;
Winson Chungdff5c082016-11-02 17:28:03 -0700125 mSnapAlgorithm.setSnapToEdge(snapToEdge);
126 });
127 }
Winson Chung655332c2016-10-31 13:14:28 -0700128 }
129
130 /**
131 * Handler for the case where the listener dies.
132 */
133 private class PinnedStackListenerDeathHandler implements IBinder.DeathRecipient {
134
135 @Override
136 public void binderDied() {
137 // Clean up the state if the listener dies
138 mInInteractiveMode = false;
139 mPinnedStackListener = null;
140 }
141 }
142
143 PinnedStackController(WindowManagerService service, DisplayContent displayContent) {
144 mService = service;
145 mDisplayContent = displayContent;
146 mSnapAlgorithm = new PipSnapAlgorithm(service.mContext);
Winson Chungb5c41b72016-12-07 15:00:47 -0800147 mMotionHelper = new PipMotionHelper(UiThread.getHandler());
Winson Chung14fefc22016-11-02 10:02:29 -0700148 mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo());
Winson Chung655332c2016-10-31 13:14:28 -0700149 reloadResources();
150 }
151
152 void onConfigurationChanged() {
153 reloadResources();
154 }
155
156 /**
157 * Reloads all the resources for the current configuration.
158 */
159 void reloadResources() {
160 final Resources res = mService.mContext.getResources();
161 final Size defaultSizeDp = Size.parseSize(res.getString(
162 com.android.internal.R.string.config_defaultPictureInPictureSize));
163 final Size screenEdgeInsetsDp = Size.parseSize(res.getString(
164 com.android.internal.R.string.config_defaultPictureInPictureScreenEdgeInsets));
165 mDefaultStackGravity = res.getInteger(
166 com.android.internal.R.integer.config_defaultPictureInPictureGravity);
167 mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics);
168 mDefaultStackSize = new Size(dpToPx(defaultSizeDp.getWidth(), mTmpMetrics),
169 dpToPx(defaultSizeDp.getHeight(), mTmpMetrics));
170 mScreenEdgeInsets = new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics),
171 dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics));
172 }
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;
182 notifyBoundsChanged(mIsImeShowing);
Winson Chunga29eb982016-12-14 12:01:27 -0800183 notifyMinimizeChanged(mIsMinimized);
184 notifySnapToEdgeChanged(mIsSnappingToEdge);
185 notifyActionsChanged(mActions);
Winson Chung655332c2016-10-31 13:14:28 -0700186 } catch (RemoteException e) {
187 Log.e(TAG, "Failed to register pinned stack listener", e);
188 }
189 }
190
191 /**
Winson Chung84a38342016-11-08 16:15:10 -0800192 * Returns the current bounds (or the default bounds if there are no current bounds) with the
193 * specified aspect ratio.
194 */
195 Rect getAspectRatioBounds(Rect stackBounds, float aspectRatio) {
196 // Save the snap fraction, calculate the aspect ratio based on the current bounds
197 final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds,
198 getMovementBounds(stackBounds));
199 final float radius = PointF.length(stackBounds.width(), stackBounds.height());
200 final int height = (int) Math.round(Math.sqrt((radius * radius) /
201 (aspectRatio * aspectRatio + 1)));
202 final int width = Math.round(height * aspectRatio);
203 final int left = (int) (stackBounds.centerX() - width / 2f);
204 final int top = (int) (stackBounds.centerY() - height / 2f);
205 stackBounds.set(left, top, left + width, top + height);
206 mSnapAlgorithm.applySnapFraction(stackBounds, getMovementBounds(stackBounds), snapFraction);
207 return stackBounds;
208 }
209
210 /**
Winson Chung655332c2016-10-31 13:14:28 -0700211 * @return the default bounds to show the PIP when there is no active PIP.
212 */
213 Rect getDefaultBounds() {
Winson Chung655332c2016-10-31 13:14:28 -0700214 final Rect insetBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700215 getInsetBounds(insetBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700216
217 final Rect defaultBounds = new Rect();
218 Gravity.apply(mDefaultStackGravity, mDefaultStackSize.getWidth(),
219 mDefaultStackSize.getHeight(), insetBounds, 0, 0, defaultBounds);
220 return defaultBounds;
221 }
222
223 /**
224 * @return the movement bounds for the given {@param stackBounds} and the current state of the
225 * controller.
226 */
227 Rect getMovementBounds(Rect stackBounds) {
Winson Chung14fefc22016-11-02 10:02:29 -0700228 return getMovementBounds(stackBounds, true /* adjustForIme */);
229 }
230
231 /**
232 * @return the movement bounds for the given {@param stackBounds} and the current state of the
233 * controller.
234 */
235 Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
Winson Chung655332c2016-10-31 13:14:28 -0700236 final Rect movementBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700237 getInsetBounds(movementBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700238
239 // Adjust the right/bottom to ensure the stack bounds never goes offscreen
240 movementBounds.right = Math.max(movementBounds.left, movementBounds.right -
241 stackBounds.width());
242 movementBounds.bottom = Math.max(movementBounds.top, movementBounds.bottom -
243 stackBounds.height());
244
Winson Chung14fefc22016-11-02 10:02:29 -0700245 // Apply the movement bounds adjustments based on the current state
246 if (adjustForIme) {
247 if (mIsImeShowing) {
248 movementBounds.bottom -= mImeHeight;
249 }
Winson Chung655332c2016-10-31 13:14:28 -0700250 }
Winson Chung655332c2016-10-31 13:14:28 -0700251 return movementBounds;
252 }
253
254 /**
Winson Chung114aeea2017-01-09 16:08:07 -0800255 * @return the repositioned PIP bounds given it's pre-change bounds, and the new display
256 * content.
Winson Chung655332c2016-10-31 13:14:28 -0700257 */
Winson Chung114aeea2017-01-09 16:08:07 -0800258 Rect onDisplayChanged(Rect preChangeStackBounds, DisplayContent displayContent) {
Winson Chung14fefc22016-11-02 10:02:29 -0700259 final Rect postChangeStackBounds = new Rect(preChangeStackBounds);
Winson Chung114aeea2017-01-09 16:08:07 -0800260 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
Winson Chung14fefc22016-11-02 10:02:29 -0700261 if (!mDisplayInfo.equals(displayInfo)) {
262 // Calculate the snap fraction of the current stack along the old movement bounds, and
263 // then update the stack bounds to the same fraction along the rotated movement bounds.
264 final Rect preChangeMovementBounds = getMovementBounds(preChangeStackBounds);
265 final float snapFraction = mSnapAlgorithm.getSnapFraction(preChangeStackBounds,
266 preChangeMovementBounds);
267 mDisplayInfo.copyFrom(displayInfo);
268
269 final Rect postChangeMovementBounds = getMovementBounds(preChangeStackBounds,
270 false /* adjustForIme */);
271 mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds,
272 snapFraction);
Winson Chungd5a01592016-11-11 16:25:04 -0800273 if (mIsMinimized) {
274 final Point displaySize = new Point(mDisplayInfo.logicalWidth,
275 mDisplayInfo.logicalHeight);
Winson Chung114aeea2017-01-09 16:08:07 -0800276 mService.getStableInsetsLocked(displayContent.getDisplayId(), mStableInsets);
Winson Chungd5a01592016-11-11 16:25:04 -0800277 mSnapAlgorithm.applyMinimizedOffset(postChangeStackBounds, postChangeMovementBounds,
Winson Chung114aeea2017-01-09 16:08:07 -0800278 displaySize, mStableInsets);
Winson Chungd5a01592016-11-11 16:25:04 -0800279 }
Winson Chung14fefc22016-11-02 10:02:29 -0700280 }
281 return postChangeStackBounds;
Winson Chung655332c2016-10-31 13:14:28 -0700282 }
283
284 /**
285 * Sets the Ime state and height.
286 */
287 void setAdjustedForIme(boolean adjustedForIme, int imeHeight) {
288 // Return early if there is no state change
289 if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) {
290 return;
291 }
292
293 final Rect stackBounds = new Rect();
294 mService.getStackBounds(PINNED_STACK_ID, stackBounds);
295 final Rect prevMovementBounds = getMovementBounds(stackBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700296 mIsImeShowing = adjustedForIme;
297 mImeHeight = imeHeight;
298 if (mInInteractiveMode) {
299 // If the user is currently interacting with the PIP and the ime state changes, then
300 // don't adjust the bounds and defer that to after the interaction
301 notifyBoundsChanged(adjustedForIme /* adjustedForIme */);
302 } else {
303 // Otherwise, we can move the PIP to a sane location to ensure that it does not block
304 // the user from interacting with the IME
Winson Chung14fefc22016-11-02 10:02:29 -0700305 final Rect movementBounds = getMovementBounds(stackBounds);
306 final Rect toBounds = new Rect(stackBounds);
307 if (adjustedForIme) {
308 // IME visible
Winson Chungd5a01592016-11-11 16:25:04 -0800309 if (stackBounds.top == prevMovementBounds.bottom) {
310 // If the PIP is resting on top of the IME, then adjust it with the hiding IME
311 toBounds.offsetTo(toBounds.left, movementBounds.bottom);
312 } else {
313 toBounds.offset(0, Math.min(0, movementBounds.bottom - stackBounds.top));
314 }
Winson Chung655332c2016-10-31 13:14:28 -0700315 } else {
Winson Chung14fefc22016-11-02 10:02:29 -0700316 // IME hidden
317 if (stackBounds.top == prevMovementBounds.bottom) {
318 // If the PIP is resting on top of the IME, then adjust it with the hiding IME
319 toBounds.offsetTo(toBounds.left, movementBounds.bottom);
320 }
Winson Chung655332c2016-10-31 13:14:28 -0700321 }
322 if (!toBounds.equals(stackBounds)) {
323 if (mBoundsAnimator != null) {
324 mBoundsAnimator.cancel();
325 }
326 mBoundsAnimator = mMotionHelper.createAnimationToBounds(stackBounds, toBounds);
327 mBoundsAnimator.start();
328 }
329 }
330 }
331
332 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800333 * Sets the current set of actions.
334 */
335 void setActions(List<RemoteAction> actions) {
336 mActions.clear();
Winson Chungc2baac02017-01-11 13:34:47 -0800337 if (actions != null) {
338 mActions.addAll(actions);
339 }
Winson Chunga29eb982016-12-14 12:01:27 -0800340 notifyActionsChanged(mActions);
341 }
342
343 /**
344 * Notifies listeners that the PIP movement bounds have changed.
Winson Chung655332c2016-10-31 13:14:28 -0700345 */
346 private void notifyBoundsChanged(boolean adjustedForIme) {
347 if (mPinnedStackListener != null) {
348 try {
349 mPinnedStackListener.onBoundsChanged(adjustedForIme);
350 } catch (RemoteException e) {
351 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
352 }
353 }
354 }
355
356 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800357 * Notifies listeners that the PIP minimized state has changed.
358 */
359 private void notifyMinimizeChanged(boolean isMinimized) {
360 if (mPinnedStackListener != null) {
361 try {
362 mPinnedStackListener.onMinimizedStateChanged(isMinimized);
363 } catch (RemoteException e) {
364 Slog.e(TAG_WM, "Error delivering minimize changed event.", e);
365 }
366 }
367 }
368
369 /**
370 * Notifies listeners that the PIP snap-to-edge state has changed.
371 */
372 private void notifySnapToEdgeChanged(boolean isSnappingToEdge) {
373 if (mPinnedStackListener != null) {
374 try {
375 mPinnedStackListener.onSnapToEdgeStateChanged(isSnappingToEdge);
376 } catch (RemoteException e) {
377 Slog.e(TAG_WM, "Error delivering snap-to-edge changed event.", e);
378 }
379 }
380 }
381
382 /**
383 * Notifies listeners that the PIP actions have changed.
384 */
385 private void notifyActionsChanged(List<RemoteAction> actions) {
386 if (mPinnedStackListener != null) {
387 try {
388 mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions));
389 } catch (RemoteException e) {
390 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
391 }
392 }
393 }
394
395 /**
Winson Chung655332c2016-10-31 13:14:28 -0700396 * @return the bounds on the screen that the PIP can be visible in.
397 */
Winson Chung14fefc22016-11-02 10:02:29 -0700398 private void getInsetBounds(Rect outRect) {
399 mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth,
400 mDisplayInfo.logicalHeight, mTmpInsets);
401 outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y,
402 mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x,
403 mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y);
Winson Chung655332c2016-10-31 13:14:28 -0700404 }
405
406 /**
407 * @return the pixels for a given dp value.
408 */
409 private int dpToPx(float dpValue, DisplayMetrics dm) {
410 return (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, dpValue, dm);
411 }
412
413 void dump(String prefix, PrintWriter pw) {
414 pw.println(prefix + "PinnedStackController");
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700415 pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw);
416 pw.println();
417 mService.getStackBounds(PINNED_STACK_ID, mTmpRect);
418 pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw);
419 pw.println();
Winson Chung655332c2016-10-31 13:14:28 -0700420 pw.println(prefix + " mIsImeShowing=" + mIsImeShowing);
421 pw.println(prefix + " mInInteractiveMode=" + mInInteractiveMode);
Winson Chungfa7053782016-11-08 15:45:10 -0800422 pw.println(prefix + " mIsMinimized=" + mIsMinimized);
Winson Chunga29eb982016-12-14 12:01:27 -0800423 if (mActions.isEmpty()) {
424 pw.println(prefix + " mActions=[]");
425 } else {
426 pw.println(prefix + " mActions=[");
427 for (int i = 0; i < mActions.size(); i++) {
428 RemoteAction action = mActions.get(i);
429 pw.print(prefix + " Action[" + i + "]: ");
430 action.dump("", pw);
431 }
432 pw.println(prefix + " ]");
433 }
Winson Chung655332c2016-10-31 13:14:28 -0700434 }
435}