blob: 8308417fa14193d11fb24c32c4fc3f8cb4e85279 [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 Jaggi84afb1a2016-09-28 14:54:04 +020032import static com.android.server.wm.AppTransition.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;
Jorim Jaggi50981592015-12-29 17:54:12 +010047import android.view.DisplayInfo;
Jorim Jaggia6c934e2015-12-21 13:22:31 +010048import android.view.IDockedStackListener;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080049import android.view.animation.AnimationUtils;
50import android.view.animation.Interpolator;
Jorim Jaggieb88d832016-04-13 20:17:43 -070051import android.view.animation.PathInterpolator;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -070052import android.view.inputmethod.InputMethodManagerInternal;
Jorim Jaggi50981592015-12-29 17:54:12 +010053
Jorim Jaggi85639432016-05-06 17:27:55 -070054import com.android.internal.policy.DividerSnapAlgorithm;
55import com.android.internal.policy.DockedDividerUtils;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -070056import com.android.server.LocalServices;
Jorim Jaggiff71d202016-04-14 13:12:36 -070057import com.android.server.wm.WindowManagerService.H;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010058
Jorim Jaggi31f71702016-05-04 16:43:04 -070059import java.io.PrintWriter;
Chong Zhangbaba7832016-03-24 10:21:26 -070060
Filip Gruszczynski466f3212015-09-21 17:57:57 -070061/**
Jorim Jaggi61f39a72015-10-29 16:54:18 +010062 * Keeps information about the docked stack divider.
Filip Gruszczynski466f3212015-09-21 17:57:57 -070063 */
Robert Carrf59b8dd2017-10-02 18:58:36 -070064public class DockedStackDividerController {
Jorim Jaggi61f39a72015-10-29 16:54:18 +010065
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080066 private static final String TAG = TAG_WITH_CLASS_NAME ? "DockedStackDividerController" : TAG_WM;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010067
Jorim Jaggif97ed922016-02-18 18:57:07 -080068 /**
69 * The fraction during the maximize/clip reveal animation the divider meets the edge of the clip
70 * revealing surface at the earliest.
71 */
72 private static final float CLIP_REVEAL_MEET_EARLIEST = 0.6f;
73
74 /**
75 * The fraction during the maximize/clip reveal animation the divider meets the edge of the clip
76 * revealing surface at the latest.
77 */
78 private static final float CLIP_REVEAL_MEET_LAST = 1f;
79
80 /**
81 * If the app translates at least CLIP_REVEAL_MEET_FRACTION_MIN * minimize distance, we start
82 * meet somewhere between {@link #CLIP_REVEAL_MEET_LAST} and {@link #CLIP_REVEAL_MEET_EARLIEST}.
83 */
84 private static final float CLIP_REVEAL_MEET_FRACTION_MIN = 0.4f;
85
86 /**
87 * If the app translates equals or more than CLIP_REVEAL_MEET_FRACTION_MIN * minimize distance,
88 * we meet at {@link #CLIP_REVEAL_MEET_EARLIEST}.
89 */
90 private static final float CLIP_REVEAL_MEET_FRACTION_MAX = 0.8f;
91
Jorim Jaggieb88d832016-04-13 20:17:43 -070092 private static final Interpolator IME_ADJUST_ENTRY_INTERPOLATOR =
Jorim Jaggiff71d202016-04-14 13:12:36 -070093 new PathInterpolator(0.2f, 0f, 0.1f, 1f);
Jorim Jaggieb88d832016-04-13 20:17:43 -070094
Jorim Jaggi698e7632016-04-13 21:02:22 -070095 private static final long IME_ADJUST_ANIM_DURATION = 280;
Jorim Jaggieb88d832016-04-13 20:17:43 -070096
Jorim Jaggiff71d202016-04-14 13:12:36 -070097 private static final long IME_ADJUST_DRAWN_TIMEOUT = 200;
98
Chong Zhang198afac2016-04-15 12:03:11 -070099 private static final int DIVIDER_WIDTH_INACTIVE_DP = 4;
100
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800101 private final WindowManagerService mService;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700102 private final DisplayContent mDisplayContent;
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700103 private int mDividerWindowWidth;
Chong Zhang198afac2016-04-15 12:03:11 -0700104 private int mDividerWindowWidthInactive;
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700105 private int mDividerInsets;
Matthew Nge15352e2016-12-20 15:36:29 -0800106 private int mTaskHeightInMinimizedMode;
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100107 private boolean mResizing;
108 private WindowState mWindow;
109 private final Rect mTmpRect = new Rect();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800110 private final Rect mTmpRect2 = new Rect();
Jorim Jaggi85639432016-05-06 17:27:55 -0700111 private final Rect mTmpRect3 = new Rect();
Filip Gruszczynski77049052015-11-09 14:01:21 -0800112 private final Rect mLastRect = new Rect();
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800113 private boolean mLastVisibility = false;
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100114 private final RemoteCallbackList<IDockedStackListener> mDockedStackListeners
115 = new RemoteCallbackList<>();
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700116
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800117 private boolean mMinimizedDock;
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700118 private int mOriginalDockedSide = DOCKED_INVALID;
Chong Zhangbaba7832016-03-24 10:21:26 -0700119 private boolean mAnimatingForMinimizedDockedStack;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800120 private boolean mAnimationStarted;
121 private long mAnimationStartTime;
122 private float mAnimationStart;
123 private float mAnimationTarget;
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800124 private long mAnimationDuration;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700125 private boolean mAnimationStartDelayed;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800126 private final Interpolator mMinimizedDockInterpolator;
Jorim Jaggif97ed922016-02-18 18:57:07 -0800127 private float mMaximizeMeetFraction;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100128 private final Rect mTouchRegion = new Rect();
Chong Zhangbaba7832016-03-24 10:21:26 -0700129 private boolean mAnimatingForIme;
130 private boolean mAdjustedForIme;
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700131 private int mImeHeight;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700132 private WindowState mDelayedImeWin;
Chong Zhangf347ab52016-04-18 21:02:01 -0700133 private boolean mAdjustedForDivider;
134 private float mDividerAnimationStart;
135 private float mDividerAnimationTarget;
Wale Ogunwale10124582016-09-15 20:25:50 -0700136 float mLastAnimationProgress;
137 float mLastDividerProgress;
Jorim Jaggi85639432016-05-06 17:27:55 -0700138 private final DividerSnapAlgorithm[] mSnapAlgorithmForRotation = new DividerSnapAlgorithm[4];
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700139 private boolean mImeHideRequested;
Jorim Jaggif93ac2b2017-10-25 15:20:24 +0200140 private final Rect mLastDimLayerRect = new Rect();
141 private float mLastDimLayerAlpha;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700142 private TaskStack mDimmedStack;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800143
144 DockedStackDividerController(WindowManagerService service, DisplayContent displayContent) {
145 mService = service;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700146 mDisplayContent = displayContent;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800147 final Context context = service.mContext;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800148 mMinimizedDockInterpolator = AnimationUtils.loadInterpolator(
149 context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700150 loadDimens();
151 }
152
Jorim Jaggi85639432016-05-06 17:27:55 -0700153 int getSmallestWidthDpForBounds(Rect bounds) {
154 final DisplayInfo di = mDisplayContent.getDisplayInfo();
155
Jorim Jaggi85639432016-05-06 17:27:55 -0700156 final int baseDisplayWidth = mDisplayContent.mBaseDisplayWidth;
157 final int baseDisplayHeight = mDisplayContent.mBaseDisplayHeight;
158 int minWidth = Integer.MAX_VALUE;
159
160 // Go through all screen orientations and find the orientation in which the task has the
161 // smallest width.
162 for (int rotation = 0; rotation < 4; rotation++) {
163 mTmpRect.set(bounds);
164 mDisplayContent.rotateBounds(di.rotation, rotation, mTmpRect);
165 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
166 mTmpRect2.set(0, 0,
167 rotated ? baseDisplayHeight : baseDisplayWidth,
168 rotated ? baseDisplayWidth : baseDisplayHeight);
169 final int orientation = mTmpRect2.width() <= mTmpRect2.height()
170 ? ORIENTATION_PORTRAIT
171 : ORIENTATION_LANDSCAPE;
172 final int dockSide = TaskStack.getDockSideUnchecked(mTmpRect, mTmpRect2, orientation);
173 final int position = DockedDividerUtils.calculatePositionForBounds(mTmpRect, dockSide,
174 getContentWidth());
175
176 // Since we only care about feasible states, snap to the closest snap target, like it
177 // would happen when actually rotating the screen.
178 final int snappedPosition = mSnapAlgorithmForRotation[rotation]
179 .calculateNonDismissingSnapTarget(position).position;
180 DockedDividerUtils.calculateBoundsForPosition(snappedPosition, dockSide, mTmpRect,
181 mTmpRect2.width(), mTmpRect2.height(), getContentWidth());
182 mService.mPolicy.getStableInsetsLw(rotation, mTmpRect2.width(), mTmpRect2.height(),
183 mTmpRect3);
Winson Chungbdc646f2017-02-13 12:12:22 -0800184 mService.intersectDisplayInsetBounds(mTmpRect2, mTmpRect3, mTmpRect);
Jorim Jaggi85639432016-05-06 17:27:55 -0700185 minWidth = Math.min(mTmpRect.width(), minWidth);
186 }
187 return (int) (minWidth / mDisplayContent.getDisplayMetrics().density);
188 }
189
Matthew Nge15352e2016-12-20 15:36:29 -0800190 void getHomeStackBoundsInDockedMode(Rect outBounds) {
191 final DisplayInfo di = mDisplayContent.getDisplayInfo();
192 mService.mPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
193 mTmpRect);
194 int dividerSize = mDividerWindowWidth - 2 * mDividerInsets;
195 Configuration configuration = mDisplayContent.getConfiguration();
Matthew Nga9e173d2017-05-17 15:03:18 -0700196 // The offset in the left (landscape)/top (portrait) is calculated with the minimized
197 // offset value with the divider size and any system insets in that direction.
Matthew Nge15352e2016-12-20 15:36:29 -0800198 if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
199 outBounds.set(0, mTaskHeightInMinimizedMode + dividerSize + mTmpRect.top,
200 di.logicalWidth, di.logicalHeight);
201 } else {
Matthew Nga9e173d2017-05-17 15:03:18 -0700202 // In landscape append the left position with the statusbar height to match the
203 // minimized size height in portrait mode.
204 outBounds.set(mTaskHeightInMinimizedMode + dividerSize + mTmpRect.left + mTmpRect.top,
205 0, di.logicalWidth, di.logicalHeight);
Matthew Nge15352e2016-12-20 15:36:29 -0800206 }
207 }
208
209 boolean isHomeStackResizable() {
210 final TaskStack homeStack = mDisplayContent.getHomeStack();
211 if (homeStack == null) {
212 return false;
213 }
214 final Task homeTask = homeStack.findHomeTask();
215 return homeTask != null && homeTask.isResizeable();
216 }
217
Jorim Jaggi85639432016-05-06 17:27:55 -0700218 private void initSnapAlgorithmForRotations() {
Andrii Kulian441e4492016-09-29 15:25:00 -0700219 final Configuration baseConfig = mDisplayContent.getConfiguration();
Jorim Jaggi85639432016-05-06 17:27:55 -0700220
221 // Initialize the snap algorithms for all 4 screen orientations.
222 final Configuration config = new Configuration();
223 for (int rotation = 0; rotation < 4; rotation++) {
224 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
225 final int dw = rotated
226 ? mDisplayContent.mBaseDisplayHeight
227 : mDisplayContent.mBaseDisplayWidth;
228 final int dh = rotated
229 ? mDisplayContent.mBaseDisplayWidth
230 : mDisplayContent.mBaseDisplayHeight;
231 mService.mPolicy.getStableInsetsLw(rotation, dw, dh, mTmpRect);
Andrii Kulianb10330d2016-09-16 13:51:46 -0700232 config.unset();
Jorim Jaggi85639432016-05-06 17:27:55 -0700233 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Bryce Lee7566d762017-03-30 09:34:15 -0700234
235 final int displayId = mDisplayContent.getDisplayId();
236 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation,
237 baseConfig.uiMode, displayId);
238 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
239 baseConfig.uiMode, displayId);
240 mService.mPolicy.getNonDecorInsetsLw(rotation, dw, dh, mTmpRect);
241 final int leftInset = mTmpRect.left;
242 final int topInset = mTmpRect.top;
243
Wale Ogunwale822e5122017-07-26 06:02:24 -0700244 config.windowConfiguration.setAppBounds(leftInset /*left*/, topInset /*top*/,
245 leftInset + appWidth /*right*/, topInset + appHeight /*bottom*/);
Bryce Lee7566d762017-03-30 09:34:15 -0700246
Jorim Jaggi85639432016-05-06 17:27:55 -0700247 config.screenWidthDp = (int)
Andrii Kuliandb8e1062016-11-15 18:30:27 -0800248 (mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, baseConfig.uiMode,
Bryce Lee7566d762017-03-30 09:34:15 -0700249 displayId) / mDisplayContent.getDisplayMetrics().density);
Jorim Jaggi85639432016-05-06 17:27:55 -0700250 config.screenHeightDp = (int)
Andrii Kuliandb8e1062016-11-15 18:30:27 -0800251 (mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, baseConfig.uiMode,
Bryce Lee7566d762017-03-30 09:34:15 -0700252 displayId) / mDisplayContent.getDisplayMetrics().density);
Jorim Jaggi85639432016-05-06 17:27:55 -0700253 final Context rotationContext = mService.mContext.createConfigurationContext(config);
254 mSnapAlgorithmForRotation[rotation] = new DividerSnapAlgorithm(
255 rotationContext.getResources(), dw, dh, getContentWidth(),
256 config.orientation == ORIENTATION_PORTRAIT, mTmpRect);
257 }
258 }
259
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700260 private void loadDimens() {
261 final Context context = mService.mContext;
262 mDividerWindowWidth = context.getResources().getDimensionPixelSize(
263 com.android.internal.R.dimen.docked_stack_divider_thickness);
264 mDividerInsets = context.getResources().getDimensionPixelSize(
265 com.android.internal.R.dimen.docked_stack_divider_insets);
Chong Zhang198afac2016-04-15 12:03:11 -0700266 mDividerWindowWidthInactive = WindowManagerService.dipToPixel(
267 DIVIDER_WIDTH_INACTIVE_DP, mDisplayContent.getDisplayMetrics());
Matthew Nge15352e2016-12-20 15:36:29 -0800268 mTaskHeightInMinimizedMode = context.getResources().getDimensionPixelSize(
269 com.android.internal.R.dimen.task_height_of_minimized_mode);
Jorim Jaggi85639432016-05-06 17:27:55 -0700270 initSnapAlgorithmForRotations();
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700271 }
272
273 void onConfigurationChanged() {
274 loadDimens();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700275 }
276
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100277 boolean isResizing() {
278 return mResizing;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700279 }
280
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100281 int getContentWidth() {
282 return mDividerWindowWidth - 2 * mDividerInsets;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700283 }
284
Jorim Jaggi81ba11e2016-02-03 22:04:22 -0800285 int getContentInsets() {
286 return mDividerInsets;
287 }
288
Chong Zhang198afac2016-04-15 12:03:11 -0700289 int getContentWidthInactive() {
290 return mDividerWindowWidthInactive;
291 }
292
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100293 void setResizing(boolean resizing) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800294 if (mResizing != resizing) {
295 mResizing = resizing;
296 resetDragResizingChangeReported();
297 }
298 }
299
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100300 void setTouchRegion(Rect touchRegion) {
301 mTouchRegion.set(touchRegion);
302 }
303
304 void getTouchRegion(Rect outRegion) {
305 outRegion.set(mTouchRegion);
306 outRegion.offset(mWindow.getFrameLw().left, mWindow.getFrameLw().top);
307 }
308
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800309 private void resetDragResizingChangeReported() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800310 mDisplayContent.forAllWindows(WindowState::resetDragResizingChangeReported,
311 true /* traverseTopToBottom */ );
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100312 }
313
314 void setWindow(WindowState window) {
315 mWindow = window;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800316 reevaluateVisibility(false);
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100317 }
318
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800319 void reevaluateVisibility(boolean force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800320 if (mWindow == null) {
321 return;
322 }
Matthew Ng64e77cf2017-10-31 14:01:31 -0700323 TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Jorim Jaggi7998e482016-02-12 18:47:06 -0800324
325 // If the stack is invisible, we policy force hide it in WindowAnimator.shouldForceHide
326 final boolean visible = stack != null;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800327 if (mLastVisibility == visible && !force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800328 return;
329 }
330 mLastVisibility = visible;
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100331 notifyDockedDividerVisibilityChanged(visible);
Jorim Jaggi50981592015-12-29 17:54:12 +0100332 if (!visible) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700333 setResizeDimLayer(false, WINDOWING_MODE_UNDEFINED, 0f);
Jorim Jaggi50981592015-12-29 17:54:12 +0100334 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100335 }
336
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700337 private boolean wasVisible() {
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100338 return mLastVisibility;
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100339 }
340
Chong Zhangf347ab52016-04-18 21:02:01 -0700341 void setAdjustedForIme(
342 boolean adjustedForIme, boolean adjustedForDivider,
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700343 boolean animate, WindowState imeWin, int imeHeight) {
344 if (mAdjustedForIme != adjustedForIme || (adjustedForIme && mImeHeight != imeHeight)
345 || mAdjustedForDivider != adjustedForDivider) {
Jorim Jaggi3070e772016-05-17 16:41:32 -0700346 if (animate && !mAnimatingForMinimizedDockedStack) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700347 startImeAdjustAnimation(adjustedForIme, adjustedForDivider, imeWin);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700348 } else {
Jorim Jaggiff71d202016-04-14 13:12:36 -0700349 // Animation might be delayed, so only notify if we don't run an animation.
Chong Zhangf347ab52016-04-18 21:02:01 -0700350 notifyAdjustedForImeChanged(adjustedForIme || adjustedForDivider, 0 /* duration */);
Jorim Jaggieb88d832016-04-13 20:17:43 -0700351 }
Chong Zhangf347ab52016-04-18 21:02:01 -0700352 mAdjustedForIme = adjustedForIme;
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700353 mImeHeight = imeHeight;
Chong Zhangf347ab52016-04-18 21:02:01 -0700354 mAdjustedForDivider = adjustedForDivider;
Chong Zhangbaba7832016-03-24 10:21:26 -0700355 }
Chong Zhangb58bbcc2016-03-23 11:57:36 -0700356 }
357
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700358 int getImeHeightAdjustedFor() {
359 return mImeHeight;
360 }
361
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700362 void positionDockedStackedDivider(Rect frame) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700363 TaskStack stack = mDisplayContent.getSplitScreenPrimaryStack();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700364 if (stack == null) {
365 // Unfortunately we might end up with still having a divider, even though the underlying
366 // stack was already removed. This is because we are on AM thread and the removal of the
Filip Gruszczynski77049052015-11-09 14:01:21 -0800367 // divider was deferred to WM thread and hasn't happened yet. In that case let's just
368 // keep putting it in the same place it was before the stack was removed to have
369 // continuity and prevent it from jumping to the center. It will get hidden soon.
370 frame.set(mLastRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700371 return;
Filip Gruszczynski77049052015-11-09 14:01:21 -0800372 } else {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800373 stack.getDimBounds(mTmpRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700374 }
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100375 int side = stack.getDockSide();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700376 switch (side) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700377 case DOCKED_LEFT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100378 frame.set(mTmpRect.right - mDividerInsets, frame.top,
379 mTmpRect.right + frame.width() - mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700380 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700381 case DOCKED_TOP:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100382 frame.set(frame.left, mTmpRect.bottom - mDividerInsets,
383 mTmpRect.right, mTmpRect.bottom + frame.height() - mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700384 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700385 case DOCKED_RIGHT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100386 frame.set(mTmpRect.left - frame.width() + mDividerInsets, frame.top,
387 mTmpRect.left + mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700388 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700389 case DOCKED_BOTTOM:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100390 frame.set(frame.left, mTmpRect.top - frame.height() + mDividerInsets,
391 frame.right, mTmpRect.top + mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700392 break;
393 }
Filip Gruszczynski77049052015-11-09 14:01:21 -0800394 mLastRect.set(frame);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700395 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800396
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700397 private void notifyDockedDividerVisibilityChanged(boolean visible) {
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100398 final int size = mDockedStackListeners.beginBroadcast();
399 for (int i = 0; i < size; ++i) {
400 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
401 try {
402 listener.onDividerVisibilityChanged(visible);
403 } catch (RemoteException e) {
404 Slog.e(TAG_WM, "Error delivering divider visibility changed event.", e);
405 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800406 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100407 mDockedStackListeners.finishBroadcast();
408 }
409
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700410 /**
411 * Checks if the primary stack is allowed to dock to a specific side based on its original dock
412 * side.
413 *
414 * @param dockSide the side to see if it is valid
415 * @return true if the side provided is valid
416 */
417 boolean canPrimaryStackDockTo(int dockSide) {
418 if (mService.mPolicy.isDockSideAllowed(dockSide)) {
419 // Side is the same as original side
420 if (dockSide == mOriginalDockedSide) {
421 return true;
422 }
423 // Special rule that the top in portrait is always valid
424 if (dockSide == DOCKED_TOP) {
425 return true;
426 }
427 // Only if original docked side was top in portrait will allow left side for landscape
428 if (dockSide == DOCKED_LEFT && mOriginalDockedSide == DOCKED_TOP) {
429 return true;
430 }
431 }
432 return false;
433 }
434
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100435 void notifyDockedStackExistsChanged(boolean exists) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700436 // TODO(multi-display): Perform all actions only for current display.
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100437 final int size = mDockedStackListeners.beginBroadcast();
438 for (int i = 0; i < size; ++i) {
439 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
440 try {
441 listener.onDockedStackExistsChanged(exists);
442 } catch (RemoteException e) {
443 Slog.e(TAG_WM, "Error delivering docked stack exists changed event.", e);
444 }
445 }
446 mDockedStackListeners.finishBroadcast();
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700447 if (exists) {
448 InputMethodManagerInternal inputMethodManagerInternal =
449 LocalServices.getService(InputMethodManagerInternal.class);
450 if (inputMethodManagerInternal != null) {
451
452 // Hide the current IME to avoid problems with animations from IME adjustment when
453 // attaching the docked stack.
454 inputMethodManagerInternal.hideCurrentInputMethod();
455 mImeHideRequested = true;
456 }
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700457 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
458 mOriginalDockedSide = stack.getDockSide();
Matthew Ngbc527a32017-06-19 16:42:31 -0700459 return;
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700460 }
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700461 mOriginalDockedSide = DOCKED_INVALID;
Matthew Ngbc527a32017-06-19 16:42:31 -0700462 setMinimizedDockedStack(false /* minimizedDock */, false /* animate */);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700463
464 if (mDimmedStack != null) {
465 mDimmedStack.stopDimming();
466 mDimmedStack = null;
467 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100468 }
469
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700470 /**
471 * Resets the state that IME hide has been requested. See {@link #isImeHideRequested}.
472 */
473 void resetImeHideRequested() {
474 mImeHideRequested = false;
475 }
476
477 /**
478 * The docked stack divider controller makes sure the IME gets hidden when attaching the docked
479 * stack, to avoid animation problems. This flag indicates whether the request to hide the IME
480 * has been sent in an asynchronous manner, and the IME should be treated as hidden already.
481 *
482 * @return whether IME hide request has been sent
483 */
484 boolean isImeHideRequested() {
485 return mImeHideRequested;
486 }
487
Matthew Nge15352e2016-12-20 15:36:29 -0800488 private void notifyDockedStackMinimizedChanged(boolean minimizedDock, boolean animate,
489 boolean isHomeStackResizable) {
490 long animDuration = 0;
491 if (animate) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -0700492 final TaskStack stack =
Matthew Ng64e77cf2017-10-31 14:01:31 -0700493 mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Matthew Nge15352e2016-12-20 15:36:29 -0800494 final long transitionDuration = isAnimationMaximizing()
495 ? mService.mAppTransition.getLastClipRevealTransitionDuration()
496 : DEFAULT_APP_TRANSITION_DURATION;
497 mAnimationDuration = (long)
498 (transitionDuration * mService.getTransitionAnimationScaleLocked());
499 mMaximizeMeetFraction = getClipRevealMeetFraction(stack);
500 animDuration = (long) (mAnimationDuration * mMaximizeMeetFraction);
501 }
Tony Mak853304c2016-04-18 15:17:41 +0100502 mService.mH.removeMessages(NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED);
503 mService.mH.obtainMessage(NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED,
504 minimizedDock ? 1 : 0, 0).sendToTarget();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800505 final int size = mDockedStackListeners.beginBroadcast();
506 for (int i = 0; i < size; ++i) {
507 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
508 try {
Matthew Nge15352e2016-12-20 15:36:29 -0800509 listener.onDockedStackMinimizedChanged(minimizedDock, animDuration,
510 isHomeStackResizable);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800511 } catch (RemoteException e) {
512 Slog.e(TAG_WM, "Error delivering minimized dock changed event.", e);
513 }
514 }
515 mDockedStackListeners.finishBroadcast();
516 }
517
Jorim Jaggi2917dc42016-04-11 11:39:13 -0700518 void notifyDockSideChanged(int newDockSide) {
519 final int size = mDockedStackListeners.beginBroadcast();
520 for (int i = 0; i < size; ++i) {
521 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
522 try {
523 listener.onDockSideChanged(newDockSide);
524 } catch (RemoteException e) {
525 Slog.e(TAG_WM, "Error delivering dock side changed event.", e);
526 }
527 }
528 mDockedStackListeners.finishBroadcast();
529 }
530
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700531 private void notifyAdjustedForImeChanged(boolean adjustedForIme, long animDuration) {
Jorim Jaggi698e7632016-04-13 21:02:22 -0700532 final int size = mDockedStackListeners.beginBroadcast();
533 for (int i = 0; i < size; ++i) {
534 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
535 try {
536 listener.onAdjustedForImeChanged(adjustedForIme, animDuration);
537 } catch (RemoteException e) {
538 Slog.e(TAG_WM, "Error delivering adjusted for ime changed event.", e);
539 }
540 }
541 mDockedStackListeners.finishBroadcast();
542 }
543
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100544 void registerDockedStackListener(IDockedStackListener listener) {
545 mDockedStackListeners.register(listener);
546 notifyDockedDividerVisibilityChanged(wasVisible());
Wale Ogunwale61911492017-10-11 08:50:50 -0700547 notifyDockedStackExistsChanged(
Matthew Ng64e77cf2017-10-31 14:01:31 -0700548 mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility() != null);
Matthew Nge15352e2016-12-20 15:36:29 -0800549 notifyDockedStackMinimizedChanged(mMinimizedDock, false /* animate */,
550 isHomeStackResizable());
Jorim Jaggi698e7632016-04-13 21:02:22 -0700551 notifyAdjustedForImeChanged(mAdjustedForIme, 0 /* animDuration */);
552
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800553 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100554
Wale Ogunwale68278562017-09-23 17:13:55 -0700555 /**
556 * Shows a dim layer with {@param alpha} if {@param visible} is true and
557 * {@param targetWindowingMode} isn't
558 * {@link android.app.WindowConfiguration#WINDOWING_MODE_UNDEFINED} and there is a stack on the
559 * display in that windowing mode.
560 */
561 void setResizeDimLayer(boolean visible, int targetWindowingMode, float alpha) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700562 // TODO: Maybe only allow split-screen windowing modes?
563 final TaskStack stack = targetWindowingMode != WINDOWING_MODE_UNDEFINED
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800564 ? mDisplayContent.getTopStackInWindowingMode(targetWindowingMode)
Wale Ogunwale68278562017-09-23 17:13:55 -0700565 : null;
Matthew Ng64e77cf2017-10-31 14:01:31 -0700566 final TaskStack dockedStack = mDisplayContent.getSplitScreenPrimaryStack();
Jorim Jaggibc5425c2016-03-01 13:51:16 +0100567 boolean visibleAndValid = visible && stack != null && dockedStack != null;
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100568 if (visibleAndValid) {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700569 mDimmedStack = stack;
570 stack.dim(alpha);
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100571 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700572 if (!visibleAndValid && stack != null) {
573 mDimmedStack = null;
574 stack.stopDimming();
Jorim Jaggi50981592015-12-29 17:54:12 +0100575 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100576 }
577
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800578 /**
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700579 * @return The layer used for dimming the apps when dismissing docked/fullscreen stack. Just
580 * above all application surfaces.
581 */
582 private int getResizeDimLayer() {
583 return (mWindow != null) ? mWindow.mLayer - 1 : LAYER_OFFSET_DIM;
584 }
585
586 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800587 * Notifies the docked stack divider controller of a visibility change that happens without
588 * an animation.
589 */
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700590 void notifyAppVisibilityChanged() {
591 checkMinimizeChanged(false /* animate */);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800592 }
593
Jorim Jaggi84afb1a2016-09-28 14:54:04 +0200594 void notifyAppTransitionStarting(ArraySet<AppWindowToken> openingApps, int appTransition) {
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700595 final boolean wasMinimized = mMinimizedDock;
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700596 checkMinimizeChanged(true /* animate */);
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700597
598 // We were minimized, and now we are still minimized, but somebody is trying to launch an
Matthew Ng8e265522017-02-13 11:09:37 -0800599 // app in docked stack, better show recent apps so we actually get unminimized! However do
600 // not do this if keyguard is dismissed such as when the device is unlocking. This catches
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700601 // any case that was missed in ActivityStarter.postStartActivityUncheckedProcessing because
602 // we couldn't retrace the launch of the app in the docked stack to the launch from
603 // homescreen.
Jorim Jaggi84afb1a2016-09-28 14:54:04 +0200604 if (wasMinimized && mMinimizedDock && containsAppInDockedStack(openingApps)
Matthew Ng8e265522017-02-13 11:09:37 -0800605 && appTransition != TRANSIT_NONE &&
606 !AppTransition.isKeyguardGoingAwayTransit(appTransition)) {
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700607 mService.showRecentApps(true /* fromHome */);
608 }
609 }
610
611 /**
612 * @return true if {@param apps} contains an activity in the docked stack, false otherwise.
613 */
614 private boolean containsAppInDockedStack(ArraySet<AppWindowToken> apps) {
615 for (int i = apps.size() - 1; i >= 0; i--) {
616 final AppWindowToken token = apps.valueAt(i);
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700617 if (token.getTask() != null && token.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700618 return true;
619 }
620 }
621 return false;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800622 }
623
Jorim Jaggi1ae68bf2016-05-09 18:44:34 -0700624 boolean isMinimizedDock() {
625 return mMinimizedDock;
626 }
627
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700628 private void checkMinimizeChanged(boolean animate) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700629 if (mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility() == null) {
Jorim Jaggi817a5242016-05-06 15:45:00 -0700630 return;
631 }
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700632 final TaskStack homeStack = mDisplayContent.getHomeStack();
633 if (homeStack == null) {
634 return;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800635 }
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700636 final Task homeTask = homeStack.findHomeTask();
637 if (homeTask == null || !isWithinDisplay(homeTask)) {
638 return;
639 }
Matthew Ng8e265522017-02-13 11:09:37 -0800640
641 // Do not minimize when dock is already minimized while keyguard is showing and not
642 // occluded such as unlocking the screen
643 if (mMinimizedDock && mService.mPolicy.isKeyguardShowingAndNotOccluded()) {
644 return;
645 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800646 final TaskStack topSecondaryStack = mDisplayContent.getTopStackInWindowingMode(
Wale Ogunwaleb62139d2017-09-20 15:37:35 -0700647 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800648 boolean homeVisible = homeTask.getTopVisibleAppToken() != null;
649 if (homeVisible && topSecondaryStack != null) {
650 // Home should only be considered visible if it is greater or equal to the top secondary
651 // stack in terms of z-order.
652 homeVisible = homeStack.compareTo(topSecondaryStack) >= 0;
653 }
654 setMinimizedDockedStack(homeVisible, animate);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800655 }
656
657 private boolean isWithinDisplay(Task task) {
658 task.mStack.getBounds(mTmpRect);
659 mDisplayContent.getLogicalDisplayRect(mTmpRect2);
660 return mTmpRect.intersect(mTmpRect2);
661 }
662
663 /**
664 * Sets whether the docked stack is currently in a minimized state, i.e. all the tasks in the
665 * docked stack are heavily clipped so you can only see a minimal peek state.
666 *
667 * @param minimizedDock Whether the docked stack is currently minimized.
668 * @param animate Whether to animate the change.
669 */
670 private void setMinimizedDockedStack(boolean minimizedDock, boolean animate) {
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700671 final boolean wasMinimized = mMinimizedDock;
672 mMinimizedDock = minimizedDock;
Jorim Jaggi817a5242016-05-06 15:45:00 -0700673 if (minimizedDock == wasMinimized) {
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800674 return;
675 }
676
Chong Zhang22eff0a2016-07-01 14:48:11 -0700677 final boolean imeChanged = clearImeAdjustAnimation();
678 boolean minimizedChange = false;
Matthew Nge15352e2016-12-20 15:36:29 -0800679 if (isHomeStackResizable()) {
680 notifyDockedStackMinimizedChanged(minimizedDock, true /* animate */,
681 true /* isHomeStackResizable */);
682 minimizedChange = true;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800683 } else {
Matthew Nge15352e2016-12-20 15:36:29 -0800684 if (minimizedDock) {
685 if (animate) {
686 startAdjustAnimation(0f, 1f);
687 } else {
688 minimizedChange |= setMinimizedDockedStack(true);
689 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800690 } else {
Matthew Nge15352e2016-12-20 15:36:29 -0800691 if (animate) {
692 startAdjustAnimation(1f, 0f);
693 } else {
694 minimizedChange |= setMinimizedDockedStack(false);
695 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800696 }
697 }
Chong Zhang22eff0a2016-07-01 14:48:11 -0700698 if (imeChanged || minimizedChange) {
699 if (imeChanged && !minimizedChange) {
700 Slog.d(TAG, "setMinimizedDockedStack: IME adjust changed due to minimizing,"
701 + " minimizedDock=" + minimizedDock
702 + " minimizedChange=" + minimizedChange);
703 }
704 mService.mWindowPlacerLocked.performSurfacePlacement();
705 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800706 }
707
Chong Zhang22eff0a2016-07-01 14:48:11 -0700708 private boolean clearImeAdjustAnimation() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700709 final boolean changed = mDisplayContent.clearImeAdjustAnimation();
Jorim Jaggieb88d832016-04-13 20:17:43 -0700710 mAnimatingForIme = false;
Chong Zhang22eff0a2016-07-01 14:48:11 -0700711 return changed;
Jorim Jaggieb88d832016-04-13 20:17:43 -0700712 }
713
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800714 private void startAdjustAnimation(float from, float to) {
Chong Zhangbaba7832016-03-24 10:21:26 -0700715 mAnimatingForMinimizedDockedStack = true;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800716 mAnimationStarted = false;
717 mAnimationStart = from;
718 mAnimationTarget = to;
719 }
720
Chong Zhangf347ab52016-04-18 21:02:01 -0700721 private void startImeAdjustAnimation(
722 boolean adjustedForIme, boolean adjustedForDivider, WindowState imeWin) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700723
724 // If we're not in an animation, the starting point depends on whether we're adjusted
725 // or not. If we're already in an animation, we start from where the current animation
726 // left off, so that the motion doesn't look discontinuous.
727 if (!mAnimatingForIme) {
728 mAnimationStart = mAdjustedForIme ? 1 : 0;
729 mDividerAnimationStart = mAdjustedForDivider ? 1 : 0;
730 mLastAnimationProgress = mAnimationStart;
731 mLastDividerProgress = mDividerAnimationStart;
732 } else {
733 mAnimationStart = mLastAnimationProgress;
734 mDividerAnimationStart = mLastDividerProgress;
735 }
Jorim Jaggi3070e772016-05-17 16:41:32 -0700736 mAnimatingForIme = true;
737 mAnimationStarted = false;
Chong Zhangf347ab52016-04-18 21:02:01 -0700738 mAnimationTarget = adjustedForIme ? 1 : 0;
739 mDividerAnimationTarget = adjustedForDivider ? 1 : 0;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700740
Wale Ogunwale10124582016-09-15 20:25:50 -0700741 mDisplayContent.beginImeAdjustAnimation();
Jorim Jaggiff71d202016-04-14 13:12:36 -0700742
743 // We put all tasks into drag resizing mode - wait until all of them have completed the
744 // drag resizing switch.
745 if (!mService.mWaitingForDrawn.isEmpty()) {
746 mService.mH.removeMessages(H.WAITING_FOR_DRAWN_TIMEOUT);
747 mService.mH.sendEmptyMessageDelayed(H.WAITING_FOR_DRAWN_TIMEOUT,
748 IME_ADJUST_DRAWN_TIMEOUT);
749 mAnimationStartDelayed = true;
750 if (imeWin != null) {
751
752 // There might be an old window delaying the animation start - clear it.
753 if (mDelayedImeWin != null) {
754 mDelayedImeWin.mWinAnimator.endDelayingAnimationStart();
755 }
756 mDelayedImeWin = imeWin;
757 imeWin.mWinAnimator.startDelayingAnimationStart();
758 }
Jorim Jaggi31883862016-11-04 15:45:30 -0700759
760 // If we are already waiting for something to be drawn, clear out the old one so it
761 // still gets executed.
762 // TODO: Have a real system where we can wait on different windows to be drawn with
763 // different callbacks.
764 if (mService.mWaitingForDrawnCallback != null) {
765 mService.mWaitingForDrawnCallback.run();
766 }
Jorim Jaggiff71d202016-04-14 13:12:36 -0700767 mService.mWaitingForDrawnCallback = () -> {
768 mAnimationStartDelayed = false;
769 if (mDelayedImeWin != null) {
770 mDelayedImeWin.mWinAnimator.endDelayingAnimationStart();
771 }
Chong Zhang22eff0a2016-07-01 14:48:11 -0700772 // If the adjust status changed since this was posted, only notify
773 // the new states and don't animate.
774 long duration = 0;
775 if (mAdjustedForIme == adjustedForIme
776 && mAdjustedForDivider == adjustedForDivider) {
777 duration = IME_ADJUST_ANIM_DURATION;
778 } else {
779 Slog.w(TAG, "IME adjust changed while waiting for drawn:"
780 + " adjustedForIme=" + adjustedForIme
781 + " adjustedForDivider=" + adjustedForDivider
782 + " mAdjustedForIme=" + mAdjustedForIme
783 + " mAdjustedForDivider=" + mAdjustedForDivider);
784 }
Chong Zhangf347ab52016-04-18 21:02:01 -0700785 notifyAdjustedForImeChanged(
Chong Zhang22eff0a2016-07-01 14:48:11 -0700786 mAdjustedForIme || mAdjustedForDivider, duration);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700787 };
788 } else {
Chong Zhangf347ab52016-04-18 21:02:01 -0700789 notifyAdjustedForImeChanged(
790 adjustedForIme || adjustedForDivider, IME_ADJUST_ANIM_DURATION);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700791 }
Jorim Jaggieb88d832016-04-13 20:17:43 -0700792 }
793
Chong Zhang22eff0a2016-07-01 14:48:11 -0700794 private boolean setMinimizedDockedStack(boolean minimized) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700795 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Matthew Nge15352e2016-12-20 15:36:29 -0800796 notifyDockedStackMinimizedChanged(minimized, false /* animate */, isHomeStackResizable());
Chong Zhang22eff0a2016-07-01 14:48:11 -0700797 return stack != null && stack.setAdjustedForMinimizedDock(minimized ? 1f : 0f);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800798 }
799
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800800 private boolean isAnimationMaximizing() {
801 return mAnimationTarget == 0f;
802 }
803
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800804 public boolean animate(long now) {
Wale Ogunwale20ec11b2016-04-22 12:11:51 -0700805 if (mWindow == null) {
806 return false;
807 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700808 if (mAnimatingForMinimizedDockedStack) {
809 return animateForMinimizedDockedStack(now);
810 } else if (mAnimatingForIme) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700811 return animateForIme(now);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800812 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700813 return false;
Chong Zhangbaba7832016-03-24 10:21:26 -0700814 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800815
Jorim Jaggieb88d832016-04-13 20:17:43 -0700816 private boolean animateForIme(long now) {
Jorim Jaggiff71d202016-04-14 13:12:36 -0700817 if (!mAnimationStarted || mAnimationStartDelayed) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700818 mAnimationStarted = true;
819 mAnimationStartTime = now;
820 mAnimationDuration = (long)
Jorim Jaggi698e7632016-04-13 21:02:22 -0700821 (IME_ADJUST_ANIM_DURATION * mService.getWindowAnimationScaleLocked());
Jorim Jaggieb88d832016-04-13 20:17:43 -0700822 }
823 float t = Math.min(1f, (float) (now - mAnimationStartTime) / mAnimationDuration);
824 t = (mAnimationTarget == 1f ? IME_ADJUST_ENTRY_INTERPOLATOR : TOUCH_RESPONSE_INTERPOLATOR)
825 .getInterpolation(t);
Wale Ogunwale10124582016-09-15 20:25:50 -0700826 final boolean updated =
827 mDisplayContent.animateForIme(t, mAnimationTarget, mDividerAnimationTarget);
Jorim Jaggieb88d832016-04-13 20:17:43 -0700828 if (updated) {
829 mService.mWindowPlacerLocked.performSurfacePlacement();
830 }
831 if (t >= 1.0f) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700832 mLastAnimationProgress = mAnimationTarget;
833 mLastDividerProgress = mDividerAnimationTarget;
Jorim Jaggieb88d832016-04-13 20:17:43 -0700834 mAnimatingForIme = false;
835 return false;
836 } else {
837 return true;
838 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700839 }
840
841 private boolean animateForMinimizedDockedStack(long now) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700842 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800843 if (!mAnimationStarted) {
844 mAnimationStarted = true;
845 mAnimationStartTime = now;
Matthew Nge15352e2016-12-20 15:36:29 -0800846 notifyDockedStackMinimizedChanged(mMinimizedDock, true /* animate */,
847 isHomeStackResizable() /* isHomeStackResizable */);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800848 }
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800849 float t = Math.min(1f, (float) (now - mAnimationStartTime) / mAnimationDuration);
850 t = (isAnimationMaximizing() ? TOUCH_RESPONSE_INTERPOLATOR : mMinimizedDockInterpolator)
851 .getInterpolation(t);
Chong Zhang741c0ba2016-05-27 12:52:11 -0700852 if (stack != null) {
853 if (stack.setAdjustedForMinimizedDock(getMinimizeAmount(stack, t))) {
854 mService.mWindowPlacerLocked.performSurfacePlacement();
855 }
856 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800857 if (t >= 1.0f) {
Chong Zhangbaba7832016-03-24 10:21:26 -0700858 mAnimatingForMinimizedDockedStack = false;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800859 return false;
860 } else {
861 return true;
862 }
863 }
864
Wale Ogunwale10124582016-09-15 20:25:50 -0700865 float getInterpolatedAnimationValue(float t) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700866 return t * mAnimationTarget + (1 - t) * mAnimationStart;
867 }
868
Wale Ogunwale10124582016-09-15 20:25:50 -0700869 float getInterpolatedDividerValue(float t) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700870 return t * mDividerAnimationTarget + (1 - t) * mDividerAnimationStart;
871 }
872
Jorim Jaggif97ed922016-02-18 18:57:07 -0800873 /**
874 * Gets the amount how much to minimize a stack depending on the interpolated fraction t.
875 */
876 private float getMinimizeAmount(TaskStack stack, float t) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700877 final float naturalAmount = getInterpolatedAnimationValue(t);
Jorim Jaggif97ed922016-02-18 18:57:07 -0800878 if (isAnimationMaximizing()) {
879 return adjustMaximizeAmount(stack, t, naturalAmount);
880 } else {
881 return naturalAmount;
882 }
883 }
884
885 /**
886 * When maximizing the stack during a clip reveal transition, this adjusts the minimize amount
887 * during the transition such that the edge of the clip reveal rect is met earlier in the
888 * transition so we don't create a visible "hole", but only if both the clip reveal and the
889 * docked stack divider start from about the same portion on the screen.
890 */
891 private float adjustMaximizeAmount(TaskStack stack, float t, float naturalAmount) {
892 if (mMaximizeMeetFraction == 1f) {
893 return naturalAmount;
894 }
895 final int minimizeDistance = stack.getMinimizeDistance();
896 float startPrime = mService.mAppTransition.getLastClipRevealMaxTranslation()
897 / (float) minimizeDistance;
898 final float amountPrime = t * mAnimationTarget + (1 - t) * startPrime;
899 final float t2 = Math.min(t / mMaximizeMeetFraction, 1);
900 return amountPrime * t2 + naturalAmount * (1 - t2);
901 }
902
903 /**
904 * Retrieves the animation fraction at which the docked stack has to meet the clip reveal
905 * edge. See {@link #adjustMaximizeAmount}.
906 */
907 private float getClipRevealMeetFraction(TaskStack stack) {
908 if (!isAnimationMaximizing() || stack == null ||
909 !mService.mAppTransition.hadClipRevealAnimation()) {
910 return 1f;
911 }
912 final int minimizeDistance = stack.getMinimizeDistance();
913 final float fraction = Math.abs(mService.mAppTransition.getLastClipRevealMaxTranslation())
914 / (float) minimizeDistance;
915 final float t = Math.max(0, Math.min(1, (fraction - CLIP_REVEAL_MEET_FRACTION_MIN)
916 / (CLIP_REVEAL_MEET_FRACTION_MAX - CLIP_REVEAL_MEET_FRACTION_MIN)));
917 return CLIP_REVEAL_MEET_EARLIEST
918 + (1 - t) * (CLIP_REVEAL_MEET_LAST - CLIP_REVEAL_MEET_EARLIEST);
919 }
920
Jorim Jaggi50981592015-12-29 17:54:12 +0100921 public String toShortString() {
922 return TAG;
923 }
Robert Carre63e01a2016-04-18 20:27:34 -0700924
925 WindowState getWindow() {
926 return mWindow;
927 }
Jorim Jaggi31f71702016-05-04 16:43:04 -0700928
929 void dump(String prefix, PrintWriter pw) {
930 pw.println(prefix + "DockedStackDividerController");
931 pw.println(prefix + " mLastVisibility=" + mLastVisibility);
932 pw.println(prefix + " mMinimizedDock=" + mMinimizedDock);
933 pw.println(prefix + " mAdjustedForIme=" + mAdjustedForIme);
934 pw.println(prefix + " mAdjustedForDivider=" + mAdjustedForDivider);
Jorim Jaggi31f71702016-05-04 16:43:04 -0700935 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700936
937 void writeToProto(ProtoOutputStream proto, long fieldId) {
938 final long token = proto.start(fieldId);
939 proto.write(MINIMIZED_DOCK, mMinimizedDock);
940 proto.end(token);
941 }
Robert Carre63e01a2016-04-18 20:27:34 -0700942}