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