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