• 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.BiometricUnlockModel.Companion.isWakeAndUnlock
25 import com.android.systemui.keyguard.shared.model.KeyguardState
26 import com.android.systemui.keyguard.shared.model.TransitionInfo
27 import com.android.systemui.keyguard.shared.model.WakefulnessModel.Companion.isWakingOrStartingToWake
28 import com.android.systemui.util.kotlin.sample
29 import javax.inject.Inject
30 import kotlin.time.Duration
31 import kotlin.time.Duration.Companion.milliseconds
32 import kotlinx.coroutines.CoroutineScope
33 import kotlinx.coroutines.flow.collect
34 import kotlinx.coroutines.launch
35 
36 @SysUISingleton
37 class FromDozingTransitionInteractor
38 @Inject
39 constructor(
40     @Application private val scope: CoroutineScope,
41     private val keyguardInteractor: KeyguardInteractor,
42     private val keyguardTransitionRepository: KeyguardTransitionRepository,
43     private val keyguardTransitionInteractor: KeyguardTransitionInteractor,
44 ) : TransitionInteractor(FromDozingTransitionInteractor::class.simpleName!!) {
45 
46     override fun start() {
47         listenForDozingToLockscreen()
48         listenForDozingToGone()
49     }
50 
51     private fun listenForDozingToLockscreen() {
52         scope.launch {
53             keyguardInteractor.wakefulnessModel
54                 .sample(keyguardTransitionInteractor.startedKeyguardTransitionStep, ::Pair)
55                 .collect { (wakefulnessModel, lastStartedTransition) ->
56                     if (
57                         isWakingOrStartingToWake(wakefulnessModel) &&
58                             lastStartedTransition.to == KeyguardState.DOZING
59                     ) {
60                         keyguardTransitionRepository.startTransition(
61                             TransitionInfo(
62                                 name,
63                                 KeyguardState.DOZING,
64                                 KeyguardState.LOCKSCREEN,
65                                 getAnimator(),
66                             )
67                         )
68                     }
69                 }
70         }
71     }
72 
73     private fun listenForDozingToGone() {
74         scope.launch {
75             keyguardInteractor.biometricUnlockState
76                 .sample(keyguardTransitionInteractor.startedKeyguardTransitionStep, ::Pair)
77                 .collect { (biometricUnlockState, lastStartedTransition) ->
78                     if (
79                         lastStartedTransition.to == KeyguardState.DOZING &&
80                             isWakeAndUnlock(biometricUnlockState)
81                     ) {
82                         keyguardTransitionRepository.startTransition(
83                             TransitionInfo(
84                                 name,
85                                 KeyguardState.DOZING,
86                                 KeyguardState.GONE,
87                                 getAnimator(),
88                             )
89                         )
90                     }
91                 }
92         }
93     }
94 
95     private fun getAnimator(duration: Duration = DEFAULT_DURATION): ValueAnimator {
96         return ValueAnimator().apply {
97             setInterpolator(Interpolators.LINEAR)
98             setDuration(duration.inWholeMilliseconds)
99         }
100     }
101 
102     companion object {
103         private val DEFAULT_DURATION = 500.milliseconds
104     }
105 }
106