Analytics.java revision 8cc0de897f4d4b5790a363684347eeae06088f45
1/*******************************************************************************
2 *      Copyright (C) 2013 Google Inc.
3 *      Licensed to The Android Open Source Project.
4 *
5 *      Licensed under the Apache License, Version 2.0 (the "License");
6 *      you may not use this file except in compliance with the License.
7 *      You may obtain a copy of the License at
8 *
9 *           http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *      Unless required by applicable law or agreed to in writing, software
12 *      distributed under the License is distributed on an "AS IS" BASIS,
13 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *      See the License for the specific language governing permissions and
15 *      limitations under the License.
16 *******************************************************************************/
17
18package com.android.mail.analytics;
19
20import android.app.Activity;
21import android.app.ActivityManager;
22
23/**
24 * Mail wrapper for analytics libraries. Libraries should implement {@link Tracker}, and app
25 * configurations that want to enable analytics should call {@link #setTracker(Tracker)} as soon
26 * as possible upon application start.
27 * <p>
28 * {@link #getInstance()} will always return an object, but if the app has not yet (or will not
29 * ever) set its own tracker instance, method calls on that tracker will be stubbed out.
30 *
31 */
32public final class Analytics {
33
34    public static final String EVENT_CATEGORY_MENU_ITEM = "menu_item";
35
36    public static final int CD_INDEX_ACCOUNT_TYPE = 1;
37
38    public static final int CD_INDEX_ACCOUNT_COUNT = 2;
39
40    public static final int CD_INDEX_SENDER_IMAGES_ENABLED = 3;
41
42    public static final int CD_INDEX_ATTACHMENT_PREVIEWS_ENABLED = 4;
43
44    public static final int CD_INDEX_INBOX_TYPE = 5;
45
46    public static final int CD_INDEX_INBOX_SECTIONS_ENABLED = 6;
47
48    private static Tracker sInstance;
49
50    private Analytics() {
51    }
52
53    public static Tracker getInstance() {
54        synchronized (Analytics.class) {
55            if (sInstance == null) {
56                sInstance = new StubTracker();
57            }
58        }
59        return sInstance;
60    }
61
62    public static void setTracker(Tracker t) {
63        synchronized (Analytics.class) {
64            sInstance = t;
65        }
66    }
67
68    public static boolean isLoggable() {
69        return !ActivityManager.isUserAMonkey() && !ActivityManager.isRunningInTestHarness();
70    }
71
72    private static final class StubTracker implements Tracker {
73
74        @Override
75        public void activityStart(Activity a) {}
76
77        @Override
78        public void activityStop(Activity a) {}
79
80        @Override
81        public void sendEvent(String category, String action, String label, long value) {}
82
83        @Override
84        public void sendMenuItemEvent(String category, int itemResId, String label, long value) {}
85
86        @Override
87        public void sendView(String view) {}
88
89        @Override
90        public void setCustomDimension(int index, String value) {}
91
92        @Override
93        public void setCustomMetric(int index, Long value) {}
94
95        @Override
96        public void debugDispatchNow() {}
97
98    }
99
100}
101