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