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.common.traces.wm 18 19 import kotlin.js.JsExport 20 import kotlin.js.JsName 21 22 @JsExport 23 class WindowManagerTraceEntryBuilder { 24 private var policy: WindowManagerPolicy? = null 25 private var focusedApp = "" 26 private var focusedDisplayId = 0 27 private var focusedWindow = "" 28 private var inputMethodWindowAppToken = "" 29 private var isHomeRecentsComponent = false 30 private var isDisplayFrozen = false 31 private val pendingActivities = mutableListOf<String>() 32 private var root: RootWindowContainer? = null 33 private var keyguardControllerState: KeyguardControllerState? = null 34 private var where = "" 35 36 // Necessary for compatibility with JS number type 37 private var elapsedTimestamp: Long = 0L 38 private var realTimestamp: Long? = null 39 40 @JsName("setPolicy") <lambda>null41 fun setPolicy(value: WindowManagerPolicy?): WindowManagerTraceEntryBuilder = apply { 42 policy = value 43 } 44 45 @JsName("setFocusedApp") setFocusedAppnull46 fun setFocusedApp(value: String): WindowManagerTraceEntryBuilder = apply { focusedApp = value } 47 48 @JsName("setFocusedDisplayId") <lambda>null49 fun setFocusedDisplayId(value: Int): WindowManagerTraceEntryBuilder = apply { 50 focusedDisplayId = value 51 } 52 53 @JsName("setFocusedWindow") <lambda>null54 fun setFocusedWindow(value: String): WindowManagerTraceEntryBuilder = apply { 55 focusedWindow = value 56 } 57 58 @JsName("setInputMethodWindowAppToken") <lambda>null59 fun setInputMethodWindowAppToken(value: String): WindowManagerTraceEntryBuilder = apply { 60 inputMethodWindowAppToken = value 61 } 62 63 @JsName("setIsHomeRecentsComponent") <lambda>null64 fun setIsHomeRecentsComponent(value: Boolean): WindowManagerTraceEntryBuilder = apply { 65 isHomeRecentsComponent = value 66 } 67 68 @JsName("setIsDisplayFrozen") <lambda>null69 fun setIsDisplayFrozen(value: Boolean): WindowManagerTraceEntryBuilder = apply { 70 isDisplayFrozen = value 71 } 72 73 @JsName("setPendingActivities") <lambda>null74 fun setPendingActivities(value: Array<String>): WindowManagerTraceEntryBuilder = apply { 75 pendingActivities.addAll(value) 76 } 77 78 @JsName("setRoot") <lambda>null79 fun setRoot(value: RootWindowContainer?): WindowManagerTraceEntryBuilder = apply { 80 root = value 81 } 82 83 @JsName("setKeyguardControllerState") setKeyguardControllerStatenull84 fun setKeyguardControllerState( 85 value: KeyguardControllerState? 86 ): WindowManagerTraceEntryBuilder = apply { keyguardControllerState = value } 87 88 @JsName("setWhere") <lambda>null89 fun setWhere(value: String): WindowManagerTraceEntryBuilder = apply { where = value } 90 91 @JsName("setElapsedTimestamp") setElapsedTimestampnull92 fun setElapsedTimestamp(value: String): WindowManagerTraceEntryBuilder = 93 // Necessary for compatibility with JS number type 94 apply { elapsedTimestamp = value.toLong() } 95 96 @JsName("setRealToElapsedTimeOffsetNs") <lambda>null97 fun setRealToElapsedTimeOffsetNs(value: String?): WindowManagerTraceEntryBuilder = apply { 98 realTimestamp = 99 if (value != null && value.toLong() != 0L) { 100 value.toLong() + elapsedTimestamp 101 } else { 102 null 103 } 104 } 105 106 /** Constructs the window manager trace entry. */ 107 @JsName("build") buildnull108 fun build(): WindowManagerState { 109 val root = root ?: error("Root not set") 110 val keyguardControllerState = 111 keyguardControllerState ?: error("KeyguardControllerState not set") 112 113 return WindowManagerState( 114 elapsedTimestamp, 115 realTimestamp, 116 where, 117 policy, 118 focusedApp, 119 focusedDisplayId, 120 focusedWindow, 121 inputMethodWindowAppToken, 122 isHomeRecentsComponent, 123 isDisplayFrozen, 124 pendingActivities.toTypedArray(), 125 root, 126 keyguardControllerState 127 ) 128 } 129 } 130