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 import android.tools.traces.component.IComponentMatcher
20 import android.tools.traces.wm.Utils.collectDescendants
21 
22 /**
23  * Represents an activity in the window manager hierarchy
24  *
25  * This is a generic object that is reused by both Flicker and Winscope and cannot access internal
26  * Java/Android functionality
27  */
28 class Activity(
29     val state: String,
30     val frontOfTask: Boolean,
31     val procId: Int,
32     val isTranslucent: Boolean,
33     private val windowContainer: WindowContainer
<lambda>null34 ) : WindowContainer by windowContainer {
35     /**
36      * Checks if the activity contains a [WindowState] matching [componentMatcher]
37      *
38      * @param componentMatcher Components to search
39      */
40     fun getWindows(componentMatcher: IComponentMatcher): Collection<WindowState> = getWindows {
41         componentMatcher.windowMatchesAnyOf(it)
42     }
43 
44     /**
45      * Checks if the activity contains a [WindowState] matching [componentMatcher]
46      *
47      * @param componentMatcher Components to search
48      */
49     fun hasWindow(componentMatcher: IComponentMatcher): Boolean =
50         getWindows(componentMatcher).isNotEmpty()
51 
52     internal fun hasWindowState(windowState: WindowState): Boolean =
53         getWindows { windowState == it }.isNotEmpty()
54 
55     private fun getWindows(predicate: (WindowState) -> Boolean) =
56         collectDescendants<WindowState> { predicate(it) }
57 
58     override fun toString(): String {
59         return "${this::class.simpleName}: {$token $title} state=$state visible=$isVisible"
60     }
61 
62     override fun equals(other: Any?): Boolean {
63         if (this === other) return true
64         if (other !is Activity) return false
65 
66         if (state != other.state) return false
67         if (frontOfTask != other.frontOfTask) return false
68         if (procId != other.procId) return false
69         if (isTranslucent != other.isTranslucent) return false
70         if (orientation != other.orientation) return false
71         if (title != other.title) return false
72         if (token != other.token) return false
73         if (windowContainer != other.windowContainer) return false
74 
75         return true
76     }
77 
78     override fun hashCode(): Int {
79         var result = super.hashCode()
80         result = 31 * result + state.hashCode()
81         result = 31 * result + frontOfTask.hashCode()
82         result = 31 * result + procId
83         result = 31 * result + isTranslucent.hashCode()
84         result = 31 * result + windowContainer.hashCode()
85         return result
86     }
87 }
88