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.events 18 19 import android.tools.common.Timestamp 20 import kotlin.js.JsExport 21 22 /** 23 * An Event from the [EventLog] representing a window focus change or request. 24 * 25 * @param timestamp The wall clock time in nanoseconds when the entry was written. 26 * @param window The window that is the target of the focus event. 27 * @param type The type of focus event this is. 28 * @param reason The reason for the focus event. 29 * @param processId The process ID which wrote the log entry 30 * @param uid The UID which wrote the log entry, special UIDs are strings instead of numbers (e.g. 31 * root) 32 * @param threadId The thread which wrote the log entry 33 * @param tag The type tag code of the entry 34 */ 35 @JsExport 36 class FocusEvent( 37 timestamp: Timestamp, 38 val window: String, 39 val type: Type, 40 val reason: String, 41 processId: Int, 42 uid: String, 43 threadId: Int 44 ) : Event(timestamp, processId, uid, threadId, INPUT_FOCUS_TAG) { 45 enum class Type { 46 GAINED, 47 LOST, 48 REQUESTED 49 } 50 toStringnull51 override fun toString(): String { 52 return "$timestamp: Focus ${type.name} $window Reason=$reason" 53 } 54 hasFocusnull55 fun hasFocus(): Boolean { 56 return this.type == Type.GAINED 57 } 58 59 companion object { fromnull60 fun from( 61 timestamp: Timestamp, 62 processId: Int, 63 uid: String, 64 threadId: Int, 65 data: Array<String> 66 ) = 67 FocusEvent( 68 timestamp, 69 getWindowFromData(data[0]), 70 getFocusFromData(data[0]), 71 data[1].removePrefix("reason="), 72 processId, 73 uid, 74 threadId 75 ) 76 77 private fun getWindowFromData(data: String): String { 78 var expectedWhiteSpace = 2 79 return data.dropWhile { !it.isWhitespace() || --expectedWhiteSpace > 0 }.drop(1) 80 } 81 getFocusFromDatanull82 private fun getFocusFromData(data: String): Type { 83 return when { 84 data.contains(" entering ") -> Type.GAINED 85 data.contains(" leaving ") -> Type.LOST 86 else -> Type.REQUESTED 87 } 88 } 89 90 const val INPUT_FOCUS_TAG = "input_focus" 91 } 92 } 93