blob: abe51ac3a5d0fa6a430550a8232cb9762a2dcca1 [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;
Jorim Jaggiaa806142015-05-20 18:04:16 -0700125 if (state.keyguardShowing && state.keyguardNeedsInput && state.bouncerShowing
Adrian Roos1c0ca502015-10-07 12:20:42 -0700126 || BaseStatusBar.ENABLE_REMOTE_INPUT && state.remoteInputActive) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100127 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
128 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700129 } else if (state.isKeyguardShowingAndNotOccluded() || panelFocusable) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100130 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
131 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100132 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100133 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
134 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100135 }
136 }
137
138 private void applyHeight(State state) {
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200139 boolean expanded = isExpanded(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100140 if (expanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100141 mLpChanged.height = ViewGroup.LayoutParams.MATCH_PARENT;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100142 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100143 mLpChanged.height = mBarHeight;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100144 }
145 }
146
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200147 private boolean isExpanded(State state) {
148 return !state.forceCollapsed && (state.isKeyguardShowingAndNotOccluded()
149 || state.panelVisible || state.keyguardFadingAway || state.bouncerShowing
150 || state.headsUpShowing);
151 }
152
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200153 private void applyFitsSystemWindows(State state) {
154 mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
155 }
156
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100157 private void applyUserActivityTimeout(State state) {
Jorim Jaggiecbab362014-04-23 16:13:15 +0200158 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200159 && state.statusBarState == StatusBarState.KEYGUARD
160 && !state.qsExpanded) {
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100161 mLpChanged.userActivityTimeout = KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100162 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100163 mLpChanged.userActivityTimeout = -1;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100164 }
165 }
166
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200167 private void applyInputFeatures(State state) {
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200168 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200169 && state.statusBarState == StatusBarState.KEYGUARD
170 && !state.qsExpanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100171 mLpChanged.inputFeatures |=
172 WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200173 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100174 mLpChanged.inputFeatures &=
175 ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200176 }
177 }
178
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100179 private void apply(State state) {
180 applyKeyguardFlags(state);
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700181 applyForceStatusBarVisibleFlag(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100182 applyFocusableFlag(state);
183 adjustScreenOrientation(state);
184 applyHeight(state);
185 applyUserActivityTimeout(state);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200186 applyInputFeatures(state);
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200187 applyFitsSystemWindows(state);
Selim Cineka59ecc32015-04-07 10:51:49 -0700188 applyModalFlag(state);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700189 applyBrightness(state);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100190 if (mLp.copyFrom(mLpChanged) != 0) {
191 mWindowManager.updateViewLayout(mStatusBarView, mLp);
192 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100193 }
194
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700195 private void applyForceStatusBarVisibleFlag(State state) {
196 if (state.forceStatusBarVisible) {
197 mLpChanged.privateFlags |= WindowManager
198 .LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
199 } else {
200 mLpChanged.privateFlags &= ~WindowManager
201 .LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
202 }
203 }
204
Selim Cineka59ecc32015-04-07 10:51:49 -0700205 private void applyModalFlag(State state) {
206 if (state.headsUpShowing) {
207 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
208 } else {
209 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
210 }
211 }
212
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700213 private void applyBrightness(State state) {
214 if (state.forceDozeBrightness) {
215 mLpChanged.screenBrightness = mScreenBrightnessDoze;
216 } else {
217 mLpChanged.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
218 }
219 }
220
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100221 public void setKeyguardShowing(boolean showing) {
222 mCurrentState.keyguardShowing = showing;
223 apply(mCurrentState);
224 }
225
226 public void setKeyguardOccluded(boolean occluded) {
227 mCurrentState.keyguardOccluded = occluded;
228 apply(mCurrentState);
229 }
230
231 public void setKeyguardNeedsInput(boolean needsInput) {
232 mCurrentState.keyguardNeedsInput = needsInput;
233 apply(mCurrentState);
234 }
235
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700236 public void setPanelVisible(boolean visible) {
237 mCurrentState.panelVisible = visible;
238 mCurrentState.statusBarFocusable = visible;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100239 apply(mCurrentState);
240 }
241
242 public void setStatusBarFocusable(boolean focusable) {
243 mCurrentState.statusBarFocusable = focusable;
244 apply(mCurrentState);
245 }
246
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200247 public void setBouncerShowing(boolean showing) {
248 mCurrentState.bouncerShowing = showing;
249 apply(mCurrentState);
250 }
251
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200252 public void setKeyguardFadingAway(boolean keyguardFadingAway) {
253 mCurrentState.keyguardFadingAway = keyguardFadingAway;
254 apply(mCurrentState);
255 }
256
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200257 public void setQsExpanded(boolean expanded) {
258 mCurrentState.qsExpanded = expanded;
259 apply(mCurrentState);
260 }
261
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700262 public void setHeadsUpShowing(boolean showing) {
263 mCurrentState.headsUpShowing = showing;
264 apply(mCurrentState);
265 }
266
Jorim Jaggiecbab362014-04-23 16:13:15 +0200267 /**
268 * @param state The {@link StatusBarState} of the status bar.
269 */
270 public void setStatusBarState(int state) {
271 mCurrentState.statusBarState = state;
272 apply(mCurrentState);
273 }
274
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700275 public void setForceStatusBarVisible(boolean forceStatusBarVisible) {
276 mCurrentState.forceStatusBarVisible = forceStatusBarVisible;
277 apply(mCurrentState);
278 }
279
Selim Cinek737bff32015-05-08 16:08:35 -0700280 /**
281 * Force the window to be collapsed, even if it should theoretically be expanded.
282 * Used for when a heads-up comes in but we still need to wait for the touchable regions to
283 * be computed.
284 */
285 public void setForceWindowCollapsed(boolean force) {
286 mCurrentState.forceCollapsed = force;
287 apply(mCurrentState);
288 }
289
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700290 public void setPanelExpanded(boolean isExpanded) {
291 mCurrentState.panelExpanded = isExpanded;
292 apply(mCurrentState);
293 }
294
Adrian Roos1c0ca502015-10-07 12:20:42 -0700295 public void setRemoteInputActive(boolean remoteInputActive) {
296 mCurrentState.remoteInputActive = remoteInputActive;
297 apply(mCurrentState);
298 }
299
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700300 /**
301 * Set whether the screen brightness is forced to the value we use for doze mode by the status
302 * bar window.
303 */
304 public void setForceDozeBrightness(boolean forceDozeBrightness) {
305 mCurrentState.forceDozeBrightness = forceDozeBrightness;
306 apply(mCurrentState);
307 }
308
Selim Cinek7025f262015-07-13 16:22:48 -0700309 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
310 pw.println("StatusBarWindowManager state:");
311 pw.println(mCurrentState);
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200312 }
313
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100314 private static class State {
315 boolean keyguardShowing;
316 boolean keyguardOccluded;
317 boolean keyguardNeedsInput;
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700318 boolean panelVisible;
319 boolean panelExpanded;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100320 boolean statusBarFocusable;
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200321 boolean bouncerShowing;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200322 boolean keyguardFadingAway;
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200323 boolean qsExpanded;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700324 boolean headsUpShowing;
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700325 boolean forceStatusBarVisible;
Selim Cinek737bff32015-05-08 16:08:35 -0700326 boolean forceCollapsed;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700327 boolean forceDozeBrightness;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100328
Jorim Jaggiecbab362014-04-23 16:13:15 +0200329 /**
330 * The {@link BaseStatusBar} state from the status bar.
331 */
332 int statusBarState;
333
Adrian Roos1c0ca502015-10-07 12:20:42 -0700334 boolean remoteInputActive;
335
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100336 private boolean isKeyguardShowingAndNotOccluded() {
337 return keyguardShowing && !keyguardOccluded;
338 }
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200339
340 @Override
341 public String toString() {
342 StringBuilder result = new StringBuilder();
343 String newLine = "\n";
344 result.append("Window State {");
345 result.append(newLine);
346
347 Field[] fields = this.getClass().getDeclaredFields();
348
349 // Print field names paired with their values
350 for (Field field : fields) {
351 result.append(" ");
352 try {
353 result.append(field.getName());
354 result.append(": ");
355 //requires access to private field:
356 result.append(field.get(this));
357 } catch (IllegalAccessException ex) {
358 }
359 result.append(newLine);
360 }
361 result.append("}");
362
363 return result.toString();
364 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100365 }
366}