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.Rotation
20 import android.tools.Timestamp
21 import android.tools.Trace
22 
23 /**
24  * Contains a collection of parsed WindowManager trace entries and assertions to apply over a single
25  * entry.
26  *
27  * Each entry is parsed into a list of [WindowManagerState] objects.
28  *
29  * This is a generic object that is reused by both Flicker and Winscope and cannot access internal
30  * Java/Android functionality
31  */
32 data class WindowManagerTrace(override val entries: Collection<WindowManagerState>) :
33     Trace<WindowManagerState> {
34 
35     val isTablet: Boolean
<lambda>null36         get() = entries.any { it.isTablet }
37 
toStringnull38     override fun toString(): String {
39         return "WindowManagerTrace(Start: ${entries.firstOrNull()}, " +
40             "End: ${entries.lastOrNull()})"
41     }
42 
43     /** Get the initial rotation */
getInitialRotationnull44     fun getInitialRotation(): Rotation {
45         if (entries.isEmpty()) {
46             throw RuntimeException("WindowManager Trace has no entries")
47         }
48         val firstWmState = entries.first()
49         return firstWmState.policy?.rotation
50             ?: run { throw RuntimeException("Wm state has no policy") }
51     }
52 
53     /** Get the final rotation */
getFinalRotationnull54     fun getFinalRotation(): Rotation {
55         if (entries.isEmpty()) {
56             throw RuntimeException("WindowManager Trace has no entries")
57         }
58         val lastWmState = entries.last()
59         return lastWmState.policy?.rotation
60             ?: run { throw RuntimeException("Wm state has no policy") }
61     }
62 
slicenull63     override fun slice(startTimestamp: Timestamp, endTimestamp: Timestamp): WindowManagerTrace {
64         return WindowManagerTrace(
65             entries
66                 .dropWhile { it.timestamp < startTimestamp }
67                 .dropLastWhile { it.timestamp > endTimestamp }
68         )
69     }
70 
getWindowDescriptorByIdnull71     fun getWindowDescriptorById(id: Int): WindowDescriptor? {
72         for (entry in this.entries) {
73             for (window in entry.windowContainers) {
74                 if (window.id == id) {
75                     return WindowDescriptor(window)
76                 }
77             }
78         }
79         return null
80     }
81 }
82