blob: 8174c139cf640ea77b5665cc8337237d8293e7e6 [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
Jorim Jaggibc5425c2016-03-01 13:51:16 +010019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
21import static android.view.WindowManager.DOCKED_BOTTOM;
22import static android.view.WindowManager.DOCKED_LEFT;
23import static android.view.WindowManager.DOCKED_RIGHT;
24import static android.view.WindowManager.DOCKED_TOP;
25import static com.android.server.wm.AppTransition.DEFAULT_APP_TRANSITION_DURATION;
26import static com.android.server.wm.AppTransition.TOUCH_RESPONSE_INTERPOLATOR;
Jorim Jaggiff71d202016-04-14 13:12:36 -070027import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_DOCKED_DIVIDER;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010028import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
29import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Tony Mak853304c2016-04-18 15:17:41 +010030import static com.android.server.wm.WindowManagerService.H.NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010031
Filip Gruszczynski466f3212015-09-21 17:57:57 -070032import android.content.Context;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070033import android.graphics.Rect;
Jorim Jaggia6c934e2015-12-21 13:22:31 +010034import android.os.RemoteCallbackList;
Filip Gruszczynski64cdc142015-11-29 21:10:07 -080035import android.os.RemoteException;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080036import android.util.ArraySet;
Filip Gruszczynski77049052015-11-09 14:01:21 -080037import android.util.Slog;
Jorim Jaggi50981592015-12-29 17:54:12 +010038import android.view.DisplayInfo;
Jorim Jaggia6c934e2015-12-21 13:22:31 +010039import android.view.IDockedStackListener;
Jorim Jaggi50981592015-12-29 17:54:12 +010040import android.view.SurfaceControl;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080041import android.view.animation.AnimationUtils;
42import android.view.animation.Interpolator;
Jorim Jaggieb88d832016-04-13 20:17:43 -070043import android.view.animation.PathInterpolator;
Jorim Jaggi50981592015-12-29 17:54:12 +010044
45import com.android.server.wm.DimLayer.DimLayerUser;
Jorim Jaggiff71d202016-04-14 13:12:36 -070046import com.android.server.wm.WindowManagerService.H;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010047
Chong Zhangbaba7832016-03-24 10:21:26 -070048import java.util.ArrayList;
49
Filip Gruszczynski466f3212015-09-21 17:57:57 -070050/**
Jorim Jaggi61f39a72015-10-29 16:54:18 +010051 * Keeps information about the docked stack divider.
Filip Gruszczynski466f3212015-09-21 17:57:57 -070052 */
Jorim Jaggi50981592015-12-29 17:54:12 +010053public class DockedStackDividerController implements DimLayerUser {
Jorim Jaggi61f39a72015-10-29 16:54:18 +010054
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080055 private static final String TAG = TAG_WITH_CLASS_NAME ? "DockedStackDividerController" : TAG_WM;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010056
Jorim Jaggif97ed922016-02-18 18:57:07 -080057 /**
58 * The fraction during the maximize/clip reveal animation the divider meets the edge of the clip
59 * revealing surface at the earliest.
60 */
61 private static final float CLIP_REVEAL_MEET_EARLIEST = 0.6f;
62
63 /**
64 * The fraction during the maximize/clip reveal animation the divider meets the edge of the clip
65 * revealing surface at the latest.
66 */
67 private static final float CLIP_REVEAL_MEET_LAST = 1f;
68
69 /**
70 * If the app translates at least CLIP_REVEAL_MEET_FRACTION_MIN * minimize distance, we start
71 * meet somewhere between {@link #CLIP_REVEAL_MEET_LAST} and {@link #CLIP_REVEAL_MEET_EARLIEST}.
72 */
73 private static final float CLIP_REVEAL_MEET_FRACTION_MIN = 0.4f;
74
75 /**
76 * If the app translates equals or more than CLIP_REVEAL_MEET_FRACTION_MIN * minimize distance,
77 * we meet at {@link #CLIP_REVEAL_MEET_EARLIEST}.
78 */
79 private static final float CLIP_REVEAL_MEET_FRACTION_MAX = 0.8f;
80
Jorim Jaggieb88d832016-04-13 20:17:43 -070081 private static final Interpolator IME_ADJUST_ENTRY_INTERPOLATOR =
Jorim Jaggiff71d202016-04-14 13:12:36 -070082 new PathInterpolator(0.2f, 0f, 0.1f, 1f);
Jorim Jaggieb88d832016-04-13 20:17:43 -070083
Jorim Jaggi698e7632016-04-13 21:02:22 -070084 private static final long IME_ADJUST_ANIM_DURATION = 280;
Jorim Jaggieb88d832016-04-13 20:17:43 -070085
Jorim Jaggiff71d202016-04-14 13:12:36 -070086 private static final long IME_ADJUST_DRAWN_TIMEOUT = 200;
87
Chong Zhang198afac2016-04-15 12:03:11 -070088 private static final int DIVIDER_WIDTH_INACTIVE_DP = 4;
89
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080090 private final WindowManagerService mService;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070091 private final DisplayContent mDisplayContent;
Jorim Jaggi11c62e12016-04-05 20:41:21 -070092 private int mDividerWindowWidth;
Chong Zhang198afac2016-04-15 12:03:11 -070093 private int mDividerWindowWidthInactive;
Jorim Jaggi11c62e12016-04-05 20:41:21 -070094 private int mDividerInsets;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010095 private boolean mResizing;
96 private WindowState mWindow;
97 private final Rect mTmpRect = new Rect();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080098 private final Rect mTmpRect2 = new Rect();
Filip Gruszczynski77049052015-11-09 14:01:21 -080099 private final Rect mLastRect = new Rect();
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800100 private boolean mLastVisibility = false;
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100101 private final RemoteCallbackList<IDockedStackListener> mDockedStackListeners
102 = new RemoteCallbackList<>();
Jorim Jaggi50981592015-12-29 17:54:12 +0100103 private final DimLayer mDimLayer;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700104
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800105 private boolean mMinimizedDock;
Chong Zhangbaba7832016-03-24 10:21:26 -0700106 private boolean mAnimatingForMinimizedDockedStack;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800107 private boolean mAnimationStarted;
108 private long mAnimationStartTime;
109 private float mAnimationStart;
110 private float mAnimationTarget;
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800111 private long mAnimationDuration;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700112 private boolean mAnimationStartDelayed;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800113 private final Interpolator mMinimizedDockInterpolator;
Jorim Jaggif97ed922016-02-18 18:57:07 -0800114 private float mMaximizeMeetFraction;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100115 private final Rect mTouchRegion = new Rect();
Chong Zhangbaba7832016-03-24 10:21:26 -0700116 private boolean mAnimatingForIme;
117 private boolean mAdjustedForIme;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700118 private WindowState mDelayedImeWin;
Chong Zhangf347ab52016-04-18 21:02:01 -0700119 private boolean mAdjustedForDivider;
120 private float mDividerAnimationStart;
121 private float mDividerAnimationTarget;
122 private float mLastAnimationProgress;
123 private float mLastDividerProgress;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800124
125 DockedStackDividerController(WindowManagerService service, DisplayContent displayContent) {
126 mService = service;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700127 mDisplayContent = displayContent;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800128 final Context context = service.mContext;
Jorim Jaggibc5425c2016-03-01 13:51:16 +0100129 mDimLayer = new DimLayer(displayContent.mService, this, displayContent.getDisplayId(),
130 "DockedStackDim");
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800131 mMinimizedDockInterpolator = AnimationUtils.loadInterpolator(
132 context, android.R.interpolator.fast_out_slow_in);
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700133 loadDimens();
134 }
135
136 private void loadDimens() {
137 final Context context = mService.mContext;
138 mDividerWindowWidth = context.getResources().getDimensionPixelSize(
139 com.android.internal.R.dimen.docked_stack_divider_thickness);
140 mDividerInsets = context.getResources().getDimensionPixelSize(
141 com.android.internal.R.dimen.docked_stack_divider_insets);
Chong Zhang198afac2016-04-15 12:03:11 -0700142 mDividerWindowWidthInactive = WindowManagerService.dipToPixel(
143 DIVIDER_WIDTH_INACTIVE_DP, mDisplayContent.getDisplayMetrics());
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700144 }
145
146 void onConfigurationChanged() {
147 loadDimens();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700148 }
149
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100150 boolean isResizing() {
151 return mResizing;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700152 }
153
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100154 int getContentWidth() {
155 return mDividerWindowWidth - 2 * mDividerInsets;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700156 }
157
Jorim Jaggi81ba11e2016-02-03 22:04:22 -0800158 int getContentInsets() {
159 return mDividerInsets;
160 }
161
Chong Zhang198afac2016-04-15 12:03:11 -0700162 int getContentWidthInactive() {
163 return mDividerWindowWidthInactive;
164 }
165
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100166 void setResizing(boolean resizing) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800167 if (mResizing != resizing) {
168 mResizing = resizing;
169 resetDragResizingChangeReported();
170 }
171 }
172
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100173 void setTouchRegion(Rect touchRegion) {
174 mTouchRegion.set(touchRegion);
175 }
176
177 void getTouchRegion(Rect outRegion) {
178 outRegion.set(mTouchRegion);
179 outRegion.offset(mWindow.getFrameLw().left, mWindow.getFrameLw().top);
180 }
181
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800182 private void resetDragResizingChangeReported() {
183 final WindowList windowList = mDisplayContent.getWindowList();
184 for (int i = windowList.size() - 1; i >= 0; i--) {
185 windowList.get(i).resetDragResizingChangeReported();
186 }
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100187 }
188
189 void setWindow(WindowState window) {
190 mWindow = window;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800191 reevaluateVisibility(false);
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100192 }
193
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800194 void reevaluateVisibility(boolean force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800195 if (mWindow == null) {
196 return;
197 }
Jorim Jaggie48f4282015-11-06 17:32:44 +0100198 TaskStack stack = mDisplayContent.mService.mStackIdToStack.get(DOCKED_STACK_ID);
Jorim Jaggi7998e482016-02-12 18:47:06 -0800199
200 // If the stack is invisible, we policy force hide it in WindowAnimator.shouldForceHide
201 final boolean visible = stack != null;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800202 if (mLastVisibility == visible && !force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800203 return;
204 }
205 mLastVisibility = visible;
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100206 notifyDockedDividerVisibilityChanged(visible);
Jorim Jaggi50981592015-12-29 17:54:12 +0100207 if (!visible) {
208 setResizeDimLayer(false, INVALID_STACK_ID, 0f);
209 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100210 }
211
212 boolean wasVisible() {
213 return mLastVisibility;
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100214 }
215
Chong Zhangf347ab52016-04-18 21:02:01 -0700216 void setAdjustedForIme(
217 boolean adjustedForIme, boolean adjustedForDivider,
218 boolean animate, WindowState imeWin) {
219 if (mAdjustedForIme != adjustedForIme || mAdjustedForDivider != adjustedForDivider) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700220 if (animate) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700221 startImeAdjustAnimation(adjustedForIme, adjustedForDivider, imeWin);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700222 } else {
Jorim Jaggiff71d202016-04-14 13:12:36 -0700223 // Animation might be delayed, so only notify if we don't run an animation.
Chong Zhangf347ab52016-04-18 21:02:01 -0700224 notifyAdjustedForImeChanged(adjustedForIme || adjustedForDivider, 0 /* duration */);
Jorim Jaggieb88d832016-04-13 20:17:43 -0700225 }
Chong Zhangf347ab52016-04-18 21:02:01 -0700226 mAdjustedForIme = adjustedForIme;
227 mAdjustedForDivider = adjustedForDivider;
Chong Zhangbaba7832016-03-24 10:21:26 -0700228 }
Chong Zhangb58bbcc2016-03-23 11:57:36 -0700229 }
230
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700231 void positionDockedStackedDivider(Rect frame) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700232 TaskStack stack = mDisplayContent.getDockedStackLocked();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700233 if (stack == null) {
234 // Unfortunately we might end up with still having a divider, even though the underlying
235 // stack was already removed. This is because we are on AM thread and the removal of the
Filip Gruszczynski77049052015-11-09 14:01:21 -0800236 // divider was deferred to WM thread and hasn't happened yet. In that case let's just
237 // keep putting it in the same place it was before the stack was removed to have
238 // continuity and prevent it from jumping to the center. It will get hidden soon.
239 frame.set(mLastRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700240 return;
Filip Gruszczynski77049052015-11-09 14:01:21 -0800241 } else {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800242 stack.getDimBounds(mTmpRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700243 }
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100244 int side = stack.getDockSide();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700245 switch (side) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700246 case DOCKED_LEFT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100247 frame.set(mTmpRect.right - mDividerInsets, frame.top,
248 mTmpRect.right + frame.width() - mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700249 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700250 case DOCKED_TOP:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100251 frame.set(frame.left, mTmpRect.bottom - mDividerInsets,
252 mTmpRect.right, mTmpRect.bottom + frame.height() - mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700253 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700254 case DOCKED_RIGHT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100255 frame.set(mTmpRect.left - frame.width() + mDividerInsets, frame.top,
256 mTmpRect.left + mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700257 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700258 case DOCKED_BOTTOM:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100259 frame.set(frame.left, mTmpRect.top - frame.height() + mDividerInsets,
260 frame.right, mTmpRect.top + mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700261 break;
262 }
Filip Gruszczynski77049052015-11-09 14:01:21 -0800263 mLastRect.set(frame);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700264 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800265
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100266 void notifyDockedDividerVisibilityChanged(boolean visible) {
267 final int size = mDockedStackListeners.beginBroadcast();
268 for (int i = 0; i < size; ++i) {
269 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
270 try {
271 listener.onDividerVisibilityChanged(visible);
272 } catch (RemoteException e) {
273 Slog.e(TAG_WM, "Error delivering divider visibility changed event.", e);
274 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800275 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100276 mDockedStackListeners.finishBroadcast();
277 }
278
279 void notifyDockedStackExistsChanged(boolean exists) {
280 final int size = mDockedStackListeners.beginBroadcast();
281 for (int i = 0; i < size; ++i) {
282 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
283 try {
284 listener.onDockedStackExistsChanged(exists);
285 } catch (RemoteException e) {
286 Slog.e(TAG_WM, "Error delivering docked stack exists changed event.", e);
287 }
288 }
289 mDockedStackListeners.finishBroadcast();
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700290 if (!exists) {
291 setMinimizedDockedStack(false);
292 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100293 }
294
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800295 void notifyDockedStackMinimizedChanged(boolean minimizedDock, long animDuration) {
Tony Mak853304c2016-04-18 15:17:41 +0100296 mService.mH.removeMessages(NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED);
297 mService.mH.obtainMessage(NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED,
298 minimizedDock ? 1 : 0, 0).sendToTarget();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800299 final int size = mDockedStackListeners.beginBroadcast();
300 for (int i = 0; i < size; ++i) {
301 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
302 try {
303 listener.onDockedStackMinimizedChanged(minimizedDock, animDuration);
304 } catch (RemoteException e) {
305 Slog.e(TAG_WM, "Error delivering minimized dock changed event.", e);
306 }
307 }
308 mDockedStackListeners.finishBroadcast();
309 }
310
Jorim Jaggi2917dc42016-04-11 11:39:13 -0700311 void notifyDockSideChanged(int newDockSide) {
312 final int size = mDockedStackListeners.beginBroadcast();
313 for (int i = 0; i < size; ++i) {
314 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
315 try {
316 listener.onDockSideChanged(newDockSide);
317 } catch (RemoteException e) {
318 Slog.e(TAG_WM, "Error delivering dock side changed event.", e);
319 }
320 }
321 mDockedStackListeners.finishBroadcast();
322 }
323
Jorim Jaggi698e7632016-04-13 21:02:22 -0700324 void notifyAdjustedForImeChanged(boolean adjustedForIme, long animDuration) {
325 final int size = mDockedStackListeners.beginBroadcast();
326 for (int i = 0; i < size; ++i) {
327 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
328 try {
329 listener.onAdjustedForImeChanged(adjustedForIme, animDuration);
330 } catch (RemoteException e) {
331 Slog.e(TAG_WM, "Error delivering adjusted for ime changed event.", e);
332 }
333 }
334 mDockedStackListeners.finishBroadcast();
335 }
336
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100337 void registerDockedStackListener(IDockedStackListener listener) {
338 mDockedStackListeners.register(listener);
339 notifyDockedDividerVisibilityChanged(wasVisible());
340 notifyDockedStackExistsChanged(
341 mDisplayContent.mService.mStackIdToStack.get(DOCKED_STACK_ID) != null);
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700342 notifyDockedStackMinimizedChanged(mMinimizedDock, 0 /* animDuration */);
Jorim Jaggi698e7632016-04-13 21:02:22 -0700343 notifyAdjustedForImeChanged(mAdjustedForIme, 0 /* animDuration */);
344
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800345 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100346
347 void setResizeDimLayer(boolean visible, int targetStackId, float alpha) {
348 SurfaceControl.openTransaction();
Jorim Jaggibc5425c2016-03-01 13:51:16 +0100349 final TaskStack stack = mDisplayContent.mService.mStackIdToStack.get(targetStackId);
350 final TaskStack dockedStack = mDisplayContent.getDockedStackLocked();
351 boolean visibleAndValid = visible && stack != null && dockedStack != null;
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100352 if (visibleAndValid) {
Jorim Jaggi50981592015-12-29 17:54:12 +0100353 stack.getDimBounds(mTmpRect);
Jorim Jaggi10b89dc2016-01-05 15:40:17 +0100354 if (mTmpRect.height() > 0 && mTmpRect.width() > 0) {
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100355 mDimLayer.setBounds(mTmpRect);
Chong Zhang1402c2e2016-04-21 15:17:47 -0700356 mDimLayer.show(mService.mLayersController.getResizeDimLayer(),
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100357 alpha, 0 /* duration */);
358 } else {
359 visibleAndValid = false;
360 }
361 }
362 if (!visibleAndValid) {
Jorim Jaggi50981592015-12-29 17:54:12 +0100363 mDimLayer.hide();
364 }
365 SurfaceControl.closeTransaction();
366 }
367
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800368 /**
369 * Notifies the docked stack divider controller of a visibility change that happens without
370 * an animation.
371 */
372 void notifyAppVisibilityChanged(AppWindowToken wtoken, boolean visible) {
373 final Task task = wtoken.mTask;
374 if (!task.isHomeTask() || !task.isVisibleForUser()) {
375 return;
376 }
377
Tony Mak853304c2016-04-18 15:17:41 +0100378 // If the app that having visibility change is not the top visible one in the task,
379 // it does not affect whether the docked stack is minimized, ignore it.
380 if (task.getTopVisibleAppToken() == null || task.getTopVisibleAppToken() != wtoken) {
381 return;
382 }
383
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800384 // If the stack is completely offscreen, this might just be an intermediate state when
385 // docking a task/launching recents at the same time, but home doesn't actually get
386 // visible after the state settles in.
387 if (isWithinDisplay(task)
388 && mDisplayContent.getDockedStackVisibleForUserLocked() != null) {
389 setMinimizedDockedStack(visible, false /* animate */);
390 }
391 }
392
393 void notifyAppTransitionStarting(ArraySet<AppWindowToken> openingApps,
394 ArraySet<AppWindowToken> closingApps) {
395 if (containsHomeTaskWithinDisplay(openingApps)) {
396 setMinimizedDockedStack(true /* minimized */, true /* animate */);
397 } else if (containsHomeTaskWithinDisplay(closingApps)) {
398 setMinimizedDockedStack(false /* minimized */, true /* animate */);
399 }
400 }
401
402 private boolean containsHomeTaskWithinDisplay(ArraySet<AppWindowToken> apps) {
403 for (int i = apps.size() - 1; i >= 0; i--) {
404 final Task task = apps.valueAt(i).mTask;
405 if (task != null && task.isHomeTask()) {
406 return isWithinDisplay(task);
407 }
408 }
409 return false;
410 }
411
412 private boolean isWithinDisplay(Task task) {
413 task.mStack.getBounds(mTmpRect);
414 mDisplayContent.getLogicalDisplayRect(mTmpRect2);
415 return mTmpRect.intersect(mTmpRect2);
416 }
417
418 /**
419 * Sets whether the docked stack is currently in a minimized state, i.e. all the tasks in the
420 * docked stack are heavily clipped so you can only see a minimal peek state.
421 *
422 * @param minimizedDock Whether the docked stack is currently minimized.
423 * @param animate Whether to animate the change.
424 */
425 private void setMinimizedDockedStack(boolean minimizedDock, boolean animate) {
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700426 final boolean wasMinimized = mMinimizedDock;
427 mMinimizedDock = minimizedDock;
428 if (minimizedDock == wasMinimized
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800429 || mDisplayContent.getDockedStackVisibleForUserLocked() == null) {
430 return;
431 }
432
Jorim Jaggieb88d832016-04-13 20:17:43 -0700433 clearImeAdjustAnimation();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800434 if (minimizedDock) {
435 if (animate) {
436 startAdjustAnimation(0f, 1f);
437 } else {
438 setMinimizedDockedStack(true);
439 }
440 } else {
441 if (animate) {
442 startAdjustAnimation(1f, 0f);
443 } else {
444 setMinimizedDockedStack(false);
445 }
446 }
447 }
448
Jorim Jaggieb88d832016-04-13 20:17:43 -0700449 private void clearImeAdjustAnimation() {
450 final ArrayList<TaskStack> stacks = mDisplayContent.getStacks();
451 for (int i = stacks.size() - 1; i >= 0; --i) {
452 final TaskStack stack = stacks.get(i);
453 if (stack != null && stack.isAdjustedForIme()) {
454 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
455 }
456 }
457 mAnimatingForIme = false;
458 }
459
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800460 private void startAdjustAnimation(float from, float to) {
Chong Zhangbaba7832016-03-24 10:21:26 -0700461 mAnimatingForMinimizedDockedStack = true;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800462 mAnimationStarted = false;
463 mAnimationStart = from;
464 mAnimationTarget = to;
465 }
466
Chong Zhangf347ab52016-04-18 21:02:01 -0700467 private void startImeAdjustAnimation(
468 boolean adjustedForIme, boolean adjustedForDivider, WindowState imeWin) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700469 mAnimatingForIme = true;
470 mAnimationStarted = false;
Chong Zhangf347ab52016-04-18 21:02:01 -0700471
472 // If we're not in an animation, the starting point depends on whether we're adjusted
473 // or not. If we're already in an animation, we start from where the current animation
474 // left off, so that the motion doesn't look discontinuous.
475 if (!mAnimatingForIme) {
476 mAnimationStart = mAdjustedForIme ? 1 : 0;
477 mDividerAnimationStart = mAdjustedForDivider ? 1 : 0;
478 mLastAnimationProgress = mAnimationStart;
479 mLastDividerProgress = mDividerAnimationStart;
480 } else {
481 mAnimationStart = mLastAnimationProgress;
482 mDividerAnimationStart = mLastDividerProgress;
483 }
484 mAnimationTarget = adjustedForIme ? 1 : 0;
485 mDividerAnimationTarget = adjustedForDivider ? 1 : 0;
Jorim Jaggiff71d202016-04-14 13:12:36 -0700486
487 final ArrayList<TaskStack> stacks = mDisplayContent.getStacks();
488 for (int i = stacks.size() - 1; i >= 0; --i) {
489 final TaskStack stack = stacks.get(i);
490 if (stack.isVisibleLocked() && stack.isAdjustedForIme()) {
491 stack.beginImeAdjustAnimation();
492 }
493 }
494
495 // We put all tasks into drag resizing mode - wait until all of them have completed the
496 // drag resizing switch.
497 if (!mService.mWaitingForDrawn.isEmpty()) {
498 mService.mH.removeMessages(H.WAITING_FOR_DRAWN_TIMEOUT);
499 mService.mH.sendEmptyMessageDelayed(H.WAITING_FOR_DRAWN_TIMEOUT,
500 IME_ADJUST_DRAWN_TIMEOUT);
501 mAnimationStartDelayed = true;
502 if (imeWin != null) {
503
504 // There might be an old window delaying the animation start - clear it.
505 if (mDelayedImeWin != null) {
506 mDelayedImeWin.mWinAnimator.endDelayingAnimationStart();
507 }
508 mDelayedImeWin = imeWin;
509 imeWin.mWinAnimator.startDelayingAnimationStart();
510 }
511 mService.mWaitingForDrawnCallback = () -> {
512 mAnimationStartDelayed = false;
513 if (mDelayedImeWin != null) {
514 mDelayedImeWin.mWinAnimator.endDelayingAnimationStart();
515 }
Chong Zhangf347ab52016-04-18 21:02:01 -0700516 notifyAdjustedForImeChanged(
517 adjustedForIme || adjustedForDivider, IME_ADJUST_ANIM_DURATION);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700518 };
519 } else {
Chong Zhangf347ab52016-04-18 21:02:01 -0700520 notifyAdjustedForImeChanged(
521 adjustedForIme || adjustedForDivider, IME_ADJUST_ANIM_DURATION);
Jorim Jaggiff71d202016-04-14 13:12:36 -0700522 }
Jorim Jaggieb88d832016-04-13 20:17:43 -0700523 }
524
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800525 private void setMinimizedDockedStack(boolean minimized) {
526 final TaskStack stack = mDisplayContent.getDockedStackVisibleForUserLocked();
Jorim Jaggief92d6f2016-03-25 22:07:16 -0700527 notifyDockedStackMinimizedChanged(minimized, 0);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800528 if (stack == null) {
529 return;
530 }
531 if (stack.setAdjustedForMinimizedDock(minimized ? 1f : 0f)) {
532 mService.mWindowPlacerLocked.performSurfacePlacement();
533 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800534 }
535
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800536 private boolean isAnimationMaximizing() {
537 return mAnimationTarget == 0f;
538 }
539
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800540 public boolean animate(long now) {
Wale Ogunwale20ec11b2016-04-22 12:11:51 -0700541 if (mWindow == null) {
542 return false;
543 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700544 if (mAnimatingForMinimizedDockedStack) {
545 return animateForMinimizedDockedStack(now);
546 } else if (mAnimatingForIme) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700547 return animateForIme(now);
Chong Zhangbaba7832016-03-24 10:21:26 -0700548 } else {
Wale Ogunwale20ec11b2016-04-22 12:11:51 -0700549 if (mDimLayer != null && mDimLayer.isDimming()) {
Chong Zhang1402c2e2016-04-21 15:17:47 -0700550 mDimLayer.setLayer(mService.mLayersController.getResizeDimLayer());
551 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800552 return false;
553 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700554 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800555
Jorim Jaggieb88d832016-04-13 20:17:43 -0700556 private boolean animateForIme(long now) {
Jorim Jaggiff71d202016-04-14 13:12:36 -0700557 if (!mAnimationStarted || mAnimationStartDelayed) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700558 mAnimationStarted = true;
559 mAnimationStartTime = now;
560 mAnimationDuration = (long)
Jorim Jaggi698e7632016-04-13 21:02:22 -0700561 (IME_ADJUST_ANIM_DURATION * mService.getWindowAnimationScaleLocked());
Jorim Jaggieb88d832016-04-13 20:17:43 -0700562 }
563 float t = Math.min(1f, (float) (now - mAnimationStartTime) / mAnimationDuration);
564 t = (mAnimationTarget == 1f ? IME_ADJUST_ENTRY_INTERPOLATOR : TOUCH_RESPONSE_INTERPOLATOR)
565 .getInterpolation(t);
Chong Zhangbaba7832016-03-24 10:21:26 -0700566 final ArrayList<TaskStack> stacks = mDisplayContent.getStacks();
Jorim Jaggieb88d832016-04-13 20:17:43 -0700567 boolean updated = false;
Chong Zhangbaba7832016-03-24 10:21:26 -0700568 for (int i = stacks.size() - 1; i >= 0; --i) {
569 final TaskStack stack = stacks.get(i);
570 if (stack != null && stack.isAdjustedForIme()) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700571 if (t >= 1f && mAnimationTarget == 0f && mDividerAnimationTarget == 0f) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700572 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
573 updated = true;
574 } else {
Chong Zhangf347ab52016-04-18 21:02:01 -0700575 mLastAnimationProgress = getInterpolatedAnimationValue(t);
576 mLastDividerProgress = getInterpolatedDividerValue(t);
577 updated |= stack.updateAdjustForIme(
578 mLastAnimationProgress,
579 mLastDividerProgress,
Jorim Jaggiff71d202016-04-14 13:12:36 -0700580 false /* force */);
581 }
582 if (t >= 1f) {
583 stack.endImeAdjustAnimation();
Chong Zhangbaba7832016-03-24 10:21:26 -0700584 }
585 }
586 }
Jorim Jaggieb88d832016-04-13 20:17:43 -0700587 if (updated) {
588 mService.mWindowPlacerLocked.performSurfacePlacement();
589 }
590 if (t >= 1.0f) {
Chong Zhangf347ab52016-04-18 21:02:01 -0700591 mLastAnimationProgress = mAnimationTarget;
592 mLastDividerProgress = mDividerAnimationTarget;
Jorim Jaggieb88d832016-04-13 20:17:43 -0700593 mAnimatingForIme = false;
594 return false;
595 } else {
596 return true;
597 }
Chong Zhangbaba7832016-03-24 10:21:26 -0700598 }
599
600 private boolean animateForMinimizedDockedStack(long now) {
Jorim Jaggif97ed922016-02-18 18:57:07 -0800601 final TaskStack stack = mDisplayContent.getDockedStackVisibleForUserLocked();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800602 if (!mAnimationStarted) {
603 mAnimationStarted = true;
604 mAnimationStartTime = now;
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800605 final long transitionDuration = isAnimationMaximizing()
606 ? mService.mAppTransition.getLastClipRevealTransitionDuration()
607 : DEFAULT_APP_TRANSITION_DURATION;
608 mAnimationDuration = (long)
609 (transitionDuration * mService.getTransitionAnimationScaleLocked());
Jorim Jaggif97ed922016-02-18 18:57:07 -0800610 mMaximizeMeetFraction = getClipRevealMeetFraction(stack);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800611 notifyDockedStackMinimizedChanged(mMinimizedDock,
Jorim Jaggif97ed922016-02-18 18:57:07 -0800612 (long) (mAnimationDuration * mMaximizeMeetFraction));
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800613 }
Jorim Jaggi8fe7e0a2016-02-12 19:43:39 -0800614 float t = Math.min(1f, (float) (now - mAnimationStartTime) / mAnimationDuration);
615 t = (isAnimationMaximizing() ? TOUCH_RESPONSE_INTERPOLATOR : mMinimizedDockInterpolator)
616 .getInterpolation(t);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800617 if (stack != null) {
Jorim Jaggif97ed922016-02-18 18:57:07 -0800618 if (stack.setAdjustedForMinimizedDock(getMinimizeAmount(stack, t))) {
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800619 mService.mWindowPlacerLocked.performSurfacePlacement();
620 }
621 }
622 if (t >= 1.0f) {
Chong Zhangbaba7832016-03-24 10:21:26 -0700623 mAnimatingForMinimizedDockedStack = false;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800624 return false;
625 } else {
626 return true;
627 }
628 }
629
Jorim Jaggieb88d832016-04-13 20:17:43 -0700630 private float getInterpolatedAnimationValue(float t) {
631 return t * mAnimationTarget + (1 - t) * mAnimationStart;
632 }
633
Chong Zhangf347ab52016-04-18 21:02:01 -0700634 private float getInterpolatedDividerValue(float t) {
635 return t * mDividerAnimationTarget + (1 - t) * mDividerAnimationStart;
636 }
637
Jorim Jaggif97ed922016-02-18 18:57:07 -0800638 /**
639 * Gets the amount how much to minimize a stack depending on the interpolated fraction t.
640 */
641 private float getMinimizeAmount(TaskStack stack, float t) {
Jorim Jaggieb88d832016-04-13 20:17:43 -0700642 final float naturalAmount = getInterpolatedAnimationValue(t);
Jorim Jaggif97ed922016-02-18 18:57:07 -0800643 if (isAnimationMaximizing()) {
644 return adjustMaximizeAmount(stack, t, naturalAmount);
645 } else {
646 return naturalAmount;
647 }
648 }
649
650 /**
651 * When maximizing the stack during a clip reveal transition, this adjusts the minimize amount
652 * during the transition such that the edge of the clip reveal rect is met earlier in the
653 * transition so we don't create a visible "hole", but only if both the clip reveal and the
654 * docked stack divider start from about the same portion on the screen.
655 */
656 private float adjustMaximizeAmount(TaskStack stack, float t, float naturalAmount) {
657 if (mMaximizeMeetFraction == 1f) {
658 return naturalAmount;
659 }
660 final int minimizeDistance = stack.getMinimizeDistance();
661 float startPrime = mService.mAppTransition.getLastClipRevealMaxTranslation()
662 / (float) minimizeDistance;
663 final float amountPrime = t * mAnimationTarget + (1 - t) * startPrime;
664 final float t2 = Math.min(t / mMaximizeMeetFraction, 1);
665 return amountPrime * t2 + naturalAmount * (1 - t2);
666 }
667
668 /**
669 * Retrieves the animation fraction at which the docked stack has to meet the clip reveal
670 * edge. See {@link #adjustMaximizeAmount}.
671 */
672 private float getClipRevealMeetFraction(TaskStack stack) {
673 if (!isAnimationMaximizing() || stack == null ||
674 !mService.mAppTransition.hadClipRevealAnimation()) {
675 return 1f;
676 }
677 final int minimizeDistance = stack.getMinimizeDistance();
678 final float fraction = Math.abs(mService.mAppTransition.getLastClipRevealMaxTranslation())
679 / (float) minimizeDistance;
680 final float t = Math.max(0, Math.min(1, (fraction - CLIP_REVEAL_MEET_FRACTION_MIN)
681 / (CLIP_REVEAL_MEET_FRACTION_MAX - CLIP_REVEAL_MEET_FRACTION_MIN)));
682 return CLIP_REVEAL_MEET_EARLIEST
683 + (1 - t) * (CLIP_REVEAL_MEET_LAST - CLIP_REVEAL_MEET_EARLIEST);
684 }
685
Jorim Jaggi50981592015-12-29 17:54:12 +0100686 @Override
687 public boolean isFullscreen() {
688 return false;
689 }
690
691 @Override
692 public DisplayInfo getDisplayInfo() {
693 return mDisplayContent.getDisplayInfo();
694 }
695
696 @Override
697 public void getDimBounds(Rect outBounds) {
698 // This dim layer user doesn't need this.
699 }
700
701 @Override
702 public String toShortString() {
703 return TAG;
704 }
Robert Carre63e01a2016-04-18 20:27:34 -0700705
706 WindowState getWindow() {
707 return mWindow;
708 }
709}