• 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 com.android.launcher3.Utilities;
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_ALL_APPS = 1;
32     public static final int UI_STATE_WIDGET_BOTTOM_SHEET = 2;
33     public static final int UI_STATE_ROOT_VIEW = 3;
34 
35     public static final int FLAG_LIGHT_NAV = 1 << 0;
36     public static final int FLAG_DARK_NAV = 1 << 1;
37     public static final int FLAG_LIGHT_STATUS = 1 << 2;
38     public static final int FLAG_DARK_STATUS = 1 << 3;
39 
40     private final Window mWindow;
41     private final int[] mStates = new int[4];
42 
SystemUiController(Window window)43     public SystemUiController(Window window) {
44         mWindow = window;
45     }
46 
updateUiState(int uiState, boolean isLight)47     public void updateUiState(int uiState, boolean isLight) {
48         updateUiState(uiState, isLight
49                 ? (FLAG_LIGHT_NAV | FLAG_LIGHT_STATUS) : (FLAG_DARK_NAV | FLAG_DARK_STATUS));
50     }
51 
updateUiState(int uiState, int flags)52     public void updateUiState(int uiState, int flags) {
53         if (mStates[uiState] == flags) {
54             return;
55         }
56         mStates[uiState] = flags;
57 
58         int oldFlags = mWindow.getDecorView().getSystemUiVisibility();
59         // Apply the state flags in priority order
60         int newFlags = oldFlags;
61         for (int stateFlag : mStates) {
62             if (Utilities.ATLEAST_OREO) {
63                 if ((stateFlag & FLAG_LIGHT_NAV) != 0) {
64                     newFlags |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
65                 } else if ((stateFlag & FLAG_DARK_NAV) != 0) {
66                     newFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
67                 }
68             }
69 
70             if ((stateFlag & FLAG_LIGHT_STATUS) != 0) {
71                 newFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
72             } else if ((stateFlag & FLAG_DARK_STATUS) != 0) {
73                 newFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
74             }
75         }
76         if (newFlags != oldFlags) {
77             mWindow.getDecorView().setSystemUiVisibility(newFlags);
78         }
79     }
80 }
81