• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static com.android.managedprovisioning.analytics.AnalyticsUtils.CATEGORY_VIEW_UNKNOWN;
30 
31 import android.annotation.IntDef;
32 import android.app.admin.DevicePolicyEventLogger;
33 import android.content.Context;
34 import com.android.internal.annotations.VisibleForTesting;
35 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
36 import com.android.managedprovisioning.common.SettingsFacade;
37 
38 /**
39  * Utility class to log time.
40  */
41 public class TimeLogger {
42 
43     private final int mCategory;
44     private final Context mContext;
45     private final MetricsLoggerWrapper mMetricsLoggerWrapper;
46     private final AnalyticsUtils mAnalyticsUtils;
47     private final ProvisioningAnalyticsTracker mProvisioningTracker;
48     private Long mStartTime;
49 
50     @IntDef({
51             PROVISIONING_PROVISIONING_ACTIVITY_TIME_MS,
52             PROVISIONING_PREPROVISIONING_ACTIVITY_TIME_MS,
53             PROVISIONING_ENCRYPT_DEVICE_ACTIVITY_TIME_MS,
54             PROVISIONING_WEB_ACTIVITY_TIME_MS,
55             PROVISIONING_COPY_ACCOUNT_TASK_MS,
56             PROVISIONING_CREATE_PROFILE_TASK_MS,
57             PROVISIONING_START_PROFILE_TASK_MS,
58             PROVISIONING_DOWNLOAD_PACKAGE_TASK_MS,
59             PROVISIONING_INSTALL_PACKAGE_TASK_MS})
60     public @interface TimeCategory {}
61 
TimeLogger(Context context, @TimeCategory int category)62     public TimeLogger(Context context, @TimeCategory int category) {
63         this(context, category, new MetricsLoggerWrapper(), new AnalyticsUtils(),
64                 new ProvisioningAnalyticsTracker(
65                     MetricsWriterFactory.getMetricsWriter(context, new SettingsFacade()),
66                     new ManagedProvisioningSharedPreferences(context)));
67     }
68 
69     @VisibleForTesting
TimeLogger( Context context, int category, MetricsLoggerWrapper metricsLoggerWrapper, AnalyticsUtils analyticsUtils, ProvisioningAnalyticsTracker provisioningAnalyticsTracker)70     public TimeLogger(
71             Context context,
72             int category,
73             MetricsLoggerWrapper metricsLoggerWrapper,
74             AnalyticsUtils analyticsUtils,
75             ProvisioningAnalyticsTracker provisioningAnalyticsTracker) {
76         mContext = checkNotNull(context);
77         mCategory = checkNotNull(category);
78         mMetricsLoggerWrapper = checkNotNull(metricsLoggerWrapper);
79         mAnalyticsUtils = checkNotNull(analyticsUtils);
80         mProvisioningTracker = checkNotNull(provisioningAnalyticsTracker);
81     }
82 
83     /**
84      * Notifies start time to logger.
85      */
start()86     public void start() {
87         mStartTime = mAnalyticsUtils.elapsedRealTime();
88     }
89 
90     /**
91      * Notifies stop time to logger. Call is ignored if there is no start time.
92      */
stop()93     public void stop() {
94         // Ignore logging time if we couldn't find start time.
95         if (mStartTime != null) {
96             // Provisioning wouldn't run for 25 days, so int should be fine.
97             final int time = (int) (mAnalyticsUtils.elapsedRealTime() - mStartTime);
98             // Clear stored start time, we shouldn't log total time twice for same start time.
99             mStartTime = null;
100             mMetricsLoggerWrapper.logAction(mContext, mCategory, time);
101             final int devicePolicyEvent =
102                     AnalyticsUtils.getDevicePolicyEventForCategory(mCategory);
103             if (devicePolicyEvent != CATEGORY_VIEW_UNKNOWN) {
104                 mProvisioningTracker.logTimeLoggerEvent(devicePolicyEvent, time);
105             }
106         }
107     }
108 }
109