blob: 0dbdca17cd40308c0a95a0d61782fb201f5aa24e [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 Jaggiecbab362014-04-23 16:13:15 +020030import com.android.systemui.statusbar.BaseStatusBar;
31import com.android.systemui.statusbar.StatusBarState;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010032
33/**
34 * Encapsulates all logic for the status bar window state management.
35 */
36public class StatusBarWindowManager {
37
38 private final Context mContext;
39 private final WindowManager mWindowManager;
40 private View mStatusBarView;
41 private WindowManager.LayoutParams mLp;
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010042 private WindowManager.LayoutParams mLpChanged;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010043 private int mBarHeight;
44 private final boolean mKeyguardScreenRotation;
45
46 private final State mCurrentState = new State();
47
48 public StatusBarWindowManager(Context context) {
49 mContext = context;
50 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
51 mKeyguardScreenRotation = shouldEnableKeyguardScreenRotation();
52 }
53
54 private boolean shouldEnableKeyguardScreenRotation() {
55 Resources res = mContext.getResources();
56 return SystemProperties.getBoolean("lockscreen.rot_override", false)
57 || res.getBoolean(R.bool.config_enableLockScreenRotation);
58 }
59
60 /**
61 * Adds the status bar view to the window manager.
62 *
63 * @param statusBarView The view to add.
64 * @param barHeight The height of the status bar in collapsed state.
65 */
66 public void add(View statusBarView, int barHeight) {
67
68 // Now that the status bar window encompasses the sliding panel and its
69 // translucent backdrop, the entire thing is made TRANSLUCENT and is
70 // hardware-accelerated.
71 mLp = new WindowManager.LayoutParams(
72 ViewGroup.LayoutParams.MATCH_PARENT,
73 barHeight,
74 WindowManager.LayoutParams.TYPE_STATUS_BAR,
75 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
76 | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
77 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
78 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
Adrian Roosea562512014-05-05 13:33:03 +020079 | WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
Jorim Jaggi5cf17872014-03-26 18:31:48 +010080 PixelFormat.TRANSLUCENT);
Jorim Jaggi5cf17872014-03-26 18:31:48 +010081 mLp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
Jorim Jaggi76aaef52014-05-08 19:16:49 +020082 mLp.gravity = Gravity.TOP;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010083 mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010084 mLp.setTitle("StatusBar");
85 mLp.packageName = mContext.getPackageName();
86 mStatusBarView = statusBarView;
87 mBarHeight = barHeight;
88 mWindowManager.addView(mStatusBarView, mLp);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010089 mLpChanged = new WindowManager.LayoutParams();
90 mLpChanged.copyFrom(mLp);
Jorim Jaggi5cf17872014-03-26 18:31:48 +010091 }
92
93 private void applyKeyguardFlags(State state) {
94 if (state.keyguardShowing) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010095 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
96 mLpChanged.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010097 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +010098 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
99 mLpChanged.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100100 }
101 }
102
103 private void adjustScreenOrientation(State state) {
Jorim Jaggica36cf72014-04-17 20:36:15 +0200104 if (state.isKeyguardShowingAndNotOccluded()) {
105 if (mKeyguardScreenRotation) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100106 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
Jorim Jaggica36cf72014-04-17 20:36:15 +0200107 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100108 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
Jorim Jaggica36cf72014-04-17 20:36:15 +0200109 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100110 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100111 mLpChanged.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100112 }
113 }
114
115 private void applyFocusableFlag(State state) {
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200116 if (state.isKeyguardShowingAndNotOccluded() && state.keyguardNeedsInput
117 && state.bouncerShowing) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100118 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
119 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100120 } else if (state.isKeyguardShowingAndNotOccluded() || state.statusBarFocusable) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100121 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
122 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100123 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100124 mLpChanged.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
125 mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100126 }
127 }
128
129 private void applyHeight(State state) {
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200130 boolean expanded = state.isKeyguardShowingAndNotOccluded() || state.statusBarExpanded
Adrian Roos0002a452014-07-03 13:46:07 +0200131 || state.keyguardFadingAway || state.bouncerShowing;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100132 if (expanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100133 mLpChanged.height = ViewGroup.LayoutParams.MATCH_PARENT;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100134 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100135 mLpChanged.height = mBarHeight;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100136 }
137 }
138
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200139 private void applyFitsSystemWindows(State state) {
140 mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
141 }
142
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100143 private void applyUserActivityTimeout(State state) {
Jorim Jaggiecbab362014-04-23 16:13:15 +0200144 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200145 && state.statusBarState == StatusBarState.KEYGUARD
146 && !state.qsExpanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100147 mLpChanged.userActivityTimeout = state.keyguardUserActivityTimeout;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100148 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100149 mLpChanged.userActivityTimeout = -1;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100150 }
151 }
152
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200153 private void applyInputFeatures(State state) {
Jorim Jaggi4222d9a2014-04-23 16:13:15 +0200154 if (state.isKeyguardShowingAndNotOccluded()
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200155 && state.statusBarState == StatusBarState.KEYGUARD
156 && !state.qsExpanded) {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100157 mLpChanged.inputFeatures |=
158 WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200159 } else {
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100160 mLpChanged.inputFeatures &=
161 ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200162 }
163 }
164
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100165 private void apply(State state) {
166 applyKeyguardFlags(state);
167 applyFocusableFlag(state);
168 adjustScreenOrientation(state);
169 applyHeight(state);
170 applyUserActivityTimeout(state);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200171 applyInputFeatures(state);
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200172 applyFitsSystemWindows(state);
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100173 if (mLp.copyFrom(mLpChanged) != 0) {
174 mWindowManager.updateViewLayout(mStatusBarView, mLp);
175 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100176 }
177
178 public void setKeyguardShowing(boolean showing) {
179 mCurrentState.keyguardShowing = showing;
180 apply(mCurrentState);
181 }
182
183 public void setKeyguardOccluded(boolean occluded) {
184 mCurrentState.keyguardOccluded = occluded;
185 apply(mCurrentState);
186 }
187
188 public void setKeyguardNeedsInput(boolean needsInput) {
189 mCurrentState.keyguardNeedsInput = needsInput;
190 apply(mCurrentState);
191 }
192
193 public void setStatusBarExpanded(boolean expanded) {
194 mCurrentState.statusBarExpanded = expanded;
195 mCurrentState.statusBarFocusable = expanded;
196 apply(mCurrentState);
197 }
198
199 public void setStatusBarFocusable(boolean focusable) {
200 mCurrentState.statusBarFocusable = focusable;
201 apply(mCurrentState);
202 }
203
204 public void setKeyguardUserActivityTimeout(long timeout) {
205 mCurrentState.keyguardUserActivityTimeout = timeout;
206 apply(mCurrentState);
207 }
208
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200209 public void setBouncerShowing(boolean showing) {
210 mCurrentState.bouncerShowing = showing;
211 apply(mCurrentState);
212 }
213
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200214 public void setKeyguardFadingAway(boolean keyguardFadingAway) {
215 mCurrentState.keyguardFadingAway = keyguardFadingAway;
216 apply(mCurrentState);
217 }
218
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200219 public void setQsExpanded(boolean expanded) {
220 mCurrentState.qsExpanded = expanded;
221 apply(mCurrentState);
222 }
223
Jorim Jaggiecbab362014-04-23 16:13:15 +0200224 /**
225 * @param state The {@link StatusBarState} of the status bar.
226 */
227 public void setStatusBarState(int state) {
228 mCurrentState.statusBarState = state;
229 apply(mCurrentState);
230 }
231
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100232 private static class State {
233 boolean keyguardShowing;
234 boolean keyguardOccluded;
235 boolean keyguardNeedsInput;
236 boolean statusBarExpanded;
237 boolean statusBarFocusable;
238 long keyguardUserActivityTimeout;
Jorim Jaggi5fd4d052014-05-13 22:50:20 +0200239 boolean bouncerShowing;
Jorim Jaggie29b2db2014-05-30 23:17:03 +0200240 boolean keyguardFadingAway;
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200241 boolean qsExpanded;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100242
Jorim Jaggiecbab362014-04-23 16:13:15 +0200243 /**
244 * The {@link BaseStatusBar} state from the status bar.
245 */
246 int statusBarState;
247
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100248 private boolean isKeyguardShowingAndNotOccluded() {
249 return keyguardShowing && !keyguardOccluded;
250 }
251 }
252}