Home | History | Annotate | Download | only in analytics
      1 /*
      2  * Copyright 2016, 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.managedprovisioning.analytics;
     18 
     19 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_COPY_ACCOUNT_TASK_MS;
     20 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_CREATE_PROFILE_TASK_MS;
     21 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_DOWNLOAD_PACKAGE_TASK_MS;
     22 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_ENCRYPT_DEVICE_ACTIVITY_TIME_MS;
     23 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_INSTALL_PACKAGE_TASK_MS;
     24 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_PREPROVISIONING_ACTIVITY_TIME_MS;
     25 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_PROVISIONING_ACTIVITY_TIME_MS;
     26 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_START_PROFILE_TASK_MS;
     27 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_WEB_ACTIVITY_TIME_MS;
     28 import static com.android.internal.util.Preconditions.checkNotNull;
     29 
     30 import android.annotation.IntDef;
     31 import android.content.Context;
     32 
     33 import com.android.internal.annotations.VisibleForTesting;
     34 
     35 /**
     36  * Utility class to log time.
     37  */
     38 public class TimeLogger {
     39 
     40     private final int mCategory;
     41     private final Context mContext;
     42     private final MetricsLoggerWrapper mMetricsLoggerWrapper;
     43     private final AnalyticsUtils mAnalyticsUtils;
     44     private Long mStartTime;
     45 
     46     @IntDef({
     47             PROVISIONING_PROVISIONING_ACTIVITY_TIME_MS,
     48             PROVISIONING_PREPROVISIONING_ACTIVITY_TIME_MS,
     49             PROVISIONING_ENCRYPT_DEVICE_ACTIVITY_TIME_MS,
     50             PROVISIONING_WEB_ACTIVITY_TIME_MS,
     51             PROVISIONING_COPY_ACCOUNT_TASK_MS,
     52             PROVISIONING_CREATE_PROFILE_TASK_MS,
     53             PROVISIONING_START_PROFILE_TASK_MS,
     54             PROVISIONING_DOWNLOAD_PACKAGE_TASK_MS,
     55             PROVISIONING_INSTALL_PACKAGE_TASK_MS})
     56     public @interface TimeCategory {}
     57 
     58     public TimeLogger(Context context, @TimeCategory int category) {
     59         this(context, category, new MetricsLoggerWrapper(), new AnalyticsUtils());
     60     }
     61 
     62     @VisibleForTesting
     63     TimeLogger(
     64             Context context,
     65             int category,
     66             MetricsLoggerWrapper metricsLoggerWrapper,
     67             AnalyticsUtils analyticsUtils) {
     68         mContext = checkNotNull(context);
     69         mCategory = checkNotNull(category);
     70         mMetricsLoggerWrapper = checkNotNull(metricsLoggerWrapper);
     71         mAnalyticsUtils = checkNotNull(analyticsUtils);
     72     }
     73 
     74     /**
     75      * Notifies start time to logger.
     76      */
     77     public void start() {
     78         mStartTime = mAnalyticsUtils.elapsedRealTime();
     79     }
     80 
     81     /**
     82      * Notifies stop time to logger. Call is ignored if there is no start time.
     83      */
     84     public void stop() {
     85         // Ignore logging time if we couldn't find start time.
     86         if (mStartTime != null) {
     87             // Provisioning wouldn't run for 25 days, so int should be fine.
     88             final int time = (int) (mAnalyticsUtils.elapsedRealTime() - mStartTime);
     89             // Clear stored start time, we shouldn't log total time twice for same start time.
     90             mStartTime = null;
     91             mMetricsLoggerWrapper.logAction(mContext, mCategory, time);
     92         }
     93     }
     94 }
     95