blob: 75c06ff32274c172429503eebaa45855b3cbc00b [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;
Filip Gruszczynski77049052015-11-09 14:01:21 -080023import android.util.Slog;
Jorim Jaggi50981592015-12-29 17:54:12 +010024import android.view.DisplayInfo;
Jorim Jaggia6c934e2015-12-21 13:22:31 +010025import android.view.IDockedStackListener;
Jorim Jaggi50981592015-12-29 17:54:12 +010026import android.view.SurfaceControl;
27
28import com.android.server.wm.DimLayer.DimLayerUser;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010029
Jorim Jaggic662d8e2016-02-05 16:54:54 -080030import java.util.ArrayList;
31
Jorim Jaggie48f4282015-11-06 17:32:44 +010032import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Jorim Jaggi50981592015-12-29 17:54:12 +010033import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010034import static android.view.WindowManager.DOCKED_BOTTOM;
35import static android.view.WindowManager.DOCKED_LEFT;
36import static android.view.WindowManager.DOCKED_RIGHT;
37import static android.view.WindowManager.DOCKED_TOP;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080038import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
39import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070040
41/**
Jorim Jaggi61f39a72015-10-29 16:54:18 +010042 * Keeps information about the docked stack divider.
Filip Gruszczynski466f3212015-09-21 17:57:57 -070043 */
Jorim Jaggi50981592015-12-29 17:54:12 +010044public class DockedStackDividerController implements DimLayerUser {
Jorim Jaggi61f39a72015-10-29 16:54:18 +010045
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080046 private static final String TAG = TAG_WITH_CLASS_NAME ? "DockedStackDividerController" : TAG_WM;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010047
Filip Gruszczynski466f3212015-09-21 17:57:57 -070048 private final DisplayContent mDisplayContent;
Jorim Jaggi1fcbab62015-11-04 16:39:50 +010049 private final int mDividerWindowWidth;
50 private final int mDividerInsets;
Jorim Jaggi61f39a72015-10-29 16:54:18 +010051 private boolean mResizing;
52 private WindowState mWindow;
53 private final Rect mTmpRect = new Rect();
Filip Gruszczynski77049052015-11-09 14:01:21 -080054 private final Rect mLastRect = new Rect();
Filip Gruszczynski64cdc142015-11-29 21:10:07 -080055 private boolean mLastVisibility = false;
Jorim Jaggia6c934e2015-12-21 13:22:31 +010056 private final RemoteCallbackList<IDockedStackListener> mDockedStackListeners
57 = new RemoteCallbackList<>();
Jorim Jaggi50981592015-12-29 17:54:12 +010058 private final DimLayer mDimLayer;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -070059
Filip Gruszczynski466f3212015-09-21 17:57:57 -070060 DockedStackDividerController(Context context, DisplayContent displayContent) {
Filip Gruszczynski466f3212015-09-21 17:57:57 -070061 mDisplayContent = displayContent;
Jorim Jaggi1fcbab62015-11-04 16:39:50 +010062 mDividerWindowWidth = context.getResources().getDimensionPixelSize(
Filip Gruszczynski466f3212015-09-21 17:57:57 -070063 com.android.internal.R.dimen.docked_stack_divider_thickness);
Jorim Jaggi1fcbab62015-11-04 16:39:50 +010064 mDividerInsets = context.getResources().getDimensionPixelSize(
65 com.android.internal.R.dimen.docked_stack_divider_insets);
Jorim Jaggi50981592015-12-29 17:54:12 +010066 mDimLayer = new DimLayer(displayContent.mService, this, displayContent.getDisplayId());
Filip Gruszczynski466f3212015-09-21 17:57:57 -070067 }
68
Jorim Jaggi61f39a72015-10-29 16:54:18 +010069 boolean isResizing() {
70 return mResizing;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070071 }
72
Jorim Jaggi1fcbab62015-11-04 16:39:50 +010073 int getContentWidth() {
74 return mDividerWindowWidth - 2 * mDividerInsets;
Filip Gruszczynski466f3212015-09-21 17:57:57 -070075 }
76
Jorim Jaggi81ba11e2016-02-03 22:04:22 -080077 int getContentInsets() {
78 return mDividerInsets;
79 }
80
Jorim Jaggi61f39a72015-10-29 16:54:18 +010081 void setResizing(boolean resizing) {
Jorim Jaggic662d8e2016-02-05 16:54:54 -080082 if (mResizing != resizing) {
83 mResizing = resizing;
84 resetDragResizingChangeReported();
85 }
86 }
87
88 private void resetDragResizingChangeReported() {
89 final WindowList windowList = mDisplayContent.getWindowList();
90 for (int i = windowList.size() - 1; i >= 0; i--) {
91 windowList.get(i).resetDragResizingChangeReported();
92 }
Jorim Jaggi61f39a72015-10-29 16:54:18 +010093 }
94
95 void setWindow(WindowState window) {
96 mWindow = window;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -080097 reevaluateVisibility(false);
Jorim Jaggi61f39a72015-10-29 16:54:18 +010098 }
99
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800100 void reevaluateVisibility(boolean force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800101 if (mWindow == null) {
102 return;
103 }
Jorim Jaggie48f4282015-11-06 17:32:44 +0100104 TaskStack stack = mDisplayContent.mService.mStackIdToStack.get(DOCKED_STACK_ID);
Jorim Jaggi7998e482016-02-12 18:47:06 -0800105
106 // If the stack is invisible, we policy force hide it in WindowAnimator.shouldForceHide
107 final boolean visible = stack != null;
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800108 if (mLastVisibility == visible && !force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800109 return;
110 }
111 mLastVisibility = visible;
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100112 notifyDockedDividerVisibilityChanged(visible);
Jorim Jaggi50981592015-12-29 17:54:12 +0100113 if (!visible) {
114 setResizeDimLayer(false, INVALID_STACK_ID, 0f);
115 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100116 }
117
118 boolean wasVisible() {
119 return mLastVisibility;
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100120 }
121
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700122 void positionDockedStackedDivider(Rect frame) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700123 TaskStack stack = mDisplayContent.getDockedStackLocked();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700124 if (stack == null) {
125 // Unfortunately we might end up with still having a divider, even though the underlying
126 // stack was already removed. This is because we are on AM thread and the removal of the
Filip Gruszczynski77049052015-11-09 14:01:21 -0800127 // divider was deferred to WM thread and hasn't happened yet. In that case let's just
128 // keep putting it in the same place it was before the stack was removed to have
129 // continuity and prevent it from jumping to the center. It will get hidden soon.
130 frame.set(mLastRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700131 return;
Filip Gruszczynski77049052015-11-09 14:01:21 -0800132 } else {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800133 stack.getDimBounds(mTmpRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700134 }
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100135 int side = stack.getDockSide();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700136 switch (side) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700137 case DOCKED_LEFT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100138 frame.set(mTmpRect.right - mDividerInsets, frame.top,
139 mTmpRect.right + frame.width() - mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700140 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700141 case DOCKED_TOP:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100142 frame.set(frame.left, mTmpRect.bottom - mDividerInsets,
143 mTmpRect.right, mTmpRect.bottom + frame.height() - mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700144 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700145 case DOCKED_RIGHT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100146 frame.set(mTmpRect.left - frame.width() + mDividerInsets, frame.top,
147 mTmpRect.left + mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700148 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700149 case DOCKED_BOTTOM:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100150 frame.set(frame.left, mTmpRect.top - frame.height() + mDividerInsets,
151 frame.right, mTmpRect.top + mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700152 break;
153 }
Filip Gruszczynski77049052015-11-09 14:01:21 -0800154 mLastRect.set(frame);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700155 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800156
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100157 void notifyDockedDividerVisibilityChanged(boolean visible) {
158 final int size = mDockedStackListeners.beginBroadcast();
159 for (int i = 0; i < size; ++i) {
160 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
161 try {
162 listener.onDividerVisibilityChanged(visible);
163 } catch (RemoteException e) {
164 Slog.e(TAG_WM, "Error delivering divider visibility changed event.", e);
165 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800166 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100167 mDockedStackListeners.finishBroadcast();
168 }
169
170 void notifyDockedStackExistsChanged(boolean exists) {
171 final int size = mDockedStackListeners.beginBroadcast();
172 for (int i = 0; i < size; ++i) {
173 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
174 try {
175 listener.onDockedStackExistsChanged(exists);
176 } catch (RemoteException e) {
177 Slog.e(TAG_WM, "Error delivering docked stack exists changed event.", e);
178 }
179 }
180 mDockedStackListeners.finishBroadcast();
181 }
182
183 void registerDockedStackListener(IDockedStackListener listener) {
184 mDockedStackListeners.register(listener);
185 notifyDockedDividerVisibilityChanged(wasVisible());
186 notifyDockedStackExistsChanged(
187 mDisplayContent.mService.mStackIdToStack.get(DOCKED_STACK_ID) != null);
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800188 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100189
190 void setResizeDimLayer(boolean visible, int targetStackId, float alpha) {
191 SurfaceControl.openTransaction();
192 TaskStack stack = mDisplayContent.mService.mStackIdToStack.get(targetStackId);
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100193 boolean visibleAndValid = visible && stack != null;
194 if (visibleAndValid) {
Jorim Jaggi50981592015-12-29 17:54:12 +0100195 stack.getDimBounds(mTmpRect);
Jorim Jaggi10b89dc2016-01-05 15:40:17 +0100196 if (mTmpRect.height() > 0 && mTmpRect.width() > 0) {
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100197 mDimLayer.setBounds(mTmpRect);
198 mDimLayer.show(mDisplayContent.mService.mLayersController.getResizeDimLayer(),
199 alpha, 0 /* duration */);
200 } else {
201 visibleAndValid = false;
202 }
203 }
204 if (!visibleAndValid) {
Jorim Jaggi50981592015-12-29 17:54:12 +0100205 mDimLayer.hide();
206 }
207 SurfaceControl.closeTransaction();
208 }
209
210 @Override
211 public boolean isFullscreen() {
212 return false;
213 }
214
215 @Override
216 public DisplayInfo getDisplayInfo() {
217 return mDisplayContent.getDisplayInfo();
218 }
219
220 @Override
221 public void getDimBounds(Rect outBounds) {
222 // This dim layer user doesn't need this.
223 }
224
225 @Override
226 public String toShortString() {
227 return TAG;
228 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700229}