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 android.tools.common.traces.component.IComponentMatcher 20 import android.tools.common.traces.wm.Utils.collectDescendants 21 import kotlin.js.JsExport 22 23 /** 24 * Represents an activity in the window manager hierarchy 25 * 26 * This is a generic object that is reused by both Flicker and Winscope and cannot access internal 27 * Java/Android functionality 28 */ 29 @JsExport 30 class Activity( 31 val state: String, 32 val frontOfTask: Boolean, 33 val procId: Int, 34 val isTranslucent: Boolean, 35 private val windowContainer: IWindowContainer <lambda>null36) : IWindowContainer by windowContainer { 37 /** 38 * Checks if the activity contains a [WindowState] matching [componentMatcher] 39 * 40 * @param componentMatcher Components to search 41 */ 42 fun getWindows(componentMatcher: IComponentMatcher): Array<WindowState> = getWindows { 43 componentMatcher.windowMatchesAnyOf(it) 44 } 45 46 /** 47 * Checks if the activity contains a [WindowState] matching [componentMatcher] 48 * 49 * @param componentMatcher Components to search 50 */ 51 fun hasWindow(componentMatcher: IComponentMatcher): Boolean = 52 getWindows(componentMatcher).isNotEmpty() 53 54 internal fun hasWindowState(windowState: WindowState): Boolean = 55 getWindows { windowState == it }.isNotEmpty() 56 57 private fun getWindows(predicate: (WindowState) -> Boolean) = 58 collectDescendants<WindowState> { predicate(it) } 59 60 override fun toString(): String { 61 return "${this::class.simpleName}: {$token $title} state=$state visible=$isVisible" 62 } 63 64 override fun equals(other: Any?): Boolean { 65 if (this === other) return true 66 if (other !is Activity) return false 67 68 if (state != other.state) return false 69 if (frontOfTask != other.frontOfTask) return false 70 if (procId != other.procId) return false 71 if (isTranslucent != other.isTranslucent) return false 72 if (orientation != other.orientation) return false 73 if (title != other.title) return false 74 if (token != other.token) return false 75 if (windowContainer != other.windowContainer) return false 76 77 return true 78 } 79 80 override fun hashCode(): Int { 81 var result = super.hashCode() 82 result = 31 * result + state.hashCode() 83 result = 31 * result + frontOfTask.hashCode() 84 result = 31 * result + procId 85 result = 31 * result + isTranslucent.hashCode() 86 result = 31 * result + windowContainer.hashCode() 87 return result 88 } 89 } 90