blob: faca8db3ab87890989302253ce0efada2a5aab45 [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;
Jorim Jaggiad5d2842016-11-01 18:22:53 -070021import static android.view.Display.DEFAULT_DISPLAY;
Winson Chung655332c2016-10-31 13:14:28 -070022
23import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
24import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
25
26import android.animation.ValueAnimator;
27import android.content.res.Resources;
28import android.graphics.Point;
29import android.graphics.Rect;
30import android.os.Handler;
31import android.os.IBinder;
32import android.os.RemoteException;
33import android.util.DisplayMetrics;
34import android.util.Log;
35import android.util.Size;
36import android.util.Slog;
37import android.util.TypedValue;
Winson Chung14fefc22016-11-02 10:02:29 -070038import android.view.DisplayInfo;
Winson Chung655332c2016-10-31 13:14:28 -070039import android.view.Gravity;
40import android.view.IPinnedStackController;
41import android.view.IPinnedStackListener;
42
43import com.android.internal.os.BackgroundThread;
44import com.android.internal.policy.PipMotionHelper;
45import com.android.internal.policy.PipSnapAlgorithm;
46
47import java.io.PrintWriter;
48
49/**
50 * Holds the common state of the pinned stack between the system and SystemUI.
51 */
52class PinnedStackController {
53
54 private static final String TAG = TAG_WITH_CLASS_NAME ? "PinnedStackController" : TAG_WM;
55
56 private final WindowManagerService mService;
57 private final DisplayContent mDisplayContent;
58 private final Handler mHandler = new Handler();
59
60 private IPinnedStackListener mPinnedStackListener;
61 private final PinnedStackListenerDeathHandler mPinnedStackListenerDeathHandler =
62 new PinnedStackListenerDeathHandler();
63
64 private final PinnedStackControllerCallback mCallbacks = new PinnedStackControllerCallback();
65 private final PipSnapAlgorithm mSnapAlgorithm;
66 private final PipMotionHelper mMotionHelper;
67
68 // States that affect how the PIP can be manipulated
69 private boolean mInInteractiveMode;
70 private boolean mIsImeShowing;
71 private int mImeHeight;
Winson Chung655332c2016-10-31 13:14:28 -070072 private ValueAnimator mBoundsAnimator = null;
73
Winson Chung14fefc22016-11-02 10:02:29 -070074 // Used to calculate stack bounds across rotations
75 private final DisplayInfo mDisplayInfo = new DisplayInfo();
76
Winson Chung655332c2016-10-31 13:14:28 -070077 // The size and position information that describes where the pinned stack will go by default.
78 private int mDefaultStackGravity;
79 private Size mDefaultStackSize;
80 private Point mScreenEdgeInsets;
81
82 // Temp vars for calculation
83 private final DisplayMetrics mTmpMetrics = new DisplayMetrics();
84 private final Rect mTmpInsets = new Rect();
Jorim Jaggiad5d2842016-11-01 18:22:53 -070085 private final Rect mTmpRect = new Rect();
Winson Chung655332c2016-10-31 13:14:28 -070086
87 /**
88 * The callback object passed to listeners for them to notify the controller of state changes.
89 */
90 private class PinnedStackControllerCallback extends IPinnedStackController.Stub {
91
92 @Override
93 public void setInInteractiveMode(final boolean inInteractiveMode) {
94 mHandler.post(() -> {
95 // Cancel any existing animations on the PIP once the user starts dragging it
96 if (mBoundsAnimator != null && inInteractiveMode) {
97 mBoundsAnimator.cancel();
98 }
99 mInInteractiveMode = inInteractiveMode;
Winson Chung655332c2016-10-31 13:14:28 -0700100 });
101 }
102 }
103
104 /**
105 * Handler for the case where the listener dies.
106 */
107 private class PinnedStackListenerDeathHandler implements IBinder.DeathRecipient {
108
109 @Override
110 public void binderDied() {
111 // Clean up the state if the listener dies
112 mInInteractiveMode = false;
113 mPinnedStackListener = null;
114 }
115 }
116
117 PinnedStackController(WindowManagerService service, DisplayContent displayContent) {
118 mService = service;
119 mDisplayContent = displayContent;
120 mSnapAlgorithm = new PipSnapAlgorithm(service.mContext);
121 mMotionHelper = new PipMotionHelper(BackgroundThread.getHandler());
Winson Chung14fefc22016-11-02 10:02:29 -0700122 mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo());
Winson Chung655332c2016-10-31 13:14:28 -0700123 reloadResources();
124 }
125
126 void onConfigurationChanged() {
127 reloadResources();
128 }
129
130 /**
131 * Reloads all the resources for the current configuration.
132 */
133 void reloadResources() {
134 final Resources res = mService.mContext.getResources();
135 final Size defaultSizeDp = Size.parseSize(res.getString(
136 com.android.internal.R.string.config_defaultPictureInPictureSize));
137 final Size screenEdgeInsetsDp = Size.parseSize(res.getString(
138 com.android.internal.R.string.config_defaultPictureInPictureScreenEdgeInsets));
139 mDefaultStackGravity = res.getInteger(
140 com.android.internal.R.integer.config_defaultPictureInPictureGravity);
141 mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics);
142 mDefaultStackSize = new Size(dpToPx(defaultSizeDp.getWidth(), mTmpMetrics),
143 dpToPx(defaultSizeDp.getHeight(), mTmpMetrics));
144 mScreenEdgeInsets = new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics),
145 dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics));
146 }
147
148 /**
149 * Registers a pinned stack listener.
150 */
151 void registerPinnedStackListener(IPinnedStackListener listener) {
152 try {
153 listener.asBinder().linkToDeath(mPinnedStackListenerDeathHandler, 0);
154 listener.onListenerRegistered(mCallbacks);
155 mPinnedStackListener = listener;
156 notifyBoundsChanged(mIsImeShowing);
157 } catch (RemoteException e) {
158 Log.e(TAG, "Failed to register pinned stack listener", e);
159 }
160 }
161
162 /**
163 * @return the default bounds to show the PIP when there is no active PIP.
164 */
165 Rect getDefaultBounds() {
Winson Chung655332c2016-10-31 13:14:28 -0700166 final Rect insetBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700167 getInsetBounds(insetBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700168
169 final Rect defaultBounds = new Rect();
170 Gravity.apply(mDefaultStackGravity, mDefaultStackSize.getWidth(),
171 mDefaultStackSize.getHeight(), insetBounds, 0, 0, defaultBounds);
172 return defaultBounds;
173 }
174
175 /**
176 * @return the movement bounds for the given {@param stackBounds} and the current state of the
177 * controller.
178 */
179 Rect getMovementBounds(Rect stackBounds) {
Winson Chung14fefc22016-11-02 10:02:29 -0700180 return getMovementBounds(stackBounds, true /* adjustForIme */);
181 }
182
183 /**
184 * @return the movement bounds for the given {@param stackBounds} and the current state of the
185 * controller.
186 */
187 Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
Winson Chung655332c2016-10-31 13:14:28 -0700188 final Rect movementBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700189 getInsetBounds(movementBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700190
191 // Adjust the right/bottom to ensure the stack bounds never goes offscreen
192 movementBounds.right = Math.max(movementBounds.left, movementBounds.right -
193 stackBounds.width());
194 movementBounds.bottom = Math.max(movementBounds.top, movementBounds.bottom -
195 stackBounds.height());
196
Winson Chung14fefc22016-11-02 10:02:29 -0700197 // Apply the movement bounds adjustments based on the current state
198 if (adjustForIme) {
199 if (mIsImeShowing) {
200 movementBounds.bottom -= mImeHeight;
201 }
Winson Chung655332c2016-10-31 13:14:28 -0700202 }
Winson Chung655332c2016-10-31 13:14:28 -0700203 return movementBounds;
204 }
205
206 /**
Winson Chung14fefc22016-11-02 10:02:29 -0700207 * @return the repositioned PIP bounds given it's pre-change bounds, and the new display info.
Winson Chung655332c2016-10-31 13:14:28 -0700208 */
Winson Chung14fefc22016-11-02 10:02:29 -0700209 Rect onDisplayChanged(Rect preChangeStackBounds, DisplayInfo displayInfo) {
210 final Rect postChangeStackBounds = new Rect(preChangeStackBounds);
211 if (!mDisplayInfo.equals(displayInfo)) {
212 // Calculate the snap fraction of the current stack along the old movement bounds, and
213 // then update the stack bounds to the same fraction along the rotated movement bounds.
214 final Rect preChangeMovementBounds = getMovementBounds(preChangeStackBounds);
215 final float snapFraction = mSnapAlgorithm.getSnapFraction(preChangeStackBounds,
216 preChangeMovementBounds);
217 mDisplayInfo.copyFrom(displayInfo);
218
219 final Rect postChangeMovementBounds = getMovementBounds(preChangeStackBounds,
220 false /* adjustForIme */);
221 mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds,
222 snapFraction);
223 }
224 return postChangeStackBounds;
Winson Chung655332c2016-10-31 13:14:28 -0700225 }
226
227 /**
228 * Sets the Ime state and height.
229 */
230 void setAdjustedForIme(boolean adjustedForIme, int imeHeight) {
231 // Return early if there is no state change
232 if (mIsImeShowing == adjustedForIme && mImeHeight == imeHeight) {
233 return;
234 }
235
236 final Rect stackBounds = new Rect();
237 mService.getStackBounds(PINNED_STACK_ID, stackBounds);
238 final Rect prevMovementBounds = getMovementBounds(stackBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700239 mIsImeShowing = adjustedForIme;
240 mImeHeight = imeHeight;
241 if (mInInteractiveMode) {
242 // If the user is currently interacting with the PIP and the ime state changes, then
243 // don't adjust the bounds and defer that to after the interaction
244 notifyBoundsChanged(adjustedForIme /* adjustedForIme */);
245 } else {
246 // Otherwise, we can move the PIP to a sane location to ensure that it does not block
247 // the user from interacting with the IME
Winson Chung14fefc22016-11-02 10:02:29 -0700248 final Rect movementBounds = getMovementBounds(stackBounds);
249 final Rect toBounds = new Rect(stackBounds);
250 if (adjustedForIme) {
251 // IME visible
252 toBounds.offset(0, Math.min(0, movementBounds.bottom - stackBounds.top));
Winson Chung655332c2016-10-31 13:14:28 -0700253 } else {
Winson Chung14fefc22016-11-02 10:02:29 -0700254 // IME hidden
255 if (stackBounds.top == prevMovementBounds.bottom) {
256 // If the PIP is resting on top of the IME, then adjust it with the hiding IME
257 toBounds.offsetTo(toBounds.left, movementBounds.bottom);
258 }
Winson Chung655332c2016-10-31 13:14:28 -0700259 }
260 if (!toBounds.equals(stackBounds)) {
261 if (mBoundsAnimator != null) {
262 mBoundsAnimator.cancel();
263 }
264 mBoundsAnimator = mMotionHelper.createAnimationToBounds(stackBounds, toBounds);
265 mBoundsAnimator.start();
266 }
267 }
268 }
269
270 /**
Winson Chung655332c2016-10-31 13:14:28 -0700271 * Sends a broadcast that the PIP movement bounds have changed.
272 */
273 private void notifyBoundsChanged(boolean adjustedForIme) {
274 if (mPinnedStackListener != null) {
275 try {
276 mPinnedStackListener.onBoundsChanged(adjustedForIme);
277 } catch (RemoteException e) {
278 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
279 }
280 }
281 }
282
283 /**
284 * @return the bounds on the screen that the PIP can be visible in.
285 */
Winson Chung14fefc22016-11-02 10:02:29 -0700286 private void getInsetBounds(Rect outRect) {
287 mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth,
288 mDisplayInfo.logicalHeight, mTmpInsets);
289 outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y,
290 mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x,
291 mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y);
Winson Chung655332c2016-10-31 13:14:28 -0700292 }
293
294 /**
295 * @return the pixels for a given dp value.
296 */
297 private int dpToPx(float dpValue, DisplayMetrics dm) {
298 return (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, dpValue, dm);
299 }
300
301 void dump(String prefix, PrintWriter pw) {
302 pw.println(prefix + "PinnedStackController");
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700303 pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw);
304 pw.println();
305 mService.getStackBounds(PINNED_STACK_ID, mTmpRect);
306 pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw);
307 pw.println();
Winson Chung655332c2016-10-31 13:14:28 -0700308 pw.println(prefix + " mIsImeShowing=" + mIsImeShowing);
309 pw.println(prefix + " mInInteractiveMode=" + mInInteractiveMode);
310 }
311}