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