CallLog.java revision 158c5e41d427171de492492751bf99efeab3c8a4
1/* 2 * Copyright (C) 2006 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 18package android.provider; 19 20import android.content.ContentProvider; 21import android.content.ContentResolver; 22import android.content.ContentValues; 23import android.content.Context; 24import android.content.Intent; 25import android.content.pm.UserInfo; 26import android.database.Cursor; 27import android.net.Uri; 28import android.os.UserHandle; 29import android.os.UserManager; 30import android.provider.ContactsContract.CommonDataKinds.Callable; 31import android.provider.ContactsContract.CommonDataKinds.Phone; 32import android.provider.ContactsContract.DataUsageFeedback; 33import android.telecomm.PhoneAccountHandle; 34import android.text.TextUtils; 35 36import com.android.internal.telephony.CallerInfo; 37import com.android.internal.telephony.PhoneConstants; 38 39import java.util.List; 40 41/** 42 * The CallLog provider contains information about placed and received calls. 43 */ 44public class CallLog { 45 public static final String AUTHORITY = "call_log"; 46 47 /** 48 * The content:// style URL for this provider 49 */ 50 public static final Uri CONTENT_URI = 51 Uri.parse("content://" + AUTHORITY); 52 53 /** 54 * Contains the recent calls. 55 */ 56 public static class Calls implements BaseColumns { 57 /** 58 * The content:// style URL for this table 59 */ 60 public static final Uri CONTENT_URI = 61 Uri.parse("content://call_log/calls"); 62 63 /** 64 * The content:// style URL for filtering this table on phone numbers 65 */ 66 public static final Uri CONTENT_FILTER_URI = 67 Uri.parse("content://call_log/calls/filter"); 68 69 /** 70 * Query parameter used to limit the number of call logs returned. 71 * <p> 72 * TYPE: integer 73 */ 74 public static final String LIMIT_PARAM_KEY = "limit"; 75 76 /** 77 * Query parameter used to specify the starting record to return. 78 * <p> 79 * TYPE: integer 80 */ 81 public static final String OFFSET_PARAM_KEY = "offset"; 82 83 /** 84 * An optional URI parameter which instructs the provider to allow the operation to be 85 * applied to voicemail records as well. 86 * <p> 87 * TYPE: Boolean 88 * <p> 89 * Using this parameter with a value of {@code true} will result in a security error if the 90 * calling package does not have appropriate permissions to access voicemails. 91 * 92 * @hide 93 */ 94 public static final String ALLOW_VOICEMAILS_PARAM_KEY = "allow_voicemails"; 95 96 /** 97 * An optional extra used with {@link #CONTENT_TYPE Calls.CONTENT_TYPE} and 98 * {@link Intent#ACTION_VIEW} to specify that the presented list of calls should be 99 * filtered for a particular call type. 100 * 101 * Applications implementing a call log UI should check for this extra, and display a 102 * filtered list of calls based on the specified call type. If not applicable within the 103 * application's UI, it should be silently ignored. 104 * 105 * <p> 106 * The following example brings up the call log, showing only missed calls. 107 * <pre> 108 * Intent intent = new Intent(Intent.ACTION_VIEW); 109 * intent.setType(CallLog.Calls.CONTENT_TYPE); 110 * intent.putExtra(CallLog.Calls.EXTRA_CALL_TYPE_FILTER, CallLog.Calls.MISSED_TYPE); 111 * startActivity(intent); 112 * </pre> 113 * </p> 114 */ 115 public static final String EXTRA_CALL_TYPE_FILTER = "extra_call_type_filter"; 116 117 /** 118 * Content uri used to access call log entries, including voicemail records. You must have 119 * the READ_CALL_LOG and WRITE_CALL_LOG permissions to read and write to the call log. 120 */ 121 public static final Uri CONTENT_URI_WITH_VOICEMAIL = CONTENT_URI.buildUpon() 122 .appendQueryParameter(ALLOW_VOICEMAILS_PARAM_KEY, "true") 123 .build(); 124 125 /** 126 * The default sort order for this table 127 */ 128 public static final String DEFAULT_SORT_ORDER = "date DESC"; 129 130 /** 131 * The MIME type of {@link #CONTENT_URI} and {@link #CONTENT_FILTER_URI} 132 * providing a directory of calls. 133 */ 134 public static final String CONTENT_TYPE = "vnd.android.cursor.dir/calls"; 135 136 /** 137 * The MIME type of a {@link #CONTENT_URI} sub-directory of a single 138 * call. 139 */ 140 public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/calls"; 141 142 /** 143 * The type of the call (incoming, outgoing or missed). 144 * <P>Type: INTEGER (int)</P> 145 */ 146 public static final String TYPE = "type"; 147 148 /** Call log type for incoming calls. */ 149 public static final int INCOMING_TYPE = 1; 150 /** Call log type for outgoing calls. */ 151 public static final int OUTGOING_TYPE = 2; 152 /** Call log type for missed calls. */ 153 public static final int MISSED_TYPE = 3; 154 /** Call log type for voicemails. */ 155 public static final int VOICEMAIL_TYPE = 4; 156 157 /** 158 * Bit-mask describing features of the call (e.g. video). 159 * 160 * <P>Type: INTEGER (int)</P> 161 */ 162 public static final String FEATURES = "features"; 163 164 /** Call had no associated features (e.g. voice-only). */ 165 public static final int FEATURES_NONE = 0x0; 166 /** Call had video. */ 167 public static final int FEATURES_VIDEO = 0x1; 168 169 /** 170 * The phone number as the user entered it. 171 * <P>Type: TEXT</P> 172 */ 173 public static final String NUMBER = "number"; 174 175 /** 176 * The number presenting rules set by the network. 177 * 178 * <p> 179 * Allowed values: 180 * <ul> 181 * <li>{@link #PRESENTATION_ALLOWED}</li> 182 * <li>{@link #PRESENTATION_RESTRICTED}</li> 183 * <li>{@link #PRESENTATION_UNKNOWN}</li> 184 * <li>{@link #PRESENTATION_PAYPHONE}</li> 185 * </ul> 186 * </p> 187 * 188 * <P>Type: INTEGER</P> 189 */ 190 public static final String NUMBER_PRESENTATION = "presentation"; 191 192 /** Number is allowed to display for caller id. */ 193 public static final int PRESENTATION_ALLOWED = 1; 194 /** Number is blocked by user. */ 195 public static final int PRESENTATION_RESTRICTED = 2; 196 /** Number is not specified or unknown by network. */ 197 public static final int PRESENTATION_UNKNOWN = 3; 198 /** Number is a pay phone. */ 199 public static final int PRESENTATION_PAYPHONE = 4; 200 201 /** 202 * The ISO 3166-1 two letters country code of the country where the 203 * user received or made the call. 204 * <P> 205 * Type: TEXT 206 * </P> 207 */ 208 public static final String COUNTRY_ISO = "countryiso"; 209 210 /** 211 * The date the call occured, in milliseconds since the epoch 212 * <P>Type: INTEGER (long)</P> 213 */ 214 public static final String DATE = "date"; 215 216 /** 217 * The duration of the call in seconds 218 * <P>Type: INTEGER (long)</P> 219 */ 220 public static final String DURATION = "duration"; 221 222 /** 223 * The data usage of the call in bytes. 224 * <P>Type: INTEGER (long)</P> 225 */ 226 public static final String DATA_USAGE = "data_usage"; 227 228 /** 229 * Whether or not the call has been acknowledged 230 * <P>Type: INTEGER (boolean)</P> 231 */ 232 public static final String NEW = "new"; 233 234 /** 235 * The cached name associated with the phone number, if it exists. 236 * This value is not guaranteed to be current, if the contact information 237 * associated with this number has changed. 238 * <P>Type: TEXT</P> 239 */ 240 public static final String CACHED_NAME = "name"; 241 242 /** 243 * The cached number type (Home, Work, etc) associated with the 244 * phone number, if it exists. 245 * This value is not guaranteed to be current, if the contact information 246 * associated with this number has changed. 247 * <P>Type: INTEGER</P> 248 */ 249 public static final String CACHED_NUMBER_TYPE = "numbertype"; 250 251 /** 252 * The cached number label, for a custom number type, associated with the 253 * phone number, if it exists. 254 * This value is not guaranteed to be current, if the contact information 255 * associated with this number has changed. 256 * <P>Type: TEXT</P> 257 */ 258 public static final String CACHED_NUMBER_LABEL = "numberlabel"; 259 260 /** 261 * URI of the voicemail entry. Populated only for {@link #VOICEMAIL_TYPE}. 262 * <P>Type: TEXT</P> 263 */ 264 public static final String VOICEMAIL_URI = "voicemail_uri"; 265 266 /** 267 * Whether this item has been read or otherwise consumed by the user. 268 * <p> 269 * Unlike the {@link #NEW} field, which requires the user to have acknowledged the 270 * existence of the entry, this implies the user has interacted with the entry. 271 * <P>Type: INTEGER (boolean)</P> 272 */ 273 public static final String IS_READ = "is_read"; 274 275 /** 276 * A geocoded location for the number associated with this call. 277 * <p> 278 * The string represents a city, state, or country associated with the number. 279 * <P>Type: TEXT</P> 280 */ 281 public static final String GEOCODED_LOCATION = "geocoded_location"; 282 283 /** 284 * The cached URI to look up the contact associated with the phone number, if it exists. 285 * This value may not be current if the contact information associated with this number 286 * has changed. 287 * <P>Type: TEXT</P> 288 */ 289 public static final String CACHED_LOOKUP_URI = "lookup_uri"; 290 291 /** 292 * The cached phone number of the contact which matches this entry, if it exists. 293 * This value may not be current if the contact information associated with this number 294 * has changed. 295 * <P>Type: TEXT</P> 296 */ 297 public static final String CACHED_MATCHED_NUMBER = "matched_number"; 298 299 /** 300 * The cached normalized(E164) version of the phone number, if it exists. 301 * This value may not be current if the contact information associated with this number 302 * has changed. 303 * <P>Type: TEXT</P> 304 */ 305 public static final String CACHED_NORMALIZED_NUMBER = "normalized_number"; 306 307 /** 308 * The cached photo id of the picture associated with the phone number, if it exists. 309 * This value may not be current if the contact information associated with this number 310 * has changed. 311 * <P>Type: INTEGER (long)</P> 312 */ 313 public static final String CACHED_PHOTO_ID = "photo_id"; 314 315 /** 316 * The cached phone number, formatted with formatting rules based on the country the 317 * user was in when the call was made or received. 318 * This value is not guaranteed to be present, and may not be current if the contact 319 * information associated with this number 320 * has changed. 321 * <P>Type: TEXT</P> 322 */ 323 public static final String CACHED_FORMATTED_NUMBER = "formatted_number"; 324 325 // Note: PHONE_ACCOUNT_* constant values are "subscription_*" due to a historic naming 326 // that was encoded into call log databases. 327 328 /** 329 * The component name of the account in string form. 330 * <P>Type: TEXT</P> 331 */ 332 public static final String PHONE_ACCOUNT_COMPONENT_NAME = "subscription_component_name"; 333 334 /** 335 * The identifier of a account that is unique to a specified component. 336 * <P>Type: TEXT</P> 337 */ 338 public static final String PHONE_ACCOUNT_ID = "subscription_id"; 339 340 /** 341 * Adds a call to the call log. 342 * 343 * @param ci the CallerInfo object to get the target contact from. Can be null 344 * if the contact is unknown. 345 * @param context the context used to get the ContentResolver 346 * @param number the phone number to be added to the calls db 347 * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which 348 * is set by the network and denotes the number presenting rules for 349 * "allowed", "payphone", "restricted" or "unknown" 350 * @param callType enumerated values for "incoming", "outgoing", or "missed" 351 * @param features features of the call (e.g. Video). 352 * @param accountHandle The accountHandle object identifying the provider of the call 353 * @param start time stamp for the call in milliseconds 354 * @param duration call duration in seconds 355 * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for 356 * the call. 357 * @result The URI of the call log entry belonging to the user that made or received this 358 * call. 359 * {@hide} 360 */ 361 public static Uri addCall(CallerInfo ci, Context context, String number, 362 int presentation, int callType, int features, PhoneAccountHandle accountHandle, 363 long start, int duration, Long dataUsage) { 364 return addCall(ci, context, number, presentation, callType, features, accountHandle, 365 start, duration, dataUsage, false); 366 } 367 368 /** 369 * Adds a call to the call log. 370 * 371 * @param ci the CallerInfo object to get the target contact from. Can be null 372 * if the contact is unknown. 373 * @param context the context used to get the ContentResolver 374 * @param number the phone number to be added to the calls db 375 * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which 376 * is set by the network and denotes the number presenting rules for 377 * "allowed", "payphone", "restricted" or "unknown" 378 * @param callType enumerated values for "incoming", "outgoing", or "missed" 379 * @param features features of the call (e.g. Video). 380 * @param accountHandle The accountHandle object identifying the provider of the call 381 * @param start time stamp for the call in milliseconds 382 * @param duration call duration in seconds 383 * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for 384 * the call. 385 * @param addForAllUsers If true, the call is added to the call log of all currently 386 * running users. The caller must have the MANAGE_USERS permission if this is true. 387 * 388 * @result The URI of the call log entry belonging to the user that made or received this 389 * call. 390 * {@hide} 391 */ 392 public static Uri addCall(CallerInfo ci, Context context, String number, 393 int presentation, int callType, int features, PhoneAccountHandle accountHandle, 394 long start, int duration, Long dataUsage, boolean addForAllUsers) { 395 final ContentResolver resolver = context.getContentResolver(); 396 int numberPresentation = PRESENTATION_ALLOWED; 397 398 // Remap network specified number presentation types 399 // PhoneConstants.PRESENTATION_xxx to calllog number presentation types 400 // Calls.PRESENTATION_xxx, in order to insulate the persistent calllog 401 // from any future radio changes. 402 // If the number field is empty set the presentation type to Unknown. 403 if (presentation == PhoneConstants.PRESENTATION_RESTRICTED) { 404 numberPresentation = PRESENTATION_RESTRICTED; 405 } else if (presentation == PhoneConstants.PRESENTATION_PAYPHONE) { 406 numberPresentation = PRESENTATION_PAYPHONE; 407 } else if (TextUtils.isEmpty(number) 408 || presentation == PhoneConstants.PRESENTATION_UNKNOWN) { 409 numberPresentation = PRESENTATION_UNKNOWN; 410 } 411 if (numberPresentation != PRESENTATION_ALLOWED) { 412 number = ""; 413 if (ci != null) { 414 ci.name = ""; 415 } 416 } 417 418 // accountHandle information 419 String accountComponentString = null; 420 String accountId = null; 421 if (accountHandle != null) { 422 accountComponentString = accountHandle.getComponentName().flattenToString(); 423 accountId = accountHandle.getId(); 424 } 425 426 ContentValues values = new ContentValues(6); 427 428 values.put(NUMBER, number); 429 values.put(NUMBER_PRESENTATION, Integer.valueOf(numberPresentation)); 430 values.put(TYPE, Integer.valueOf(callType)); 431 values.put(FEATURES, features); 432 values.put(DATE, Long.valueOf(start)); 433 values.put(DURATION, Long.valueOf(duration)); 434 if (dataUsage != null) { 435 values.put(DATA_USAGE, dataUsage); 436 } 437 values.put(PHONE_ACCOUNT_COMPONENT_NAME, accountComponentString); 438 values.put(PHONE_ACCOUNT_ID, accountId); 439 values.put(NEW, Integer.valueOf(1)); 440 if (callType == MISSED_TYPE) { 441 values.put(IS_READ, Integer.valueOf(0)); 442 } 443 if (ci != null) { 444 values.put(CACHED_NAME, ci.name); 445 values.put(CACHED_NUMBER_TYPE, ci.numberType); 446 values.put(CACHED_NUMBER_LABEL, ci.numberLabel); 447 } 448 449 if ((ci != null) && (ci.contactIdOrZero > 0)) { 450 // Update usage information for the number associated with the contact ID. 451 // We need to use both the number and the ID for obtaining a data ID since other 452 // contacts may have the same number. 453 454 final Cursor cursor; 455 456 // We should prefer normalized one (probably coming from 457 // Phone.NORMALIZED_NUMBER column) first. If it isn't available try others. 458 if (ci.normalizedNumber != null) { 459 final String normalizedPhoneNumber = ci.normalizedNumber; 460 cursor = resolver.query(Phone.CONTENT_URI, 461 new String[] { Phone._ID }, 462 Phone.CONTACT_ID + " =? AND " + Phone.NORMALIZED_NUMBER + " =?", 463 new String[] { String.valueOf(ci.contactIdOrZero), 464 normalizedPhoneNumber}, 465 null); 466 } else { 467 final String phoneNumber = ci.phoneNumber != null ? ci.phoneNumber : number; 468 cursor = resolver.query( 469 Uri.withAppendedPath(Callable.CONTENT_FILTER_URI, 470 Uri.encode(phoneNumber)), 471 new String[] { Phone._ID }, 472 Phone.CONTACT_ID + " =?", 473 new String[] { String.valueOf(ci.contactIdOrZero) }, 474 null); 475 } 476 477 if (cursor != null) { 478 try { 479 if (cursor.getCount() > 0 && cursor.moveToFirst()) { 480 final Uri feedbackUri = DataUsageFeedback.FEEDBACK_URI.buildUpon() 481 .appendPath(cursor.getString(0)) 482 .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, 483 DataUsageFeedback.USAGE_TYPE_CALL) 484 .build(); 485 resolver.update(feedbackUri, new ContentValues(), null, null); 486 } 487 } finally { 488 cursor.close(); 489 } 490 } 491 } 492 493 Uri result = null; 494 495 if (addForAllUsers) { 496 // Insert the entry for all currently running users, in order to trigger any 497 // ContentObservers currently set on the call log. 498 final UserManager userManager = (UserManager) context.getSystemService( 499 Context.USER_SERVICE); 500 List<UserInfo> users = userManager.getUsers(true); 501 final int currentUserId = userManager.getUserHandle(); 502 final int count = users.size(); 503 for (int i = 0; i < count; i++) { 504 final UserInfo user = users.get(i); 505 final UserHandle userHandle = user.getUserHandle(); 506 if (userManager.isUserRunning(userHandle) && 507 !userManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS, 508 userHandle)) { 509 Uri uri = addEntryAndRemoveExpiredEntries(context, 510 ContentProvider.maybeAddUserId(CONTENT_URI, user.id), values); 511 if (user.id == currentUserId) { 512 result = uri; 513 } 514 } 515 } 516 } else { 517 result = addEntryAndRemoveExpiredEntries(context, CONTENT_URI, values); 518 } 519 520 return result; 521 } 522 523 /** 524 * Query the call log database for the last dialed number. 525 * @param context Used to get the content resolver. 526 * @return The last phone number dialed (outgoing) or an empty 527 * string if none exist yet. 528 */ 529 public static String getLastOutgoingCall(Context context) { 530 final ContentResolver resolver = context.getContentResolver(); 531 Cursor c = null; 532 try { 533 c = resolver.query( 534 CONTENT_URI, 535 new String[] {NUMBER}, 536 TYPE + " = " + OUTGOING_TYPE, 537 null, 538 DEFAULT_SORT_ORDER + " LIMIT 1"); 539 if (c == null || !c.moveToFirst()) { 540 return ""; 541 } 542 return c.getString(0); 543 } finally { 544 if (c != null) c.close(); 545 } 546 } 547 548 private static Uri addEntryAndRemoveExpiredEntries(Context context, Uri uri, 549 ContentValues values) { 550 final ContentResolver resolver = context.getContentResolver(); 551 Uri result = resolver.insert(uri, values); 552 resolver.delete(uri, "_id IN " + 553 "(SELECT _id FROM calls ORDER BY " + DEFAULT_SORT_ORDER 554 + " LIMIT -1 OFFSET 500)", null); 555 return result; 556 } 557 } 558} 559