1 /* 2 * Copyright (C) 2021 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.systemui.statusbar.window 18 19 import android.app.StatusBarManager 20 import android.app.StatusBarManager.WindowVisibleState 21 import android.app.StatusBarManager.WINDOW_STATE_SHOWING 22 import android.app.StatusBarManager.WINDOW_STATUS_BAR 23 import android.app.StatusBarManager.windowStateToString 24 import android.util.Log 25 import com.android.systemui.dagger.SysUISingleton 26 import com.android.systemui.dagger.qualifiers.DisplayId 27 import com.android.systemui.statusbar.CommandQueue 28 import com.android.systemui.statusbar.phone.CentralSurfaces 29 import javax.inject.Inject 30 31 /** 32 * A centralized class maintaining the state of the status bar window. 33 * 34 * Classes that want to get updates about the status bar window state should subscribe to this class 35 * via [addListener] and should NOT add their own callback on [CommandQueue]. 36 */ 37 @SysUISingleton 38 class StatusBarWindowStateController @Inject constructor( 39 @DisplayId private val thisDisplayId: Int, 40 commandQueue: CommandQueue 41 ) { 42 private val commandQueueCallback = object : CommandQueue.Callbacks { setWindowStatenull43 override fun setWindowState( 44 displayId: Int, 45 @StatusBarManager.WindowType window: Int, 46 @WindowVisibleState state: Int 47 ) { 48 this@StatusBarWindowStateController.setWindowState(displayId, window, state) 49 } 50 } 51 private val listeners: MutableSet<StatusBarWindowStateListener> = HashSet() 52 53 @WindowVisibleState private var windowState: Int = WINDOW_STATE_SHOWING 54 55 init { 56 commandQueue.addCallback(commandQueueCallback) 57 } 58 59 /** Adds a listener. */ addListenernull60 fun addListener(listener: StatusBarWindowStateListener) { 61 listeners.add(listener) 62 } 63 removeListenernull64 fun removeListener(listener: StatusBarWindowStateListener) { 65 listeners.remove(listener) 66 } 67 68 /** Returns true if the window is currently showing. */ windowIsShowingnull69 fun windowIsShowing() = windowState == WINDOW_STATE_SHOWING 70 71 private fun setWindowState( 72 displayId: Int, 73 @StatusBarManager.WindowType window: Int, 74 @WindowVisibleState state: Int 75 ) { 76 if (displayId != thisDisplayId) { 77 return 78 } 79 if (window != WINDOW_STATUS_BAR) { 80 return 81 } 82 if (windowState == state) { 83 return 84 } 85 86 windowState = state 87 if (CentralSurfaces.DEBUG_WINDOW_STATE) { 88 Log.d(CentralSurfaces.TAG, "Status bar " + windowStateToString(state)) 89 } 90 listeners.forEach { it.onStatusBarWindowStateChanged(state) } 91 } 92 } 93