1 package android.platform.uiautomator_helpers 2 3 import android.os.Trace 4 import android.util.Log 5 6 /** Tracing utils until androidx tracing library is updated in the tree. */ 7 @Deprecated("Use com.android.app.tracing utils instead.") 8 object TracingUtils { 9 10 // from frameworks/base/core/java/android/os/Trace.java MAX_SECTION_NAME_LEN. 11 private const val MAX_TRACE_NAME_LEN = 127 12 private const val TAG = "TracingUtils" 13 14 @Deprecated( 15 "Use com.android.app.tracing.traceSection instead", 16 replaceWith = ReplaceWith("com.android.app.tracing.traceSection(sectionName, block)") 17 ) tracenull18 inline fun <T> trace(sectionName: String, block: () -> T): T { 19 Trace.beginSection(sectionName.shortenedIfNeeded()) 20 try { 21 return block() 22 } finally { 23 Trace.endSection() 24 } 25 } 26 27 /** Shortens the section name if it's too long. */ beginSectionSafenull28 fun beginSectionSafe(sectionName: String) { 29 Trace.beginSection(sectionName.shortenedIfNeeded()) 30 } 31 32 /** Shorten the length of a string to make it less than the limit for atraces. */ shortenedIfNeedednull33 fun String.shortenedIfNeeded(): String = 34 if (length > MAX_TRACE_NAME_LEN) { 35 Log.w(TAG, "Section name too long: \"$this\" (len=$length, max=$MAX_TRACE_NAME_LEN)") 36 substring(0, MAX_TRACE_NAME_LEN) 37 } else this 38 } 39