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.parsers
18 
19 import android.app.ActivityTaskManager
20 import android.app.WindowConfiguration
21 import android.tools.traces.component.IComponentMatcher
22 
23 data class WaitForValidActivityState(
24     @JvmField val activityMatcher: IComponentMatcher?,
25     @JvmField val windowIdentifier: String?,
26     @JvmField val stackId: Int,
27     @JvmField val windowingMode: Int,
28     @JvmField val activityType: Int
29 ) {
30     constructor(
31         activityName: IComponentMatcher
32     ) : this(
33         activityName,
34         windowIdentifier = activityName.toWindowIdentifier(),
35         stackId = ActivityTaskManager.INVALID_STACK_ID,
36         windowingMode = WindowConfiguration.WINDOWING_MODE_UNDEFINED,
37         activityType = WindowConfiguration.ACTIVITY_TYPE_UNDEFINED
38     )
39 
40     private constructor(
41         builder: Builder
42     ) : this(
43         activityMatcher = builder.activityName,
44         windowIdentifier = builder.windowIdentifier,
45         stackId = builder.stackId,
46         windowingMode = builder.windowingMode,
47         activityType = builder.activityType
48     )
49 
toStringnull50     override fun toString(): String {
51         val sb = StringBuilder("wait:")
52         if (activityMatcher != null) {
53             sb.append(" activity=").append(activityMatcher.toActivityIdentifier())
54         }
55         if (activityType != WindowConfiguration.ACTIVITY_TYPE_UNDEFINED) {
56             sb.append(" type=").append(activityTypeName(activityType))
57         }
58         if (windowIdentifier != null) {
59             sb.append(" window=").append(windowIdentifier)
60         }
61         if (windowingMode != WindowConfiguration.WINDOWING_MODE_UNDEFINED) {
62             sb.append(" mode=").append(windowingModeName(windowingMode))
63         }
64         if (stackId != ActivityTaskManager.INVALID_STACK_ID) {
65             sb.append(" stack=").append(stackId)
66         }
67         return sb.toString()
68     }
69 
70     class Builder constructor(internal var activityName: IComponentMatcher? = null) {
71         internal var windowIdentifier: String? = activityName?.toWindowIdentifier()
72         internal var stackId = ActivityTaskManager.INVALID_STACK_ID
73         internal var windowingMode = WindowConfiguration.WINDOWING_MODE_UNDEFINED
74         internal var activityType = WindowConfiguration.ACTIVITY_TYPE_UNDEFINED
75 
setWindowIdentifiernull76         fun setWindowIdentifier(windowIdentifier: String): Builder {
77             this.windowIdentifier = windowIdentifier
78             return this
79         }
80 
setStackIdnull81         fun setStackId(stackId: Int): Builder {
82             this.stackId = stackId
83             return this
84         }
85 
setWindowingModenull86         fun setWindowingMode(windowingMode: Int): Builder {
87             this.windowingMode = windowingMode
88             return this
89         }
90 
setActivityTypenull91         fun setActivityType(activityType: Int): Builder {
92             this.activityType = activityType
93             return this
94         }
95 
buildnull96         fun build(): WaitForValidActivityState {
97             return WaitForValidActivityState(this)
98         }
99     }
100 
101     companion object {
102         @JvmStatic
forWindownull103         fun forWindow(windowName: String): WaitForValidActivityState {
104             return Builder().setWindowIdentifier(windowName).build()
105         }
106 
windowingModeNamenull107         private fun windowingModeName(windowingMode: Int): String {
108             return when (windowingMode) {
109                 WindowConfiguration.WINDOWING_MODE_UNDEFINED -> "UNDEFINED"
110                 WindowConfiguration.WINDOWING_MODE_FULLSCREEN -> "FULLSCREEN"
111                 WindowConfiguration.WINDOWING_MODE_PINNED -> "PINNED"
112                 WindowConfiguration.WINDOWING_MODE_FREEFORM -> "FREEFORM"
113                 else -> throw IllegalArgumentException("Unknown WINDOWING_MODE_: $windowingMode")
114             }
115         }
116 
activityTypeNamenull117         private fun activityTypeName(activityType: Int): String {
118             return when (activityType) {
119                 WindowConfiguration.ACTIVITY_TYPE_UNDEFINED -> "UNDEFINED"
120                 WindowConfiguration.ACTIVITY_TYPE_STANDARD -> "STANDARD"
121                 WindowConfiguration.ACTIVITY_TYPE_HOME -> "HOME"
122                 WindowConfiguration.ACTIVITY_TYPE_RECENTS -> "RECENTS"
123                 WindowConfiguration.ACTIVITY_TYPE_ASSISTANT -> "ASSISTANT"
124                 else -> throw IllegalArgumentException("Unknown ACTIVITY_TYPE_: $activityType")
125             }
126         }
127     }
128 }
129