1 /* 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 17 package com.android.server.telecom; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.Manifest.permission; 22 import android.net.Uri; 23 import android.os.AsyncTask; 24 import android.provider.CallLog.Calls; 25 import android.telecom.CallState; 26 import android.telecom.DisconnectCause; 27 import android.telecom.PhoneAccountHandle; 28 import android.telecom.VideoProfile; 29 import android.telephony.PhoneNumberUtils; 30 31 // TODO: Needed for move to system service: import com.android.internal.R; 32 import com.android.internal.telephony.CallerInfo; 33 import com.android.internal.telephony.PhoneConstants; 34 35 /** 36 * Helper class that provides functionality to write information about calls and their associated 37 * caller details to the call log. All logging activity will be performed asynchronously in a 38 * background thread to avoid blocking on the main thread. 39 */ 40 final class CallLogManager extends CallsManagerListenerBase { 41 /** 42 * Parameter object to hold the arguments to add a call in the call log DB. 43 */ 44 private static class AddCallArgs { 45 /** 46 * @param callerInfo Caller details. 47 * @param number The phone number to be logged. 48 * @param presentation Number presentation of the phone number to be logged. 49 * @param callType The type of call (e.g INCOMING_TYPE). @see 50 * {@link android.provider.CallLog} for the list of values. 51 * @param features The features of the call (e.g. FEATURES_VIDEO). @see 52 * {@link android.provider.CallLog} for the list of values. 53 * @param creationDate Time when the call was created (milliseconds since epoch). 54 * @param durationInMillis Duration of the call (milliseconds). 55 * @param dataUsage Data usage in bytes, or null if not applicable. 56 */ AddCallArgs(Context context, CallerInfo callerInfo, String number, int presentation, int callType, int features, PhoneAccountHandle accountHandle, long creationDate, long durationInMillis, Long dataUsage)57 public AddCallArgs(Context context, CallerInfo callerInfo, String number, 58 int presentation, int callType, int features, PhoneAccountHandle accountHandle, 59 long creationDate, long durationInMillis, Long dataUsage) { 60 this.context = context; 61 this.callerInfo = callerInfo; 62 this.number = number; 63 this.presentation = presentation; 64 this.callType = callType; 65 this.features = features; 66 this.accountHandle = accountHandle; 67 this.timestamp = creationDate; 68 this.durationInSec = (int)(durationInMillis / 1000); 69 this.dataUsage = dataUsage; 70 } 71 // Since the members are accessed directly, we don't use the 72 // mXxxx notation. 73 public final Context context; 74 public final CallerInfo callerInfo; 75 public final String number; 76 public final int presentation; 77 public final int callType; 78 public final int features; 79 public final PhoneAccountHandle accountHandle; 80 public final long timestamp; 81 public final int durationInSec; 82 public final Long dataUsage; 83 } 84 85 private static final String TAG = CallLogManager.class.getSimpleName(); 86 87 private final Context mContext; 88 private static final String ACTION_CALLS_TABLE_ADD_ENTRY = 89 "com.android.server.telecom.intent.action.CALLS_ADD_ENTRY"; 90 private static final String PERMISSION_PROCESS_CALLLOG_INFO = 91 "android.permission.PROCESS_CALLLOG_INFO"; 92 private static final String CALL_TYPE = "callType"; 93 private static final String CALL_DURATION = "duration"; 94 CallLogManager(Context context)95 public CallLogManager(Context context) { 96 mContext = context; 97 } 98 99 @Override onCallStateChanged(Call call, int oldState, int newState)100 public void onCallStateChanged(Call call, int oldState, int newState) { 101 int disconnectCause = call.getDisconnectCause().getCode(); 102 boolean isNewlyDisconnected = 103 newState == CallState.DISCONNECTED || newState == CallState.ABORTED; 104 boolean isCallCanceled = isNewlyDisconnected && disconnectCause == DisconnectCause.CANCELED; 105 106 // Log newly disconnected calls only if: 107 // 1) It was not in the "choose account" phase when disconnected 108 // 2) It is a conference call 109 // 3) Call was not explicitly canceled 110 if (isNewlyDisconnected && 111 (oldState != CallState.PRE_DIAL_WAIT && 112 !call.isConference() && 113 !isCallCanceled)) { 114 int type; 115 if (!call.isIncoming()) { 116 type = Calls.OUTGOING_TYPE; 117 } else if (disconnectCause == DisconnectCause.MISSED) { 118 type = Calls.MISSED_TYPE; 119 } else { 120 type = Calls.INCOMING_TYPE; 121 } 122 logCall(call, type); 123 } 124 } 125 126 /** 127 * Logs a call to the call log based on the {@link Call} object passed in. 128 * 129 * @param call The call object being logged 130 * @param callLogType The type of call log entry to log this call as. See: 131 * {@link android.provider.CallLog.Calls#INCOMING_TYPE} 132 * {@link android.provider.CallLog.Calls#OUTGOING_TYPE} 133 * {@link android.provider.CallLog.Calls#MISSED_TYPE} 134 */ logCall(Call call, int callLogType)135 void logCall(Call call, int callLogType) { 136 final long creationTime = call.getCreationTimeMillis(); 137 final long age = call.getAgeMillis(); 138 139 final String logNumber = getLogNumber(call); 140 141 Log.d(TAG, "logNumber set to: %s", Log.pii(logNumber)); 142 143 final PhoneAccountHandle accountHandle = call.getTargetPhoneAccount(); 144 145 // TODO(vt): Once data usage is available, wire it up here. 146 int callFeatures = getCallFeatures(call.getVideoStateHistory()); 147 logCall(call.getCallerInfo(), logNumber, call.getHandlePresentation(), 148 callLogType, callFeatures, accountHandle, creationTime, age, null); 149 } 150 151 /** 152 * Inserts a call into the call log, based on the parameters passed in. 153 * 154 * @param callerInfo Caller details. 155 * @param number The number the call was made to or from. 156 * @param presentation 157 * @param callType The type of call. 158 * @param features The features of the call. 159 * @param start The start time of the call, in milliseconds. 160 * @param duration The duration of the call, in milliseconds. 161 * @param dataUsage The data usage for the call, null if not applicable. 162 */ logCall( CallerInfo callerInfo, String number, int presentation, int callType, int features, PhoneAccountHandle accountHandle, long start, long duration, Long dataUsage)163 private void logCall( 164 CallerInfo callerInfo, 165 String number, 166 int presentation, 167 int callType, 168 int features, 169 PhoneAccountHandle accountHandle, 170 long start, 171 long duration, 172 Long dataUsage) { 173 boolean isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(mContext, number); 174 175 // On some devices, to avoid accidental redialing of emergency numbers, we *never* log 176 // emergency calls to the Call Log. (This behavior is set on a per-product basis, based 177 // on carrier requirements.) 178 final boolean okToLogEmergencyNumber = 179 mContext.getResources().getBoolean(R.bool.allow_emergency_numbers_in_call_log); 180 181 // Don't log emergency numbers if the device doesn't allow it. 182 final boolean isOkToLogThisCall = !isEmergencyNumber || okToLogEmergencyNumber; 183 184 sendAddCallBroadcast(callType, duration); 185 186 if (isOkToLogThisCall) { 187 Log.d(TAG, "Logging Calllog entry: " + callerInfo + ", " 188 + Log.pii(number) + "," + presentation + ", " + callType 189 + ", " + start + ", " + duration); 190 AddCallArgs args = new AddCallArgs(mContext, callerInfo, number, presentation, 191 callType, features, accountHandle, start, duration, dataUsage); 192 logCallAsync(args); 193 } else { 194 Log.d(TAG, "Not adding emergency call to call log."); 195 } 196 } 197 198 /** 199 * Based on the video state of the call, determines the call features applicable for the call. 200 * 201 * @param videoState The video state. 202 * @return The call features. 203 */ getCallFeatures(int videoState)204 private static int getCallFeatures(int videoState) { 205 if ((videoState & VideoProfile.VideoState.TX_ENABLED) 206 == VideoProfile.VideoState.TX_ENABLED) { 207 return Calls.FEATURES_VIDEO; 208 } 209 return 0; 210 } 211 212 /** 213 * Retrieve the phone number from the call, and then process it before returning the 214 * actual number that is to be logged. 215 * 216 * @param call The phone connection. 217 * @return the phone number to be logged. 218 */ getLogNumber(Call call)219 private String getLogNumber(Call call) { 220 Uri handle = call.getOriginalHandle(); 221 222 if (handle == null) { 223 return null; 224 } 225 226 String handleString = handle.getSchemeSpecificPart(); 227 if (!PhoneNumberUtils.isUriNumber(handleString)) { 228 handleString = PhoneNumberUtils.stripSeparators(handleString); 229 } 230 return handleString; 231 } 232 233 /** 234 * Adds the call defined by the parameters in the provided AddCallArgs to the CallLogProvider 235 * using an AsyncTask to avoid blocking the main thread. 236 * 237 * @param args Prepopulated call details. 238 * @return A handle to the AsyncTask that will add the call to the call log asynchronously. 239 */ logCallAsync(AddCallArgs args)240 public AsyncTask<AddCallArgs, Void, Uri[]> logCallAsync(AddCallArgs args) { 241 return new LogCallAsyncTask().execute(args); 242 } 243 244 /** 245 * Helper AsyncTask to access the call logs database asynchronously since database operations 246 * can take a long time depending on the system's load. Since it extends AsyncTask, it uses 247 * its own thread pool. 248 */ 249 private class LogCallAsyncTask extends AsyncTask<AddCallArgs, Void, Uri[]> { 250 @Override doInBackground(AddCallArgs... callList)251 protected Uri[] doInBackground(AddCallArgs... callList) { 252 int count = callList.length; 253 Uri[] result = new Uri[count]; 254 for (int i = 0; i < count; i++) { 255 AddCallArgs c = callList[i]; 256 257 try { 258 // May block. 259 result[i] = Calls.addCall(c.callerInfo, c.context, c.number, c.presentation, 260 c.callType, c.features, c.accountHandle, c.timestamp, c.durationInSec, 261 c.dataUsage, true /* addForAllUsers */); 262 } catch (Exception e) { 263 // This is very rare but may happen in legitimate cases. 264 // E.g. If the phone is encrypted and thus write request fails, it may cause 265 // some kind of Exception (right now it is IllegalArgumentException, but this 266 // might change). 267 // 268 // We don't want to crash the whole process just because of that, so just log 269 // it instead. 270 Log.e(TAG, e, "Exception raised during adding CallLog entry."); 271 result[i] = null; 272 } 273 } 274 return result; 275 } 276 277 /** 278 * Performs a simple sanity check to make sure the call was written in the database. 279 * Typically there is only one result per call so it is easy to identify which one failed. 280 */ 281 @Override onPostExecute(Uri[] result)282 protected void onPostExecute(Uri[] result) { 283 for (Uri uri : result) { 284 if (uri == null) { 285 Log.w(TAG, "Failed to write call to the log."); 286 } 287 } 288 } 289 } 290 sendAddCallBroadcast(int callType, long duration)291 private void sendAddCallBroadcast(int callType, long duration) { 292 Intent callAddIntent = new Intent(ACTION_CALLS_TABLE_ADD_ENTRY); 293 callAddIntent.putExtra(CALL_TYPE, callType); 294 callAddIntent.putExtra(CALL_DURATION, duration); 295 mContext.sendBroadcast(callAddIntent, PERMISSION_PROCESS_CALLLOG_INFO); 296 } 297 } 298