• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.car.qc;
18 
19 import android.annotation.IntDef;
20 import android.os.Build;
21 import android.util.Log;
22 
23 import com.android.systemui.CarSystemUIStatsLog;
24 import com.android.systemui.dagger.SysUISingleton;
25 
26 import java.util.UUID;
27 
28 import javax.inject.Inject;
29 
30 /**
31  * Helper class that directly interacts with {@link CarSystemUIStatsLog}, a generated class that
32  * contains logging methods for DataSubscriptionController.
33  */
34 @SysUISingleton
35 public class DataSubscriptionStatsLogHelper {
36 
37     private static final String TAG = DataSubscriptionStatsLogHelper.class.getSimpleName();
38     private long mSessionId;
39     private int mCurrentMessageType;
40 
41     /**
42      * IntDef representing enum values of CarSystemUiDataSubscriptionEventReported.event_type.
43      */
44     @IntDef({
45             DataSubscriptionEventType.UNSPECIFIED_EVENT_TYPE,
46             DataSubscriptionEventType.SESSION_STARTED,
47             DataSubscriptionEventType.SESSION_FINISHED,
48             DataSubscriptionEventType.BUTTON_CLICKED,
49     })
50 
51     public @interface DataSubscriptionEventType {
52         int UNSPECIFIED_EVENT_TYPE =
53                 CarSystemUIStatsLog
54                         .CAR_SYSTEM_UI_DATA_SUBSCRIPTION_EVENT_REPORTED__EVENT_TYPE__UNSPECIFIED_EVENT_TYPE;
55         int SESSION_STARTED =
56                 CarSystemUIStatsLog
57                         .CAR_SYSTEM_UI_DATA_SUBSCRIPTION_EVENT_REPORTED__EVENT_TYPE__SESSION_STARTED;
58         int SESSION_FINISHED =
59                 CarSystemUIStatsLog
60                         .CAR_SYSTEM_UI_DATA_SUBSCRIPTION_EVENT_REPORTED__EVENT_TYPE__SESSION_FINISHED;
61         int BUTTON_CLICKED =
62                 CarSystemUIStatsLog
63                         .CAR_SYSTEM_UI_DATA_SUBSCRIPTION_EVENT_REPORTED__EVENT_TYPE__BUTTON_CLICKED;
64     }
65 
66     /**
67      * IntDef representing enum values of CarSystemUiDataSubscriptionEventReported.message_type.
68      */
69     @IntDef({
70             DataSubscriptionMessageType.UNSPECIFIED_MESSAGE_TYPE,
71             DataSubscriptionMessageType.PROACTIVE,
72             DataSubscriptionMessageType.REACTIVE,
73     })
74 
75     public @interface DataSubscriptionMessageType {
76 
77         int UNSPECIFIED_MESSAGE_TYPE =
78                 CarSystemUIStatsLog
79                         .CAR_SYSTEM_UI_DATA_SUBSCRIPTION_EVENT_REPORTED__MESSAGE_TYPE__UNSPECIFIED_MESSAGE_TYPE;
80         int PROACTIVE =
81                 CarSystemUIStatsLog
82                         .CAR_SYSTEM_UI_DATA_SUBSCRIPTION_EVENT_REPORTED__MESSAGE_TYPE__PROACTIVE;
83         int REACTIVE =
84                 CarSystemUIStatsLog
85                         .CAR_SYSTEM_UI_DATA_SUBSCRIPTION_EVENT_REPORTED__MESSAGE_TYPE__REACTIVE;
86     }
87 
88     /**
89      * Construct logging instance of DataSubscriptionStatsLogHelper.
90      */
91     @Inject
DataSubscriptionStatsLogHelper()92     public DataSubscriptionStatsLogHelper() {}
93 
94     /**
95      * Logs that a new Data Subscription session has started.
96      * Additionally, resets measurements and IDs such as
97      * session ID and start time.
98      */
logSessionStarted(@ataSubscriptionMessageType int messageType)99     public void logSessionStarted(@DataSubscriptionMessageType int messageType) {
100         mSessionId = UUID.randomUUID().getMostSignificantBits();
101         mCurrentMessageType = messageType;
102         writeDataSubscriptionEventReported(DataSubscriptionEventType.SESSION_STARTED, messageType);
103     }
104 
105     /**
106      * Logs that the current Data Subscription session has finished.
107      */
logSessionFinished()108     public void logSessionFinished() {
109         writeDataSubscriptionEventReported(DataSubscriptionEventType.SESSION_FINISHED);
110     }
111 
112     /**
113      * Logs that the "See plans" button is clicked. This method should be called after
114      * logSessionStarted() is called.
115      */
logButtonClicked()116     public void logButtonClicked() {
117         writeDataSubscriptionEventReported(DataSubscriptionEventType.BUTTON_CLICKED);
118     }
119 
120     /**
121      * Writes to CarSystemUiDataSubscriptionEventReported atom with {@code messageType} as the only
122      * field, and log all other fields as unspecified.
123      *
124      * @param eventType one of {@link DataSubscriptionEventType}
125      */
writeDataSubscriptionEventReported(int eventType)126     private void writeDataSubscriptionEventReported(int eventType) {
127         writeDataSubscriptionEventReported(
128                 eventType, /* messageType */ mCurrentMessageType);
129     }
130 
131     /**
132      * Writes to CarSystemUiDataSubscriptionEventReported atom with all the optional fields filled.
133      *
134      * @param eventType   one of {@link DataSubscriptionEventType}
135      * @param messageType one of {@link DataSubscriptionMessageType}
136      */
writeDataSubscriptionEventReported(int eventType, int messageType)137     private void writeDataSubscriptionEventReported(int eventType, int messageType) {
138         if (Build.isDebuggable()) {
139             Log.v(TAG, "writing CAR_SYSTEM_UI_DATA_SUBSCRIPTION_EVENT_REPORTED. sessionId="
140                     + mSessionId + ", eventType= " + eventType
141                     + ", messageType=" + messageType);
142         }
143         CarSystemUIStatsLog.write(
144                 /* atomId */ CarSystemUIStatsLog.CAR_SYSTEM_UI_DATA_SUBSCRIPTION_EVENT_REPORTED,
145                 /* sessionId */ mSessionId,
146                 /* eventType */ eventType,
147                 /* messageType */ messageType);
148     }
149 }
150