blob: 3bfcf00885576e7c9f8031f9015f9f929913f2ba [file] [log] [blame]
Jorim Jaggi50981592015-12-29 17:54:12 +01001/*
2 * Copyright (C) 2015 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 */
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080016
Jorim Jaggi50981592015-12-29 17:54:12 +010017package com.android.server.wm;
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080018
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080019import android.util.Slog;
20import android.view.Display;
21
22import java.io.PrintWriter;
Wale Ogunwale26b7b432016-02-03 14:12:08 -080023import java.util.ArrayDeque;
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080024
Wale Ogunwale26b7b432016-02-03 14:12:08 -080025import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
26import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Jorim Jaggi50981592015-12-29 17:54:12 +010027import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
28import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
29import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
30import static com.android.server.wm.WindowManagerService.WINDOW_LAYER_MULTIPLIER;
31
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080032/**
33 * Controller for assigning layers to windows on the display.
34 *
35 * This class encapsulates general algorithm for assigning layers and special rules that we need to
36 * apply on top. The general algorithm goes through windows from bottom to the top and the higher
37 * the window is, the higher layer is assigned. The final layer is equal to base layer +
38 * adjustment from the order. This means that the window list is assumed to be ordered roughly by
39 * the base layer (there are exceptions, e.g. due to keyguard and wallpaper and they need to be
40 * handled with care, because they break the algorithm).
41 *
42 * On top of the general algorithm we add special rules, that govern such amazing things as:
43 * <li>IME (which has higher base layer, but will be positioned above application windows)</li>
44 * <li>docked/pinned windows (that need to be lifted above other application windows, including
45 * animations)
46 * <li>dock divider (which needs to live above applications, but below IME)</li>
47 * <li>replaced windows, which need to live above their normal level, because they anticipate
48 * an animation</li>.
49 */
50public class WindowLayersController {
51 private final WindowManagerService mService;
52
53 private int mInputMethodAnimLayerAdjustment;
54
55 public WindowLayersController(WindowManagerService service) {
56 mService = service;
57 }
58
59 private int mHighestApplicationLayer = 0;
Wale Ogunwale26b7b432016-02-03 14:12:08 -080060 private ArrayDeque<WindowState> mPinnedWindows = new ArrayDeque<>();
61 private ArrayDeque<WindowState> mDockedWindows = new ArrayDeque<>();
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080062 private WindowState mDockDivider = null;
Wale Ogunwale26b7b432016-02-03 14:12:08 -080063 private ArrayDeque<WindowState> mReplacingWindows = new ArrayDeque<>();
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080064
65 final void assignLayersLocked(WindowList windows) {
66 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Assigning layers based on windows=" + windows,
67 new RuntimeException("here").fillInStackTrace());
68
69 clear();
70 int curBaseLayer = 0;
71 int curLayer = 0;
72 boolean anyLayerChanged = false;
73 for (int i = 0, windowCount = windows.size(); i < windowCount; i++) {
74 final WindowState w = windows.get(i);
75 boolean layerChanged = false;
76
77 int oldLayer = w.mLayer;
78 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow || (i > 0 && w.mIsWallpaper)) {
79 curLayer += WINDOW_LAYER_MULTIPLIER;
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080080 } else {
81 curBaseLayer = curLayer = w.mBaseLayer;
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080082 }
Robert Carr67eec032016-03-22 16:57:33 -070083 assignAnimLayer(w, curLayer);
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080084
Robert Carr67eec032016-03-22 16:57:33 -070085 // TODO: Preserved old behavior of code here but not sure comparing
86 // oldLayer to mAnimLayer and mLayer makes sense...though the
87 // worst case would be unintentionalp layer reassignment.
88 if (w.mLayer != oldLayer || w.mWinAnimator.mAnimLayer != oldLayer) {
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080089 layerChanged = true;
90 anyLayerChanged = true;
91 }
92
93 if (w.mAppToken != null) {
94 mHighestApplicationLayer = Math.max(mHighestApplicationLayer,
Robert Carr67eec032016-03-22 16:57:33 -070095 w.mWinAnimator.mAnimLayer);
Filip Gruszczynski92e432c2015-12-15 19:17:09 -080096 }
97 collectSpecialWindows(w);
98
99 if (layerChanged) {
100 w.scheduleAnimationIfDimming();
101 }
102 }
103
104 adjustSpecialWindows();
105
106 //TODO (multidisplay): Magnification is supported only for the default display.
107 if (mService.mAccessibilityController != null && anyLayerChanged
108 && windows.get(windows.size() - 1).getDisplayId() == Display.DEFAULT_DISPLAY) {
109 mService.mAccessibilityController.onWindowLayersChangedLocked();
110 }
111
112 if (DEBUG_LAYERS) logDebugLayers(windows);
113 }
114
115 void setInputMethodAnimLayerAdjustment(int adj) {
116 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
117 mInputMethodAnimLayerAdjustment = adj;
118 final WindowState imw = mService.mInputMethodWindow;
119 if (imw != null) {
120 imw.mWinAnimator.mAnimLayer = imw.mLayer + adj;
121 if (DEBUG_LAYERS) Slog.v(TAG_WM, "IM win " + imw
122 + " anim layer: " + imw.mWinAnimator.mAnimLayer);
123 for (int i = imw.mChildWindows.size() - 1; i >= 0; i--) {
124 final WindowState childWindow = imw.mChildWindows.get(i);
125 childWindow.mWinAnimator.mAnimLayer = childWindow.mLayer + adj;
126 if (DEBUG_LAYERS) Slog.v(TAG_WM, "IM win " + childWindow
127 + " anim layer: " + childWindow.mWinAnimator.mAnimLayer);
128 }
129 }
Michael Wright6db8e692015-12-17 17:19:46 -0500130 for (int i = mService.mInputMethodDialogs.size() - 1; i >= 0; i--) {
Filip Gruszczynski92e432c2015-12-15 19:17:09 -0800131 final WindowState dialog = mService.mInputMethodDialogs.get(i);
132 dialog.mWinAnimator.mAnimLayer = dialog.mLayer + adj;
133 if (DEBUG_LAYERS) Slog.v(TAG_WM, "IM win " + imw
134 + " anim layer: " + dialog.mWinAnimator.mAnimLayer);
135 }
136 }
137
138 int getSpecialWindowAnimLayerAdjustment(WindowState win) {
139 if (win.mIsImWindow) {
140 return mInputMethodAnimLayerAdjustment;
141 } else if (win.mIsWallpaper) {
142 return mService.mWallpaperControllerLocked.getAnimLayerAdjustment();
143 }
144 return 0;
145 }
146
Jorim Jaggi50981592015-12-29 17:54:12 +0100147 /**
148 * @return The layer used for dimming the apps when dismissing docked/fullscreen stack. Just
149 * above all application surfaces.
150 */
151 int getResizeDimLayer() {
152 return mDockDivider.mLayer - 1;
153 }
154
Filip Gruszczynski92e432c2015-12-15 19:17:09 -0800155 private void logDebugLayers(WindowList windows) {
156 for (int i = 0, n = windows.size(); i < n; i++) {
157 final WindowState w = windows.get(i);
158 final WindowStateAnimator winAnimator = w.mWinAnimator;
159 Slog.v(TAG_WM, "Assign layer " + w + ": " + "mBase=" + w.mBaseLayer
160 + " mLayer=" + w.mLayer + (w.mAppToken == null
161 ? "" : " mAppLayer=" + w.mAppToken.mAppAnimator.animLayerAdjustment)
162 + " =mAnimLayer=" + winAnimator.mAnimLayer);
163 }
164 }
165
166 private void clear() {
167 mHighestApplicationLayer = 0;
Wale Ogunwale26b7b432016-02-03 14:12:08 -0800168 mPinnedWindows.clear();
169 mDockedWindows.clear();
170 mReplacingWindows.clear();
Filip Gruszczynski92e432c2015-12-15 19:17:09 -0800171 mDockDivider = null;
172 }
173
174 private void collectSpecialWindows(WindowState w) {
Filip Gruszczynskibedb5662016-01-04 15:56:07 -0800175 if (w.mAttrs.type == TYPE_DOCK_DIVIDER) {
Filip Gruszczynski92e432c2015-12-15 19:17:09 -0800176 mDockDivider = w;
Wale Ogunwale26b7b432016-02-03 14:12:08 -0800177 return;
178 }
179 if (w.mWillReplaceWindow) {
180 mReplacingWindows.add(w);
181 }
182 final TaskStack stack = w.getStack();
183 if (stack == null) {
184 return;
185 }
186 if (stack.mStackId == PINNED_STACK_ID) {
187 mPinnedWindows.add(w);
188 } else if (stack.mStackId == DOCKED_STACK_ID) {
189 mDockedWindows.add(w);
Filip Gruszczynski92e432c2015-12-15 19:17:09 -0800190 }
191 }
192
193 private void adjustSpecialWindows() {
Robert Carr6412d752016-03-31 17:23:35 -0700194 int layer = mHighestApplicationLayer + WINDOW_LAYER_MULTIPLIER;
Wale Ogunwale26b7b432016-02-03 14:12:08 -0800195 // For pinned and docked stack window, we want to make them above other windows also when
196 // these windows are animating.
197 while (!mDockedWindows.isEmpty()) {
198 layer = assignAndIncreaseLayerIfNeeded(mDockedWindows.remove(), layer);
199 }
Jorim Jaggi50981592015-12-29 17:54:12 +0100200
Filip Gruszczynski92e432c2015-12-15 19:17:09 -0800201 layer = assignAndIncreaseLayerIfNeeded(mDockDivider, layer);
Robert Carrebe9c0d2016-03-31 15:27:40 -0700202
203 // If we have a dock divider ensure the Input Method is above it.
204 if (mDockDivider != null && mService.mInputMethodWindow != null) {
205 layer = assignAndIncreaseLayerIfNeeded(mService.mInputMethodWindow, layer);
206 }
207
Wale Ogunwale26b7b432016-02-03 14:12:08 -0800208 // We know that we will be animating a relaunching window in the near future, which will
209 // receive a z-order increase. We want the replaced window to immediately receive the same
210 // treatment, e.g. to be above the dock divider.
211 while (!mReplacingWindows.isEmpty()) {
212 layer = assignAndIncreaseLayerIfNeeded(mReplacingWindows.remove(), layer);
213 }
214
215 while (!mPinnedWindows.isEmpty()) {
216 layer = assignAndIncreaseLayerIfNeeded(mPinnedWindows.remove(), layer);
217 }
Filip Gruszczynski92e432c2015-12-15 19:17:09 -0800218 }
219
220 private int assignAndIncreaseLayerIfNeeded(WindowState win, int layer) {
221 if (win != null) {
Robert Carr67eec032016-03-22 16:57:33 -0700222 assignAnimLayer(win, layer);
Robert Carr6412d752016-03-31 17:23:35 -0700223 // Make sure we leave space inbetween normal windows for dims and such.
224 layer += WINDOW_LAYER_MULTIPLIER;
Filip Gruszczynski92e432c2015-12-15 19:17:09 -0800225 }
226 return layer;
227 }
Robert Carr6412d752016-03-31 17:23:35 -0700228
Robert Carr67eec032016-03-22 16:57:33 -0700229 private void assignAnimLayer(WindowState w, int layer) {
230 w.mLayer = layer;
231 w.mWinAnimator.mAnimLayer = w.mLayer + w.getAnimLayerAdjustment() +
232 getSpecialWindowAnimLayerAdjustment(w);
Jorim Jaggic69bd222016-03-15 14:38:37 +0100233 if (w.mAppToken != null && w.mAppToken.mAppAnimator.thumbnailForceAboveLayer > 0
234 && w.mWinAnimator.mAnimLayer > w.mAppToken.mAppAnimator.thumbnailForceAboveLayer) {
235 w.mAppToken.mAppAnimator.thumbnailForceAboveLayer = w.mWinAnimator.mAnimLayer;
236 }
Robert Carr67eec032016-03-22 16:57:33 -0700237 }
Filip Gruszczynski92e432c2015-12-15 19:17:09 -0800238
239 void dump(PrintWriter pw, String s) {
240 if (mInputMethodAnimLayerAdjustment != 0 ||
241 mService.mWallpaperControllerLocked.getAnimLayerAdjustment() != 0) {
242 pw.print(" mInputMethodAnimLayerAdjustment=");
243 pw.print(mInputMethodAnimLayerAdjustment);
244 pw.print(" mWallpaperAnimLayerAdjustment=");
245 pw.println(mService.mWallpaperControllerLocked.getAnimLayerAdjustment());
246 }
247 }
248}