2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package android.provider;
20 import android.content.ContentProvider;
21 import android.content.ContentResolver;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.UserInfo;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.os.UserHandle;
29 import android.os.UserManager;
30 import android.provider.ContactsContract.CommonDataKinds.Callable;
31 import android.provider.ContactsContract.CommonDataKinds.Phone;
32 import android.provider.ContactsContract.DataUsageFeedback;
33 import android.telecom.PhoneAccountHandle;
34 import android.text.TextUtils;
36 import com.android.internal.telephony.CallerInfo;
37 import com.android.internal.telephony.PhoneConstants;
39 import java.util.List;
42 * The CallLog provider contains information about placed and received calls.
44 public class CallLog {
45 public static final String AUTHORITY = "call_log";
48 * The content:// style URL for this provider
50 public static final Uri CONTENT_URI =
51 Uri.parse("content://" + AUTHORITY);
54 * Contains the recent calls.
56 public static class Calls implements BaseColumns {
58 * The content:// style URL for this table
60 public static final Uri CONTENT_URI =
61 Uri.parse("content://call_log/calls");
64 * The content:// style URL for filtering this table on phone numbers
66 public static final Uri CONTENT_FILTER_URI =
67 Uri.parse("content://call_log/calls/filter");
70 * Query parameter used to limit the number of call logs returned.
74 public static final String LIMIT_PARAM_KEY = "limit";
77 * Query parameter used to specify the starting record to return.
81 public static final String OFFSET_PARAM_KEY = "offset";
84 * An optional URI parameter which instructs the provider to allow the operation to be
85 * applied to voicemail records as well.
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.
94 public static final String ALLOW_VOICEMAILS_PARAM_KEY = "allow_voicemails";
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.
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.
106 * The following example brings up the call log, showing only missed calls.
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);
115 public static final String EXTRA_CALL_TYPE_FILTER =
116 "android.provider.extra.CALL_TYPE_FILTER";
119 * Content uri used to access call log entries, including voicemail records. You must have
120 * the READ_CALL_LOG and WRITE_CALL_LOG permissions to read and write to the call log, as
121 * well as READ_VOICEMAIL and WRITE_VOICEMAIL permissions to read and write voicemails.
123 public static final Uri CONTENT_URI_WITH_VOICEMAIL = CONTENT_URI.buildUpon()
124 .appendQueryParameter(ALLOW_VOICEMAILS_PARAM_KEY, "true")
128 * The default sort order for this table
130 public static final String DEFAULT_SORT_ORDER = "date DESC";
133 * The MIME type of {@link #CONTENT_URI} and {@link #CONTENT_FILTER_URI}
134 * providing a directory of calls.
136 public static final String CONTENT_TYPE = "vnd.android.cursor.dir/calls";
139 * The MIME type of a {@link #CONTENT_URI} sub-directory of a single
142 public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/calls";
145 * The type of the call (incoming, outgoing or missed).
146 * <P>Type: INTEGER (int)</P>
148 public static final String TYPE = "type";
150 /** Call log type for incoming calls. */
151 public static final int INCOMING_TYPE = 1;
152 /** Call log type for outgoing calls. */
153 public static final int OUTGOING_TYPE = 2;
154 /** Call log type for missed calls. */
155 public static final int MISSED_TYPE = 3;
156 /** Call log type for voicemails. */
157 public static final int VOICEMAIL_TYPE = 4;
160 * Bit-mask describing features of the call (e.g. video).
162 * <P>Type: INTEGER (int)</P>
164 public static final String FEATURES = "features";
166 /** Call had video. */
167 public static final int FEATURES_VIDEO = 0x1;
170 * The phone number as the user entered it.
173 public static final String NUMBER = "number";
176 * The number presenting rules set by the network.
181 * <li>{@link #PRESENTATION_ALLOWED}</li>
182 * <li>{@link #PRESENTATION_RESTRICTED}</li>
183 * <li>{@link #PRESENTATION_UNKNOWN}</li>
184 * <li>{@link #PRESENTATION_PAYPHONE}</li>
188 * <P>Type: INTEGER</P>
190 public static final String NUMBER_PRESENTATION = "presentation";
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;
202 * The ISO 3166-1 two letters country code of the country where the
203 * user received or made the call.
208 public static final String COUNTRY_ISO = "countryiso";
211 * The date the call occured, in milliseconds since the epoch
212 * <P>Type: INTEGER (long)</P>
214 public static final String DATE = "date";
217 * The duration of the call in seconds
218 * <P>Type: INTEGER (long)</P>
220 public static final String DURATION = "duration";
223 * The data usage of the call in bytes.
224 * <P>Type: INTEGER (long)</P>
226 public static final String DATA_USAGE = "data_usage";
229 * Whether or not the call has been acknowledged
230 * <P>Type: INTEGER (boolean)</P>
232 public static final String NEW = "new";
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.
240 public static final String CACHED_NAME = "name";
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>
249 public static final String CACHED_NUMBER_TYPE = "numbertype";
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.
258 public static final String CACHED_NUMBER_LABEL = "numberlabel";
261 * URI of the voicemail entry. Populated only for {@link #VOICEMAIL_TYPE}.
264 public static final String VOICEMAIL_URI = "voicemail_uri";
267 * Transcription of the call or voicemail entry. This will only be populated for call log
268 * entries of type {@link #VOICEMAIL_TYPE} that have valid transcriptions.
270 public static final String TRANSCRIPTION = "transcription";
273 * Whether this item has been read or otherwise consumed by the user.
275 * Unlike the {@link #NEW} field, which requires the user to have acknowledged the
276 * existence of the entry, this implies the user has interacted with the entry.
277 * <P>Type: INTEGER (boolean)</P>
279 public static final String IS_READ = "is_read";
282 * A geocoded location for the number associated with this call.
284 * The string represents a city, state, or country associated with the number.
287 public static final String GEOCODED_LOCATION = "geocoded_location";
290 * The cached URI to look up the contact associated with the phone number, if it exists.
291 * This value may not be current if the contact information associated with this number
295 public static final String CACHED_LOOKUP_URI = "lookup_uri";
298 * The cached phone number of the contact which matches this entry, if it exists.
299 * This value may not be current if the contact information associated with this number
303 public static final String CACHED_MATCHED_NUMBER = "matched_number";
306 * The cached normalized(E164) version of the phone number, if it exists.
307 * This value may not be current if the contact information associated with this number
311 public static final String CACHED_NORMALIZED_NUMBER = "normalized_number";
314 * The cached photo id of the picture associated with the phone number, if it exists.
315 * This value may not be current if the contact information associated with this number
317 * <P>Type: INTEGER (long)</P>
319 public static final String CACHED_PHOTO_ID = "photo_id";
322 * The cached phone number, formatted with formatting rules based on the country the
323 * user was in when the call was made or received.
324 * This value is not guaranteed to be present, and may not be current if the contact
325 * information associated with this number
329 public static final String CACHED_FORMATTED_NUMBER = "formatted_number";
331 // Note: PHONE_ACCOUNT_* constant values are "subscription_*" due to a historic naming
332 // that was encoded into call log databases.
335 * The component name of the account in string form.
338 public static final String PHONE_ACCOUNT_COMPONENT_NAME = "subscription_component_name";
341 * The identifier of a account that is unique to a specified component.
344 public static final String PHONE_ACCOUNT_ID = "subscription_id";
347 * The identifier of a account that is unique to a specified component. Equivalent value
348 * to {@link #PHONE_ACCOUNT_ID}. For ContactsProvider internal use only.
349 * <P>Type: INTEGER</P>
353 public static final String SUB_ID = "sub_id";
356 * If a successful call is made that is longer than this duration, update the phone number
357 * in the ContactsProvider with the normalized version of the number, based on the user's
358 * current country code.
360 private static final int MIN_DURATION_FOR_NORMALIZED_NUMBER_UPDATE_MS = 1000 * 10;
363 * Adds a call to the call log.
365 * @param ci the CallerInfo object to get the target contact from. Can be null
366 * if the contact is unknown.
367 * @param context the context used to get the ContentResolver
368 * @param number the phone number to be added to the calls db
369 * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which
370 * is set by the network and denotes the number presenting rules for
371 * "allowed", "payphone", "restricted" or "unknown"
372 * @param callType enumerated values for "incoming", "outgoing", or "missed"
373 * @param features features of the call (e.g. Video).
374 * @param accountHandle The accountHandle object identifying the provider of the call
375 * @param start time stamp for the call in milliseconds
376 * @param duration call duration in seconds
377 * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for
379 * @result The URI of the call log entry belonging to the user that made or received this
383 public static Uri addCall(CallerInfo ci, Context context, String number,
384 int presentation, int callType, int features, PhoneAccountHandle accountHandle,
385 long start, int duration, Long dataUsage) {
386 // FIXME using -1 as subId instead of SubscriptionManager.INVALID_SUB_ID
387 return addCall(ci, context, number, presentation, callType, features, accountHandle,
388 start, duration, dataUsage, false);
393 * Adds a call to the call log.
395 * @param ci the CallerInfo object to get the target contact from. Can be null
396 * if the contact is unknown.
397 * @param context the context used to get the ContentResolver
398 * @param number the phone number to be added to the calls db
399 * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which
400 * is set by the network and denotes the number presenting rules for
401 * "allowed", "payphone", "restricted" or "unknown"
402 * @param callType enumerated values for "incoming", "outgoing", or "missed"
403 * @param features features of the call (e.g. Video).
404 * @param accountHandle The accountHandle object identifying the provider of the call
405 * @param start time stamp for the call in milliseconds
406 * @param duration call duration in seconds
407 * @param subId the subscription id.
408 * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for
410 * @param addForAllUsers If true, the call is added to the call log of all currently
411 * running users. The caller must have the MANAGE_USERS permission if this is true.
413 * @result The URI of the call log entry belonging to the user that made or received this
417 public static Uri addCall(CallerInfo ci, Context context, String number,
418 int presentation, int callType, int features, PhoneAccountHandle accountHandle,
419 long start, int duration, Long dataUsage, boolean addForAllUsers) {
420 final ContentResolver resolver = context.getContentResolver();
421 int numberPresentation = PRESENTATION_ALLOWED;
423 // Remap network specified number presentation types
424 // PhoneConstants.PRESENTATION_xxx to calllog number presentation types
425 // Calls.PRESENTATION_xxx, in order to insulate the persistent calllog
426 // from any future radio changes.
427 // If the number field is empty set the presentation type to Unknown.
428 if (presentation == PhoneConstants.PRESENTATION_RESTRICTED) {
429 numberPresentation = PRESENTATION_RESTRICTED;
430 } else if (presentation == PhoneConstants.PRESENTATION_PAYPHONE) {
431 numberPresentation = PRESENTATION_PAYPHONE;
432 } else if (TextUtils.isEmpty(number)
433 || presentation == PhoneConstants.PRESENTATION_UNKNOWN) {
434 numberPresentation = PRESENTATION_UNKNOWN;
436 if (numberPresentation != PRESENTATION_ALLOWED) {
443 // accountHandle information
444 String accountComponentString = null;
445 String accountId = null;
446 if (accountHandle != null) {
447 accountComponentString = accountHandle.getComponentName().flattenToString();
448 accountId = accountHandle.getId();
451 ContentValues values = new ContentValues(6);
453 values.put(NUMBER, number);
454 values.put(NUMBER_PRESENTATION, Integer.valueOf(numberPresentation));
455 values.put(TYPE, Integer.valueOf(callType));
456 values.put(FEATURES, features);
457 values.put(DATE, Long.valueOf(start));
458 values.put(DURATION, Long.valueOf(duration));
459 if (dataUsage != null) {
460 values.put(DATA_USAGE, dataUsage);
462 values.put(PHONE_ACCOUNT_COMPONENT_NAME, accountComponentString);
463 values.put(PHONE_ACCOUNT_ID, accountId);
464 values.put(NEW, Integer.valueOf(1));
466 if (callType == MISSED_TYPE) {
467 values.put(IS_READ, Integer.valueOf(0));
470 values.put(CACHED_NAME, ci.name);
471 values.put(CACHED_NUMBER_TYPE, ci.numberType);
472 values.put(CACHED_NUMBER_LABEL, ci.numberLabel);
475 if ((ci != null) && (ci.contactIdOrZero > 0)) {
476 // Update usage information for the number associated with the contact ID.
477 // We need to use both the number and the ID for obtaining a data ID since other
478 // contacts may have the same number.
482 // We should prefer normalized one (probably coming from
483 // Phone.NORMALIZED_NUMBER column) first. If it isn't available try others.
484 if (ci.normalizedNumber != null) {
485 final String normalizedPhoneNumber = ci.normalizedNumber;
486 cursor = resolver.query(Phone.CONTENT_URI,
487 new String[] { Phone._ID },
488 Phone.CONTACT_ID + " =? AND " + Phone.NORMALIZED_NUMBER + " =?",
489 new String[] { String.valueOf(ci.contactIdOrZero),
490 normalizedPhoneNumber},
493 final String phoneNumber = ci.phoneNumber != null ? ci.phoneNumber : number;
494 cursor = resolver.query(
495 Uri.withAppendedPath(Callable.CONTENT_FILTER_URI,
496 Uri.encode(phoneNumber)),
497 new String[] { Phone._ID },
498 Phone.CONTACT_ID + " =?",
499 new String[] { String.valueOf(ci.contactIdOrZero) },
503 if (cursor != null) {
505 if (cursor.getCount() > 0 && cursor.moveToFirst()) {
506 final Uri feedbackUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
507 .appendPath(cursor.getString(0))
508 .appendQueryParameter(DataUsageFeedback.USAGE_TYPE,
509 DataUsageFeedback.USAGE_TYPE_CALL)
511 resolver.update(feedbackUri, new ContentValues(), null, null);
521 if (addForAllUsers) {
522 // Insert the entry for all currently running users, in order to trigger any
523 // ContentObservers currently set on the call log.
524 final UserManager userManager = (UserManager) context.getSystemService(
525 Context.USER_SERVICE);
526 List<UserInfo> users = userManager.getUsers(true);
527 final int currentUserId = userManager.getUserHandle();
528 final int count = users.size();
529 for (int i = 0; i < count; i++) {
530 final UserInfo user = users.get(i);
531 final UserHandle userHandle = user.getUserHandle();
532 if (userManager.isUserRunning(userHandle)
533 && !userManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
535 && !user.isManagedProfile()) {
536 Uri uri = addEntryAndRemoveExpiredEntries(context,
537 ContentProvider.maybeAddUserId(CONTENT_URI, user.id), values);
538 if (user.id == currentUserId) {
544 result = addEntryAndRemoveExpiredEntries(context, CONTENT_URI, values);
551 * Query the call log database for the last dialed number.
552 * @param context Used to get the content resolver.
553 * @return The last phone number dialed (outgoing) or an empty
554 * string if none exist yet.
556 public static String getLastOutgoingCall(Context context) {
557 final ContentResolver resolver = context.getContentResolver();
562 new String[] {NUMBER},
563 TYPE + " = " + OUTGOING_TYPE,
565 DEFAULT_SORT_ORDER + " LIMIT 1");
566 if (c == null || !c.moveToFirst()) {
569 return c.getString(0);
571 if (c != null) c.close();
575 private static Uri addEntryAndRemoveExpiredEntries(Context context, Uri uri,
576 ContentValues values) {
577 final ContentResolver resolver = context.getContentResolver();
578 Uri result = resolver.insert(uri, values);
579 resolver.delete(uri, "_id IN " +
580 "(SELECT _id FROM calls ORDER BY " + DEFAULT_SORT_ORDER
581 + " LIMIT -1 OFFSET 500)", null);