1/*
2 * Copyright (C) 2015 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 */
16package com.android.internal.logging;
17
18import android.content.Context;
19import android.metrics.LogMaker;
20import android.os.Build;
21import android.view.View;
22
23import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
24
25/**
26 * Log all the things.
27 *
28 * @hide
29 */
30public class MetricsLogger {
31    // define metric categories in frameworks/base/proto/src/metrics_constants.proto.
32    // mirror changes in native version at system/core/libmetricslogger/metrics_logger.cpp
33
34    private static MetricsLogger sMetricsLogger;
35
36    private static MetricsLogger getLogger() {
37        if (sMetricsLogger == null) {
38            sMetricsLogger = new MetricsLogger();
39        }
40        return sMetricsLogger;
41    }
42
43    protected void saveLog(Object[] rep) {
44        EventLogTags.writeSysuiMultiAction(rep);
45    }
46
47    public static final int VIEW_UNKNOWN = MetricsEvent.VIEW_UNKNOWN;
48    public static final int LOGTAG = EventLogTags.SYSUI_MULTI_ACTION;
49
50    public void write(LogMaker content) {
51        if (content.getType() == MetricsEvent.TYPE_UNKNOWN) {
52            content.setType(MetricsEvent.TYPE_ACTION);
53        }
54        saveLog(content.serialize());
55    }
56
57    public void visible(int category) throws IllegalArgumentException {
58        if (Build.IS_DEBUGGABLE && category == VIEW_UNKNOWN) {
59            throw new IllegalArgumentException("Must define metric category");
60        }
61        EventLogTags.writeSysuiViewVisibility(category, 100);
62        saveLog(new LogMaker(category)
63                        .setType(MetricsEvent.TYPE_OPEN)
64                        .serialize());
65    }
66
67    public void hidden(int category) throws IllegalArgumentException {
68        if (Build.IS_DEBUGGABLE && category == VIEW_UNKNOWN) {
69            throw new IllegalArgumentException("Must define metric category");
70        }
71        EventLogTags.writeSysuiViewVisibility(category, 0);
72        saveLog(new LogMaker(category)
73                        .setType(MetricsEvent.TYPE_CLOSE)
74                        .serialize());
75    }
76
77    public void visibility(int category, boolean visibile)
78            throws IllegalArgumentException {
79        if (visibile) {
80            visible(category);
81        } else {
82            hidden(category);
83        }
84    }
85
86    public void visibility(int category, int vis)
87            throws IllegalArgumentException {
88        visibility(category, vis == View.VISIBLE);
89    }
90
91    public void action(int category) {
92        EventLogTags.writeSysuiAction(category, "");
93        saveLog(new LogMaker(category)
94                        .setType(MetricsEvent.TYPE_ACTION)
95                        .serialize());
96    }
97
98    public void action(int category, int value) {
99        EventLogTags.writeSysuiAction(category, Integer.toString(value));
100        saveLog(new LogMaker(category)
101                        .setType(MetricsEvent.TYPE_ACTION)
102                        .setSubtype(value)
103                        .serialize());
104    }
105
106    public void action(int category, boolean value) {
107        EventLogTags.writeSysuiAction(category, Boolean.toString(value));
108        saveLog(new LogMaker(category)
109                        .setType(MetricsEvent.TYPE_ACTION)
110                        .setSubtype(value ? 1 : 0)
111                        .serialize());
112    }
113
114    public void action(int category, String pkg) {
115        if (Build.IS_DEBUGGABLE && category == VIEW_UNKNOWN) {
116            throw new IllegalArgumentException("Must define metric category");
117        }
118        EventLogTags.writeSysuiAction(category, pkg);
119        saveLog(new LogMaker(category)
120                .setType(MetricsEvent.TYPE_ACTION)
121                .setPackageName(pkg)
122                .serialize());
123    }
124
125    /** Add an integer value to the monotonically increasing counter with the given name. */
126    public void count(String name, int value) {
127        EventLogTags.writeSysuiCount(name, value);
128        saveLog(new LogMaker(MetricsEvent.RESERVED_FOR_LOGBUILDER_COUNTER)
129                        .setCounterName(name)
130                        .setCounterValue(value)
131                        .serialize());
132    }
133
134    /** Increment the bucket with the integer label on the histogram with the given name. */
135    public void histogram(String name, int bucket) {
136        // see LogHistogram in system/core/libmetricslogger/metrics_logger.cpp
137        EventLogTags.writeSysuiHistogram(name, bucket);
138        saveLog(new LogMaker(MetricsEvent.RESERVED_FOR_LOGBUILDER_HISTOGRAM)
139                        .setCounterName(name)
140                        .setCounterBucket(bucket)
141                        .setCounterValue(1)
142                        .serialize());
143    }
144
145    /** @deprecated use {@link #visible(int)} */
146    @Deprecated
147    public static void visible(Context context, int category) throws IllegalArgumentException {
148        getLogger().visible(category);
149    }
150
151    /** @deprecated use {@link #hidden(int)} */
152    @Deprecated
153    public static void hidden(Context context, int category) throws IllegalArgumentException {
154        getLogger().hidden(category);
155    }
156
157    /** @deprecated use {@link #visibility(int, boolean)} */
158    @Deprecated
159    public static void visibility(Context context, int category, boolean visibile)
160            throws IllegalArgumentException {
161        getLogger().visibility(category, visibile);
162    }
163
164    /** @deprecated use {@link #visibility(int, int)} */
165    @Deprecated
166    public static void visibility(Context context, int category, int vis)
167            throws IllegalArgumentException {
168        visibility(context, category, vis == View.VISIBLE);
169    }
170
171    /** @deprecated use {@link #action(int)} */
172    @Deprecated
173    public static void action(Context context, int category) {
174        getLogger().action(category);
175    }
176
177    /** @deprecated use {@link #action(int, int)} */
178    @Deprecated
179    public static void action(Context context, int category, int value) {
180        getLogger().action(category, value);
181    }
182
183    /** @deprecated use {@link #action(int, boolean)} */
184    @Deprecated
185    public static void action(Context context, int category, boolean value) {
186        getLogger().action(category, value);
187    }
188
189    /** @deprecated use {@link #write(LogMaker)} */
190    @Deprecated
191    public static void action(LogMaker content) {
192        getLogger().write(content);
193    }
194
195    /** @deprecated use {@link #action(int, String)} */
196    @Deprecated
197    public static void action(Context context, int category, String pkg) {
198        getLogger().action(category, pkg);
199    }
200
201    /**
202     * Add an integer value to the monotonically increasing counter with the given name.
203     * @deprecated use {@link #count(String, int)}
204     */
205    @Deprecated
206    public static void count(Context context, String name, int value) {
207        getLogger().count(name, value);
208    }
209
210    /**
211     * Increment the bucket with the integer label on the histogram with the given name.
212     * @deprecated use {@link #histogram(String, int)}
213     */
214    @Deprecated
215    public static void histogram(Context context, String name, int bucket) {
216        getLogger().histogram(name, bucket);
217    }
218}
219