blob: 2cd2ef1203cb5560938d46cb28a9c652bdf76f84 [file] [log] [blame]
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001/*
2 * Copyright (C) 2012 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
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070019import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Wale Ogunwale68278562017-09-23 17:13:55 -070020import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Jorim Jaggi85639432016-05-06 17:27:55 -070021import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
22import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
23import static android.view.Surface.ROTATION_270;
24import static android.view.Surface.ROTATION_90;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010025import static android.view.WindowManager.DOCKED_BOTTOM;
Matthew Ngcb1b8e42017-10-20 16:29:23 -070026import static android.view.WindowManager.DOCKED_INVALID;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010027import static android.view.WindowManager.DOCKED_LEFT;
28import static android.view.WindowManager.DOCKED_RIGHT;
29import static android.view.WindowManager.DOCKED_TOP;
30import static com.android.server.wm.AppTransition.DEFAULT_APP_TRANSITION_DURATION;
31import static com.android.server.wm.AppTransition.TOUCH_RESPONSE_INTERPOLATOR;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010032import static android.view.WindowManager.TRANSIT_NONE;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010033import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
34import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Tony Mak853304c2016-04-18 15:17:41 +010035import static com.android.server.wm.WindowManagerService.H.NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070036import static com.android.server.wm.WindowManagerService.LAYER_OFFSET_DIM;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070037import static com.android.server.wm.DockedStackDividerControllerProto.MINIMIZED_DOCK;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010038
Filip Gruszczynski466f3212015-09-21 17:57:57 -070039import android.content.Context;
Jorim Jaggi85639432016-05-06 17:27:55 -070040import android.content.res.Configuration;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070041import android.graphics.Rect;
Jorim Jaggia6c934e2015-12-21 13:22:31 +010042import android.os.RemoteCallbackList;
Filip Gruszczynski64cdc142015-11-29 21:10:07 -080043import android.os.RemoteException;
Jorim Jaggi936aaeb2016-08-26 19:02:11 -070044import android.util.ArraySet;
Filip Gruszczynski77049052015-11-09 14:01:21 -080045import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -070046import android.util.proto.ProtoOutputStream;
Adrian Roos11c25582018-02-19 18:06:36 +010047import android.view.DisplayCutout;
Jorim Jaggi50981592015-12-29 17:54:12 +010048import android.view.DisplayInfo;
Jorim Jaggia6c934e2015-12-21 13:22:31 +010049import android.view.IDockedStackListener;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080050import android.view.animation.AnimationUtils;
51import android.view.animation.Interpolator;
Jorim Jaggieb88d832016-04-13 20:17:43 -070052import android.view.animation.PathInterpolator;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -070053import android.view.inputmethod.InputMethodManagerInternal;
Jorim Jaggi50981592015-12-29 17:54:12 +010054
Jorim Jaggi85639432016-05-06 17:27:55 -070055import com.android.internal.policy.DividerSnapAlgorithm;
56import com.android.internal.policy.DockedDividerUtils;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -070057import com.android.server.LocalServices;
Jorim Jaggiff71d202016-04-14 13:12:36 -070058import com.android.server.wm.WindowManagerService.H;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010059
Jorim Jaggi31f71702016-05-04 16:43:04 -070060import java.io.PrintWriter;
Chong Zhangbaba7832016-03-24 10:21:26 -070061
Filip Gruszczynski466f3212015-09-21 17:57:57 -070062/**
Jorim Jaggi61f39a72015-10-29 16:54:18 +010063 * Keeps information about the docked stack divider.
Filip Gruszczynski466f3212015-09-21 17:57:57 -070064 */
Robert Carrf59b8dd2017-10-02 18:58:36 -070065public class DockedStackDividerController {
Jorim Jaggi61f39a72015-10-29 16:54:18 +010066
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080067 private static final String TAG = TAG_WITH_CLASS_NAME ? "DockedStackDividerController" : TAG_WM;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010068
Jorim Jaggif97ed922016-02-18 18:57:07 -080069 /**
70 * The fraction during the maximize/clip reveal animation the divider meets the edge of the clip
71 * revealing surface at the earliest.
72 */
73 private static final float CLIP_REVEAL_MEET_EARLIEST = 0.6f;
74
75 /**
76 * The fraction during the maximize/clip reveal animation the divider meets the edge of the clip
77 * revealing surface at the latest.
78 */
79 private static final float CLIP_REVEAL_MEET_LAST = 1f;
80
81 /**
82 * If the app translates at least CLIP_REVEAL_MEET_FRACTION_MIN * minimize distance, we start
83 * meet somewhere between {@link #CLIP_REVEAL_MEET_LAST} and {@link #CLIP_REVEAL_MEET_EARLIEST}.
84 */
85 private static final float CLIP_REVEAL_MEET_FRACTION_MIN = 0.4f;
86
87 /**
88 * If the app translates equals or more than CLIP_REVEAL_MEET_FRACTION_MIN * minimize distance,
89 * we meet at {@link #CLIP_REVEAL_MEET_EARLIEST}.
90 */
91 private static final float CLIP_REVEAL_MEET_FRACTION_MAX = 0.8f;
92
Jorim Jaggieb88d832016-04-13 20:17:43 -070093 private static final Interpolator IME_ADJUST_ENTRY_INTERPOLATOR =
Jorim Jaggiff71d202016-04-14 13:12:36 -070094 new PathInterpolator(0.2f, 0f, 0.1f, 1f);
Jorim Jaggieb88d832016-04-13 20:17:43 -070095
Jorim Jaggi698e7632016-04-13 21:02:22 -070096 private static final long IME_ADJUST_ANIM_DURATION = 280;
Jorim Jaggieb88d832016-04-13 20:17:43 -070097
Jorim Jaggiff71d202016-04-14 13:12:36 -070098 private static final long IME_ADJUST_DRAWN_TIMEOUT = 200;
99
Chong Zhang198afac2016-04-15 12:03:11 -0700100 private static final int DIVIDER_WIDTH_INACTIVE_DP = 4;
101
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800102 private final WindowManagerService mService;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700103 private final DisplayContent mDisplayContent;
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700104 private int mDividerWindowWidth;
Chong Zhang198afac2016-04-15 12:03:11 -0700105 private int mDividerWindowWidthInactive;
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700106 private int mDividerInsets;
Matthew Nge15352e2016-12-20 15:36:29 -0800107 private int mTaskHeightInMinimizedMode;
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100108 private boolean mResizing;
109 private WindowState mWindow;
110 private final Rect mTmpRect = new Rect();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800111 private final Rect mTmpRect2 = new Rect();
Jorim Jaggi85639432016-05-06 17:27:55 -0700112 private final Rect mTmpRect3 = new Rect();
Filip Gruszczynski77049052015-11-09 14:01:21 -0800113 private final Rect mLastRect = new Rect();
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800114 private boolean mLastVisibility = false;
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100115 private final RemoteCallbackList<IDockedStackListener> mDockedStackListeners
116 = new RemoteCallbackList<>();
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700117
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800118 private boolean mMinimizedDock;
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700119 private int mOriginalDockedSide = DOCKED_INVALID;
Chong Zhangbaba7832016-03-24 10:21:26 -0700120 private boolean mAnimatingForMinimizedDockedStack;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800121 private boolean mAnimationStarted;
122 private long mAnimationStartTime;
123 private float mAnimationStart;
124 private float mAnimationTarget;
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800125 private long mAnimationDuration;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700126 private boolean mAnimationStartDelayed;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800127 private final Interpolator mMinimizedDockInterpolator;
Jorim Jaggif97ed922016-02-18 18:57:07 -0800128 private float mMaximizeMeetFraction;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100129 private final Rect mTouchRegion = new Rect();
Chong Zhangbaba7832016-03-24 10:21:26 -0700130 private boolean mAnimatingForIme;
131 private boolean mAdjustedForIme;
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700132 private int mImeHeight;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700133 private WindowState mDelayedImeWin;
Chong Zhangf347ab52016-04-18 21:02:01 -0700134 private boolean mAdjustedForDivider;
135 private float mDividerAnimationStart;
136 private float mDividerAnimationTarget;
Wale Ogunwale10124582016-09-15 20:25:50 -0700137 float mLastAnimationProgress;
138 float mLastDividerProgress;
Jorim Jaggi85639432016-05-06 17:27:55 -0700139 private final DividerSnapAlgorithm[] mSnapAlgorithmForRotation = new DividerSnapAlgorithm[4];
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700140 private boolean mImeHideRequested;
Jorim Jaggif93ac2b2017-10-25 15:20:24 +0200141 private final Rect mLastDimLayerRect = new Rect();
142 private float mLastDimLayerAlpha;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700143 private TaskStack mDimmedStack;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800144
145 DockedStackDividerController(WindowManagerService service, DisplayContent displayContent) {
146 mService = service;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700147 mDisplayContent = displayContent;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800148 final Context context = service.mContext;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800149 mMinimizedDockInterpolator = AnimationUtils.loadInterpolator(
150 context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700151 loadDimens();
152 }
153
Jorim Jaggi85639432016-05-06 17:27:55 -0700154 int getSmallestWidthDpForBounds(Rect bounds) {
155 final DisplayInfo di = mDisplayContent.getDisplayInfo();
156
Jorim Jaggi85639432016-05-06 17:27:55 -0700157 final int baseDisplayWidth = mDisplayContent.mBaseDisplayWidth;
158 final int baseDisplayHeight = mDisplayContent.mBaseDisplayHeight;
159 int minWidth = Integer.MAX_VALUE;
160
161 // Go through all screen orientations and find the orientation in which the task has the
162 // smallest width.
163 for (int rotation = 0; rotation < 4; rotation++) {
164 mTmpRect.set(bounds);
165 mDisplayContent.rotateBounds(di.rotation, rotation, mTmpRect);
166 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
167 mTmpRect2.set(0, 0,
168 rotated ? baseDisplayHeight : baseDisplayWidth,
169 rotated ? baseDisplayWidth : baseDisplayHeight);
170 final int orientation = mTmpRect2.width() <= mTmpRect2.height()
171 ? ORIENTATION_PORTRAIT
172 : ORIENTATION_LANDSCAPE;
173 final int dockSide = TaskStack.getDockSideUnchecked(mTmpRect, mTmpRect2, orientation);
174 final int position = DockedDividerUtils.calculatePositionForBounds(mTmpRect, dockSide,
175 getContentWidth());
176
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100177 final DisplayCutout displayCutout = mDisplayContent.calculateDisplayCutoutForRotation(
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100178 rotation).getDisplayCutout();
Adrian Roos11c25582018-02-19 18:06:36 +0100179
Jorim Jaggi85639432016-05-06 17:27:55 -0700180 // Since we only care about feasible states, snap to the closest snap target, like it
181 // would happen when actually rotating the screen.
182 final int snappedPosition = mSnapAlgorithmForRotation[rotation]
183 .calculateNonDismissingSnapTarget(position).position;
184 DockedDividerUtils.calculateBoundsForPosition(snappedPosition, dockSide, mTmpRect,
185 mTmpRect2.width(), mTmpRect2.height(), getContentWidth());
186 mService.mPolicy.getStableInsetsLw(rotation, mTmpRect2.width(), mTmpRect2.height(),
Adrian Roos11c25582018-02-19 18:06:36 +0100187 displayCutout, mTmpRect3);
Winson Chungbdc646f2017-02-13 12:12:22 -0800188 mService.intersectDisplayInsetBounds(mTmpRect2, mTmpRect3, mTmpRect);
Jorim Jaggi85639432016-05-06 17:27:55 -0700189 minWidth = Math.min(mTmpRect.width(), minWidth);
190 }
191 return (int) (minWidth / mDisplayContent.getDisplayMetrics().density);
192 }
193
Matthew Nge15352e2016-12-20 15:36:29 -0800194 void getHomeStackBoundsInDockedMode(Rect outBounds) {
195 final DisplayInfo di = mDisplayContent.getDisplayInfo();
196 mService.mPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
Adrian Roos11c25582018-02-19 18:06:36 +0100197 di.displayCutout, mTmpRect);
Matthew Nge15352e2016-12-20 15:36:29 -0800198 int dividerSize = mDividerWindowWidth - 2 * mDividerInsets;
199 Configuration configuration = mDisplayContent.getConfiguration();
Matthew Nga9e173d2017-05-17 15:03:18 -0700200 // The offset in the left (landscape)/top (portrait) is calculated with the minimized
201 // offset value with the divider size and any system insets in that direction.
Matthew Nge15352e2016-12-20 15:36:29 -0800202 if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
203 outBounds.set(0, mTaskHeightInMinimizedMode + dividerSize + mTmpRect.top,
204 di.logicalWidth, di.logicalHeight);
205 } else {
Matthew Nga9e173d2017-05-17 15:03:18 -0700206 // In landscape append the left position with the statusbar height to match the
207 // minimized size height in portrait mode.
208 outBounds.set(mTaskHeightInMinimizedMode + dividerSize + mTmpRect.left + mTmpRect.top,
209 0, di.logicalWidth, di.logicalHeight);
Matthew Nge15352e2016-12-20 15:36:29 -0800210 }
211 }
212
213 boolean isHomeStackResizable() {
214 final TaskStack homeStack = mDisplayContent.getHomeStack();
215 if (homeStack == null) {
216 return false;
217 }
218 final Task homeTask = homeStack.findHomeTask();
219 return homeTask != null && homeTask.isResizeable();
220 }
221
Jorim Jaggi85639432016-05-06 17:27:55 -0700222 private void initSnapAlgorithmForRotations() {
Andrii Kulian441e4492016-09-29 15:25:00 -0700223 final Configuration baseConfig = mDisplayContent.getConfiguration();
Jorim Jaggi85639432016-05-06 17:27:55 -0700224
225 // Initialize the snap algorithms for all 4 screen orientations.
226 final Configuration config = new Configuration();
227 for (int rotation = 0; rotation < 4; rotation++) {
228 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
229 final int dw = rotated
230 ? mDisplayContent.mBaseDisplayHeight
231 : mDisplayContent.mBaseDisplayWidth;
232 final int dh = rotated
233 ? mDisplayContent.mBaseDisplayWidth
234 : mDisplayContent.mBaseDisplayHeight;
Adrian Roos11c25582018-02-19 18:06:36 +0100235 final DisplayCutout displayCutout =
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100236 mDisplayContent.calculateDisplayCutoutForRotation(rotation).getDisplayCutout();
Adrian Roos11c25582018-02-19 18:06:36 +0100237 mService.mPolicy.getStableInsetsLw(rotation, dw, dh, displayCutout, mTmpRect);
Andrii Kulianb10330d2016-09-16 13:51:46 -0700238 config.unset();
Jorim Jaggi85639432016-05-06 17:27:55 -0700239 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Bryce Lee7566d762017-03-30 09:34:15 -0700240
241 final int displayId = mDisplayContent.getDisplayId();
242 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +0100243 baseConfig.uiMode, displayId, displayCutout);
Bryce Lee7566d762017-03-30 09:34:15 -0700244 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roosbed538e2018-02-21 17:50:07 +0100245 baseConfig.uiMode, displayId, displayCutout);
Adrian Roos11c25582018-02-19 18:06:36 +0100246 mService.mPolicy.getNonDecorInsetsLw(rotation, dw, dh, displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -0700247 final int leftInset = mTmpRect.left;
248 final int topInset = mTmpRect.top;
249
Wale Ogunwale822e5122017-07-26 06:02:24 -0700250 config.windowConfiguration.setAppBounds(leftInset /*left*/, topInset /*top*/,
251 leftInset + appWidth /*right*/, topInset + appHeight /*bottom*/);
Bryce Lee7566d762017-03-30 09:34:15 -0700252
Adrian Roos11c25582018-02-19 18:06:36 +0100253 final float density = mDisplayContent.getDisplayMetrics().density;
254 config.screenWidthDp = (int) (mService.mPolicy.getConfigDisplayWidth(dw, dh,
255 rotation, baseConfig.uiMode, displayId, displayCutout) / density);
256 config.screenHeightDp = (int) (mService.mPolicy.getConfigDisplayHeight(dw, dh,
257 rotation, baseConfig.uiMode, displayId, displayCutout) / density);
Jorim Jaggi85639432016-05-06 17:27:55 -0700258 final Context rotationContext = mService.mContext.createConfigurationContext(config);
259 mSnapAlgorithmForRotation[rotation] = new DividerSnapAlgorithm(
260 rotationContext.getResources(), dw, dh, getContentWidth(),
261 config.orientation == ORIENTATION_PORTRAIT, mTmpRect);
262 }
263 }
264
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700265 private void loadDimens() {
266 final Context context = mService.mContext;
267 mDividerWindowWidth = context.getResources().getDimensionPixelSize(
268 com.android.internal.R.dimen.docked_stack_divider_thickness);
269 mDividerInsets = context.getResources().getDimensionPixelSize(
270 com.android.internal.R.dimen.docked_stack_divider_insets);
Chong Zhang198afac2016-04-15 12:03:11 -0700271 mDividerWindowWidthInactive = WindowManagerService.dipToPixel(
272 DIVIDER_WIDTH_INACTIVE_DP, mDisplayContent.getDisplayMetrics());
Matthew Nge15352e2016-12-20 15:36:29 -0800273 mTaskHeightInMinimizedMode = context.getResources().getDimensionPixelSize(
274 com.android.internal.R.dimen.task_height_of_minimized_mode);
Jorim Jaggi85639432016-05-06 17:27:55 -0700275 initSnapAlgorithmForRotations();
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700276 }
277
278 void onConfigurationChanged() {
279 loadDimens();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700280 }
281
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100282 boolean isResizing() {
283 return mResizing;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700284 }
285
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100286 int getContentWidth() {
287 return mDividerWindowWidth - 2 * mDividerInsets;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700288 }
289
Jorim Jaggi81ba11e2016-02-03 22:04:22 -0800290 int getContentInsets() {
291 return mDividerInsets;
292 }
293
Chong Zhang198afac2016-04-15 12:03:11 -0700294 int getContentWidthInactive() {
295 return mDividerWindowWidthInactive;
296 }
297
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100298 void setResizing(boolean resizing) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800299 if (mResizing != resizing) {
300 mResizing = resizing;
301 resetDragResizingChangeReported();
302 }
303 }
304
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100305 void setTouchRegion(Rect touchRegion) {
306 mTouchRegion.set(touchRegion);
307 }
308
309 void getTouchRegion(Rect outRegion) {
310 outRegion.set(mTouchRegion);
311 outRegion.offset(mWindow.getFrameLw().left, mWindow.getFrameLw().top);
312 }
313
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800314 private void resetDragResizingChangeReported() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800315 mDisplayContent.forAllWindows(WindowState::resetDragResizingChangeReported,
316 true /* traverseTopToBottom */ );
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100317 }
318
319 void setWindow(WindowState window) {
320 mWindow = window;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800321 reevaluateVisibility(false);
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100322 }
323
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800324 void reevaluateVisibility(boolean force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800325 if (mWindow == null) {
326 return;
327 }
Matthew Ng64e77cf2017-10-31 14:01:31 -0700328 TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Jorim Jaggi7998e482016-02-12 18:47:06 -0800329
330 // If the stack is invisible, we policy force hide it in WindowAnimator.shouldForceHide
331 final boolean visible = stack != null;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800332 if (mLastVisibility == visible && !force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800333 return;
334 }
335 mLastVisibility = visible;
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100336 notifyDockedDividerVisibilityChanged(visible);
Jorim Jaggi50981592015-12-29 17:54:12 +0100337 if (!visible) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700338 setResizeDimLayer(false, WINDOWING_MODE_UNDEFINED, 0f);
Jorim Jaggi50981592015-12-29 17:54:12 +0100339 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100340 }
341
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700342 private boolean wasVisible() {
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100343 return mLastVisibility;
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100344 }
345
Chong Zhangf347ab52016-04-18 21:02:01 -0700346 void setAdjustedForIme(
347 boolean adjustedForIme, boolean adjustedForDivider,
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700348 boolean animate, WindowState imeWin, int imeHeight) {
349 if (mAdjustedForIme != adjustedForIme || (adjustedForIme && mImeHeight != imeHeight)
350 || mAdjustedForDivider != adjustedForDivider) {
Jorim Jaggi3070e772016-05-17 16:41:32 -0700351 if (animate && !mAnimatingForMinimizedDockedStack) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700352 startImeAdjustAnimation(adjustedForIme, adjustedForDivider, imeWin);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700353 } else {
Jorim Jaggiff71d202016-04-14 13:12:36 -0700354 // Animation might be delayed, so only notify if we don't run an animation.
Chong Zhangf347ab52016-04-18 21:02:01 -0700355 notifyAdjustedForImeChanged(adjustedForIme || adjustedForDivider, 0 /* duration */);
Jorim Jaggieb88d832016-04-13 20:17:43 -0700356 }
Chong Zhangf347ab52016-04-18 21:02:01 -0700357 mAdjustedForIme = adjustedForIme;
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700358 mImeHeight = imeHeight;
Chong Zhangf347ab52016-04-18 21:02:01 -0700359 mAdjustedForDivider = adjustedForDivider;
Chong Zhangbaba7832016-03-24 10:21:26 -0700360 }
Chong Zhangb58bbcc2016-03-23 11:57:36 -0700361 }
362
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700363 int getImeHeightAdjustedFor() {
364 return mImeHeight;
365 }
366
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700367 void positionDockedStackedDivider(Rect frame) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700368 TaskStack stack = mDisplayContent.getSplitScreenPrimaryStack();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700369 if (stack == null) {
370 // Unfortunately we might end up with still having a divider, even though the underlying
371 // stack was already removed. This is because we are on AM thread and the removal of the
Filip Gruszczynski77049052015-11-09 14:01:21 -0800372 // divider was deferred to WM thread and hasn't happened yet. In that case let's just
373 // keep putting it in the same place it was before the stack was removed to have
374 // continuity and prevent it from jumping to the center. It will get hidden soon.
375 frame.set(mLastRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700376 return;
Filip Gruszczynski77049052015-11-09 14:01:21 -0800377 } else {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800378 stack.getDimBounds(mTmpRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700379 }
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100380 int side = stack.getDockSide();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700381 switch (side) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700382 case DOCKED_LEFT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100383 frame.set(mTmpRect.right - mDividerInsets, frame.top,
384 mTmpRect.right + frame.width() - mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700385 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700386 case DOCKED_TOP:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100387 frame.set(frame.left, mTmpRect.bottom - mDividerInsets,
388 mTmpRect.right, mTmpRect.bottom + frame.height() - mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700389 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700390 case DOCKED_RIGHT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100391 frame.set(mTmpRect.left - frame.width() + mDividerInsets, frame.top,
392 mTmpRect.left + mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700393 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700394 case DOCKED_BOTTOM:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100395 frame.set(frame.left, mTmpRect.top - frame.height() + mDividerInsets,
396 frame.right, mTmpRect.top + mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700397 break;
398 }
Filip Gruszczynski77049052015-11-09 14:01:21 -0800399 mLastRect.set(frame);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700400 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800401
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700402 private void notifyDockedDividerVisibilityChanged(boolean visible) {
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100403 final int size = mDockedStackListeners.beginBroadcast();
404 for (int i = 0; i < size; ++i) {
405 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
406 try {
407 listener.onDividerVisibilityChanged(visible);
408 } catch (RemoteException e) {
409 Slog.e(TAG_WM, "Error delivering divider visibility changed event.", e);
410 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800411 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100412 mDockedStackListeners.finishBroadcast();
413 }
414
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700415 /**
416 * Checks if the primary stack is allowed to dock to a specific side based on its original dock
417 * side.
418 *
419 * @param dockSide the side to see if it is valid
420 * @return true if the side provided is valid
421 */
422 boolean canPrimaryStackDockTo(int dockSide) {
423 if (mService.mPolicy.isDockSideAllowed(dockSide)) {
424 // Side is the same as original side
425 if (dockSide == mOriginalDockedSide) {
426 return true;
427 }
428 // Special rule that the top in portrait is always valid
429 if (dockSide == DOCKED_TOP) {
430 return true;
431 }
432 // Only if original docked side was top in portrait will allow left side for landscape
433 if (dockSide == DOCKED_LEFT && mOriginalDockedSide == DOCKED_TOP) {
434 return true;
435 }
436 }
437 return false;
438 }
439
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100440 void notifyDockedStackExistsChanged(boolean exists) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700441 // TODO(multi-display): Perform all actions only for current display.
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100442 final int size = mDockedStackListeners.beginBroadcast();
443 for (int i = 0; i < size; ++i) {
444 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
445 try {
446 listener.onDockedStackExistsChanged(exists);
447 } catch (RemoteException e) {
448 Slog.e(TAG_WM, "Error delivering docked stack exists changed event.", e);
449 }
450 }
451 mDockedStackListeners.finishBroadcast();
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700452 if (exists) {
453 InputMethodManagerInternal inputMethodManagerInternal =
454 LocalServices.getService(InputMethodManagerInternal.class);
455 if (inputMethodManagerInternal != null) {
456
457 // Hide the current IME to avoid problems with animations from IME adjustment when
458 // attaching the docked stack.
459 inputMethodManagerInternal.hideCurrentInputMethod();
460 mImeHideRequested = true;
461 }
Matthew Nge8b052a2018-01-16 14:33:47 -0800462
463 // If a primary stack was just created, it will not have access to display content at
464 // this point so pass it from here to get a valid dock side.
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700465 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Matthew Nge8b052a2018-01-16 14:33:47 -0800466 mOriginalDockedSide = stack.getDockSideForDisplay(mDisplayContent);
Matthew Ngbc527a32017-06-19 16:42:31 -0700467 return;
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700468 }
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700469 mOriginalDockedSide = DOCKED_INVALID;
Matthew Ngbc527a32017-06-19 16:42:31 -0700470 setMinimizedDockedStack(false /* minimizedDock */, false /* animate */);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700471
472 if (mDimmedStack != null) {
473 mDimmedStack.stopDimming();
474 mDimmedStack = null;
475 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100476 }
477
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700478 /**
479 * Resets the state that IME hide has been requested. See {@link #isImeHideRequested}.
480 */
481 void resetImeHideRequested() {
482 mImeHideRequested = false;
483 }
484
485 /**
486 * The docked stack divider controller makes sure the IME gets hidden when attaching the docked
487 * stack, to avoid animation problems. This flag indicates whether the request to hide the IME
488 * has been sent in an asynchronous manner, and the IME should be treated as hidden already.
489 *
490 * @return whether IME hide request has been sent
491 */
492 boolean isImeHideRequested() {
493 return mImeHideRequested;
494 }
495
Matthew Nge15352e2016-12-20 15:36:29 -0800496 private void notifyDockedStackMinimizedChanged(boolean minimizedDock, boolean animate,
497 boolean isHomeStackResizable) {
498 long animDuration = 0;
499 if (animate) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -0700500 final TaskStack stack =
Matthew Ng64e77cf2017-10-31 14:01:31 -0700501 mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Matthew Nge15352e2016-12-20 15:36:29 -0800502 final long transitionDuration = isAnimationMaximizing()
503 ? mService.mAppTransition.getLastClipRevealTransitionDuration()
504 : DEFAULT_APP_TRANSITION_DURATION;
505 mAnimationDuration = (long)
506 (transitionDuration * mService.getTransitionAnimationScaleLocked());
507 mMaximizeMeetFraction = getClipRevealMeetFraction(stack);
508 animDuration = (long) (mAnimationDuration * mMaximizeMeetFraction);
509 }
Tony Mak853304c2016-04-18 15:17:41 +0100510 mService.mH.removeMessages(NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED);
511 mService.mH.obtainMessage(NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED,
512 minimizedDock ? 1 : 0, 0).sendToTarget();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800513 final int size = mDockedStackListeners.beginBroadcast();
514 for (int i = 0; i < size; ++i) {
515 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
516 try {
Matthew Nge15352e2016-12-20 15:36:29 -0800517 listener.onDockedStackMinimizedChanged(minimizedDock, animDuration,
518 isHomeStackResizable);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800519 } catch (RemoteException e) {
520 Slog.e(TAG_WM, "Error delivering minimized dock changed event.", e);
521 }
522 }
523 mDockedStackListeners.finishBroadcast();
524 }
525
Jorim Jaggi2917dc42016-04-11 11:39:13 -0700526 void notifyDockSideChanged(int newDockSide) {
527 final int size = mDockedStackListeners.beginBroadcast();
528 for (int i = 0; i < size; ++i) {
529 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
530 try {
531 listener.onDockSideChanged(newDockSide);
532 } catch (RemoteException e) {
533 Slog.e(TAG_WM, "Error delivering dock side changed event.", e);
534 }
535 }
536 mDockedStackListeners.finishBroadcast();
537 }
538
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700539 private void notifyAdjustedForImeChanged(boolean adjustedForIme, long animDuration) {
Jorim Jaggi698e7632016-04-13 21:02:22 -0700540 final int size = mDockedStackListeners.beginBroadcast();
541 for (int i = 0; i < size; ++i) {
542 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
543 try {
544 listener.onAdjustedForImeChanged(adjustedForIme, animDuration);
545 } catch (RemoteException e) {
546 Slog.e(TAG_WM, "Error delivering adjusted for ime changed event.", e);
547 }
548 }
549 mDockedStackListeners.finishBroadcast();
550 }
551
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100552 void registerDockedStackListener(IDockedStackListener listener) {
553 mDockedStackListeners.register(listener);
554 notifyDockedDividerVisibilityChanged(wasVisible());
Wale Ogunwale61911492017-10-11 08:50:50 -0700555 notifyDockedStackExistsChanged(
Matthew Ng64e77cf2017-10-31 14:01:31 -0700556 mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility() != null);
Matthew Nge15352e2016-12-20 15:36:29 -0800557 notifyDockedStackMinimizedChanged(mMinimizedDock, false /* animate */,
558 isHomeStackResizable());
Jorim Jaggi698e7632016-04-13 21:02:22 -0700559 notifyAdjustedForImeChanged(mAdjustedForIme, 0 /* animDuration */);
560
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800561 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100562
Wale Ogunwale68278562017-09-23 17:13:55 -0700563 /**
564 * Shows a dim layer with {@param alpha} if {@param visible} is true and
565 * {@param targetWindowingMode} isn't
566 * {@link android.app.WindowConfiguration#WINDOWING_MODE_UNDEFINED} and there is a stack on the
567 * display in that windowing mode.
568 */
569 void setResizeDimLayer(boolean visible, int targetWindowingMode, float alpha) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700570 // TODO: Maybe only allow split-screen windowing modes?
571 final TaskStack stack = targetWindowingMode != WINDOWING_MODE_UNDEFINED
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800572 ? mDisplayContent.getTopStackInWindowingMode(targetWindowingMode)
Wale Ogunwale68278562017-09-23 17:13:55 -0700573 : null;
Matthew Ng64e77cf2017-10-31 14:01:31 -0700574 final TaskStack dockedStack = mDisplayContent.getSplitScreenPrimaryStack();
Jorim Jaggibc5425c2016-03-01 13:51:16 +0100575 boolean visibleAndValid = visible && stack != null && dockedStack != null;
chaviw783729c2018-01-25 15:14:10 -0800576
577 // Ensure an old dim that was shown for the docked stack divider is removed so we don't end
578 // up with dim layers that can no longer be removed.
579 if (mDimmedStack != null && mDimmedStack != stack) {
580 mDimmedStack.stopDimming();
581 mDimmedStack = null;
582 }
583
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100584 if (visibleAndValid) {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700585 mDimmedStack = stack;
586 stack.dim(alpha);
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100587 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700588 if (!visibleAndValid && stack != null) {
589 mDimmedStack = null;
590 stack.stopDimming();
Jorim Jaggi50981592015-12-29 17:54:12 +0100591 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100592 }
593
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800594 /**
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700595 * @return The layer used for dimming the apps when dismissing docked/fullscreen stack. Just
596 * above all application surfaces.
597 */
598 private int getResizeDimLayer() {
599 return (mWindow != null) ? mWindow.mLayer - 1 : LAYER_OFFSET_DIM;
600 }
601
602 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800603 * Notifies the docked stack divider controller of a visibility change that happens without
604 * an animation.
605 */
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700606 void notifyAppVisibilityChanged() {
607 checkMinimizeChanged(false /* animate */);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800608 }
609
Jorim Jaggi84afb1a2016-09-28 14:54:04 +0200610 void notifyAppTransitionStarting(ArraySet<AppWindowToken> openingApps, int appTransition) {
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700611 final boolean wasMinimized = mMinimizedDock;
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700612 checkMinimizeChanged(true /* animate */);
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700613
614 // We were minimized, and now we are still minimized, but somebody is trying to launch an
Matthew Ng8e265522017-02-13 11:09:37 -0800615 // app in docked stack, better show recent apps so we actually get unminimized! However do
616 // not do this if keyguard is dismissed such as when the device is unlocking. This catches
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700617 // any case that was missed in ActivityStarter.postStartActivityUncheckedProcessing because
618 // we couldn't retrace the launch of the app in the docked stack to the launch from
619 // homescreen.
Jorim Jaggi84afb1a2016-09-28 14:54:04 +0200620 if (wasMinimized && mMinimizedDock && containsAppInDockedStack(openingApps)
Matthew Ng8e265522017-02-13 11:09:37 -0800621 && appTransition != TRANSIT_NONE &&
622 !AppTransition.isKeyguardGoingAwayTransit(appTransition)) {
Winson Chungc1674272018-02-21 10:15:17 -0800623 if (mService.mAmInternal.isRecentsComponentHomeActivity(mService.mCurrentUserId)) {
624 // When the home activity is the recents component and we are already minimized,
625 // then there is nothing to do here since home is already visible
626 } else {
627 mService.showRecentApps();
628 }
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700629 }
630 }
631
632 /**
633 * @return true if {@param apps} contains an activity in the docked stack, false otherwise.
634 */
635 private boolean containsAppInDockedStack(ArraySet<AppWindowToken> apps) {
636 for (int i = apps.size() - 1; i >= 0; i--) {
637 final AppWindowToken token = apps.valueAt(i);
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700638 if (token.getTask() != null && token.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700639 return true;
640 }
641 }
642 return false;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800643 }
644
Jorim Jaggi1ae68bf2016-05-09 18:44:34 -0700645 boolean isMinimizedDock() {
646 return mMinimizedDock;
647 }
648
Winson Chungc1674272018-02-21 10:15:17 -0800649 void checkMinimizeChanged(boolean animate) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700650 if (mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility() == null) {
Jorim Jaggi817a5242016-05-06 15:45:00 -0700651 return;
652 }
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700653 final TaskStack homeStack = mDisplayContent.getHomeStack();
654 if (homeStack == null) {
655 return;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800656 }
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700657 final Task homeTask = homeStack.findHomeTask();
658 if (homeTask == null || !isWithinDisplay(homeTask)) {
659 return;
660 }
Matthew Ng8e265522017-02-13 11:09:37 -0800661
662 // Do not minimize when dock is already minimized while keyguard is showing and not
663 // occluded such as unlocking the screen
664 if (mMinimizedDock && mService.mPolicy.isKeyguardShowingAndNotOccluded()) {
665 return;
666 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800667 final TaskStack topSecondaryStack = mDisplayContent.getTopStackInWindowingMode(
Wale Ogunwaleb62139d2017-09-20 15:37:35 -0700668 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
Winson Chungf557c3b2018-03-16 10:55:20 -0700669 final RecentsAnimationController recentsAnim = mService.getRecentsAnimationController();
670 final boolean minimizedForRecentsAnimation = recentsAnim != null &&
671 recentsAnim.isSplitScreenMinimized();
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800672 boolean homeVisible = homeTask.getTopVisibleAppToken() != null;
673 if (homeVisible && topSecondaryStack != null) {
674 // Home should only be considered visible if it is greater or equal to the top secondary
675 // stack in terms of z-order.
676 homeVisible = homeStack.compareTo(topSecondaryStack) >= 0;
677 }
Winson Chungf557c3b2018-03-16 10:55:20 -0700678 setMinimizedDockedStack(homeVisible || minimizedForRecentsAnimation, animate);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800679 }
680
681 private boolean isWithinDisplay(Task task) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800682 task.getBounds(mTmpRect);
683 mDisplayContent.getBounds(mTmpRect2);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800684 return mTmpRect.intersect(mTmpRect2);
685 }
686
687 /**
688 * Sets whether the docked stack is currently in a minimized state, i.e. all the tasks in the
689 * docked stack are heavily clipped so you can only see a minimal peek state.
690 *
691 * @param minimizedDock Whether the docked stack is currently minimized.
692 * @param animate Whether to animate the change.
693 */
694 private void setMinimizedDockedStack(boolean minimizedDock, boolean animate) {
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700695 final boolean wasMinimized = mMinimizedDock;
696 mMinimizedDock = minimizedDock;
Jorim Jaggi817a5242016-05-06 15:45:00 -0700697 if (minimizedDock == wasMinimized) {
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800698 return;
699 }
700
Chong Zhang22eff0a2016-07-01 14:48:11 -0700701 final boolean imeChanged = clearImeAdjustAnimation();
702 boolean minimizedChange = false;
Matthew Nge15352e2016-12-20 15:36:29 -0800703 if (isHomeStackResizable()) {
Winson Chungc1674272018-02-21 10:15:17 -0800704 notifyDockedStackMinimizedChanged(minimizedDock, animate,
Matthew Nge15352e2016-12-20 15:36:29 -0800705 true /* isHomeStackResizable */);
706 minimizedChange = true;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800707 } else {
Matthew Nge15352e2016-12-20 15:36:29 -0800708 if (minimizedDock) {
709 if (animate) {
710 startAdjustAnimation(0f, 1f);
711 } else {
712 minimizedChange |= setMinimizedDockedStack(true);
713 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800714 } else {
Matthew Nge15352e2016-12-20 15:36:29 -0800715 if (animate) {
716 startAdjustAnimation(1f, 0f);
717 } else {
718 minimizedChange |= setMinimizedDockedStack(false);
719 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800720 }
721 }
Chong Zhang22eff0a2016-07-01 14:48:11 -0700722 if (imeChanged || minimizedChange) {
723 if (imeChanged && !minimizedChange) {
724 Slog.d(TAG, "setMinimizedDockedStack: IME adjust changed due to minimizing,"
725 + " minimizedDock=" + minimizedDock
726 + " minimizedChange=" + minimizedChange);
727 }
728 mService.mWindowPlacerLocked.performSurfacePlacement();
729 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800730 }
731
Chong Zhang22eff0a2016-07-01 14:48:11 -0700732 private boolean clearImeAdjustAnimation() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700733 final boolean changed = mDisplayContent.clearImeAdjustAnimation();
Jorim Jaggieb88d832016-04-13 20:17:43 -0700734 mAnimatingForIme = false;
Chong Zhang22eff0a2016-07-01 14:48:11 -0700735 return changed;
Jorim Jaggieb88d832016-04-13 20:17:43 -0700736 }
737
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800738 private void startAdjustAnimation(float from, float to) {
Chong Zhangbaba7832016-03-24 10:21:26 -0700739 mAnimatingForMinimizedDockedStack = true;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800740 mAnimationStarted = false;
741 mAnimationStart = from;
742 mAnimationTarget = to;
743 }
744
Chong Zhangf347ab52016-04-18 21:02:01 -0700745 private void startImeAdjustAnimation(
746 boolean adjustedForIme, boolean adjustedForDivider, WindowState imeWin) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700747
748 // If we're not in an animation, the starting point depends on whether we're adjusted
749 // or not. If we're already in an animation, we start from where the current animation
750 // left off, so that the motion doesn't look discontinuous.
751 if (!mAnimatingForIme) {
752 mAnimationStart = mAdjustedForIme ? 1 : 0;
753 mDividerAnimationStart = mAdjustedForDivider ? 1 : 0;
754 mLastAnimationProgress = mAnimationStart;
755 mLastDividerProgress = mDividerAnimationStart;
756 } else {
757 mAnimationStart = mLastAnimationProgress;
758 mDividerAnimationStart = mLastDividerProgress;
759 }
Jorim Jaggi3070e772016-05-17 16:41:32 -0700760 mAnimatingForIme = true;
761 mAnimationStarted = false;
Chong Zhangf347ab52016-04-18 21:02:01 -0700762 mAnimationTarget = adjustedForIme ? 1 : 0;
763 mDividerAnimationTarget = adjustedForDivider ? 1 : 0;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700764
Wale Ogunwale10124582016-09-15 20:25:50 -0700765 mDisplayContent.beginImeAdjustAnimation();
Jorim Jaggiff71d202016-04-14 13:12:36 -0700766
767 // We put all tasks into drag resizing mode - wait until all of them have completed the
768 // drag resizing switch.
769 if (!mService.mWaitingForDrawn.isEmpty()) {
770 mService.mH.removeMessages(H.WAITING_FOR_DRAWN_TIMEOUT);
771 mService.mH.sendEmptyMessageDelayed(H.WAITING_FOR_DRAWN_TIMEOUT,
772 IME_ADJUST_DRAWN_TIMEOUT);
773 mAnimationStartDelayed = true;
774 if (imeWin != null) {
775
776 // There might be an old window delaying the animation start - clear it.
777 if (mDelayedImeWin != null) {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100778 mDelayedImeWin.endDelayingAnimationStart();
Jorim Jaggiff71d202016-04-14 13:12:36 -0700779 }
780 mDelayedImeWin = imeWin;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100781 imeWin.startDelayingAnimationStart();
Jorim Jaggiff71d202016-04-14 13:12:36 -0700782 }
Jorim Jaggi31883862016-11-04 15:45:30 -0700783
784 // If we are already waiting for something to be drawn, clear out the old one so it
785 // still gets executed.
786 // TODO: Have a real system where we can wait on different windows to be drawn with
787 // different callbacks.
788 if (mService.mWaitingForDrawnCallback != null) {
789 mService.mWaitingForDrawnCallback.run();
790 }
Jorim Jaggiff71d202016-04-14 13:12:36 -0700791 mService.mWaitingForDrawnCallback = () -> {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100792 synchronized (mService.mWindowMap) {
793 mAnimationStartDelayed = false;
794 if (mDelayedImeWin != null) {
795 mDelayedImeWin.endDelayingAnimationStart();
796 }
797 // If the adjust status changed since this was posted, only notify
798 // the new states and don't animate.
799 long duration = 0;
800 if (mAdjustedForIme == adjustedForIme
801 && mAdjustedForDivider == adjustedForDivider) {
802 duration = IME_ADJUST_ANIM_DURATION;
803 } else {
804 Slog.w(TAG, "IME adjust changed while waiting for drawn:"
805 + " adjustedForIme=" + adjustedForIme
806 + " adjustedForDivider=" + adjustedForDivider
807 + " mAdjustedForIme=" + mAdjustedForIme
808 + " mAdjustedForDivider=" + mAdjustedForDivider);
809 }
810 notifyAdjustedForImeChanged(
811 mAdjustedForIme || mAdjustedForDivider, duration);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700812 }
Jorim Jaggiff71d202016-04-14 13:12:36 -0700813 };
814 } else {
Chong Zhangf347ab52016-04-18 21:02:01 -0700815 notifyAdjustedForImeChanged(
816 adjustedForIme || adjustedForDivider, IME_ADJUST_ANIM_DURATION);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700817 }
Jorim Jaggieb88d832016-04-13 20:17:43 -0700818 }
819
Chong Zhang22eff0a2016-07-01 14:48:11 -0700820 private boolean setMinimizedDockedStack(boolean minimized) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700821 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Matthew Nge15352e2016-12-20 15:36:29 -0800822 notifyDockedStackMinimizedChanged(minimized, false /* animate */, isHomeStackResizable());
Chong Zhang22eff0a2016-07-01 14:48:11 -0700823 return stack != null && stack.setAdjustedForMinimizedDock(minimized ? 1f : 0f);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800824 }
825
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800826 private boolean isAnimationMaximizing() {
827 return mAnimationTarget == 0f;
828 }
829
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800830 public boolean animate(long now) {
Wale Ogunwale20ec11b2016-04-22 12:11:51 -0700831 if (mWindow == null) {
832 return false;
833 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700834 if (mAnimatingForMinimizedDockedStack) {
835 return animateForMinimizedDockedStack(now);
836 } else if (mAnimatingForIme) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700837 return animateForIme(now);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800838 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700839 return false;
Chong Zhangbaba7832016-03-24 10:21:26 -0700840 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800841
Jorim Jaggieb88d832016-04-13 20:17:43 -0700842 private boolean animateForIme(long now) {
Jorim Jaggiff71d202016-04-14 13:12:36 -0700843 if (!mAnimationStarted || mAnimationStartDelayed) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700844 mAnimationStarted = true;
845 mAnimationStartTime = now;
846 mAnimationDuration = (long)
Jorim Jaggi698e7632016-04-13 21:02:22 -0700847 (IME_ADJUST_ANIM_DURATION * mService.getWindowAnimationScaleLocked());
Jorim Jaggieb88d832016-04-13 20:17:43 -0700848 }
849 float t = Math.min(1f, (float) (now - mAnimationStartTime) / mAnimationDuration);
850 t = (mAnimationTarget == 1f ? IME_ADJUST_ENTRY_INTERPOLATOR : TOUCH_RESPONSE_INTERPOLATOR)
851 .getInterpolation(t);
Wale Ogunwale10124582016-09-15 20:25:50 -0700852 final boolean updated =
853 mDisplayContent.animateForIme(t, mAnimationTarget, mDividerAnimationTarget);
Jorim Jaggieb88d832016-04-13 20:17:43 -0700854 if (updated) {
855 mService.mWindowPlacerLocked.performSurfacePlacement();
856 }
857 if (t >= 1.0f) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700858 mLastAnimationProgress = mAnimationTarget;
859 mLastDividerProgress = mDividerAnimationTarget;
Jorim Jaggieb88d832016-04-13 20:17:43 -0700860 mAnimatingForIme = false;
861 return false;
862 } else {
863 return true;
864 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700865 }
866
867 private boolean animateForMinimizedDockedStack(long now) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700868 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800869 if (!mAnimationStarted) {
870 mAnimationStarted = true;
871 mAnimationStartTime = now;
Matthew Nge15352e2016-12-20 15:36:29 -0800872 notifyDockedStackMinimizedChanged(mMinimizedDock, true /* animate */,
873 isHomeStackResizable() /* isHomeStackResizable */);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800874 }
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800875 float t = Math.min(1f, (float) (now - mAnimationStartTime) / mAnimationDuration);
876 t = (isAnimationMaximizing() ? TOUCH_RESPONSE_INTERPOLATOR : mMinimizedDockInterpolator)
877 .getInterpolation(t);
Chong Zhang741c0ba2016-05-27 12:52:11 -0700878 if (stack != null) {
879 if (stack.setAdjustedForMinimizedDock(getMinimizeAmount(stack, t))) {
880 mService.mWindowPlacerLocked.performSurfacePlacement();
881 }
882 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800883 if (t >= 1.0f) {
Chong Zhangbaba7832016-03-24 10:21:26 -0700884 mAnimatingForMinimizedDockedStack = false;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800885 return false;
886 } else {
887 return true;
888 }
889 }
890
Wale Ogunwale10124582016-09-15 20:25:50 -0700891 float getInterpolatedAnimationValue(float t) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700892 return t * mAnimationTarget + (1 - t) * mAnimationStart;
893 }
894
Wale Ogunwale10124582016-09-15 20:25:50 -0700895 float getInterpolatedDividerValue(float t) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700896 return t * mDividerAnimationTarget + (1 - t) * mDividerAnimationStart;
897 }
898
Jorim Jaggif97ed922016-02-18 18:57:07 -0800899 /**
900 * Gets the amount how much to minimize a stack depending on the interpolated fraction t.
901 */
902 private float getMinimizeAmount(TaskStack stack, float t) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700903 final float naturalAmount = getInterpolatedAnimationValue(t);
Jorim Jaggif97ed922016-02-18 18:57:07 -0800904 if (isAnimationMaximizing()) {
905 return adjustMaximizeAmount(stack, t, naturalAmount);
906 } else {
907 return naturalAmount;
908 }
909 }
910
911 /**
912 * When maximizing the stack during a clip reveal transition, this adjusts the minimize amount
913 * during the transition such that the edge of the clip reveal rect is met earlier in the
914 * transition so we don't create a visible "hole", but only if both the clip reveal and the
915 * docked stack divider start from about the same portion on the screen.
916 */
917 private float adjustMaximizeAmount(TaskStack stack, float t, float naturalAmount) {
918 if (mMaximizeMeetFraction == 1f) {
919 return naturalAmount;
920 }
921 final int minimizeDistance = stack.getMinimizeDistance();
922 float startPrime = mService.mAppTransition.getLastClipRevealMaxTranslation()
923 / (float) minimizeDistance;
924 final float amountPrime = t * mAnimationTarget + (1 - t) * startPrime;
925 final float t2 = Math.min(t / mMaximizeMeetFraction, 1);
926 return amountPrime * t2 + naturalAmount * (1 - t2);
927 }
928
929 /**
930 * Retrieves the animation fraction at which the docked stack has to meet the clip reveal
931 * edge. See {@link #adjustMaximizeAmount}.
932 */
933 private float getClipRevealMeetFraction(TaskStack stack) {
934 if (!isAnimationMaximizing() || stack == null ||
935 !mService.mAppTransition.hadClipRevealAnimation()) {
936 return 1f;
937 }
938 final int minimizeDistance = stack.getMinimizeDistance();
939 final float fraction = Math.abs(mService.mAppTransition.getLastClipRevealMaxTranslation())
940 / (float) minimizeDistance;
941 final float t = Math.max(0, Math.min(1, (fraction - CLIP_REVEAL_MEET_FRACTION_MIN)
942 / (CLIP_REVEAL_MEET_FRACTION_MAX - CLIP_REVEAL_MEET_FRACTION_MIN)));
943 return CLIP_REVEAL_MEET_EARLIEST
944 + (1 - t) * (CLIP_REVEAL_MEET_LAST - CLIP_REVEAL_MEET_EARLIEST);
945 }
946
Jorim Jaggi50981592015-12-29 17:54:12 +0100947 public String toShortString() {
948 return TAG;
949 }
Robert Carre63e01a2016-04-18 20:27:34 -0700950
951 WindowState getWindow() {
952 return mWindow;
953 }
Jorim Jaggi31f71702016-05-04 16:43:04 -0700954
955 void dump(String prefix, PrintWriter pw) {
956 pw.println(prefix + "DockedStackDividerController");
957 pw.println(prefix + " mLastVisibility=" + mLastVisibility);
958 pw.println(prefix + " mMinimizedDock=" + mMinimizedDock);
959 pw.println(prefix + " mAdjustedForIme=" + mAdjustedForIme);
960 pw.println(prefix + " mAdjustedForDivider=" + mAdjustedForDivider);
Jorim Jaggi31f71702016-05-04 16:43:04 -0700961 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700962
963 void writeToProto(ProtoOutputStream proto, long fieldId) {
964 final long token = proto.start(fieldId);
965 proto.write(MINIMIZED_DOCK, mMinimizedDock);
966 proto.end(token);
967 }
Robert Carre63e01a2016-04-18 20:27:34 -0700968}