summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/setupwizard/cmstats/SetupStats.java
blob: f3a47b3dd5e483b73771ef40b55c92672364c087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cyanogenmod.setupwizard.cmstats;

import android.content.Context;
import android.content.Intent;
import android.os.UserHandle;
import android.util.Log;

import java.util.LinkedList;


public class SetupStats {

    private static final String TAG = SetupStats.class.getSimpleName();

    private static final String ANALYTIC_INTENT = "com.cyngn.stats.action.SEND_ANALYTIC_EVENT";
    private static final String ANALYTIC_PERMISSION = "com.cyngn.stats.SEND_ANALYTICS";

    public static final String TRACKING_ID = "tracking_id";

    private final LinkedList<Event> mEvents = new LinkedList<Event>();

    private static final SetupStats sInstance = new SetupStats();

    private static final boolean DEBUG = false;

    private SetupStats() {}

    public static void addEvent(String category, String action,
            String label, String value) {
        sInstance.mEvents.add(new Event(category, action, label, value));
    }

    public static void addEvent(String category, String action) {
        sInstance.mEvents.add(new Event(category, action, null, null));
    }

    public static void sendEvents(Context context) {
        while (!sInstance.mEvents.isEmpty()) {
            sInstance.sendEvent(context, sInstance.mEvents.remove());
        }
    }

    private void sendEvent(Context context, Event event) {

        if (!StatsUtils.isStatsPackageInstalled(context)
                || !StatsUtils.isStatsCollectionEnabled(context)) {
            return;
        }

        // Create new intent
        Intent intent = new Intent();
        intent.setAction(ANALYTIC_INTENT);

        // add tracking id
        intent.putExtra(TRACKING_ID, context.getPackageName());
        // append
        intent.putExtra(Fields.EVENT_CATEGORY, event.category);
        if (DEBUG) Log.d(TAG, Fields.EVENT_CATEGORY + "=" + event.category);
        intent.putExtra(Fields.EVENT_ACTION, event.action);
        if (DEBUG) Log.d(TAG, Fields.EVENT_ACTION + "=" + event.action);
        // check if exist
        if (event.label != null) {
            intent.putExtra(Fields.EVENT_LABEL, event.label);
            if (DEBUG) Log.d(TAG, Fields.EVENT_LABEL + "=" + event.label);
        }

        if (event.value != null) {
            intent.putExtra(Fields.EVENT_VALUE, event.value);
            if (DEBUG) Log.d(TAG, Fields.EVENT_VALUE + "=" + event.value);
        }

        // broadcast for internal package
        context.sendBroadcastAsUser(intent,
                new UserHandle(UserHandle.USER_CURRENT), ANALYTIC_PERMISSION);
    }

    private static final class Event {
        private final String category;
        private final String action;
        private final String label;
        private final String value;

        public Event(String category, String action, String label, String value) {
            this.action = action;
            this.category = category;
            this.label = label;
            this.value = value;
        }
    }

    public static final class Fields {
        public static final String EVENT_CATEGORY = "category";
        public static final String EVENT_ACTION = "action";
        public static final String EVENT_LABEL = "label";
        public static final String EVENT_VALUE = "value";
    }

    public static final class Categories {
        public static final String APP_LAUNCH = "app_launch";
        public static final String APP_FINISHED = "app_finish";
        public static final String PAGE_LOAD = "page_load";
        public static final String EXTERNAL_PAGE_LOAD = "external_page_load";
        public static final String BUTTON_CLICK = "button_click";
        public static final String SETTING_CHANGED = "setting_changed";
    }

    public static final class Action {
        public static final String PAGE_LOADED = "page_loaded";
        public static final String PREVIOUS_BUTTON = "previous_button";
        public static final String NEXT_BUTTON = "next_button";
        public static final String CHANGE_LOCALE = "change_local";
        public static final String EXTERNAL_PAGE_LAUNCH = "external_page_launch";
        public static final String EXTERNAL_PAGE_RESULT = "external_page_result";
        public static final String ENABLE_MOBILE_DATA = "enable_mobile_data";
        public static final String PREFERRED_DATA_SIM = "preferred_data_sim";
        public static final String APPLY_CUSTOM_THEME = "apply_custom_theme";
        public static final String USE_SECURE_SMS = "use_secure_sms";
        public static final String ENABLE_BACKUP = "enable_backup";
        public static final String ENABLE_NAV_KEYS = "enable_nav_keys";
        public static final String ENABLE_LOCATION = "enable_location";
        public static final String ENABLE_NETWORK_LOCATION = "enable_network_location";
        public static final String ENABLE_GPS_LOCATION = "enable_gps_location";
        public static final String DATE_CHANGED = "date_changed";
        public static final String TIME_CHANGED = "time_changed";
        public static final String TIMEZONE_CHANGED = "timezone_changed";
    }

    public static final class Label {
        public static final String PAGE = "page";
        public static final String LOCALE = "local";
        public static final String RESULT = "result";
        public static final String WIFI_SETUP = "wifi_setup";
        public static final String BLUETOOTH_SETUP = "bluetooth_setup";
        public static final String CYANOGEN_ACCOUNT = "cyanogen_account_setup";
        public static final String CAPTIVE_PORTAL_LOGIN = "captive_portal_login";
        public static final String EMERGENCY_CALL = "emergency_call";
        public static final String GMS_ACCOUNT = "gms_account";
        public static final String RESTORE = "restore";
        public static final String CHECKED = "checked";
        public static final String VALUE = "value";
        public static final String SLOT = "slot";
        public static final String TOTAL_TIME = "total_time";
        public static final String FINGERPRINT_SETUP = "fingerprint_setup";
        public static final String LOCKSCREEN_SETUP = "lockscreen_setup";
    }

}