• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2025 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 package com.android.systemui.model
17 
18 import android.view.Display
19 import com.android.systemui.dagger.SysUISingleton
20 import com.android.systemui.shade.shared.flag.ShadeWindowGoesAround
21 import java.util.concurrent.CopyOnWriteArrayList
22 import javax.inject.Inject
23 
24 /**
25  * Channels changes from several [SysUiState]s to a single callback.
26  *
27  * There are several [SysUiState]s (one per display). This class allows for listeners to listen to
28  * sysui state updates from any of those [SysUiState] instances.
29  *
30  *                      ┌────────────────────┐
31  *                      │ SysUIStateOverride │
32  *                      │ displayId=2        │
33  *                      └┬───────────────────┘
34  *                       │  ▲
35  * ┌───────────────┐     │  │  ┌────────────────────┐
36  * │ SysUIState    │     │  │  │ SysUIStateOverride │
37  * │ displayId=0   │     │  │  │ displayId=1        │
38  * └────────────┬──┘     │  │  └┬───────────────────┘
39  *              │        │  │   │ ▲
40  *              ▼        ▼  │   ▼ │
41  *            ┌─────────────┴─────┴─┐
42  *            │SysUiStateDispatcher │
43  *            └────────┬────────────┘
44  *                     │
45  *                     ▼
46  *             ┌──────────────────┐
47  *             │ listeners for    │
48  *             │ all displays     │
49  *             └──────────────────┘
50  */
51 @SysUISingleton
52 class SysUIStateDispatcher @Inject constructor() {
53 
54     private val listeners = CopyOnWriteArrayList<SysUiState.SysUiStateCallback>()
55 
56     /** Called from each [SysUiState] to propagate new state changes. */
57     fun dispatchSysUIStateChange(sysUiFlags: Long, displayId: Int) {
58         if (displayId != Display.DEFAULT_DISPLAY && !ShadeWindowGoesAround.isEnabled) return
59         listeners.forEach { listener ->
60             listener.onSystemUiStateChanged(sysUiFlags = sysUiFlags, displayId = displayId)
61         }
62     }
63 
64     /**
65      * Registers a listener to listen for system UI state changes.
66      *
67      * Listeners will have [SysUiState.SysUiStateCallback.onSystemUiStateChanged] called whenever a
68      * system UI state changes.
69      */
70     fun registerListener(listener: SysUiState.SysUiStateCallback) {
71         listeners += listener
72     }
73 
74     fun unregisterListener(listener: SysUiState.SysUiStateCallback) {
75         listeners -= listener
76     }
77 }
78