StatusBarWindowManager.java revision c580cbfe9c64044b796f81c6b1f22f8898109605
1/*
2 * Copyright (C) 2014 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.statusbar.phone;
18
19import android.content.Context;
20import android.content.pm.ActivityInfo;
21import android.content.res.Resources;
22import android.graphics.PixelFormat;
23import android.os.SystemProperties;
24import android.view.Gravity;
25import android.view.View;
26import android.view.ViewGroup;
27import android.view.WindowManager;
28
29import com.android.keyguard.R;
30import com.android.systemui.statusbar.BaseStatusBar;
31import com.android.systemui.statusbar.StatusBarState;
32
33/**
34 * Encapsulates all logic for the status bar window state management.
35 */
36public class StatusBarWindowManager {
37
38    private final Context mContext;
39    private final WindowManager mWindowManager;
40    private View mStatusBarView;
41    private WindowManager.LayoutParams mLp;
42    private int mBarHeight;
43    private final boolean mKeyguardScreenRotation;
44
45    private final State mCurrentState = new State();
46
47    public StatusBarWindowManager(Context context) {
48        mContext = context;
49        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
50        mKeyguardScreenRotation = shouldEnableKeyguardScreenRotation();
51    }
52
53    private boolean shouldEnableKeyguardScreenRotation() {
54        Resources res = mContext.getResources();
55        return SystemProperties.getBoolean("lockscreen.rot_override", false)
56                || res.getBoolean(R.bool.config_enableLockScreenRotation);
57    }
58
59    /**
60     * Adds the status bar view to the window manager.
61     *
62     * @param statusBarView The view to add.
63     * @param barHeight The height of the status bar in collapsed state.
64     */
65    public void add(View statusBarView, int barHeight) {
66
67        // Now that the status bar window encompasses the sliding panel and its
68        // translucent backdrop, the entire thing is made TRANSLUCENT and is
69        // hardware-accelerated.
70        mLp = new WindowManager.LayoutParams(
71                ViewGroup.LayoutParams.MATCH_PARENT,
72                barHeight,
73                WindowManager.LayoutParams.TYPE_STATUS_BAR,
74                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
75                        | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
76                        | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
77                        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
78                        | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
79                        | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
80                PixelFormat.TRANSLUCENT);
81
82        mLp.width = ViewGroup.LayoutParams.MATCH_PARENT;
83        mLp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
84        mLp.gravity = Gravity.TOP;
85        mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
86        mLp.setTitle("StatusBar");
87        mLp.packageName = mContext.getPackageName();
88        mStatusBarView = statusBarView;
89        mBarHeight = barHeight;
90        mWindowManager.addView(mStatusBarView, mLp);
91    }
92
93    private void applyKeyguardFlags(State state) {
94        if (state.keyguardShowing) {
95            mLp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
96            mLp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
97        } else {
98            mLp.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
99            mLp.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
100        }
101    }
102
103    private void adjustScreenOrientation(State state) {
104        if (state.isKeyguardShowingAndNotOccluded()) {
105            if (mKeyguardScreenRotation) {
106                mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
107            } else {
108                mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
109            }
110        } else {
111            mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
112        }
113    }
114
115    private void applyFocusableFlag(State state) {
116        if (state.isKeyguardShowingAndNotOccluded() && state.keyguardNeedsInput) {
117            mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
118            mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
119        } else if (state.isKeyguardShowingAndNotOccluded() || state.statusBarFocusable) {
120            mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
121            mLp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
122        } else {
123            mLp.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
124            mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
125        }
126    }
127
128    private void applyHeight(State state) {
129        boolean expanded = state.isKeyguardShowingAndNotOccluded() || state.statusBarExpanded;
130        if (expanded) {
131            mLp.height = ViewGroup.LayoutParams.MATCH_PARENT;
132        } else {
133            mLp.height = mBarHeight;
134        }
135    }
136
137    private void applyFitsSystemWindows(State state) {
138        mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
139    }
140
141    private void applyUserActivityTimeout(State state) {
142        if (state.isKeyguardShowingAndNotOccluded()
143                && state.statusBarState == StatusBarState.KEYGUARD) {
144            mLp.userActivityTimeout = state.keyguardUserActivityTimeout;
145        } else {
146            mLp.userActivityTimeout = -1;
147        }
148    }
149
150    private void applyInputFeatures(State state) {
151        if (state.isKeyguardShowingAndNotOccluded()
152                && state.statusBarState == StatusBarState.KEYGUARD) {
153            mLp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
154        } else {
155            mLp.inputFeatures &= ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
156        }
157    }
158
159    private void apply(State state) {
160        applyKeyguardFlags(state);
161        applyFocusableFlag(state);
162        adjustScreenOrientation(state);
163        applyHeight(state);
164        applyUserActivityTimeout(state);
165        applyInputFeatures(state);
166        applyFitsSystemWindows(state);
167        mWindowManager.updateViewLayout(mStatusBarView, mLp);
168    }
169
170    public void setKeyguardShowing(boolean showing) {
171        mCurrentState.keyguardShowing = showing;
172        apply(mCurrentState);
173    }
174
175    public void setKeyguardOccluded(boolean occluded) {
176        mCurrentState.keyguardOccluded = occluded;
177        apply(mCurrentState);
178    }
179
180    public void setKeyguardNeedsInput(boolean needsInput) {
181        mCurrentState.keyguardNeedsInput = needsInput;
182        apply(mCurrentState);
183    }
184
185    public void setStatusBarExpanded(boolean expanded) {
186        mCurrentState.statusBarExpanded = expanded;
187        mCurrentState.statusBarFocusable = expanded;
188        apply(mCurrentState);
189    }
190
191    public void setStatusBarFocusable(boolean focusable) {
192        mCurrentState.statusBarFocusable = focusable;
193        apply(mCurrentState);
194    }
195
196    public void setKeyguardUserActivityTimeout(long timeout) {
197        mCurrentState.keyguardUserActivityTimeout = timeout;
198        apply(mCurrentState);
199    }
200
201    /**
202     * @param state The {@link StatusBarState} of the status bar.
203     */
204    public void setStatusBarState(int state) {
205        mCurrentState.statusBarState = state;
206        apply(mCurrentState);
207    }
208
209    private static class State {
210        boolean keyguardShowing;
211        boolean keyguardOccluded;
212        boolean keyguardNeedsInput;
213        boolean statusBarExpanded;
214        boolean statusBarFocusable;
215        long keyguardUserActivityTimeout;
216
217        /**
218         * The {@link BaseStatusBar} state from the status bar.
219         */
220        int statusBarState;
221
222        private boolean isKeyguardShowingAndNotOccluded() {
223            return keyguardShowing && !keyguardOccluded;
224        }
225    }
226}
227