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.content.Context
20 import android.os.IBinder
21 import android.view.SurfaceControl
22 import android.view.WindowManager
23 import android.window.TransitionInfo
24 import com.android.window.flags.Flags.enableDesktopWindowingWallpaperActivity
25 import com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_DESKTOP_MODE
26 import com.android.wm.shell.shared.DesktopModeStatus
27 import com.android.wm.shell.sysui.ShellInit
28 import com.android.wm.shell.transition.Transitions
29 import com.android.wm.shell.util.KtProtoLog
30 
31 /**
32  * A [Transitions.TransitionObserver] that observes shell transitions and updates
33  * the [DesktopModeTaskRepository] state TODO: b/332682201
34  * This observes transitions related to desktop mode
35  * and other transitions that originate both within and outside shell.
36  */
37 class DesktopTasksTransitionObserver(
38     context: Context,
39     private val desktopModeTaskRepository: DesktopModeTaskRepository,
40     private val transitions: Transitions,
41     shellInit: ShellInit
42 ) : Transitions.TransitionObserver {
43 
44     init {
45         if (Transitions.ENABLE_SHELL_TRANSITIONS &&
46             DesktopModeStatus.canEnterDesktopMode(context)) {
47             shellInit.addInitCallback(::onInit, this)
48         }
49     }
50 
51     fun onInit() {
52         KtProtoLog.d(WM_SHELL_DESKTOP_MODE, "DesktopTasksTransitionObserver: onInit")
53         transitions.registerObserver(this)
54     }
55 
56     override fun onTransitionReady(
57         transition: IBinder,
58         info: TransitionInfo,
59         startTransaction: SurfaceControl.Transaction,
60         finishTransaction: SurfaceControl.Transaction
61     ) {
62         // TODO: b/332682201 Update repository state
63         updateWallpaperToken(info)
64     }
65 
66     override fun onTransitionStarting(transition: IBinder) {
67         // TODO: b/332682201 Update repository state
68     }
69 
70     override fun onTransitionMerged(merged: IBinder, playing: IBinder) {
71         // TODO: b/332682201 Update repository state
72     }
73 
74     override fun onTransitionFinished(transition: IBinder, aborted: Boolean) {
75         // TODO: b/332682201 Update repository state
76     }
77 
78     private fun updateWallpaperToken(info: TransitionInfo) {
79         if (!enableDesktopWindowingWallpaperActivity()) {
80             return
81         }
82         info.changes.forEach { change ->
83             change.taskInfo?.let { taskInfo ->
84                 if (DesktopWallpaperActivity.isWallpaperTask(taskInfo)) {
85                     when (change.mode) {
86                         WindowManager.TRANSIT_OPEN ->
87                             desktopModeTaskRepository.wallpaperActivityToken = taskInfo.token
88                         WindowManager.TRANSIT_CLOSE ->
89                             desktopModeTaskRepository.wallpaperActivityToken = null
90                         else -> {}
91                     }
92                 }
93             }
94         }
95     }
96 }
97