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