1 /*
2  * Copyright (C) 2023 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 android.tools.traces.wm
18 
19 /**
20  * Represents a task fragment in the window manager hierarchy
21  *
22  * This is a generic object that is reused by both Flicker and Winscope and cannot access internal
23  * Java/Android functionality
24  */
25 class TaskFragment(
26     override val activityType: Int,
27     val displayId: Int,
28     val minWidth: Int,
29     val minHeight: Int,
30     private val windowContainer: WindowContainer
<lambda>null31 ) : WindowContainer by windowContainer {
32     val tasks: Collection<Task>
33         get() = this.children.reversed().filterIsInstance<Task>()
34 
35     val taskFragments: Collection<TaskFragment>
36         get() = this.children.reversed().filterIsInstance<TaskFragment>()
37 
38     val activities: Collection<Activity>
39         get() = this.children.reversed().filterIsInstance<Activity>()
40 
41     fun getActivity(predicate: (Activity) -> Boolean): Activity? {
42         var activity: Activity? = activities.firstOrNull { predicate(it) }
43         if (activity != null) {
44             return activity
45         }
46         for (task in tasks) {
47             activity = task.getActivity(predicate)
48             if (activity != null) {
49                 return activity
50             }
51         }
52         for (taskFragment in taskFragments) {
53             activity = taskFragment.getActivity(predicate)
54             if (activity != null) {
55                 return activity
56             }
57         }
58         return null
59     }
60 
61     override fun toString(): String {
62         return "${this::class.simpleName}: {$token $title} bounds=$bounds"
63     }
64 
65     override fun equals(other: Any?): Boolean {
66         if (this === other) return true
67         if (other !is TaskFragment) return false
68 
69         if (activityType != other.activityType) return false
70         if (displayId != other.displayId) return false
71         if (minWidth != other.minWidth) return false
72         if (minHeight != other.minHeight) return false
73         if (windowContainer != other.windowContainer) return false
74 
75         return true
76     }
77 
78     override fun hashCode(): Int {
79         var result = activityType
80         result = 31 * result + displayId
81         result = 31 * result + minWidth
82         result = 31 * result + minHeight
83         result = 31 * result + windowContainer.hashCode()
84         return result
85     }
86 }
87