blob: 888e19c50f38fb944f3b516f6e59b76918fd0a5d [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 Jaggi6b88cdf2014-12-22 20:56:50 +010030import com.android.systemui.keyguard.KeyguardViewMediator;
Jorim Jaggiecbab362014-04-23 16:13:15 +020031import com.android.systemui.statusbar.BaseStatusBar;
Adrian Roosd28ccd72016-01-06 15:23:14 +010032import com.android.systemui.statusbar.RemoteInputController;
Jorim Jaggiecbab362014-04-23 16:13:15 +020033import 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 */
Adrian Roosd28ccd72016-01-06 15:23:14 +010042public class StatusBarWindowManager implements RemoteInputController.Callback {
Jorim Jaggi5cf17872014-03-26 18:31:48 +010043
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.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100104 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100105 mLpChanged.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100106 }
Adrian Roosd5c2db62016-03-08 16:11:31 -0800107
108 if (state.keyguardShowing && !state.backdropShowing) {
109 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
110 } else {
111 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
112 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100113 }
114
115 private void adjustScreenOrientation(State state) {
Jorim Jaggica36cf72014-04-17 20:36:15 +0200116 if (state.isKeyguardShowingAndNotOccluded()) {
117 if (mKeyguardScreenRotation) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100118 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
Jorim Jaggica36cf72014-04-17 20:36:15 +0200119 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100120 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
Jorim Jaggica36cf72014-04-17 20:36:15 +0200121 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100122 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100123 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100124 }
125 }
126
127 private void applyFocusableFlag(State state) {
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700128 boolean panelFocusable = state.statusBarFocusable && state.panelExpanded;
Jorim Jaggiaa806142015-05-20 18:04:16 -0700129 if (state.keyguardShowing && state.keyguardNeedsInput && state.bouncerShowing
Adrian Roos1c0ca502015-10-07 12:20:42 -0700130 || BaseStatusBar.ENABLE_REMOTE_INPUT && state.remoteInputActive) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100131 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
132 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700133 } else if (state.isKeyguardShowingAndNotOccluded() || panelFocusable) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100134 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
135 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100136 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100137 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
138 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100139 }
Adrian Roosdc5b4532016-01-06 20:49:41 +0100140
Adrian Roos5153d4a2016-03-22 10:01:56 -0700141 mLpChanged.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100142 }
143
144 private void applyHeight(State state) {
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200145 boolean expanded = isExpanded(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100146 if (expanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100147 mLpChanged.height = ViewGroup.LayoutParams.MATCH_PARENT;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100148 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100149 mLpChanged.height = mBarHeight;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100150 }
151 }
152
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200153 private boolean isExpanded(State state) {
154 return !state.forceCollapsed && (state.isKeyguardShowingAndNotOccluded()
155 || state.panelVisible || state.keyguardFadingAway || state.bouncerShowing
156 || state.headsUpShowing);
157 }
158
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200159 private void applyFitsSystemWindows(State state) {
160 mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
161 }
162
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100163 private void applyUserActivityTimeout(State state) {
Jorim Jaggiecbab362014-04-23 16:13:15 +0200164 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200165 && state.statusBarState == StatusBarState.KEYGUARD
166 && !state.qsExpanded) {
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100167 mLpChanged.userActivityTimeout = KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100168 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100169 mLpChanged.userActivityTimeout = -1;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100170 }
171 }
172
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200173 private void applyInputFeatures(State state) {
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200174 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200175 && state.statusBarState == StatusBarState.KEYGUARD
Vadim Tryshev6069c402016-03-09 14:24:29 -0800176 && !state.qsExpanded && !state.forceUserActivity) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100177 mLpChanged.inputFeatures |=
178 WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200179 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100180 mLpChanged.inputFeatures &=
181 ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200182 }
183 }
184
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100185 private void apply(State state) {
186 applyKeyguardFlags(state);
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700187 applyForceStatusBarVisibleFlag(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100188 applyFocusableFlag(state);
189 adjustScreenOrientation(state);
190 applyHeight(state);
191 applyUserActivityTimeout(state);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200192 applyInputFeatures(state);
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200193 applyFitsSystemWindows(state);
Selim Cineka59ecc32015-04-07 10:51:49 -0700194 applyModalFlag(state);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700195 applyBrightness(state);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100196 if (mLp.copyFrom(mLpChanged) != 0) {
197 mWindowManager.updateViewLayout(mStatusBarView, mLp);
198 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100199 }
200
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700201 private void applyForceStatusBarVisibleFlag(State state) {
202 if (state.forceStatusBarVisible) {
203 mLpChanged.privateFlags |= WindowManager
204 .LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
205 } else {
206 mLpChanged.privateFlags &= ~WindowManager
207 .LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
208 }
209 }
210
Selim Cineka59ecc32015-04-07 10:51:49 -0700211 private void applyModalFlag(State state) {
212 if (state.headsUpShowing) {
213 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
214 } else {
215 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
216 }
217 }
218
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700219 private void applyBrightness(State state) {
220 if (state.forceDozeBrightness) {
221 mLpChanged.screenBrightness = mScreenBrightnessDoze;
222 } else {
223 mLpChanged.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
224 }
225 }
226
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100227 public void setKeyguardShowing(boolean showing) {
228 mCurrentState.keyguardShowing = showing;
229 apply(mCurrentState);
230 }
231
232 public void setKeyguardOccluded(boolean occluded) {
233 mCurrentState.keyguardOccluded = occluded;
234 apply(mCurrentState);
235 }
236
237 public void setKeyguardNeedsInput(boolean needsInput) {
238 mCurrentState.keyguardNeedsInput = needsInput;
239 apply(mCurrentState);
240 }
241
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700242 public void setPanelVisible(boolean visible) {
243 mCurrentState.panelVisible = visible;
244 mCurrentState.statusBarFocusable = visible;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100245 apply(mCurrentState);
246 }
247
248 public void setStatusBarFocusable(boolean focusable) {
249 mCurrentState.statusBarFocusable = focusable;
250 apply(mCurrentState);
251 }
252
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200253 public void setBouncerShowing(boolean showing) {
254 mCurrentState.bouncerShowing = showing;
255 apply(mCurrentState);
256 }
257
Adrian Roosd5c2db62016-03-08 16:11:31 -0800258 public void setBackdropShowing(boolean showing) {
259 mCurrentState.backdropShowing = showing;
260 apply(mCurrentState);
261 }
262
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200263 public void setKeyguardFadingAway(boolean keyguardFadingAway) {
264 mCurrentState.keyguardFadingAway = keyguardFadingAway;
265 apply(mCurrentState);
266 }
267
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200268 public void setQsExpanded(boolean expanded) {
269 mCurrentState.qsExpanded = expanded;
270 apply(mCurrentState);
271 }
272
Vadim Tryshev6069c402016-03-09 14:24:29 -0800273 public void setForceUserActivity(boolean forceUserActivity) {
274 mCurrentState.forceUserActivity = forceUserActivity;
275 apply(mCurrentState);
276 }
277
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700278 public void setHeadsUpShowing(boolean showing) {
279 mCurrentState.headsUpShowing = showing;
280 apply(mCurrentState);
281 }
282
Jorim Jaggiecbab362014-04-23 16:13:15 +0200283 /**
284 * @param state The {@link StatusBarState} of the status bar.
285 */
286 public void setStatusBarState(int state) {
287 mCurrentState.statusBarState = state;
288 apply(mCurrentState);
289 }
290
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700291 public void setForceStatusBarVisible(boolean forceStatusBarVisible) {
292 mCurrentState.forceStatusBarVisible = forceStatusBarVisible;
293 apply(mCurrentState);
294 }
295
Selim Cinek737bff32015-05-08 16:08:35 -0700296 /**
297 * Force the window to be collapsed, even if it should theoretically be expanded.
298 * Used for when a heads-up comes in but we still need to wait for the touchable regions to
299 * be computed.
300 */
301 public void setForceWindowCollapsed(boolean force) {
302 mCurrentState.forceCollapsed = force;
303 apply(mCurrentState);
304 }
305
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700306 public void setPanelExpanded(boolean isExpanded) {
307 mCurrentState.panelExpanded = isExpanded;
308 apply(mCurrentState);
309 }
310
Adrian Roosd28ccd72016-01-06 15:23:14 +0100311 @Override
312 public void onRemoteInputActive(boolean remoteInputActive) {
Adrian Roos1c0ca502015-10-07 12:20:42 -0700313 mCurrentState.remoteInputActive = remoteInputActive;
314 apply(mCurrentState);
315 }
316
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700317 /**
318 * Set whether the screen brightness is forced to the value we use for doze mode by the status
319 * bar window.
320 */
321 public void setForceDozeBrightness(boolean forceDozeBrightness) {
322 mCurrentState.forceDozeBrightness = forceDozeBrightness;
323 apply(mCurrentState);
324 }
325
Jorim Jaggi11c62e12016-04-05 20:41:21 -0700326 public void setBarHeight(int barHeight) {
327 mBarHeight = barHeight;
328 apply(mCurrentState);
329 }
330
Selim Cinek7025f262015-07-13 16:22:48 -0700331 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
332 pw.println("StatusBarWindowManager state:");
333 pw.println(mCurrentState);
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200334 }
335
Adrian Roosd5c2db62016-03-08 16:11:31 -0800336 public boolean isShowingWallpaper() {
337 return !mCurrentState.backdropShowing;
338 }
339
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100340 private static class State {
341 boolean keyguardShowing;
342 boolean keyguardOccluded;
343 boolean keyguardNeedsInput;
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700344 boolean panelVisible;
345 boolean panelExpanded;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100346 boolean statusBarFocusable;
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200347 boolean bouncerShowing;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200348 boolean keyguardFadingAway;
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200349 boolean qsExpanded;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700350 boolean headsUpShowing;
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700351 boolean forceStatusBarVisible;
Selim Cinek737bff32015-05-08 16:08:35 -0700352 boolean forceCollapsed;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700353 boolean forceDozeBrightness;
Vadim Tryshev6069c402016-03-09 14:24:29 -0800354 boolean forceUserActivity;
Adrian Roosd5c2db62016-03-08 16:11:31 -0800355 boolean backdropShowing;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100356
Jorim Jaggiecbab362014-04-23 16:13:15 +0200357 /**
358 * The {@link BaseStatusBar} state from the status bar.
359 */
360 int statusBarState;
361
Adrian Roos1c0ca502015-10-07 12:20:42 -0700362 boolean remoteInputActive;
363
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100364 private boolean isKeyguardShowingAndNotOccluded() {
365 return keyguardShowing && !keyguardOccluded;
366 }
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200367
368 @Override
369 public String toString() {
370 StringBuilder result = new StringBuilder();
371 String newLine = "\n";
372 result.append("Window State {");
373 result.append(newLine);
374
375 Field[] fields = this.getClass().getDeclaredFields();
376
377 // Print field names paired with their values
378 for (Field field : fields) {
379 result.append(" ");
380 try {
381 result.append(field.getName());
382 result.append(": ");
383 //requires access to private field:
384 result.append(field.get(this));
385 } catch (IllegalAccessException ex) {
386 }
387 result.append(newLine);
388 }
389 result.append("}");
390
391 return result.toString();
392 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100393 }
394}