• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.view.Display
21 import com.android.systemui.dagger.SysUISingleton
22 import dagger.Binds
23 import dagger.Module
24 import javax.inject.Inject
25 import kotlinx.coroutines.channels.BufferOverflow
26 import kotlinx.coroutines.flow.Flow
27 import kotlinx.coroutines.flow.MutableSharedFlow
28 import kotlinx.coroutines.flow.MutableStateFlow
29 import kotlinx.coroutines.flow.StateFlow
30 import kotlinx.coroutines.flow.asSharedFlow
31 import kotlinx.coroutines.flow.asStateFlow
32 
33 @SysUISingleton
34 class FakeConfigurationRepository @Inject constructor() : ConfigurationRepository {
35     private val _onAnyConfigurationChange =
36         MutableSharedFlow<Unit>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
37     override val onAnyConfigurationChange: Flow<Unit> = _onAnyConfigurationChange.asSharedFlow()
38 
39     private val _onConfigurationChange =
40         MutableSharedFlow<Unit>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
41     override val onConfigurationChange: Flow<Unit> = _onConfigurationChange.asSharedFlow()
42 
43     private val _configurationChangeValues =
44         MutableSharedFlow<Configuration>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
45     override val configurationValues: Flow<Configuration> =
46         _configurationChangeValues.asSharedFlow()
47 
48     private val _onMovedToDisplay = MutableStateFlow<Int>(Display.DEFAULT_DISPLAY)
49     override val onMovedToDisplay: StateFlow<Int>
50         get() = _onMovedToDisplay
51 
52     private val _scaleForResolution = MutableStateFlow(1f)
53     override val scaleForResolution: StateFlow<Float> = _scaleForResolution.asStateFlow()
54 
55     private val pixelSizes = mutableMapOf<Int, MutableStateFlow<Int>>()
56 
onAnyConfigurationChangenull57     fun onAnyConfigurationChange() {
58         _onAnyConfigurationChange.tryEmit(Unit)
59     }
60 
onConfigurationChangenull61     fun onConfigurationChange() {
62         _onConfigurationChange.tryEmit(Unit)
63     }
64 
onConfigurationChangenull65     fun onConfigurationChange(configChange: Configuration) {
66         _configurationChangeValues.tryEmit(configChange)
67         onAnyConfigurationChange()
68     }
69 
onMovedToDisplaynull70     fun onMovedToDisplay(newDisplayId: Int) {
71         _onMovedToDisplay.value = newDisplayId
72     }
73 
setScaleForResolutionnull74     fun setScaleForResolution(scale: Float) {
75         _scaleForResolution.value = scale
76     }
77 
getResolutionScalenull78     override fun getResolutionScale(): Float = _scaleForResolution.value
79 
80     override fun getDimensionPixelSize(id: Int): Int = pixelSizes[id]?.value ?: 0
81 
82     fun setDimensionPixelSize(id: Int, pixelSize: Int) {
83         pixelSizes.getOrPut(id) { MutableStateFlow(pixelSize) }.value = pixelSize
84     }
85 }
86 
87 @Module
88 interface FakeConfigurationRepositoryModule {
bindFakenull89     @Binds fun bindFake(fake: FakeConfigurationRepository): ConfigurationRepository
90 }
91