| 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.wm.shell.desktopmode |
| 18 |
|
| 19 |
import android.app.ActivityManager |
| 20 |
import android.app.WindowConfiguration.ACTIVITY_TYPE_HOME |
| 21 |
import android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD |
| 22 |
import android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM |
| 23 |
import android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN |
| 24 |
import android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED |
| 25 |
import android.app.WindowConfiguration.WindowingMode |
| 26 |
import android.content.Context |
| 27 |
import android.os.IBinder |
| 28 |
import android.view.SurfaceControl |
| 29 |
import android.view.WindowManager.TRANSIT_CHANGE |
| 30 |
import android.view.WindowManager.TRANSIT_OPEN |
| 31 |
import android.view.WindowManager.TRANSIT_TO_FRONT |
| 32 |
import android.window.TransitionInfo |
| 33 |
import android.window.TransitionRequestInfo |
| 34 |
import android.window.WindowContainerTransaction |
| 35 |
import androidx.annotation.BinderThread |
| 36 |
import com.android.internal.protolog.common.ProtoLog |
| 37 |
import com.android.wm.shell.ShellTaskOrganizer |
| 38 |
import com.android.wm.shell.common.ExecutorUtils |
| 39 |
import com.android.wm.shell.common.ExternalInterfaceBinder |
| 40 |
import com.android.wm.shell.common.RemoteCallable |
| 41 |
import com.android.wm.shell.common.ShellExecutor |
| 42 |
import com.android.wm.shell.common.annotations.ExternalThread |
| 43 |
import com.android.wm.shell.common.annotations.ShellMainThread |
| 44 |
import com.android.wm.shell.desktopmode.DesktopModeTaskRepository.VisibleTasksListener |
| 45 |
import com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_DESKTOP_MODE |
| 46 |
import com.android.wm.shell.sysui.ShellController |
| 47 |
import com.android.wm.shell.sysui.ShellInit |
| 48 |
import com.android.wm.shell.sysui.ShellSharedConstants |
| 49 |
import com.android.wm.shell.transition.Transitions |
| 50 |
import java.util.concurrent.Executor |
| 51 |
import java.util.function.Consumer |
| 52 |
|
| 53 |
/** Handles moving tasks in and out of desktop */ |
| 54 |
class DesktopTasksController( |
| 55 |
private val context: Context, |
| 56 |
shellInit: ShellInit, |
| 57 |
private val shellController: ShellController, |
| 58 |
private val shellTaskOrganizer: ShellTaskOrganizer, |
| 59 |
private val transitions: Transitions, |
| 60 |
private val desktopModeTaskRepository: DesktopModeTaskRepository, |
| 61 |
@ShellMainThread private val mainExecutor: ShellExecutor |
| 62 |
) : RemoteCallable<DesktopTasksController>, Transitions.TransitionHandler { |
| 63 |
|
| 64 |
private val desktopMode: DesktopModeImpl |
| 65 |
|
| 66 |
init { |
| 67 |
desktopMode = DesktopModeImpl() |
| 68 |
if (DesktopModeStatus.isProto2Enabled()) { |
| 69 |
shellInit.addInitCallback({ onInit() }, this) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
private fun onInit() { |
| 74 |
ProtoLog.d(WM_SHELL_DESKTOP_MODE, "Initialize DesktopTasksController") |
| 75 |
shellController.addExternalInterface( |
| 76 |
ShellSharedConstants.KEY_EXTRA_SHELL_DESKTOP_MODE, |
| 77 |
{ createExternalInterface() }, |
| 78 |
this |
| 79 |
) |
| 80 |
transitions.addHandler(this) |
| 81 |
} |
| 82 |
|
| 83 |
/** Show all tasks, that are part of the desktop, on top of launcher */ |
| 84 |
fun showDesktopApps() { |
| 85 |
ProtoLog.v(WM_SHELL_DESKTOP_MODE, "showDesktopApps") |
| 86 |
val wct = WindowContainerTransaction() |
| 87 |
|
| 88 |
bringDesktopAppsToFront(wct) |
| 89 |
|
| 90 |
// Execute transaction if there are pending operations |
| 91 |
if (!wct.isEmpty) { |
| 92 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 93 |
transitions.startTransition(TRANSIT_TO_FRONT, wct, null /* handler */) |
| 94 |
} else { |
| 95 |
shellTaskOrganizer.applyTransaction(wct) |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** Move a task with given `taskId` to desktop */ |
| 101 |
fun moveToDesktop(taskId: Int) { |
| 102 |
shellTaskOrganizer.getRunningTaskInfo(taskId)?.let { task -> moveToDesktop(task) } |
| 103 |
} |
| 104 |
|
| 105 |
/** Move a task to desktop */ |
| 106 |
fun moveToDesktop(task: ActivityManager.RunningTaskInfo) { |
| 107 |
ProtoLog.v(WM_SHELL_DESKTOP_MODE, "moveToDesktop: %d", task.taskId) |
| 108 |
|
| 109 |
val wct = WindowContainerTransaction() |
| 110 |
// Bring other apps to front first |
| 111 |
bringDesktopAppsToFront(wct) |
| 112 |
|
| 113 |
wct.setWindowingMode(task.getToken(), WINDOWING_MODE_FREEFORM) |
| 114 |
wct.reorder(task.getToken(), true /* onTop */) |
| 115 |
|
| 116 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 117 |
transitions.startTransition(TRANSIT_CHANGE, wct, null /* handler */) |
| 118 |
} else { |
| 119 |
shellTaskOrganizer.applyTransaction(wct) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** Move a task with given `taskId` to fullscreen */ |
| 124 |
fun moveToFullscreen(taskId: Int) { |
| 125 |
shellTaskOrganizer.getRunningTaskInfo(taskId)?.let { task -> moveToFullscreen(task) } |
| 126 |
} |
| 127 |
|
| 128 |
/** Move a task to fullscreen */ |
| 129 |
fun moveToFullscreen(task: ActivityManager.RunningTaskInfo) { |
| 130 |
ProtoLog.v(WM_SHELL_DESKTOP_MODE, "moveToFullscreen: %d", task.taskId) |
| 131 |
|
| 132 |
val wct = WindowContainerTransaction() |
| 133 |
wct.setWindowingMode(task.getToken(), WINDOWING_MODE_FULLSCREEN) |
| 134 |
wct.setBounds(task.getToken(), null) |
| 135 |
if (Transitions.ENABLE_SHELL_TRANSITIONS) { |
| 136 |
transitions.startTransition(TRANSIT_CHANGE, wct, null /* handler */) |
| 137 |
} else { |
| 138 |
shellTaskOrganizer.applyTransaction(wct) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get windowing move for a given `taskId` |
| 144 |
* |
| 145 |
* @return [WindowingMode] for the task or [WINDOWING_MODE_UNDEFINED] if task is not found |
| 146 |
*/ |
| 147 |
@WindowingMode |
| 148 |
fun getTaskWindowingMode(taskId: Int): Int { |
| 149 |
return shellTaskOrganizer.getRunningTaskInfo(taskId)?.windowingMode |
| 150 |
?: WINDOWING_MODE_UNDEFINED |
| 151 |
} |
| 152 |
|
| 153 |
private fun bringDesktopAppsToFront(wct: WindowContainerTransaction) { |
| 154 |
val activeTasks = desktopModeTaskRepository.getActiveTasks() |
| 155 |
|
| 156 |
// Skip if all tasks are already visible |
| 157 |
if (activeTasks.isNotEmpty() && activeTasks.all(desktopModeTaskRepository::isVisibleTask)) { |
| 158 |
ProtoLog.d( |
| 159 |
WM_SHELL_DESKTOP_MODE, |
| 160 |
"bringDesktopAppsToFront: active tasks are already in front, skipping." |
| 161 |
) |
| 162 |
return |
| 163 |
} |
| 164 |
|
| 165 |
ProtoLog.v(WM_SHELL_DESKTOP_MODE, "bringDesktopAppsToFront") |
| 166 |
|
| 167 |
// First move home to front and then other tasks on top of it |
| 168 |
moveHomeTaskToFront(wct) |
| 169 |
|
| 170 |
val allTasksInZOrder = desktopModeTaskRepository.getFreeformTasksInZOrder() |
| 171 |
activeTasks |
| 172 |
// Sort descending as the top task is at index 0. It should be ordered to top last |
| 173 |
.sortedByDescending { taskId -> allTasksInZOrder.indexOf(taskId) } |
| 174 |
.mapNotNull { taskId -> shellTaskOrganizer.getRunningTaskInfo(taskId) } |
| 175 |
.forEach { task -> wct.reorder(task.token, true /* onTop */) } |
| 176 |
} |
| 177 |
|
| 178 |
private fun moveHomeTaskToFront(wct: WindowContainerTransaction) { |
| 179 |
shellTaskOrganizer |
| 180 |
.getRunningTasks(context.displayId) |
| 181 |
.firstOrNull { task -> task.activityType == ACTIVITY_TYPE_HOME } |
| 182 |
?.let { homeTask -> wct.reorder(homeTask.getToken(), true /* onTop */) } |
| 183 |
} |
| 184 |
|
| 185 |
override fun getContext(): Context { |
| 186 |
return context |
| 187 |
} |
| 188 |
|
| 189 |
override fun getRemoteCallExecutor(): ShellExecutor { |
| 190 |
return mainExecutor |
| 191 |
} |
| 192 |
|
| 193 |
override fun startAnimation( |
| 194 |
transition: IBinder, |
| 195 |
info: TransitionInfo, |
| 196 |
startTransaction: SurfaceControl.Transaction, |
| 197 |
finishTransaction: SurfaceControl.Transaction, |
| 198 |
finishCallback: Transitions.TransitionFinishCallback |
| 199 |
): Boolean { |
| 200 |
// This handler should never be the sole handler, so should not animate anything. |
| 201 |
return false |
| 202 |
} |
| 203 |
|
| 204 |
override fun handleRequest( |
| 205 |
transition: IBinder, |
| 206 |
request: TransitionRequestInfo |
| 207 |
): WindowContainerTransaction? { |
| 208 |
// Check if we should skip handling this transition |
| 209 |
val task: ActivityManager.RunningTaskInfo? = request.triggerTask |
| 210 |
val shouldHandleRequest = |
| 211 |
when { |
| 212 |
// Only handle open or to front transitions |
| 213 |
request.type != TRANSIT_OPEN && request.type != TRANSIT_TO_FRONT -> false |
| 214 |
// Only handle when it is a task transition |
| 215 |
task == null -> false |
| 216 |
// Only handle standard type tasks |
| 217 |
task.activityType != ACTIVITY_TYPE_STANDARD -> false |
| 218 |
// Only handle fullscreen or freeform tasks |
| 219 |
task.windowingMode != WINDOWING_MODE_FULLSCREEN && |
| 220 |
task.windowingMode != WINDOWING_MODE_FREEFORM -> false |
| 221 |
// Otherwise process it |
| 222 |
else -> true |
| 223 |
} |
| 224 |
|
| 225 |
if (!shouldHandleRequest) { |
| 226 |
return null |
| 227 |
} |
| 228 |
|
| 229 |
val activeTasks = desktopModeTaskRepository.getActiveTasks() |
| 230 |
|
| 231 |
// Check if we should switch a fullscreen task to freeform |
| 232 |
if (task?.windowingMode == WINDOWING_MODE_FULLSCREEN) { |
| 233 |
// If there are any visible desktop tasks, switch the task to freeform |
| 234 |
if (activeTasks.any { desktopModeTaskRepository.isVisibleTask(it) }) { |
| 235 |
ProtoLog.d( |
| 236 |
WM_SHELL_DESKTOP_MODE, |
| 237 |
"DesktopTasksController#handleRequest: switch fullscreen task to freeform," + |
| 238 |
" taskId=%d", |
| 239 |
task.taskId |
| 240 |
) |
| 241 |
return WindowContainerTransaction().apply { |
| 242 |
setWindowingMode(task.token, WINDOWING_MODE_FREEFORM) |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
// CHeck if we should switch a freeform task to fullscreen |
| 248 |
if (task?.windowingMode == WINDOWING_MODE_FREEFORM) { |
| 249 |
// If no visible desktop tasks, switch this task to freeform as the transition came |
| 250 |
// outside of this controller |
| 251 |
if (activeTasks.none { desktopModeTaskRepository.isVisibleTask(it) }) { |
| 252 |
ProtoLog.d( |
| 253 |
WM_SHELL_DESKTOP_MODE, |
| 254 |
"DesktopTasksController#handleRequest: switch freeform task to fullscreen," + |
| 255 |
" taskId=%d", |
| 256 |
task.taskId |
| 257 |
) |
| 258 |
return WindowContainerTransaction().apply { |
| 259 |
setWindowingMode(task.token, WINDOWING_MODE_FULLSCREEN) |
| 260 |
setBounds(task.token, null) |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
return null |
| 265 |
} |
| 266 |
|
| 267 |
/** Creates a new instance of the external interface to pass to another process. */ |
| 268 |
private fun createExternalInterface(): ExternalInterfaceBinder { |
| 269 |
return IDesktopModeImpl(this) |
| 270 |
} |
| 271 |
|
| 272 |
/** Get connection interface between sysui and shell */ |
| 273 |
fun asDesktopMode(): DesktopMode { |
| 274 |
return desktopMode |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Adds a listener to find out about changes in the visibility of freeform tasks. |
| 279 |
* |
| 280 |
* @param listener the listener to add. |
| 281 |
* @param callbackExecutor the executor to call the listener on. |
| 282 |
*/ |
| 283 |
fun addListener(listener: VisibleTasksListener, callbackExecutor: Executor) { |
| 284 |
desktopModeTaskRepository.addVisibleTasksListener(listener, callbackExecutor) |
| 285 |
} |
| 286 |
|
| 287 |
/** The interface for calls from outside the shell, within the host process. */ |
| 288 |
@ExternalThread |
| 289 |
private inner class DesktopModeImpl : DesktopMode { |
| 290 |
override fun addListener(listener: VisibleTasksListener, callbackExecutor: Executor) { |
| 291 |
mainExecutor.execute { |
| 292 |
[email protected](listener, callbackExecutor) |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/** The interface for calls from outside the host process. */ |
| 298 |
@BinderThread |
| 299 |
private class IDesktopModeImpl(private var controller: DesktopTasksController?) : |
| 300 |
IDesktopMode.Stub(), ExternalInterfaceBinder { |
| 301 |
/** Invalidates this instance, preventing future calls from updating the controller. */ |
| 302 |
override fun invalidate() { |
| 303 |
controller = null |
| 304 |
} |
| 305 |
|
| 306 |
override fun showDesktopApps() { |
| 307 |
ExecutorUtils.executeRemoteCallWithTaskPermission( |
| 308 |
controller, |
| 309 |
"showDesktopApps", |
| 310 |
Consumer(DesktopTasksController::showDesktopApps) |
| 311 |
) |
| 312 |
} |
| 313 |
} |
| 314 |
} |