Jorim Jaggi | 5098159 | 2015-12-29 17:54:12 +0100 | [diff] [blame] | 1 | /* |
| 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 Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 16 | |
Jorim Jaggi | 5098159 | 2015-12-29 17:54:12 +0100 | [diff] [blame] | 17 | package com.android.server.wm; |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 18 | |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 19 | import android.util.Slog; |
| 20 | import android.view.Display; |
| 21 | |
| 22 | import java.io.PrintWriter; |
Wale Ogunwale | 26b7b43 | 2016-02-03 14:12:08 -0800 | [diff] [blame] | 23 | import java.util.ArrayDeque; |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 24 | |
Wale Ogunwale | 26b7b43 | 2016-02-03 14:12:08 -0800 | [diff] [blame] | 25 | import static android.app.ActivityManager.StackId.DOCKED_STACK_ID; |
| 26 | import static android.app.ActivityManager.StackId.PINNED_STACK_ID; |
Jorim Jaggi | 5098159 | 2015-12-29 17:54:12 +0100 | [diff] [blame] | 27 | import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER; |
| 28 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS; |
| 29 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 30 | import static com.android.server.wm.WindowManagerService.WINDOW_LAYER_MULTIPLIER; |
| 31 | |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 32 | /** |
| 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 | */ |
| 50 | public 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 Ogunwale | 26b7b43 | 2016-02-03 14:12:08 -0800 | [diff] [blame] | 60 | private ArrayDeque<WindowState> mPinnedWindows = new ArrayDeque<>(); |
| 61 | private ArrayDeque<WindowState> mDockedWindows = new ArrayDeque<>(); |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 62 | private WindowState mDockDivider = null; |
Wale Ogunwale | 26b7b43 | 2016-02-03 14:12:08 -0800 | [diff] [blame] | 63 | private ArrayDeque<WindowState> mReplacingWindows = new ArrayDeque<>(); |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 64 | |
| 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 Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 80 | } else { |
| 81 | curBaseLayer = curLayer = w.mBaseLayer; |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 82 | } |
Robert Carr | 67eec03 | 2016-03-22 16:57:33 -0700 | [diff] [blame] | 83 | assignAnimLayer(w, curLayer); |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 84 | |
Robert Carr | 67eec03 | 2016-03-22 16:57:33 -0700 | [diff] [blame] | 85 | // 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 Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 89 | layerChanged = true; |
| 90 | anyLayerChanged = true; |
| 91 | } |
| 92 | |
| 93 | if (w.mAppToken != null) { |
| 94 | mHighestApplicationLayer = Math.max(mHighestApplicationLayer, |
Robert Carr | 67eec03 | 2016-03-22 16:57:33 -0700 | [diff] [blame] | 95 | w.mWinAnimator.mAnimLayer); |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 96 | } |
| 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 Wright | 6db8e69 | 2015-12-17 17:19:46 -0500 | [diff] [blame] | 130 | for (int i = mService.mInputMethodDialogs.size() - 1; i >= 0; i--) { |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 131 | 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 Jaggi | 5098159 | 2015-12-29 17:54:12 +0100 | [diff] [blame] | 147 | /** |
| 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 Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 155 | 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 Ogunwale | 26b7b43 | 2016-02-03 14:12:08 -0800 | [diff] [blame] | 168 | mPinnedWindows.clear(); |
| 169 | mDockedWindows.clear(); |
| 170 | mReplacingWindows.clear(); |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 171 | mDockDivider = null; |
| 172 | } |
| 173 | |
| 174 | private void collectSpecialWindows(WindowState w) { |
Filip Gruszczynski | bedb566 | 2016-01-04 15:56:07 -0800 | [diff] [blame] | 175 | if (w.mAttrs.type == TYPE_DOCK_DIVIDER) { |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 176 | mDockDivider = w; |
Wale Ogunwale | 26b7b43 | 2016-02-03 14:12:08 -0800 | [diff] [blame] | 177 | 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 Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | private void adjustSpecialWindows() { |
Robert Carr | 6412d75 | 2016-03-31 17:23:35 -0700 | [diff] [blame] | 194 | int layer = mHighestApplicationLayer + WINDOW_LAYER_MULTIPLIER; |
Wale Ogunwale | 26b7b43 | 2016-02-03 14:12:08 -0800 | [diff] [blame] | 195 | // 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 Jaggi | 5098159 | 2015-12-29 17:54:12 +0100 | [diff] [blame] | 200 | |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 201 | layer = assignAndIncreaseLayerIfNeeded(mDockDivider, layer); |
Robert Carr | ebe9c0d | 2016-03-31 15:27:40 -0700 | [diff] [blame] | 202 | |
| 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 Ogunwale | 26b7b43 | 2016-02-03 14:12:08 -0800 | [diff] [blame] | 208 | // 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 Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | private int assignAndIncreaseLayerIfNeeded(WindowState win, int layer) { |
| 221 | if (win != null) { |
Robert Carr | 67eec03 | 2016-03-22 16:57:33 -0700 | [diff] [blame] | 222 | assignAnimLayer(win, layer); |
Robert Carr | 6412d75 | 2016-03-31 17:23:35 -0700 | [diff] [blame] | 223 | // Make sure we leave space inbetween normal windows for dims and such. |
| 224 | layer += WINDOW_LAYER_MULTIPLIER; |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 225 | } |
| 226 | return layer; |
| 227 | } |
Robert Carr | 6412d75 | 2016-03-31 17:23:35 -0700 | [diff] [blame] | 228 | |
Robert Carr | 67eec03 | 2016-03-22 16:57:33 -0700 | [diff] [blame] | 229 | private void assignAnimLayer(WindowState w, int layer) { |
| 230 | w.mLayer = layer; |
| 231 | w.mWinAnimator.mAnimLayer = w.mLayer + w.getAnimLayerAdjustment() + |
| 232 | getSpecialWindowAnimLayerAdjustment(w); |
Jorim Jaggi | c69bd22 | 2016-03-15 14:38:37 +0100 | [diff] [blame] | 233 | 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 Carr | 67eec03 | 2016-03-22 16:57:33 -0700 | [diff] [blame] | 237 | } |
Filip Gruszczynski | 92e432c | 2015-12-15 19:17:09 -0800 | [diff] [blame] | 238 | |
| 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 | } |