• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2022 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.keyguard.domain.interactor
18 
19 import android.animation.ValueAnimator
20 import com.android.systemui.animation.Interpolators
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.dagger.qualifiers.Application
23 import com.android.systemui.keyguard.data.repository.KeyguardTransitionRepository
24 import com.android.systemui.keyguard.shared.model.KeyguardState
25 import com.android.systemui.keyguard.shared.model.TransitionInfo
26 import com.android.systemui.keyguard.shared.model.WakefulnessState
27 import com.android.systemui.util.kotlin.sample
28 import javax.inject.Inject
29 import kotlin.time.Duration
30 import kotlin.time.Duration.Companion.milliseconds
31 import kotlinx.coroutines.CoroutineScope
32 import kotlinx.coroutines.flow.combine
33 import kotlinx.coroutines.launch
34 
35 @SysUISingleton
36 class FromGoneTransitionInteractor
37 @Inject
38 constructor(
39     @Application private val scope: CoroutineScope,
40     private val keyguardInteractor: KeyguardInteractor,
41     private val keyguardTransitionRepository: KeyguardTransitionRepository,
42     private val keyguardTransitionInteractor: KeyguardTransitionInteractor,
43 ) : TransitionInteractor(FromGoneTransitionInteractor::class.simpleName!!) {
44 
45     override fun start() {
46         listenForGoneToAodOrDozing()
47         listenForGoneToDreaming()
48         listenForGoneToLockscreen()
49     }
50 
51     // Primarily for when the user chooses to lock down the device
52     private fun listenForGoneToLockscreen() {
53         scope.launch {
54             keyguardInteractor.isKeyguardShowing
55                 .sample(keyguardTransitionInteractor.startedKeyguardTransitionStep, ::Pair)
56                 .collect { (isKeyguardShowing, lastStartedStep) ->
57                     if (isKeyguardShowing && lastStartedStep.to == KeyguardState.GONE) {
58                         keyguardTransitionRepository.startTransition(
59                             TransitionInfo(
60                                 name,
61                                 KeyguardState.GONE,
62                                 KeyguardState.LOCKSCREEN,
63                                 getAnimator(),
64                             )
65                         )
66                     }
67                 }
68         }
69     }
70 
71     private fun listenForGoneToDreaming() {
72         scope.launch {
73             keyguardInteractor.isAbleToDream
74                 .sample(keyguardTransitionInteractor.startedKeyguardTransitionStep, ::Pair)
75                 .collect { (isAbleToDream, lastStartedStep) ->
76                     if (isAbleToDream && lastStartedStep.to == KeyguardState.GONE) {
77                         keyguardTransitionRepository.startTransition(
78                             TransitionInfo(
79                                 name,
80                                 KeyguardState.GONE,
81                                 KeyguardState.DREAMING,
82                                 getAnimator(TO_DREAMING_DURATION),
83                             )
84                         )
85                     }
86                 }
87         }
88     }
89 
90     private fun listenForGoneToAodOrDozing() {
91         scope.launch {
92             keyguardInteractor.wakefulnessModel
93                 .sample(
94                     combine(
95                         keyguardTransitionInteractor.startedKeyguardTransitionStep,
96                         keyguardInteractor.isAodAvailable,
97                         ::Pair
98                     ),
99                     ::toTriple
100                 )
101                 .collect { (wakefulnessState, lastStartedStep, isAodAvailable) ->
102                     if (
103                         lastStartedStep.to == KeyguardState.GONE &&
104                             wakefulnessState.state == WakefulnessState.STARTING_TO_SLEEP
105                     ) {
106                         keyguardTransitionRepository.startTransition(
107                             TransitionInfo(
108                                 name,
109                                 KeyguardState.GONE,
110                                 if (isAodAvailable) {
111                                     KeyguardState.AOD
112                                 } else {
113                                     KeyguardState.DOZING
114                                 },
115                                 getAnimator(),
116                             )
117                         )
118                     }
119                 }
120         }
121     }
122 
123     private fun getAnimator(duration: Duration = DEFAULT_DURATION): ValueAnimator {
124         return ValueAnimator().apply {
125             setInterpolator(Interpolators.LINEAR)
126             setDuration(duration.inWholeMilliseconds)
127         }
128     }
129 
130     companion object {
131         private val DEFAULT_DURATION = 500.milliseconds
132         val TO_DREAMING_DURATION = 933.milliseconds
133     }
134 }
135