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.surfaceflinger.LayersTrace
20 
21 class TransitionChange(
22     val transitMode: TransitionType,
23     val layerId: Int,
24     val windowId: Int,
25 ) {
26 
toStringnull27     override fun toString(): String = Formatter(null, null).format(this)
28 
29     override fun hashCode(): Int {
30         var result = transitMode.hashCode()
31         result = 31 * result + layerId
32         result = 31 * result + windowId
33         return result
34     }
35 
equalsnull36     override fun equals(other: Any?): Boolean {
37         if (this === other) return true
38         if (other !is TransitionChange) return false
39 
40         if (transitMode != other.transitMode) return false
41         if (layerId != other.layerId) return false
42         return windowId == other.windowId
43     }
44 
45     class Formatter(val layersTrace: LayersTrace?, val wmTrace: WindowManagerTrace?) {
formatnull46         fun format(change: TransitionChange): String {
47             val layerName =
48                 layersTrace
49                     ?.entries
50                     ?.flatMap { it.flattenedLayers }
51                     ?.firstOrNull { it.id == change.layerId }
52                     ?.name
53 
54             val windowName =
55                 wmTrace
56                     ?.entries
57                     ?.flatMap { it.windowStates }
58                     ?.firstOrNull { it.id == change.windowId }
59                     ?.name
60 
61             return buildString {
62                 append("TransitionChange(")
63                 append("transitMode=${change.transitMode}, ")
64                 append("layerId=${change.layerId}, ")
65                 if (layerName != null) {
66                     append("layerName=$layerName, ")
67                 }
68                 append("windowId=${change.windowId}, ")
69                 if (windowName != null) {
70                     append("windowName=$windowName, ")
71                 }
72                 append(")")
73             }
74         }
75     }
76 }
77