summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/metrics/MetricsTracker.java
blob: 218a4e29ac9df5edcaa4f6b03e850d4544193e75 (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
package com.android.messaging.metrics;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.android.messaging.sms.MmsUtils;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class MetricsTracker {
    private static final String SEND_FAILED_ACTION = "send_failed";
    private static final String SEND_SUCCESS_ACTION = "send_success";
    private static final String SMS_CATEGORY = "sms";
    private static final String MMS_CATEGORY = "mms";
    private static final String MMS_GROUP_CATEGORY = "mms_group";

    public static final String TAG = "Metrics";

    private final Context mContext;

    private static volatile MetricsTracker mInstance;
    private static final Executor EXECUTOR = AsyncTask.SERIAL_EXECUTOR;

    public static MetricsTracker getInstance(Context context) {
        if (mInstance == null) {
            synchronized (MetricsTracker.class) {
                if (mInstance == null) {
                    mInstance = new MetricsTracker(context.getApplicationContext());
                }
            }
        }
        return mInstance;
    }

    private MetricsTracker(Context context) {
        this.mContext = context.getApplicationContext();
    }

    private void writeEventAsync(final Event event) {
        EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                writeEvent(event);
            }
        });
    }

    private void writeEvent(Event event) {
        MetricsDatabase metricsDatabase = MetricsDatabase.getInstance(mContext);
        metricsDatabase.insert(event);
        Log.i(TAG, event.toString() + " event saved to database");
    }

    private void trackSmsSentFailedAsync() {
        writeEventAsync(new Event(SMS_CATEGORY, SEND_FAILED_ACTION));
    }

    private void trackSmsSentSuccessAsync() {
        writeEventAsync(new Event(SMS_CATEGORY, SEND_SUCCESS_ACTION));
    }

    private void trackMmsSentFailed() {
        writeEvent(new Event(MMS_CATEGORY, SEND_FAILED_ACTION));
    }

    private void trackMmsSentSuccess() {
        writeEvent(new Event(MMS_CATEGORY, SEND_SUCCESS_ACTION));
    }

    private void trackGroupMmsSentSuccess() {
        writeEvent(new Event(MMS_GROUP_CATEGORY, SEND_SUCCESS_ACTION));
    }

    private void trackGroupMmsSentFailed() {
        writeEvent(new Event(MMS_GROUP_CATEGORY, SEND_FAILED_ACTION));
    }

    private void trackMmsSent(boolean success, boolean isGroupMessage) {
        if (isGroupMessage) {
            if (success) {
                trackGroupMmsSentSuccess();
            } else {
                trackGroupMmsSentFailed();
            }
        } else {
            if (success) {
                trackMmsSentSuccess();
            } else {
                trackMmsSentFailed();
            }
        }
    }

    public void trackMmsSentAsync(final boolean success, final boolean isGroupMessage) {
        EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                trackMmsSent(success, isGroupMessage);
            }
        });
    }

    public void trackSmsSentAsync(boolean success) {
        if (success) {
            trackSmsSentSuccessAsync();
        } else {
            trackSmsSentFailedAsync();
        }
    }
}