blob: ccfa0dd058ded20ae34aae427d0be1e866ba3781 [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;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -070027import android.view.Window;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010028import android.view.WindowManager;
29
30import com.android.keyguard.R;
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +010031import com.android.systemui.keyguard.KeyguardViewMediator;
Jorim Jaggiecbab362014-04-23 16:13:15 +020032import com.android.systemui.statusbar.BaseStatusBar;
33import com.android.systemui.statusbar.StatusBarState;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010034
Selim Cinek7025f262015-07-13 16:22:48 -070035import java.io.FileDescriptor;
36import java.io.PrintWriter;
Selim Cinek6a1bd2b2015-06-02 16:02:41 +020037import java.lang.reflect.Field;
38
Jorim Jaggi5cf17872014-03-26 18:31:48 +010039/**
40 * Encapsulates all logic for the status bar window state management.
41 */
42public class StatusBarWindowManager {
43
44 private final Context mContext;
45 private final WindowManager mWindowManager;
46 private View mStatusBarView;
47 private WindowManager.LayoutParams mLp;
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010048 private WindowManager.LayoutParams mLpChanged;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010049 private int mBarHeight;
50 private final boolean mKeyguardScreenRotation;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -070051 private final float mScreenBrightnessDoze;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010052 private final State mCurrentState = new State();
53
54 public StatusBarWindowManager(Context context) {
55 mContext = context;
56 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
57 mKeyguardScreenRotation = shouldEnableKeyguardScreenRotation();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -070058 mScreenBrightnessDoze = mContext.getResources().getInteger(
59 com.android.internal.R.integer.config_screenBrightnessDoze) / 255f;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010060 }
61
62 private boolean shouldEnableKeyguardScreenRotation() {
63 Resources res = mContext.getResources();
64 return SystemProperties.getBoolean("lockscreen.rot_override", false)
65 || res.getBoolean(R.bool.config_enableLockScreenRotation);
66 }
67
68 /**
69 * Adds the status bar view to the window manager.
70 *
71 * @param statusBarView The view to add.
72 * @param barHeight The height of the status bar in collapsed state.
73 */
74 public void add(View statusBarView, int barHeight) {
75
76 // Now that the status bar window encompasses the sliding panel and its
77 // translucent backdrop, the entire thing is made TRANSLUCENT and is
78 // hardware-accelerated.
79 mLp = new WindowManager.LayoutParams(
80 ViewGroup.LayoutParams.MATCH_PARENT,
81 barHeight,
82 WindowManager.LayoutParams.TYPE_STATUS_BAR,
83 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
84 | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
85 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
86 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
Adrian Roosea562512014-05-05 13:33:03 +020087 | WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
Jorim Jaggi5cf17872014-03-26 18:31:48 +010088 PixelFormat.TRANSLUCENT);
Jorim Jaggi5cf17872014-03-26 18:31:48 +010089 mLp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
Jorim Jaggi76aaef52014-05-08 19:16:49 +020090 mLp.gravity = Gravity.TOP;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010091 mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010092 mLp.setTitle("StatusBar");
93 mLp.packageName = mContext.getPackageName();
94 mStatusBarView = statusBarView;
95 mBarHeight = barHeight;
96 mWindowManager.addView(mStatusBarView, mLp);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010097 mLpChanged = new WindowManager.LayoutParams();
98 mLpChanged.copyFrom(mLp);
Jorim Jaggi5cf17872014-03-26 18:31:48 +010099 }
100
101 private void applyKeyguardFlags(State state) {
102 if (state.keyguardShowing) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100103 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
104 mLpChanged.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100105 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100106 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
107 mLpChanged.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100108 }
109 }
110
111 private void adjustScreenOrientation(State state) {
Jorim Jaggica36cf72014-04-17 20:36:15 +0200112 if (state.isKeyguardShowingAndNotOccluded()) {
113 if (mKeyguardScreenRotation) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100114 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
Jorim Jaggica36cf72014-04-17 20:36:15 +0200115 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100116 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
Jorim Jaggica36cf72014-04-17 20:36:15 +0200117 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100118 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100119 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100120 }
121 }
122
123 private void applyFocusableFlag(State state) {
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700124 boolean panelFocusable = state.statusBarFocusable && state.panelExpanded;
Adrian Rooseb91ca52015-06-10 15:49:11 -0700125 if (state.keyguardShowing && state.keyguardNeedsInput && state.bouncerShowing) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100126 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
127 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700128 } else if (state.isKeyguardShowingAndNotOccluded() || panelFocusable) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100129 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
130 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100131 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100132 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
133 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100134 }
135 }
136
137 private void applyHeight(State state) {
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200138 boolean expanded = isExpanded(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100139 if (expanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100140 mLpChanged.height = ViewGroup.LayoutParams.MATCH_PARENT;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100141 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100142 mLpChanged.height = mBarHeight;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100143 }
144 }
145
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200146 private boolean isExpanded(State state) {
147 return !state.forceCollapsed && (state.isKeyguardShowingAndNotOccluded()
148 || state.panelVisible || state.keyguardFadingAway || state.bouncerShowing
149 || state.headsUpShowing);
150 }
151
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200152 private void applyFitsSystemWindows(State state) {
153 mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
154 }
155
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100156 private void applyUserActivityTimeout(State state) {
Jorim Jaggiecbab362014-04-23 16:13:15 +0200157 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200158 && state.statusBarState == StatusBarState.KEYGUARD
159 && !state.qsExpanded) {
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100160 mLpChanged.userActivityTimeout = KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100161 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100162 mLpChanged.userActivityTimeout = -1;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100163 }
164 }
165
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200166 private void applyInputFeatures(State state) {
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200167 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200168 && state.statusBarState == StatusBarState.KEYGUARD
169 && !state.qsExpanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100170 mLpChanged.inputFeatures |=
171 WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200172 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100173 mLpChanged.inputFeatures &=
174 ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200175 }
176 }
177
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100178 private void apply(State state) {
179 applyKeyguardFlags(state);
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700180 applyForceStatusBarVisibleFlag(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100181 applyFocusableFlag(state);
182 adjustScreenOrientation(state);
183 applyHeight(state);
184 applyUserActivityTimeout(state);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200185 applyInputFeatures(state);
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200186 applyFitsSystemWindows(state);
Selim Cineka59ecc32015-04-07 10:51:49 -0700187 applyModalFlag(state);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700188 applyBrightness(state);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100189 if (mLp.copyFrom(mLpChanged) != 0) {
190 mWindowManager.updateViewLayout(mStatusBarView, mLp);
191 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100192 }
193
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700194 private void applyForceStatusBarVisibleFlag(State state) {
195 if (state.forceStatusBarVisible) {
196 mLpChanged.privateFlags |= WindowManager
197 .LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
198 } else {
199 mLpChanged.privateFlags &= ~WindowManager
200 .LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
201 }
202 }
203
Selim Cineka59ecc32015-04-07 10:51:49 -0700204 private void applyModalFlag(State state) {
205 if (state.headsUpShowing) {
206 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
207 } else {
208 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
209 }
210 }
211
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700212 private void applyBrightness(State state) {
213 if (state.forceDozeBrightness) {
214 mLpChanged.screenBrightness = mScreenBrightnessDoze;
215 } else {
216 mLpChanged.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
217 }
218 }
219
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100220 public void setKeyguardShowing(boolean showing) {
221 mCurrentState.keyguardShowing = showing;
222 apply(mCurrentState);
223 }
224
225 public void setKeyguardOccluded(boolean occluded) {
226 mCurrentState.keyguardOccluded = occluded;
227 apply(mCurrentState);
228 }
229
230 public void setKeyguardNeedsInput(boolean needsInput) {
231 mCurrentState.keyguardNeedsInput = needsInput;
232 apply(mCurrentState);
233 }
234
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700235 public void setPanelVisible(boolean visible) {
236 mCurrentState.panelVisible = visible;
237 mCurrentState.statusBarFocusable = visible;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100238 apply(mCurrentState);
239 }
240
241 public void setStatusBarFocusable(boolean focusable) {
242 mCurrentState.statusBarFocusable = focusable;
243 apply(mCurrentState);
244 }
245
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200246 public void setBouncerShowing(boolean showing) {
247 mCurrentState.bouncerShowing = showing;
248 apply(mCurrentState);
249 }
250
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200251 public void setKeyguardFadingAway(boolean keyguardFadingAway) {
252 mCurrentState.keyguardFadingAway = keyguardFadingAway;
253 apply(mCurrentState);
254 }
255
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200256 public void setQsExpanded(boolean expanded) {
257 mCurrentState.qsExpanded = expanded;
258 apply(mCurrentState);
259 }
260
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700261 public void setHeadsUpShowing(boolean showing) {
262 mCurrentState.headsUpShowing = showing;
263 apply(mCurrentState);
264 }
265
Jorim Jaggiecbab362014-04-23 16:13:15 +0200266 /**
267 * @param state The {@link StatusBarState} of the status bar.
268 */
269 public void setStatusBarState(int state) {
270 mCurrentState.statusBarState = state;
271 apply(mCurrentState);
272 }
273
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700274 public void setForceStatusBarVisible(boolean forceStatusBarVisible) {
275 mCurrentState.forceStatusBarVisible = forceStatusBarVisible;
276 apply(mCurrentState);
277 }
278
Selim Cinek737bff32015-05-08 16:08:35 -0700279 /**
280 * Force the window to be collapsed, even if it should theoretically be expanded.
281 * Used for when a heads-up comes in but we still need to wait for the touchable regions to
282 * be computed.
283 */
284 public void setForceWindowCollapsed(boolean force) {
285 mCurrentState.forceCollapsed = force;
286 apply(mCurrentState);
287 }
288
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700289 public void setPanelExpanded(boolean isExpanded) {
290 mCurrentState.panelExpanded = isExpanded;
291 apply(mCurrentState);
292 }
293
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700294 /**
295 * Set whether the screen brightness is forced to the value we use for doze mode by the status
296 * bar window.
297 */
298 public void setForceDozeBrightness(boolean forceDozeBrightness) {
299 mCurrentState.forceDozeBrightness = forceDozeBrightness;
300 apply(mCurrentState);
301 }
302
Selim Cinek7025f262015-07-13 16:22:48 -0700303 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
304 pw.println("StatusBarWindowManager state:");
305 pw.println(mCurrentState);
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200306 }
307
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100308 private static class State {
309 boolean keyguardShowing;
310 boolean keyguardOccluded;
311 boolean keyguardNeedsInput;
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700312 boolean panelVisible;
313 boolean panelExpanded;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100314 boolean statusBarFocusable;
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200315 boolean bouncerShowing;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200316 boolean keyguardFadingAway;
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200317 boolean qsExpanded;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700318 boolean headsUpShowing;
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700319 boolean forceStatusBarVisible;
Selim Cinek737bff32015-05-08 16:08:35 -0700320 boolean forceCollapsed;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700321 boolean forceDozeBrightness;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100322
Jorim Jaggiecbab362014-04-23 16:13:15 +0200323 /**
324 * The {@link BaseStatusBar} state from the status bar.
325 */
326 int statusBarState;
327
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100328 private boolean isKeyguardShowingAndNotOccluded() {
329 return keyguardShowing && !keyguardOccluded;
330 }
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200331
332 @Override
333 public String toString() {
334 StringBuilder result = new StringBuilder();
335 String newLine = "\n";
336 result.append("Window State {");
337 result.append(newLine);
338
339 Field[] fields = this.getClass().getDeclaredFields();
340
341 // Print field names paired with their values
342 for (Field field : fields) {
343 result.append(" ");
344 try {
345 result.append(field.getName());
346 result.append(": ");
347 //requires access to private field:
348 result.append(field.get(this));
349 } catch (IllegalAccessException ex) {
350 }
351 result.append(newLine);
352 }
353 result.append("}");
354
355 return result.toString();
356 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100357 }
358}