Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3772f162 authored by Orhan Uysal's avatar Orhan Uysal Committed by Android (Google) Code Review
Browse files

Merge "Create a desktop shell command handler." into main

parents 8445077b 27b6942f
Loading
Loading
Loading
Loading
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell.desktopmode

import android.window.WindowContainerTransaction
import com.android.wm.shell.sysui.ShellCommandHandler
import java.io.PrintWriter

/**
 * Handles the shell commands for the DesktopTasksController.
 */
class DesktopModeShellCommandHandler(private val controller: DesktopTasksController) :
    ShellCommandHandler.ShellCommandActionHandler {

    override fun onShellCommand(args: Array<String>, pw: PrintWriter): Boolean {
        return when (args[0]) {
            "moveToDesktop" -> {
                if (!runMoveToDesktop(args, pw)) {
                    pw.println("Task not found. Please enter a valid taskId.")
                    false
                } else {
                    true
                }
            }

            else -> {
                pw.println("Invalid command: ${args[0]}")
                false
            }
        }
    }

    private fun runMoveToDesktop(args: Array<String>, pw: PrintWriter): Boolean {
        if (args.size < 2) {
            // First argument is the action name.
            pw.println("Error: task id should be provided as arguments")
            return false
        }

        val taskId = try {
            args[1].toInt()
        } catch (e: NumberFormatException) {
            pw.println("Error: task id should be an integer")
            return false
        }

        return controller.moveToDesktopWithoutDecor(taskId, WindowContainerTransaction())
    }

    override fun printShellCommandHelp(pw: PrintWriter, prefix: String) {
        pw.println("$prefix moveToDesktop <taskId> ")
        pw.println("$prefix  Move a task with given id to desktop mode.")
    }
}
 No newline at end of file
+39 −0
Original line number Diff line number Diff line
@@ -100,6 +100,9 @@ class DesktopTasksController(

    private val desktopMode: DesktopModeImpl
    private var visualIndicator: DesktopModeVisualIndicator? = null
    private val desktopModeShellCommandHandler: DesktopModeShellCommandHandler =
        DesktopModeShellCommandHandler(this)

    private val mOnAnimationFinishedCallback = Consumer<SurfaceControl.Transaction> {
        t: SurfaceControl.Transaction ->
        visualIndicator?.releaseVisualIndicator(t)
@@ -148,6 +151,8 @@ class DesktopTasksController(
    private fun onInit() {
        KtProtoLog.d(WM_SHELL_DESKTOP_MODE, "Initialize DesktopTasksController")
        shellCommandHandler.addDumpCallback(this::dump, this)
        shellCommandHandler.addCommandCallback("desktopmode", desktopModeShellCommandHandler,
            this)
        shellController.addExternalInterface(
            ShellSharedConstants.KEY_EXTRA_SHELL_DESKTOP_MODE,
            { createExternalInterface() },
@@ -240,6 +245,40 @@ class DesktopTasksController(
        }
    }

    /** Move a task with given `taskId` to desktop without decor */
    fun moveToDesktopWithoutDecor(
        taskId: Int,
        wct: WindowContainerTransaction
    ): Boolean {
        val task = shellTaskOrganizer.getRunningTaskInfo(taskId) ?: return false
        moveToDesktopWithoutDecor(task, wct)
        return true
    }

    /**
     * Move a task to desktop without decor
     */
    private fun moveToDesktopWithoutDecor(
        task: RunningTaskInfo,
        wct: WindowContainerTransaction
    ) {
        KtProtoLog.v(
            WM_SHELL_DESKTOP_MODE,
            "DesktopTasksController: moveToDesktopWithoutDecor taskId=%d",
            task.taskId
        )
        exitSplitIfApplicable(wct, task)
        // Bring other apps to front first
        bringDesktopAppsToFront(task.displayId, wct)
        addMoveToDesktopChanges(wct, task)

        if (Transitions.ENABLE_SHELL_TRANSITIONS) {
            transitions.startTransition(TRANSIT_CHANGE, wct, null /* handler */)
        } else {
            shellTaskOrganizer.applyTransaction(wct)
        }
    }

    /**
     * Move a task to desktop
     */