• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.wm.shell.desktopmode
18 
19 import android.animation.Animator
20 import android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM
21 import android.os.Handler
22 import android.os.IBinder
23 import android.view.SurfaceControl.Transaction
24 import android.view.WindowManager.TRANSIT_TO_BACK
25 import android.window.TransitionInfo
26 import android.window.TransitionRequestInfo
27 import android.window.WindowContainerTransaction
28 import com.android.internal.jank.InteractionJankMonitor
29 import com.android.internal.protolog.ProtoLog
30 import com.android.wm.shell.common.DisplayController
31 import com.android.wm.shell.common.ShellExecutor
32 import com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_DESKTOP_MODE
33 import com.android.wm.shell.shared.TransitionUtil
34 import com.android.wm.shell.shared.animation.MinimizeAnimator.create
35 import com.android.wm.shell.transition.Transitions
36 
37 /**
38  * The [Transitions.TransitionHandler] that handles transitions for tasks that are:
39  * - Closing or going to back as part of back navigation
40  * - Going to back as part of minimization button usage.
41  *
42  * Note that this handler is used only for animating transitions.
43  */
44 class DesktopMinimizationTransitionHandler(
45     private val mainExecutor: ShellExecutor,
46     private val animExecutor: ShellExecutor,
47     private val displayController: DisplayController,
48     private val animHandler: Handler,
49 ) : Transitions.TransitionHandler {
50 
51     /** Shouldn't handle anything */
52     override fun handleRequest(
53         transition: IBinder,
54         request: TransitionRequestInfo,
55     ): WindowContainerTransaction? = null
56 
57     /** Animates a transition with minimizing tasks */
58     override fun startAnimation(
59         transition: IBinder,
60         info: TransitionInfo,
61         startTransaction: Transaction,
62         finishTransaction: Transaction,
63         finishCallback: Transitions.TransitionFinishCallback,
64     ): Boolean {
65         val shouldAnimate =
66             TransitionUtil.isClosingType(info.type) || info.type == Transitions.TRANSIT_MINIMIZE
67         if (!shouldAnimate) return false
68 
69         val animations = mutableListOf<Animator>()
70         val onAnimFinish: (Animator) -> Unit = { animator ->
71             mainExecutor.execute {
72                 // Animation completed
73                 animations.remove(animator)
74                 if (animations.isEmpty()) {
75                     // All animations completed, finish the transition
76                     finishCallback.onTransitionFinished(/* wct= */ null)
77                 }
78             }
79         }
80 
81         val checkChangeMode = { change: TransitionInfo.Change ->
82             change.mode == info.type ||
83                 (info.type == Transitions.TRANSIT_MINIMIZE && change.mode == TRANSIT_TO_BACK)
84         }
85         animations +=
86             info.changes
87                 .filter {
88                     checkChangeMode(it) && it.taskInfo?.windowingMode == WINDOWING_MODE_FREEFORM
89                 }
90                 .mapNotNull { createMinimizeAnimation(it, finishTransaction, onAnimFinish) }
91         if (animations.isEmpty()) return false
92         animExecutor.execute { animations.forEach(Animator::start) }
93         return true
94     }
95 
96     private fun createMinimizeAnimation(
97         change: TransitionInfo.Change,
98         finishTransaction: Transaction,
99         onAnimFinish: (Animator) -> Unit,
100     ): Animator? {
101         val t = Transaction()
102         val sc = change.leash
103         finishTransaction.hide(sc)
104         val displayContext =
105             change.taskInfo?.let { displayController.getDisplayContext(it.displayId) }
106         if (displayContext == null) {
107             logW(
108                 "displayContext is null for taskId=${change.taskInfo?.taskId}, " +
109                     "displayId=${change.taskInfo?.displayId}"
110             )
111             return null
112         }
113         return create(
114             displayContext,
115             change,
116             t,
117             onAnimFinish,
118             InteractionJankMonitor.getInstance(),
119             animHandler,
120         )
121     }
122 
123     private companion object {
124         private fun logW(msg: String, vararg arguments: Any?) {
125             ProtoLog.w(WM_SHELL_DESKTOP_MODE, "%s: $msg", TAG, *arguments)
126         }
127 
128         const val TAG = "DesktopMinimizationTransitionHandler"
129     }
130 }
131