Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.pm.ActivityInfo; |
| 21 | import android.content.res.Resources; |
| 22 | import android.graphics.PixelFormat; |
| 23 | import android.os.SystemProperties; |
| 24 | import android.view.Gravity; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.view.WindowManager; |
| 28 | |
| 29 | import com.android.keyguard.R; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 30 | import com.android.systemui.statusbar.BaseStatusBar; |
| 31 | import com.android.systemui.statusbar.StatusBarState; |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Encapsulates all logic for the status bar window state management. |
| 35 | */ |
| 36 | public class StatusBarWindowManager { |
| 37 | |
| 38 | private final Context mContext; |
| 39 | private final WindowManager mWindowManager; |
| 40 | private View mStatusBarView; |
| 41 | private WindowManager.LayoutParams mLp; |
| 42 | private int mBarHeight; |
| 43 | private final boolean mKeyguardScreenRotation; |
| 44 | |
| 45 | private final State mCurrentState = new State(); |
| 46 | |
| 47 | public StatusBarWindowManager(Context context) { |
| 48 | mContext = context; |
| 49 | mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 50 | mKeyguardScreenRotation = shouldEnableKeyguardScreenRotation(); |
| 51 | } |
| 52 | |
| 53 | private boolean shouldEnableKeyguardScreenRotation() { |
| 54 | Resources res = mContext.getResources(); |
| 55 | return SystemProperties.getBoolean("lockscreen.rot_override", false) |
| 56 | || res.getBoolean(R.bool.config_enableLockScreenRotation); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Adds the status bar view to the window manager. |
| 61 | * |
| 62 | * @param statusBarView The view to add. |
| 63 | * @param barHeight The height of the status bar in collapsed state. |
| 64 | */ |
| 65 | public void add(View statusBarView, int barHeight) { |
| 66 | |
| 67 | // Now that the status bar window encompasses the sliding panel and its |
| 68 | // translucent backdrop, the entire thing is made TRANSLUCENT and is |
| 69 | // hardware-accelerated. |
| 70 | mLp = new WindowManager.LayoutParams( |
| 71 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 72 | barHeight, |
| 73 | WindowManager.LayoutParams.TYPE_STATUS_BAR, |
| 74 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 75 | | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING |
| 76 | | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH |
| 77 | | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
| 78 | | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION |
| 79 | | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, |
| 80 | PixelFormat.TRANSLUCENT); |
| 81 | |
| 82 | mLp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; |
| 83 | mLp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL; |
| 84 | mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE; |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 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) { |
Jorim Jaggi | ca36cf7 | 2014-04-17 20:36:15 +0200 | [diff] [blame] | 103 | if (state.isKeyguardShowingAndNotOccluded()) { |
| 104 | if (mKeyguardScreenRotation) { |
| 105 | mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER; |
| 106 | } else { |
| 107 | mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR; |
| 108 | } |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 109 | } else { |
Jorim Jaggi | ca36cf7 | 2014-04-17 20:36:15 +0200 | [diff] [blame] | 110 | mLp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | private void applyFocusableFlag(State state) { |
| 115 | if (state.isKeyguardShowingAndNotOccluded() && state.keyguardNeedsInput) { |
| 116 | mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| 117 | mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 118 | } else if (state.isKeyguardShowingAndNotOccluded() || state.statusBarFocusable) { |
| 119 | mLp.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| 120 | mLp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 121 | } else { |
| 122 | mLp.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| 123 | mLp.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | private void applyHeight(State state) { |
| 128 | boolean expanded = state.isKeyguardShowingAndNotOccluded() || state.statusBarExpanded; |
| 129 | if (expanded) { |
| 130 | mLp.height = ViewGroup.LayoutParams.MATCH_PARENT; |
| 131 | } else { |
| 132 | mLp.height = mBarHeight; |
| 133 | } |
| 134 | } |
| 135 | |
Jorim Jaggi | a005f1b | 2014-04-16 19:06:10 +0200 | [diff] [blame] | 136 | private void applyFitsSystemWindows(State state) { |
| 137 | mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded()); |
| 138 | } |
| 139 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 140 | private void applyUserActivityTimeout(State state) { |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 141 | if (state.isKeyguardShowingAndNotOccluded() |
| 142 | && state.statusBarState == StatusBarState.KEYGUARD) { |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 143 | mLp.userActivityTimeout = state.keyguardUserActivityTimeout; |
| 144 | } else { |
| 145 | mLp.userActivityTimeout = -1; |
| 146 | } |
| 147 | } |
| 148 | |
Jorim Jaggi | 03c701e | 2014-04-02 12:39:51 +0200 | [diff] [blame] | 149 | private void applyInputFeatures(State state) { |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame^] | 150 | if (state.isKeyguardShowingAndNotOccluded() |
| 151 | && state.statusBarState == StatusBarState.KEYGUARD) { |
Jorim Jaggi | 03c701e | 2014-04-02 12:39:51 +0200 | [diff] [blame] | 152 | mLp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY; |
| 153 | } else { |
| 154 | mLp.inputFeatures &= ~WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY; |
| 155 | } |
| 156 | } |
| 157 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 158 | private void apply(State state) { |
| 159 | applyKeyguardFlags(state); |
| 160 | applyFocusableFlag(state); |
| 161 | adjustScreenOrientation(state); |
| 162 | applyHeight(state); |
| 163 | applyUserActivityTimeout(state); |
Jorim Jaggi | 03c701e | 2014-04-02 12:39:51 +0200 | [diff] [blame] | 164 | applyInputFeatures(state); |
Jorim Jaggi | a005f1b | 2014-04-16 19:06:10 +0200 | [diff] [blame] | 165 | applyFitsSystemWindows(state); |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 166 | mWindowManager.updateViewLayout(mStatusBarView, mLp); |
| 167 | } |
| 168 | |
| 169 | public void setKeyguardShowing(boolean showing) { |
| 170 | mCurrentState.keyguardShowing = showing; |
| 171 | apply(mCurrentState); |
| 172 | } |
| 173 | |
| 174 | public void setKeyguardOccluded(boolean occluded) { |
| 175 | mCurrentState.keyguardOccluded = occluded; |
| 176 | apply(mCurrentState); |
| 177 | } |
| 178 | |
| 179 | public void setKeyguardNeedsInput(boolean needsInput) { |
| 180 | mCurrentState.keyguardNeedsInput = needsInput; |
| 181 | apply(mCurrentState); |
| 182 | } |
| 183 | |
| 184 | public void setStatusBarExpanded(boolean expanded) { |
| 185 | mCurrentState.statusBarExpanded = expanded; |
| 186 | mCurrentState.statusBarFocusable = expanded; |
| 187 | apply(mCurrentState); |
| 188 | } |
| 189 | |
| 190 | public void setStatusBarFocusable(boolean focusable) { |
| 191 | mCurrentState.statusBarFocusable = focusable; |
| 192 | apply(mCurrentState); |
| 193 | } |
| 194 | |
| 195 | public void setKeyguardUserActivityTimeout(long timeout) { |
| 196 | mCurrentState.keyguardUserActivityTimeout = timeout; |
| 197 | apply(mCurrentState); |
| 198 | } |
| 199 | |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 200 | /** |
| 201 | * @param state The {@link StatusBarState} of the status bar. |
| 202 | */ |
| 203 | public void setStatusBarState(int state) { |
| 204 | mCurrentState.statusBarState = state; |
| 205 | apply(mCurrentState); |
| 206 | } |
| 207 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 208 | private static class State { |
| 209 | boolean keyguardShowing; |
| 210 | boolean keyguardOccluded; |
| 211 | boolean keyguardNeedsInput; |
| 212 | boolean statusBarExpanded; |
| 213 | boolean statusBarFocusable; |
| 214 | long keyguardUserActivityTimeout; |
| 215 | |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 216 | /** |
| 217 | * The {@link BaseStatusBar} state from the status bar. |
| 218 | */ |
| 219 | int statusBarState; |
| 220 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 221 | private boolean isKeyguardShowingAndNotOccluded() { |
| 222 | return keyguardShowing && !keyguardOccluded; |
| 223 | } |
| 224 | } |
| 225 | } |