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 com.android.wm.shell.common.desktopmode.DesktopModeTransitionSource.UNKNOWN
20 import com.android.wm.shell.sysui.ShellCommandHandler
21 import java.io.PrintWriter
22 
23 /** Handles the shell commands for the DesktopTasksController. */
24 class DesktopModeShellCommandHandler(private val controller: DesktopTasksController) :
25     ShellCommandHandler.ShellCommandActionHandler {
26 
onShellCommandnull27     override fun onShellCommand(args: Array<String>, pw: PrintWriter): Boolean {
28         return when (args[0]) {
29             "moveToDesktop" -> {
30                 if (!runMoveToDesktop(args, pw)) {
31                     pw.println("Task not found. Please enter a valid taskId.")
32                     false
33                 } else {
34                     true
35                 }
36             }
37             "moveToNextDisplay" -> {
38                 if (!runMoveToNextDisplay(args, pw)) {
39                     pw.println("Task not found. Please enter a valid taskId.")
40                     false
41                 } else {
42                     true
43                 }
44             }
45             else -> {
46                 pw.println("Invalid command: ${args[0]}")
47                 false
48             }
49         }
50     }
51 
runMoveToDesktopnull52     private fun runMoveToDesktop(args: Array<String>, pw: PrintWriter): Boolean {
53         if (args.size < 2) {
54             // First argument is the action name.
55             pw.println("Error: task id should be provided as arguments")
56             return false
57         }
58 
59         val taskId =
60             try {
61                 args[1].toInt()
62             } catch (e: NumberFormatException) {
63                 pw.println("Error: task id should be an integer")
64                 return false
65             }
66 
67         return controller.moveToDesktop(taskId, transitionSource = UNKNOWN)
68     }
69 
runMoveToNextDisplaynull70     private fun runMoveToNextDisplay(args: Array<String>, pw: PrintWriter): Boolean {
71         if (args.size < 2) {
72             // First argument is the action name.
73             pw.println("Error: task id should be provided as arguments")
74             return false
75         }
76 
77         val taskId =
78             try {
79                 args[1].toInt()
80             } catch (e: NumberFormatException) {
81                 pw.println("Error: task id should be an integer")
82                 return false
83             }
84 
85         controller.moveToNextDisplay(taskId)
86         return true
87     }
88 
printShellCommandHelpnull89     override fun printShellCommandHelp(pw: PrintWriter, prefix: String) {
90         pw.println("$prefix moveToDesktop <taskId> ")
91         pw.println("$prefix  Move a task with given id to desktop mode.")
92         pw.println("$prefix moveToNextDisplay <taskId> ")
93         pw.println("$prefix  Move a task with given id to next display.")
94     }
95 }
96