1 /*
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 
17 package com.android.systemui.statusbar.phone;
18 
19 import android.graphics.Rect;
20 import android.view.View;
21 
22 import com.android.systemui.statusbar.policy.BatteryController;
23 
24 import static com.android.systemui.statusbar.phone.BarTransitions.MODE_LIGHTS_OUT_TRANSPARENT;
25 import static com.android.systemui.statusbar.phone.BarTransitions.MODE_TRANSPARENT;
26 
27 /**
28  * Controls how light status bar flag applies to the icons.
29  */
30 public class LightStatusBarController {
31 
32     private final StatusBarIconController mIconController;
33     private final BatteryController mBatteryController;
34     private FingerprintUnlockController mFingerprintUnlockController;
35 
36     private int mFullscreenStackVisibility;
37     private int mDockedStackVisibility;
38     private boolean mFullscreenLight;
39     private boolean mDockedLight;
40 
41     private final Rect mLastFullscreenBounds = new Rect();
42     private final Rect mLastDockedBounds = new Rect();
43 
LightStatusBarController(StatusBarIconController iconController, BatteryController batteryController)44     public LightStatusBarController(StatusBarIconController iconController,
45             BatteryController batteryController) {
46         mIconController = iconController;
47         mBatteryController = batteryController;
48     }
49 
setFingerprintUnlockController( FingerprintUnlockController fingerprintUnlockController)50     public void setFingerprintUnlockController(
51             FingerprintUnlockController fingerprintUnlockController) {
52         mFingerprintUnlockController = fingerprintUnlockController;
53     }
54 
onSystemUiVisibilityChanged(int fullscreenStackVis, int dockedStackVis, int mask, Rect fullscreenStackBounds, Rect dockedStackBounds, boolean sbModeChanged, int statusBarMode)55     public void onSystemUiVisibilityChanged(int fullscreenStackVis, int dockedStackVis, int mask,
56             Rect fullscreenStackBounds, Rect dockedStackBounds, boolean sbModeChanged,
57             int statusBarMode) {
58         int oldFullscreen = mFullscreenStackVisibility;
59         int newFullscreen = (oldFullscreen & ~mask) | (fullscreenStackVis & mask);
60         int diffFullscreen = newFullscreen ^ oldFullscreen;
61         int oldDocked = mDockedStackVisibility;
62         int newDocked = (oldDocked & ~mask) | (dockedStackVis & mask);
63         int diffDocked = newDocked ^ oldDocked;
64         if ((diffFullscreen & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0
65                 || (diffDocked & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0
66                 || sbModeChanged
67                 || !mLastFullscreenBounds.equals(fullscreenStackBounds)
68                 || !mLastDockedBounds.equals(dockedStackBounds)) {
69 
70             mFullscreenLight = isLight(newFullscreen, statusBarMode);
71             mDockedLight = isLight(newDocked, statusBarMode);
72             update(fullscreenStackBounds, dockedStackBounds);
73         }
74         mFullscreenStackVisibility = newFullscreen;
75         mDockedStackVisibility = newDocked;
76         mLastFullscreenBounds.set(fullscreenStackBounds);
77         mLastDockedBounds.set(dockedStackBounds);
78     }
79 
isLight(int vis, int statusBarMode)80     private boolean isLight(int vis, int statusBarMode) {
81         boolean isTransparentBar = (statusBarMode == MODE_TRANSPARENT
82                 || statusBarMode == MODE_LIGHTS_OUT_TRANSPARENT);
83         boolean allowLight = isTransparentBar && !mBatteryController.isPowerSave();
84         boolean light = (vis & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0;
85         return allowLight && light;
86     }
87 
animateChange()88     private boolean animateChange() {
89         if (mFingerprintUnlockController == null) {
90             return false;
91         }
92         int unlockMode = mFingerprintUnlockController.getMode();
93         return unlockMode != FingerprintUnlockController.MODE_WAKE_AND_UNLOCK_PULSING
94                 && unlockMode != FingerprintUnlockController.MODE_WAKE_AND_UNLOCK;
95     }
96 
update(Rect fullscreenStackBounds, Rect dockedStackBounds)97     private void update(Rect fullscreenStackBounds, Rect dockedStackBounds) {
98         boolean hasDockedStack = !dockedStackBounds.isEmpty();
99 
100         // If both are light or fullscreen is light and there is no docked stack, all icons get
101         // dark.
102         if ((mFullscreenLight && mDockedLight) || (mFullscreenLight && !hasDockedStack)) {
103             mIconController.setIconsDarkArea(null);
104             mIconController.setIconsDark(true, animateChange());
105 
106         }
107 
108         // If no one is light or the fullscreen is not light and there is no docked stack,
109         // all icons become white.
110         else if ((!mFullscreenLight && !mDockedLight) || (!mFullscreenLight && !hasDockedStack)) {
111             mIconController.setIconsDark(false, animateChange());
112 
113         }
114 
115         // Not the same for every stack, magic!
116         else {
117             Rect bounds = mFullscreenLight ? fullscreenStackBounds : dockedStackBounds;
118             if (bounds.isEmpty()) {
119                 mIconController.setIconsDarkArea(null);
120             } else {
121                 mIconController.setIconsDarkArea(bounds);
122             }
123             mIconController.setIconsDark(true, animateChange());
124         }
125     }
126 }
127