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 21 import androidx.annotation.IntDef; 22 23 import java.lang.annotation.Retention; 24 import java.lang.annotation.RetentionPolicy; 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_SCRIM_VIEW = 1; 35 public static final int UI_STATE_WIDGET_BOTTOM_SHEET = 2; 36 public static final int UI_STATE_FULLSCREEN_TASK = 3; 37 public static final int UI_STATE_ALL_APPS = 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 /** 45 * Security type based on WifiConfiguration.KeyMgmt 46 */ 47 @Retention(RetentionPolicy.SOURCE) 48 @IntDef(flag = true, value = { 49 FLAG_LIGHT_NAV, 50 FLAG_DARK_NAV, 51 FLAG_LIGHT_STATUS, 52 FLAG_DARK_STATUS, 53 }) 54 public @interface SystemUiControllerFlags {} 55 56 private final View mView; 57 private final int[] mStates = new int[5]; 58 SystemUiController(View view)59 public SystemUiController(View view) { 60 mView = view; 61 } 62 updateUiState(int uiState, boolean isLight)63 public void updateUiState(int uiState, boolean isLight) { 64 updateUiState(uiState, isLight 65 ? (FLAG_LIGHT_NAV | FLAG_LIGHT_STATUS) : (FLAG_DARK_NAV | FLAG_DARK_STATUS)); 66 } 67 updateUiState(int uiState, @SystemUiControllerFlags int flags)68 public void updateUiState(int uiState, @SystemUiControllerFlags int flags) { 69 if (mStates[uiState] == flags) { 70 return; 71 } 72 mStates[uiState] = flags; 73 74 int oldFlags = mView.getSystemUiVisibility(); 75 // Apply the state flags in priority order 76 int newFlags = oldFlags; 77 for (int stateFlag : mStates) { 78 newFlags = getSysUiVisibilityFlags(stateFlag, newFlags); 79 } 80 if (newFlags != oldFlags) { 81 mView.setSystemUiVisibility(newFlags); 82 } 83 } 84 85 /** 86 * Returns the sysui visibility for the base layer 87 */ getBaseSysuiVisibility()88 public int getBaseSysuiVisibility() { 89 return getSysUiVisibilityFlags( 90 mStates[UI_STATE_BASE_WINDOW], mView.getSystemUiVisibility()); 91 } 92 getSysUiVisibilityFlags(int stateFlag, int currentVisibility)93 private int getSysUiVisibilityFlags(int stateFlag, int currentVisibility) { 94 if ((stateFlag & FLAG_LIGHT_NAV) != 0) { 95 currentVisibility |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; 96 } else if ((stateFlag & FLAG_DARK_NAV) != 0) { 97 currentVisibility &= ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); 98 } 99 100 if ((stateFlag & FLAG_LIGHT_STATUS) != 0) { 101 currentVisibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; 102 } else if ((stateFlag & FLAG_DARK_STATUS) != 0) { 103 currentVisibility &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); 104 } 105 return currentVisibility; 106 } 107 108 @Override toString()109 public String toString() { 110 return "mStates=" + Arrays.toString(mStates); 111 } 112 } 113