1 /*
2  * Copyright (C) 2021 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 com.android.server.wm.traces.common.windowmanager
18 
19 import com.android.server.wm.traces.common.ITrace
20 
21 /**
22  * Contains a collection of parsed WindowManager trace entries and assertions to apply over a single
23  * entry.
24  *
25  * Each entry is parsed into a list of [WindowManagerState] objects.
26  *
27  * This is a generic object that is reused by both Flicker and Winscope and cannot
28  * access internal Java/Android functionality
29  *
30  */
31 data class WindowManagerTrace(
32     override val entries: Array<WindowManagerState>,
33     override val source: String
34 ) : ITrace<WindowManagerState>,
<lambda>null35     List<WindowManagerState> by entries.toList() {
36     override fun toString(): String {
37         return "WindowManagerTrace(Start: ${entries.first()}, " +
38             "End: ${entries.last()})"
39     }
40 
41     override fun equals(other: Any?): Boolean {
42         if (this === other) return true
43         if (other !is WindowManagerTrace) return false
44 
45         if (!entries.contentEquals(other.entries)) return false
46         if (source != other.source) return false
47 
48         return true
49     }
50 
51     override fun hashCode(): Int {
52         var result = entries.contentHashCode()
53         result = 31 * result + source.hashCode()
54         return result
55     }
56 
57     /**
58      * Split the trace by the start and end timestamp.
59      *
60      * @param from the start timestamp
61      * @param to the end timestamp
62      * @return the subtrace trace(from, to)
63      */
64     fun filter(from: Long, to: Long): WindowManagerTrace {
65         return WindowManagerTrace(
66             this.entries
67                 .dropWhile { it.timestamp < from }
68                 .dropLastWhile { it.timestamp > to }
69                 .toTypedArray(),
70             source = "")
71     }
72 }
73