blob: 5c8fadbef6e20c712c89ac6ca5c22767d4ac2619 [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;
Steven Timotiusaf03df62017-07-18 16:56:43 -070037import static com.android.server.wm.proto.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 Roos11c25582018-02-19 18:06:36 +0100178 rotation);
179
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 =
236 mDisplayContent.calculateDisplayCutoutForRotation(rotation);
237 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 Roos11c25582018-02-19 18:06:36 +0100245 baseConfig.uiMode, displayId, mDisplayContent.getDisplayInfo().displayCutout);
246 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 Chungdff7a732017-12-11 12:17:06 -0800623 mService.showRecentApps();
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700624 }
625 }
626
627 /**
628 * @return true if {@param apps} contains an activity in the docked stack, false otherwise.
629 */
630 private boolean containsAppInDockedStack(ArraySet<AppWindowToken> apps) {
631 for (int i = apps.size() - 1; i >= 0; i--) {
632 final AppWindowToken token = apps.valueAt(i);
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700633 if (token.getTask() != null && token.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700634 return true;
635 }
636 }
637 return false;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800638 }
639
Jorim Jaggi1ae68bf2016-05-09 18:44:34 -0700640 boolean isMinimizedDock() {
641 return mMinimizedDock;
642 }
643
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700644 private void checkMinimizeChanged(boolean animate) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700645 if (mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility() == null) {
Jorim Jaggi817a5242016-05-06 15:45:00 -0700646 return;
647 }
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700648 final TaskStack homeStack = mDisplayContent.getHomeStack();
649 if (homeStack == null) {
650 return;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800651 }
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700652 final Task homeTask = homeStack.findHomeTask();
653 if (homeTask == null || !isWithinDisplay(homeTask)) {
654 return;
655 }
Matthew Ng8e265522017-02-13 11:09:37 -0800656
657 // Do not minimize when dock is already minimized while keyguard is showing and not
658 // occluded such as unlocking the screen
659 if (mMinimizedDock && mService.mPolicy.isKeyguardShowingAndNotOccluded()) {
660 return;
661 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800662 final TaskStack topSecondaryStack = mDisplayContent.getTopStackInWindowingMode(
Wale Ogunwaleb62139d2017-09-20 15:37:35 -0700663 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800664 boolean homeVisible = homeTask.getTopVisibleAppToken() != null;
665 if (homeVisible && topSecondaryStack != null) {
666 // Home should only be considered visible if it is greater or equal to the top secondary
667 // stack in terms of z-order.
668 homeVisible = homeStack.compareTo(topSecondaryStack) >= 0;
669 }
670 setMinimizedDockedStack(homeVisible, animate);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800671 }
672
673 private boolean isWithinDisplay(Task task) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800674 task.getBounds(mTmpRect);
675 mDisplayContent.getBounds(mTmpRect2);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800676 return mTmpRect.intersect(mTmpRect2);
677 }
678
679 /**
680 * Sets whether the docked stack is currently in a minimized state, i.e. all the tasks in the
681 * docked stack are heavily clipped so you can only see a minimal peek state.
682 *
683 * @param minimizedDock Whether the docked stack is currently minimized.
684 * @param animate Whether to animate the change.
685 */
686 private void setMinimizedDockedStack(boolean minimizedDock, boolean animate) {
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700687 final boolean wasMinimized = mMinimizedDock;
688 mMinimizedDock = minimizedDock;
Jorim Jaggi817a5242016-05-06 15:45:00 -0700689 if (minimizedDock == wasMinimized) {
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800690 return;
691 }
692
Chong Zhang22eff0a2016-07-01 14:48:11 -0700693 final boolean imeChanged = clearImeAdjustAnimation();
694 boolean minimizedChange = false;
Matthew Nge15352e2016-12-20 15:36:29 -0800695 if (isHomeStackResizable()) {
696 notifyDockedStackMinimizedChanged(minimizedDock, true /* animate */,
697 true /* isHomeStackResizable */);
698 minimizedChange = true;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800699 } else {
Matthew Nge15352e2016-12-20 15:36:29 -0800700 if (minimizedDock) {
701 if (animate) {
702 startAdjustAnimation(0f, 1f);
703 } else {
704 minimizedChange |= setMinimizedDockedStack(true);
705 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800706 } else {
Matthew Nge15352e2016-12-20 15:36:29 -0800707 if (animate) {
708 startAdjustAnimation(1f, 0f);
709 } else {
710 minimizedChange |= setMinimizedDockedStack(false);
711 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800712 }
713 }
Chong Zhang22eff0a2016-07-01 14:48:11 -0700714 if (imeChanged || minimizedChange) {
715 if (imeChanged && !minimizedChange) {
716 Slog.d(TAG, "setMinimizedDockedStack: IME adjust changed due to minimizing,"
717 + " minimizedDock=" + minimizedDock
718 + " minimizedChange=" + minimizedChange);
719 }
720 mService.mWindowPlacerLocked.performSurfacePlacement();
721 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800722 }
723
Chong Zhang22eff0a2016-07-01 14:48:11 -0700724 private boolean clearImeAdjustAnimation() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700725 final boolean changed = mDisplayContent.clearImeAdjustAnimation();
Jorim Jaggieb88d832016-04-13 20:17:43 -0700726 mAnimatingForIme = false;
Chong Zhang22eff0a2016-07-01 14:48:11 -0700727 return changed;
Jorim Jaggieb88d832016-04-13 20:17:43 -0700728 }
729
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800730 private void startAdjustAnimation(float from, float to) {
Chong Zhangbaba7832016-03-24 10:21:26 -0700731 mAnimatingForMinimizedDockedStack = true;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800732 mAnimationStarted = false;
733 mAnimationStart = from;
734 mAnimationTarget = to;
735 }
736
Chong Zhangf347ab52016-04-18 21:02:01 -0700737 private void startImeAdjustAnimation(
738 boolean adjustedForIme, boolean adjustedForDivider, WindowState imeWin) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700739
740 // If we're not in an animation, the starting point depends on whether we're adjusted
741 // or not. If we're already in an animation, we start from where the current animation
742 // left off, so that the motion doesn't look discontinuous.
743 if (!mAnimatingForIme) {
744 mAnimationStart = mAdjustedForIme ? 1 : 0;
745 mDividerAnimationStart = mAdjustedForDivider ? 1 : 0;
746 mLastAnimationProgress = mAnimationStart;
747 mLastDividerProgress = mDividerAnimationStart;
748 } else {
749 mAnimationStart = mLastAnimationProgress;
750 mDividerAnimationStart = mLastDividerProgress;
751 }
Jorim Jaggi3070e772016-05-17 16:41:32 -0700752 mAnimatingForIme = true;
753 mAnimationStarted = false;
Chong Zhangf347ab52016-04-18 21:02:01 -0700754 mAnimationTarget = adjustedForIme ? 1 : 0;
755 mDividerAnimationTarget = adjustedForDivider ? 1 : 0;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700756
Wale Ogunwale10124582016-09-15 20:25:50 -0700757 mDisplayContent.beginImeAdjustAnimation();
Jorim Jaggiff71d202016-04-14 13:12:36 -0700758
759 // We put all tasks into drag resizing mode - wait until all of them have completed the
760 // drag resizing switch.
761 if (!mService.mWaitingForDrawn.isEmpty()) {
762 mService.mH.removeMessages(H.WAITING_FOR_DRAWN_TIMEOUT);
763 mService.mH.sendEmptyMessageDelayed(H.WAITING_FOR_DRAWN_TIMEOUT,
764 IME_ADJUST_DRAWN_TIMEOUT);
765 mAnimationStartDelayed = true;
766 if (imeWin != null) {
767
768 // There might be an old window delaying the animation start - clear it.
769 if (mDelayedImeWin != null) {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100770 mDelayedImeWin.endDelayingAnimationStart();
Jorim Jaggiff71d202016-04-14 13:12:36 -0700771 }
772 mDelayedImeWin = imeWin;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100773 imeWin.startDelayingAnimationStart();
Jorim Jaggiff71d202016-04-14 13:12:36 -0700774 }
Jorim Jaggi31883862016-11-04 15:45:30 -0700775
776 // If we are already waiting for something to be drawn, clear out the old one so it
777 // still gets executed.
778 // TODO: Have a real system where we can wait on different windows to be drawn with
779 // different callbacks.
780 if (mService.mWaitingForDrawnCallback != null) {
781 mService.mWaitingForDrawnCallback.run();
782 }
Jorim Jaggiff71d202016-04-14 13:12:36 -0700783 mService.mWaitingForDrawnCallback = () -> {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100784 synchronized (mService.mWindowMap) {
785 mAnimationStartDelayed = false;
786 if (mDelayedImeWin != null) {
787 mDelayedImeWin.endDelayingAnimationStart();
788 }
789 // If the adjust status changed since this was posted, only notify
790 // the new states and don't animate.
791 long duration = 0;
792 if (mAdjustedForIme == adjustedForIme
793 && mAdjustedForDivider == adjustedForDivider) {
794 duration = IME_ADJUST_ANIM_DURATION;
795 } else {
796 Slog.w(TAG, "IME adjust changed while waiting for drawn:"
797 + " adjustedForIme=" + adjustedForIme
798 + " adjustedForDivider=" + adjustedForDivider
799 + " mAdjustedForIme=" + mAdjustedForIme
800 + " mAdjustedForDivider=" + mAdjustedForDivider);
801 }
802 notifyAdjustedForImeChanged(
803 mAdjustedForIme || mAdjustedForDivider, duration);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700804 }
Jorim Jaggiff71d202016-04-14 13:12:36 -0700805 };
806 } else {
Chong Zhangf347ab52016-04-18 21:02:01 -0700807 notifyAdjustedForImeChanged(
808 adjustedForIme || adjustedForDivider, IME_ADJUST_ANIM_DURATION);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700809 }
Jorim Jaggieb88d832016-04-13 20:17:43 -0700810 }
811
Chong Zhang22eff0a2016-07-01 14:48:11 -0700812 private boolean setMinimizedDockedStack(boolean minimized) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700813 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Matthew Nge15352e2016-12-20 15:36:29 -0800814 notifyDockedStackMinimizedChanged(minimized, false /* animate */, isHomeStackResizable());
Chong Zhang22eff0a2016-07-01 14:48:11 -0700815 return stack != null && stack.setAdjustedForMinimizedDock(minimized ? 1f : 0f);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800816 }
817
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800818 private boolean isAnimationMaximizing() {
819 return mAnimationTarget == 0f;
820 }
821
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800822 public boolean animate(long now) {
Wale Ogunwale20ec11b2016-04-22 12:11:51 -0700823 if (mWindow == null) {
824 return false;
825 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700826 if (mAnimatingForMinimizedDockedStack) {
827 return animateForMinimizedDockedStack(now);
828 } else if (mAnimatingForIme) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700829 return animateForIme(now);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800830 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700831 return false;
Chong Zhangbaba7832016-03-24 10:21:26 -0700832 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800833
Jorim Jaggieb88d832016-04-13 20:17:43 -0700834 private boolean animateForIme(long now) {
Jorim Jaggiff71d202016-04-14 13:12:36 -0700835 if (!mAnimationStarted || mAnimationStartDelayed) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700836 mAnimationStarted = true;
837 mAnimationStartTime = now;
838 mAnimationDuration = (long)
Jorim Jaggi698e7632016-04-13 21:02:22 -0700839 (IME_ADJUST_ANIM_DURATION * mService.getWindowAnimationScaleLocked());
Jorim Jaggieb88d832016-04-13 20:17:43 -0700840 }
841 float t = Math.min(1f, (float) (now - mAnimationStartTime) / mAnimationDuration);
842 t = (mAnimationTarget == 1f ? IME_ADJUST_ENTRY_INTERPOLATOR : TOUCH_RESPONSE_INTERPOLATOR)
843 .getInterpolation(t);
Wale Ogunwale10124582016-09-15 20:25:50 -0700844 final boolean updated =
845 mDisplayContent.animateForIme(t, mAnimationTarget, mDividerAnimationTarget);
Jorim Jaggieb88d832016-04-13 20:17:43 -0700846 if (updated) {
847 mService.mWindowPlacerLocked.performSurfacePlacement();
848 }
849 if (t >= 1.0f) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700850 mLastAnimationProgress = mAnimationTarget;
851 mLastDividerProgress = mDividerAnimationTarget;
Jorim Jaggieb88d832016-04-13 20:17:43 -0700852 mAnimatingForIme = false;
853 return false;
854 } else {
855 return true;
856 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700857 }
858
859 private boolean animateForMinimizedDockedStack(long now) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700860 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800861 if (!mAnimationStarted) {
862 mAnimationStarted = true;
863 mAnimationStartTime = now;
Matthew Nge15352e2016-12-20 15:36:29 -0800864 notifyDockedStackMinimizedChanged(mMinimizedDock, true /* animate */,
865 isHomeStackResizable() /* isHomeStackResizable */);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800866 }
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800867 float t = Math.min(1f, (float) (now - mAnimationStartTime) / mAnimationDuration);
868 t = (isAnimationMaximizing() ? TOUCH_RESPONSE_INTERPOLATOR : mMinimizedDockInterpolator)
869 .getInterpolation(t);
Chong Zhang741c0ba2016-05-27 12:52:11 -0700870 if (stack != null) {
871 if (stack.setAdjustedForMinimizedDock(getMinimizeAmount(stack, t))) {
872 mService.mWindowPlacerLocked.performSurfacePlacement();
873 }
874 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800875 if (t >= 1.0f) {
Chong Zhangbaba7832016-03-24 10:21:26 -0700876 mAnimatingForMinimizedDockedStack = false;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800877 return false;
878 } else {
879 return true;
880 }
881 }
882
Wale Ogunwale10124582016-09-15 20:25:50 -0700883 float getInterpolatedAnimationValue(float t) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700884 return t * mAnimationTarget + (1 - t) * mAnimationStart;
885 }
886
Wale Ogunwale10124582016-09-15 20:25:50 -0700887 float getInterpolatedDividerValue(float t) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700888 return t * mDividerAnimationTarget + (1 - t) * mDividerAnimationStart;
889 }
890
Jorim Jaggif97ed922016-02-18 18:57:07 -0800891 /**
892 * Gets the amount how much to minimize a stack depending on the interpolated fraction t.
893 */
894 private float getMinimizeAmount(TaskStack stack, float t) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700895 final float naturalAmount = getInterpolatedAnimationValue(t);
Jorim Jaggif97ed922016-02-18 18:57:07 -0800896 if (isAnimationMaximizing()) {
897 return adjustMaximizeAmount(stack, t, naturalAmount);
898 } else {
899 return naturalAmount;
900 }
901 }
902
903 /**
904 * When maximizing the stack during a clip reveal transition, this adjusts the minimize amount
905 * during the transition such that the edge of the clip reveal rect is met earlier in the
906 * transition so we don't create a visible "hole", but only if both the clip reveal and the
907 * docked stack divider start from about the same portion on the screen.
908 */
909 private float adjustMaximizeAmount(TaskStack stack, float t, float naturalAmount) {
910 if (mMaximizeMeetFraction == 1f) {
911 return naturalAmount;
912 }
913 final int minimizeDistance = stack.getMinimizeDistance();
914 float startPrime = mService.mAppTransition.getLastClipRevealMaxTranslation()
915 / (float) minimizeDistance;
916 final float amountPrime = t * mAnimationTarget + (1 - t) * startPrime;
917 final float t2 = Math.min(t / mMaximizeMeetFraction, 1);
918 return amountPrime * t2 + naturalAmount * (1 - t2);
919 }
920
921 /**
922 * Retrieves the animation fraction at which the docked stack has to meet the clip reveal
923 * edge. See {@link #adjustMaximizeAmount}.
924 */
925 private float getClipRevealMeetFraction(TaskStack stack) {
926 if (!isAnimationMaximizing() || stack == null ||
927 !mService.mAppTransition.hadClipRevealAnimation()) {
928 return 1f;
929 }
930 final int minimizeDistance = stack.getMinimizeDistance();
931 final float fraction = Math.abs(mService.mAppTransition.getLastClipRevealMaxTranslation())
932 / (float) minimizeDistance;
933 final float t = Math.max(0, Math.min(1, (fraction - CLIP_REVEAL_MEET_FRACTION_MIN)
934 / (CLIP_REVEAL_MEET_FRACTION_MAX - CLIP_REVEAL_MEET_FRACTION_MIN)));
935 return CLIP_REVEAL_MEET_EARLIEST
936 + (1 - t) * (CLIP_REVEAL_MEET_LAST - CLIP_REVEAL_MEET_EARLIEST);
937 }
938
Jorim Jaggi50981592015-12-29 17:54:12 +0100939 public String toShortString() {
940 return TAG;
941 }
Robert Carre63e01a2016-04-18 20:27:34 -0700942
943 WindowState getWindow() {
944 return mWindow;
945 }
Jorim Jaggi31f71702016-05-04 16:43:04 -0700946
947 void dump(String prefix, PrintWriter pw) {
948 pw.println(prefix + "DockedStackDividerController");
949 pw.println(prefix + " mLastVisibility=" + mLastVisibility);
950 pw.println(prefix + " mMinimizedDock=" + mMinimizedDock);
951 pw.println(prefix + " mAdjustedForIme=" + mAdjustedForIme);
952 pw.println(prefix + " mAdjustedForDivider=" + mAdjustedForDivider);
Jorim Jaggi31f71702016-05-04 16:43:04 -0700953 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700954
955 void writeToProto(ProtoOutputStream proto, long fieldId) {
956 final long token = proto.start(fieldId);
957 proto.write(MINIMIZED_DOCK, mMinimizedDock);
958 proto.end(token);
959 }
Robert Carre63e01a2016-04-18 20:27:34 -0700960}