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.systemui.mediaprojection.taskswitcher
18 
19 import android.app.ActivityManager.RunningTaskInfo
20 import android.app.IActivityTaskManager
21 import android.app.TaskStackListener
22 import android.content.Intent
23 import android.window.IWindowContainerToken
24 import android.window.WindowContainerToken
25 import com.android.systemui.util.mockito.any
26 import com.android.systemui.util.mockito.mock
27 import com.android.systemui.util.mockito.whenever
28 
29 class FakeActivityTaskManager {
30 
31     private val runningTasks = mutableListOf<RunningTaskInfo>()
32     private val taskTaskListeners = mutableListOf<TaskStackListener>()
33 
34     val activityTaskManager = mock<IActivityTaskManager>()
35 
36     init {
37         whenever(activityTaskManager.registerTaskStackListener(any())).thenAnswer {
38             taskTaskListeners += it.arguments[0] as TaskStackListener
39             return@thenAnswer Unit
40         }
41         whenever(activityTaskManager.unregisterTaskStackListener(any())).thenAnswer {
42             taskTaskListeners -= it.arguments[0] as TaskStackListener
43             return@thenAnswer Unit
44         }
45         whenever(activityTaskManager.getTasks(any(), any(), any(), any())).thenAnswer {
46             val maxNumTasks = it.arguments[0] as Int
47             return@thenAnswer runningTasks.take(maxNumTasks)
48         }
49         whenever(activityTaskManager.startActivityFromRecents(any(), any())).thenAnswer {
50             val taskId = it.arguments[0] as Int
51             val runningTask = runningTasks.find { runningTask -> runningTask.taskId == taskId }
52             if (runningTask != null) {
53                 moveTaskToForeground(runningTask)
54                 return@thenAnswer 0
55             } else {
56                 return@thenAnswer -1
57             }
58         }
59     }
60 
61     fun moveTaskToForeground(task: RunningTaskInfo) {
62         taskTaskListeners.forEach { it.onTaskMovedToFront(task) }
63     }
64 
65     fun addRunningTasks(vararg tasks: RunningTaskInfo) {
66         runningTasks += tasks
67     }
68 
69     companion object {
70 
71         fun createTask(
72             taskId: Int,
73             token: WindowContainerToken = createToken(),
74             baseIntent: Intent = Intent()
75         ) =
76             RunningTaskInfo().apply {
77                 this.taskId = taskId
78                 this.token = token
79                 this.baseIntent = baseIntent
80             }
81 
82         fun createToken(): WindowContainerToken {
83             val realToken = object : IWindowContainerToken.Stub() {}
84             return WindowContainerToken(realToken)
85         }
86     }
87 }
88