1 /*
2  * Copyright (C) 2017 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.launcher3.util;
18 
19 import android.view.View;
20 import android.view.Window;
21 
22 import androidx.annotation.IntDef;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.util.Arrays;
27 
28 /**
29  * Utility class to manage various window flags to control system UI.
30  */
31 public class SystemUiController {
32 
33     // Various UI states in increasing order of priority
34     public static final int UI_STATE_BASE_WINDOW = 0;
35     public static final int UI_STATE_SCRIM_VIEW = 1;
36     public static final int UI_STATE_WIDGET_BOTTOM_SHEET = 2;
37     public static final int UI_STATE_FULLSCREEN_TASK = 3;
38     public static final int UI_STATE_ALL_APPS = 4;
39 
40     public static final int FLAG_LIGHT_NAV = 1 << 0;
41     public static final int FLAG_DARK_NAV = 1 << 1;
42     public static final int FLAG_LIGHT_STATUS = 1 << 2;
43     public static final int FLAG_DARK_STATUS = 1 << 3;
44 
45     /**
46      * Security type based on WifiConfiguration.KeyMgmt
47      */
48     @Retention(RetentionPolicy.SOURCE)
49     @IntDef(flag = true, value = {
50             FLAG_LIGHT_NAV,
51             FLAG_DARK_NAV,
52             FLAG_LIGHT_STATUS,
53             FLAG_DARK_STATUS,
54     })
55     public @interface SystemUiControllerFlags {}
56 
57     private final Window mWindow;
58     private final int[] mStates = new int[5];
59 
SystemUiController(Window window)60     public SystemUiController(Window window) {
61         mWindow = window;
62     }
63 
updateUiState(int uiState, boolean isLight)64     public void updateUiState(int uiState, boolean isLight) {
65         updateUiState(uiState, isLight
66                 ? (FLAG_LIGHT_NAV | FLAG_LIGHT_STATUS) : (FLAG_DARK_NAV | FLAG_DARK_STATUS));
67     }
68 
updateUiState(int uiState, @SystemUiControllerFlags int flags)69     public void updateUiState(int uiState, @SystemUiControllerFlags int flags) {
70         if (mStates[uiState] == flags) {
71             return;
72         }
73         mStates[uiState] = flags;
74 
75         int oldFlags = mWindow.getDecorView().getSystemUiVisibility();
76         // Apply the state flags in priority order
77         int newFlags = oldFlags;
78         for (int stateFlag : mStates) {
79             newFlags = getSysUiVisibilityFlags(stateFlag, newFlags);
80         }
81         if (newFlags != oldFlags) {
82             mWindow.getDecorView().setSystemUiVisibility(newFlags);
83         }
84     }
85 
86     /**
87      * Returns the sysui visibility for the base layer
88      */
getBaseSysuiVisibility()89     public int getBaseSysuiVisibility() {
90         return getSysUiVisibilityFlags(
91                 mStates[UI_STATE_BASE_WINDOW], mWindow.getDecorView().getSystemUiVisibility());
92     }
93 
getSysUiVisibilityFlags(int stateFlag, int currentVisibility)94     private int getSysUiVisibilityFlags(int stateFlag, int currentVisibility) {
95         if ((stateFlag & FLAG_LIGHT_NAV) != 0) {
96             currentVisibility |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
97         } else if ((stateFlag & FLAG_DARK_NAV) != 0) {
98             currentVisibility &= ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
99         }
100 
101         if ((stateFlag & FLAG_LIGHT_STATUS) != 0) {
102             currentVisibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
103         } else if ((stateFlag & FLAG_DARK_STATUS) != 0) {
104             currentVisibility &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
105         }
106         return currentVisibility;
107     }
108 
109     @Override
toString()110     public String toString() {
111         return "mStates=" + Arrays.toString(mStates);
112     }
113 }
114