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.text.TextUtils;
20 import android.view.View;
21 import android.view.Window;
22 
23 import com.android.launcher3.Utilities;
24 
25 import java.util.Arrays;
26 
27 /**
28  * Utility class to manage various window flags to control system UI.
29  */
30 public class SystemUiController {
31 
32     // Various UI states in increasing order of priority
33     public static final int UI_STATE_BASE_WINDOW = 0;
34     public static final int UI_STATE_ALL_APPS = 1;
35     public static final int UI_STATE_WIDGET_BOTTOM_SHEET = 2;
36     public static final int UI_STATE_ROOT_VIEW = 3;
37     public static final int UI_STATE_OVERVIEW = 4;
38 
39     public static final int FLAG_LIGHT_NAV = 1 << 0;
40     public static final int FLAG_DARK_NAV = 1 << 1;
41     public static final int FLAG_LIGHT_STATUS = 1 << 2;
42     public static final int FLAG_DARK_STATUS = 1 << 3;
43 
44     private final Window mWindow;
45     private final int[] mStates = new int[5];
46 
SystemUiController(Window window)47     public SystemUiController(Window window) {
48         mWindow = window;
49     }
50 
updateUiState(int uiState, boolean isLight)51     public void updateUiState(int uiState, boolean isLight) {
52         updateUiState(uiState, isLight
53                 ? (FLAG_LIGHT_NAV | FLAG_LIGHT_STATUS) : (FLAG_DARK_NAV | FLAG_DARK_STATUS));
54     }
55 
updateUiState(int uiState, int flags)56     public void updateUiState(int uiState, int flags) {
57         if (mStates[uiState] == flags) {
58             return;
59         }
60         mStates[uiState] = flags;
61 
62         int oldFlags = mWindow.getDecorView().getSystemUiVisibility();
63         // Apply the state flags in priority order
64         int newFlags = oldFlags;
65         for (int stateFlag : mStates) {
66             if (Utilities.ATLEAST_OREO) {
67                 if ((stateFlag & FLAG_LIGHT_NAV) != 0) {
68                     newFlags |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
69                 } else if ((stateFlag & FLAG_DARK_NAV) != 0) {
70                     newFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
71                 }
72             }
73 
74             if ((stateFlag & FLAG_LIGHT_STATUS) != 0) {
75                 newFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
76             } else if ((stateFlag & FLAG_DARK_STATUS) != 0) {
77                 newFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
78             }
79         }
80         if (newFlags != oldFlags) {
81             mWindow.getDecorView().setSystemUiVisibility(newFlags);
82         }
83     }
84 
85     @Override
toString()86     public String toString() {
87         return "mStates=" + Arrays.toString(mStates);
88     }
89 }
90