1 /* 2 * Copyright (C) 2024 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.settings.biometrics.fingerprint2.domain.interactor 18 19 import android.content.Context 20 import com.android.settingslib.display.DisplayDensityUtils 21 import kotlinx.coroutines.CoroutineScope 22 import kotlinx.coroutines.flow.Flow 23 import kotlinx.coroutines.flow.MutableStateFlow 24 import kotlinx.coroutines.flow.SharingStarted 25 import kotlinx.coroutines.flow.asStateFlow 26 import kotlinx.coroutines.flow.flowOf 27 import kotlinx.coroutines.flow.shareIn 28 import kotlinx.coroutines.flow.update 29 30 /** 31 * This class is responsible for handling updates to fontScale and displayDensity and forwarding 32 * these events to classes that need them 33 */ 34 interface DisplayDensityInteractor { 35 /** Indicates the display density has been updated. */ updateDisplayDensitynull36 fun updateDisplayDensity(density: Int) 37 38 /** Indicates the font scale has been updates. */ 39 fun updateFontScale(fontScale: Float) 40 41 /** A flow that propagates fontscale. */ 42 val fontScale: Flow<Float> 43 44 /** A flow that propagates displayDensity. */ 45 val displayDensity: Flow<Int> 46 47 /** A flow that propagates the default display density. */ 48 val defaultDisplayDensity: Flow<Int> 49 } 50 51 /** 52 * Implementation of the [DisplayDensityInteractor]. This interactor is used to forward activity 53 * information to the rest of the application. 54 */ 55 class DisplayDensityInteractorImpl(context: Context, scope: CoroutineScope) : 56 DisplayDensityInteractor { 57 58 val displayDensityUtils = DisplayDensityUtils(context) 59 60 override fun updateDisplayDensity(density: Int) { 61 _displayDensity.update { density } 62 } 63 64 override fun updateFontScale(fontScale: Float) { 65 _fontScale.update { fontScale } 66 } 67 68 private val _fontScale = MutableStateFlow(context.resources.configuration.fontScale) 69 private val _displayDensity = 70 MutableStateFlow( 71 displayDensityUtils.defaultDisplayDensityValues[ 72 displayDensityUtils.currentIndexForDefaultDisplay] 73 ) 74 75 override val fontScale: Flow<Float> = _fontScale.asStateFlow() 76 77 override val displayDensity: Flow<Int> = _displayDensity.asStateFlow() 78 79 override val defaultDisplayDensity: Flow<Int> = 80 flowOf(displayDensityUtils.defaultDensityForDefaultDisplay) 81 .shareIn(scope, SharingStarted.Eagerly, 1) 82 } 83