blob: 716e326d5911e6e6102bfcd058687ee8f526c6e7 [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) {
102 if (!state.isKeyguardShowingAndNotOccluded() || mKeyguardScreenRotation) {
103 mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
104 } else {
105 mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
106 }
107 }
108
109 private void applyFocusableFlag(State state) {
110 if (state.isKeyguardShowingAndNotOccluded() && state.keyguardNeedsInput) {
111 mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
112 mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
113 } else if (state.isKeyguardShowingAndNotOccluded() || state.statusBarFocusable) {
114 mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
115 mLp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
116 } else {
117 mLp.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
118 mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
119 }
120 }
121
122 private void applyHeight(State state) {
123 boolean expanded = state.isKeyguardShowingAndNotOccluded() || state.statusBarExpanded;
124 if (expanded) {
125 mLp.height = ViewGroup.LayoutParams.MATCH_PARENT;
126 } else {
127 mLp.height = mBarHeight;
128 }
129 }
130
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200131 private void applyFitsSystemWindows(State state) {
132 mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
133 }
134
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100135 private void applyUserActivityTimeout(State state) {
136 if (state.isKeyguardShowingAndNotOccluded()) {
137 mLp.userActivityTimeout = state.keyguardUserActivityTimeout;
138 } else {
139 mLp.userActivityTimeout = -1;
140 }
141 }
142
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200143 private void applyInputFeatures(State state) {
144 if (state.isKeyguardShowingAndNotOccluded()) {
145 mLp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
146 } else {
147 mLp.inputFeatures &= ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
148 }
149 }
150
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100151 private void apply(State state) {
152 applyKeyguardFlags(state);
153 applyFocusableFlag(state);
154 adjustScreenOrientation(state);
155 applyHeight(state);
156 applyUserActivityTimeout(state);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200157 applyInputFeatures(state);
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200158 applyFitsSystemWindows(state);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100159 mWindowManager.updateViewLayout(mStatusBarView, mLp);
160 }
161
162 public void setKeyguardShowing(boolean showing) {
163 mCurrentState.keyguardShowing = showing;
164 apply(mCurrentState);
165 }
166
167 public void setKeyguardOccluded(boolean occluded) {
168 mCurrentState.keyguardOccluded = occluded;
169 apply(mCurrentState);
170 }
171
172 public void setKeyguardNeedsInput(boolean needsInput) {
173 mCurrentState.keyguardNeedsInput = needsInput;
174 apply(mCurrentState);
175 }
176
177 public void setStatusBarExpanded(boolean expanded) {
178 mCurrentState.statusBarExpanded = expanded;
179 mCurrentState.statusBarFocusable = expanded;
180 apply(mCurrentState);
181 }
182
183 public void setStatusBarFocusable(boolean focusable) {
184 mCurrentState.statusBarFocusable = focusable;
185 apply(mCurrentState);
186 }
187
188 public void setKeyguardUserActivityTimeout(long timeout) {
189 mCurrentState.keyguardUserActivityTimeout = timeout;
190 apply(mCurrentState);
191 }
192
193 private static class State {
194 boolean keyguardShowing;
195 boolean keyguardOccluded;
196 boolean keyguardNeedsInput;
197 boolean statusBarExpanded;
198 boolean statusBarFocusable;
199 long keyguardUserActivityTimeout;
200
201 private boolean isKeyguardShowingAndNotOccluded() {
202 return keyguardShowing && !keyguardOccluded;
203 }
204 }
205}