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