blob: faaa12d91a3ff203d7f0447c11582bac21221e7c [file] [log] [blame]
Yorke Leef98fb572014-03-05 10:56:55 -08001/*
2 * Copyright 2014, 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 */
16
Ben Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Yorke Leef98fb572014-03-05 10:56:55 -080019import android.content.Context;
20import android.net.Uri;
21import android.os.AsyncTask;
22import android.provider.CallLog.Calls;
Sailesh Nepal810735e2014-03-18 18:15:46 -070023import android.telecomm.CallState;
Yorke Leef98fb572014-03-05 10:56:55 -080024import android.telephony.PhoneNumberUtils;
25
26import com.android.internal.telephony.PhoneConstants;
27
28/**
29 * Helper class that provides functionality to write information about calls and their associated
30 * caller details to the call log. All logging activity will be performed asynchronously in a
31 * background thread to avoid blocking on the main thread.
32 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070033final class CallLogManager extends CallsManagerListenerBase {
Yorke Leef98fb572014-03-05 10:56:55 -080034 /**
35 * Parameter object to hold the arguments to add a call in the call log DB.
36 */
37 private static class AddCallArgs {
38 /**
39 * @param contactInfo Caller details.
40 * @param number The phone number to be logged.
41 * @param presentation Number presentation of the phone number to be logged.
42 * @param callType The type of call (e.g INCOMING_TYPE). @see
43 * {@link android.provider.CallLog} for the list of values.
44 * @param creationDate Time when the call was created (milliseconds since epoch).
45 * @param durationInMillis Duration of the call (milliseconds).
46 */
47 public AddCallArgs(Context context, ContactInfo contactInfo, String number,
48 int presentation, int callType, long creationDate, long durationInMillis) {
49 this.context = context;
50 this.contactInfo = contactInfo;
51 this.number = number;
52 this.presentation = presentation;
53 this.callType = callType;
54 this.timestamp = creationDate;
55 this.durationInSec = (int)(durationInMillis / 1000);
56 }
57 // Since the members are accessed directly, we don't use the
58 // mXxxx notation.
59 public final Context context;
60 public final ContactInfo contactInfo;
61 public final String number;
62 public final int presentation;
63 public final int callType;
64 public final long timestamp;
65 public final int durationInSec;
66 }
67
68 private static final String TAG = CallLogManager.class.getSimpleName();
69
70 private final Context mContext;
71
72 public CallLogManager(Context context) {
73 mContext = context;
74 }
75
Sailesh Nepal810735e2014-03-18 18:15:46 -070076 @Override
77 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
78 if (newState == CallState.DISCONNECTED || newState == CallState.ABORTED) {
79 int type;
80 if (!call.isIncoming()) {
81 type = Calls.OUTGOING_TYPE;
82 } else if (oldState == CallState.RINGING) {
83 type = Calls.MISSED_TYPE;
84 } else {
85 type = Calls.INCOMING_TYPE;
86 }
87 logCall(call, type);
88 }
Yorke Leef98fb572014-03-05 10:56:55 -080089 }
90
91 /**
92 * Logs a call to the call log based on the {@link Call} object passed in.
93 *
94 * @param call The call object being logged
95 * @param callLogType The type of call log entry to log this call as. See:
96 * {@link android.provider.CallLog.Calls#INCOMING_TYPE}
97 * {@link android.provider.CallLog.Calls#OUTGOING_TYPE}
98 * {@link android.provider.CallLog.Calls#MISSED_TYPE}
99 */
100 private void logCall(Call call, int callLogType) {
Sailesh Nepalce704b92014-03-17 18:31:43 -0700101 Uri number = call.getHandle();
Yorke Leef98fb572014-03-05 10:56:55 -0800102 final long creationTime = call.getCreationTimeInMilliseconds();
103 final long age = call.getAgeInMilliseconds();
104
105 final ContactInfo contactInfo = call.getContactInfo(); // May be null.
106 final String logNumber = getLogNumber(call);
107
108 Log.d(TAG, "logNumber set to:" + Log.pii(logNumber) + ", number set to: "
109 + Log.pii(number));
110
111 final int presentation = getPresentation(call, contactInfo);
112
113 logCall(contactInfo, logNumber, presentation, callLogType, creationTime, age);
114 }
115
116 /**
117 * Inserts a call into the call log, based on the parameters passed in.
118 *
119 * @param contactInfo Caller details.
120 * @param number The number the call was made to or from.
121 * @param presentation
122 * @param callType The type of call.
123 * @param start The start time of the call, in milliseconds.
124 * @param duration The duration of the call, in milliseconds.
125 */
126 private void logCall(
127 ContactInfo contactInfo,
128 String number,
129 int presentation,
130 int callType,
131 long start,
132 long duration) {
133 boolean isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(number, mContext);
134
135 // On some devices, to avoid accidental redialing of emergency numbers, we *never* log
136 // emergency calls to the Call Log. (This behavior is set on a per-product basis, based
137 // on carrier requirements.)
138 final boolean okToLogEmergencyNumber =
139 mContext.getResources().getBoolean(R.bool.allow_emergency_numbers_in_call_log);
140
141 // Don't log emergency numbers if the device doesn't allow it.
142 final boolean isOkToLogThisCall = !isEmergencyNumber || okToLogEmergencyNumber;
143
144 if (isOkToLogThisCall) {
145 Log.d(TAG, "Logging Calllog entry: " + contactInfo + ", "
146 + Log.pii(number) + "," + presentation + ", " + callType
147 + ", " + start + ", " + duration);
148 AddCallArgs args = new AddCallArgs(mContext, contactInfo, number, presentation,
149 callType, start, duration);
150 logCallAsync(args);
151 } else {
152 Log.d(TAG, "Not adding emergency call to call log.");
153 }
154 }
155
156 /**
157 * Retrieve the phone number from the call, and then process it before returning the
158 * actual number that is to be logged.
159 *
160 * @param call The phone connection.
161 * @return the phone number to be logged.
162 */
163 private String getLogNumber(Call call) {
Sailesh Nepalce704b92014-03-17 18:31:43 -0700164 Uri handle = call.getHandle();
Yorke Leef98fb572014-03-05 10:56:55 -0800165
166 if (handle == null) {
167 return null;
168 }
169
Sailesh Nepalce704b92014-03-17 18:31:43 -0700170 // TODO: Add support for SIP numbers.
171 String handleString = handle.toString();
172 if (!PhoneNumberUtils.isUriNumber(handleString)) {
173 handleString = PhoneNumberUtils.stripSeparators(handleString);
Yorke Leef98fb572014-03-05 10:56:55 -0800174 }
Sailesh Nepalce704b92014-03-17 18:31:43 -0700175 return handleString;
Yorke Leef98fb572014-03-05 10:56:55 -0800176 }
177
178 /**
179 * Gets the presentation from the {@link ContactInfo} if not null. Otherwise, gets it from the
180 * {@link Call}.
181 *
182 * TODO: There needs to be a way to pass information from
183 * Connection.getNumberPresentation() into a {@link Call} object. Until then, always return
184 * PhoneConstants.PRESENTATION_ALLOWED. On top of that, we might need to introduce
185 * getNumberPresentation to the ContactInfo object as well.
186 *
187 * @param call The call object to retrieve caller details from.
188 * @param contactInfo The CallerInfo. May be null.
189 * @return The number presentation constant to insert into the call logs.
190 */
191 private int getPresentation(Call call, ContactInfo contactInfo) {
192 return PhoneConstants.PRESENTATION_ALLOWED;
193 }
194
195 /**
196 * Adds the call defined by the parameters in the provided AddCallArgs to the CallLogProvider
197 * using an AsyncTask to avoid blocking the main thread.
198 *
199 * @param args Prepopulated call details.
200 * @return A handle to the AsyncTask that will add the call to the call log asynchronously.
201 */
202 public AsyncTask<AddCallArgs, Void, Uri[]> logCallAsync(AddCallArgs args) {
203 return new LogCallAsyncTask().execute(args);
204 }
205
206 /**
207 * Helper AsyncTask to access the call logs database asynchronously since database operations
208 * can take a long time depending on the system's load. Since it extends AsyncTask, it uses
209 * its own thread pool.
210 */
211 private class LogCallAsyncTask extends AsyncTask<AddCallArgs, Void, Uri[]> {
212 @Override
213 protected Uri[] doInBackground(AddCallArgs... callList) {
214 int count = callList.length;
215 Uri[] result = new Uri[count];
216 for (int i = 0; i < count; i++) {
217 AddCallArgs c = callList[i];
218
219 try {
220 // May block.
221 result[i] = Calls.addCall(null, c.context, c.number, c.presentation,
222 c.callType, c.timestamp, c.durationInSec);
223 } catch (Exception e) {
224 // This is very rare but may happen in legitimate cases.
225 // E.g. If the phone is encrypted and thus write request fails, it may cause
226 // some kind of Exception (right now it is IllegalArgumentException, but this
227 // might change).
228 //
229 // We don't want to crash the whole process just because of that, so just log
230 // it instead.
231 Log.e(TAG, e, "Exception raised during adding CallLog entry.");
232 result[i] = null;
233 }
234 }
235 return result;
236 }
237
238 /**
239 * Performs a simple sanity check to make sure the call was written in the database.
240 * Typically there is only one result per call so it is easy to identify which one failed.
241 */
242 @Override
243 protected void onPostExecute(Uri[] result) {
244 for (Uri uri : result) {
245 if (uri == null) {
246 Log.w(TAG, "Failed to write call to the log.");
247 }
248 }
249 }
250 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800251}