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.view;
18 
19 import android.os.Trace;
20 
21 /**
22  * Keeps and caches strings used to trace {@link View} traversals.
23  * <p>
24  * This is done to avoid expensive computations of them every time, which can improve performance.
25  */
26 class ViewTraversalTracingStrings {
27 
28     /** {@link Trace} tag used to mark {@link View#onMeasure(int, int)}. */
29     public final String onMeasure;
30 
31     /** {@link Trace} tag used to mark {@link View#onLayout(boolean, int, int, int, int)}. */
32     public final String onLayout;
33 
34     /** Caches the view simple name to avoid re-computations. */
35     public final String classSimpleName;
36 
37     /** Prefix for request layout stacktraces output in logs. */
38     public final String requestLayoutStacktracePrefix;
39 
40     /** {@link Trace} tag used to mark {@link View#onMeasure(int, int)} happening before layout. */
41     public final String onMeasureBeforeLayout;
42 
43     /**
44      * @param v {@link View} from where to get the class name.
45      */
ViewTraversalTracingStrings(View v)46     ViewTraversalTracingStrings(View v) {
47         String className = v.getClass().getSimpleName();
48         classSimpleName = className;
49         onMeasureBeforeLayout = getTraceName("onMeasureBeforeLayout", className, v);
50         onMeasure = getTraceName("onMeasure", className, v);
51         onLayout = getTraceName("onLayout", className, v);
52         requestLayoutStacktracePrefix = "requestLayout " + className;
53     }
54 
getTraceName(String sectionName, String className, View v)55     private String getTraceName(String sectionName, String className, View v) {
56         StringBuilder out = new StringBuilder();
57         out.append(sectionName);
58         out.append(" ");
59         out.append(className);
60         v.appendId(out);
61         return out.substring(0, Math.min(out.length() - 1, Trace.MAX_SECTION_NAME_LEN - 1));
62     }
63 }
64