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 /**
25  * A component matcher that matches the targeted window and layer with ids windowId and layerId
26  * respectively and all their children windows and layers.
27  *
28  * If you want to only match the window and layer with the specified ids then use
29  * ExactComponentIdMatcher instead.
30  */
31 class FullComponentIdMatcher(val windowId: Int, val layerId: Int) : IComponentMatcher {
32     /**
33      * @param windows to search
34      * @return if any of the components matches any of [windows]
35      */
windowMatchesAnyOfnull36     override fun windowMatchesAnyOf(windows: Collection<WindowContainer>): Boolean =
37         windows.any {
38             val parent = it.parent
39             when {
40                 it.token == windowId.toString(16) -> true
41                 parent != null -> windowMatchesAnyOf(parent)
42                 else -> false
43             }
44         }
45 
46     /**
47      * @param activities to search
48      * @return if any of the components matches any of [activities]
49      */
activityMatchesAnyOfnull50     override fun activityMatchesAnyOf(activities: Collection<Activity>) =
51         activities.any { it.token == windowId.toString(16) }
52 
53     /**
54      * @param layers to search
55      * @return if any of the components matches any of [layers]
56      */
layerMatchesAnyOfnull57     override fun layerMatchesAnyOf(layers: Collection<Layer>) =
58         layers.any {
59             val parent = it.parent
60             when {
61                 it.id == layerId -> true
62                 parent != null -> layerMatchesAnyOf(parent)
63                 else -> false
64             }
65         }
66 
67     /** {@inheritDoc} */
checknull68     override fun check(
69         layers: Collection<Layer>,
70         condition: Predicate<Collection<Layer>>,
71     ): Boolean = condition.test(layers.filter { layerMatchesAnyOf(it) })
72 
73     /** {@inheritDoc} */
toActivityIdentifiernull74     override fun toActivityIdentifier() = toWindowIdentifier()
75 
76     /** {@inheritDoc} */
77     override fun toWindowIdentifier() = "Window#${windowId.toString(16)} & children"
78 
79     /** {@inheritDoc} */
80     override fun toLayerIdentifier(): String = "Layer#$layerId & children"
81 }
82