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.component
18 
19 import android.tools.traces.surfaceflinger.Layer
20 import android.tools.traces.wm.Activity
21 import android.tools.traces.wm.WindowContainer
22 
23 interface IComponentMatcher {
ornull24     fun or(other: IComponentMatcher): IComponentMatcher {
25         return OrComponentMatcher(listOf(this, other))
26     }
27 
28     /**
29      * @param window to search
30      * @return if any of the components matches [window]
31      */
windowMatchesAnyOfnull32     fun windowMatchesAnyOf(window: WindowContainer): Boolean = windowMatchesAnyOf(listOf(window))
33 
34     /**
35      * @param windows to search
36      * @return if any of the [windows] fit the matching conditions of the matcher
37      */
38     fun windowMatchesAnyOf(windows: Collection<WindowContainer>): Boolean
39 
40     /**
41      * @param activity to search
42      * @return if any of the components matches [activity]
43      */
44     fun activityMatchesAnyOf(activity: Activity): Boolean = activityMatchesAnyOf(listOf(activity))
45 
46     /**
47      * @param activities to search
48      * @return if any of the components matches any of [activities]
49      */
50     fun activityMatchesAnyOf(activities: Collection<Activity>): Boolean
51 
52     /**
53      * @param layer to search
54      * @return if any of the components matches [layer]
55      */
56     fun layerMatchesAnyOf(layer: Layer): Boolean = layerMatchesAnyOf(listOf(layer))
57 
58     /**
59      * @param layers to search
60      * @return if any of the components matches any of [layers]
61      */
62     fun layerMatchesAnyOf(layers: Collection<Layer>): Boolean
63 
64     /**
65      * @return an identifier string that provides enough information to determine which activities
66      *
67      * ```
68      *         the matcher is looking to match. Mostly used for debugging purposes in error messages
69      * ```
70      */
71     fun toActivityIdentifier(): String
72 
73     /**
74      * @return an identifier string that provides enough information to determine which windows the
75      *
76      * ```
77      *         matcher is looking to match. Mostly used for debugging purposes in error messages.
78      * ```
79      */
80     fun toWindowIdentifier(): String
81 
82     /**
83      * @return an identifier string that provides enough information to determine which layers the
84      *
85      * ```
86      *         matcher is looking to match. Mostly used for debugging purposes in error messages.
87      * ```
88      */
89     fun toLayerIdentifier(): String
90 
91     /**
92      * @param layers Collection of layers check for matches
93      * @param condition A function taking the matched layers of a base level component and returning
94      *
95      * ```
96      *              true or false base on if the check succeeded.
97      * @return
98      * ```
99      *
100      * true iff all the check condition is satisfied according to the ComponentMatcher's
101      *
102      * ```
103      *         defined execution of it.
104      * ```
105      */
106     fun check(layers: Collection<Layer>, condition: (Collection<Layer>) -> Boolean): Boolean
107 
108     fun filterLayers(layers: Collection<Layer>): Collection<Layer> =
109         layers.filter { layerMatchesAnyOf(it) }
110 
filterWindowsnull111     fun filterWindows(windows: Collection<WindowContainer>): Collection<WindowContainer> =
112         windows.filter { windowMatchesAnyOf(it) }
113 }
114