blob: 84a9f64331a657408c22b638e83ee854f7a2d71d [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;
32import com.android.systemui.statusbar.StatusBarState;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010033
34/**
35 * Encapsulates all logic for the status bar window state management.
36 */
37public class StatusBarWindowManager {
38
39 private final Context mContext;
40 private final WindowManager mWindowManager;
41 private View mStatusBarView;
42 private WindowManager.LayoutParams mLp;
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010043 private WindowManager.LayoutParams mLpChanged;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010044 private int mBarHeight;
45 private final boolean mKeyguardScreenRotation;
46
47 private final State mCurrentState = new State();
48
49 public StatusBarWindowManager(Context context) {
50 mContext = context;
51 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
52 mKeyguardScreenRotation = shouldEnableKeyguardScreenRotation();
53 }
54
55 private boolean shouldEnableKeyguardScreenRotation() {
56 Resources res = mContext.getResources();
57 return SystemProperties.getBoolean("lockscreen.rot_override", false)
58 || res.getBoolean(R.bool.config_enableLockScreenRotation);
59 }
60
61 /**
62 * Adds the status bar view to the window manager.
63 *
64 * @param statusBarView The view to add.
65 * @param barHeight The height of the status bar in collapsed state.
66 */
67 public void add(View statusBarView, int barHeight) {
68
69 // Now that the status bar window encompasses the sliding panel and its
70 // translucent backdrop, the entire thing is made TRANSLUCENT and is
71 // hardware-accelerated.
72 mLp = new WindowManager.LayoutParams(
73 ViewGroup.LayoutParams.MATCH_PARENT,
74 barHeight,
75 WindowManager.LayoutParams.TYPE_STATUS_BAR,
76 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
77 | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
78 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
79 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
Adrian Roosea562512014-05-05 13:33:03 +020080 | WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
Jorim Jaggi5cf17872014-03-26 18:31:48 +010081 PixelFormat.TRANSLUCENT);
Jorim Jaggi5cf17872014-03-26 18:31:48 +010082 mLp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
Jorim Jaggi76aaef52014-05-08 19:16:49 +020083 mLp.gravity = Gravity.TOP;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010084 mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010085 mLp.setTitle("StatusBar");
86 mLp.packageName = mContext.getPackageName();
87 mStatusBarView = statusBarView;
88 mBarHeight = barHeight;
89 mWindowManager.addView(mStatusBarView, mLp);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010090 mLpChanged = new WindowManager.LayoutParams();
91 mLpChanged.copyFrom(mLp);
Jorim Jaggi5cf17872014-03-26 18:31:48 +010092 }
93
94 private void applyKeyguardFlags(State state) {
95 if (state.keyguardShowing) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010096 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
97 mLpChanged.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010098 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010099 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
100 mLpChanged.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100101 }
102 }
103
104 private void adjustScreenOrientation(State state) {
Jorim Jaggica36cf72014-04-17 20:36:15 +0200105 if (state.isKeyguardShowingAndNotOccluded()) {
106 if (mKeyguardScreenRotation) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100107 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
Jorim Jaggica36cf72014-04-17 20:36:15 +0200108 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100109 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
Jorim Jaggica36cf72014-04-17 20:36:15 +0200110 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100111 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100112 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100113 }
114 }
115
116 private void applyFocusableFlag(State state) {
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200117 if (state.isKeyguardShowingAndNotOccluded() && state.keyguardNeedsInput
Adrian Roos497ab022015-02-10 20:49:33 +0100118 && state.bouncerShowing
119 || BaseStatusBar.ENABLE_REMOTE_INPUT && state.statusBarExpanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100120 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
121 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100122 } else if (state.isKeyguardShowingAndNotOccluded() || state.statusBarFocusable) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100123 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
124 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100125 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100126 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
127 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100128 }
129 }
130
131 private void applyHeight(State state) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200132 boolean expanded = state.isKeyguardShowingAndNotOccluded() || state.statusBarExpanded
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700133 || state.keyguardFadingAway || state.bouncerShowing || state.headsUpShowing;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100134 if (expanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100135 mLpChanged.height = ViewGroup.LayoutParams.MATCH_PARENT;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100136 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100137 mLpChanged.height = mBarHeight;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100138 }
139 }
140
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200141 private void applyFitsSystemWindows(State state) {
142 mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
143 }
144
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100145 private void applyUserActivityTimeout(State state) {
Jorim Jaggiecbab362014-04-23 16:13:15 +0200146 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200147 && state.statusBarState == StatusBarState.KEYGUARD
148 && !state.qsExpanded) {
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100149 mLpChanged.userActivityTimeout = KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100150 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100151 mLpChanged.userActivityTimeout = -1;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100152 }
153 }
154
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200155 private void applyInputFeatures(State state) {
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200156 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200157 && state.statusBarState == StatusBarState.KEYGUARD
158 && !state.qsExpanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100159 mLpChanged.inputFeatures |=
160 WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200161 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100162 mLpChanged.inputFeatures &=
163 ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200164 }
165 }
166
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100167 private void apply(State state) {
168 applyKeyguardFlags(state);
169 applyFocusableFlag(state);
170 adjustScreenOrientation(state);
171 applyHeight(state);
172 applyUserActivityTimeout(state);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200173 applyInputFeatures(state);
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200174 applyFitsSystemWindows(state);
Selim Cineka59ecc32015-04-07 10:51:49 -0700175 applyModalFlag(state);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100176 if (mLp.copyFrom(mLpChanged) != 0) {
177 mWindowManager.updateViewLayout(mStatusBarView, mLp);
178 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100179 }
180
Selim Cineka59ecc32015-04-07 10:51:49 -0700181 private void applyModalFlag(State state) {
182 if (state.headsUpShowing) {
183 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
184 } else {
185 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
186 }
187 }
188
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100189 public void setKeyguardShowing(boolean showing) {
190 mCurrentState.keyguardShowing = showing;
191 apply(mCurrentState);
192 }
193
194 public void setKeyguardOccluded(boolean occluded) {
195 mCurrentState.keyguardOccluded = occluded;
196 apply(mCurrentState);
197 }
198
199 public void setKeyguardNeedsInput(boolean needsInput) {
200 mCurrentState.keyguardNeedsInput = needsInput;
201 apply(mCurrentState);
202 }
203
204 public void setStatusBarExpanded(boolean expanded) {
205 mCurrentState.statusBarExpanded = expanded;
206 mCurrentState.statusBarFocusable = expanded;
207 apply(mCurrentState);
208 }
209
210 public void setStatusBarFocusable(boolean focusable) {
211 mCurrentState.statusBarFocusable = focusable;
212 apply(mCurrentState);
213 }
214
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200215 public void setBouncerShowing(boolean showing) {
216 mCurrentState.bouncerShowing = showing;
217 apply(mCurrentState);
218 }
219
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200220 public void setKeyguardFadingAway(boolean keyguardFadingAway) {
221 mCurrentState.keyguardFadingAway = keyguardFadingAway;
222 apply(mCurrentState);
223 }
224
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200225 public void setQsExpanded(boolean expanded) {
226 mCurrentState.qsExpanded = expanded;
227 apply(mCurrentState);
228 }
229
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700230 public void setHeadsUpShowing(boolean showing) {
231 mCurrentState.headsUpShowing = showing;
232 apply(mCurrentState);
233 }
234
Jorim Jaggiecbab362014-04-23 16:13:15 +0200235 /**
236 * @param state The {@link StatusBarState} of the status bar.
237 */
238 public void setStatusBarState(int state) {
239 mCurrentState.statusBarState = state;
240 apply(mCurrentState);
241 }
242
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100243 private static class State {
244 boolean keyguardShowing;
245 boolean keyguardOccluded;
246 boolean keyguardNeedsInput;
247 boolean statusBarExpanded;
248 boolean statusBarFocusable;
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200249 boolean bouncerShowing;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200250 boolean keyguardFadingAway;
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200251 boolean qsExpanded;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700252 boolean headsUpShowing;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100253
Jorim Jaggiecbab362014-04-23 16:13:15 +0200254 /**
255 * The {@link BaseStatusBar} state from the status bar.
256 */
257 int statusBarState;
258
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100259 private boolean isKeyguardShowingAndNotOccluded() {
260 return keyguardShowing && !keyguardOccluded;
261 }
262 }
263}