1 /*
2  * Copyright (C) 2023 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.car.carlauncher.calmmode;
18 
19 import android.annotation.IntDef;
20 import android.os.Build;
21 import android.util.Log;
22 
23 import com.android.car.carlauncher.CarLauncherStatsLog;
24 
25 import java.util.UUID;
26 
27 /**
28  * Helper class that directly interacts with {@link CarLauncherStatsLog}, a generated class that
29  * contains logging methods for CalmModeActivity.
30  * logging methods for CalmModeActivity.
31  */
32 public class CalmModeStatsLogHelper {
33     private static final String TAG = CalmModeStatsLogHelper.class.getSimpleName();
34     public static final String INTENT_EXTRA_CALM_MODE_LAUNCH_TYPE =
35             CalmModeLaunchType.class.getSimpleName();
36     private static CalmModeStatsLogHelper sInstance;
37     private long mSessionId;
38 
39     /**
40      * IntDef representing enum values of CarCalmModeEventReported.event_type.
41      */
42     @IntDef({
43             CalmModeEventType.UNSPECIFIED_EVENT_TYPE,
44             CalmModeEventType.SESSION_STARTED,
45             CalmModeEventType.SESSION_FINISHED,
46     })
47     public @interface CalmModeEventType {
48         int UNSPECIFIED_EVENT_TYPE =
49                 CarLauncherStatsLog
50                         .CAR_CALM_MODE_EVENT_REPORTED__EVENT_TYPE__UNSPECIFIED_EVENT_TYPE;
51         int SESSION_STARTED =
52                 CarLauncherStatsLog.CAR_CALM_MODE_EVENT_REPORTED__EVENT_TYPE__SESSION_STARTED;
53         int SESSION_FINISHED =
54                 CarLauncherStatsLog.CAR_CALM_MODE_EVENT_REPORTED__EVENT_TYPE__SESSION_FINISHED;
55     }
56 
57     /**
58      * IntDef representing enum values of CarCalmModeEventReported.launch_type.
59      */
60     @IntDef({
61             CalmModeLaunchType.UNSPECIFIED_LAUNCH_TYPE,
62             CalmModeLaunchType.SETTINGS,
63             CalmModeLaunchType.QUICK_CONTROLS,
64     })
65     public @interface CalmModeLaunchType {
66         int UNSPECIFIED_LAUNCH_TYPE =
67                 CarLauncherStatsLog
68                         .CAR_CALM_MODE_EVENT_REPORTED__LAUNCH_TYPE__UNSPECIFIED_LAUNCH_TYPE;
69         int SETTINGS =
70                 CarLauncherStatsLog.CAR_CALM_MODE_EVENT_REPORTED__LAUNCH_TYPE__SETTINGS;
71         int QUICK_CONTROLS =
72                 CarLauncherStatsLog.CAR_CALM_MODE_EVENT_REPORTED__LAUNCH_TYPE__QUICK_CONTROLS;
73     }
74 
75     /**
76      * Returns the current logging instance of CalmModeStatsLogHelper to write this devices'
77      * CarLauncherStatsModule.
78      *
79      * @return the logging instance of CalmModeStatsLogHelper.
80      */
getInstance()81     public static CalmModeStatsLogHelper getInstance() {
82         if (sInstance == null) {
83             sInstance = new CalmModeStatsLogHelper();
84         }
85         return sInstance;
86     }
87 
88     /**
89      * Logs that a new Calm mode session has started. Additionally, resets measurements and IDs such
90      * as session ID and start time.
91      */
logSessionStarted(@almModeLaunchType int launchType)92     public void logSessionStarted(@CalmModeLaunchType int launchType) {
93         mSessionId = UUID.randomUUID().getMostSignificantBits();
94         writeCarCalmModeEventReported(CalmModeEventType.SESSION_STARTED, launchType);
95     }
96 
97     /**
98      * Logs that the current Calm mode session has finished.
99      */
logSessionFinished()100     public void logSessionFinished() {
101         writeCarCalmModeEventReported(CalmModeEventType.SESSION_FINISHED);
102     }
103 
104     /**
105      * Writes to CarCalmModeEvent atom with {@code eventType} as the only field, and log all other
106      * fields as unspecified.
107      *
108      * @param eventType one of {@link CalmModeEventType}
109      */
writeCarCalmModeEventReported(int eventType)110     private void writeCarCalmModeEventReported(int eventType) {
111         writeCarCalmModeEventReported(
112                 eventType, /* launchType */ CalmModeLaunchType.UNSPECIFIED_LAUNCH_TYPE);
113     }
114 
115     /**
116      * Writes to CarCalmModeEvent atom with all the optional fields filled.
117      *
118      * @param eventType one of {@link CalmModeEventType}
119      * @param launchType one of {@link CalmModeLaunchType}
120      */
writeCarCalmModeEventReported(int eventType, int launchType)121     private void writeCarCalmModeEventReported(int eventType, int launchType) {
122         long eventId = UUID.randomUUID().getMostSignificantBits();
123         if (Build.isDebuggable()) {
124             Log.v(TAG, "writing CAR_CALM_MODE_EVENT_REPORTED. sessionId=" + mSessionId
125                     + ", eventId=" + eventId + "eventType= " + eventType
126                     + ", launchType=" + launchType);
127         }
128         CarLauncherStatsLog.write(
129                 /* atomId */ CarLauncherStatsLog.CAR_CALM_MODE_EVENT_REPORTED,
130                 /* sessionId */ mSessionId,
131                 /* eventId */ eventId,
132                 /* eventType */ eventType,
133                 /* launchType */ launchType);
134     }
135 }
136