1 /*
2  * Copyright (C) 2023 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 package com.android.quickstep.util
17 
18 import android.animation.ValueAnimator
19 import android.os.IBinder
20 import android.os.RemoteException
21 import android.view.SurfaceControl
22 import android.view.SurfaceControl.Transaction
23 import android.window.IRemoteTransitionFinishedCallback
24 import android.window.RemoteTransitionStub
25 import android.window.TransitionInfo
26 import com.android.launcher3.anim.AnimatorListeners.forEndCallback
27 import com.android.launcher3.util.Executors
28 import com.android.wm.shell.shared.TransitionUtil
29 
30 /** Remote animation which fades out the closing targets */
31 class FadeOutRemoteTransition : RemoteTransitionStub() {
32 
startAnimationnull33     override fun startAnimation(
34         transition: IBinder,
35         info: TransitionInfo,
36         startT: Transaction,
37         finishCB: IRemoteTransitionFinishedCallback
38     ) {
39         val anim = ValueAnimator.ofFloat(1f, 0f)
40 
41         val closingControls: MutableList<SurfaceControl> = mutableListOf()
42         for (chg in info.changes) {
43             startT.show(chg.leash)
44             if (TransitionUtil.isClosingType(chg.mode)) {
45                 closingControls.add(chg.leash)
46             }
47         }
48         startT.apply()
49 
50         anim.addUpdateListener {
51             val t = Transaction()
52             closingControls.forEach { t.setAlpha(it, anim.animatedValue as Float) }
53             t.apply()
54         }
55         anim.addListener(
56             forEndCallback(
57                 Runnable {
58                     val t = Transaction()
59                     closingControls.forEach { t.hide(it) }
60                     try {
61                         finishCB.onTransitionFinished(null, t)
62                     } catch (e: RemoteException) {
63                         // Ignore
64                     }
65                 }
66             )
67         )
68 
69         Executors.MAIN_EXECUTOR.execute { anim.start() }
70     }
71 }
72