blob: 685403c05ba2295dd544ec191894f6de0864cc84 [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);
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800105 final boolean visible = stack != null && stack.isVisibleLocked();
Filip Gruszczynski85d5cc42015-12-04 09:21:37 -0800106 if (mLastVisibility == visible && !force) {
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800107 return;
108 }
109 mLastVisibility = visible;
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100110 notifyDockedDividerVisibilityChanged(visible);
Jorim Jaggi50981592015-12-29 17:54:12 +0100111 if (!visible) {
112 setResizeDimLayer(false, INVALID_STACK_ID, 0f);
113 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100114 }
115
116 boolean wasVisible() {
117 return mLastVisibility;
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100118 }
119
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700120 void positionDockedStackedDivider(Rect frame) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700121 TaskStack stack = mDisplayContent.getDockedStackLocked();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700122 if (stack == null) {
123 // Unfortunately we might end up with still having a divider, even though the underlying
124 // stack was already removed. This is because we are on AM thread and the removal of the
Filip Gruszczynski77049052015-11-09 14:01:21 -0800125 // divider was deferred to WM thread and hasn't happened yet. In that case let's just
126 // keep putting it in the same place it was before the stack was removed to have
127 // continuity and prevent it from jumping to the center. It will get hidden soon.
128 frame.set(mLastRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700129 return;
Filip Gruszczynski77049052015-11-09 14:01:21 -0800130 } else {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800131 stack.getDimBounds(mTmpRect);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700132 }
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100133 int side = stack.getDockSide();
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700134 switch (side) {
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700135 case DOCKED_LEFT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100136 frame.set(mTmpRect.right - mDividerInsets, frame.top,
137 mTmpRect.right + frame.width() - mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700138 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700139 case DOCKED_TOP:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100140 frame.set(frame.left, mTmpRect.bottom - mDividerInsets,
141 mTmpRect.right, mTmpRect.bottom + frame.height() - mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700142 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700143 case DOCKED_RIGHT:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100144 frame.set(mTmpRect.left - frame.width() + mDividerInsets, frame.top,
145 mTmpRect.left + mDividerInsets, frame.bottom);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700146 break;
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700147 case DOCKED_BOTTOM:
Jorim Jaggi1fcbab62015-11-04 16:39:50 +0100148 frame.set(frame.left, mTmpRect.top - frame.height() + mDividerInsets,
149 frame.right, mTmpRect.top + mDividerInsets);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700150 break;
151 }
Filip Gruszczynski77049052015-11-09 14:01:21 -0800152 mLastRect.set(frame);
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700153 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800154
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100155 void notifyDockedDividerVisibilityChanged(boolean visible) {
156 final int size = mDockedStackListeners.beginBroadcast();
157 for (int i = 0; i < size; ++i) {
158 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
159 try {
160 listener.onDividerVisibilityChanged(visible);
161 } catch (RemoteException e) {
162 Slog.e(TAG_WM, "Error delivering divider visibility changed event.", e);
163 }
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800164 }
Jorim Jaggia6c934e2015-12-21 13:22:31 +0100165 mDockedStackListeners.finishBroadcast();
166 }
167
168 void notifyDockedStackExistsChanged(boolean exists) {
169 final int size = mDockedStackListeners.beginBroadcast();
170 for (int i = 0; i < size; ++i) {
171 final IDockedStackListener listener = mDockedStackListeners.getBroadcastItem(i);
172 try {
173 listener.onDockedStackExistsChanged(exists);
174 } catch (RemoteException e) {
175 Slog.e(TAG_WM, "Error delivering docked stack exists changed event.", e);
176 }
177 }
178 mDockedStackListeners.finishBroadcast();
179 }
180
181 void registerDockedStackListener(IDockedStackListener listener) {
182 mDockedStackListeners.register(listener);
183 notifyDockedDividerVisibilityChanged(wasVisible());
184 notifyDockedStackExistsChanged(
185 mDisplayContent.mService.mStackIdToStack.get(DOCKED_STACK_ID) != null);
Filip Gruszczynski64cdc142015-11-29 21:10:07 -0800186 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100187
188 void setResizeDimLayer(boolean visible, int targetStackId, float alpha) {
189 SurfaceControl.openTransaction();
190 TaskStack stack = mDisplayContent.mService.mStackIdToStack.get(targetStackId);
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100191 boolean visibleAndValid = visible && stack != null;
192 if (visibleAndValid) {
Jorim Jaggi50981592015-12-29 17:54:12 +0100193 stack.getDimBounds(mTmpRect);
Jorim Jaggi10b89dc2016-01-05 15:40:17 +0100194 if (mTmpRect.height() > 0 && mTmpRect.width() > 0) {
Jorim Jaggi7b371dd2016-01-05 15:32:34 +0100195 mDimLayer.setBounds(mTmpRect);
196 mDimLayer.show(mDisplayContent.mService.mLayersController.getResizeDimLayer(),
197 alpha, 0 /* duration */);
198 } else {
199 visibleAndValid = false;
200 }
201 }
202 if (!visibleAndValid) {
Jorim Jaggi50981592015-12-29 17:54:12 +0100203 mDimLayer.hide();
204 }
205 SurfaceControl.closeTransaction();
206 }
207
208 @Override
209 public boolean isFullscreen() {
210 return false;
211 }
212
213 @Override
214 public DisplayInfo getDisplayInfo() {
215 return mDisplayContent.getDisplayInfo();
216 }
217
218 @Override
219 public void getDimBounds(Rect outBounds) {
220 // This dim layer user doesn't need this.
221 }
222
223 @Override
224 public String toShortString() {
225 return TAG;
226 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700227}