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.systemui.dagger.SysUISingleton
20 import com.android.systemui.plugins.qs.QSFactory
21 import com.android.systemui.qs.pipeline.shared.QSPipelineFlagsRepository
22 import com.android.systemui.qs.pipeline.shared.TileSpec
23 import kotlinx.coroutines.flow.first
24 import javax.inject.Inject
25 
26 /**
27  * Given a list of tiles, it determines which of those are unavailable.
28  */
29 @SysUISingleton
30 class TilesAvailabilityInteractor
31 @Inject
32 constructor(
33         private val newTilesAvailabilityInteractor: NewTilesAvailabilityInteractor,
34         private val qsFactoryImpl: QSFactory,
35         private val qsPipelineFlagsRepository: QSPipelineFlagsRepository,
36 ){
37     /**
38      * Checks a list of tiles and returns which are unavailable. If there's no availability
39      * interactor (or the new tiles flag is off), it will construct an instance of the tile to check
40      * its availability and destroy it. Because of this it's recommended to call this method
41      * sparingly.
42      */
getUnavailableTilesnull43     suspend fun getUnavailableTiles(platformTilesToCheck: Iterable<TileSpec>): Set<TileSpec> {
44         check(platformTilesToCheck.all { it is TileSpec.PlatformTileSpec })
45         val newTilesAvailability = if (qsPipelineFlagsRepository.tilesEnabled) {
46             newTilesAvailabilityInteractor.newTilesAvailable.first()
47         } else {
48             emptyMap()
49         }
50         return (platformTilesToCheck.minus(newTilesAvailability.keys).filterNot {
51             val tile = qsFactoryImpl.createTile(it.spec)
52             (tile?.isAvailable ?: false).also {
53                 tile?.destroy()
54             }
55         } + newTilesAvailability.filterNot { it.value }.keys).toSet()
56     }
57 }
58