CallLog.java revision a07707a899b223fa9f62eec553a54b643bb1a29e
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         * The date the row is last inserted, updated, or marked as deleted, in milliseconds
425         * since the epoch. Read only.
426         * <P>Type: INTEGER (long)</P>
427         */
428        public static final String LAST_MODIFIED = "last_modified";
429
430        /**
431         * If a successful call is made that is longer than this duration, update the phone number
432         * in the ContactsProvider with the normalized version of the number, based on the user's
433         * current country code.
434         */
435        private static final int MIN_DURATION_FOR_NORMALIZED_NUMBER_UPDATE_MS = 1000 * 10;
436
437        /**
438         * Adds a call to the call log.
439         *
440         * @param ci the CallerInfo object to get the target contact from.  Can be null
441         * if the contact is unknown.
442         * @param context the context used to get the ContentResolver
443         * @param number the phone number to be added to the calls db
444         * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which
445         *        is set by the network and denotes the number presenting rules for
446         *        "allowed", "payphone", "restricted" or "unknown"
447         * @param callType enumerated values for "incoming", "outgoing", or "missed"
448         * @param features features of the call (e.g. Video).
449         * @param accountHandle The accountHandle object identifying the provider of the call
450         * @param start time stamp for the call in milliseconds
451         * @param duration call duration in seconds
452         * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for
453         *                  the call.
454         * @result The URI of the call log entry belonging to the user that made or received this
455         *        call.
456         * {@hide}
457         */
458        public static Uri addCall(CallerInfo ci, Context context, String number,
459                int presentation, int callType, int features, PhoneAccountHandle accountHandle,
460                long start, int duration, Long dataUsage) {
461            return addCall(ci, context, number, "", presentation, callType, features, accountHandle,
462                    start, duration, dataUsage, false, null, false);
463        }
464
465
466        /**
467         * Adds a call to the call log.
468         *
469         * @param ci the CallerInfo object to get the target contact from.  Can be null
470         * if the contact is unknown.
471         * @param context the context used to get the ContentResolver
472         * @param number the phone number to be added to the calls db
473         * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which
474         *        is set by the network and denotes the number presenting rules for
475         *        "allowed", "payphone", "restricted" or "unknown"
476         * @param callType enumerated values for "incoming", "outgoing", or "missed"
477         * @param features features of the call (e.g. Video).
478         * @param accountHandle The accountHandle object identifying the provider of the call
479         * @param start time stamp for the call in milliseconds
480         * @param duration call duration in seconds
481         * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for
482         *                  the call.
483         * @param addForAllUsers If true, the call is added to the call log of all currently
484         *        running users. The caller must have the MANAGE_USERS permission if this is true.
485         * @param userToBeInsertedTo {@link UserHandle} of user that the call is going to be
486         *                           inserted to. null if it is inserted to the current user. The
487         *                           value is ignored if @{link addForAllUsers} is true.
488         * @result The URI of the call log entry belonging to the user that made or received this
489         *        call.
490         * {@hide}
491         */
492        public static Uri addCall(CallerInfo ci, Context context, String number,
493                String postDialDigits, int presentation, int callType, int features,
494                PhoneAccountHandle accountHandle, long start, int duration, Long dataUsage,
495                boolean addForAllUsers, UserHandle userToBeInsertedTo) {
496            return addCall(ci, context, number, postDialDigits, presentation, callType, features,
497                    accountHandle, start, duration, dataUsage, addForAllUsers, userToBeInsertedTo,
498                    false);
499        }
500
501        /**
502         * Adds a call to the call log.
503         *
504         * @param ci the CallerInfo object to get the target contact from.  Can be null
505         * if the contact is unknown.
506         * @param context the context used to get the ContentResolver
507         * @param number the phone number to be added to the calls db
508         * @param postDialDigits the post-dial digits that were dialed after the number,
509         *        if it was outgoing. Otherwise it is ''.
510         * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which
511         *        is set by the network and denotes the number presenting rules for
512         *        "allowed", "payphone", "restricted" or "unknown"
513         * @param callType enumerated values for "incoming", "outgoing", or "missed"
514         * @param features features of the call (e.g. Video).
515         * @param accountHandle The accountHandle object identifying the provider of the call
516         * @param start time stamp for the call in milliseconds
517         * @param duration call duration in seconds
518         * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for
519         *                  the call.
520         * @param addForAllUsers If true, the call is added to the call log of all currently
521         *        running users. The caller must have the MANAGE_USERS permission if this is true.
522         * @param userToBeInsertedTo {@link UserHandle} of user that the call is going to be
523         *                           inserted to. null if it is inserted to the current user. The
524         *                           value is ignored if @{link addForAllUsers} is true.
525         * @param is_read Flag to show if the missed call log has been read by the user or not.
526         *                Used for call log restore of missed calls.
527         *
528         * @result The URI of the call log entry belonging to the user that made or received this
529         *        call.
530         * {@hide}
531         */
532        public static Uri addCall(CallerInfo ci, Context context, String number,
533                String postDialDigits, int presentation, int callType, int features,
534                PhoneAccountHandle accountHandle, long start, int duration, Long dataUsage,
535                boolean addForAllUsers, UserHandle userToBeInsertedTo, boolean is_read) {
536            final ContentResolver resolver = context.getContentResolver();
537            int numberPresentation = PRESENTATION_ALLOWED;
538
539            TelecomManager tm = null;
540            try {
541                tm = TelecomManager.from(context);
542            } catch (UnsupportedOperationException e) {}
543
544            String accountAddress = null;
545            if (tm != null && accountHandle != null) {
546                PhoneAccount account = tm.getPhoneAccount(accountHandle);
547                if (account != null) {
548                    Uri address = account.getSubscriptionAddress();
549                    if (address != null) {
550                        accountAddress = address.getSchemeSpecificPart();
551                    }
552                }
553            }
554
555            // Remap network specified number presentation types
556            // PhoneConstants.PRESENTATION_xxx to calllog number presentation types
557            // Calls.PRESENTATION_xxx, in order to insulate the persistent calllog
558            // from any future radio changes.
559            // If the number field is empty set the presentation type to Unknown.
560            if (presentation == PhoneConstants.PRESENTATION_RESTRICTED) {
561                numberPresentation = PRESENTATION_RESTRICTED;
562            } else if (presentation == PhoneConstants.PRESENTATION_PAYPHONE) {
563                numberPresentation = PRESENTATION_PAYPHONE;
564            } else if (TextUtils.isEmpty(number)
565                    || presentation == PhoneConstants.PRESENTATION_UNKNOWN) {
566                numberPresentation = PRESENTATION_UNKNOWN;
567            }
568            if (numberPresentation != PRESENTATION_ALLOWED) {
569                number = "";
570                if (ci != null) {
571                    ci.name = "";
572                }
573            }
574
575            // accountHandle information
576            String accountComponentString = null;
577            String accountId = null;
578            if (accountHandle != null) {
579                accountComponentString = accountHandle.getComponentName().flattenToString();
580                accountId = accountHandle.getId();
581            }
582
583            ContentValues values = new ContentValues(6);
584
585            values.put(NUMBER, number);
586            values.put(POST_DIAL_DIGITS, postDialDigits);
587            values.put(NUMBER_PRESENTATION, Integer.valueOf(numberPresentation));
588            values.put(TYPE, Integer.valueOf(callType));
589            values.put(FEATURES, features);
590            values.put(DATE, Long.valueOf(start));
591            values.put(DURATION, Long.valueOf(duration));
592            if (dataUsage != null) {
593                values.put(DATA_USAGE, dataUsage);
594            }
595            values.put(PHONE_ACCOUNT_COMPONENT_NAME, accountComponentString);
596            values.put(PHONE_ACCOUNT_ID, accountId);
597            values.put(PHONE_ACCOUNT_ADDRESS, accountAddress);
598            values.put(NEW, Integer.valueOf(1));
599            values.put(ADD_FOR_ALL_USERS, addForAllUsers ? 1 : 0);
600
601            if (callType == MISSED_TYPE) {
602                values.put(IS_READ, Integer.valueOf(is_read ? 1 : 0));
603            }
604
605            if ((ci != null) && (ci.contactIdOrZero > 0)) {
606                // Update usage information for the number associated with the contact ID.
607                // We need to use both the number and the ID for obtaining a data ID since other
608                // contacts may have the same number.
609
610                final Cursor cursor;
611
612                // We should prefer normalized one (probably coming from
613                // Phone.NORMALIZED_NUMBER column) first. If it isn't available try others.
614                if (ci.normalizedNumber != null) {
615                    final String normalizedPhoneNumber = ci.normalizedNumber;
616                    cursor = resolver.query(Phone.CONTENT_URI,
617                            new String[] { Phone._ID },
618                            Phone.CONTACT_ID + " =? AND " + Phone.NORMALIZED_NUMBER + " =?",
619                            new String[] { String.valueOf(ci.contactIdOrZero),
620                                    normalizedPhoneNumber},
621                            null);
622                } else {
623                    final String phoneNumber = ci.phoneNumber != null ? ci.phoneNumber : number;
624                    cursor = resolver.query(
625                            Uri.withAppendedPath(Callable.CONTENT_FILTER_URI,
626                                    Uri.encode(phoneNumber)),
627                            new String[] { Phone._ID },
628                            Phone.CONTACT_ID + " =?",
629                            new String[] { String.valueOf(ci.contactIdOrZero) },
630                            null);
631                }
632
633                if (cursor != null) {
634                    try {
635                        if (cursor.getCount() > 0 && cursor.moveToFirst()) {
636                            final String dataId = cursor.getString(0);
637                            updateDataUsageStatForData(resolver, dataId);
638                            if (duration >= MIN_DURATION_FOR_NORMALIZED_NUMBER_UPDATE_MS
639                                    && callType == Calls.OUTGOING_TYPE
640                                    && TextUtils.isEmpty(ci.normalizedNumber)) {
641                                updateNormalizedNumber(context, resolver, dataId, number);
642                            }
643                        }
644                    } finally {
645                        cursor.close();
646                    }
647                }
648            }
649
650            Uri result = null;
651
652            if (addForAllUsers) {
653                // Insert the entry for all currently running users, in order to trigger any
654                // ContentObservers currently set on the call log.
655                final UserManager userManager = (UserManager) context.getSystemService(
656                        Context.USER_SERVICE);
657                List<UserInfo> users = userManager.getUsers(true);
658                final int currentUserId = userManager.getUserHandle();
659                final int count = users.size();
660                for (int i = 0; i < count; i++) {
661                    final UserInfo user = users.get(i);
662                    final UserHandle userHandle = user.getUserHandle();
663                    if (userManager.isUserRunning(userHandle)
664                            && !userManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
665                                    userHandle)
666                            && !user.isManagedProfile()) {
667                        Uri uri = addEntryAndRemoveExpiredEntries(context,
668                                ContentProvider.maybeAddUserId(CONTENT_URI, user.id), values);
669                        if (user.id == currentUserId) {
670                            result = uri;
671                        }
672                    }
673                }
674            } else {
675                Uri uri = CONTENT_URI;
676                if (userToBeInsertedTo != null) {
677                    uri = ContentProvider
678                            .maybeAddUserId(CONTENT_URI, userToBeInsertedTo.getIdentifier());
679                }
680                result = addEntryAndRemoveExpiredEntries(context, uri, values);
681            }
682            return result;
683        }
684
685        /**
686         * Query the call log database for the last dialed number.
687         * @param context Used to get the content resolver.
688         * @return The last phone number dialed (outgoing) or an empty
689         * string if none exist yet.
690         */
691        public static String getLastOutgoingCall(Context context) {
692            final ContentResolver resolver = context.getContentResolver();
693            Cursor c = null;
694            try {
695                c = resolver.query(
696                    CONTENT_URI,
697                    new String[] {NUMBER},
698                    TYPE + " = " + OUTGOING_TYPE,
699                    null,
700                    DEFAULT_SORT_ORDER + " LIMIT 1");
701                if (c == null || !c.moveToFirst()) {
702                    return "";
703                }
704                return c.getString(0);
705            } finally {
706                if (c != null) c.close();
707            }
708        }
709
710        private static Uri addEntryAndRemoveExpiredEntries(Context context, Uri uri,
711                ContentValues values) {
712            final ContentResolver resolver = context.getContentResolver();
713            Uri result = resolver.insert(uri, values);
714            resolver.delete(uri, "_id IN " +
715                    "(SELECT _id FROM calls ORDER BY " + DEFAULT_SORT_ORDER
716                    + " LIMIT -1 OFFSET 500)", null);
717            return result;
718        }
719
720        private static void updateDataUsageStatForData(ContentResolver resolver, String dataId) {
721            final Uri feedbackUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
722                    .appendPath(dataId)
723                    .appendQueryParameter(DataUsageFeedback.USAGE_TYPE,
724                                DataUsageFeedback.USAGE_TYPE_CALL)
725                    .build();
726            resolver.update(feedbackUri, new ContentValues(), null, null);
727        }
728
729        /*
730         * Update the normalized phone number for the given dataId in the ContactsProvider, based
731         * on the user's current country.
732         */
733        private static void updateNormalizedNumber(Context context, ContentResolver resolver,
734                String dataId, String number) {
735            if (TextUtils.isEmpty(number) || TextUtils.isEmpty(dataId)) {
736                return;
737            }
738            final String countryIso = getCurrentCountryIso(context);
739            if (TextUtils.isEmpty(countryIso)) {
740                return;
741            }
742            final String normalizedNumber = PhoneNumberUtils.formatNumberToE164(number,
743                    getCurrentCountryIso(context));
744            if (TextUtils.isEmpty(normalizedNumber)) {
745                return;
746            }
747            final ContentValues values = new ContentValues();
748            values.put(Phone.NORMALIZED_NUMBER, normalizedNumber);
749            resolver.update(Data.CONTENT_URI, values, Data._ID + "=?", new String[] {dataId});
750        }
751
752        private static String getCurrentCountryIso(Context context) {
753            String countryIso = null;
754            final CountryDetector detector = (CountryDetector) context.getSystemService(
755                    Context.COUNTRY_DETECTOR);
756            if (detector != null) {
757                final Country country = detector.detectCountry();
758                if (country != null) {
759                    countryIso = country.getCountryIso();
760                }
761            }
762            return countryIso;
763        }
764    }
765}
766