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.communal
18 
19 import android.annotation.SuppressLint
20 import android.app.DreamManager
21 import com.android.systemui.CoreStartable
22 import com.android.systemui.Flags.communalHub
23 import com.android.systemui.Flags.glanceableHubAllowKeyguardWhenDreaming
24 import com.android.systemui.Flags.restartDreamOnUnocclude
25 import com.android.systemui.dagger.SysUISingleton
26 import com.android.systemui.dagger.qualifiers.Background
27 import com.android.systemui.keyguard.domain.interactor.KeyguardInteractor
28 import com.android.systemui.keyguard.domain.interactor.KeyguardTransitionInteractor
29 import com.android.systemui.keyguard.shared.model.Edge
30 import com.android.systemui.keyguard.shared.model.KeyguardState
31 import com.android.systemui.keyguard.shared.model.TransitionState
32 import com.android.systemui.keyguard.shared.model.filterState
33 import com.android.systemui.power.domain.interactor.PowerInteractor
34 import com.android.systemui.scene.shared.model.Scenes
35 import com.android.systemui.util.kotlin.Utils.Companion.sampleFilter
36 import javax.inject.Inject
37 import kotlinx.coroutines.CoroutineScope
38 import kotlinx.coroutines.flow.filter
39 import kotlinx.coroutines.flow.launchIn
40 import kotlinx.coroutines.flow.onEach
41 
42 /**
43  * A [CoreStartable] responsible for automatically starting the dream when the communal hub is
44  * shown, to support the user swiping away the hub to enter the dream.
45  */
46 @SysUISingleton
47 class CommunalDreamStartable
48 @Inject
49 constructor(
50     private val powerInteractor: PowerInteractor,
51     private val keyguardInteractor: KeyguardInteractor,
52     private val keyguardTransitionInteractor: KeyguardTransitionInteractor,
53     private val dreamManager: DreamManager,
54     @Background private val bgScope: CoroutineScope,
55 ) : CoreStartable {
56     @SuppressLint("MissingPermission")
57     override fun start() {
58         if (!communalHub()) {
59             return
60         }
61 
62         // Return to dream from occluded when not already dreaming.
63         if (restartDreamOnUnocclude()) {
64             keyguardTransitionInteractor
65                 .transition(Edge.create(from = KeyguardState.OCCLUDED, to = KeyguardState.DREAMING))
66                 .filterState(TransitionState.STARTED)
67                 .sampleFilter(keyguardInteractor.isDreaming) { isDreaming -> !isDreaming }
68                 .onEach { dreamManager.startDream() }
69                 .launchIn(bgScope)
70         }
71 
72         // Restart the dream underneath the hub in order to support the ability to swipe
73         // away the hub to enter the dream.
74         keyguardTransitionInteractor
75             .transition(
76                 edge = Edge.create(to = Scenes.Communal),
77                 edgeWithoutSceneContainer = Edge.create(to = KeyguardState.GLANCEABLE_HUB)
78             )
79             .filterState(TransitionState.FINISHED)
80             .sampleFilter(powerInteractor.isAwake) { isAwake ->
81                 dreamManager.canStartDreaming(isAwake)
82             }
83             .sampleFilter(keyguardInteractor.isDreaming) { isDreaming -> !isDreaming }
84             .filter { !glanceableHubAllowKeyguardWhenDreaming() }
85             .onEach { dreamManager.startDream() }
86             .launchIn(bgScope)
87     }
88 }
89