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.qs.tiles.viewmodel 18 19 import com.android.internal.util.Preconditions 20 import com.android.systemui.dagger.SysUISingleton 21 import com.android.systemui.qs.QsEventLogger 22 import com.android.systemui.qs.pipeline.shared.TileSpec 23 import javax.inject.Inject 24 25 interface QSTileConfigProvider { 26 27 /** 28 * Returns a [QSTileConfig] for a [tileSpec]: 29 * - injected config for [TileSpec.PlatformTileSpec] or throws [IllegalArgumentException] if 30 * there is none 31 * - new config for [TileSpec.CustomTileSpec]. 32 * - throws [IllegalArgumentException] for [TileSpec.Invalid] 33 */ getConfignull34 fun getConfig(tileSpec: String): QSTileConfig 35 36 fun hasConfig(tileSpec: String): Boolean 37 } 38 39 @SysUISingleton 40 class QSTileConfigProviderImpl 41 @Inject 42 constructor( 43 private val configs: Map<String, QSTileConfig>, 44 private val qsEventLogger: QsEventLogger, 45 ) : QSTileConfigProvider { 46 47 init { 48 for (entry in configs.entries) { 49 val configTileSpec = entry.value.tileSpec.spec 50 val keyTileSpec = entry.key 51 Preconditions.checkArgument( 52 configTileSpec == keyTileSpec, 53 "A wrong config is injected keySpec=$keyTileSpec configSpec=$configTileSpec" 54 ) 55 } 56 } 57 58 override fun hasConfig(tileSpec: String): Boolean = 59 when (TileSpec.create(tileSpec)) { 60 is TileSpec.PlatformTileSpec -> configs.containsKey(tileSpec) 61 is TileSpec.CustomTileSpec -> true 62 is TileSpec.Invalid -> false 63 } 64 65 override fun getConfig(tileSpec: String): QSTileConfig = 66 when (val spec = TileSpec.create(tileSpec)) { 67 is TileSpec.PlatformTileSpec -> { 68 configs[tileSpec] 69 ?: throw IllegalArgumentException("There is no config for spec=$tileSpec") 70 } 71 is TileSpec.CustomTileSpec -> 72 QSTileConfig( 73 spec, 74 QSTileUIConfig.Empty, 75 qsEventLogger.getNewInstanceId(), 76 ) 77 is TileSpec.Invalid -> 78 throw IllegalArgumentException("TileSpec.Invalid doesn't support configs") 79 } 80 } 81