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 */
16
17package com.android.systemui.stackdivider;
18
19import android.content.Context;
20import android.graphics.PixelFormat;
21import android.os.Binder;
22import android.view.View;
23import android.view.WindowManager;
24
25import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
26import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
27import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
28import static android.view.WindowManager.LayoutParams.FLAG_SLIPPERY;
29import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH;
30import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
31import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
32import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
33import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
34
35/**
36 * Manages the window parameters of the docked stack divider.
37 */
38public class DividerWindowManager {
39
40    private static final String WINDOW_TITLE = "DockedStackDivider";
41
42    private final WindowManager mWindowManager;
43    private WindowManager.LayoutParams mLp;
44    private View mView;
45
46    public DividerWindowManager(Context ctx) {
47        mWindowManager = ctx.getSystemService(WindowManager.class);
48    }
49
50    public void add(View view, int width, int height) {
51        mLp = new WindowManager.LayoutParams(
52                width, height, TYPE_DOCK_DIVIDER,
53                FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL
54                        | FLAG_WATCH_OUTSIDE_TOUCH | FLAG_SPLIT_TOUCH | FLAG_SLIPPERY,
55                PixelFormat.TRANSLUCENT);
56        mLp.token = new Binder();
57        mLp.setTitle(WINDOW_TITLE);
58        mLp.privateFlags |= PRIVATE_FLAG_NO_MOVE_ANIMATION;
59        mLp.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
60        view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
61                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
62                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
63        mWindowManager.addView(view, mLp);
64        mView = view;
65    }
66
67    public void remove() {
68        if (mView != null) {
69            mWindowManager.removeView(mView);
70        }
71        mView = null;
72    }
73
74    public void setSlippery(boolean slippery) {
75        boolean changed = false;
76        if (slippery && (mLp.flags & FLAG_SLIPPERY) == 0) {
77            mLp.flags |= FLAG_SLIPPERY;
78            changed = true;
79        } else if (!slippery && (mLp.flags & FLAG_SLIPPERY) != 0) {
80            mLp.flags &= ~FLAG_SLIPPERY;
81            changed = true;
82        }
83        if (changed) {
84            mWindowManager.updateViewLayout(mView, mLp);
85        }
86    }
87
88    public void setTouchable(boolean touchable) {
89        boolean changed = false;
90        if (!touchable && (mLp.flags & FLAG_NOT_TOUCHABLE) == 0) {
91            mLp.flags |= FLAG_NOT_TOUCHABLE;
92            changed = true;
93        } else if (touchable && (mLp.flags & FLAG_NOT_TOUCHABLE) != 0) {
94            mLp.flags &= ~FLAG_NOT_TOUCHABLE;
95            changed = true;
96        }
97        if (changed) {
98            mWindowManager.updateViewLayout(mView, mLp);
99        }
100    }
101}
102