• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.util.ArrayMap
20 import android.util.ArraySet
21 import java.util.concurrent.Executor
22 
23 /**
24  * Keeps track of task data related to desktop mode.
25  */
26 class DesktopModeTaskRepository {
27 
28     /**
29      * Set of task ids that are marked as active in desktop mode.
30      * Active tasks in desktop mode are freeform tasks that are visible or have been visible after
31      * desktop mode was activated.
32      * Task gets removed from this list when it vanishes. Or when desktop mode is turned off.
33      */
34     private val activeTasks = ArraySet<Int>()
35     private val visibleTasks = ArraySet<Int>()
36     // Tasks currently in freeform mode, ordered from top to bottom (top is at index 0).
37     private val freeformTasksInZOrder = mutableListOf<Int>()
38     private val activeTasksListeners = ArraySet<ActiveTasksListener>()
39     // Track visible tasks separately because a task may be part of the desktop but not visible.
40     private val visibleTasksListeners = ArrayMap<VisibleTasksListener, Executor>()
41 
42     /**
43      * Add a [ActiveTasksListener] to be notified of updates to active tasks in the repository.
44      */
addActiveTaskListenernull45     fun addActiveTaskListener(activeTasksListener: ActiveTasksListener) {
46         activeTasksListeners.add(activeTasksListener)
47     }
48 
49     /**
50      * Add a [VisibleTasksListener] to be notified when freeform tasks are visible or not.
51      */
addVisibleTasksListenernull52     fun addVisibleTasksListener(visibleTasksListener: VisibleTasksListener, executor: Executor) {
53         visibleTasksListeners.put(visibleTasksListener, executor)
54         executor.execute(
55                 Runnable { visibleTasksListener.onVisibilityChanged(visibleTasks.size > 0) })
56     }
57 
58     /**
59      * Remove a previously registered [ActiveTasksListener]
60      */
removeActiveTasksListenernull61     fun removeActiveTasksListener(activeTasksListener: ActiveTasksListener) {
62         activeTasksListeners.remove(activeTasksListener)
63     }
64 
65     /**
66      * Remove a previously registered [VisibleTasksListener]
67      */
removeVisibleTasksListenernull68     fun removeVisibleTasksListener(visibleTasksListener: VisibleTasksListener) {
69         visibleTasksListeners.remove(visibleTasksListener)
70     }
71 
72     /**
73      * Mark a task with given [taskId] as active.
74      *
75      * @return `true` if the task was not active
76      */
addActiveTasknull77     fun addActiveTask(taskId: Int): Boolean {
78         val added = activeTasks.add(taskId)
79         if (added) {
80             activeTasksListeners.onEach { it.onActiveTasksChanged() }
81         }
82         return added
83     }
84 
85     /**
86      * Remove task with given [taskId] from active tasks.
87      *
88      * @return `true` if the task was active
89      */
removeActiveTasknull90     fun removeActiveTask(taskId: Int): Boolean {
91         val removed = activeTasks.remove(taskId)
92         if (removed) {
93             activeTasksListeners.onEach { it.onActiveTasksChanged() }
94         }
95         return removed
96     }
97 
98     /**
99      * Check if a task with the given [taskId] was marked as an active task
100      */
isActiveTasknull101     fun isActiveTask(taskId: Int): Boolean {
102         return activeTasks.contains(taskId)
103     }
104 
105     /**
106      * Whether a task is visible.
107      */
isVisibleTasknull108     fun isVisibleTask(taskId: Int): Boolean {
109         return visibleTasks.contains(taskId)
110     }
111 
112     /**
113      * Get a set of the active tasks
114      */
getActiveTasksnull115     fun getActiveTasks(): ArraySet<Int> {
116         return ArraySet(activeTasks)
117     }
118 
119     /**
120      * Get a list of freeform tasks, ordered from top-bottom (top at index 0).
121      */
getFreeformTasksInZOrdernull122     fun getFreeformTasksInZOrder(): List<Int> {
123         return freeformTasksInZOrder
124     }
125 
126     /**
127      * Updates whether a freeform task with this id is visible or not and notifies listeners.
128      */
updateVisibleFreeformTasksnull129     fun updateVisibleFreeformTasks(taskId: Int, visible: Boolean) {
130         val prevCount: Int = visibleTasks.size
131         if (visible) {
132             visibleTasks.add(taskId)
133         } else {
134             visibleTasks.remove(taskId)
135         }
136         if (prevCount == 0 && visibleTasks.size == 1 ||
137                 prevCount > 0 && visibleTasks.size == 0) {
138             for ((listener, executor) in visibleTasksListeners) {
139                 executor.execute(
140                         Runnable { listener.onVisibilityChanged(visibleTasks.size > 0) })
141             }
142         }
143     }
144 
145     /**
146      * Get number of tasks that are marked as visible
147      */
getVisibleTaskCountnull148     fun getVisibleTaskCount(): Int {
149         return visibleTasks.size
150     }
151 
152     /**
153      * Add (or move if it already exists) the task to the top of the ordered list.
154      */
addOrMoveFreeformTaskToTopnull155     fun addOrMoveFreeformTaskToTop(taskId: Int) {
156         if (freeformTasksInZOrder.contains(taskId)) {
157             freeformTasksInZOrder.remove(taskId)
158         }
159         freeformTasksInZOrder.add(0, taskId)
160     }
161 
162     /**
163      * Remove the task from the ordered list.
164      */
removeFreeformTasknull165     fun removeFreeformTask(taskId: Int) {
166         freeformTasksInZOrder.remove(taskId)
167     }
168 
169     /**
170      * Defines interface for classes that can listen to changes for active tasks in desktop mode.
171      */
172     interface ActiveTasksListener {
173         /**
174          * Called when the active tasks change in desktop mode.
175          */
176         @JvmDefault
onActiveTasksChangednull177         fun onActiveTasksChanged() {}
178     }
179 
180     /**
181      * Defines interface for classes that can listen to changes for visible tasks in desktop mode.
182      */
183     interface VisibleTasksListener {
184         /**
185          * Called when the desktop starts or stops showing freeform tasks.
186          */
187         @JvmDefault
onVisibilityChangednull188         fun onVisibilityChanged(hasVisibleFreeformTasks: Boolean) {}
189     }
190 }
191