summaryrefslogtreecommitdiffstats
path: root/src/com/android/dialer/incall/InCallMetricsJob.java
blob: f406b8318e5f3fd3565afae4e20cf8a878da5380 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package com.android.dialer.incall;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.CallLog;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import com.android.dialer.util.TelecomUtil;
import com.android.phone.common.incall.DialerDataSubscription;
import com.android.phone.common.incall.api.InCallQueries;
import com.cyanogen.ambient.analytics.AnalyticsServices;
import com.cyanogen.ambient.common.api.Result;
import com.cyanogen.ambient.incall.CallLogConstants;

import com.android.internal.annotations.VisibleForTesting;
import com.cyanogen.ambient.analytics.Event;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * InCallMetricsJob is an aggregation and shipping service that is fired
 * once every 24 hours to pass Metrics to ModCore's analytics service.
 *
 * InCallMetrics is responsible for retrieving call data from the call log as well
 * as events logged to shared preferences.
 *
 * InCall Metrics is used to send other data besides just InCallPlugin Metrics,
 * as we currently are aggregating sim calls for better UI/UX data;
 */
public class InCallMetricsJob extends JobService {

    private static final String TAG = InCallMetricsJob.class.getSimpleName();
    private static final boolean DEBUG = false;

    private static final String CALL_COUNT_SUCCESS = "call_count_success";
    private static final String CALL_COUNT_FAILURE = "call_count_failure";
    private static final String CALL_IS_PSTN = "call_is_pstn";
    private static final String ORIGIN = "origin";

    private InCallMetricsTask mUploadTask;

    @Override
    public boolean onStartJob(JobParameters params) {
        if (DEBUG) Log.v(TAG, "sending events");
        // Send stored Incall Specific events

        mUploadTask = new InCallMetricsTask(params);
        mUploadTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, (Void) null);

        // Running on another thread, return true.
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        // Cancel our async task
        mUploadTask.cancel(true);

