1 /*
2  * Copyright (C) 2014 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 package com.android.mail.analytics;
17 
18 import android.os.SystemClock;
19 
20 import com.google.common.collect.Maps;
21 
22 import java.util.Map;
23 
24 /**
25  * Generic static singleton timer that keeps track of start time of various events. It  logs the
26  * event's duration into Analytics using the provided naming information.
27  * This timer class supports multiple data points per event ("lapping").
28  *
29  * This class also holds some defaults constant IDs that we log. This provides an easy way to check
30  * what data we are logging as well as ensuring that the IDs are consistent when accessed by
31  * different classes.
32  */
33 public class AnalyticsTimer {
34     public static final String OPEN_CONV_VIEW_FROM_LIST = "open_conv_from_list";
35     public static final String COLD_START_LAUNCHER = "cold_start_to_list";
36     public static final String SEARCH_TO_LIST = "search_to_list";
37     public static final String COMPOSE_HTML_TO_SPAN = "compose_html_to_span";
38     public static final String COMPOSE_SPAN_TO_HTML = "compose_span_to_html";
39 
40     private final Map<String, Long> mStartTimes = Maps.newConcurrentMap();
41 
42     // Static singleton class to ensure that you can access the timer from anywhere in the code
43     private static final AnalyticsTimer mInstance = new AnalyticsTimer();
44 
AnalyticsTimer()45     private AnalyticsTimer() {}
46 
getInstance()47     public static AnalyticsTimer getInstance() {
48         return mInstance;
49     }
50 
51     /**
52      * Record the current time as the start time of the provided id. If the id has a previously
53      * recorded start time, that time is overwritten.
54      * @param id
55      */
trackStart(String id)56     public void trackStart(String id) {
57         mStartTimes.put(id, SystemClock.uptimeMillis());
58     }
59 
60     /**
61      * Logs the duration of the event with the provided category, name, and label.
62      * This method can be destructive, meaning that any additional calls without calling
63      * {@link AnalyticsTimer#trackStart(String)} will do nothing
64      * We allow the method to be destructive to prevent the following cases from happening:
65      *   - recurring methods that call this with irrelevant mapped start times.
66      *   - multiple entry ways to the method that calls this, thus misusing the
67      *     start time.
68      * With destructive read, we ensure that we only log the event that we care about and only once.
69      * @param id id of the event
70      * @param isDestructive if you are done with this tag (used for multiple data points per tag)
71      * @param category category for analytics logging
72      * @param name name for analytics logging
73      * @param label label for analytics logging
74      */
logDuration(String id, boolean isDestructive, String category, String name, String label)75     public void logDuration(String id, boolean isDestructive, String category, String name,
76             String label) {
77         try {
78             logDurationAndReturn(id, isDestructive, category, name, label);
79         } catch (IllegalStateException e) { }
80     }
81 
82 
83     /**
84      * Same as logDuration except with the logged time returned (or exception thrown)
85      * @return logged time in millis
86      * @throws java.lang.IllegalStateException
87      */
logDurationAndReturn(String id, boolean isDestructive, String category, String name, String label)88     public long logDurationAndReturn(String id, boolean isDestructive, String category, String name,
89             String label) throws IllegalStateException {
90         final Long value = isDestructive ? mStartTimes.remove(id) : mStartTimes.get(id);
91         if (value == null) {
92             throw new IllegalStateException("Trying to log id that doesn't exist: " + id);
93         }
94         final long time = SystemClock.uptimeMillis() - value;
95         Analytics.getInstance().sendTiming(category, time, name, label);
96         return time;
97     }
98 
99     /**
100      * Removes a previously recorded start time of the provided id, if it exists.
101      */
stopTracking(String id)102     public void stopTracking(String id) {
103         mStartTimes.remove(id);
104     }
105 }
106