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
17package com.android.settings.core.instrumentation;
18
19import android.content.Context;
20import android.metrics.LogMaker;
21import android.util.Pair;
22
23import com.android.internal.logging.MetricsLogger;
24import com.android.internal.logging.nano.MetricsProto;
25
26/**
27 * {@link LogWriter} that writes data to eventlog.
28 */
29public class EventLogWriter implements LogWriter {
30
31    public void visible(Context context, int source, int category) {
32        final LogMaker logMaker = new LogMaker(category)
33                .setType(MetricsProto.MetricsEvent.TYPE_OPEN)
34                .addTaggedData(MetricsProto.MetricsEvent.FIELD_CONTEXT, source);
35        MetricsLogger.action(logMaker);
36    }
37
38    public void hidden(Context context, int category) {
39        MetricsLogger.hidden(context, category);
40    }
41
42    public void action(Context context, int category, Pair<Integer, Object>... taggedData) {
43        action(context, category, "", taggedData);
44    }
45
46    public void actionWithSource(Context context, int source, int category) {
47        final LogMaker logMaker = new LogMaker(category)
48                .setType(MetricsProto.MetricsEvent.TYPE_ACTION);
49        if (source != MetricsProto.MetricsEvent.VIEW_UNKNOWN) {
50            logMaker.addTaggedData(MetricsProto.MetricsEvent.FIELD_CONTEXT, source);
51        }
52        MetricsLogger.action(logMaker);
53    }
54
55    public void action(Context context, int category, int value) {
56        MetricsLogger.action(context, category, Integer.toString(value));
57    }
58
59    public void action(Context context, int category, boolean value) {
60        MetricsLogger.action(context, category, Boolean.toString(value));
61    }
62
63    public void action(Context context, int category, String pkg,
64            Pair<Integer, Object>... taggedData) {
65        if (taggedData == null || taggedData.length == 0) {
66            MetricsLogger.action(context, category, pkg);
67        } else {
68            final LogMaker logMaker = new LogMaker(category)
69                    .setType(MetricsProto.MetricsEvent.TYPE_ACTION)
70                    .setPackageName(pkg);
71            for (Pair<Integer, Object> pair : taggedData) {
72                logMaker.addTaggedData(pair.first, pair.second);
73            }
74            MetricsLogger.action(logMaker);
75        }
76    }
77
78    public void count(Context context, String name, int value) {
79        MetricsLogger.count(context, name, value);
80    }
81
82    public void histogram(Context context, String name, int bucket) {
83        MetricsLogger.histogram(context, name, bucket);
84    }
85}
86