• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.util.Arrays;
23 
24 /**
25  * Utility class to manage various window flags to control system UI.
26  */
27 public class SystemUiController {
28 
29     // Various UI states in increasing order of priority
30     public static final int UI_STATE_BASE_WINDOW = 0;
31     public static final int UI_STATE_SCRIM_VIEW = 1;
32     public static final int UI_STATE_WIDGET_BOTTOM_SHEET = 2;
33     public static final int UI_STATE_FULLSCREEN_TASK = 3;
34     public static final int UI_STATE_ALLAPPS = 4;
35 
36     public static final int FLAG_LIGHT_NAV = 1 << 0;
37     public static final int FLAG_DARK_NAV = 1 << 1;
38     public static final int FLAG_LIGHT_STATUS = 1 << 2;
39     public static final int FLAG_DARK_STATUS = 1 << 3;
40 
41     private final Window mWindow;
42     private final int[] mStates = new int[5];
43 
SystemUiController(Window window)44     public SystemUiController(Window window) {
45         mWindow = window;
46     }
47 
updateUiState(int uiState, boolean isLight)48     public void updateUiState(int uiState, boolean isLight) {
49         updateUiState(uiState, isLight
50                 ? (FLAG_LIGHT_NAV | FLAG_LIGHT_STATUS) : (FLAG_DARK_NAV | FLAG_DARK_STATUS));
51     }
52 
updateUiState(int uiState, int flags)53     public void updateUiState(int uiState, int flags) {
54         if (mStates[uiState] == flags) {
55             return;
56         }
57         mStates[uiState] = flags;
58 
59         int oldFlags = mWindow.getDecorView().getSystemUiVisibility();
60         // Apply the state flags in priority order
61         int newFlags = oldFlags;
62         for (int stateFlag : mStates) {
63             newFlags = getSysUiVisibilityFlags(stateFlag, newFlags);
64         }
65         if (newFlags != oldFlags) {
66             mWindow.getDecorView().setSystemUiVisibility(newFlags);
67         }
68     }
69 
70     /**
71      * Returns the sysui visibility for the base layer
72      */
getBaseSysuiVisibility()73     public int getBaseSysuiVisibility() {
74         return getSysUiVisibilityFlags(
75                 mStates[UI_STATE_BASE_WINDOW], mWindow.getDecorView().getSystemUiVisibility());
76     }
77 
getSysUiVisibilityFlags(int stateFlag, int currentVisibility)78     private int getSysUiVisibilityFlags(int stateFlag, int currentVisibility) {
79         if ((stateFlag & FLAG_LIGHT_NAV) != 0) {
80             currentVisibility |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
81         } else if ((stateFlag & FLAG_DARK_NAV) != 0) {
82             currentVisibility &= ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
83         }
84 
85         if ((stateFlag & FLAG_LIGHT_STATUS) != 0) {
86             currentVisibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
87         } else if ((stateFlag & FLAG_DARK_STATUS) != 0) {
88             currentVisibility &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
89         }
90         return currentVisibility;
91     }
92 
93     @Override
toString()94     public String toString() {
95         return "mStates=" + Arrays.toString(mStates);
96     }
97 }
98