blob: 6dddf1832f6cf44396524ee5047b3c83b8e52e89 [file] [log] [blame]
Jorim Jaggi40db0292016-06-27 17:58:03 -07001/*
2 * Copyright (C) 2016 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.graphics.Rect;
20import android.view.View;
21
22import com.android.systemui.statusbar.policy.BatteryController;
23
24import static com.android.systemui.statusbar.phone.BarTransitions.MODE_LIGHTS_OUT_TRANSPARENT;
25import static com.android.systemui.statusbar.phone.BarTransitions.MODE_TRANSPARENT;
26
27/**
28 * Controls how light status bar flag applies to the icons.
29 */
30public class LightBarController implements BatteryController.BatteryStateChangeCallback {
31
32 private static final float NAV_BAR_INVERSION_SCRIM_ALPHA_THRESHOLD = 0.1f;
33
34 private final StatusBarIconController mStatusBarIconController;
35 private final BatteryController mBatteryController;
36 private FingerprintUnlockController mFingerprintUnlockController;
Jorim Jaggi40db0292016-06-27 17:58:03 -070037
Jason Monk49fa0162017-01-11 09:21:56 -050038 private LightBarTransitionsController mNavigationBarController;
Jorim Jaggi40db0292016-06-27 17:58:03 -070039 private int mSystemUiVisibility;
40 private int mFullscreenStackVisibility;
41 private int mDockedStackVisibility;
42 private boolean mFullscreenLight;
43 private boolean mDockedLight;
44 private int mLastStatusBarMode;
45 private int mLastNavigationBarMode;
46 private boolean mNavigationLight;
47 private float mScrimAlpha;
48
49 private final Rect mLastFullscreenBounds = new Rect();
50 private final Rect mLastDockedBounds = new Rect();
51
52 public LightBarController(StatusBarIconController statusBarIconController,
Jorim Jaggi40db0292016-06-27 17:58:03 -070053 BatteryController batteryController) {
54 mStatusBarIconController = statusBarIconController;
Jorim Jaggi40db0292016-06-27 17:58:03 -070055 mBatteryController = batteryController;
56 batteryController.addCallback(this);
57 }
58
Jason Monk49fa0162017-01-11 09:21:56 -050059 public void setNavigationBar(LightBarTransitionsController navigationBar) {
60 mNavigationBarController = navigationBar;
61 }
62
Jorim Jaggi40db0292016-06-27 17:58:03 -070063 public void setFingerprintUnlockController(
64 FingerprintUnlockController fingerprintUnlockController) {
65 mFingerprintUnlockController = fingerprintUnlockController;
66 }
67
Jason Monk49fa0162017-01-11 09:21:56 -050068 public void onSystemUiVisibilityChanged(int fullscreenStackVis, int dockedStackVis,
Jorim Jaggi40db0292016-06-27 17:58:03 -070069 int mask, Rect fullscreenStackBounds, Rect dockedStackBounds, boolean sbModeChanged,
Jason Monk49fa0162017-01-11 09:21:56 -050070 int statusBarMode) {
Jorim Jaggi40db0292016-06-27 17:58:03 -070071 int oldFullscreen = mFullscreenStackVisibility;
72 int newFullscreen = (oldFullscreen & ~mask) | (fullscreenStackVis & mask);
73 int diffFullscreen = newFullscreen ^ oldFullscreen;
74 int oldDocked = mDockedStackVisibility;
75 int newDocked = (oldDocked & ~mask) | (dockedStackVis & mask);
76 int diffDocked = newDocked ^ oldDocked;
77 if ((diffFullscreen & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0
78 || (diffDocked & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0
79 || sbModeChanged
80 || !mLastFullscreenBounds.equals(fullscreenStackBounds)
81 || !mLastDockedBounds.equals(dockedStackBounds)) {
82
83 mFullscreenLight = isLight(newFullscreen, statusBarMode,
84 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
85 mDockedLight = isLight(newDocked, statusBarMode, View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
86 updateStatus(fullscreenStackBounds, dockedStackBounds);
87 }
88
Jason Monk49fa0162017-01-11 09:21:56 -050089 mFullscreenStackVisibility = newFullscreen;
90 mDockedStackVisibility = newDocked;
91 mLastStatusBarMode = statusBarMode;
92 mLastFullscreenBounds.set(fullscreenStackBounds);
93 mLastDockedBounds.set(dockedStackBounds);
94 }
95
96 public void onNavigationVisibilityChanged(int vis, int mask, boolean nbModeChanged,
97 int navigationBarMode) {
Jorim Jaggi40db0292016-06-27 17:58:03 -070098 int oldVis = mSystemUiVisibility;
99 int newVis = (oldVis & ~mask) | (vis & mask);
100 int diffVis = newVis ^ oldVis;
101 if ((diffVis & View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR) != 0
102 || nbModeChanged) {
103 boolean last = mNavigationLight;
104 mNavigationLight = isNavigationLight(newVis, navigationBarMode);
105 if (mNavigationLight != last) {
106 updateNavigation();
107 }
108 }
Jorim Jaggi40db0292016-06-27 17:58:03 -0700109 mSystemUiVisibility = newVis;
Jorim Jaggi40db0292016-06-27 17:58:03 -0700110 mLastNavigationBarMode = navigationBarMode;
Jorim Jaggi40db0292016-06-27 17:58:03 -0700111 }
112
113 private void reevaluate() {
Jason Monk49fa0162017-01-11 09:21:56 -0500114 onSystemUiVisibilityChanged(mFullscreenStackVisibility,
Jorim Jaggi40db0292016-06-27 17:58:03 -0700115 mDockedStackVisibility, 0 /* mask */, mLastFullscreenBounds, mLastDockedBounds,
Jason Monk49fa0162017-01-11 09:21:56 -0500116 true /* sbModeChange*/, mLastStatusBarMode);
117 onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */, true /* nbModeChanged */,
Jorim Jaggi40db0292016-06-27 17:58:03 -0700118 mLastNavigationBarMode);
119 }
120
121 public void setScrimAlpha(float alpha) {
122 mScrimAlpha = alpha;
123 reevaluate();
124 }
125
126 private boolean isNavigationLight(int vis, int barMode) {
127 return isLight(vis, barMode, View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR)
128 && mScrimAlpha < NAV_BAR_INVERSION_SCRIM_ALPHA_THRESHOLD;
129 }
130
131 private boolean isLight(int vis, int barMode, int flag) {
132 boolean isTransparentBar = (barMode == MODE_TRANSPARENT
133 || barMode == MODE_LIGHTS_OUT_TRANSPARENT);
134 boolean allowLight = isTransparentBar && !mBatteryController.isPowerSave();
135 boolean light = (vis & flag) != 0;
136 return allowLight && light;
137 }
138
139 private boolean animateChange() {
140 if (mFingerprintUnlockController == null) {
141 return false;
142 }
143 int unlockMode = mFingerprintUnlockController.getMode();
144 return unlockMode != FingerprintUnlockController.MODE_WAKE_AND_UNLOCK_PULSING
145 && unlockMode != FingerprintUnlockController.MODE_WAKE_AND_UNLOCK;
146 }
147
148 private void updateStatus(Rect fullscreenStackBounds, Rect dockedStackBounds) {
149 boolean hasDockedStack = !dockedStackBounds.isEmpty();
150
151 // If both are light or fullscreen is light and there is no docked stack, all icons get
152 // dark.
153 if ((mFullscreenLight && mDockedLight) || (mFullscreenLight && !hasDockedStack)) {
154 mStatusBarIconController.setIconsDarkArea(null);
155 mStatusBarIconController.getTransitionsController().setIconsDark(true, animateChange());
156
157 }
158
159 // If no one is light or the fullscreen is not light and there is no docked stack,
160 // all icons become white.
161 else if ((!mFullscreenLight && !mDockedLight) || (!mFullscreenLight && !hasDockedStack)) {
162 mStatusBarIconController.getTransitionsController().setIconsDark(
163 false, animateChange());
164 }
165
166 // Not the same for every stack, magic!
167 else {
168 Rect bounds = mFullscreenLight ? fullscreenStackBounds : dockedStackBounds;
169 if (bounds.isEmpty()) {
170 mStatusBarIconController.setIconsDarkArea(null);
171 } else {
172 mStatusBarIconController.setIconsDarkArea(bounds);
173 }
174 mStatusBarIconController.getTransitionsController().setIconsDark(true, animateChange());
175 }
176 }
177
178 private void updateNavigation() {
Jason Monk49fa0162017-01-11 09:21:56 -0500179 if (mNavigationBarController != null) {
180 mNavigationBarController.setIconsDark(
Jorim Jaggi40db0292016-06-27 17:58:03 -0700181 mNavigationLight, animateChange());
182 }
183 }
184
185 @Override
186 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
187
188 }
189
190 @Override
191 public void onPowerSaveChanged(boolean isPowerSave) {
192 reevaluate();
193 }
194}