1 /*
2  * Copyright (C) 2020 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.systemui.shared.system;
18 
19 import android.annotation.IntDef;
20 import android.os.Build;
21 import android.view.View;
22 
23 import com.android.internal.jank.InteractionJankMonitor;
24 import com.android.internal.jank.InteractionJankMonitor.Configuration;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 public final class InteractionJankMonitorWrapper {
30     private static final String TAG = "JankMonitorWrapper";
31 
32     // Launcher journeys.
33     public static final int CUJ_APP_LAUNCH_FROM_RECENTS =
34             InteractionJankMonitor.CUJ_LAUNCHER_APP_LAUNCH_FROM_RECENTS;
35     public static final int CUJ_APP_LAUNCH_FROM_ICON =
36             InteractionJankMonitor.CUJ_LAUNCHER_APP_LAUNCH_FROM_ICON;
37     public static final int CUJ_APP_CLOSE_TO_HOME =
38             InteractionJankMonitor.CUJ_LAUNCHER_APP_CLOSE_TO_HOME;
39     public static final int CUJ_APP_CLOSE_TO_PIP =
40             InteractionJankMonitor.CUJ_LAUNCHER_APP_CLOSE_TO_PIP;
41     public static final int CUJ_QUICK_SWITCH =
42             InteractionJankMonitor.CUJ_LAUNCHER_QUICK_SWITCH;
43     public static final int CUJ_OPEN_ALL_APPS =
44             InteractionJankMonitor.CUJ_LAUNCHER_OPEN_ALL_APPS;
45     public static final int CUJ_ALL_APPS_SCROLL =
46             InteractionJankMonitor.CUJ_LAUNCHER_ALL_APPS_SCROLL;
47     public static final int CUJ_APP_LAUNCH_FROM_WIDGET =
48             InteractionJankMonitor.CUJ_LAUNCHER_APP_LAUNCH_FROM_WIDGET;
49 
50     @IntDef({
51             CUJ_APP_LAUNCH_FROM_RECENTS,
52             CUJ_APP_LAUNCH_FROM_ICON,
53             CUJ_APP_CLOSE_TO_HOME,
54             CUJ_APP_CLOSE_TO_PIP,
55             CUJ_QUICK_SWITCH,
56             CUJ_APP_LAUNCH_FROM_WIDGET,
57     })
58     @Retention(RetentionPolicy.SOURCE)
59     public @interface CujType {
60     }
61 
62     /**
63      * Begin a trace session.
64      *
65      * @param v       an attached view.
66      * @param cujType the specific {@link InteractionJankMonitor.CujType}.
67      */
begin(View v, @CujType int cujType)68     public static void begin(View v, @CujType int cujType) {
69         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return;
70         InteractionJankMonitor.getInstance().begin(v, cujType);
71     }
72 
73     /**
74      * Begin a trace session.
75      *
76      * @param v       an attached view.
77      * @param cujType the specific {@link InteractionJankMonitor.CujType}.
78      * @param timeout duration to cancel the instrumentation in ms
79      */
begin(View v, @CujType int cujType, long timeout)80     public static void begin(View v, @CujType int cujType, long timeout) {
81         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return;
82         Configuration.Builder builder =
83                 Configuration.Builder.withView(cujType, v)
84                         .setTimeout(timeout);
85         InteractionJankMonitor.getInstance().begin(builder);
86     }
87 
88     /**
89      * End a trace session.
90      *
91      * @param cujType the specific {@link InteractionJankMonitor.CujType}.
92      */
end(@ujType int cujType)93     public static void end(@CujType int cujType) {
94         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return;
95         InteractionJankMonitor.getInstance().end(cujType);
96     }
97 
98     /**
99      * Cancel the trace session.
100      */
cancel(@ujType int cujType)101     public static void cancel(@CujType int cujType) {
102         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return;
103         InteractionJankMonitor.getInstance().cancel(cujType);
104     }
105 }
106