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.notifications.ui.viewmodel 18 19 import androidx.compose.runtime.getValue 20 import com.android.app.tracing.coroutines.launchTraced as launch 21 import com.android.systemui.lifecycle.ExclusiveActivatable 22 import com.android.systemui.lifecycle.Hydrator 23 import com.android.systemui.media.controls.domain.pipeline.interactor.MediaCarouselInteractor 24 import com.android.systemui.scene.domain.interactor.SceneInteractor 25 import com.android.systemui.shade.domain.interactor.ShadeInteractor 26 import com.android.systemui.shade.ui.viewmodel.ShadeHeaderViewModel 27 import com.android.systemui.statusbar.disableflags.domain.interactor.DisableFlagsInteractor 28 import com.android.systemui.statusbar.notification.stack.ui.viewmodel.NotificationsPlaceholderViewModel 29 import com.android.systemui.utils.coroutines.flow.flatMapLatestConflated 30 import dagger.assisted.AssistedFactory 31 import dagger.assisted.AssistedInject 32 import kotlinx.coroutines.awaitCancellation 33 import kotlinx.coroutines.coroutineScope 34 import kotlinx.coroutines.flow.distinctUntilChanged 35 import kotlinx.coroutines.flow.filter 36 import kotlinx.coroutines.flow.flowOf 37 38 /** 39 * Models UI state used to render the content of the notifications shade overlay. 40 * 41 * Different from [NotificationsShadeOverlayActionsViewModel], which only models user actions that 42 * can be performed to navigate to other scenes. 43 */ 44 class NotificationsShadeOverlayContentViewModel 45 @AssistedInject 46 constructor( 47 val shadeHeaderViewModelFactory: ShadeHeaderViewModel.Factory, 48 val notificationsPlaceholderViewModelFactory: NotificationsPlaceholderViewModel.Factory, 49 val sceneInteractor: SceneInteractor, 50 private val shadeInteractor: ShadeInteractor, 51 disableFlagsInteractor: DisableFlagsInteractor, 52 mediaCarouselInteractor: MediaCarouselInteractor, 53 ) : ExclusiveActivatable() { 54 55 private val hydrator = Hydrator("NotificationsShadeOverlayContentViewModel.hydrator") 56 57 val showMedia: Boolean by 58 hydrator.hydratedStateOf( 59 traceName = "showMedia", 60 initialValue = 61 disableFlagsInteractor.disableFlags.value.isQuickSettingsEnabled() && 62 mediaCarouselInteractor.hasActiveMediaOrRecommendation.value, 63 source = <lambda>null64 disableFlagsInteractor.disableFlags.flatMapLatestConflated { 65 if (it.isQuickSettingsEnabled()) { 66 mediaCarouselInteractor.hasActiveMediaOrRecommendation 67 } else { 68 flowOf(false) 69 } 70 }, 71 ) 72 onActivatednull73 override suspend fun onActivated(): Nothing { 74 coroutineScope { 75 launch { hydrator.activate() } 76 77 launch { 78 shadeInteractor.isShadeTouchable 79 .distinctUntilChanged() 80 .filter { !it } 81 .collect { 82 shadeInteractor.collapseNotificationsShade( 83 loggingReason = "device became non-interactive" 84 ) 85 } 86 } 87 } 88 awaitCancellation() 89 } 90 onScrimClickednull91 fun onScrimClicked() { 92 shadeInteractor.collapseNotificationsShade(loggingReason = "shade scrim clicked") 93 } 94 95 @AssistedFactory 96 interface Factory { createnull97 fun create(): NotificationsShadeOverlayContentViewModel 98 } 99 } 100