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.text.TextUtils;
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     @Override
visible(Context context, int source, int category, int latency)33     public void visible(Context context, int source, int category, int latency) {
34         final LogMaker logMaker = new LogMaker(category)
35                 .setType(MetricsProto.MetricsEvent.TYPE_OPEN)
36                 .addTaggedData(MetricsProto.MetricsEvent.FIELD_CONTEXT, source)
37                 .addTaggedData(MetricsProto.MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_INT_VALUE,
38                         latency);
39         MetricsLogger.action(logMaker);
40     }
41 
42     @Override
hidden(Context context, int category, int visibleTime)43     public void hidden(Context context, int category, int visibleTime) {
44         final LogMaker logMaker = new LogMaker(category)
45                 .setType(MetricsProto.MetricsEvent.TYPE_CLOSE)
46                 .addTaggedData(MetricsProto.MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_INT_VALUE,
47                         visibleTime);
48         MetricsLogger.action(logMaker);
49     }
50 
51     @Override
action(Context context, int category, Pair<Integer, Object>... taggedData)52     public void action(Context context, int category, Pair<Integer, Object>... taggedData) {
53         final LogMaker logMaker = new LogMaker(category)
54                 .setType(MetricsProto.MetricsEvent.TYPE_ACTION);
55         if (taggedData != null) {
56             for (Pair<Integer, Object> pair : taggedData) {
57                 logMaker.addTaggedData(pair.first, pair.second);
58             }
59         }
60         MetricsLogger.action(logMaker);
61     }
62 
63     @Override
action(Context context, int category, int value)64     public void action(Context context, int category, int value) {
65         MetricsLogger.action(context, category, value);
66     }
67 
68     @Override
action(Context context, int category, boolean value)69     public void action(Context context, int category, boolean value) {
70         MetricsLogger.action(context, category, value);
71     }
72 
73     @Override
action(Context context, int category, String pkg)74     public void action(Context context, int category, String pkg) {
75         final LogMaker logMaker = new LogMaker(category)
76                 .setType(MetricsProto.MetricsEvent.TYPE_ACTION)
77                 .setPackageName(pkg);
78 
79         MetricsLogger.action(logMaker);
80     }
81 
82     @Override
action(int attribution, int action, int pageId, String key, int value)83     public void action(int attribution, int action, int pageId, String key, int value) {
84         final LogMaker logMaker = new LogMaker(action)
85                 .setType(MetricsProto.MetricsEvent.TYPE_ACTION);
86         if (attribution != MetricsProto.MetricsEvent.VIEW_UNKNOWN) {
87             logMaker.addTaggedData(MetricsProto.MetricsEvent.FIELD_CONTEXT, pageId);
88         }
89         if (!TextUtils.isEmpty(key)) {
90             logMaker.addTaggedData(MetricsProto.MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_NAME,
91                     key);
92             logMaker.addTaggedData(
93                     MetricsProto.MetricsEvent.FIELD_SETTINGS_PREFERENCE_CHANGE_INT_VALUE,
94                     value);
95         }
96         MetricsLogger.action(logMaker);
97     }
98 }
99