blob: e418ac5ad9b8a6a33e95b0f699ae0e8c3f30fcc8 [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;
84 mLp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
85 mLp.setTitle("StatusBar");
86 mLp.packageName = mContext.getPackageName();
87 mStatusBarView = statusBarView;
88 mBarHeight = barHeight;
89 mWindowManager.addView(mStatusBarView, mLp);
90 }
91
92 private void applyKeyguardFlags(State state) {
93 if (state.keyguardShowing) {
94 mLp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
95 mLp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
96 } else {
97 mLp.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
98 mLp.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
99 }
100 }
101
102 private void adjustScreenOrientation(State state) {
103 if (!state.isKeyguardShowingAndNotOccluded() || mKeyguardScreenRotation) {
104 mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
105 } else {
106 mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
107 }
108 }
109
110 private void applyFocusableFlag(State state) {
111 if (state.isKeyguardShowingAndNotOccluded() && state.keyguardNeedsInput) {
112 mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
113 mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
114 } else if (state.isKeyguardShowingAndNotOccluded() || state.statusBarFocusable) {
115 mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
116 mLp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
117 } else {
118 mLp.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
119 mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
120 }
121 }
122
123 private void applyHeight(State state) {
124 boolean expanded = state.isKeyguardShowingAndNotOccluded() || state.statusBarExpanded;
125 if (expanded) {
126 mLp.height = ViewGroup.LayoutParams.MATCH_PARENT;
127 } else {
128 mLp.height = mBarHeight;
129 }
130 }
131
132 private void applyUserActivityTimeout(State state) {
133 if (state.isKeyguardShowingAndNotOccluded()) {
134 mLp.userActivityTimeout = state.keyguardUserActivityTimeout;
135 } else {
136 mLp.userActivityTimeout = -1;
137 }
138 }
139
140 private void apply(State state) {
141 applyKeyguardFlags(state);
142 applyFocusableFlag(state);
143 adjustScreenOrientation(state);
144 applyHeight(state);
145 applyUserActivityTimeout(state);
146 mWindowManager.updateViewLayout(mStatusBarView, mLp);
147 }
148
149 public void setKeyguardShowing(boolean showing) {
150 mCurrentState.keyguardShowing = showing;
151 apply(mCurrentState);
152 }
153
154 public void setKeyguardOccluded(boolean occluded) {
155 mCurrentState.keyguardOccluded = occluded;
156 apply(mCurrentState);
157 }
158
159 public void setKeyguardNeedsInput(boolean needsInput) {
160 mCurrentState.keyguardNeedsInput = needsInput;
161 apply(mCurrentState);
162 }
163
164 public void setStatusBarExpanded(boolean expanded) {
165 mCurrentState.statusBarExpanded = expanded;
166 mCurrentState.statusBarFocusable = expanded;
167 apply(mCurrentState);
168 }
169
170 public void setStatusBarFocusable(boolean focusable) {
171 mCurrentState.statusBarFocusable = focusable;
172 apply(mCurrentState);
173 }
174
175 public void setKeyguardUserActivityTimeout(long timeout) {
176 mCurrentState.keyguardUserActivityTimeout = timeout;
177 apply(mCurrentState);
178 }
179
180 private static class State {
181 boolean keyguardShowing;
182 boolean keyguardOccluded;
183 boolean keyguardNeedsInput;
184 boolean statusBarExpanded;
185 boolean statusBarFocusable;
186 long keyguardUserActivityTimeout;
187
188 private boolean isKeyguardShowingAndNotOccluded() {
189 return keyguardShowing && !keyguardOccluded;
190 }
191 }
192}