Home | History | Annotate | Download | only in instrumentation
      1 /*
      2  * Copyright (C) 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.settingslib.core.instrumentation;
     18 
     19 import android.content.Context;
     20 import android.metrics.LogMaker;
     21 import android.util.Log;
     22 import android.util.Pair;
     23 
     24 import com.android.internal.logging.MetricsLogger;
     25 import com.android.internal.logging.nano.MetricsProto;
     26 
     27 /**
     28  * {@link LogWriter} that writes data to eventlog.
     29  */
     30 public class EventLogWriter implements LogWriter {
     31 
     32     private final MetricsLogger mMetricsLogger = new MetricsLogger();
     33 
     34     public void visible(Context context, int source, int category) {
     35         final LogMaker logMaker = new LogMaker(category)
     36                 .setType(MetricsProto.MetricsEvent.TYPE_OPEN)
     37                 .addTaggedData(MetricsProto.MetricsEvent.FIELD_CONTEXT, source);
     38         MetricsLogger.action(logMaker);
     39     }
     40 
     41     public void hidden(Context context, int category) {
     42         MetricsLogger.hidden(context, category);
     43     }
     44 
     45     public void action(int category, int value, Pair<Integer, Object>... taggedData) {
     46         if (taggedData == null || taggedData.length == 0) {
     47             mMetricsLogger.action(category, value);
     48         } else {
     49             final LogMaker logMaker = new LogMaker(category)
     50                     .setType(MetricsProto.MetricsEvent.TYPE_ACTION)
     51                     .setSubtype(value);
     52             for (Pair<Integer, Object> pair : taggedData) {
     53                 logMaker.addTaggedData(pair.first, pair.second);
     54             }
     55             mMetricsLogger.write(logMaker);
     56         }
     57     }
     58 
     59     public void action(int category, boolean value, Pair<Integer, Object>... taggedData) {
     60         action(category, value ? 1 : 0, taggedData);
     61     }
     62 
     63     public void action(Context context, int category, Pair<Integer, Object>... taggedData) {
     64         action(context, category, "", taggedData);
     65     }
     66 
     67     public void actionWithSource(Context context, int source, int category) {
     68         final LogMaker logMaker = new LogMaker(category)
     69                 .setType(MetricsProto.MetricsEvent.TYPE_ACTION);
     70         if (source != MetricsProto.MetricsEvent.VIEW_UNKNOWN) {
     71             logMaker.addTaggedData(MetricsProto.MetricsEvent.FIELD_CONTEXT, source);
     72         }
     73         MetricsLogger.action(logMaker);
     74     }
     75 
     76     /** @deprecated use {@link #action(int, int, Pair[])} */
     77     @Deprecated
     78     public void action(Context context, int category, int value) {
     79         MetricsLogger.action(context, category, value);
     80     }
     81 
     82     /** @deprecated use {@link #action(int, boolean, Pair[])} */
     83     @Deprecated
     84     public void action(Context context, int category, boolean value) {
     85         MetricsLogger.action(context, category, value);
     86     }
     87 
     88     public void action(Context context, int category, String pkg,
     89             Pair<Integer, Object>... taggedData) {
     90         if (taggedData == null || taggedData.length == 0) {
     91             MetricsLogger.action(context, category, pkg);
     92         } else {
     93             final LogMaker logMaker = new LogMaker(category)
     94                     .setType(MetricsProto.MetricsEvent.TYPE_ACTION)
     95                     .setPackageName(pkg);
     96             for (Pair<Integer, Object> pair : taggedData) {
     97                 logMaker.addTaggedData(pair.first, pair.second);
     98             }
     99             MetricsLogger.action(logMaker);
    100         }
    101     }
    102 
    103     public void count(Context context, String name, int value) {
    104         MetricsLogger.count(context, name, value);
    105     }
    106 
    107     public void histogram(Context context, String name, int bucket) {
    108         MetricsLogger.histogram(context, name, bucket);
    109     }
    110 }
    111