1 /*
<lambda>null2  * 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.phone
18 
19 import com.android.systemui.dagger.SysUISingleton
20 import com.android.systemui.statusbar.policy.CallbackController
21 import java.lang.ref.WeakReference
22 import javax.inject.Inject
23 
24 /**
25  * Publishes updates to the status bar's margins.
26  *
27  * While the status bar view consumes the entire width of the device, the status bar
28  * contents are laid out with margins for rounded corners, padding from the absolute
29  * edges, and potentially display cutouts in the corner.
30  */
31 @SysUISingleton
32 class StatusBarLocationPublisher @Inject constructor()
33 : CallbackController<StatusBarMarginUpdatedListener> {
34     private val listeners = mutableSetOf<WeakReference<StatusBarMarginUpdatedListener>>()
35 
36     var marginLeft: Int = 0
37         private set
38     var marginRight: Int = 0
39         private set
40 
41     override fun addCallback(listener: StatusBarMarginUpdatedListener) {
42         listeners.add(WeakReference(listener))
43     }
44 
45     override fun removeCallback(listener: StatusBarMarginUpdatedListener) {
46         var toRemove: WeakReference<StatusBarMarginUpdatedListener>? = null
47         for (l in listeners) {
48             if (l.get() == listener) {
49                 toRemove = l
50             }
51         }
52 
53         if (toRemove != null) {
54             listeners.remove(toRemove)
55         }
56     }
57 
58     fun updateStatusBarMargin(left: Int, right: Int) {
59         marginLeft = left
60         marginRight = right
61 
62         notifyListeners()
63     }
64 
65     private fun notifyListeners() {
66         var listenerList: List<WeakReference<StatusBarMarginUpdatedListener>>
67         synchronized(this) {
68             listenerList = listeners.toList()
69         }
70 
71         listenerList.forEach { wrapper ->
72             if (wrapper.get() == null) {
73                 listeners.remove(wrapper)
74             }
75 
76             wrapper.get()?.onStatusBarMarginUpdated(marginLeft, marginRight)
77         }
78     }
79 }
80 
81 interface StatusBarMarginUpdatedListener {
onStatusBarMarginUpdatednull82     fun onStatusBarMarginUpdated(marginLeft: Int, marginRight: Int)
83 }