blob: ec99d1fd0c9b63176167dadd75de8605f8258543 [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
Filip Gruszczynski466f3212015-09-21 17:57:57 -070019import android.content.Context;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070020import android.graphics.Rect;
Jorim Jaggia6c934e2015-12-21 13:22:31 +010021import android.os.RemoteCallbackList;
Filip Gruszczynski64cdc142015-11-29 21:10:07 -080022import android.os.RemoteException;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080023import android.util.ArraySet;
24import android.util.Log;
Filip Gruszczynski77049052015-11-09 14:01:21 -080025import android.util.Slog;
Jorim Jaggi50981592015-12-29 17:54:12 +010026import android.view.DisplayInfo;
Jorim Jaggia6c934e2015-12-21 13:22:31 +010027import android.view.IDockedStackListener;
Jorim Jaggi50981592015-12-29 17:54:12 +010028import android.view.SurfaceControl;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080029import android.view.animation.AnimationUtils;
30import android.view.animation.Interpolator;
Jorim Jaggi50981592015-12-29 17:54:12 +010031
32import com.android.server.wm.DimLayer.DimLayerUser;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010033
Jorim Jaggic662d8e2016-02-05 16:54:54 -080034import java.util.ArrayList;
35
Jorim Jaggie48f4282015-11-06 17:32:44 +010036import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Jorim Jaggi50981592015-12-29 17:54:12 +010037import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_LEFT;
40import static android.view.WindowManager.DOCKED_RIGHT;
41import static android.view.WindowManager.DOCKED_TOP;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080042import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
43import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070044
45/**
Jorim Jaggi61f39a72015-10-29 16:54:18 +010046 * Keeps information about the docked stack divider.
Filip Gruszczynski466f3212015-09-21 17:57:57 -070047 */
Jorim Jaggi50981592015-12-29 17:54:12 +010048public class DockedStackDividerController implements DimLayerUser {
Jorim Jaggi61f39a72015-10-29 16:54:18 +010049
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080050 private static final String TAG = TAG_WITH_CLASS_NAME ? "DockedStackDividerController" : TAG_WM;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010051
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080052 private static final long MINIMIZED_DOCK_ANIMATION_DURATION = 400;
53
54 private final WindowManagerService mService;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070055 private final DisplayContent mDisplayContent;
Jorim Jaggi1fcbab62015-11-04 16:39:50 +010056 private final int mDividerWindowWidth;
57 private final int mDividerInsets;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010058 private boolean mResizing;
59 private WindowState mWindow;
60 private final Rect mTmpRect = new Rect();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080061 private final Rect mTmpRect2 = new Rect();
Filip Gruszczynski77049052015-11-09 14:01:21 -080062 private final Rect mLastRect = new Rect();
Filip Gruszczynski64cdc142015-11-29 21:10:07 -080063 private boolean mLastVisibility = false;
Jorim Jaggia6c934e2015-12-21 13:22:31 +010064 private final RemoteCallbackList<IDockedStackListener> mDockedStackListeners
65 = new RemoteCallbackList<>();
Jorim Jaggi50981592015-12-29 17:54:12 +010066 private final DimLayer mDimLayer;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -070067
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080068 private boolean mMinimizedDock;
69 private boolean mAnimating;
70 private boolean mAnimationStarted;
71 private long mAnimationStartTime;
72 private float mAnimationStart;
73 private float mAnimationTarget;
74 private final Interpolator mMinimizedDockInterpolator;
75
76 DockedStackDividerController(WindowManagerService service, DisplayContent displayContent) {
77 mService = service;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070078 mDisplayContent = displayContent;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080079 final Context context = service.mContext;
Jorim Jaggi1fcbab62015-11-04 16:39:50 +010080 mDividerWindowWidth = context.getResources().getDimensionPixelSize(
Filip Gruszczynski466f3212015-09-21 17:57:57 -070081 com.android.internal.R.dimen.docked_stack_divider_thickness);
Jorim Jaggi1fcbab62015-11-04 16:39:50 +010082 mDividerInsets = context.getResources().getDimensionPixelSize(
83 com.android.internal.R.dimen.docked_stack_divider_insets);
Jorim Jaggi50981592015-12-29 17:54:12 +010084 mDimLayer = new DimLayer(displayContent.mService, this, displayContent.getDisplayId());
Jorim Jaggi42625d1b2016-02-11 20:11:07 -080085 mMinimizedDockInterpolator = AnimationUtils.loadInterpolator(
86 context, android.R.interpolator.fast_out_slow_in);
Filip Gruszczynski466f3212015-09-21 17:57:57 -070087 }
88
Jorim Jaggi61f39a72015-10-29 16:54:18 +010089 boolean isResizing() {
90 return mResizing;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070091 }
92
Jorim Jaggi1fcbab62015-11-04 16:39:50 +010093 int getContentWidth() {
94 return mDividerWindowWidth - 2 * mDividerInsets;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070095 }
96
Jorim Jaggi81ba11e2016-02-03 22:04:22 -080097 int getContentInsets() {
98 return mDividerInsets;
99 }
100
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100101 void setResizing(boolean resizing) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -0800102 if (mResizing != resizing) {
103 mResizing = resizing;
104 resetDragResizingChangeReported();
105 }
106 }
107
108 private void resetDragResizingChangeReported() {
109 final WindowList windowList = mDisplayContent.getWindowList();
110 for (int i = windowList.size() - 1; i >= 0; i--) {
111 windowList.get(i).resetDragResizingChangeReported();
112 }
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100113 }
114
115 void setWindow(WindowState window) {
116 mWindow = window;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800117 reevaluateVisibility(false);
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100118 }
119
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800120 void reevaluateVisibility(boolean force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800121 if (mWindow == null) {
122 return;
123 }
Jorim Jaggie48f4282015-11-06 17:32:44 +0100124 TaskStack stack = mDisplayContent.mService.mStackIdToStack.get(DOCKED_STACK_ID);
Jorim Jaggi7998e482016-02-12 18:47:06 -0800125
126 // If the stack is invisible, we policy force hide it in WindowAnimator.shouldForceHide
127 final boolean visible = stack != null;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800128 if (mLastVisibility == visible && !force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800129 return;
130 }
131 mLastVisibility = visible;
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100132 notifyDockedDividerVisibilityChanged(visible);
Jorim Jaggi50981592015-12-29 17:54:12 +0100133 if (!visible) {
134 setResizeDimLayer(false, INVALID_STACK_ID, 0f);
135 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100136 }
137
138 boolean wasVisible() {
139 return mLastVisibility;
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100140 }
141
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700142 void positionDockedStackedDivider(Rect frame) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700143 TaskStack stack = mDisplayContent.getDockedStackLocked();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700144 if (stack == null) {
145 // Unfortunately we might end up with still having a divider, even though the underlying
146 // stack was already removed. This is because we are on AM thread and the removal of the
Filip Gruszczynski77049052015-11-09 14:01:21 -0800147 // divider was deferred to WM thread and hasn't happened yet. In that case let's just
148 // keep putting it in the same place it was before the stack was removed to have
149 // continuity and prevent it from jumping to the center. It will get hidden soon.
150 frame.set(mLastRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700151 return;
Filip Gruszczynski77049052015-11-09 14:01:21 -0800152 } else {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800153 stack.getDimBounds(mTmpRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700154 }
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100155 int side = stack.getDockSide();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700156 switch (side) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700157 case DOCKED_LEFT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100158 frame.set(mTmpRect.right - mDividerInsets, frame.top,
159 mTmpRect.right + frame.width() - mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700160 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700161 case DOCKED_TOP:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100162 frame.set(frame.left, mTmpRect.bottom - mDividerInsets,
163 mTmpRect.right, mTmpRect.bottom + frame.height() - mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700164 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700165 case DOCKED_RIGHT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100166 frame.set(mTmpRect.left - frame.width() + mDividerInsets, frame.top,
167 mTmpRect.left + mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700168 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700169 case DOCKED_BOTTOM:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100170 frame.set(frame.left, mTmpRect.top - frame.height() + mDividerInsets,
171 frame.right, mTmpRect.top + mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700172 break;
173 }
Filip Gruszczynski77049052015-11-09 14:01:21 -0800174 mLastRect.set(frame);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700175 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800176
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100177 void notifyDockedDividerVisibilityChanged(boolean visible) {
178 final int size = mDockedStackListeners.beginBroadcast();
179 for (int i = 0; i < size; ++i) {
180 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
181 try {
182 listener.onDividerVisibilityChanged(visible);
183 } catch (RemoteException e) {
184 Slog.e(TAG_WM, "Error delivering divider visibility changed event.", e);
185 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800186 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100187 mDockedStackListeners.finishBroadcast();
188 }
189
190 void notifyDockedStackExistsChanged(boolean exists) {
191 final int size = mDockedStackListeners.beginBroadcast();
192 for (int i = 0; i < size; ++i) {
193 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
194 try {
195 listener.onDockedStackExistsChanged(exists);
196 } catch (RemoteException e) {
197 Slog.e(TAG_WM, "Error delivering docked stack exists changed event.", e);
198 }
199 }
200 mDockedStackListeners.finishBroadcast();
201 }
202
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800203 void notifyDockedStackMinimizedChanged(boolean minimizedDock, long animDuration) {
204 final int size = mDockedStackListeners.beginBroadcast();
205 for (int i = 0; i < size; ++i) {
206 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
207 try {
208 listener.onDockedStackMinimizedChanged(minimizedDock, animDuration);
209 } catch (RemoteException e) {
210 Slog.e(TAG_WM, "Error delivering minimized dock changed event.", e);
211 }
212 }
213 mDockedStackListeners.finishBroadcast();
214 }
215
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100216 void registerDockedStackListener(IDockedStackListener listener) {
217 mDockedStackListeners.register(listener);
218 notifyDockedDividerVisibilityChanged(wasVisible());
219 notifyDockedStackExistsChanged(
220 mDisplayContent.mService.mStackIdToStack.get(DOCKED_STACK_ID) != null);
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800221 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100222
223 void setResizeDimLayer(boolean visible, int targetStackId, float alpha) {
224 SurfaceControl.openTransaction();
225 TaskStack stack = mDisplayContent.mService.mStackIdToStack.get(targetStackId);
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100226 boolean visibleAndValid = visible && stack != null;
227 if (visibleAndValid) {
Jorim Jaggi50981592015-12-29 17:54:12 +0100228 stack.getDimBounds(mTmpRect);
Jorim Jaggi10b89dc2016-01-05 15:40:17 +0100229 if (mTmpRect.height() > 0 && mTmpRect.width() > 0) {
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100230 mDimLayer.setBounds(mTmpRect);
231 mDimLayer.show(mDisplayContent.mService.mLayersController.getResizeDimLayer(),
232 alpha, 0 /* duration */);
233 } else {
234 visibleAndValid = false;
235 }
236 }
237 if (!visibleAndValid) {
Jorim Jaggi50981592015-12-29 17:54:12 +0100238 mDimLayer.hide();
239 }
240 SurfaceControl.closeTransaction();
241 }
242
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800243 /**
244 * Notifies the docked stack divider controller of a visibility change that happens without
245 * an animation.
246 */
247 void notifyAppVisibilityChanged(AppWindowToken wtoken, boolean visible) {
248 final Task task = wtoken.mTask;
249 if (!task.isHomeTask() || !task.isVisibleForUser()) {
250 return;
251 }
252
253 // If the stack is completely offscreen, this might just be an intermediate state when
254 // docking a task/launching recents at the same time, but home doesn't actually get
255 // visible after the state settles in.
256 if (isWithinDisplay(task)
257 && mDisplayContent.getDockedStackVisibleForUserLocked() != null) {
258 setMinimizedDockedStack(visible, false /* animate */);
259 }
260 }
261
262 void notifyAppTransitionStarting(ArraySet<AppWindowToken> openingApps,
263 ArraySet<AppWindowToken> closingApps) {
264 if (containsHomeTaskWithinDisplay(openingApps)) {
265 setMinimizedDockedStack(true /* minimized */, true /* animate */);
266 } else if (containsHomeTaskWithinDisplay(closingApps)) {
267 setMinimizedDockedStack(false /* minimized */, true /* animate */);
268 }
269 }
270
271 private boolean containsHomeTaskWithinDisplay(ArraySet<AppWindowToken> apps) {
272 for (int i = apps.size() - 1; i >= 0; i--) {
273 final Task task = apps.valueAt(i).mTask;
274 if (task != null && task.isHomeTask()) {
275 return isWithinDisplay(task);
276 }
277 }
278 return false;
279 }
280
281 private boolean isWithinDisplay(Task task) {
282 task.mStack.getBounds(mTmpRect);
283 mDisplayContent.getLogicalDisplayRect(mTmpRect2);
284 return mTmpRect.intersect(mTmpRect2);
285 }
286
287 /**
288 * Sets whether the docked stack is currently in a minimized state, i.e. all the tasks in the
289 * docked stack are heavily clipped so you can only see a minimal peek state.
290 *
291 * @param minimizedDock Whether the docked stack is currently minimized.
292 * @param animate Whether to animate the change.
293 */
294 private void setMinimizedDockedStack(boolean minimizedDock, boolean animate) {
295 if (minimizedDock == mMinimizedDock
296 || mDisplayContent.getDockedStackVisibleForUserLocked() == null) {
297 return;
298 }
299
300 mMinimizedDock = minimizedDock;
301 if (minimizedDock) {
302 if (animate) {
303 startAdjustAnimation(0f, 1f);
304 } else {
305 setMinimizedDockedStack(true);
306 }
307 } else {
308 if (animate) {
309 startAdjustAnimation(1f, 0f);
310 } else {
311 setMinimizedDockedStack(false);
312 }
313 }
314 }
315
316 private void startAdjustAnimation(float from, float to) {
317 mAnimating = true;
318 mAnimationStarted = false;
319 mAnimationStart = from;
320 mAnimationTarget = to;
321 }
322
323 private void setMinimizedDockedStack(boolean minimized) {
324 final TaskStack stack = mDisplayContent.getDockedStackVisibleForUserLocked();
325 if (stack == null) {
326 return;
327 }
328 if (stack.setAdjustedForMinimizedDock(minimized ? 1f : 0f)) {
329 mService.mWindowPlacerLocked.performSurfacePlacement();
330 }
331 notifyDockedStackMinimizedChanged(minimized, 0);
332 }
333
334 public boolean animate(long now) {
335 if (!mAnimating) {
336 return false;
337 }
338
339 if (!mAnimationStarted) {
340 mAnimationStarted = true;
341 mAnimationStartTime = now;
342 notifyDockedStackMinimizedChanged(mMinimizedDock,
343 MINIMIZED_DOCK_ANIMATION_DURATION);
344 }
345 float t = Math.min(1f, (float) (now - mAnimationStartTime)
346 / MINIMIZED_DOCK_ANIMATION_DURATION);
347 t = mMinimizedDockInterpolator.getInterpolation(t);
348 final TaskStack stack = mDisplayContent.getDockedStackVisibleForUserLocked();
349 if (stack != null) {
350 final float amount = t * mAnimationTarget + (1 - t) * mAnimationStart;
351 if (stack.setAdjustedForMinimizedDock(amount)) {
352 mService.mWindowPlacerLocked.performSurfacePlacement();
353 }
354 }
355 if (t >= 1.0f) {
356 mAnimating = false;
357 return false;
358 } else {
359 return true;
360 }
361 }
362
Jorim Jaggi50981592015-12-29 17:54:12 +0100363 @Override
364 public boolean isFullscreen() {
365 return false;
366 }
367
368 @Override
369 public DisplayInfo getDisplayInfo() {
370 return mDisplayContent.getDisplayInfo();
371 }
372
373 @Override
374 public void getDimBounds(Rect outBounds) {
375 // This dim layer user doesn't need this.
376 }
377
378 @Override
379 public String toShortString() {
380 return TAG;
381 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700382}