blob: 34633c243d657272a120b1cf7d43d55959be161b [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();
85
Winson Chung655332c2016-10-31 13:14:28 -070086 // The size and position information that describes where the pinned stack will go by default.
87 private int mDefaultStackGravity;
88 private Size mDefaultStackSize;
89 private Point mScreenEdgeInsets;
90
91 // Temp vars for calculation
92 private final DisplayMetrics mTmpMetrics = new DisplayMetrics();
93 private final Rect mTmpInsets = new Rect();
Jorim Jaggiad5d2842016-11-01 18:22:53 -070094 private final Rect mTmpRect = new Rect();
Winson Chung655332c2016-10-31 13:14:28 -070095
96 /**
97 * The callback object passed to listeners for them to notify the controller of state changes.
98 */
99 private class PinnedStackControllerCallback extends IPinnedStackController.Stub {
100
101 @Override
102 public void setInInteractiveMode(final boolean inInteractiveMode) {
103 mHandler.post(() -> {
104 // Cancel any existing animations on the PIP once the user starts dragging it
105 if (mBoundsAnimator != null && inInteractiveMode) {
106 mBoundsAnimator.cancel();
107 }
108 mInInteractiveMode = inInteractiveMode;
Winson Chung655332c2016-10-31 13:14:28 -0700109 });
110 }
Winson Chungdff5c082016-11-02 17:28:03 -0700111
112 @Override
Winson Chungfa7053782016-11-08 15:45:10 -0800113 public void setIsMinimized(final boolean isMinimized) {
114 mHandler.post(() -> {
115 mIsMinimized = isMinimized;
116 });
117 }
118
119 @Override
Winson Chungdff5c082016-11-02 17:28:03 -0700120 public void setSnapToEdge(final boolean snapToEdge) {
121 mHandler.post(() -> {
Winson Chunga29eb982016-12-14 12:01:27 -0800122 mIsSnappingToEdge = snapToEdge;
Winson Chungdff5c082016-11-02 17:28:03 -0700123 mSnapAlgorithm.setSnapToEdge(snapToEdge);
124 });
125 }
Winson Chung655332c2016-10-31 13:14:28 -0700126 }
127
128 /**
129 * Handler for the case where the listener dies.
130 */
131 private class PinnedStackListenerDeathHandler implements IBinder.DeathRecipient {
132
133 @Override
134 public void binderDied() {
135 // Clean up the state if the listener dies
136 mInInteractiveMode = false;
137 mPinnedStackListener = null;
138 }
139 }
140
141 PinnedStackController(WindowManagerService service, DisplayContent displayContent) {
142 mService = service;
143 mDisplayContent = displayContent;
144 mSnapAlgorithm = new PipSnapAlgorithm(service.mContext);
Winson Chungb5c41b72016-12-07 15:00:47 -0800145 mMotionHelper = new PipMotionHelper(UiThread.getHandler());
Winson Chung14fefc22016-11-02 10:02:29 -0700146 mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo());
Winson Chung655332c2016-10-31 13:14:28 -0700147 reloadResources();
148 }
149
150 void onConfigurationChanged() {
151 reloadResources();
152 }
153
154 /**
155 * Reloads all the resources for the current configuration.
156 */
157 void reloadResources() {
158 final Resources res = mService.mContext.getResources();
159 final Size defaultSizeDp = Size.parseSize(res.getString(
160 com.android.internal.R.string.config_defaultPictureInPictureSize));
161 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);
166 mDefaultStackSize = new Size(dpToPx(defaultSizeDp.getWidth(), mTmpMetrics),
167 dpToPx(defaultSizeDp.getHeight(), mTmpMetrics));
168 mScreenEdgeInsets = new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics),
169 dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics));
170 }
171
172 /**
173 * Registers a pinned stack listener.
174 */
175 void registerPinnedStackListener(IPinnedStackListener listener) {
176 try {
177 listener.asBinder().linkToDeath(mPinnedStackListenerDeathHandler, 0);
178 listener.onListenerRegistered(mCallbacks);
179 mPinnedStackListener = listener;
180 notifyBoundsChanged(mIsImeShowing);
Winson Chunga29eb982016-12-14 12:01:27 -0800181 notifyMinimizeChanged(mIsMinimized);
182 notifySnapToEdgeChanged(mIsSnappingToEdge);
183 notifyActionsChanged(mActions);
Winson Chung655332c2016-10-31 13:14:28 -0700184 } catch (RemoteException e) {
185 Log.e(TAG, "Failed to register pinned stack listener", e);
186 }
187 }
188
189 /**
Winson Chung84a38342016-11-08 16:15:10 -0800190 * Returns the current bounds (or the default bounds if there are no current bounds) with the
191 * specified aspect ratio.
192 */
193 Rect getAspectRatioBounds(Rect stackBounds, float aspectRatio) {
194 // Save the snap fraction, calculate the aspect ratio based on the current bounds
195 final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds,
196 getMovementBounds(stackBounds));
197 final float radius = PointF.length(stackBounds.width(), stackBounds.height());
198 final int height = (int) Math.round(Math.sqrt((radius * radius) /
199 (aspectRatio * aspectRatio + 1)));
200 final int width = Math.round(height * aspectRatio);
201 final int left = (int) (stackBounds.centerX() - width / 2f);
202 final int top = (int) (stackBounds.centerY() - height / 2f);
203 stackBounds.set(left, top, left + width, top + height);
204 mSnapAlgorithm.applySnapFraction(stackBounds, getMovementBounds(stackBounds), snapFraction);
205 return stackBounds;
206 }
207
208 /**
Winson Chung655332c2016-10-31 13:14:28 -0700209 * @return the default bounds to show the PIP when there is no active PIP.
210 */
211 Rect getDefaultBounds() {
Winson Chung655332c2016-10-31 13:14:28 -0700212 final Rect insetBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700213 getInsetBounds(insetBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700214
215 final Rect defaultBounds = new Rect();
216 Gravity.apply(mDefaultStackGravity, mDefaultStackSize.getWidth(),
217 mDefaultStackSize.getHeight(), insetBounds, 0, 0, defaultBounds);
218 return defaultBounds;
219 }
220
221 /**
222 * @return the movement bounds for the given {@param stackBounds} and the current state of the
223 * controller.
224 */
225 Rect getMovementBounds(Rect stackBounds) {
Winson Chung14fefc22016-11-02 10:02:29 -0700226 return getMovementBounds(stackBounds, true /* adjustForIme */);
227 }
228
229 /**
230 * @return the movement bounds for the given {@param stackBounds} and the current state of the
231 * controller.
232 */
233 Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
Winson Chung655332c2016-10-31 13:14:28 -0700234 final Rect movementBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700235 getInsetBounds(movementBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700236
237 // Adjust the right/bottom to ensure the stack bounds never goes offscreen
238 movementBounds.right = Math.max(movementBounds.left, movementBounds.right -
239 stackBounds.width());
240 movementBounds.bottom = Math.max(movementBounds.top, movementBounds.bottom -
241 stackBounds.height());
242
Winson Chung14fefc22016-11-02 10:02:29 -0700243 // Apply the movement bounds adjustments based on the current state
244 if (adjustForIme) {
245 if (mIsImeShowing) {
246 movementBounds.bottom -= mImeHeight;
247 }
Winson Chung655332c2016-10-31 13:14:28 -0700248 }
Winson Chung655332c2016-10-31 13:14:28 -0700249 return movementBounds;
250 }
251
252 /**
Winson Chung14fefc22016-11-02 10:02:29 -0700253 * @return the repositioned PIP bounds given it's pre-change bounds, and the new display info.
Winson Chung655332c2016-10-31 13:14:28 -0700254 */
Winson Chung14fefc22016-11-02 10:02:29 -0700255 Rect onDisplayChanged(Rect preChangeStackBounds, DisplayInfo displayInfo) {
256 final Rect postChangeStackBounds = new Rect(preChangeStackBounds);
257 if (!mDisplayInfo.equals(displayInfo)) {
258 // Calculate the snap fraction of the current stack along the old movement bounds, and
259 // then update the stack bounds to the same fraction along the rotated movement bounds.
260 final Rect preChangeMovementBounds = getMovementBounds(preChangeStackBounds);
261 final float snapFraction = mSnapAlgorithm.getSnapFraction(preChangeStackBounds,
262 preChangeMovementBounds);
263 mDisplayInfo.copyFrom(displayInfo);
264
265 final Rect postChangeMovementBounds = getMovementBounds(preChangeStackBounds,
266 false /* adjustForIme */);
267 mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds,
268 snapFraction);
Winson Chungd5a01592016-11-11 16:25:04 -0800269 if (mIsMinimized) {
270 final Point displaySize = new Point(mDisplayInfo.logicalWidth,
271 mDisplayInfo.logicalHeight);
272 mSnapAlgorithm.applyMinimizedOffset(postChangeStackBounds, postChangeMovementBounds,
273 displaySize);
274 }
Winson Chung14fefc22016-11-02 10:02:29 -0700275 }
276 return postChangeStackBounds;
Winson Chung655332c2016-10-31 13:14:28 -0700277 }
278
279 /**
280 * Sets the Ime state and height.
281 */
282 void setAdjustedForIme(boolean adjustedForIme, int imeHeight) {
283 // Return early if there is no state change
284 if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) {
285 return;
286 }
287
288 final Rect stackBounds = new Rect();
289 mService.getStackBounds(PINNED_STACK_ID, stackBounds);
290 final Rect prevMovementBounds = getMovementBounds(stackBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700291 mIsImeShowing = adjustedForIme;
292 mImeHeight = imeHeight;
293 if (mInInteractiveMode) {
294 // If the user is currently interacting with the PIP and the ime state changes, then
295 // don't adjust the bounds and defer that to after the interaction
296 notifyBoundsChanged(adjustedForIme /* adjustedForIme */);
297 } else {
298 // Otherwise, we can move the PIP to a sane location to ensure that it does not block
299 // the user from interacting with the IME
Winson Chung14fefc22016-11-02 10:02:29 -0700300 final Rect movementBounds = getMovementBounds(stackBounds);
301 final Rect toBounds = new Rect(stackBounds);
302 if (adjustedForIme) {
303 // IME visible
Winson Chungd5a01592016-11-11 16:25:04 -0800304 if (stackBounds.top == prevMovementBounds.bottom) {
305 // If the PIP is resting on top of the IME, then adjust it with the hiding IME
306 toBounds.offsetTo(toBounds.left, movementBounds.bottom);
307 } else {
308 toBounds.offset(0, Math.min(0, movementBounds.bottom - stackBounds.top));
309 }
Winson Chung655332c2016-10-31 13:14:28 -0700310 } else {
Winson Chung14fefc22016-11-02 10:02:29 -0700311 // IME hidden
312 if (stackBounds.top == prevMovementBounds.bottom) {
313 // If the PIP is resting on top of the IME, then adjust it with the hiding IME
314 toBounds.offsetTo(toBounds.left, movementBounds.bottom);
315 }
Winson Chung655332c2016-10-31 13:14:28 -0700316 }
317 if (!toBounds.equals(stackBounds)) {
318 if (mBoundsAnimator != null) {
319 mBoundsAnimator.cancel();
320 }
321 mBoundsAnimator = mMotionHelper.createAnimationToBounds(stackBounds, toBounds);
322 mBoundsAnimator.start();
323 }
324 }
325 }
326
327 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800328 * Sets the current set of actions.
329 */
330 void setActions(List<RemoteAction> actions) {
331 mActions.clear();
332 mActions.addAll(actions);
333 notifyActionsChanged(mActions);
334 }
335
336 /**
337 * Notifies listeners that the PIP movement bounds have changed.
Winson Chung655332c2016-10-31 13:14:28 -0700338 */
339 private void notifyBoundsChanged(boolean adjustedForIme) {
340 if (mPinnedStackListener != null) {
341 try {
342 mPinnedStackListener.onBoundsChanged(adjustedForIme);
343 } catch (RemoteException e) {
344 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
345 }
346 }
347 }
348
349 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800350 * Notifies listeners that the PIP minimized state has changed.
351 */
352 private void notifyMinimizeChanged(boolean isMinimized) {
353 if (mPinnedStackListener != null) {
354 try {
355 mPinnedStackListener.onMinimizedStateChanged(isMinimized);
356 } catch (RemoteException e) {
357 Slog.e(TAG_WM, "Error delivering minimize changed event.", e);
358 }
359 }
360 }
361
362 /**
363 * Notifies listeners that the PIP snap-to-edge state has changed.
364 */
365 private void notifySnapToEdgeChanged(boolean isSnappingToEdge) {
366 if (mPinnedStackListener != null) {
367 try {
368 mPinnedStackListener.onSnapToEdgeStateChanged(isSnappingToEdge);
369 } catch (RemoteException e) {
370 Slog.e(TAG_WM, "Error delivering snap-to-edge changed event.", e);
371 }
372 }
373 }
374
375 /**
376 * Notifies listeners that the PIP actions have changed.
377 */
378 private void notifyActionsChanged(List<RemoteAction> actions) {
379 if (mPinnedStackListener != null) {
380 try {
381 mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions));
382 } catch (RemoteException e) {
383 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
384 }
385 }
386 }
387
388 /**
Winson Chung655332c2016-10-31 13:14:28 -0700389 * @return the bounds on the screen that the PIP can be visible in.
390 */
Winson Chung14fefc22016-11-02 10:02:29 -0700391 private void getInsetBounds(Rect outRect) {
392 mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth,
393 mDisplayInfo.logicalHeight, mTmpInsets);
394 outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y,
395 mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x,
396 mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y);
Winson Chung655332c2016-10-31 13:14:28 -0700397 }
398
399 /**
400 * @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);
414 pw.println(prefix + " mInInteractiveMode=" + mInInteractiveMode);
Winson Chungfa7053782016-11-08 15:45:10 -0800415 pw.println(prefix + " mIsMinimized=" + mIsMinimized);
Winson Chunga29eb982016-12-14 12:01:27 -0800416 if (mActions.isEmpty()) {
417 pw.println(prefix + " mActions=[]");
418 } else {
419 pw.println(prefix + " mActions=[");
420 for (int i = 0; i < mActions.size(); i++) {
421 RemoteAction action = mActions.get(i);
422 pw.print(prefix + " Action[" + i + "]: ");
423 action.dump("", pw);
424 }
425 pw.println(prefix + " ]");
426 }
Winson Chung655332c2016-10-31 13:14:28 -0700427 }
428}