        // report that we should try again soon.
        return true;
    }

    class InCallMetricsTask extends AsyncTask<Void, Void, Boolean> {

        JobParameters mMetricsJobParams;

        public InCallMetricsTask(JobParameters params) {
            this.mMetricsJobParams = params;
        }

        private static final long TIMEOUT_MILLIS = 1000;

        @Override
        protected Boolean doInBackground(Void... params) {

            if (TelecomUtil.hasReadPhoneStatus(getApplicationContext())) {
                // Get current time a day ago for call aggregation.
                Date d = new Date(System.currentTimeMillis());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(d);
                calendar.add(Calendar.DATE, -1);
                d.setTime(calendar.getTime().getTime());

                lookupCallsSince(d.getTime(), getContentResolver(), getApplicationContext());
            }

            HashMap<String, Event.Builder> eventsToSend
                    = InCallMetricsHelper.getEventsToSend(InCallMetricsJob.this);

            for (String key : eventsToSend.keySet()) {

                Event.Builder eventBuilder = eventsToSend.get(key);

                if (DEBUG) Log.v(TAG, "sending:" + eventBuilder.toString());

                if (isCancelled()) {
                    return false;
                }

                Event builtEvent = eventBuilder.build();

                Result r = AnalyticsServices.AnalyticsApi.sendEvent(
                        DialerDataSubscription.get(InCallMetricsJob.this).mClient,
                        builtEvent)
                        .await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);

                if (builtEvent.getCustomFields().containsKey(InCallMetricsHelper.PARAM_PROVIDER)) {

                    ComponentName componentName = ComponentName.unflattenFromString(
                            (String)builtEvent.getCustomFields().get(
                                    InCallMetricsHelper.PARAM_PROVIDER));

                    InCallQueries.shipAnalyticsToPlugin(DialerDataSubscription.get(
                            InCallMetricsJob.this).mClient, componentName, builtEvent);
                }


                // if any of our results were not successful, something is wrong.
                // Stop this job for now.
                if (!r.getStatus().isSuccess()) {
                    return false;
                }

                // We sent all the data we had for this event to the database. So clear it from our
                // SharedPreferences.
                InCallMetricsHelper.clearEventData(InCallMetricsJob.this, key);
            }
            return true;
        }

        @Override
        protected void onCancelled() {
            if (DEBUG) Log.w(TAG, "InCall Metrics Job Cancelled");
            // do nothing
        }

        @Override
        protected void onPostExecute(Boolean success) {
            if (DEBUG) Log.v(TAG, "was success: " + success);

            // attempt to reschedule if analytics service is unavailable for our events
            jobFinished(mMetricsJobParams, !success /* reschedule */);
        }
    }

    @VisibleForTesting
    /* package */ static void lookupCallsSince(long time,
            ContentResolver contentResolver, Context context) {

        Uri uri = CallLogConstants.CONTENT_ALL_URI.buildUpon().build();

        String[] projection = new String[] {
                CallLog.Calls.DURATION,
                CallLog.Calls.DATE,
                CallLogConstants.PLUGIN_PACKAGE_NAME,
                CallLog.Calls.NUMBER,
                ORIGIN
        };


        String where = CallLog.Calls.DATE + " >= ?";

        String[] args = new String[] {
                String.valueOf(time)
        };

        Cursor c = contentResolver.query(uri, projection, where, args, null);

        // Ensure that our ContentProvider was available to return a cursor
        if (c == null) {
            return;
        }

        HashMap<String, HashMap<String, Object>> keys = new HashMap<>();

        while (c.moveToNext()) {
            String pluginComponent =
                    c.getString(c.getColumnIndex(CallLogConstants.PLUGIN_PACKAGE_NAME));

            String callOrigin =
                    c.getString(c.getColumnIndex(ORIGIN));

            long callDuration =
                    c.getLong(c.getColumnIndex(CallLog.Calls.DURATION));

            boolean isPSTN =
                    PhoneNumberUtils.isGlobalPhoneNumber(
                            c.getString(
                                    c.getColumnIndex(CallLog.Calls.NUMBER)));

            if (callOrigin == null) {
                // mark this as an unknown location call;
                callOrigin = "unknown";
            }

            if (pluginComponent == null) {
                // TODO: figure out which sim the call was placed through
                pluginComponent = "sim";
            }
            pluginComponent += InCallMetricsHelper.DELIMIT + callOrigin;
            HashMap<String, Object> data;
            long call = 1;
            long calls_success = 0;
            long calls_failure = 0;
            if (keys.containsKey(pluginComponent)) {
                data = keys.get(pluginComponent);
                if (data.containsKey(CallLog.Calls.DURATION)) {
                    // Determine if the call was successfully connected to another device
                    // for a period of time. (includes: people, voicemails, machines)
                    // TODO: figure out how to fix false positive for calls that immediately end
                    // once they are started
                    if (callDuration > 0) {
                        // Call succeeded, increase by one
                        calls_success += call;
                        if (data.containsKey(CALL_COUNT_SUCCESS)) {
                            // add previous success to our current count;
                            calls_success += (long) data.get(CALL_COUNT_SUCCESS);
                        }
                        if (data.containsKey(CALL_COUNT_FAILURE)) {
                            // call success, keep old failure count w/o modification
                            calls_failure = (long) data.get(CALL_COUNT_FAILURE);
                        }
                    } else {
                        // Call failed to connect, increase by one
                        calls_failure += call;
                        if (data.containsKey(CALL_COUNT_SUCCESS)) {
                            // Not a successful call, keep old count
                            calls_success = (long) data.get(CALL_COUNT_SUCCESS);
                        }
                        if (data.containsKey(CALL_COUNT_FAILURE)) {
                            // add previous failures to our current count of one
                            calls_failure += (long) data.get(CALL_COUNT_FAILURE);
                        }
                    }
                    callDuration += (long) data.get(CallLog.Calls.DURATION);
                }
            } else {
                data = new HashMap<>();
                if (callDuration > 0) {
                    calls_success = call;
                } else {
                    calls_failure = call;
                }
            }
            data.put(CallLog.Calls.DURATION, callDuration);
            data.put(CALL_COUNT_SUCCESS, calls_success);
            data.put(CALL_COUNT_FAILURE, calls_failure);
            data.put(CALL_IS_PSTN, isPSTN);
            keys.put(pluginComponent, data);
        }

        c.close();

        for (String key : keys.keySet()) {
            // Shippit
            HashMap<String, Object> value = keys.get(key);
            String[] keySplit = key.split(InCallMetricsHelper.DELIMIT);
            String pluginComponent = keySplit[0];
            String callOrigin = keySplit[1];
            long callDuration = (Long)value.get(CallLog.Calls.DURATION);
            long callCountSuccess = (Long)value.get(CALL_COUNT_SUCCESS);
            long callCountFailure = (Long)value.get(CALL_COUNT_FAILURE);
            boolean isPSTN = (Boolean) value.get(CALL_IS_PSTN);

            if (DEBUG) {
                Log.v(TAG, "Method:" + pluginComponent
                        + " Origin:" + callOrigin
                        + " CountSuccess:" + callCountSuccess
                        + " CountFailure:" + callCountFailure
                        + " Duration:" + callDuration);
            }

            HashMap<InCallMetricsHelper.Parameters, Object> params = new HashMap<>();
            params.put(InCallMetricsHelper.Parameters.OUTGOING_SUCCESS, callCountSuccess);
            params.put(InCallMetricsHelper.Parameters.OUTGOING_FAIL, callCountFailure);
            params.put(InCallMetricsHelper.Parameters.OUTGOING_TOTAL_DURATION, callDuration);
            params.put(InCallMetricsHelper.Parameters.PROVIDER_NAME, pluginComponent);
            params.put(InCallMetricsHelper.Parameters.ACTION_LOCATION, callOrigin);

            InCallMetricsHelper.Events event;
            if (pluginComponent.startsWith("sim")) {
                event = InCallMetricsHelper.Events.CALL_SIM_PSTN;
            } else {
                if (isPSTN) {
                    event = InCallMetricsHelper.Events.CALL_PROVIDER_VOICE;
                } else {
                    event = InCallMetricsHelper.Events.CALL_PROVIDER_PSTN;
                }
            }

            InCallMetricsHelper.sendEvent(context, InCallMetricsHelper.Categories.CALLS,
                    event, params, ComponentName.unflattenFromString(pluginComponent));

        }
    }
}