blob: bfb4269f7806ef8d90c2d97ff847845b0ce03777 [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;
117 });
118 }
119
120 @Override
Winson Chungdff5c082016-11-02 17:28:03 -0700121 public void setSnapToEdge(final boolean snapToEdge) {
122 mHandler.post(() -> {
Winson Chunga29eb982016-12-14 12:01:27 -0800123 mIsSnappingToEdge = snapToEdge;
Winson Chungdff5c082016-11-02 17:28:03 -0700124 mSnapAlgorithm.setSnapToEdge(snapToEdge);
125 });
126 }
Winson Chung655332c2016-10-31 13:14:28 -0700127 }
128
129 /**
130 * Handler for the case where the listener dies.
131 */
132 private class PinnedStackListenerDeathHandler implements IBinder.DeathRecipient {
133
134 @Override
135 public void binderDied() {
136 // Clean up the state if the listener dies
137 mInInteractiveMode = false;
138 mPinnedStackListener = null;
139 }
140 }
141
142 PinnedStackController(WindowManagerService service, DisplayContent displayContent) {
143 mService = service;
144 mDisplayContent = displayContent;
145 mSnapAlgorithm = new PipSnapAlgorithm(service.mContext);
Winson Chungb5c41b72016-12-07 15:00:47 -0800146 mMotionHelper = new PipMotionHelper(UiThread.getHandler());
Winson Chung14fefc22016-11-02 10:02:29 -0700147 mDisplayInfo.copyFrom(mDisplayContent.getDisplayInfo());
Winson Chung655332c2016-10-31 13:14:28 -0700148 reloadResources();
149 }
150
151 void onConfigurationChanged() {
152 reloadResources();
153 }
154
155 /**
156 * Reloads all the resources for the current configuration.
157 */
158 void reloadResources() {
159 final Resources res = mService.mContext.getResources();
160 final Size defaultSizeDp = Size.parseSize(res.getString(
161 com.android.internal.R.string.config_defaultPictureInPictureSize));
162 final Size screenEdgeInsetsDp = Size.parseSize(res.getString(
163 com.android.internal.R.string.config_defaultPictureInPictureScreenEdgeInsets));
164 mDefaultStackGravity = res.getInteger(
165 com.android.internal.R.integer.config_defaultPictureInPictureGravity);
166 mDisplayContent.getDisplay().getRealMetrics(mTmpMetrics);
167 mDefaultStackSize = new Size(dpToPx(defaultSizeDp.getWidth(), mTmpMetrics),
168 dpToPx(defaultSizeDp.getHeight(), mTmpMetrics));
169 mScreenEdgeInsets = new Point(dpToPx(screenEdgeInsetsDp.getWidth(), mTmpMetrics),
170 dpToPx(screenEdgeInsetsDp.getHeight(), mTmpMetrics));
171 }
172
173 /**
174 * Registers a pinned stack listener.
175 */
176 void registerPinnedStackListener(IPinnedStackListener listener) {
177 try {
178 listener.asBinder().linkToDeath(mPinnedStackListenerDeathHandler, 0);
179 listener.onListenerRegistered(mCallbacks);
180 mPinnedStackListener = listener;
181 notifyBoundsChanged(mIsImeShowing);
Winson Chunga29eb982016-12-14 12:01:27 -0800182 notifyMinimizeChanged(mIsMinimized);
183 notifySnapToEdgeChanged(mIsSnappingToEdge);
184 notifyActionsChanged(mActions);
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 Chung84a38342016-11-08 16:15:10 -0800191 * Returns the current bounds (or the default bounds if there are no current bounds) with the
192 * specified aspect ratio.
193 */
194 Rect getAspectRatioBounds(Rect stackBounds, float aspectRatio) {
195 // Save the snap fraction, calculate the aspect ratio based on the current bounds
196 final float snapFraction = mSnapAlgorithm.getSnapFraction(stackBounds,
197 getMovementBounds(stackBounds));
198 final float radius = PointF.length(stackBounds.width(), stackBounds.height());
199 final int height = (int) Math.round(Math.sqrt((radius * radius) /
200 (aspectRatio * aspectRatio + 1)));
201 final int width = Math.round(height * aspectRatio);
202 final int left = (int) (stackBounds.centerX() - width / 2f);
203 final int top = (int) (stackBounds.centerY() - height / 2f);
204 stackBounds.set(left, top, left + width, top + height);
205 mSnapAlgorithm.applySnapFraction(stackBounds, getMovementBounds(stackBounds), snapFraction);
206 return stackBounds;
207 }
208
209 /**
Winson Chung655332c2016-10-31 13:14:28 -0700210 * @return the default bounds to show the PIP when there is no active PIP.
211 */
212 Rect getDefaultBounds() {
Winson Chung655332c2016-10-31 13:14:28 -0700213 final Rect insetBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700214 getInsetBounds(insetBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700215
216 final Rect defaultBounds = new Rect();
217 Gravity.apply(mDefaultStackGravity, mDefaultStackSize.getWidth(),
218 mDefaultStackSize.getHeight(), insetBounds, 0, 0, defaultBounds);
219 return defaultBounds;
220 }
221
222 /**
223 * @return the movement bounds for the given {@param stackBounds} and the current state of the
224 * controller.
225 */
226 Rect getMovementBounds(Rect stackBounds) {
Winson Chung14fefc22016-11-02 10:02:29 -0700227 return getMovementBounds(stackBounds, true /* adjustForIme */);
228 }
229
230 /**
231 * @return the movement bounds for the given {@param stackBounds} and the current state of the
232 * controller.
233 */
234 Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
Winson Chung655332c2016-10-31 13:14:28 -0700235 final Rect movementBounds = new Rect();
Winson Chung14fefc22016-11-02 10:02:29 -0700236 getInsetBounds(movementBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700237
238 // Adjust the right/bottom to ensure the stack bounds never goes offscreen
239 movementBounds.right = Math.max(movementBounds.left, movementBounds.right -
240 stackBounds.width());
241 movementBounds.bottom = Math.max(movementBounds.top, movementBounds.bottom -
242 stackBounds.height());
243
Winson Chung14fefc22016-11-02 10:02:29 -0700244 // Apply the movement bounds adjustments based on the current state
245 if (adjustForIme) {
246 if (mIsImeShowing) {
247 movementBounds.bottom -= mImeHeight;
248 }
Winson Chung655332c2016-10-31 13:14:28 -0700249 }
Winson Chung655332c2016-10-31 13:14:28 -0700250 return movementBounds;
251 }
252
253 /**
Winson Chung114aeea2017-01-09 16:08:07 -0800254 * @return the repositioned PIP bounds given it's pre-change bounds, and the new display
255 * content.
Winson Chung655332c2016-10-31 13:14:28 -0700256 */
Winson Chung114aeea2017-01-09 16:08:07 -0800257 Rect onDisplayChanged(Rect preChangeStackBounds, DisplayContent displayContent) {
Winson Chung14fefc22016-11-02 10:02:29 -0700258 final Rect postChangeStackBounds = new Rect(preChangeStackBounds);
Winson Chung114aeea2017-01-09 16:08:07 -0800259 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
Winson Chung14fefc22016-11-02 10:02:29 -0700260 if (!mDisplayInfo.equals(displayInfo)) {
261 // Calculate the snap fraction of the current stack along the old movement bounds, and
262 // then update the stack bounds to the same fraction along the rotated movement bounds.
263 final Rect preChangeMovementBounds = getMovementBounds(preChangeStackBounds);
264 final float snapFraction = mSnapAlgorithm.getSnapFraction(preChangeStackBounds,
265 preChangeMovementBounds);
266 mDisplayInfo.copyFrom(displayInfo);
267
268 final Rect postChangeMovementBounds = getMovementBounds(preChangeStackBounds,
269 false /* adjustForIme */);
270 mSnapAlgorithm.applySnapFraction(postChangeStackBounds, postChangeMovementBounds,
271 snapFraction);
Winson Chungd5a01592016-11-11 16:25:04 -0800272 if (mIsMinimized) {
273 final Point displaySize = new Point(mDisplayInfo.logicalWidth,
274 mDisplayInfo.logicalHeight);
Winson Chung114aeea2017-01-09 16:08:07 -0800275 mService.getStableInsetsLocked(displayContent.getDisplayId(), mStableInsets);
Winson Chungd5a01592016-11-11 16:25:04 -0800276 mSnapAlgorithm.applyMinimizedOffset(postChangeStackBounds, postChangeMovementBounds,
Winson Chung114aeea2017-01-09 16:08:07 -0800277 displaySize, mStableInsets);
Winson Chungd5a01592016-11-11 16:25:04 -0800278 }
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
292 final Rect stackBounds = new Rect();
293 mService.getStackBounds(PINNED_STACK_ID, stackBounds);
294 final Rect prevMovementBounds = getMovementBounds(stackBounds);
Winson Chung655332c2016-10-31 13:14:28 -0700295 mIsImeShowing = adjustedForIme;
296 mImeHeight = imeHeight;
297 if (mInInteractiveMode) {
298 // If the user is currently interacting with the PIP and the ime state changes, then
299 // don't adjust the bounds and defer that to after the interaction
300 notifyBoundsChanged(adjustedForIme /* adjustedForIme */);
301 } else {
302 // Otherwise, we can move the PIP to a sane location to ensure that it does not block
303 // the user from interacting with the IME
Winson Chung14fefc22016-11-02 10:02:29 -0700304 final Rect movementBounds = getMovementBounds(stackBounds);
305 final Rect toBounds = new Rect(stackBounds);
306 if (adjustedForIme) {
307 // IME visible
Winson Chungd5a01592016-11-11 16:25:04 -0800308 if (stackBounds.top == prevMovementBounds.bottom) {
309 // If the PIP is resting on top of the IME, then adjust it with the hiding IME
310 toBounds.offsetTo(toBounds.left, movementBounds.bottom);
311 } else {
312 toBounds.offset(0, Math.min(0, movementBounds.bottom - stackBounds.top));
313 }
Winson Chung655332c2016-10-31 13:14:28 -0700314 } else {
Winson Chung14fefc22016-11-02 10:02:29 -0700315 // IME hidden
316 if (stackBounds.top == prevMovementBounds.bottom) {
317 // If the PIP is resting on top of the IME, then adjust it with the hiding IME
318 toBounds.offsetTo(toBounds.left, movementBounds.bottom);
319 }
Winson Chung655332c2016-10-31 13:14:28 -0700320 }
321 if (!toBounds.equals(stackBounds)) {
322 if (mBoundsAnimator != null) {
323 mBoundsAnimator.cancel();
324 }
325 mBoundsAnimator = mMotionHelper.createAnimationToBounds(stackBounds, toBounds);
326 mBoundsAnimator.start();
327 }
328 }
329 }
330
331 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800332 * Sets the current set of actions.
333 */
334 void setActions(List<RemoteAction> actions) {
335 mActions.clear();
336 mActions.addAll(actions);
337 notifyActionsChanged(mActions);
338 }
339
340 /**
341 * Notifies listeners that the PIP movement bounds have changed.
Winson Chung655332c2016-10-31 13:14:28 -0700342 */
343 private void notifyBoundsChanged(boolean adjustedForIme) {
344 if (mPinnedStackListener != null) {
345 try {
346 mPinnedStackListener.onBoundsChanged(adjustedForIme);
347 } catch (RemoteException e) {
348 Slog.e(TAG_WM, "Error delivering bounds changed event.", e);
349 }
350 }
351 }
352
353 /**
Winson Chunga29eb982016-12-14 12:01:27 -0800354 * Notifies listeners that the PIP minimized state has changed.
355 */
356 private void notifyMinimizeChanged(boolean isMinimized) {
357 if (mPinnedStackListener != null) {
358 try {
359 mPinnedStackListener.onMinimizedStateChanged(isMinimized);
360 } catch (RemoteException e) {
361 Slog.e(TAG_WM, "Error delivering minimize changed event.", e);
362 }
363 }
364 }
365
366 /**
367 * Notifies listeners that the PIP snap-to-edge state has changed.
368 */
369 private void notifySnapToEdgeChanged(boolean isSnappingToEdge) {
370 if (mPinnedStackListener != null) {
371 try {
372 mPinnedStackListener.onSnapToEdgeStateChanged(isSnappingToEdge);
373 } catch (RemoteException e) {
374 Slog.e(TAG_WM, "Error delivering snap-to-edge changed event.", e);
375 }
376 }
377 }
378
379 /**
380 * Notifies listeners that the PIP actions have changed.
381 */
382 private void notifyActionsChanged(List<RemoteAction> actions) {
383 if (mPinnedStackListener != null) {
384 try {
385 mPinnedStackListener.onActionsChanged(new ParceledListSlice(actions));
386 } catch (RemoteException e) {
387 Slog.e(TAG_WM, "Error delivering actions changed event.", e);
388 }
389 }
390 }
391
392 /**
Winson Chung655332c2016-10-31 13:14:28 -0700393 * @return the bounds on the screen that the PIP can be visible in.
394 */
Winson Chung14fefc22016-11-02 10:02:29 -0700395 private void getInsetBounds(Rect outRect) {
396 mService.mPolicy.getStableInsetsLw(mDisplayInfo.rotation, mDisplayInfo.logicalWidth,
397 mDisplayInfo.logicalHeight, mTmpInsets);
398 outRect.set(mTmpInsets.left + mScreenEdgeInsets.x, mTmpInsets.top + mScreenEdgeInsets.y,
399 mDisplayInfo.logicalWidth - mTmpInsets.right - mScreenEdgeInsets.x,
400 mDisplayInfo.logicalHeight - mTmpInsets.bottom - mScreenEdgeInsets.y);
Winson Chung655332c2016-10-31 13:14:28 -0700401 }
402
403 /**
404 * @return the pixels for a given dp value.
405 */
406 private int dpToPx(float dpValue, DisplayMetrics dm) {
407 return (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, dpValue, dm);
408 }
409
410 void dump(String prefix, PrintWriter pw) {
411 pw.println(prefix + "PinnedStackController");
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700412 pw.print(prefix + " defaultBounds="); getDefaultBounds().printShortString(pw);
413 pw.println();
414 mService.getStackBounds(PINNED_STACK_ID, mTmpRect);
415 pw.print(prefix + " movementBounds="); getMovementBounds(mTmpRect).printShortString(pw);
416 pw.println();
Winson Chung655332c2016-10-31 13:14:28 -0700417 pw.println(prefix + " mIsImeShowing=" + mIsImeShowing);
418 pw.println(prefix + " mInInteractiveMode=" + mInInteractiveMode);
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}