blob: 9d2f0de4c1e08a506fda655a9bad1615ab328f9a [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;
Adrian Roosd28ccd72016-01-06 15:23:14 +010033import com.android.systemui.statusbar.RemoteInputController;
Jorim Jaggiecbab362014-04-23 16:13:15 +020034import com.android.systemui.statusbar.StatusBarState;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010035
Selim Cinek7025f262015-07-13 16:22:48 -070036import java.io.FileDescriptor;
37import java.io.PrintWriter;
Selim Cinek6a1bd2b2015-06-02 16:02:41 +020038import java.lang.reflect.Field;
39
Jorim Jaggi5cf17872014-03-26 18:31:48 +010040/**
41 * Encapsulates all logic for the status bar window state management.
42 */
Adrian Roosd28ccd72016-01-06 15:23:14 +010043public class StatusBarWindowManager implements RemoteInputController.Callback {
Jorim Jaggi5cf17872014-03-26 18:31:48 +010044
45 private final Context mContext;
46 private final WindowManager mWindowManager;
47 private View mStatusBarView;
48 private WindowManager.LayoutParams mLp;
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010049 private WindowManager.LayoutParams mLpChanged;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010050 private int mBarHeight;
51 private final boolean mKeyguardScreenRotation;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -070052 private final float mScreenBrightnessDoze;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010053 private final State mCurrentState = new State();
54
55 public StatusBarWindowManager(Context context) {
56 mContext = context;
57 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
58 mKeyguardScreenRotation = shouldEnableKeyguardScreenRotation();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -070059 mScreenBrightnessDoze = mContext.getResources().getInteger(
60 com.android.internal.R.integer.config_screenBrightnessDoze) / 255f;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010061 }
62
63 private boolean shouldEnableKeyguardScreenRotation() {
64 Resources res = mContext.getResources();
65 return SystemProperties.getBoolean("lockscreen.rot_override", false)
66 || res.getBoolean(R.bool.config_enableLockScreenRotation);
67 }
68
69 /**
70 * Adds the status bar view to the window manager.
71 *
72 * @param statusBarView The view to add.
73 * @param barHeight The height of the status bar in collapsed state.
74 */
75 public void add(View statusBarView, int barHeight) {
76
77 // Now that the status bar window encompasses the sliding panel and its
78 // translucent backdrop, the entire thing is made TRANSLUCENT and is
79 // hardware-accelerated.
80 mLp = new WindowManager.LayoutParams(
81 ViewGroup.LayoutParams.MATCH_PARENT,
82 barHeight,
83 WindowManager.LayoutParams.TYPE_STATUS_BAR,
84 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
85 | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
86 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
87 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
Adrian Roosea562512014-05-05 13:33:03 +020088 | WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
Jorim Jaggi5cf17872014-03-26 18:31:48 +010089 PixelFormat.TRANSLUCENT);
Jorim Jaggi5cf17872014-03-26 18:31:48 +010090 mLp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
Jorim Jaggi76aaef52014-05-08 19:16:49 +020091 mLp.gravity = Gravity.TOP;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010092 mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010093 mLp.setTitle("StatusBar");
94 mLp.packageName = mContext.getPackageName();
95 mStatusBarView = statusBarView;
96 mBarHeight = barHeight;
97 mWindowManager.addView(mStatusBarView, mLp);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010098 mLpChanged = new WindowManager.LayoutParams();
99 mLpChanged.copyFrom(mLp);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100100 }
101
102 private void applyKeyguardFlags(State state) {
103 if (state.keyguardShowing) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100104 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
105 mLpChanged.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100106 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100107 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
108 mLpChanged.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100109 }
110 }
111
112 private void adjustScreenOrientation(State state) {
Jorim Jaggica36cf72014-04-17 20:36:15 +0200113 if (state.isKeyguardShowingAndNotOccluded()) {
114 if (mKeyguardScreenRotation) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100115 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
Jorim Jaggica36cf72014-04-17 20:36:15 +0200116 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100117 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
Jorim Jaggica36cf72014-04-17 20:36:15 +0200118 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100119 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100120 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100121 }
122 }
123
124 private void applyFocusableFlag(State state) {
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700125 boolean panelFocusable = state.statusBarFocusable && state.panelExpanded;
Jorim Jaggiaa806142015-05-20 18:04:16 -0700126 if (state.keyguardShowing && state.keyguardNeedsInput && state.bouncerShowing
Adrian Roos1c0ca502015-10-07 12:20:42 -0700127 || BaseStatusBar.ENABLE_REMOTE_INPUT && state.remoteInputActive) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100128 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
129 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700130 } else if (state.isKeyguardShowingAndNotOccluded() || panelFocusable) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100131 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
132 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100133 } else {
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 }
137 }
138
139 private void applyHeight(State state) {
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200140 boolean expanded = isExpanded(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100141 if (expanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100142 mLpChanged.height = ViewGroup.LayoutParams.MATCH_PARENT;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100143 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100144 mLpChanged.height = mBarHeight;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100145 }
146 }
147
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200148 private boolean isExpanded(State state) {
149 return !state.forceCollapsed && (state.isKeyguardShowingAndNotOccluded()
150 || state.panelVisible || state.keyguardFadingAway || state.bouncerShowing
151 || state.headsUpShowing);
152 }
153
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200154 private void applyFitsSystemWindows(State state) {
155 mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
156 }
157
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100158 private void applyUserActivityTimeout(State state) {
Jorim Jaggiecbab362014-04-23 16:13:15 +0200159 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200160 && state.statusBarState == StatusBarState.KEYGUARD
161 && !state.qsExpanded) {
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100162 mLpChanged.userActivityTimeout = KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100163 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100164 mLpChanged.userActivityTimeout = -1;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100165 }
166 }
167
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200168 private void applyInputFeatures(State state) {
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200169 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200170 && state.statusBarState == StatusBarState.KEYGUARD
171 && !state.qsExpanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100172 mLpChanged.inputFeatures |=
173 WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200174 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100175 mLpChanged.inputFeatures &=
176 ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200177 }
178 }
179
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100180 private void apply(State state) {
181 applyKeyguardFlags(state);
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700182 applyForceStatusBarVisibleFlag(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100183 applyFocusableFlag(state);
184 adjustScreenOrientation(state);
185 applyHeight(state);
186 applyUserActivityTimeout(state);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200187 applyInputFeatures(state);
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200188 applyFitsSystemWindows(state);
Selim Cineka59ecc32015-04-07 10:51:49 -0700189 applyModalFlag(state);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700190 applyBrightness(state);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100191 if (mLp.copyFrom(mLpChanged) != 0) {
192 mWindowManager.updateViewLayout(mStatusBarView, mLp);
193 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100194 }
195
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700196 private void applyForceStatusBarVisibleFlag(State state) {
197 if (state.forceStatusBarVisible) {
198 mLpChanged.privateFlags |= WindowManager
199 .LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
200 } else {
201 mLpChanged.privateFlags &= ~WindowManager
202 .LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
203 }
204 }
205
Selim Cineka59ecc32015-04-07 10:51:49 -0700206 private void applyModalFlag(State state) {
207 if (state.headsUpShowing) {
208 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
209 } else {
210 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
211 }
212 }
213
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700214 private void applyBrightness(State state) {
215 if (state.forceDozeBrightness) {
216 mLpChanged.screenBrightness = mScreenBrightnessDoze;
217 } else {
218 mLpChanged.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
219 }
220 }
221
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100222 public void setKeyguardShowing(boolean showing) {
223 mCurrentState.keyguardShowing = showing;
224 apply(mCurrentState);
225 }
226
227 public void setKeyguardOccluded(boolean occluded) {
228 mCurrentState.keyguardOccluded = occluded;
229 apply(mCurrentState);
230 }
231
232 public void setKeyguardNeedsInput(boolean needsInput) {
233 mCurrentState.keyguardNeedsInput = needsInput;
234 apply(mCurrentState);
235 }
236
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700237 public void setPanelVisible(boolean visible) {
238 mCurrentState.panelVisible = visible;
239 mCurrentState.statusBarFocusable = visible;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100240 apply(mCurrentState);
241 }
242
243 public void setStatusBarFocusable(boolean focusable) {
244 mCurrentState.statusBarFocusable = focusable;
245 apply(mCurrentState);
246 }
247
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200248 public void setBouncerShowing(boolean showing) {
249 mCurrentState.bouncerShowing = showing;
250 apply(mCurrentState);
251 }
252
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200253 public void setKeyguardFadingAway(boolean keyguardFadingAway) {
254 mCurrentState.keyguardFadingAway = keyguardFadingAway;
255 apply(mCurrentState);
256 }
257
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200258 public void setQsExpanded(boolean expanded) {
259 mCurrentState.qsExpanded = expanded;
260 apply(mCurrentState);
261 }
262
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700263 public void setHeadsUpShowing(boolean showing) {
264 mCurrentState.headsUpShowing = showing;
265 apply(mCurrentState);
266 }
267
Jorim Jaggiecbab362014-04-23 16:13:15 +0200268 /**
269 * @param state The {@link StatusBarState} of the status bar.
270 */
271 public void setStatusBarState(int state) {
272 mCurrentState.statusBarState = state;
273 apply(mCurrentState);
274 }
275
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700276 public void setForceStatusBarVisible(boolean forceStatusBarVisible) {
277 mCurrentState.forceStatusBarVisible = forceStatusBarVisible;
278 apply(mCurrentState);
279 }
280
Selim Cinek737bff32015-05-08 16:08:35 -0700281 /**
282 * Force the window to be collapsed, even if it should theoretically be expanded.
283 * Used for when a heads-up comes in but we still need to wait for the touchable regions to
284 * be computed.
285 */
286 public void setForceWindowCollapsed(boolean force) {
287 mCurrentState.forceCollapsed = force;
288 apply(mCurrentState);
289 }
290
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700291 public void setPanelExpanded(boolean isExpanded) {
292 mCurrentState.panelExpanded = isExpanded;
293 apply(mCurrentState);
294 }
295
Adrian Roosd28ccd72016-01-06 15:23:14 +0100296 @Override
297 public void onRemoteInputActive(boolean remoteInputActive) {
Adrian Roos1c0ca502015-10-07 12:20:42 -0700298 mCurrentState.remoteInputActive = remoteInputActive;
299 apply(mCurrentState);
300 }
301
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700302 /**
303 * Set whether the screen brightness is forced to the value we use for doze mode by the status
304 * bar window.
305 */
306 public void setForceDozeBrightness(boolean forceDozeBrightness) {
307 mCurrentState.forceDozeBrightness = forceDozeBrightness;
308 apply(mCurrentState);
309 }
310
Selim Cinek7025f262015-07-13 16:22:48 -0700311 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
312 pw.println("StatusBarWindowManager state:");
313 pw.println(mCurrentState);
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200314 }
315
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100316 private static class State {
317 boolean keyguardShowing;
318 boolean keyguardOccluded;
319 boolean keyguardNeedsInput;
Selim Cinek4a21a7f2015-05-19 11:00:38 -0700320 boolean panelVisible;
321 boolean panelExpanded;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100322 boolean statusBarFocusable;
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200323 boolean bouncerShowing;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200324 boolean keyguardFadingAway;
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200325 boolean qsExpanded;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700326 boolean headsUpShowing;
Selim Cinek4a4a2bddc2015-05-07 12:50:19 -0700327 boolean forceStatusBarVisible;
Selim Cinek737bff32015-05-08 16:08:35 -0700328 boolean forceCollapsed;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700329 boolean forceDozeBrightness;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100330
Jorim Jaggiecbab362014-04-23 16:13:15 +0200331 /**
332 * The {@link BaseStatusBar} state from the status bar.
333 */
334 int statusBarState;
335
Adrian Roos1c0ca502015-10-07 12:20:42 -0700336 boolean remoteInputActive;
337
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100338 private boolean isKeyguardShowingAndNotOccluded() {
339 return keyguardShowing && !keyguardOccluded;
340 }
Selim Cinek6a1bd2b2015-06-02 16:02:41 +0200341
342 @Override
343 public String toString() {
344 StringBuilder result = new StringBuilder();
345 String newLine = "\n";
346 result.append("Window State {");
347 result.append(newLine);
348
349 Field[] fields = this.getClass().getDeclaredFields();
350
351 // Print field names paired with their values
352 for (Field field : fields) {
353 result.append(" ");
354 try {
355 result.append(field.getName());
356 result.append(": ");
357 //requires access to private field:
358 result.append(field.get(this));
359 } catch (IllegalAccessException ex) {
360 }
361 result.append(newLine);
362 }
363 result.append("}");
364
365 return result.toString();
366 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100367 }
368}