1 /*
2  * 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.common.ui.data.repository
18 
19 import android.content.res.Configuration
20 import com.android.systemui.dagger.SysUISingleton
21 import dagger.Binds
22 import dagger.Module
23 import javax.inject.Inject
24 import kotlinx.coroutines.channels.BufferOverflow
25 import kotlinx.coroutines.flow.Flow
26 import kotlinx.coroutines.flow.MutableSharedFlow
27 import kotlinx.coroutines.flow.MutableStateFlow
28 import kotlinx.coroutines.flow.asSharedFlow
29 import kotlinx.coroutines.flow.asStateFlow
30 
31 @SysUISingleton
32 class FakeConfigurationRepository @Inject constructor() : ConfigurationRepository {
33     private val _onAnyConfigurationChange =
34         MutableSharedFlow<Unit>(
35             replay = 1,
36             onBufferOverflow = BufferOverflow.DROP_OLDEST,
37         )
38     override val onAnyConfigurationChange: Flow<Unit> = _onAnyConfigurationChange.asSharedFlow()
39 
40     private val _onConfigurationChange =
41         MutableSharedFlow<Unit>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
42     override val onConfigurationChange: Flow<Unit> = _onConfigurationChange.asSharedFlow()
43 
44     private val _configurationChangeValues =
45         MutableSharedFlow<Configuration>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
46     override val configurationValues: Flow<Configuration> =
47         _configurationChangeValues.asSharedFlow()
48 
49     private val _scaleForResolution = MutableStateFlow(1f)
50     override val scaleForResolution: Flow<Float> = _scaleForResolution.asStateFlow()
51 
52     private val pixelSizes = mutableMapOf<Int, MutableStateFlow<Int>>()
53 
onAnyConfigurationChangenull54     fun onAnyConfigurationChange() {
55         _onAnyConfigurationChange.tryEmit(Unit)
56     }
57 
onConfigurationChangenull58     fun onConfigurationChange() {
59         _onConfigurationChange.tryEmit(Unit)
60     }
61 
onConfigurationChangenull62     fun onConfigurationChange(configChange: Configuration) {
63         _configurationChangeValues.tryEmit(configChange)
64         onAnyConfigurationChange()
65     }
66 
setScaleForResolutionnull67     fun setScaleForResolution(scale: Float) {
68         _scaleForResolution.value = scale
69     }
70 
getResolutionScalenull71     override fun getResolutionScale(): Float = _scaleForResolution.value
72 
73     override fun getDimensionPixelSize(id: Int): Int = pixelSizes[id]?.value ?: 0
74 
75     fun setDimensionPixelSize(id: Int, pixelSize: Int) {
76         pixelSizes.getOrPut(id) { MutableStateFlow(pixelSize) }.value = pixelSize
77     }
78 }
79 
80 @Module
81 interface FakeConfigurationRepositoryModule {
bindFakenull82     @Binds fun bindFake(fake: FakeConfigurationRepository): ConfigurationRepository
83 }
84