15098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi/*
25098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi * Copyright (C) 2015 The Android Open Source Project
35098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi *
45098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi * Licensed under the Apache License, Version 2.0 (the "License");
55098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi * you may not use this file except in compliance with the License.
65098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi * You may obtain a copy of the License at
75098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi *
85098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi *      http://www.apache.org/licenses/LICENSE-2.0
95098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi *
105098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi * Unless required by applicable law or agreed to in writing, software
115098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi * distributed under the License is distributed on an "AS IS" BASIS,
125098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi * See the License for the specific language governing permissions and
145098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi * limitations under the License.
155098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi */
1692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
175098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggipackage com.android.server.wm;
1892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
1992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynskiimport android.util.Slog;
2092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynskiimport android.view.Display;
2192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
2292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynskiimport java.io.PrintWriter;
2326b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwaleimport java.util.ArrayDeque;
2492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
2526b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwaleimport static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
2626b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwaleimport static android.app.ActivityManager.StackId.PINNED_STACK_ID;
275098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggiimport static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
285098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggiimport static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
295098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggiimport static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
3020ec11b4e61e5fffd639034f936ed5ef8359d414Wale Ogunwaleimport static com.android.server.wm.WindowManagerService.LAYER_OFFSET_DIM;
315098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggiimport static com.android.server.wm.WindowManagerService.WINDOW_LAYER_MULTIPLIER;
325098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi
3392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski/**
3492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * Controller for assigning layers to windows on the display.
3592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski *
3692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * This class encapsulates general algorithm for assigning layers and special rules that we need to
3792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * apply on top. The general algorithm goes through windows from bottom to the top and the higher
3892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * the window is, the higher layer is assigned. The final layer is equal to base layer +
3992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * adjustment from the order. This means that the window list is assumed to be ordered roughly by
4092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * the base layer (there are exceptions, e.g. due to keyguard and wallpaper and they need to be
4192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * handled with care, because they break the algorithm).
4292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski *
4392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * On top of the general algorithm we add special rules, that govern such amazing things as:
4492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * <li>IME (which has higher base layer, but will be positioned above application windows)</li>
4592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * <li>docked/pinned windows (that need to be lifted above other application windows, including
4692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * animations)
4792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * <li>dock divider (which needs to live above applications, but below IME)</li>
4892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * <li>replaced windows, which need to live above their normal level, because they anticipate
4992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski * an animation</li>.
5092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski */
5192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynskipublic class WindowLayersController {
5292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    private final WindowManagerService mService;
5392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
5492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    private int mInputMethodAnimLayerAdjustment;
5592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
5692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    public WindowLayersController(WindowManagerService service) {
5792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        mService = service;
5892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    }
5992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
6092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    private int mHighestApplicationLayer = 0;
6126b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale    private ArrayDeque<WindowState> mPinnedWindows = new ArrayDeque<>();
6226b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale    private ArrayDeque<WindowState> mDockedWindows = new ArrayDeque<>();
63b976877a4b62a6030fe67796ab8a6a69b4cc041fRobert Carr    private ArrayDeque<WindowState> mInputMethodWindows = new ArrayDeque<>();
6492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    private WindowState mDockDivider = null;
6526b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale    private ArrayDeque<WindowState> mReplacingWindows = new ArrayDeque<>();
6692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
6792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    final void assignLayersLocked(WindowList windows) {
6892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        if (DEBUG_LAYERS) Slog.v(TAG_WM, "Assigning layers based on windows=" + windows,
6992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                new RuntimeException("here").fillInStackTrace());
7092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
7192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        clear();
7292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        int curBaseLayer = 0;
7392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        int curLayer = 0;
7492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        boolean anyLayerChanged = false;
7592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        for (int i = 0, windowCount = windows.size(); i < windowCount; i++) {
7692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            final WindowState w = windows.get(i);
7792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            boolean layerChanged = false;
7892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
7992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            int oldLayer = w.mLayer;
8092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            if (w.mBaseLayer == curBaseLayer || w.mIsImWindow || (i > 0 && w.mIsWallpaper)) {
8192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                curLayer += WINDOW_LAYER_MULTIPLIER;
8292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            } else {
8392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                curBaseLayer = curLayer = w.mBaseLayer;
8492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            }
8567eec038f4628f36e813329736802c331dc2dbc6Robert Carr            assignAnimLayer(w, curLayer);
8692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
8767eec038f4628f36e813329736802c331dc2dbc6Robert Carr            // TODO: Preserved old behavior of code here but not sure comparing
8867eec038f4628f36e813329736802c331dc2dbc6Robert Carr            // oldLayer to mAnimLayer and mLayer makes sense...though the
8967eec038f4628f36e813329736802c331dc2dbc6Robert Carr            // worst case would be unintentionalp layer reassignment.
9067eec038f4628f36e813329736802c331dc2dbc6Robert Carr            if (w.mLayer != oldLayer || w.mWinAnimator.mAnimLayer != oldLayer) {
9192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                layerChanged = true;
9292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                anyLayerChanged = true;
9392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            }
9492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
9592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            if (w.mAppToken != null) {
9692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                mHighestApplicationLayer = Math.max(mHighestApplicationLayer,
9767eec038f4628f36e813329736802c331dc2dbc6Robert Carr                        w.mWinAnimator.mAnimLayer);
9892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            }
9992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            collectSpecialWindows(w);
10092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
10192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            if (layerChanged) {
10292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                w.scheduleAnimationIfDimming();
10392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            }
10492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        }
10592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
10692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        adjustSpecialWindows();
10792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
10892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        //TODO (multidisplay): Magnification is supported only for the default display.
10992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        if (mService.mAccessibilityController != null && anyLayerChanged
11092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                && windows.get(windows.size() - 1).getDisplayId() == Display.DEFAULT_DISPLAY) {
11192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            mService.mAccessibilityController.onWindowLayersChangedLocked();
11292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        }
11392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
11492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        if (DEBUG_LAYERS) logDebugLayers(windows);
11592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    }
11692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
11792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    void setInputMethodAnimLayerAdjustment(int adj) {
11892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
11992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        mInputMethodAnimLayerAdjustment = adj;
12092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        final WindowState imw = mService.mInputMethodWindow;
12192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        if (imw != null) {
12292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            imw.mWinAnimator.mAnimLayer = imw.mLayer + adj;
12392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            if (DEBUG_LAYERS) Slog.v(TAG_WM, "IM win " + imw
12492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                    + " anim layer: " + imw.mWinAnimator.mAnimLayer);
12592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            for (int i = imw.mChildWindows.size() - 1; i >= 0; i--) {
12692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                final WindowState childWindow = imw.mChildWindows.get(i);
12792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                childWindow.mWinAnimator.mAnimLayer = childWindow.mLayer + adj;
12892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                if (DEBUG_LAYERS) Slog.v(TAG_WM, "IM win " + childWindow
12992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                        + " anim layer: " + childWindow.mWinAnimator.mAnimLayer);
13092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            }
13192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        }
1326db8e69afa832b345f060da92011ed77c5a76333Michael Wright        for (int i = mService.mInputMethodDialogs.size() - 1; i >= 0; i--) {
13392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            final WindowState dialog = mService.mInputMethodDialogs.get(i);
13492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            dialog.mWinAnimator.mAnimLayer = dialog.mLayer + adj;
13592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            if (DEBUG_LAYERS) Slog.v(TAG_WM, "IM win " + imw
13692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                    + " anim layer: " + dialog.mWinAnimator.mAnimLayer);
13792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        }
13892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    }
13992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
14092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    int getSpecialWindowAnimLayerAdjustment(WindowState win) {
14192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        if (win.mIsImWindow) {
14292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            return mInputMethodAnimLayerAdjustment;
14392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        } else if (win.mIsWallpaper) {
14492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            return mService.mWallpaperControllerLocked.getAnimLayerAdjustment();
14592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        }
14692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        return 0;
14792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    }
14892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
1495098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi    /**
1505098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi     * @return The layer used for dimming the apps when dismissing docked/fullscreen stack. Just
1515098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi     *         above all application surfaces.
1525098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi     */
1535098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi    int getResizeDimLayer() {
15420ec11b4e61e5fffd639034f936ed5ef8359d414Wale Ogunwale        return (mDockDivider != null) ? mDockDivider.mLayer - 1 : LAYER_OFFSET_DIM;
1555098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi    }
1565098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi
15792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    private void logDebugLayers(WindowList windows) {
15892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        for (int i = 0, n = windows.size(); i < n; i++) {
15992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            final WindowState w = windows.get(i);
16092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            final WindowStateAnimator winAnimator = w.mWinAnimator;
16192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            Slog.v(TAG_WM, "Assign layer " + w + ": " + "mBase=" + w.mBaseLayer
16292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                    + " mLayer=" + w.mLayer + (w.mAppToken == null
16392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                    ? "" : " mAppLayer=" + w.mAppToken.mAppAnimator.animLayerAdjustment)
16492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                    + " =mAnimLayer=" + winAnimator.mAnimLayer);
16592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        }
16692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    }
16792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
16892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    private void clear() {
16992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        mHighestApplicationLayer = 0;
17026b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        mPinnedWindows.clear();
171b976877a4b62a6030fe67796ab8a6a69b4cc041fRobert Carr        mInputMethodWindows.clear();
17226b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        mDockedWindows.clear();
17326b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        mReplacingWindows.clear();
17492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        mDockDivider = null;
17592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    }
17692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
17792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    private void collectSpecialWindows(WindowState w) {
178bedb566a280e8aee2e80346af08f42b3fb5121c2Filip Gruszczynski        if (w.mAttrs.type == TYPE_DOCK_DIVIDER) {
17992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            mDockDivider = w;
18026b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale            return;
18126b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        }
18226b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        if (w.mWillReplaceWindow) {
18326b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale            mReplacingWindows.add(w);
18426b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        }
185b976877a4b62a6030fe67796ab8a6a69b4cc041fRobert Carr        if (w.mIsImWindow) {
186b976877a4b62a6030fe67796ab8a6a69b4cc041fRobert Carr            mInputMethodWindows.add(w);
187b976877a4b62a6030fe67796ab8a6a69b4cc041fRobert Carr            return;
188b976877a4b62a6030fe67796ab8a6a69b4cc041fRobert Carr        }
18926b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        final TaskStack stack = w.getStack();
19026b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        if (stack == null) {
19126b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale            return;
19226b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        }
19326b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        if (stack.mStackId == PINNED_STACK_ID) {
19426b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale            mPinnedWindows.add(w);
19526b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        } else if (stack.mStackId == DOCKED_STACK_ID) {
19626b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale            mDockedWindows.add(w);
19792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        }
19892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    }
19992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
20092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    private void adjustSpecialWindows() {
2016412d755f4eb0b562c6d25ffb1ac5357105d6b1cRobert Carr        int layer = mHighestApplicationLayer + WINDOW_LAYER_MULTIPLIER;
20226b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        // For pinned and docked stack window, we want to make them above other windows also when
20326b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        // these windows are animating.
20426b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        while (!mDockedWindows.isEmpty()) {
20526b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale            layer = assignAndIncreaseLayerIfNeeded(mDockedWindows.remove(), layer);
20626b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        }
2075098159ae31bc59aa3857fecb1847f8d7bb73e54Jorim Jaggi
20892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        layer = assignAndIncreaseLayerIfNeeded(mDockDivider, layer);
2099da141fe5ca3c8272681f6d3ea3f6871a2bc76c2Robert Carr
210b976877a4b62a6030fe67796ab8a6a69b4cc041fRobert Carr        if (mDockDivider != null && mDockDivider.isVisibleLw()) {
211b976877a4b62a6030fe67796ab8a6a69b4cc041fRobert Carr            while (!mInputMethodWindows.isEmpty()) {
212ae712e5708aeb2579bcfa38794e840350a992d97Adrian Roos                final WindowState w = mInputMethodWindows.remove();
213ae712e5708aeb2579bcfa38794e840350a992d97Adrian Roos                // Only ever move IME windows up, else we brake IME for windows above the divider.
214ae712e5708aeb2579bcfa38794e840350a992d97Adrian Roos                if (layer > w.mLayer) {
215ae712e5708aeb2579bcfa38794e840350a992d97Adrian Roos                    layer = assignAndIncreaseLayerIfNeeded(w, layer);
216ae712e5708aeb2579bcfa38794e840350a992d97Adrian Roos                }
217d82b748b268dbd35e02d21eb7cc6be7b19484f5fRobert Carr            }
2189da141fe5ca3c8272681f6d3ea3f6871a2bc76c2Robert Carr        }
2199da141fe5ca3c8272681f6d3ea3f6871a2bc76c2Robert Carr
22026b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        // We know that we will be animating a relaunching window in the near future, which will
22126b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        // receive a z-order increase. We want the replaced window to immediately receive the same
22226b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        // treatment, e.g. to be above the dock divider.
22326b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        while (!mReplacingWindows.isEmpty()) {
22426b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale            layer = assignAndIncreaseLayerIfNeeded(mReplacingWindows.remove(), layer);
22526b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        }
22626b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale
22726b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        while (!mPinnedWindows.isEmpty()) {
22826b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale            layer = assignAndIncreaseLayerIfNeeded(mPinnedWindows.remove(), layer);
22926b7b43d172a7a2be39a109f1f6697162c4ee642Wale Ogunwale        }
23092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    }
23192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
23292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    private int assignAndIncreaseLayerIfNeeded(WindowState win, int layer) {
23390da5e54482fd513ab73bbcfce9f55312d0c96f7Robert Carr        if (win != null) {
23467eec038f4628f36e813329736802c331dc2dbc6Robert Carr            assignAnimLayer(win, layer);
2356412d755f4eb0b562c6d25ffb1ac5357105d6b1cRobert Carr            // Make sure we leave space inbetween normal windows for dims and such.
2366412d755f4eb0b562c6d25ffb1ac5357105d6b1cRobert Carr            layer += WINDOW_LAYER_MULTIPLIER;
23792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        }
23892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        return layer;
23992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    }
2406412d755f4eb0b562c6d25ffb1ac5357105d6b1cRobert Carr
24167eec038f4628f36e813329736802c331dc2dbc6Robert Carr    private void assignAnimLayer(WindowState w, int layer) {
24267eec038f4628f36e813329736802c331dc2dbc6Robert Carr        w.mLayer = layer;
24367eec038f4628f36e813329736802c331dc2dbc6Robert Carr        w.mWinAnimator.mAnimLayer = w.mLayer + w.getAnimLayerAdjustment() +
24467eec038f4628f36e813329736802c331dc2dbc6Robert Carr                    getSpecialWindowAnimLayerAdjustment(w);
245c69bd2246f4ec5000591fdc381f84cd90be85b7fJorim Jaggi        if (w.mAppToken != null && w.mAppToken.mAppAnimator.thumbnailForceAboveLayer > 0
246c69bd2246f4ec5000591fdc381f84cd90be85b7fJorim Jaggi                && w.mWinAnimator.mAnimLayer > w.mAppToken.mAppAnimator.thumbnailForceAboveLayer) {
247c69bd2246f4ec5000591fdc381f84cd90be85b7fJorim Jaggi            w.mAppToken.mAppAnimator.thumbnailForceAboveLayer = w.mWinAnimator.mAnimLayer;
248c69bd2246f4ec5000591fdc381f84cd90be85b7fJorim Jaggi        }
24967eec038f4628f36e813329736802c331dc2dbc6Robert Carr    }
25092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski
25192e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    void dump(PrintWriter pw, String s) {
25292e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        if (mInputMethodAnimLayerAdjustment != 0 ||
25392e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski                mService.mWallpaperControllerLocked.getAnimLayerAdjustment() != 0) {
25492e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            pw.print("  mInputMethodAnimLayerAdjustment=");
25592e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            pw.print(mInputMethodAnimLayerAdjustment);
25692e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            pw.print("  mWallpaperAnimLayerAdjustment=");
25792e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski            pw.println(mService.mWallpaperControllerLocked.getAnimLayerAdjustment());
25892e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski        }
25992e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski    }
26092e432c30e2304272c2f5b1b33366f32c3d763cfFilip Gruszczynski}
261