1 /*
2  * 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 android.util.Log
21 import com.android.systemui.keyguard.data.repository.KeyguardTransitionRepository
22 import com.android.systemui.keyguard.shared.model.KeyguardState
23 import com.android.systemui.keyguard.shared.model.TransitionInfo
24 import java.util.UUID
25 
26 /**
27  * Each TransitionInteractor is responsible for determining under which conditions to notify
28  * [KeyguardTransitionRepository] to signal a transition. When (and if) the transition occurs is
29  * determined by [KeyguardTransitionRepository].
30  *
31  * [name] field should be a unique identifiable string representing this state, used primarily for
32  * logging
33  *
34  * MUST list implementing classes in dagger module [StartKeyguardTransitionModule] and also in the
35  * 'when' clause of [KeyguardTransitionCoreStartable]
36  */
37 sealed class TransitionInteractor(
38     val fromState: KeyguardState,
39 ) {
40     val name = this::class.simpleName ?: "UnknownTransitionInteractor"
41 
42     abstract val transitionRepository: KeyguardTransitionRepository
43     abstract val transitionInteractor: KeyguardTransitionInteractor
44     abstract fun start()
45 
46     fun startTransitionTo(
47         toState: KeyguardState,
48         animator: ValueAnimator? = getDefaultAnimatorForTransitionsToState(toState),
49         resetIfCancelled: Boolean = false
50     ): UUID? {
51         if (
52             fromState != transitionInteractor.startedKeyguardState.value &&
53                 fromState != transitionInteractor.finishedKeyguardState.value
54         ) {
55             Log.e(
56                 name,
57                 "startTransition: We were asked to transition from " +
58                     "$fromState to $toState, however we last finished a transition to " +
59                     "${transitionInteractor.finishedKeyguardState.value}, " +
60                     "and last started a transition to " +
61                     "${transitionInteractor.startedKeyguardState.value}. " +
62                     "Ignoring startTransition, but this should never happen."
63             )
64             return null
65         }
66 
67         return transitionRepository.startTransition(
68             TransitionInfo(
69                 name,
70                 fromState,
71                 toState,
72                 animator,
73             ),
74             resetIfCancelled
75         )
76     }
77 
78     /**
79      * Returns a ValueAnimator to be used for transitions to [toState], if one is not explicitly
80      * passed to [startTransitionTo].
81      */
82     abstract fun getDefaultAnimatorForTransitionsToState(toState: KeyguardState): ValueAnimator?
83 }
84