CallLog.java revision ce759a1a31ec2c1b65af6cbc4d92f48ca58e1b6f
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.location.Country; 28import android.location.CountryDetector; 29import android.net.Uri; 30import android.os.UserHandle; 31import android.os.UserManager; 32import android.provider.ContactsContract.CommonDataKinds.Callable; 33import android.provider.ContactsContract.CommonDataKinds.Phone; 34import android.provider.ContactsContract.Data; 35import android.provider.ContactsContract.DataUsageFeedback; 36import android.telecom.PhoneAccount; 37import android.telecom.PhoneAccountHandle; 38import android.telecom.TelecomManager; 39import android.telephony.PhoneNumberUtils; 40import android.text.TextUtils; 41 42import com.android.internal.telephony.CallerInfo; 43import com.android.internal.telephony.PhoneConstants; 44 45import java.util.List; 46 47/** 48 * The CallLog provider contains information about placed and received calls. 49 */ 50public class CallLog { 51 private static final String LOG_TAG = "CallLog"; 52 53 public static final String AUTHORITY = "call_log"; 54 55 /** 56 * The content:// style URL for this provider 57 */ 58 public static final Uri CONTENT_URI = 59 Uri.parse("content://" + AUTHORITY); 60 61 /** 62 * Contains the recent calls. 63 */ 64 public static class Calls implements BaseColumns { 65 /** 66 * The content:// style URL for this table 67 */ 68 public static final Uri CONTENT_URI = 69 Uri.parse("content://call_log/calls"); 70 71 /** 72 * The content:// style URL for filtering this table on phone numbers 73 */ 74 public static final Uri CONTENT_FILTER_URI = 75 Uri.parse("content://call_log/calls/filter"); 76 77 /** 78 * Query parameter used to limit the number of call logs returned. 79 * <p> 80 * TYPE: integer 81 */ 82 public static final String LIMIT_PARAM_KEY = "limit"; 83 84 /** 85 * Query parameter used to specify the starting record to return. 86 * <p> 87 * TYPE: integer 88 */ 89 public static final String OFFSET_PARAM_KEY = "offset"; 90 91 /** 92 * An optional URI parameter which instructs the provider to allow the operation to be 93 * applied to voicemail records as well. 94 * <p> 95 * TYPE: Boolean 96 * <p> 97 * Using this parameter with a value of {@code true} will result in a security error if the 98 * calling package does not have appropriate permissions to access voicemails. 99 * 100 * @hide 101 */ 102 public static final String ALLOW_VOICEMAILS_PARAM_KEY = "allow_voicemails"; 103 104 /** 105 * An optional extra used with {@link #CONTENT_TYPE Calls.CONTENT_TYPE} and 106 * {@link Intent#ACTION_VIEW} to specify that the presented list of calls should be 107 * filtered for a particular call type. 108 * 109 * Applications implementing a call log UI should check for this extra, and display a 110 * filtered list of calls based on the specified call type. If not applicable within the 111 * application's UI, it should be silently ignored. 112 * 113 * <p> 114 * The following example brings up the call log, showing only missed calls. 115 * <pre> 116 * Intent intent = new Intent(Intent.ACTION_VIEW); 117 * intent.setType(CallLog.Calls.CONTENT_TYPE); 118 * intent.putExtra(CallLog.Calls.EXTRA_CALL_TYPE_FILTER, CallLog.Calls.MISSED_TYPE); 119 * startActivity(intent); 120 * </pre> 121 * </p> 122 */ 123 public static final String EXTRA_CALL_TYPE_FILTER = 124 "android.provider.extra.CALL_TYPE_FILTER"; 125 126 /** 127 * Content uri used to access call log entries, including voicemail records. You must have 128 * the READ_CALL_LOG and WRITE_CALL_LOG permissions to read and write to the call log, as 129 * well as READ_VOICEMAIL and WRITE_VOICEMAIL permissions to read and write voicemails. 130 */ 131 public static final Uri CONTENT_URI_WITH_VOICEMAIL = CONTENT_URI.buildUpon() 132 .appendQueryParameter(ALLOW_VOICEMAILS_PARAM_KEY, "true") 133 .build(); 134 135 /** 136 * The default sort order for this table 137 */ 138 public static final String DEFAULT_SORT_ORDER = "date DESC"; 139 140 /** 141 * The MIME type of {@link #CONTENT_URI} and {@link #CONTENT_FILTER_URI} 142 * providing a directory of calls. 143 */ 144 public static final String CONTENT_TYPE = "vnd.android.cursor.dir/calls"; 145 146 /** 147 * The MIME type of a {@link #CONTENT_URI} sub-directory of a single 148 * call. 149 */ 150 public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/calls"; 151 152 /** 153 * The type of the call (incoming, outgoing or missed). 154 * <P>Type: INTEGER (int)</P> 155 * 156 * <p> 157 * Allowed values: 158 * <ul> 159 * <li>{@link #INCOMING_TYPE}</li> 160 * <li>{@link #OUTGOING_TYPE}</li> 161 * <li>{@link #MISSED_TYPE}</li> 162 * <li>{@link #VOICEMAIL_TYPE}</li> 163 * <li>{@link #REJECTED_TYPE}</li> 164 * <li>{@link #BLOCKED_TYPE}</li> 165 * </ul> 166 * </p> 167 */ 168 public static final String TYPE = "type"; 169 170 /** Call log type for incoming calls. */ 171 public static final int INCOMING_TYPE = 1; 172 /** Call log type for outgoing calls. */ 173 public static final int OUTGOING_TYPE = 2; 174 /** Call log type for missed calls. */ 175 public static final int MISSED_TYPE = 3; 176 /** Call log type for voicemails. */ 177 public static final int VOICEMAIL_TYPE = 4; 178 /** Call log type for calls rejected by direct user action. */ 179 public static final int REJECTED_TYPE = 5; 180 /** Call log type for calls blocked automatically. */ 181 public static final int BLOCKED_TYPE = 6; 182 183 /** 184 * Bit-mask describing features of the call (e.g. video). 185 * 186 * <P>Type: INTEGER (int)</P> 187 */ 188 public static final String FEATURES = "features"; 189 190 /** Call had video. */ 191 public static final int FEATURES_VIDEO = 0x1; 192 193 /** 194 * The phone number as the user entered it. 195 * <P>Type: TEXT</P> 196 */ 197 public static final String NUMBER = "number"; 198 199 /** 200 * The number presenting rules set by the network. 201 * 202 * <p> 203 * Allowed values: 204 * <ul> 205 * <li>{@link #PRESENTATION_ALLOWED}</li> 206 * <li>{@link #PRESENTATION_RESTRICTED}</li> 207 * <li>{@link #PRESENTATION_UNKNOWN}</li> 208 * <li>{@link #PRESENTATION_PAYPHONE}</li> 209 * </ul> 210 * </p> 211 * 212 * <P>Type: INTEGER</P> 213 */ 214 public static final String NUMBER_PRESENTATION = "presentation"; 215 216 /** Number is allowed to display for caller id. */ 217 public static final int PRESENTATION_ALLOWED = 1; 218 /** Number is blocked by user. */ 219 public static final int PRESENTATION_RESTRICTED = 2; 220 /** Number is not specified or unknown by network. */ 221 public static final int PRESENTATION_UNKNOWN = 3; 222 /** Number is a pay phone. */ 223 public static final int PRESENTATION_PAYPHONE = 4; 224 225 /** 226 * The ISO 3166-1 two letters country code of the country where the 227 * user received or made the call. 228 * <P> 229 * Type: TEXT 230 * </P> 231 */ 232 public static final String COUNTRY_ISO = "countryiso"; 233 234 /** 235 * The date the call occured, in milliseconds since the epoch 236 * <P>Type: INTEGER (long)</P> 237 */ 238 public static final String DATE = "date"; 239 240 /** 241 * The duration of the call in seconds 242 * <P>Type: INTEGER (long)</P> 243 */ 244 public static final String DURATION = "duration"; 245 246 /** 247 * The data usage of the call in bytes. 248 * <P>Type: INTEGER (long)</P> 249 */ 250 public static final String DATA_USAGE = "data_usage"; 251 252 /** 253 * Whether or not the call has been acknowledged 254 * <P>Type: INTEGER (boolean)</P> 255 */ 256 public static final String NEW = "new"; 257 258 /** 259 * The cached name associated with the phone number, if it exists. 260 * This value is not guaranteed to be current, if the contact information 261 * associated with this number has changed. 262 * <P>Type: TEXT</P> 263 */ 264 public static final String CACHED_NAME = "name"; 265 266 /** 267 * The cached number type (Home, Work, etc) associated with the 268 * phone number, if it exists. 269 * This value is not guaranteed to be current, if the contact information 270 * associated with this number has changed. 271 * <P>Type: INTEGER</P> 272 */ 273 public static final String CACHED_NUMBER_TYPE = "numbertype"; 274 275 /** 276 * The cached number label, for a custom number type, associated with the 277 * phone number, if it exists. 278 * This value is not guaranteed to be current, if the contact information 279 * associated with this number has changed. 280 * <P>Type: TEXT</P> 281 */ 282 public static final String CACHED_NUMBER_LABEL = "numberlabel"; 283 284 /** 285 * URI of the voicemail entry. Populated only for {@link #VOICEMAIL_TYPE}. 286 * <P>Type: TEXT</P> 287 */ 288 public static final String VOICEMAIL_URI = "voicemail_uri"; 289 290 /** 291 * Transcription of the call or voicemail entry. This will only be populated for call log 292 * entries of type {@link #VOICEMAIL_TYPE} that have valid transcriptions. 293 */ 294 public static final String TRANSCRIPTION = "transcription"; 295 296 /** 297 * Whether this item has been read or otherwise consumed by the user. 298 * <p> 299 * Unlike the {@link #NEW} field, which requires the user to have acknowledged the 300 * existence of the entry, this implies the user has interacted with the entry. 301 * <P>Type: INTEGER (boolean)</P> 302 */ 303 public static final String IS_READ = "is_read"; 304 305 /** 306 * A geocoded location for the number associated with this call. 307 * <p> 308 * The string represents a city, state, or country associated with the number. 309 * <P>Type: TEXT</P> 310 */ 311 public static final String GEOCODED_LOCATION = "geocoded_location"; 312 313 /** 314 * The cached URI to look up the contact associated with the phone number, if it exists. 315 * This value may not be current if the contact information associated with this number 316 * has changed. 317 * <P>Type: TEXT</P> 318 */ 319 public static final String CACHED_LOOKUP_URI = "lookup_uri"; 320 321 /** 322 * The cached phone number of the contact which matches this entry, if it exists. 323 * This value may not be current if the contact information associated with this number 324 * has changed. 325 * <P>Type: TEXT</P> 326 */ 327 public static final String CACHED_MATCHED_NUMBER = "matched_number"; 328 329 /** 330 * The cached normalized(E164) version of the phone number, if it exists. 331 * This value may not be current if the contact information associated with this number 332 * has changed. 333 * <P>Type: TEXT</P> 334 */ 335 public static final String CACHED_NORMALIZED_NUMBER = "normalized_number"; 336 337 /** 338 * The cached photo id of the picture associated with the phone number, if it exists. 339 * This value may not be current if the contact information associated with this number 340 * has changed. 341 * <P>Type: INTEGER (long)</P> 342 */ 343 public static final String CACHED_PHOTO_ID = "photo_id"; 344 345 /** 346 * The cached photo URI of the picture associated with the phone number, if it exists. 347 * This value may not be current if the contact information associated with this number 348 * has changed. 349 * <P>Type: TEXT (URI)</P> 350 */ 351 public static final String CACHED_PHOTO_URI = "photo_uri"; 352 353 /** 354 * The cached phone number, formatted with formatting rules based on the country the 355 * user was in when the call was made or received. 356 * This value is not guaranteed to be present, and may not be current if the contact 357 * information associated with this number 358 * has changed. 359 * <P>Type: TEXT</P> 360 */ 361 public static final String CACHED_FORMATTED_NUMBER = "formatted_number"; 362 363 // Note: PHONE_ACCOUNT_* constant values are "subscription_*" due to a historic naming 364 // that was encoded into call log databases. 365 366 /** 367 * The component name of the account used to place or receive the call; in string form. 368 * <P>Type: TEXT</P> 369 */ 370 public static final String PHONE_ACCOUNT_COMPONENT_NAME = "subscription_component_name"; 371 372 /** 373 * The identifier for the account used to place or receive the call. 374 * <P>Type: TEXT</P> 375 */ 376 public static final String PHONE_ACCOUNT_ID = "subscription_id"; 377 378 /** 379 * The address associated with the account used to place or receive the call; in string 380 * form. For SIM-based calls, this is the user's own phone number. 381 * <P>Type: TEXT</P> 382 * 383 * @hide 384 */ 385 public static final String PHONE_ACCOUNT_ADDRESS = "phone_account_address"; 386 387 /** 388 * Indicates that the entry will be hidden from all queries until the associated 389 * {@link android.telecom.PhoneAccount} is registered with the system. 390 * <P>Type: INTEGER</P> 391 * 392 * @hide 393 */ 394 public static final String PHONE_ACCOUNT_HIDDEN = "phone_account_hidden"; 395 396 /** 397 * The subscription ID used to place this call. This is no longer used and has been 398 * replaced with PHONE_ACCOUNT_COMPONENT_NAME/PHONE_ACCOUNT_ID. 399 * For ContactsProvider internal use only. 400 * <P>Type: INTEGER</P> 401 * 402 * @Deprecated 403 * @hide 404 */ 405 public static final String SUB_ID = "sub_id"; 406 407 /** 408 * The post-dial portion of a dialed number, including any digits dialed after a 409 * {@link TelecomManager#DTMF_CHARACTER_PAUSE} or a {@link 410 * TelecomManager#DTMF_CHARACTER_WAIT} and these characters themselves. 411 * <P>Type: TEXT</P> 412 */ 413 public static final String POST_DIAL_DIGITS = "post_dial_digits"; 414 415 /** 416 * Indicates that the entry will be copied from primary user to other users. 417 * <P>Type: INTEGER</P> 418 * 419 * @hide 420 */ 421 public static final String ADD_FOR_ALL_USERS = "add_for_all_users"; 422 423 /** 424 * If a successful call is made that is longer than this duration, update the phone number 425 * in the ContactsProvider with the normalized version of the number, based on the user's 426 * current country code. 427 */ 428 private static final int MIN_DURATION_FOR_NORMALIZED_NUMBER_UPDATE_MS = 1000 * 10; 429 430 /** 431 * Adds a call to the call log. 432 * 433 * @param ci the CallerInfo object to get the target contact from. Can be null 434 * if the contact is unknown. 435 * @param context the context used to get the ContentResolver 436 * @param number the phone number to be added to the calls db 437 * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which 438 * is set by the network and denotes the number presenting rules for 439 * "allowed", "payphone", "restricted" or "unknown" 440 * @param callType enumerated values for "incoming", "outgoing", or "missed" 441 * @param features features of the call (e.g. Video). 442 * @param accountHandle The accountHandle object identifying the provider of the call 443 * @param start time stamp for the call in milliseconds 444 * @param duration call duration in seconds 445 * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for 446 * the call. 447 * @result The URI of the call log entry belonging to the user that made or received this 448 * call. 449 * {@hide} 450 */ 451 public static Uri addCall(CallerInfo ci, Context context, String number, 452 int presentation, int callType, int features, PhoneAccountHandle accountHandle, 453 long start, int duration, Long dataUsage) { 454 return addCall(ci, context, number, "", presentation, callType, features, accountHandle, 455 start, duration, dataUsage, false, null, false); 456 } 457 458 459 /** 460 * Adds a call to the call log. 461 * 462 * @param ci the CallerInfo object to get the target contact from. Can be null 463 * if the contact is unknown. 464 * @param context the context used to get the ContentResolver 465 * @param number the phone number to be added to the calls db 466 * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which 467 * is set by the network and denotes the number presenting rules for 468 * "allowed", "payphone", "restricted" or "unknown" 469 * @param callType enumerated values for "incoming", "outgoing", or "missed" 470 * @param features features of the call (e.g. Video). 471 * @param accountHandle The accountHandle object identifying the provider of the call 472 * @param start time stamp for the call in milliseconds 473 * @param duration call duration in seconds 474 * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for 475 * the call. 476 * @param addForAllUsers If true, the call is added to the call log of all currently 477 * running users. The caller must have the MANAGE_USERS permission if this is true. 478 * @param userToBeInsertedTo {@link UserHandle} of user that the call is going to be 479 * inserted to. null if it is inserted to the current user. The 480 * value is ignored if @{link addForAllUsers} is true. 481 * @result The URI of the call log entry belonging to the user that made or received this 482 * call. 483 * {@hide} 484 */ 485 public static Uri addCall(CallerInfo ci, Context context, String number, 486 String postDialDigits, int presentation, int callType, int features, 487 PhoneAccountHandle accountHandle, long start, int duration, Long dataUsage, 488 boolean addForAllUsers, UserHandle userToBeInsertedTo) { 489 return addCall(ci, context, number, postDialDigits, presentation, callType, features, 490 accountHandle, start, duration, dataUsage, addForAllUsers, userToBeInsertedTo, 491 false); 492 } 493 494 /** 495 * Adds a call to the call log. 496 * 497 * @param ci the CallerInfo object to get the target contact from. Can be null 498 * if the contact is unknown. 499 * @param context the context used to get the ContentResolver 500 * @param number the phone number to be added to the calls db 501 * @param postDialDigits the post-dial digits that were dialed after the number, 502 * if it was outgoing. Otherwise it is ''. 503 * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which 504 * is set by the network and denotes the number presenting rules for 505 * "allowed", "payphone", "restricted" or "unknown" 506 * @param callType enumerated values for "incoming", "outgoing", or "missed" 507 * @param features features of the call (e.g. Video). 508 * @param accountHandle The accountHandle object identifying the provider of the call 509 * @param start time stamp for the call in milliseconds 510 * @param duration call duration in seconds 511 * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for 512 * the call. 513 * @param addForAllUsers If true, the call is added to the call log of all currently 514 * running users. The caller must have the MANAGE_USERS permission if this is true. 515 * @param userToBeInsertedTo {@link UserHandle} of user that the call is going to be 516 * inserted to. null if it is inserted to the current user. The 517 * value is ignored if @{link addForAllUsers} is true. 518 * @param is_read Flag to show if the missed call log has been read by the user or not. 519 * Used for call log restore of missed calls. 520 * 521 * @result The URI of the call log entry belonging to the user that made or received this 522 * call. 523 * {@hide} 524 */ 525 public static Uri addCall(CallerInfo ci, Context context, String number, 526 String postDialDigits, int presentation, int callType, int features, 527 PhoneAccountHandle accountHandle, long start, int duration, Long dataUsage, 528 boolean addForAllUsers, UserHandle userToBeInsertedTo, boolean is_read) { 529 final ContentResolver resolver = context.getContentResolver(); 530 int numberPresentation = PRESENTATION_ALLOWED; 531 532 TelecomManager tm = null; 533 try { 534 tm = TelecomManager.from(context); 535 } catch (UnsupportedOperationException e) {} 536 537 String accountAddress = null; 538 if (tm != null && accountHandle != null) { 539 PhoneAccount account = tm.getPhoneAccount(accountHandle); 540 if (account != null) { 541 Uri address = account.getSubscriptionAddress(); 542 if (address != null) { 543 accountAddress = address.getSchemeSpecificPart(); 544 } 545 } 546 } 547 548 // Remap network specified number presentation types 549 // PhoneConstants.PRESENTATION_xxx to calllog number presentation types 550 // Calls.PRESENTATION_xxx, in order to insulate the persistent calllog 551 // from any future radio changes. 552 // If the number field is empty set the presentation type to Unknown. 553 if (presentation == PhoneConstants.PRESENTATION_RESTRICTED) { 554 numberPresentation = PRESENTATION_RESTRICTED; 555 } else if (presentation == PhoneConstants.PRESENTATION_PAYPHONE) { 556 numberPresentation = PRESENTATION_PAYPHONE; 557 } else if (TextUtils.isEmpty(number) 558 || presentation == PhoneConstants.PRESENTATION_UNKNOWN) { 559 numberPresentation = PRESENTATION_UNKNOWN; 560 } 561 if (numberPresentation != PRESENTATION_ALLOWED) { 562 number = ""; 563 if (ci != null) { 564 ci.name = ""; 565 } 566 } 567 568 // accountHandle information 569 String accountComponentString = null; 570 String accountId = null; 571 if (accountHandle != null) { 572 accountComponentString = accountHandle.getComponentName().flattenToString(); 573 accountId = accountHandle.getId(); 574 } 575 576 ContentValues values = new ContentValues(6); 577 578 values.put(NUMBER, number); 579 values.put(POST_DIAL_DIGITS, postDialDigits); 580 values.put(NUMBER_PRESENTATION, Integer.valueOf(numberPresentation)); 581 values.put(TYPE, Integer.valueOf(callType)); 582 values.put(FEATURES, features); 583 values.put(DATE, Long.valueOf(start)); 584 values.put(DURATION, Long.valueOf(duration)); 585 if (dataUsage != null) { 586 values.put(DATA_USAGE, dataUsage); 587 } 588 values.put(PHONE_ACCOUNT_COMPONENT_NAME, accountComponentString); 589 values.put(PHONE_ACCOUNT_ID, accountId); 590 values.put(PHONE_ACCOUNT_ADDRESS, accountAddress); 591 values.put(NEW, Integer.valueOf(1)); 592 values.put(ADD_FOR_ALL_USERS, addForAllUsers ? 1 : 0); 593 594 if (callType == MISSED_TYPE) { 595 values.put(IS_READ, Integer.valueOf(is_read ? 1 : 0)); 596 } 597 598 if ((ci != null) && (ci.contactIdOrZero > 0)) { 599 // Update usage information for the number associated with the contact ID. 600 // We need to use both the number and the ID for obtaining a data ID since other 601 // contacts may have the same number. 602 603 final Cursor cursor; 604 605 // We should prefer normalized one (probably coming from 606 // Phone.NORMALIZED_NUMBER column) first. If it isn't available try others. 607 if (ci.normalizedNumber != null) { 608 final String normalizedPhoneNumber = ci.normalizedNumber; 609 cursor = resolver.query(Phone.CONTENT_URI, 610 new String[] { Phone._ID }, 611 Phone.CONTACT_ID + " =? AND " + Phone.NORMALIZED_NUMBER + " =?", 612 new String[] { String.valueOf(ci.contactIdOrZero), 613 normalizedPhoneNumber}, 614 null); 615 } else { 616 final String phoneNumber = ci.phoneNumber != null ? ci.phoneNumber : number; 617 cursor = resolver.query( 618 Uri.withAppendedPath(Callable.CONTENT_FILTER_URI, 619 Uri.encode(phoneNumber)), 620 new String[] { Phone._ID }, 621 Phone.CONTACT_ID + " =?", 622 new String[] { String.valueOf(ci.contactIdOrZero) }, 623 null); 624 } 625 626 if (cursor != null) { 627 try { 628 if (cursor.getCount() > 0 && cursor.moveToFirst()) { 629 final String dataId = cursor.getString(0); 630 updateDataUsageStatForData(resolver, dataId); 631 if (duration >= MIN_DURATION_FOR_NORMALIZED_NUMBER_UPDATE_MS 632 && callType == Calls.OUTGOING_TYPE 633 && TextUtils.isEmpty(ci.normalizedNumber)) { 634 updateNormalizedNumber(context, resolver, dataId, number); 635 } 636 } 637 } finally { 638 cursor.close(); 639 } 640 } 641 } 642 643 Uri result = null; 644 645 if (addForAllUsers) { 646 // Insert the entry for all currently running users, in order to trigger any 647 // ContentObservers currently set on the call log. 648 final UserManager userManager = (UserManager) context.getSystemService( 649 Context.USER_SERVICE); 650 List<UserInfo> users = userManager.getUsers(true); 651 final int currentUserId = userManager.getUserHandle(); 652 final int count = users.size(); 653 for (int i = 0; i < count; i++) { 654 final UserInfo user = users.get(i); 655 final UserHandle userHandle = user.getUserHandle(); 656 if (userManager.isUserRunning(userHandle) 657 && !userManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS, 658 userHandle) 659 && !user.isManagedProfile()) { 660 Uri uri = addEntryAndRemoveExpiredEntries(context, 661 ContentProvider.maybeAddUserId(CONTENT_URI, user.id), values); 662 if (user.id == currentUserId) { 663 result = uri; 664 } 665 } 666 } 667 } else { 668 Uri uri = CONTENT_URI; 669 if (userToBeInsertedTo != null) { 670 uri = ContentProvider 671 .maybeAddUserId(CONTENT_URI, userToBeInsertedTo.getIdentifier()); 672 } 673 result = addEntryAndRemoveExpiredEntries(context, uri, values); 674 } 675 return result; 676 } 677 678 /** 679 * Query the call log database for the last dialed number. 680 * @param context Used to get the content resolver. 681 * @return The last phone number dialed (outgoing) or an empty 682 * string if none exist yet. 683 */ 684 public static String getLastOutgoingCall(Context context) { 685 final ContentResolver resolver = context.getContentResolver(); 686 Cursor c = null; 687 try { 688 c = resolver.query( 689 CONTENT_URI, 690 new String[] {NUMBER}, 691 TYPE + " = " + OUTGOING_TYPE, 692 null, 693 DEFAULT_SORT_ORDER + " LIMIT 1"); 694 if (c == null || !c.moveToFirst()) { 695 return ""; 696 } 697 return c.getString(0); 698 } finally { 699 if (c != null) c.close(); 700 } 701 } 702 703 private static Uri addEntryAndRemoveExpiredEntries(Context context, Uri uri, 704 ContentValues values) { 705 final ContentResolver resolver = context.getContentResolver(); 706 Uri result = resolver.insert(uri, values); 707 resolver.delete(uri, "_id IN " + 708 "(SELECT _id FROM calls ORDER BY " + DEFAULT_SORT_ORDER 709 + " LIMIT -1 OFFSET 500)", null); 710 return result; 711 } 712 713 private static void updateDataUsageStatForData(ContentResolver resolver, String dataId) { 714 final Uri feedbackUri = DataUsageFeedback.FEEDBACK_URI.buildUpon() 715 .appendPath(dataId) 716 .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, 717 DataUsageFeedback.USAGE_TYPE_CALL) 718 .build(); 719 resolver.update(feedbackUri, new ContentValues(), null, null); 720 } 721 722 /* 723 * Update the normalized phone number for the given dataId in the ContactsProvider, based 724 * on the user's current country. 725 */ 726 private static void updateNormalizedNumber(Context context, ContentResolver resolver, 727 String dataId, String number) { 728 if (TextUtils.isEmpty(number) || TextUtils.isEmpty(dataId)) { 729 return; 730 } 731 final String countryIso = getCurrentCountryIso(context); 732 if (TextUtils.isEmpty(countryIso)) { 733 return; 734 } 735 final String normalizedNumber = PhoneNumberUtils.formatNumberToE164(number, 736 getCurrentCountryIso(context)); 737 if (TextUtils.isEmpty(normalizedNumber)) { 738 return; 739 } 740 final ContentValues values = new ContentValues(); 741 values.put(Phone.NORMALIZED_NUMBER, normalizedNumber); 742 resolver.update(Data.CONTENT_URI, values, Data._ID + "=?", new String[] {dataId}); 743 } 744 745 private static String getCurrentCountryIso(Context context) { 746 String countryIso = null; 747 final CountryDetector detector = (CountryDetector) context.getSystemService( 748 Context.COUNTRY_DETECTOR); 749 if (detector != null) { 750 final Country country = detector.detectCountry(); 751 if (country != null) { 752 countryIso = country.getCountryIso(); 753 } 754 } 755 return countryIso; 756 } 757 } 758} 759