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.os.Build;
20import android.view.View;
21
22import com.android.internal.logging.MetricsProto.MetricsEvent;
23
24/**
25 * Log all the things.
26 *
27 * @hide
28 */
29public class MetricsLogger {
30    // define metric categories in frameworks/base/proto/src/metrics_constants.proto.
31
32    public static final int VIEW_UNKNOWN = MetricsEvent.VIEW_UNKNOWN;
33
34    public static void visible(Context context, int category) throws IllegalArgumentException {
35        if (Build.IS_DEBUGGABLE && category == VIEW_UNKNOWN) {
36            throw new IllegalArgumentException("Must define metric category");
37        }
38        EventLogTags.writeSysuiViewVisibility(category, 100);
39    }
40
41    public static void hidden(Context context, int category) throws IllegalArgumentException {
42        if (Build.IS_DEBUGGABLE && category == VIEW_UNKNOWN) {
43            throw new IllegalArgumentException("Must define metric category");
44        }
45        EventLogTags.writeSysuiViewVisibility(category, 0);
46    }
47
48    public static void visibility(Context context, int category, boolean visibile)
49            throws IllegalArgumentException {
50        if (visibile) {
51            visible(context, category);
52        } else {
53            hidden(context, category);
54        }
55    }
56
57    public static void visibility(Context context, int category, int vis)
58            throws IllegalArgumentException {
59        visibility(context, category, vis == View.VISIBLE);
60    }
61
62    public static void action(Context context, int category) {
63        action(context, category, "");
64    }
65
66    public static void action(Context context, int category, int value) {
67        action(context, category, Integer.toString(value));
68    }
69
70    public static void action(Context context, int category, boolean value) {
71        action(context, category, Boolean.toString(value));
72    }
73
74    public static void action(Context context, int category, String pkg) {
75        if (Build.IS_DEBUGGABLE && category == VIEW_UNKNOWN) {
76            throw new IllegalArgumentException("Must define metric category");
77        }
78        EventLogTags.writeSysuiAction(category, pkg);
79    }
80
81    /** Add an integer value to the monotonically increasing counter with the given name. */
82    public static void count(Context context, String name, int value) {
83        EventLogTags.writeSysuiCount(name, value);
84    }
85
86    /** Increment the bucket with the integer label on the histogram with the given name. */
87    public static void histogram(Context context, String name, int bucket) {
88        EventLogTags.writeSysuiHistogram(name, bucket);
89    }
90}
91