blob: fe57cefbcb29777865f2b14306b0e962d60651eb [file] [log] [blame]
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001/*
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
Jorim Jaggi5cf17872014-03-26 18:31:48 +010019import 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;
Jorim Jaggiecbab362014-04-23 16:13:15 +020030import com.android.systemui.statusbar.BaseStatusBar;
31import com.android.systemui.statusbar.StatusBarState;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010032
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
Adrian Roosea562512014-05-05 13:33:03 +020078 | WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
Jorim Jaggi5cf17872014-03-26 18:31:48 +010079 PixelFormat.TRANSLUCENT);
Jorim Jaggi5cf17872014-03-26 18:31:48 +010080 mLp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
Jorim Jaggi76aaef52014-05-08 19:16:49 +020081 mLp.gravity = Gravity.TOP;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010082 mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010083 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) {
Jorim Jaggica36cf72014-04-17 20:36:15 +0200101 if (state.isKeyguardShowingAndNotOccluded()) {
102 if (mKeyguardScreenRotation) {
103 mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
104 } else {
105 mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
106 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100107 } else {
Jorim Jaggica36cf72014-04-17 20:36:15 +0200108 mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100109 }
110 }
111
112 private void applyFocusableFlag(State state) {
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200113 if (state.isKeyguardShowingAndNotOccluded() && state.keyguardNeedsInput
114 && state.bouncerShowing) {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100115 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) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200127 boolean expanded = state.isKeyguardShowingAndNotOccluded() || state.statusBarExpanded
128 || state.keyguardFadingAway;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100129 if (expanded) {
130 mLp.height = ViewGroup.LayoutParams.MATCH_PARENT;
131 } else {
132 mLp.height = mBarHeight;
133 }
134 }
135
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200136 private void applyFitsSystemWindows(State state) {
137 mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
138 }
139
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100140 private void applyUserActivityTimeout(State state) {
Jorim Jaggiecbab362014-04-23 16:13:15 +0200141 if (state.isKeyguardShowingAndNotOccluded()
142 && state.statusBarState == StatusBarState.KEYGUARD) {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100143 mLp.userActivityTimeout = state.keyguardUserActivityTimeout;
144 } else {
145 mLp.userActivityTimeout = -1;
146 }
147 }
148
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200149 private void applyInputFeatures(State state) {
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200150 if (state.isKeyguardShowingAndNotOccluded()
151 && state.statusBarState == StatusBarState.KEYGUARD) {
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200152 mLp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
153 } else {
154 mLp.inputFeatures &= ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
155 }
156 }
157
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100158 private void apply(State state) {
159 applyKeyguardFlags(state);
160 applyFocusableFlag(state);
161 adjustScreenOrientation(state);
162 applyHeight(state);
163 applyUserActivityTimeout(state);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200164 applyInputFeatures(state);
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200165 applyFitsSystemWindows(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100166 mWindowManager.updateViewLayout(mStatusBarView, mLp);
167 }
168
169 public void setKeyguardShowing(boolean showing) {
170 mCurrentState.keyguardShowing = showing;
171 apply(mCurrentState);
172 }
173
174 public void setKeyguardOccluded(boolean occluded) {
175 mCurrentState.keyguardOccluded = occluded;
176 apply(mCurrentState);
177 }
178
179 public void setKeyguardNeedsInput(boolean needsInput) {
180 mCurrentState.keyguardNeedsInput = needsInput;
181 apply(mCurrentState);
182 }
183
184 public void setStatusBarExpanded(boolean expanded) {
185 mCurrentState.statusBarExpanded = expanded;
186 mCurrentState.statusBarFocusable = expanded;
187 apply(mCurrentState);
188 }
189
190 public void setStatusBarFocusable(boolean focusable) {
191 mCurrentState.statusBarFocusable = focusable;
192 apply(mCurrentState);
193 }
194
195 public void setKeyguardUserActivityTimeout(long timeout) {
196 mCurrentState.keyguardUserActivityTimeout = timeout;
197 apply(mCurrentState);
198 }
199
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200200 public void setBouncerShowing(boolean showing) {
201 mCurrentState.bouncerShowing = showing;
202 apply(mCurrentState);
203 }
204
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200205 public void setKeyguardFadingAway(boolean keyguardFadingAway) {
206 mCurrentState.keyguardFadingAway = keyguardFadingAway;
207 apply(mCurrentState);
208 }
209
Jorim Jaggiecbab362014-04-23 16:13:15 +0200210 /**
211 * @param state The {@link StatusBarState} of the status bar.
212 */
213 public void setStatusBarState(int state) {
214 mCurrentState.statusBarState = state;
215 apply(mCurrentState);
216 }
217
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100218 private static class State {
219 boolean keyguardShowing;
220 boolean keyguardOccluded;
221 boolean keyguardNeedsInput;
222 boolean statusBarExpanded;
223 boolean statusBarFocusable;
224 long keyguardUserActivityTimeout;
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200225 boolean bouncerShowing;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200226 boolean keyguardFadingAway;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100227
Jorim Jaggiecbab362014-04-23 16:13:15 +0200228 /**
229 * The {@link BaseStatusBar} state from the status bar.
230 */
231 int statusBarState;
232
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100233 private boolean isKeyguardShowingAndNotOccluded() {
234 return keyguardShowing && !keyguardOccluded;
235 }
236 }
237}