blob: 9d52c8385725d918ffad6fc300e2ffcda92c7cad [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Debashish Chatterjee29c15752011-07-04 09:48:13 +010017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018package android.provider;
19
20import android.content.ContentResolver;
21import android.content.ContentValues;
22import android.content.Context;
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -080023import android.database.Cursor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.net.Uri;
Daisuke Miyakawae23362a2012-05-06 16:54:25 -070025import android.provider.ContactsContract.CommonDataKinds.Callable;
Daisuke Miyakawaf4685912011-06-25 12:31:13 -070026import android.provider.ContactsContract.CommonDataKinds.Phone;
27import android.provider.ContactsContract.DataUsageFeedback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Chiao Cheng509e1f12012-08-01 15:40:55 -070030import com.android.internal.telephony.CallerInfo;
31import com.android.internal.telephony.PhoneConstants;
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033/**
34 * The CallLog provider contains information about placed and received calls.
35 */
36public class CallLog {
37 public static final String AUTHORITY = "call_log";
38
39 /**
40 * The content:// style URL for this provider
41 */
42 public static final Uri CONTENT_URI =
43 Uri.parse("content://" + AUTHORITY);
44
45 /**
46 * Contains the recent calls.
47 */
48 public static class Calls implements BaseColumns {
49 /**
50 * The content:// style URL for this table
51 */
52 public static final Uri CONTENT_URI =
53 Uri.parse("content://call_log/calls");
54
55 /**
56 * The content:// style URL for filtering this table on phone numbers
57 */
58 public static final Uri CONTENT_FILTER_URI =
59 Uri.parse("content://call_log/calls/filter");
60
61 /**
Chiao Cheng509e1f12012-08-01 15:40:55 -070062 * Query parameter used to limit the number of call logs returned.
63 * <p>
64 * TYPE: integer
65 */
66 public static final String LIMIT_PARAM_KEY = "limit";
67
68 /**
69 * Query parameter used to specify the starting record to return.
70 * <p>
71 * TYPE: integer
72 */
73 public static final String OFFSET_PARAM_KEY = "offset";
74
75 /**
Flavio Lerda9ef78f02011-06-29 10:51:59 +010076 * An optional URI parameter which instructs the provider to allow the operation to be
77 * applied to voicemail records as well.
78 * <p>
79 * TYPE: Boolean
80 * <p>
81 * Using this parameter with a value of {@code true} will result in a security error if the
82 * calling package does not have appropriate permissions to access voicemails.
83 *
84 * @hide
85 */
86 public static final String ALLOW_VOICEMAILS_PARAM_KEY = "allow_voicemails";
87
88 /**
89 * Content uri with {@link #ALLOW_VOICEMAILS_PARAM_KEY} set. This can directly be used to
90 * access call log entries that includes voicemail records.
91 *
92 * @hide
93 */
94 public static final Uri CONTENT_URI_WITH_VOICEMAIL = CONTENT_URI.buildUpon()
95 .appendQueryParameter(ALLOW_VOICEMAILS_PARAM_KEY, "true")
96 .build();
97
98 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 * The default sort order for this table
100 */
101 public static final String DEFAULT_SORT_ORDER = "date DESC";
102
103 /**
104 * The MIME type of {@link #CONTENT_URI} and {@link #CONTENT_FILTER_URI}
105 * providing a directory of calls.
106 */
107 public static final String CONTENT_TYPE = "vnd.android.cursor.dir/calls";
108
109 /**
110 * The MIME type of a {@link #CONTENT_URI} sub-directory of a single
111 * call.
112 */
113 public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/calls";
114
115 /**
Yusuf T. Mobilef6737d32009-05-15 10:56:36 -0700116 * The type of the call (incoming, outgoing or missed).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * <P>Type: INTEGER (int)</P>
118 */
119 public static final String TYPE = "type";
120
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100121 /** Call log type for incoming calls. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 public static final int INCOMING_TYPE = 1;
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100123 /** Call log type for outgoing calls. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public static final int OUTGOING_TYPE = 2;
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100125 /** Call log type for missed calls. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 public static final int MISSED_TYPE = 3;
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100127 /**
128 * Call log type for voicemails.
129 * @hide
130 */
131 public static final int VOICEMAIL_TYPE = 4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 /**
134 * The phone number as the user entered it.
135 * <P>Type: TEXT</P>
136 */
137 public static final String NUMBER = "number";
138
139 /**
Jay Shraunerd88eb712013-06-10 10:10:11 -0700140 * The number presenting rules set by the network for "allowed",
141 * "payphone", "restricted" or "unknown".
142 * <P>Type: INTEGER</P>
143 */
144 public static final String NUMBER_PRESENTATION = "presentation";
145
146 /** Number is allowed to display for caller id. */
147 public static final int PRESENTATION_ALLOWED = 1;
148 /** Number is blocked by user. */
149 public static final int PRESENTATION_RESTRICTED = 2;
150 /** Number is not specified or unknown by network. */
151 public static final int PRESENTATION_UNKNOWN = 3;
152 /** Number is a pay phone. */
153 public static final int PRESENTATION_PAYPHONE = 4;
154
155 /**
Bai Tao224744c2010-08-31 09:59:13 +0800156 * The ISO 3166-1 two letters country code of the country where the
157 * user received or made the call.
158 * <P>
159 * Type: TEXT
160 * </P>
161 *
162 * @hide
163 */
164 public static final String COUNTRY_ISO = "countryiso";
165
166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * The date the call occured, in milliseconds since the epoch
168 * <P>Type: INTEGER (long)</P>
169 */
170 public static final String DATE = "date";
171
172 /**
173 * The duration of the call in seconds
174 * <P>Type: INTEGER (long)</P>
175 */
176 public static final String DURATION = "duration";
177
178 /**
179 * Whether or not the call has been acknowledged
180 * <P>Type: INTEGER (boolean)</P>
181 */
182 public static final String NEW = "new";
183
184 /**
185 * The cached name associated with the phone number, if it exists.
186 * This value is not guaranteed to be current, if the contact information
187 * associated with this number has changed.
188 * <P>Type: TEXT</P>
189 */
190 public static final String CACHED_NAME = "name";
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 /**
193 * The cached number type (Home, Work, etc) associated with the
194 * phone number, if it exists.
195 * This value is not guaranteed to be current, if the contact information
196 * associated with this number has changed.
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800197 * <P>Type: INTEGER</P>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 */
199 public static final String CACHED_NUMBER_TYPE = "numbertype";
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 /**
202 * The cached number label, for a custom number type, associated with the
203 * phone number, if it exists.
204 * This value is not guaranteed to be current, if the contact information
205 * associated with this number has changed.
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800206 * <P>Type: TEXT</P>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 */
208 public static final String CACHED_NUMBER_LABEL = "numberlabel";
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 /**
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100211 * URI of the voicemail entry. Populated only for {@link #VOICEMAIL_TYPE}.
212 * <P>Type: TEXT</P>
213 * @hide
214 */
215 public static final String VOICEMAIL_URI = "voicemail_uri";
216
217 /**
Flavio Lerda651212d2011-07-21 16:53:59 +0100218 * Whether this item has been read or otherwise consumed by the user.
219 * <p>
220 * Unlike the {@link #NEW} field, which requires the user to have acknowledged the
221 * existence of the entry, this implies the user has interacted with the entry.
222 * <P>Type: INTEGER (boolean)</P>
Flavio Lerda651212d2011-07-21 16:53:59 +0100223 */
224 public static final String IS_READ = "is_read";
225
226 /**
Flavio Lerda270f9302011-08-09 12:01:13 +0100227 * A geocoded location for the number associated with this call.
228 * <p>
229 * The string represents a city, state, or country associated with the number.
230 * <P>Type: TEXT</P>
231 * @hide
232 */
233 public static final String GEOCODED_LOCATION = "geocoded_location";
234
235 /**
Flavio Lerda2d538d42011-08-16 08:29:06 +0100236 * The cached URI to look up the contact associated with the phone number, if it exists.
237 * This value is not guaranteed to be current, if the contact information
238 * associated with this number has changed.
239 * <P>Type: TEXT</P>
240 * @hide
241 */
242 public static final String CACHED_LOOKUP_URI = "lookup_uri";
243
244 /**
245 * The cached phone number of the contact which matches this entry, if it exists.
246 * This value is not guaranteed to be current, if the contact information
247 * associated with this number has changed.
248 * <P>Type: TEXT</P>
249 * @hide
250 */
251 public static final String CACHED_MATCHED_NUMBER = "matched_number";
252
253 /**
254 * The cached normalized version of the phone number, if it exists.
255 * This value is not guaranteed to be current, if the contact information
256 * associated with this number has changed.
257 * <P>Type: TEXT</P>
258 * @hide
259 */
260 public static final String CACHED_NORMALIZED_NUMBER = "normalized_number";
261
262 /**
263 * The cached photo id of the picture associated with the phone number, if it exists.
264 * This value is not guaranteed to be current, if the contact information
265 * associated with this number has changed.
266 * <P>Type: INTEGER (long)</P>
267 * @hide
268 */
269 public static final String CACHED_PHOTO_ID = "photo_id";
270
271 /**
Flavio Lerda0fce15b2011-10-01 18:55:33 +0100272 * The cached formatted phone number.
273 * This value is not guaranteed to be present.
274 * <P>Type: TEXT</P>
275 * @hide
276 */
277 public static final String CACHED_FORMATTED_NUMBER = "formatted_number";
278
279 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 * Adds a call to the call log.
281 *
282 * @param ci the CallerInfo object to get the target contact from. Can be null
283 * if the contact is unknown.
284 * @param context the context used to get the ContentResolver
285 * @param number the phone number to be added to the calls db
Jay Shraunerd88eb712013-06-10 10:10:11 -0700286 * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which
287 * is set by the network and denotes the number presenting rules for
The Android Open Source Project10592532009-03-18 17:39:46 -0700288 * "allowed", "payphone", "restricted" or "unknown"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 * @param callType enumerated values for "incoming", "outgoing", or "missed"
290 * @param start time stamp for the call in milliseconds
291 * @param duration call duration in seconds
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800292 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 * {@hide}
294 */
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800295 public static Uri addCall(CallerInfo ci, Context context, String number,
The Android Open Source Project10592532009-03-18 17:39:46 -0700296 int presentation, int callType, long start, int duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 final ContentResolver resolver = context.getContentResolver();
Jay Shraunerd88eb712013-06-10 10:10:11 -0700298 int numberPresentation = PRESENTATION_ALLOWED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
Jay Shraunerd88eb712013-06-10 10:10:11 -0700300 // Remap network specified number presentation types
301 // PhoneConstants.PRESENTATION_xxx to calllog number presentation types
302 // Calls.PRESENTATION_xxx, in order to insulate the persistent calllog
303 // from any future radio changes.
304 // If the number field is empty set the presentation type to Unknown.
Wink Savillea639b312012-07-10 12:37:54 -0700305 if (presentation == PhoneConstants.PRESENTATION_RESTRICTED) {
Jay Shraunerd88eb712013-06-10 10:10:11 -0700306 numberPresentation = PRESENTATION_RESTRICTED;
Wink Savillea639b312012-07-10 12:37:54 -0700307 } else if (presentation == PhoneConstants.PRESENTATION_PAYPHONE) {
Jay Shraunerd88eb712013-06-10 10:10:11 -0700308 numberPresentation = PRESENTATION_PAYPHONE;
Pauyl l Bermane1dc2ba2009-07-10 18:27:36 -0400309 } else if (TextUtils.isEmpty(number)
Wink Savillea639b312012-07-10 12:37:54 -0700310 || presentation == PhoneConstants.PRESENTATION_UNKNOWN) {
Jay Shraunerd88eb712013-06-10 10:10:11 -0700311 numberPresentation = PRESENTATION_UNKNOWN;
312 }
313 if (numberPresentation != PRESENTATION_ALLOWED) {
314 number = "";
315 if (ci != null) {
316 ci.name = "";
317 }
Wink Savilledda53912009-05-28 17:32:34 -0700318 }
Pauyl l Bermane1dc2ba2009-07-10 18:27:36 -0400319
Jay Shraunerd88eb712013-06-10 10:10:11 -0700320 ContentValues values = new ContentValues(6);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321
322 values.put(NUMBER, number);
Jay Shraunerd88eb712013-06-10 10:10:11 -0700323 values.put(NUMBER_PRESENTATION, Integer.valueOf(numberPresentation));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 values.put(TYPE, Integer.valueOf(callType));
325 values.put(DATE, Long.valueOf(start));
326 values.put(DURATION, Long.valueOf(duration));
327 values.put(NEW, Integer.valueOf(1));
Debashish Chatterjee4efaf4b2011-08-11 16:47:16 +0100328 if (callType == MISSED_TYPE) {
329 values.put(IS_READ, Integer.valueOf(0));
330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 if (ci != null) {
332 values.put(CACHED_NAME, ci.name);
333 values.put(CACHED_NUMBER_TYPE, ci.numberType);
334 values.put(CACHED_NUMBER_LABEL, ci.numberLabel);
335 }
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 if ((ci != null) && (ci.person_id > 0)) {
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700338 // Update usage information for the number associated with the contact ID.
339 // We need to use both the number and the ID for obtaining a data ID since other
340 // contacts may have the same number.
341
342 final Cursor cursor;
343
344 // We should prefer normalized one (probably coming from
345 // Phone.NORMALIZED_NUMBER column) first. If it isn't available try others.
346 if (ci.normalizedNumber != null) {
347 final String normalizedPhoneNumber = ci.normalizedNumber;
348 cursor = resolver.query(Phone.CONTENT_URI,
349 new String[] { Phone._ID },
350 Phone.CONTACT_ID + " =? AND " + Phone.NORMALIZED_NUMBER + " =?",
351 new String[] { String.valueOf(ci.person_id), normalizedPhoneNumber},
352 null);
353 } else {
354 final String phoneNumber = ci.phoneNumber != null ? ci.phoneNumber : number;
Daisuke Miyakawae23362a2012-05-06 16:54:25 -0700355 cursor = resolver.query(
356 Uri.withAppendedPath(Callable.CONTENT_FILTER_URI,
357 Uri.encode(phoneNumber)),
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700358 new String[] { Phone._ID },
Daisuke Miyakawae23362a2012-05-06 16:54:25 -0700359 Phone.CONTACT_ID + " =?",
360 new String[] { String.valueOf(ci.person_id) },
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700361 null);
362 }
363
364 if (cursor != null) {
365 try {
366 if (cursor.getCount() > 0 && cursor.moveToFirst()) {
367 final Uri feedbackUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
368 .appendPath(cursor.getString(0))
369 .appendQueryParameter(DataUsageFeedback.USAGE_TYPE,
370 DataUsageFeedback.USAGE_TYPE_CALL)
371 .build();
372 resolver.update(feedbackUri, new ContentValues(), null, null);
373 }
374 } finally {
375 cursor.close();
376 }
377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 Uri result = resolver.insert(CONTENT_URI, values);
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 removeExpiredEntries(context);
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 return result;
385 }
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800386
387 /**
388 * Query the call log database for the last dialed number.
389 * @param context Used to get the content resolver.
390 * @return The last phone number dialed (outgoing) or an empty
391 * string if none exist yet.
392 */
393 public static String getLastOutgoingCall(Context context) {
394 final ContentResolver resolver = context.getContentResolver();
395 Cursor c = null;
396 try {
397 c = resolver.query(
398 CONTENT_URI,
399 new String[] {NUMBER},
400 TYPE + " = " + OUTGOING_TYPE,
401 null,
402 DEFAULT_SORT_ORDER + " LIMIT 1");
403 if (c == null || !c.moveToFirst()) {
404 return "";
405 }
406 return c.getString(0);
407 } finally {
408 if (c != null) c.close();
409 }
410 }
411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 private static void removeExpiredEntries(Context context) {
413 final ContentResolver resolver = context.getContentResolver();
414 resolver.delete(CONTENT_URI, "_id IN " +
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800415 "(SELECT _id FROM calls ORDER BY " + DEFAULT_SORT_ORDER
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 + " LIMIT -1 OFFSET 500)", null);
417 }
418 }
419}