2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.systemui.statusbar.phone;
19 import android.app.ActivityManagerNative;
20 import android.app.IActivityManager;
21 import android.content.Context;
22 import android.content.pm.ActivityInfo;
23 import android.content.res.Resources;
24 import android.graphics.PixelFormat;
25 import android.os.RemoteException;
26 import android.os.SystemProperties;
27 import android.os.Trace;
28 import android.util.Log;
29 import android.view.Gravity;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.view.WindowManager;
34 import com.android.keyguard.R;
35 import com.android.systemui.keyguard.KeyguardViewMediator;
36 import com.android.systemui.statusbar.BaseStatusBar;
37 import com.android.systemui.statusbar.RemoteInputController;
38 import com.android.systemui.statusbar.StatusBarState;
40 import java.io.FileDescriptor;
41 import java.io.PrintWriter;
42 import java.lang.reflect.Field;
45 * Encapsulates all logic for the status bar window state management.
47 public class StatusBarWindowManager implements RemoteInputController.Callback {
49 private static final String TAG = "StatusBarWindowManager";
51 private final Context mContext;
52 private final WindowManager mWindowManager;
53 private final IActivityManager mActivityManager;
54 private View mStatusBarView;
55 private WindowManager.LayoutParams mLp;
56 private WindowManager.LayoutParams mLpChanged;
57 private boolean mHasTopUi;
58 private boolean mHasTopUiChanged;
59 private int mBarHeight;
60 private final boolean mKeyguardScreenRotation;
61 private final float mScreenBrightnessDoze;
62 private final State mCurrentState = new State();
64 public StatusBarWindowManager(Context context) {
66 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
67 mActivityManager = ActivityManagerNative.getDefault();
68 mKeyguardScreenRotation = shouldEnableKeyguardScreenRotation();
69 mScreenBrightnessDoze = mContext.getResources().getInteger(
70 com.android.internal.R.integer.config_screenBrightnessDoze) / 255f;
73 private boolean shouldEnableKeyguardScreenRotation() {
74 Resources res = mContext.getResources();
75 return SystemProperties.getBoolean("lockscreen.rot_override", false)
76 || res.getBoolean(R.bool.config_enableLockScreenRotation);
80 * Adds the status bar view to the window manager.
82 * @param statusBarView The view to add.
83 * @param barHeight The height of the status bar in collapsed state.
85 public void add(View statusBarView, int barHeight) {
87 // Now that the status bar window encompasses the sliding panel and its
88 // translucent backdrop, the entire thing is made TRANSLUCENT and is
89 // hardware-accelerated.
90 mLp = new WindowManager.LayoutParams(
91 ViewGroup.LayoutParams.MATCH_PARENT,
93 WindowManager.LayoutParams.TYPE_STATUS_BAR,
94 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
95 | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
96 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
97 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
98 | WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
99 PixelFormat.TRANSLUCENT);
100 mLp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
101 mLp.gravity = Gravity.TOP;
102 mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
103 mLp.setTitle("StatusBar");
104 mLp.packageName = mContext.getPackageName();
105 mStatusBarView = statusBarView;
106 mBarHeight = barHeight;
107 mWindowManager.addView(mStatusBarView, mLp);
108 mLpChanged = new WindowManager.LayoutParams();
109 mLpChanged.copyFrom(mLp);
112 private void applyKeyguardFlags(State state) {
113 if (state.keyguardShowing) {
114 mLpChanged.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
116 mLpChanged.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
119 if (state.keyguardShowing && !state.backdropShowing) {
120 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
122 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
126 private void adjustScreenOrientation(State state) {
127 if (state.isKeyguardShowingAndNotOccluded()) {
128 if (mKeyguardScreenRotation) {
129 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
131 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
134 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
138 private void applyFocusableFlag(State state) {
139 boolean panelFocusable = state.statusBarFocusable && state.panelExpanded;
140 if (state.keyguardShowing && state.keyguardNeedsInput && state.bouncerShowing
141 || BaseStatusBar.ENABLE_REMOTE_INPUT && state.remoteInputActive) {
142 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
143 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
144 } else if (state.isKeyguardShowingAndNotOccluded() || panelFocusable) {
145 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
146 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
148 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
149 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
152 mLpChanged.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
155 private void applyHeight(State state) {
156 boolean expanded = isExpanded(state);
158 mLpChanged.height = ViewGroup.LayoutParams.MATCH_PARENT;
160 mLpChanged.height = mBarHeight;
164 private boolean isExpanded(State state) {
165 return !state.forceCollapsed && (state.isKeyguardShowingAndNotOccluded()
166 || state.panelVisible || state.keyguardFadingAway || state.bouncerShowing
167 || state.headsUpShowing);
170 private void applyFitsSystemWindows(State state) {
171 boolean fitsSystemWindows = !state.isKeyguardShowingAndNotOccluded();
172 if (mStatusBarView.getFitsSystemWindows() != fitsSystemWindows) {
173 mStatusBarView.setFitsSystemWindows(fitsSystemWindows);
174 mStatusBarView.requestApplyInsets();
178 private void applyUserActivityTimeout(State state) {
179 if (state.isKeyguardShowingAndNotOccluded()
180 && state.statusBarState == StatusBarState.KEYGUARD
181 && !state.qsExpanded) {
182 mLpChanged.userActivityTimeout = KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
184 mLpChanged.userActivityTimeout = -1;
188 private void applyInputFeatures(State state) {
189 if (state.isKeyguardShowingAndNotOccluded()
190 && state.statusBarState == StatusBarState.KEYGUARD
191 && !state.qsExpanded && !state.forceUserActivity) {
192 mLpChanged.inputFeatures |=
193 WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
195 mLpChanged.inputFeatures &=
196 ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
200 private void apply(State state) {
201 applyKeyguardFlags(state);
202 applyForceStatusBarVisibleFlag(state);
203 applyFocusableFlag(state);
204 adjustScreenOrientation(state);
206 applyUserActivityTimeout(state);
207 applyInputFeatures(state);
208 applyFitsSystemWindows(state);
209 applyModalFlag(state);
210 applyBrightness(state);
211 applyHasTopUi(state);
212 if (mLp.copyFrom(mLpChanged) != 0) {
213 mWindowManager.updateViewLayout(mStatusBarView, mLp);
215 if (mHasTopUi != mHasTopUiChanged) {
217 mActivityManager.setHasTopUi(mHasTopUiChanged);
218 } catch (RemoteException e) {
219 Log.e(TAG, "Failed to call setHasTopUi", e);
221 mHasTopUi = mHasTopUiChanged;
225 private void applyForceStatusBarVisibleFlag(State state) {
226 if (state.forceStatusBarVisible) {
227 mLpChanged.privateFlags |= WindowManager
228 .LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
230 mLpChanged.privateFlags &= ~WindowManager
231 .LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
235 private void applyModalFlag(State state) {
236 if (state.headsUpShowing) {
237 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
239 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
243 private void applyBrightness(State state) {
244 if (state.forceDozeBrightness) {
245 mLpChanged.screenBrightness = mScreenBrightnessDoze;
247 mLpChanged.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
251 private void applyHasTopUi(State state) {
252 mHasTopUiChanged = isExpanded(state);
255 public void setKeyguardShowing(boolean showing) {
256 mCurrentState.keyguardShowing = showing;
257 apply(mCurrentState);
260 public void setKeyguardOccluded(boolean occluded) {
261 mCurrentState.keyguardOccluded = occluded;
262 apply(mCurrentState);
265 public void setKeyguardNeedsInput(boolean needsInput) {
266 mCurrentState.keyguardNeedsInput = needsInput;
267 apply(mCurrentState);
270 public void setPanelVisible(boolean visible) {
271 mCurrentState.panelVisible = visible;
272 mCurrentState.statusBarFocusable = visible;
273 apply(mCurrentState);
276 public void setStatusBarFocusable(boolean focusable) {
277 mCurrentState.statusBarFocusable = focusable;
278 apply(mCurrentState);
281 public void setBouncerShowing(boolean showing) {
282 mCurrentState.bouncerShowing = showing;
283 apply(mCurrentState);
286 public void setBackdropShowing(boolean showing) {
287 mCurrentState.backdropShowing = showing;
288 apply(mCurrentState);
291 public void setKeyguardFadingAway(boolean keyguardFadingAway) {
292 mCurrentState.keyguardFadingAway = keyguardFadingAway;
293 apply(mCurrentState);
296 public void setQsExpanded(boolean expanded) {
297 mCurrentState.qsExpanded = expanded;
298 apply(mCurrentState);
301 public void setForceUserActivity(boolean forceUserActivity) {
302 mCurrentState.forceUserActivity = forceUserActivity;
303 apply(mCurrentState);
306 public void setHeadsUpShowing(boolean showing) {
307 mCurrentState.headsUpShowing = showing;
308 apply(mCurrentState);
312 * @param state The {@link StatusBarState} of the status bar.
314 public void setStatusBarState(int state) {
315 mCurrentState.statusBarState = state;
316 apply(mCurrentState);
319 public void setForceStatusBarVisible(boolean forceStatusBarVisible) {
320 mCurrentState.forceStatusBarVisible = forceStatusBarVisible;
321 apply(mCurrentState);
325 * Force the window to be collapsed, even if it should theoretically be expanded.
326 * Used for when a heads-up comes in but we still need to wait for the touchable regions to
329 public void setForceWindowCollapsed(boolean force) {
330 mCurrentState.forceCollapsed = force;
331 apply(mCurrentState);
334 public void setPanelExpanded(boolean isExpanded) {
335 mCurrentState.panelExpanded = isExpanded;
336 apply(mCurrentState);
340 public void onRemoteInputActive(boolean remoteInputActive) {
341 mCurrentState.remoteInputActive = remoteInputActive;
342 apply(mCurrentState);
346 * Set whether the screen brightness is forced to the value we use for doze mode by the status
349 public void setForceDozeBrightness(boolean forceDozeBrightness) {
350 mCurrentState.forceDozeBrightness = forceDozeBrightness;
351 apply(mCurrentState);
354 public void setBarHeight(int barHeight) {
355 mBarHeight = barHeight;
356 apply(mCurrentState);
359 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
360 pw.println("StatusBarWindowManager state:");
361 pw.println(mCurrentState);
364 public boolean isShowingWallpaper() {
365 return !mCurrentState.backdropShowing;
368 private static class State {
369 boolean keyguardShowing;
370 boolean keyguardOccluded;
371 boolean keyguardNeedsInput;
372 boolean panelVisible;
373 boolean panelExpanded;
374 boolean statusBarFocusable;
375 boolean bouncerShowing;
376 boolean keyguardFadingAway;
378 boolean headsUpShowing;
379 boolean forceStatusBarVisible;
380 boolean forceCollapsed;
381 boolean forceDozeBrightness;
382 boolean forceUserActivity;
383 boolean backdropShowing;
386 * The {@link BaseStatusBar} state from the status bar.
390 boolean remoteInputActive;
392 private boolean isKeyguardShowingAndNotOccluded() {
393 return keyguardShowing && !keyguardOccluded;
397 public String toString() {
398 StringBuilder result = new StringBuilder();
399 String newLine = "\n";
400 result.append("Window State {");
401 result.append(newLine);
403 Field[] fields = this.getClass().getDeclaredFields();
405 // Print field names paired with their values
406 for (Field field : fields) {
409 result.append(field.getName());
411 //requires access to private field:
412 result.append(field.get(this));
413 } catch (IllegalAccessException ex) {
415 result.append(newLine);
419 return result.toString();