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.systemui.qs.panels.domain.interactor 18 19 import com.android.internal.logging.UiEventLogger 20 import com.android.systemui.dagger.SysUISingleton 21 import com.android.systemui.dagger.qualifiers.Application 22 import com.android.systemui.log.LogBuffer 23 import com.android.systemui.log.core.LogLevel 24 import com.android.systemui.qs.QSEditEvent 25 import com.android.systemui.qs.panels.data.repository.DefaultLargeTilesRepository 26 import com.android.systemui.qs.panels.data.repository.LargeTileSpanRepository 27 import com.android.systemui.qs.panels.shared.model.PanelsLog 28 import com.android.systemui.qs.pipeline.domain.interactor.CurrentTilesInteractor 29 import com.android.systemui.qs.pipeline.shared.TileSpec 30 import com.android.systemui.qs.pipeline.shared.metricSpec 31 import javax.inject.Inject 32 import kotlinx.coroutines.CoroutineScope 33 import kotlinx.coroutines.flow.SharingStarted 34 import kotlinx.coroutines.flow.StateFlow 35 import kotlinx.coroutines.flow.onEach 36 import kotlinx.coroutines.flow.stateIn 37 38 /** Interactor for retrieving the list of [TileSpec] to be displayed as icons and resizing icons. */ 39 @SysUISingleton 40 class IconTilesInteractor 41 @Inject 42 constructor( 43 private val repo: DefaultLargeTilesRepository, 44 private val currentTilesInteractor: CurrentTilesInteractor, 45 private val preferencesInteractor: QSPreferencesInteractor, 46 private val uiEventLogger: UiEventLogger, 47 largeTilesSpanRepo: LargeTileSpanRepository, 48 @PanelsLog private val logBuffer: LogBuffer, 49 @Application private val applicationScope: CoroutineScope, 50 ) { 51 52 val largeTilesSpecs = 53 preferencesInteractor.largeTilesSpecs <lambda>null54 .onEach { logChange(it) } 55 .stateIn(applicationScope, SharingStarted.Eagerly, repo.defaultLargeTiles) 56 57 val largeTilesSpan: StateFlow<Int> = largeTilesSpanRepo.span 58 isIconTilenull59 fun isIconTile(spec: TileSpec): Boolean = !largeTilesSpecs.value.contains(spec) 60 61 fun setLargeTiles(specs: Set<TileSpec>) { 62 preferencesInteractor.setLargeTilesSpecs(specs) 63 } 64 resetToDefaultnull65 fun resetToDefault() { 66 preferencesInteractor.setLargeTilesSpecs(repo.defaultLargeTiles) 67 } 68 resizenull69 fun resize(spec: TileSpec, toIcon: Boolean) { 70 if (!isCurrent(spec)) { 71 return 72 } 73 74 val isIcon = !largeTilesSpecs.value.contains(spec) 75 if (toIcon && !isIcon) { 76 preferencesInteractor.setLargeTilesSpecs(largeTilesSpecs.value - spec) 77 uiEventLogger.log( 78 /* event= */ QSEditEvent.QS_EDIT_RESIZE_SMALL, 79 /* uid= */ 0, 80 /* packageName= */ spec.metricSpec, 81 ) 82 } else if (!toIcon && isIcon) { 83 preferencesInteractor.setLargeTilesSpecs(largeTilesSpecs.value + spec) 84 uiEventLogger.log( 85 /* event= */ QSEditEvent.QS_EDIT_RESIZE_LARGE, 86 /* uid= */ 0, 87 /* packageName= */ spec.metricSpec, 88 ) 89 } 90 } 91 isCurrentnull92 private fun isCurrent(spec: TileSpec): Boolean { 93 return currentTilesInteractor.currentTilesSpecs.contains(spec) 94 } 95 logChangenull96 private fun logChange(specs: Set<TileSpec>) { 97 logBuffer.log( 98 LOG_BUFFER_LARGE_TILES_SPECS_CHANGE_TAG, 99 LogLevel.DEBUG, 100 { str1 = specs.toString() }, 101 { "Large tiles change: $str1" }, 102 ) 103 } 104 105 private companion object { 106 const val LOG_BUFFER_LARGE_TILES_SPECS_CHANGE_TAG = "LargeTilesSpecsChange" 107 } 108 } 109