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