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.tv.settings.util;
18 
19 import android.app.tvsettings.TvSettingsEnums;
20 import android.util.Log;
21 
22 import com.android.tv.twopanelsettings.slices.TvSettingsStatsLog;
23 
24 /**
25  * Utility class for instrumentation methods.
26  */
27 public final class InstrumentationUtils {
28 
29     private static final String TAG = "InstrumentationUtils";
30 
31     /**
32      * Log the PAGE_FOCUSED event to statsd.
33      *
34      * @param pageId the id of the focused page
35      * @param forward whether the page is focused in the forward navigation (deeper into the
36      *                setting tree)
37      */
logPageFocused(int pageId, Boolean forward)38     public static void logPageFocused(int pageId, Boolean forward) {
39         // It is necessary to use try-catch as StatsLog.write() could crash in extreme conditions.
40         try {
41             TvSettingsStatsLog.write(
42                     TvSettingsStatsLog.TVSETTINGS_UI_INTERACTED,
43                     forward != null
44                             ? (forward
45                                     ? TvSettingsEnums.PAGE_FOCUSED_FORWARD
46                                     : TvSettingsEnums.PAGE_FOCUSED_BACKWARD)
47                             : TvSettingsEnums.PAGE_FOCUSED,
48                     pageId);
49         } catch (Exception e) {
50             Log.e(TAG, "Unable to log PAGE_FOCUSED for id: " + pageId + " " + e);
51         }
52     }
53 
54     /**
55      * Log the ENTRY_SELECTED event with additional information to statsd.
56      *
57      * @param entryId the id of the selected entry
58      */
logEntrySelected(int entryId)59     public static void logEntrySelected(int entryId) {
60         // It is necessary to use try-catch as StatsLog.write() could crash in extreme conditions.
61         try {
62             TvSettingsStatsLog.write(
63                     TvSettingsStatsLog.TVSETTINGS_UI_INTERACTED,
64                     TvSettingsEnums.ENTRY_SELECTED,
65                     entryId);
66         } catch (Exception e) {
67             Log.e(TAG, "Unable to log ENTRY_SELECTED for id: " + entryId + " " + e);
68         }
69     }
70 
71     /**
72      * Log the TOGGLE_INTERACTED event to statsd.
73      *
74      * @param toggleId the id of the interacted toggle
75      * @param toggledOn whether the toggle is being flipped on
76      */
logToggleInteracted(int toggleId, Boolean toggledOn)77     public static void logToggleInteracted(int toggleId, Boolean toggledOn) {
78         // It is necessary to use try-catch as StatsLog.write() could crash in extreme conditions.
79         try {
80             TvSettingsStatsLog.write(
81                     TvSettingsStatsLog.TVSETTINGS_UI_INTERACTED,
82                     toggledOn != null
83                             ? (toggledOn
84                                     ? TvSettingsEnums.TOGGLED_ON
85                                     : TvSettingsEnums.TOGGLED_OFF)
86                             : TvSettingsEnums.TOGGLE_INTERACTED,
87                     toggleId);
88         } catch (Exception e) {
89             Log.e(TAG, "Unable to log TOGGLE_INTERACTED for id: " + toggleId + " " + e);
90         }
91     }
92 
93     /** Prevent this class from being accidentally instantiated. */
InstrumentationUtils()94     private InstrumentationUtils() {
95     }
96 }
97