1 /*
<lambda>null2  * Copyright (C) 2023 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.settings
18 
19 import android.content.Context
20 import android.hardware.display.DisplayManager
21 import android.view.Display
22 import java.util.concurrent.Executor
23 
24 class FakeDisplayTracker constructor(val context: Context) : DisplayTracker {
25     val displayManager: DisplayManager = context.getSystemService(DisplayManager::class.java)!!
26     override var defaultDisplayId: Int = Display.DEFAULT_DISPLAY
27     override var allDisplays: Array<Display> = displayManager.displays
28 
29     val displayCallbacks: MutableList<DisplayTracker.Callback> = ArrayList()
30     private val brightnessCallbacks: MutableList<DisplayTracker.Callback> = ArrayList()
31     override fun addDisplayChangeCallback(callback: DisplayTracker.Callback, executor: Executor) {
32         displayCallbacks.add(callback)
33     }
34     override fun addBrightnessChangeCallback(
35         callback: DisplayTracker.Callback,
36         executor: Executor
37     ) {
38         brightnessCallbacks.add(callback)
39     }
40 
41     override fun removeCallback(callback: DisplayTracker.Callback) {
42         displayCallbacks.remove(callback)
43         brightnessCallbacks.remove(callback)
44     }
45 
46     override fun getDisplay(displayId: Int): Display {
47         return allDisplays.filter { display -> display.displayId == displayId }[0]
48     }
49 
50     fun setDefaultDisplay(displayId: Int) {
51         defaultDisplayId = displayId
52     }
53 
54     fun triggerOnDisplayAdded(displayId: Int) {
55         notifyCallbacks({ onDisplayAdded(displayId) }, displayCallbacks)
56     }
57 
58     fun triggerOnDisplayRemoved(displayId: Int) {
59         notifyCallbacks({ onDisplayRemoved(displayId) }, displayCallbacks)
60     }
61 
62     fun triggerOnDisplayChanged(displayId: Int) {
63         notifyCallbacks({ onDisplayChanged(displayId) }, displayCallbacks)
64     }
65 
66     fun triggerOnDisplayBrightnessChanged(displayId: Int) {
67         notifyCallbacks({ onDisplayChanged(displayId) }, brightnessCallbacks)
68     }
69 
70     private inline fun notifyCallbacks(
71         crossinline action: DisplayTracker.Callback.() -> Unit,
72         list: List<DisplayTracker.Callback>
73     ) {
74         list.forEach { it.action() }
75     }
76 }
77