1 /* <lambda>null2 * 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.tiles.impl.modes.domain.interactor 18 19 import android.content.Context 20 import android.os.UserHandle 21 import android.text.TextUtils 22 import com.android.app.tracing.coroutines.flow.flowName 23 import com.android.settingslib.notification.modes.ZenMode 24 import com.android.settingslib.notification.modes.ZenModeDescriptions 25 import com.android.systemui.dagger.qualifiers.Background 26 import com.android.systemui.modes.shared.ModesUi 27 import com.android.systemui.qs.tiles.base.domain.interactor.QSTileDataInteractor 28 import com.android.systemui.qs.tiles.base.domain.model.DataUpdateTrigger 29 import com.android.systemui.qs.tiles.impl.modes.domain.model.ModesDndTileModel 30 import com.android.systemui.shade.ShadeDisplayAware 31 import com.android.systemui.statusbar.policy.domain.interactor.ZenModeInteractor 32 import javax.inject.Inject 33 import kotlinx.coroutines.CoroutineDispatcher 34 import kotlinx.coroutines.flow.Flow 35 import kotlinx.coroutines.flow.distinctUntilChanged 36 import kotlinx.coroutines.flow.filterNotNull 37 import kotlinx.coroutines.flow.flowOf 38 import kotlinx.coroutines.flow.flowOn 39 import kotlinx.coroutines.flow.map 40 41 class ModesDndTileDataInteractor 42 @Inject 43 constructor( 44 @ShadeDisplayAware val context: Context, 45 val zenModeInteractor: ZenModeInteractor, 46 @Background val bgDispatcher: CoroutineDispatcher, 47 ) : QSTileDataInteractor<ModesDndTileModel> { 48 49 private val zenModeDescriptions = ZenModeDescriptions(context) 50 51 override fun tileData( 52 user: UserHandle, 53 triggers: Flow<DataUpdateTrigger>, 54 ): Flow<ModesDndTileModel> = tileData() 55 56 /** 57 * An adapted version of the base class' [tileData] method for use in an old-style tile. 58 * 59 * TODO(b/299909989): Remove after the transition. 60 */ 61 fun tileData() = 62 zenModeInteractor.dndMode 63 .filterNotNull() 64 .map { dndMode -> buildTileData(dndMode) } 65 .flowName("tileData") 66 .flowOn(bgDispatcher) 67 .distinctUntilChanged() 68 69 fun getCurrentTileModel() = buildTileData(zenModeInteractor.getDndMode()) 70 71 private fun buildTileData(dndMode: ZenMode): ModesDndTileModel { 72 return ModesDndTileModel( 73 isActivated = dndMode.isActive, 74 extraStatus = TextUtils.nullIfEmpty(zenModeDescriptions.getTriggerDescription(dndMode)), 75 ) 76 } 77 78 override fun availability(user: UserHandle): Flow<Boolean> = 79 flowOf(ModesUi.isEnabled && android.app.Flags.modesUiDndTile()) 80 } 81