1 /*
2  * Copyright (C) 2022 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 com.android.wm.shell.common;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.Context;
22 import android.text.TextUtils;
23 import android.view.SurfaceControl;
24 import android.view.View;
25 
26 import com.android.internal.jank.InteractionJankMonitor;
27 
28 /** Utils class for simplfy InteractionJank trancing call */
29 public class InteractionJankMonitorUtils {
30 
31     /**
32      * Begin a trace session.
33      *
34      * @param cujType the specific {@link InteractionJankMonitor.CujType}.
35      * @param view the view to trace
36      * @param tag the tag to distinguish different flow of same type CUJ.
37      */
beginTracing(@nteractionJankMonitor.CujType int cujType, @NonNull View view, @Nullable String tag)38     public static void beginTracing(@InteractionJankMonitor.CujType int cujType,
39             @NonNull View view, @Nullable String tag) {
40         final InteractionJankMonitor.Configuration.Builder builder =
41                 InteractionJankMonitor.Configuration.Builder.withView(cujType, view);
42         if (!TextUtils.isEmpty(tag)) {
43             builder.setTag(tag);
44         }
45         InteractionJankMonitor.getInstance().begin(builder);
46     }
47 
48     /**
49      * Begin a trace session.
50      *
51      * @param cujType the specific {@link InteractionJankMonitor.CujType}.
52      * @param context the context
53      * @param surface the surface to trace
54      * @param tag the tag to distinguish different flow of same type CUJ.
55      */
beginTracing(@nteractionJankMonitor.CujType int cujType, @NonNull Context context, @NonNull SurfaceControl surface, @Nullable String tag)56     public static void beginTracing(@InteractionJankMonitor.CujType int cujType,
57             @NonNull Context context, @NonNull SurfaceControl surface, @Nullable String tag) {
58         final InteractionJankMonitor.Configuration.Builder builder =
59                 InteractionJankMonitor.Configuration.Builder.withSurface(cujType, context, surface);
60         if (!TextUtils.isEmpty(tag)) {
61             builder.setTag(tag);
62         }
63         InteractionJankMonitor.getInstance().begin(builder);
64     }
65 
66     /**
67      * End a trace session.
68      *
69      * @param cujType the specific {@link InteractionJankMonitor.CujType}.
70      */
endTracing(@nteractionJankMonitor.CujType int cujType)71     public static void endTracing(@InteractionJankMonitor.CujType int cujType) {
72         InteractionJankMonitor.getInstance().end(cujType);
73     }
74 
75     /**
76      * Cancel the trace session.
77      *
78      * @param cujType the specific {@link InteractionJankMonitor.CujType}.
79      */
cancelTracing(@nteractionJankMonitor.CujType int cujType)80     public static void cancelTracing(@InteractionJankMonitor.CujType int cujType) {
81         InteractionJankMonitor.getInstance().cancel(cujType);
82     }
83 }
84