blob: d79ba897150b9ba0ae4137adad5c0f253a51bebb [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 Jaggi50981592015-12-29 17:54:12 +010057import com.android.server.wm.DimLayer.DimLayerUser;
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 */
Jorim Jaggi50981592015-12-29 17:54:12 +010065public class DockedStackDividerController implements DimLayerUser {
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<>();
Jorim Jaggi50981592015-12-29 17:54:12 +0100117 private final DimLayer mDimLayer;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700118
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800119 private boolean mMinimizedDock;
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700120 private int mOriginalDockedSide = DOCKED_INVALID;
Chong Zhangbaba7832016-03-24 10:21:26 -0700121 private boolean mAnimatingForMinimizedDockedStack;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800122 private boolean mAnimationStarted;
123 private long mAnimationStartTime;
124 private float mAnimationStart;
125 private float mAnimationTarget;
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800126 private long mAnimationDuration;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700127 private boolean mAnimationStartDelayed;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800128 private final Interpolator mMinimizedDockInterpolator;
Jorim Jaggif97ed922016-02-18 18:57:07 -0800129 private float mMaximizeMeetFraction;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100130 private final Rect mTouchRegion = new Rect();
Chong Zhangbaba7832016-03-24 10:21:26 -0700131 private boolean mAnimatingForIme;
132 private boolean mAdjustedForIme;
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700133 private int mImeHeight;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700134 private WindowState mDelayedImeWin;
Chong Zhangf347ab52016-04-18 21:02:01 -0700135 private boolean mAdjustedForDivider;
136 private float mDividerAnimationStart;
137 private float mDividerAnimationTarget;
Wale Ogunwale10124582016-09-15 20:25:50 -0700138 float mLastAnimationProgress;
139 float mLastDividerProgress;
Jorim Jaggi85639432016-05-06 17:27:55 -0700140 private final DividerSnapAlgorithm[] mSnapAlgorithmForRotation = new DividerSnapAlgorithm[4];
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700141 private boolean mImeHideRequested;
Jorim Jaggif93ac2b2017-10-25 15:20:24 +0200142 private final Rect mLastDimLayerRect = new Rect();
143 private float mLastDimLayerAlpha;
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 Jaggibc5425c2016-03-01 13:51:16 +0100149 mDimLayer = new DimLayer(displayContent.mService, this, displayContent.getDisplayId(),
150 "DockedStackDim");
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800151 mMinimizedDockInterpolator = AnimationUtils.loadInterpolator(
152 context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700153 loadDimens();
154 }
155
Jorim Jaggi85639432016-05-06 17:27:55 -0700156 int getSmallestWidthDpForBounds(Rect bounds) {
157 final DisplayInfo di = mDisplayContent.getDisplayInfo();
158
Jorim Jaggi85639432016-05-06 17:27:55 -0700159 final int baseDisplayWidth = mDisplayContent.mBaseDisplayWidth;
160 final int baseDisplayHeight = mDisplayContent.mBaseDisplayHeight;
161 int minWidth = Integer.MAX_VALUE;
162
163 // Go through all screen orientations and find the orientation in which the task has the
164 // smallest width.
165 for (int rotation = 0; rotation < 4; rotation++) {
166 mTmpRect.set(bounds);
167 mDisplayContent.rotateBounds(di.rotation, rotation, mTmpRect);
168 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
169 mTmpRect2.set(0, 0,
170 rotated ? baseDisplayHeight : baseDisplayWidth,
171 rotated ? baseDisplayWidth : baseDisplayHeight);
172 final int orientation = mTmpRect2.width() <= mTmpRect2.height()
173 ? ORIENTATION_PORTRAIT
174 : ORIENTATION_LANDSCAPE;
175 final int dockSide = TaskStack.getDockSideUnchecked(mTmpRect, mTmpRect2, orientation);
176 final int position = DockedDividerUtils.calculatePositionForBounds(mTmpRect, dockSide,
177 getContentWidth());
178
179 // Since we only care about feasible states, snap to the closest snap target, like it
180 // would happen when actually rotating the screen.
181 final int snappedPosition = mSnapAlgorithmForRotation[rotation]
182 .calculateNonDismissingSnapTarget(position).position;
183 DockedDividerUtils.calculateBoundsForPosition(snappedPosition, dockSide, mTmpRect,
184 mTmpRect2.width(), mTmpRect2.height(), getContentWidth());
185 mService.mPolicy.getStableInsetsLw(rotation, mTmpRect2.width(), mTmpRect2.height(),
186 mTmpRect3);
Winson Chungbdc646f2017-02-13 12:12:22 -0800187 mService.intersectDisplayInsetBounds(mTmpRect2, mTmpRect3, mTmpRect);
Jorim Jaggi85639432016-05-06 17:27:55 -0700188 minWidth = Math.min(mTmpRect.width(), minWidth);
189 }
190 return (int) (minWidth / mDisplayContent.getDisplayMetrics().density);
191 }
192
Matthew Nge15352e2016-12-20 15:36:29 -0800193 void getHomeStackBoundsInDockedMode(Rect outBounds) {
194 final DisplayInfo di = mDisplayContent.getDisplayInfo();
195 mService.mPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
196 mTmpRect);
197 int dividerSize = mDividerWindowWidth - 2 * mDividerInsets;
198 Configuration configuration = mDisplayContent.getConfiguration();
Matthew Nga9e173d2017-05-17 15:03:18 -0700199 // The offset in the left (landscape)/top (portrait) is calculated with the minimized
200 // offset value with the divider size and any system insets in that direction.
Matthew Nge15352e2016-12-20 15:36:29 -0800201 if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
202 outBounds.set(0, mTaskHeightInMinimizedMode + dividerSize + mTmpRect.top,
203 di.logicalWidth, di.logicalHeight);
204 } else {
Matthew Nga9e173d2017-05-17 15:03:18 -0700205 // In landscape append the left position with the statusbar height to match the
206 // minimized size height in portrait mode.
207 outBounds.set(mTaskHeightInMinimizedMode + dividerSize + mTmpRect.left + mTmpRect.top,
208 0, di.logicalWidth, di.logicalHeight);
Matthew Nge15352e2016-12-20 15:36:29 -0800209 }
210 }
211
212 boolean isHomeStackResizable() {
213 final TaskStack homeStack = mDisplayContent.getHomeStack();
214 if (homeStack == null) {
215 return false;
216 }
217 final Task homeTask = homeStack.findHomeTask();
218 return homeTask != null && homeTask.isResizeable();
219 }
220
Jorim Jaggi85639432016-05-06 17:27:55 -0700221 private void initSnapAlgorithmForRotations() {
Andrii Kulian441e4492016-09-29 15:25:00 -0700222 final Configuration baseConfig = mDisplayContent.getConfiguration();
Jorim Jaggi85639432016-05-06 17:27:55 -0700223
224 // Initialize the snap algorithms for all 4 screen orientations.
225 final Configuration config = new Configuration();
226 for (int rotation = 0; rotation < 4; rotation++) {
227 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
228 final int dw = rotated
229 ? mDisplayContent.mBaseDisplayHeight
230 : mDisplayContent.mBaseDisplayWidth;
231 final int dh = rotated
232 ? mDisplayContent.mBaseDisplayWidth
233 : mDisplayContent.mBaseDisplayHeight;
234 mService.mPolicy.getStableInsetsLw(rotation, dw, dh, mTmpRect);
Andrii Kulianb10330d2016-09-16 13:51:46 -0700235 config.unset();
Jorim Jaggi85639432016-05-06 17:27:55 -0700236 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Bryce Lee7566d762017-03-30 09:34:15 -0700237
238 final int displayId = mDisplayContent.getDisplayId();
239 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation,
240 baseConfig.uiMode, displayId);
241 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
242 baseConfig.uiMode, displayId);
243 mService.mPolicy.getNonDecorInsetsLw(rotation, dw, dh, mTmpRect);
244 final int leftInset = mTmpRect.left;
245 final int topInset = mTmpRect.top;
246
Wale Ogunwale822e5122017-07-26 06:02:24 -0700247 config.windowConfiguration.setAppBounds(leftInset /*left*/, topInset /*top*/,
248 leftInset + appWidth /*right*/, topInset + appHeight /*bottom*/);
Bryce Lee7566d762017-03-30 09:34:15 -0700249
Jorim Jaggi85639432016-05-06 17:27:55 -0700250 config.screenWidthDp = (int)
Andrii Kuliandb8e1062016-11-15 18:30:27 -0800251 (mService.mPolicy.getConfigDisplayWidth(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 config.screenHeightDp = (int)
Andrii Kuliandb8e1062016-11-15 18:30:27 -0800254 (mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, baseConfig.uiMode,
Bryce Lee7566d762017-03-30 09:34:15 -0700255 displayId) / mDisplayContent.getDisplayMetrics().density);
Jorim Jaggi85639432016-05-06 17:27:55 -0700256 final Context rotationContext = mService.mContext.createConfigurationContext(config);
257 mSnapAlgorithmForRotation[rotation] = new DividerSnapAlgorithm(
258 rotationContext.getResources(), dw, dh, getContentWidth(),
259 config.orientation == ORIENTATION_PORTRAIT, mTmpRect);
260 }
261 }
262
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700263 private void loadDimens() {
264 final Context context = mService.mContext;
265 mDividerWindowWidth = context.getResources().getDimensionPixelSize(
266 com.android.internal.R.dimen.docked_stack_divider_thickness);
267 mDividerInsets = context.getResources().getDimensionPixelSize(
268 com.android.internal.R.dimen.docked_stack_divider_insets);
Chong Zhang198afac2016-04-15 12:03:11 -0700269 mDividerWindowWidthInactive = WindowManagerService.dipToPixel(
270 DIVIDER_WIDTH_INACTIVE_DP, mDisplayContent.getDisplayMetrics());
Matthew Nge15352e2016-12-20 15:36:29 -0800271 mTaskHeightInMinimizedMode = context.getResources().getDimensionPixelSize(
272 com.android.internal.R.dimen.task_height_of_minimized_mode);
Jorim Jaggi85639432016-05-06 17:27:55 -0700273 initSnapAlgorithmForRotations();
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700274 }
275
276 void onConfigurationChanged() {
277 loadDimens();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700278 }
279
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100280 boolean isResizing() {
281 return mResizing;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700282 }
283
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100284 int getContentWidth() {
285 return mDividerWindowWidth - 2 * mDividerInsets;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700286 }
287
Jorim Jaggi81ba11e2016-02-03 22:04:22 -0800288 int getContentInsets() {
289 return mDividerInsets;
290 }
291
Chong Zhang198afac2016-04-15 12:03:11 -0700292 int getContentWidthInactive() {
293 return mDividerWindowWidthInactive;
294 }
295
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100296 void setResizing(boolean resizing) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800297 if (mResizing != resizing) {
298 mResizing = resizing;
299 resetDragResizingChangeReported();
300 }
301 }
302
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100303 void setTouchRegion(Rect touchRegion) {
304 mTouchRegion.set(touchRegion);
305 }
306
307 void getTouchRegion(Rect outRegion) {
308 outRegion.set(mTouchRegion);
309 outRegion.offset(mWindow.getFrameLw().left, mWindow.getFrameLw().top);
310 }
311
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800312 private void resetDragResizingChangeReported() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800313 mDisplayContent.forAllWindows(WindowState::resetDragResizingChangeReported,
314 true /* traverseTopToBottom */ );
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100315 }
316
317 void setWindow(WindowState window) {
318 mWindow = window;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800319 reevaluateVisibility(false);
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100320 }
321
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800322 void reevaluateVisibility(boolean force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800323 if (mWindow == null) {
324 return;
325 }
Matthew Ng64e77cf2017-10-31 14:01:31 -0700326 TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Jorim Jaggi7998e482016-02-12 18:47:06 -0800327
328 // If the stack is invisible, we policy force hide it in WindowAnimator.shouldForceHide
329 final boolean visible = stack != null;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800330 if (mLastVisibility == visible && !force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800331 return;
332 }
333 mLastVisibility = visible;
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100334 notifyDockedDividerVisibilityChanged(visible);
Jorim Jaggi50981592015-12-29 17:54:12 +0100335 if (!visible) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700336 setResizeDimLayer(false, WINDOWING_MODE_UNDEFINED, 0f);
Jorim Jaggi50981592015-12-29 17:54:12 +0100337 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100338 }
339
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700340 private boolean wasVisible() {
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100341 return mLastVisibility;
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100342 }
343
Chong Zhangf347ab52016-04-18 21:02:01 -0700344 void setAdjustedForIme(
345 boolean adjustedForIme, boolean adjustedForDivider,
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700346 boolean animate, WindowState imeWin, int imeHeight) {
347 if (mAdjustedForIme != adjustedForIme || (adjustedForIme && mImeHeight != imeHeight)
348 || mAdjustedForDivider != adjustedForDivider) {
Jorim Jaggi3070e772016-05-17 16:41:32 -0700349 if (animate && !mAnimatingForMinimizedDockedStack) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700350 startImeAdjustAnimation(adjustedForIme, adjustedForDivider, imeWin);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700351 } else {
Jorim Jaggiff71d202016-04-14 13:12:36 -0700352 // Animation might be delayed, so only notify if we don't run an animation.
Chong Zhangf347ab52016-04-18 21:02:01 -0700353 notifyAdjustedForImeChanged(adjustedForIme || adjustedForDivider, 0 /* duration */);
Jorim Jaggieb88d832016-04-13 20:17:43 -0700354 }
Chong Zhangf347ab52016-04-18 21:02:01 -0700355 mAdjustedForIme = adjustedForIme;
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700356 mImeHeight = imeHeight;
Chong Zhangf347ab52016-04-18 21:02:01 -0700357 mAdjustedForDivider = adjustedForDivider;
Chong Zhangbaba7832016-03-24 10:21:26 -0700358 }
Chong Zhangb58bbcc2016-03-23 11:57:36 -0700359 }
360
Keisuke Kuroyanagi19d9a8f2016-05-12 16:49:02 -0700361 int getImeHeightAdjustedFor() {
362 return mImeHeight;
363 }
364
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700365 void positionDockedStackedDivider(Rect frame) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700366 TaskStack stack = mDisplayContent.getSplitScreenPrimaryStack();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700367 if (stack == null) {
368 // Unfortunately we might end up with still having a divider, even though the underlying
369 // stack was already removed. This is because we are on AM thread and the removal of the
Filip Gruszczynski77049052015-11-09 14:01:21 -0800370 // divider was deferred to WM thread and hasn't happened yet. In that case let's just
371 // keep putting it in the same place it was before the stack was removed to have
372 // continuity and prevent it from jumping to the center. It will get hidden soon.
373 frame.set(mLastRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700374 return;
Filip Gruszczynski77049052015-11-09 14:01:21 -0800375 } else {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800376 stack.getDimBounds(mTmpRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700377 }
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100378 int side = stack.getDockSide();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700379 switch (side) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700380 case DOCKED_LEFT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100381 frame.set(mTmpRect.right - mDividerInsets, frame.top,
382 mTmpRect.right + frame.width() - mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700383 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700384 case DOCKED_TOP:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100385 frame.set(frame.left, mTmpRect.bottom - mDividerInsets,
386 mTmpRect.right, mTmpRect.bottom + frame.height() - mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700387 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700388 case DOCKED_RIGHT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100389 frame.set(mTmpRect.left - frame.width() + mDividerInsets, frame.top,
390 mTmpRect.left + mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700391 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700392 case DOCKED_BOTTOM:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100393 frame.set(frame.left, mTmpRect.top - frame.height() + mDividerInsets,
394 frame.right, mTmpRect.top + mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700395 break;
396 }
Filip Gruszczynski77049052015-11-09 14:01:21 -0800397 mLastRect.set(frame);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700398 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800399
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700400 private void notifyDockedDividerVisibilityChanged(boolean visible) {
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100401 final int size = mDockedStackListeners.beginBroadcast();
402 for (int i = 0; i < size; ++i) {
403 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
404 try {
405 listener.onDividerVisibilityChanged(visible);
406 } catch (RemoteException e) {
407 Slog.e(TAG_WM, "Error delivering divider visibility changed event.", e);
408 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800409 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100410 mDockedStackListeners.finishBroadcast();
411 }
412
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700413 /**
414 * Checks if the primary stack is allowed to dock to a specific side based on its original dock
415 * side.
416 *
417 * @param dockSide the side to see if it is valid
418 * @return true if the side provided is valid
419 */
420 boolean canPrimaryStackDockTo(int dockSide) {
421 if (mService.mPolicy.isDockSideAllowed(dockSide)) {
422 // Side is the same as original side
423 if (dockSide == mOriginalDockedSide) {
424 return true;
425 }
426 // Special rule that the top in portrait is always valid
427 if (dockSide == DOCKED_TOP) {
428 return true;
429 }
430 // Only if original docked side was top in portrait will allow left side for landscape
431 if (dockSide == DOCKED_LEFT && mOriginalDockedSide == DOCKED_TOP) {
432 return true;
433 }
434 }
435 return false;
436 }
437
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100438 void notifyDockedStackExistsChanged(boolean exists) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700439 // TODO(multi-display): Perform all actions only for current display.
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100440 final int size = mDockedStackListeners.beginBroadcast();
441 for (int i = 0; i < size; ++i) {
442 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
443 try {
444 listener.onDockedStackExistsChanged(exists);
445 } catch (RemoteException e) {
446 Slog.e(TAG_WM, "Error delivering docked stack exists changed event.", e);
447 }
448 }
449 mDockedStackListeners.finishBroadcast();
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700450 if (exists) {
451 InputMethodManagerInternal inputMethodManagerInternal =
452 LocalServices.getService(InputMethodManagerInternal.class);
453 if (inputMethodManagerInternal != null) {
454
455 // Hide the current IME to avoid problems with animations from IME adjustment when
456 // attaching the docked stack.
457 inputMethodManagerInternal.hideCurrentInputMethod();
458 mImeHideRequested = true;
459 }
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700460 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
461 mOriginalDockedSide = stack.getDockSide();
Matthew Ngbc527a32017-06-19 16:42:31 -0700462 return;
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700463 }
Matthew Ngcb1b8e42017-10-20 16:29:23 -0700464 mOriginalDockedSide = DOCKED_INVALID;
Matthew Ngbc527a32017-06-19 16:42:31 -0700465 setMinimizedDockedStack(false /* minimizedDock */, false /* animate */);
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100466 }
467
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700468 /**
469 * Resets the state that IME hide has been requested. See {@link #isImeHideRequested}.
470 */
471 void resetImeHideRequested() {
472 mImeHideRequested = false;
473 }
474
475 /**
476 * The docked stack divider controller makes sure the IME gets hidden when attaching the docked
477 * stack, to avoid animation problems. This flag indicates whether the request to hide the IME
478 * has been sent in an asynchronous manner, and the IME should be treated as hidden already.
479 *
480 * @return whether IME hide request has been sent
481 */
482 boolean isImeHideRequested() {
483 return mImeHideRequested;
484 }
485
Matthew Nge15352e2016-12-20 15:36:29 -0800486 private void notifyDockedStackMinimizedChanged(boolean minimizedDock, boolean animate,
487 boolean isHomeStackResizable) {
488 long animDuration = 0;
489 if (animate) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -0700490 final TaskStack stack =
Matthew Ng64e77cf2017-10-31 14:01:31 -0700491 mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Matthew Nge15352e2016-12-20 15:36:29 -0800492 final long transitionDuration = isAnimationMaximizing()
493 ? mService.mAppTransition.getLastClipRevealTransitionDuration()
494 : DEFAULT_APP_TRANSITION_DURATION;
495 mAnimationDuration = (long)
496 (transitionDuration * mService.getTransitionAnimationScaleLocked());
497 mMaximizeMeetFraction = getClipRevealMeetFraction(stack);
498 animDuration = (long) (mAnimationDuration * mMaximizeMeetFraction);
499 }
Tony Mak853304c2016-04-18 15:17:41 +0100500 mService.mH.removeMessages(NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED);
501 mService.mH.obtainMessage(NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED,
502 minimizedDock ? 1 : 0, 0).sendToTarget();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800503 final int size = mDockedStackListeners.beginBroadcast();
504 for (int i = 0; i < size; ++i) {
505 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
506 try {
Matthew Nge15352e2016-12-20 15:36:29 -0800507 listener.onDockedStackMinimizedChanged(minimizedDock, animDuration,
508 isHomeStackResizable);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800509 } catch (RemoteException e) {
510 Slog.e(TAG_WM, "Error delivering minimized dock changed event.", e);
511 }
512 }
513 mDockedStackListeners.finishBroadcast();
514 }
515
Jorim Jaggi2917dc42016-04-11 11:39:13 -0700516 void notifyDockSideChanged(int newDockSide) {
517 final int size = mDockedStackListeners.beginBroadcast();
518 for (int i = 0; i < size; ++i) {
519 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
520 try {
521 listener.onDockSideChanged(newDockSide);
522 } catch (RemoteException e) {
523 Slog.e(TAG_WM, "Error delivering dock side changed event.", e);
524 }
525 }
526 mDockedStackListeners.finishBroadcast();
527 }
528
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700529 private void notifyAdjustedForImeChanged(boolean adjustedForIme, long animDuration) {
Jorim Jaggi698e7632016-04-13 21:02:22 -0700530 final int size = mDockedStackListeners.beginBroadcast();
531 for (int i = 0; i < size; ++i) {
532 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
533 try {
534 listener.onAdjustedForImeChanged(adjustedForIme, animDuration);
535 } catch (RemoteException e) {
536 Slog.e(TAG_WM, "Error delivering adjusted for ime changed event.", e);
537 }
538 }
539 mDockedStackListeners.finishBroadcast();
540 }
541
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100542 void registerDockedStackListener(IDockedStackListener listener) {
543 mDockedStackListeners.register(listener);
544 notifyDockedDividerVisibilityChanged(wasVisible());
Wale Ogunwale61911492017-10-11 08:50:50 -0700545 notifyDockedStackExistsChanged(
Matthew Ng64e77cf2017-10-31 14:01:31 -0700546 mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility() != null);
Matthew Nge15352e2016-12-20 15:36:29 -0800547 notifyDockedStackMinimizedChanged(mMinimizedDock, false /* animate */,
548 isHomeStackResizable());
Jorim Jaggi698e7632016-04-13 21:02:22 -0700549 notifyAdjustedForImeChanged(mAdjustedForIme, 0 /* animDuration */);
550
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800551 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100552
Wale Ogunwale68278562017-09-23 17:13:55 -0700553 /**
554 * Shows a dim layer with {@param alpha} if {@param visible} is true and
555 * {@param targetWindowingMode} isn't
556 * {@link android.app.WindowConfiguration#WINDOWING_MODE_UNDEFINED} and there is a stack on the
557 * display in that windowing mode.
558 */
559 void setResizeDimLayer(boolean visible, int targetWindowingMode, float alpha) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700560 // TODO: Maybe only allow split-screen windowing modes?
561 final TaskStack stack = targetWindowingMode != WINDOWING_MODE_UNDEFINED
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800562 ? mDisplayContent.getTopStackInWindowingMode(targetWindowingMode)
Wale Ogunwale68278562017-09-23 17:13:55 -0700563 : null;
Matthew Ng64e77cf2017-10-31 14:01:31 -0700564 final TaskStack dockedStack = mDisplayContent.getSplitScreenPrimaryStack();
Jorim Jaggibc5425c2016-03-01 13:51:16 +0100565 boolean visibleAndValid = visible && stack != null && dockedStack != null;
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100566 if (visibleAndValid) {
Jorim Jaggi50981592015-12-29 17:54:12 +0100567 stack.getDimBounds(mTmpRect);
Jorim Jaggi10b89dc2016-01-05 15:40:17 +0100568 if (mTmpRect.height() > 0 && mTmpRect.width() > 0) {
Jorim Jaggif93ac2b2017-10-25 15:20:24 +0200569 if (!mLastDimLayerRect.equals(mTmpRect) || mLastDimLayerAlpha != alpha) {
570 try {
571 // TODO: This should use the regular animation transaction - here and below
572 mService.openSurfaceTransaction();
573 mDimLayer.setBounds(mTmpRect);
574 mDimLayer.show(getResizeDimLayer(), alpha, 0 /* duration */);
575 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200576 mService.closeSurfaceTransaction("setResizeDimLayer");
Jorim Jaggif93ac2b2017-10-25 15:20:24 +0200577 }
578 }
579 mLastDimLayerRect.set(mTmpRect);
580 mLastDimLayerAlpha = alpha;
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100581 } else {
582 visibleAndValid = false;
583 }
584 }
585 if (!visibleAndValid) {
Jorim Jaggif93ac2b2017-10-25 15:20:24 +0200586 if (mLastDimLayerAlpha != 0f) {
587 try {
588 mService.openSurfaceTransaction();
589 mDimLayer.hide();
590 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200591 mService.closeSurfaceTransaction("setResizeDimLayer");
Jorim Jaggif93ac2b2017-10-25 15:20:24 +0200592 }
593 }
594 mLastDimLayerAlpha = 0f;
Jorim Jaggi50981592015-12-29 17:54:12 +0100595 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100596 }
597
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800598 /**
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700599 * @return The layer used for dimming the apps when dismissing docked/fullscreen stack. Just
600 * above all application surfaces.
601 */
602 private int getResizeDimLayer() {
603 return (mWindow != null) ? mWindow.mLayer - 1 : LAYER_OFFSET_DIM;
604 }
605
606 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800607 * Notifies the docked stack divider controller of a visibility change that happens without
608 * an animation.
609 */
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700610 void notifyAppVisibilityChanged() {
611 checkMinimizeChanged(false /* animate */);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800612 }
613
Jorim Jaggi84afb1a2016-09-28 14:54:04 +0200614 void notifyAppTransitionStarting(ArraySet<AppWindowToken> openingApps, int appTransition) {
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700615 final boolean wasMinimized = mMinimizedDock;
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700616 checkMinimizeChanged(true /* animate */);
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700617
618 // We were minimized, and now we are still minimized, but somebody is trying to launch an
Matthew Ng8e265522017-02-13 11:09:37 -0800619 // app in docked stack, better show recent apps so we actually get unminimized! However do
620 // not do this if keyguard is dismissed such as when the device is unlocking. This catches
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700621 // any case that was missed in ActivityStarter.postStartActivityUncheckedProcessing because
622 // we couldn't retrace the launch of the app in the docked stack to the launch from
623 // homescreen.
Jorim Jaggi84afb1a2016-09-28 14:54:04 +0200624 if (wasMinimized && mMinimizedDock && containsAppInDockedStack(openingApps)
Matthew Ng8e265522017-02-13 11:09:37 -0800625 && appTransition != TRANSIT_NONE &&
626 !AppTransition.isKeyguardGoingAwayTransit(appTransition)) {
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700627 mService.showRecentApps(true /* fromHome */);
628 }
629 }
630
631 /**
632 * @return true if {@param apps} contains an activity in the docked stack, false otherwise.
633 */
634 private boolean containsAppInDockedStack(ArraySet<AppWindowToken> apps) {
635 for (int i = apps.size() - 1; i >= 0; i--) {
636 final AppWindowToken token = apps.valueAt(i);
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700637 if (token.getTask() != null && token.inSplitScreenPrimaryWindowingMode()) {
Jorim Jaggi936aaeb2016-08-26 19:02:11 -0700638 return true;
639 }
640 }
641 return false;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800642 }
643
Jorim Jaggi1ae68bf2016-05-09 18:44:34 -0700644 boolean isMinimizedDock() {
645 return mMinimizedDock;
646 }
647
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700648 private void checkMinimizeChanged(boolean animate) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700649 if (mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility() == null) {
Jorim Jaggi817a5242016-05-06 15:45:00 -0700650 return;
651 }
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700652 final TaskStack homeStack = mDisplayContent.getHomeStack();
653 if (homeStack == null) {
654 return;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800655 }
Jorim Jaggid3ec5072016-04-28 15:57:47 -0700656 final Task homeTask = homeStack.findHomeTask();
657 if (homeTask == null || !isWithinDisplay(homeTask)) {
658 return;
659 }
Matthew Ng8e265522017-02-13 11:09:37 -0800660
661 // Do not minimize when dock is already minimized while keyguard is showing and not
662 // occluded such as unlocking the screen
663 if (mMinimizedDock && mService.mPolicy.isKeyguardShowingAndNotOccluded()) {
664 return;
665 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800666 final TaskStack topSecondaryStack = mDisplayContent.getTopStackInWindowingMode(
Wale Ogunwaleb62139d2017-09-20 15:37:35 -0700667 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800668 boolean homeVisible = homeTask.getTopVisibleAppToken() != null;
669 if (homeVisible && topSecondaryStack != null) {
670 // Home should only be considered visible if it is greater or equal to the top secondary
671 // stack in terms of z-order.
672 homeVisible = homeStack.compareTo(topSecondaryStack) >= 0;
673 }
674 setMinimizedDockedStack(homeVisible, animate);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800675 }
676
677 private boolean isWithinDisplay(Task task) {
678 task.mStack.getBounds(mTmpRect);
679 mDisplayContent.getLogicalDisplayRect(mTmpRect2);
680 return mTmpRect.intersect(mTmpRect2);
681 }
682
683 /**
684 * Sets whether the docked stack is currently in a minimized state, i.e. all the tasks in the
685 * docked stack are heavily clipped so you can only see a minimal peek state.
686 *
687 * @param minimizedDock Whether the docked stack is currently minimized.
688 * @param animate Whether to animate the change.
689 */
690 private void setMinimizedDockedStack(boolean minimizedDock, boolean animate) {
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700691 final boolean wasMinimized = mMinimizedDock;
692 mMinimizedDock = minimizedDock;
Jorim Jaggi817a5242016-05-06 15:45:00 -0700693 if (minimizedDock == wasMinimized) {
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800694 return;
695 }
696
Chong Zhang22eff0a2016-07-01 14:48:11 -0700697 final boolean imeChanged = clearImeAdjustAnimation();
698 boolean minimizedChange = false;
Matthew Nge15352e2016-12-20 15:36:29 -0800699 if (isHomeStackResizable()) {
700 notifyDockedStackMinimizedChanged(minimizedDock, true /* animate */,
701 true /* isHomeStackResizable */);
702 minimizedChange = true;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800703 } else {
Matthew Nge15352e2016-12-20 15:36:29 -0800704 if (minimizedDock) {
705 if (animate) {
706 startAdjustAnimation(0f, 1f);
707 } else {
708 minimizedChange |= setMinimizedDockedStack(true);
709 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800710 } else {
Matthew Nge15352e2016-12-20 15:36:29 -0800711 if (animate) {
712 startAdjustAnimation(1f, 0f);
713 } else {
714 minimizedChange |= setMinimizedDockedStack(false);
715 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800716 }
717 }
Chong Zhang22eff0a2016-07-01 14:48:11 -0700718 if (imeChanged || minimizedChange) {
719 if (imeChanged && !minimizedChange) {
720 Slog.d(TAG, "setMinimizedDockedStack: IME adjust changed due to minimizing,"
721 + " minimizedDock=" + minimizedDock
722 + " minimizedChange=" + minimizedChange);
723 }
724 mService.mWindowPlacerLocked.performSurfacePlacement();
725 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800726 }
727
Chong Zhang22eff0a2016-07-01 14:48:11 -0700728 private boolean clearImeAdjustAnimation() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700729 final boolean changed = mDisplayContent.clearImeAdjustAnimation();
Jorim Jaggieb88d832016-04-13 20:17:43 -0700730 mAnimatingForIme = false;
Chong Zhang22eff0a2016-07-01 14:48:11 -0700731 return changed;
Jorim Jaggieb88d832016-04-13 20:17:43 -0700732 }
733
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800734 private void startAdjustAnimation(float from, float to) {
Chong Zhangbaba7832016-03-24 10:21:26 -0700735 mAnimatingForMinimizedDockedStack = true;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800736 mAnimationStarted = false;
737 mAnimationStart = from;
738 mAnimationTarget = to;
739 }
740
Chong Zhangf347ab52016-04-18 21:02:01 -0700741 private void startImeAdjustAnimation(
742 boolean adjustedForIme, boolean adjustedForDivider, WindowState imeWin) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700743
744 // If we're not in an animation, the starting point depends on whether we're adjusted
745 // or not. If we're already in an animation, we start from where the current animation
746 // left off, so that the motion doesn't look discontinuous.
747 if (!mAnimatingForIme) {
748 mAnimationStart = mAdjustedForIme ? 1 : 0;
749 mDividerAnimationStart = mAdjustedForDivider ? 1 : 0;
750 mLastAnimationProgress = mAnimationStart;
751 mLastDividerProgress = mDividerAnimationStart;
752 } else {
753 mAnimationStart = mLastAnimationProgress;
754 mDividerAnimationStart = mLastDividerProgress;
755 }
Jorim Jaggi3070e772016-05-17 16:41:32 -0700756 mAnimatingForIme = true;
757 mAnimationStarted = false;
Chong Zhangf347ab52016-04-18 21:02:01 -0700758 mAnimationTarget = adjustedForIme ? 1 : 0;
759 mDividerAnimationTarget = adjustedForDivider ? 1 : 0;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700760
Wale Ogunwale10124582016-09-15 20:25:50 -0700761 mDisplayContent.beginImeAdjustAnimation();
Jorim Jaggiff71d202016-04-14 13:12:36 -0700762
763 // We put all tasks into drag resizing mode - wait until all of them have completed the
764 // drag resizing switch.
765 if (!mService.mWaitingForDrawn.isEmpty()) {
766 mService.mH.removeMessages(H.WAITING_FOR_DRAWN_TIMEOUT);
767 mService.mH.sendEmptyMessageDelayed(H.WAITING_FOR_DRAWN_TIMEOUT,
768 IME_ADJUST_DRAWN_TIMEOUT);
769 mAnimationStartDelayed = true;
770 if (imeWin != null) {
771
772 // There might be an old window delaying the animation start - clear it.
773 if (mDelayedImeWin != null) {
774 mDelayedImeWin.mWinAnimator.endDelayingAnimationStart();
775 }
776 mDelayedImeWin = imeWin;
777 imeWin.mWinAnimator.startDelayingAnimationStart();
778 }
Jorim Jaggi31883862016-11-04 15:45:30 -0700779
780 // If we are already waiting for something to be drawn, clear out the old one so it
781 // still gets executed.
782 // TODO: Have a real system where we can wait on different windows to be drawn with
783 // different callbacks.
784 if (mService.mWaitingForDrawnCallback != null) {
785 mService.mWaitingForDrawnCallback.run();
786 }
Jorim Jaggiff71d202016-04-14 13:12:36 -0700787 mService.mWaitingForDrawnCallback = () -> {
788 mAnimationStartDelayed = false;
789 if (mDelayedImeWin != null) {
790 mDelayedImeWin.mWinAnimator.endDelayingAnimationStart();
791 }
Chong Zhang22eff0a2016-07-01 14:48:11 -0700792 // If the adjust status changed since this was posted, only notify
793 // the new states and don't animate.
794 long duration = 0;
795 if (mAdjustedForIme == adjustedForIme
796 && mAdjustedForDivider == adjustedForDivider) {
797 duration = IME_ADJUST_ANIM_DURATION;
798 } else {
799 Slog.w(TAG, "IME adjust changed while waiting for drawn:"
800 + " adjustedForIme=" + adjustedForIme
801 + " adjustedForDivider=" + adjustedForDivider
802 + " mAdjustedForIme=" + mAdjustedForIme
803 + " mAdjustedForDivider=" + mAdjustedForDivider);
804 }
Chong Zhangf347ab52016-04-18 21:02:01 -0700805 notifyAdjustedForImeChanged(
Chong Zhang22eff0a2016-07-01 14:48:11 -0700806 mAdjustedForIme || mAdjustedForDivider, duration);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700807 };
808 } else {
Chong Zhangf347ab52016-04-18 21:02:01 -0700809 notifyAdjustedForImeChanged(
810 adjustedForIme || adjustedForDivider, IME_ADJUST_ANIM_DURATION);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700811 }
Jorim Jaggieb88d832016-04-13 20:17:43 -0700812 }
813
Chong Zhang22eff0a2016-07-01 14:48:11 -0700814 private boolean setMinimizedDockedStack(boolean minimized) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700815 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Matthew Nge15352e2016-12-20 15:36:29 -0800816 notifyDockedStackMinimizedChanged(minimized, false /* animate */, isHomeStackResizable());
Chong Zhang22eff0a2016-07-01 14:48:11 -0700817 return stack != null && stack.setAdjustedForMinimizedDock(minimized ? 1f : 0f);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800818 }
819
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800820 private boolean isAnimationMaximizing() {
821 return mAnimationTarget == 0f;
822 }
823
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800824 public boolean animate(long now) {
Wale Ogunwale20ec11b2016-04-22 12:11:51 -0700825 if (mWindow == null) {
826 return false;
827 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700828 if (mAnimatingForMinimizedDockedStack) {
829 return animateForMinimizedDockedStack(now);
830 } else if (mAnimatingForIme) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700831 return animateForIme(now);
Chong Zhangbaba7832016-03-24 10:21:26 -0700832 } else {
Wale Ogunwale20ec11b2016-04-22 12:11:51 -0700833 if (mDimLayer != null && mDimLayer.isDimming()) {
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700834 mDimLayer.setLayer(getResizeDimLayer());
Chong Zhang1402c2e2016-04-21 15:17:47 -0700835 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800836 return false;
837 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700838 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800839
Jorim Jaggieb88d832016-04-13 20:17:43 -0700840 private boolean animateForIme(long now) {
Jorim Jaggiff71d202016-04-14 13:12:36 -0700841 if (!mAnimationStarted || mAnimationStartDelayed) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700842 mAnimationStarted = true;
843 mAnimationStartTime = now;
844 mAnimationDuration = (long)
Jorim Jaggi698e7632016-04-13 21:02:22 -0700845 (IME_ADJUST_ANIM_DURATION * mService.getWindowAnimationScaleLocked());
Jorim Jaggieb88d832016-04-13 20:17:43 -0700846 }
847 float t = Math.min(1f, (float) (now - mAnimationStartTime) / mAnimationDuration);
848 t = (mAnimationTarget == 1f ? IME_ADJUST_ENTRY_INTERPOLATOR : TOUCH_RESPONSE_INTERPOLATOR)
849 .getInterpolation(t);
Wale Ogunwale10124582016-09-15 20:25:50 -0700850 final boolean updated =
851 mDisplayContent.animateForIme(t, mAnimationTarget, mDividerAnimationTarget);
Jorim Jaggieb88d832016-04-13 20:17:43 -0700852 if (updated) {
853 mService.mWindowPlacerLocked.performSurfacePlacement();
854 }
855 if (t >= 1.0f) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700856 mLastAnimationProgress = mAnimationTarget;
857 mLastDividerProgress = mDividerAnimationTarget;
Jorim Jaggieb88d832016-04-13 20:17:43 -0700858 mAnimatingForIme = false;
859 return false;
860 } else {
861 return true;
862 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700863 }
864
865 private boolean animateForMinimizedDockedStack(long now) {
Matthew Ng64e77cf2017-10-31 14:01:31 -0700866 final TaskStack stack = mDisplayContent.getSplitScreenPrimaryStackIgnoringVisibility();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800867 if (!mAnimationStarted) {
868 mAnimationStarted = true;
869 mAnimationStartTime = now;
Matthew Nge15352e2016-12-20 15:36:29 -0800870 notifyDockedStackMinimizedChanged(mMinimizedDock, true /* animate */,
871 isHomeStackResizable() /* isHomeStackResizable */);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800872 }
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800873 float t = Math.min(1f, (float) (now - mAnimationStartTime) / mAnimationDuration);
874 t = (isAnimationMaximizing() ? TOUCH_RESPONSE_INTERPOLATOR : mMinimizedDockInterpolator)
875 .getInterpolation(t);
Chong Zhang741c0ba2016-05-27 12:52:11 -0700876 if (stack != null) {
877 if (stack.setAdjustedForMinimizedDock(getMinimizeAmount(stack, t))) {
878 mService.mWindowPlacerLocked.performSurfacePlacement();
879 }
880 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800881 if (t >= 1.0f) {
Chong Zhangbaba7832016-03-24 10:21:26 -0700882 mAnimatingForMinimizedDockedStack = false;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800883 return false;
884 } else {
885 return true;
886 }
887 }
888
Wale Ogunwale10124582016-09-15 20:25:50 -0700889 float getInterpolatedAnimationValue(float t) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700890 return t * mAnimationTarget + (1 - t) * mAnimationStart;
891 }
892
Wale Ogunwale10124582016-09-15 20:25:50 -0700893 float getInterpolatedDividerValue(float t) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700894 return t * mDividerAnimationTarget + (1 - t) * mDividerAnimationStart;
895 }
896
Jorim Jaggif97ed922016-02-18 18:57:07 -0800897 /**
898 * Gets the amount how much to minimize a stack depending on the interpolated fraction t.
899 */
900 private float getMinimizeAmount(TaskStack stack, float t) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700901 final float naturalAmount = getInterpolatedAnimationValue(t);
Jorim Jaggif97ed922016-02-18 18:57:07 -0800902 if (isAnimationMaximizing()) {
903 return adjustMaximizeAmount(stack, t, naturalAmount);
904 } else {
905 return naturalAmount;
906 }
907 }
908
909 /**
910 * When maximizing the stack during a clip reveal transition, this adjusts the minimize amount
911 * during the transition such that the edge of the clip reveal rect is met earlier in the
912 * transition so we don't create a visible "hole", but only if both the clip reveal and the
913 * docked stack divider start from about the same portion on the screen.
914 */
915 private float adjustMaximizeAmount(TaskStack stack, float t, float naturalAmount) {
916 if (mMaximizeMeetFraction == 1f) {
917 return naturalAmount;
918 }
919 final int minimizeDistance = stack.getMinimizeDistance();
920 float startPrime = mService.mAppTransition.getLastClipRevealMaxTranslation()
921 / (float) minimizeDistance;
922 final float amountPrime = t * mAnimationTarget + (1 - t) * startPrime;
923 final float t2 = Math.min(t / mMaximizeMeetFraction, 1);
924 return amountPrime * t2 + naturalAmount * (1 - t2);
925 }
926
927 /**
928 * Retrieves the animation fraction at which the docked stack has to meet the clip reveal
929 * edge. See {@link #adjustMaximizeAmount}.
930 */
931 private float getClipRevealMeetFraction(TaskStack stack) {
932 if (!isAnimationMaximizing() || stack == null ||
933 !mService.mAppTransition.hadClipRevealAnimation()) {
934 return 1f;
935 }
936 final int minimizeDistance = stack.getMinimizeDistance();
937 final float fraction = Math.abs(mService.mAppTransition.getLastClipRevealMaxTranslation())
938 / (float) minimizeDistance;
939 final float t = Math.max(0, Math.min(1, (fraction - CLIP_REVEAL_MEET_FRACTION_MIN)
940 / (CLIP_REVEAL_MEET_FRACTION_MAX - CLIP_REVEAL_MEET_FRACTION_MIN)));
941 return CLIP_REVEAL_MEET_EARLIEST
942 + (1 - t) * (CLIP_REVEAL_MEET_LAST - CLIP_REVEAL_MEET_EARLIEST);
943 }
944
Jorim Jaggi50981592015-12-29 17:54:12 +0100945 @Override
Wale Ogunwale29bfbb82016-05-12 15:13:52 -0700946 public boolean dimFullscreen() {
Jorim Jaggi50981592015-12-29 17:54:12 +0100947 return false;
948 }
949
950 @Override
951 public DisplayInfo getDisplayInfo() {
952 return mDisplayContent.getDisplayInfo();
953 }
954
955 @Override
Wale Ogunwalef0a60a92017-01-19 09:44:40 -0800956 public boolean isAttachedToDisplay() {
957 return mDisplayContent != null;
958 }
959
960 @Override
Jorim Jaggi50981592015-12-29 17:54:12 +0100961 public void getDimBounds(Rect outBounds) {
962 // This dim layer user doesn't need this.
963 }
964
965 @Override
966 public String toShortString() {
967 return TAG;
968 }
Robert Carre63e01a2016-04-18 20:27:34 -0700969
970 WindowState getWindow() {
971 return mWindow;
972 }
Jorim Jaggi31f71702016-05-04 16:43:04 -0700973
974 void dump(String prefix, PrintWriter pw) {
975 pw.println(prefix + "DockedStackDividerController");
976 pw.println(prefix + " mLastVisibility=" + mLastVisibility);
977 pw.println(prefix + " mMinimizedDock=" + mMinimizedDock);
978 pw.println(prefix + " mAdjustedForIme=" + mAdjustedForIme);
979 pw.println(prefix + " mAdjustedForDivider=" + mAdjustedForDivider);
980 if (mDimLayer.isDimming()) {
981 pw.println(prefix + " Dim layer is dimming: ");
982 mDimLayer.printTo(prefix + " ", pw);
983 }
984 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700985
986 void writeToProto(ProtoOutputStream proto, long fieldId) {
987 final long token = proto.start(fieldId);
988 proto.write(MINIMIZED_DOCK, mMinimizedDock);
989 proto.end(token);
990 }
Robert Carre63e01a2016-04-18 20:27:34 -0700991}