1 /* 2 * Copyright (C) 2018 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.google.android.setupcompat.logging; 18 19 import android.content.Context; 20 import androidx.annotation.NonNull; 21 import com.google.android.setupcompat.internal.Preconditions; 22 import com.google.android.setupcompat.internal.SetupCompatServiceInvoker; 23 import com.google.android.setupcompat.logging.internal.MetricBundleConverter; 24 import com.google.android.setupcompat.logging.internal.SetupMetricsLoggingConstants.MetricType; 25 import java.util.concurrent.TimeUnit; 26 27 /** SetupMetricsLogger provides an easy way to log custom metrics to SetupWizard. */ 28 public class SetupMetricsLogger { 29 30 /** Logs an instance of {@link CustomEvent} to SetupWizard. */ logCustomEvent(@onNull Context context, @NonNull CustomEvent customEvent)31 public static void logCustomEvent(@NonNull Context context, @NonNull CustomEvent customEvent) { 32 Preconditions.checkNotNull(context, "Context cannot be null."); 33 Preconditions.checkNotNull(customEvent, "CustomEvent cannot be null."); 34 SetupCompatServiceInvoker.get(context) 35 .logMetricEvent( 36 MetricType.CUSTOM_EVENT, MetricBundleConverter.createBundleForLogging(customEvent)); 37 } 38 39 /** Increments the counter value with the name {@code counterName} by {@code times}. */ logCounter( @onNull Context context, @NonNull MetricKey counterName, int times)40 public static void logCounter( 41 @NonNull Context context, @NonNull MetricKey counterName, int times) { 42 Preconditions.checkNotNull(context, "Context cannot be null."); 43 Preconditions.checkNotNull(counterName, "CounterName cannot be null."); 44 Preconditions.checkArgument(times > 0, "Counter cannot be negative."); 45 SetupCompatServiceInvoker.get(context) 46 .logMetricEvent( 47 MetricType.COUNTER_EVENT, 48 MetricBundleConverter.createBundleForLoggingCounter(counterName, times)); 49 } 50 51 /** 52 * Logs the {@link Timer}'s duration by calling {@link #logDuration(Context, MetricKey, long)}. 53 */ logDuration(@onNull Context context, @NonNull Timer timer)54 public static void logDuration(@NonNull Context context, @NonNull Timer timer) { 55 Preconditions.checkNotNull(context, "Context cannot be null."); 56 Preconditions.checkNotNull(timer, "Timer cannot be null."); 57 Preconditions.checkArgument( 58 timer.isStopped(), "Timer should be stopped before calling logDuration."); 59 logDuration( 60 context, timer.getMetricKey(), TimeUnit.NANOSECONDS.toMillis(timer.getDurationInNanos())); 61 } 62 63 /** Logs a duration event to SetupWizard. */ logDuration( @onNull Context context, @NonNull MetricKey timerName, long timeInMillis)64 public static void logDuration( 65 @NonNull Context context, @NonNull MetricKey timerName, long timeInMillis) { 66 Preconditions.checkNotNull(context, "Context cannot be null."); 67 Preconditions.checkNotNull(timerName, "Timer name cannot be null."); 68 Preconditions.checkArgument(timeInMillis >= 0, "Duration cannot be negative."); 69 SetupCompatServiceInvoker.get(context) 70 .logMetricEvent( 71 MetricType.DURATION_EVENT, 72 MetricBundleConverter.createBundleForLoggingTimer(timerName, timeInMillis)); 73 } 74 } 75