StatusBarWindowManager.java revision ea56251d92050e9a672d1f66d0d4621e4dd4136e
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_DRAWS_SYSTEM_BAR_BACKGROUNDS,
79                PixelFormat.TRANSLUCENT);
80        mLp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
81        mLp.gravity = Gravity.TOP;
82        mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
83        mLp.setTitle("StatusBar");
84        mLp.packageName = mContext.getPackageName();
85        mStatusBarView = statusBarView;
86        mBarHeight = barHeight;
87        mWindowManager.addView(mStatusBarView, mLp);
88    }
89
90    private void applyKeyguardFlags(State state) {
91        if (state.keyguardShowing) {
92            mLp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
93            mLp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
94        } else {
95            mLp.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
96            mLp.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
97        }
98    }
99
100    private void adjustScreenOrientation(State state) {
101        if (state.isKeyguardShowingAndNotOccluded()) {
102            if (mKeyguardScreenRotation) {
103                mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
104            } else {
105                mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
106            }
107        } else {
108            mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
109        }
110    }
111
112    private void applyFocusableFlag(State state) {
113        if (state.isKeyguardShowingAndNotOccluded() && state.keyguardNeedsInput
114                && state.bouncerShowing) {
115            mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
116            mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
117        } else if (state.isKeyguardShowingAndNotOccluded() || state.statusBarFocusable) {
118            mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
119            mLp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
120        } else {
121            mLp.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
122            mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
123        }
124    }
125
126    private void applyHeight(State state) {
127        boolean expanded = state.isKeyguardShowingAndNotOccluded() || state.statusBarExpanded;
128        if (expanded) {
129            mLp.height = ViewGroup.LayoutParams.MATCH_PARENT;
130        } else {
131            mLp.height = mBarHeight;
132        }
133    }
134
135    private void applyFitsSystemWindows(State state) {
136        mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
137    }
138
139    private void applyUserActivityTimeout(State state) {
140        if (state.isKeyguardShowingAndNotOccluded()
141                && state.statusBarState == StatusBarState.KEYGUARD) {
142            mLp.userActivityTimeout = state.keyguardUserActivityTimeout;
143        } else {
144            mLp.userActivityTimeout = -1;
145        }
146    }
147
148    private void applyInputFeatures(State state) {
149        if (state.isKeyguardShowingAndNotOccluded()
150                && state.statusBarState == StatusBarState.KEYGUARD) {
151            mLp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
152        } else {
153            mLp.inputFeatures &= ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
154        }
155    }
156
157    private void apply(State state) {
158        applyKeyguardFlags(state);
159        applyFocusableFlag(state);
160        adjustScreenOrientation(state);
161        applyHeight(state);
162        applyUserActivityTimeout(state);
163        applyInputFeatures(state);
164        applyFitsSystemWindows(state);
165        mWindowManager.updateViewLayout(mStatusBarView, mLp);
166    }
167
168    public void setKeyguardShowing(boolean showing) {
169        mCurrentState.keyguardShowing = showing;
170        apply(mCurrentState);
171    }
172
173    public void setKeyguardOccluded(boolean occluded) {
174        mCurrentState.keyguardOccluded = occluded;
175        apply(mCurrentState);
176    }
177
178    public void setKeyguardNeedsInput(boolean needsInput) {
179        mCurrentState.keyguardNeedsInput = needsInput;
180        apply(mCurrentState);
181    }
182
183    public void setStatusBarExpanded(boolean expanded) {
184        mCurrentState.statusBarExpanded = expanded;
185        mCurrentState.statusBarFocusable = expanded;
186        apply(mCurrentState);
187    }
188
189    public void setStatusBarFocusable(boolean focusable) {
190        mCurrentState.statusBarFocusable = focusable;
191        apply(mCurrentState);
192    }
193
194    public void setKeyguardUserActivityTimeout(long timeout) {
195        mCurrentState.keyguardUserActivityTimeout = timeout;
196        apply(mCurrentState);
197    }
198
199    public void setBouncerShowing(boolean showing) {
200        mCurrentState.bouncerShowing = showing;
201        apply(mCurrentState);
202    }
203
204    /**
205     * @param state The {@link StatusBarState} of the status bar.
206     */
207    public void setStatusBarState(int state) {
208        mCurrentState.statusBarState = state;
209        apply(mCurrentState);
210    }
211
212    private static class State {
213        boolean keyguardShowing;
214        boolean keyguardOccluded;
215        boolean keyguardNeedsInput;
216        boolean statusBarExpanded;
217        boolean statusBarFocusable;
218        long keyguardUserActivityTimeout;
219        boolean bouncerShowing;
220
221        /**
222         * The {@link BaseStatusBar} state from the status bar.
223         */
224        int statusBarState;
225
226        private boolean isKeyguardShowingAndNotOccluded() {
227            return keyguardShowing && !keyguardOccluded;
228        }
229    }
230}
231