blob: eba7d9ffb948e49a50b9d0445038b1334ba37312 [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
118 && state.bouncerShowing) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100119 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
120 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100121 } else if (state.isKeyguardShowingAndNotOccluded() || state.statusBarFocusable) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100122 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
123 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100124 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100125 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
126 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100127 }
128 }
129
130 private void applyHeight(State state) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200131 boolean expanded = state.isKeyguardShowingAndNotOccluded() || state.statusBarExpanded
Adrian Roos0002a452014-07-03 13:46:07 +0200132 || state.keyguardFadingAway || state.bouncerShowing;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100133 if (expanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100134 mLpChanged.height = ViewGroup.LayoutParams.MATCH_PARENT;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100135 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100136 mLpChanged.height = mBarHeight;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100137 }
138 }
139
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200140 private void applyFitsSystemWindows(State state) {
141 mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
142 }
143
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100144 private void applyUserActivityTimeout(State state) {
Jorim Jaggiecbab362014-04-23 16:13:15 +0200145 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200146 && state.statusBarState == StatusBarState.KEYGUARD
147 && !state.qsExpanded) {
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100148 mLpChanged.userActivityTimeout = KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100149 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100150 mLpChanged.userActivityTimeout = -1;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100151 }
152 }
153
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200154 private void applyInputFeatures(State state) {
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200155 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200156 && state.statusBarState == StatusBarState.KEYGUARD
157 && !state.qsExpanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100158 mLpChanged.inputFeatures |=
159 WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200160 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100161 mLpChanged.inputFeatures &=
162 ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200163 }
164 }
165
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100166 private void apply(State state) {
167 applyKeyguardFlags(state);
168 applyFocusableFlag(state);
169 adjustScreenOrientation(state);
170 applyHeight(state);
171 applyUserActivityTimeout(state);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200172 applyInputFeatures(state);
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200173 applyFitsSystemWindows(state);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100174 if (mLp.copyFrom(mLpChanged) != 0) {
175 mWindowManager.updateViewLayout(mStatusBarView, mLp);
176 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100177 }
178
179 public void setKeyguardShowing(boolean showing) {
180 mCurrentState.keyguardShowing = showing;
181 apply(mCurrentState);
182 }
183
184 public void setKeyguardOccluded(boolean occluded) {
185 mCurrentState.keyguardOccluded = occluded;
186 apply(mCurrentState);
187 }
188
189 public void setKeyguardNeedsInput(boolean needsInput) {
190 mCurrentState.keyguardNeedsInput = needsInput;
191 apply(mCurrentState);
192 }
193
194 public void setStatusBarExpanded(boolean expanded) {
195 mCurrentState.statusBarExpanded = expanded;
196 mCurrentState.statusBarFocusable = expanded;
197 apply(mCurrentState);
198 }
199
200 public void setStatusBarFocusable(boolean focusable) {
201 mCurrentState.statusBarFocusable = focusable;
202 apply(mCurrentState);
203 }
204
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200205 public void setBouncerShowing(boolean showing) {
206 mCurrentState.bouncerShowing = showing;
207 apply(mCurrentState);
208 }
209
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200210 public void setKeyguardFadingAway(boolean keyguardFadingAway) {
211 mCurrentState.keyguardFadingAway = keyguardFadingAway;
212 apply(mCurrentState);
213 }
214
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200215 public void setQsExpanded(boolean expanded) {
216 mCurrentState.qsExpanded = expanded;
217 apply(mCurrentState);
218 }
219
Jorim Jaggiecbab362014-04-23 16:13:15 +0200220 /**
221 * @param state The {@link StatusBarState} of the status bar.
222 */
223 public void setStatusBarState(int state) {
224 mCurrentState.statusBarState = state;
225 apply(mCurrentState);
226 }
227
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100228 private static class State {
229 boolean keyguardShowing;
230 boolean keyguardOccluded;
231 boolean keyguardNeedsInput;
232 boolean statusBarExpanded;
233 boolean statusBarFocusable;
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200234 boolean bouncerShowing;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200235 boolean keyguardFadingAway;
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200236 boolean qsExpanded;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100237
Jorim Jaggiecbab362014-04-23 16:13:15 +0200238 /**
239 * The {@link BaseStatusBar} state from the status bar.
240 */
241 int statusBarState;
242
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100243 private boolean isKeyguardShowingAndNotOccluded() {
244 return keyguardShowing && !keyguardOccluded;
245 }
246 }
247}