blob: d175d7ac57f34677531d2d4e0d91dc1b5e46a764 [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
19import android.app.ActionBar;
20import android.content.Context;
21import android.content.pm.ActivityInfo;
22import android.content.res.Resources;
23import android.graphics.PixelFormat;
24import android.os.SystemProperties;
25import android.view.Gravity;
26import android.view.View;
27import android.view.ViewGroup;
28import android.view.WindowManager;
29
30import com.android.keyguard.R;
31
32/**
33 * Encapsulates all logic for the status bar window state management.
34 */
35public class StatusBarWindowManager {
36
37 private final Context mContext;
38 private final WindowManager mWindowManager;
39 private View mStatusBarView;
40 private WindowManager.LayoutParams mLp;
41 private int mBarHeight;
42 private final boolean mKeyguardScreenRotation;
43
44 private final State mCurrentState = new State();
45
46 public StatusBarWindowManager(Context context) {
47 mContext = context;
48 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
49 mKeyguardScreenRotation = shouldEnableKeyguardScreenRotation();
50 }
51
52 private boolean shouldEnableKeyguardScreenRotation() {
53 Resources res = mContext.getResources();
54 return SystemProperties.getBoolean("lockscreen.rot_override", false)
55 || res.getBoolean(R.bool.config_enableLockScreenRotation);
56 }
57
58 /**
59 * Adds the status bar view to the window manager.
60 *
61 * @param statusBarView The view to add.
62 * @param barHeight The height of the status bar in collapsed state.
63 */
64 public void add(View statusBarView, int barHeight) {
65
66 // Now that the status bar window encompasses the sliding panel and its
67 // translucent backdrop, the entire thing is made TRANSLUCENT and is
68 // hardware-accelerated.
69 mLp = new WindowManager.LayoutParams(
70 ViewGroup.LayoutParams.MATCH_PARENT,
71 barHeight,
72 WindowManager.LayoutParams.TYPE_STATUS_BAR,
73 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
74 | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
75 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
76 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
77 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
78 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
79 PixelFormat.TRANSLUCENT);
80
81 mLp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
82 mLp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL;
83 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);
89 }
90
91 private void applyKeyguardFlags(State state) {
92 if (state.keyguardShowing) {
93 mLp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
94 mLp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
95 } else {
96 mLp.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
97 mLp.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
98 }
99 }
100
101 private void adjustScreenOrientation(State state) {
Jorim Jaggica36cf72014-04-17 20:36:15 +0200102 if (state.isKeyguardShowingAndNotOccluded()) {
103 if (mKeyguardScreenRotation) {
104 mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
105 } else {
106 mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
107 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100108 } else {
Jorim Jaggica36cf72014-04-17 20:36:15 +0200109 mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100110 }
111 }
112
113 private void applyFocusableFlag(State state) {
114 if (state.isKeyguardShowingAndNotOccluded() && state.keyguardNeedsInput) {
115 mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
116 mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
117 } else if (state.isKeyguardShowingAndNotOccluded() || state.statusBarFocusable) {
118 mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
119 mLp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
120 } else {
121 mLp.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
122 mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
123 }
124 }
125
126 private void applyHeight(State state) {
127 boolean expanded = state.isKeyguardShowingAndNotOccluded() || state.statusBarExpanded;
128 if (expanded) {
129 mLp.height = ViewGroup.LayoutParams.MATCH_PARENT;
130 } else {
131 mLp.height = mBarHeight;
132 }
133 }
134
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200135 private void applyFitsSystemWindows(State state) {
136 mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
137 }
138
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100139 private void applyUserActivityTimeout(State state) {
140 if (state.isKeyguardShowingAndNotOccluded()) {
141 mLp.userActivityTimeout = state.keyguardUserActivityTimeout;
142 } else {
143 mLp.userActivityTimeout = -1;
144 }
145 }
146
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200147 private void applyInputFeatures(State state) {
148 if (state.isKeyguardShowingAndNotOccluded()) {
149 mLp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
150 } else {
151 mLp.inputFeatures &= ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
152 }
153 }
154
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100155 private void apply(State state) {
156 applyKeyguardFlags(state);
157 applyFocusableFlag(state);
158 adjustScreenOrientation(state);
159 applyHeight(state);
160 applyUserActivityTimeout(state);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200161 applyInputFeatures(state);
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200162 applyFitsSystemWindows(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100163 mWindowManager.updateViewLayout(mStatusBarView, mLp);
164 }
165
166 public void setKeyguardShowing(boolean showing) {
167 mCurrentState.keyguardShowing = showing;
168 apply(mCurrentState);
169 }
170
171 public void setKeyguardOccluded(boolean occluded) {
172 mCurrentState.keyguardOccluded = occluded;
173 apply(mCurrentState);
174 }
175
176 public void setKeyguardNeedsInput(boolean needsInput) {
177 mCurrentState.keyguardNeedsInput = needsInput;
178 apply(mCurrentState);
179 }
180
181 public void setStatusBarExpanded(boolean expanded) {
182 mCurrentState.statusBarExpanded = expanded;
183 mCurrentState.statusBarFocusable = expanded;
184 apply(mCurrentState);
185 }
186
187 public void setStatusBarFocusable(boolean focusable) {
188 mCurrentState.statusBarFocusable = focusable;
189 apply(mCurrentState);
190 }
191
192 public void setKeyguardUserActivityTimeout(long timeout) {
193 mCurrentState.keyguardUserActivityTimeout = timeout;
194 apply(mCurrentState);
195 }
196
197 private static class State {
198 boolean keyguardShowing;
199 boolean keyguardOccluded;
200 boolean keyguardNeedsInput;
201 boolean statusBarExpanded;
202 boolean statusBarFocusable;
203 long keyguardUserActivityTimeout;
204
205 private boolean isKeyguardShowingAndNotOccluded() {
206 return keyguardShowing && !keyguardOccluded;
207 }
208 }
209}