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.quickstep.util
18 
19 import android.animation.AnimatorSet
20 import android.content.Context
21 import com.android.launcher3.LauncherAnimationRunner.AnimationResult
22 import com.android.launcher3.anim.AnimatorListeners.forEndCallback
23 import com.android.launcher3.util.RunnableList
24 
25 /** Interface to represent animation for back to Launcher transition */
26 interface BackAnimState {
27 
addOnAnimCompleteCallbacknull28     fun addOnAnimCompleteCallback(r: Runnable)
29 
30     fun applyToAnimationResult(result: AnimationResult, c: Context)
31 
32     fun start()
33 }
34 
35 class AnimatorBackState(private val springAnim: RectFSpringAnim?, private val anim: AnimatorSet?) :
36     BackAnimState {
37 
38     override fun addOnAnimCompleteCallback(r: Runnable) {
39         val springAnimWait = RunnableList()
40         springAnim?.addAnimatorListener(forEndCallback(springAnimWait::executeAllAndDestroy))
41             ?: springAnimWait.executeAllAndDestroy()
42 
43         val animWait = RunnableList()
44         anim?.addListener(
45             forEndCallback(Runnable { springAnimWait.add(animWait::executeAllAndDestroy) })
46         ) ?: springAnimWait.add(animWait::executeAllAndDestroy)
47         animWait.add(r)
48     }
49 
50     override fun applyToAnimationResult(result: AnimationResult, c: Context) {
51         result.setAnimation(anim, c)
52     }
53 
54     override fun start() {
55         anim?.start()
56     }
57 }
58 
59 class AlreadyStartedBackAnimState(private val onEndCallback: RunnableList) : BackAnimState {
60 
addOnAnimCompleteCallbacknull61     override fun addOnAnimCompleteCallback(r: Runnable) {
62         onEndCallback.add(r)
63     }
64 
applyToAnimationResultnull65     override fun applyToAnimationResult(result: AnimationResult, c: Context) {
66         addOnAnimCompleteCallback(result::onAnimationFinished)
67     }
68 
startnull69     override fun start() {}
70 }
71