blob: 5dca67ff6f989504d9e45cf8d52dfc2bdcb1fe9b [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 /**
Bai Tao224744c2010-08-31 09:59:13 +0800140 * The ISO 3166-1 two letters country code of the country where the
141 * user received or made the call.
142 * <P>
143 * Type: TEXT
144 * </P>
145 *
146 * @hide
147 */
148 public static final String COUNTRY_ISO = "countryiso";
149
150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 * The date the call occured, in milliseconds since the epoch
152 * <P>Type: INTEGER (long)</P>
153 */
154 public static final String DATE = "date";
155
156 /**
157 * The duration of the call in seconds
158 * <P>Type: INTEGER (long)</P>
159 */
160 public static final String DURATION = "duration";
161
162 /**
163 * Whether or not the call has been acknowledged
164 * <P>Type: INTEGER (boolean)</P>
165 */
166 public static final String NEW = "new";
167
168 /**
169 * The cached name associated with the phone number, if it exists.
170 * This value is not guaranteed to be current, if the contact information
171 * associated with this number has changed.
172 * <P>Type: TEXT</P>
173 */
174 public static final String CACHED_NAME = "name";
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 /**
177 * The cached number type (Home, Work, etc) associated with the
178 * phone number, if it exists.
179 * This value is not guaranteed to be current, if the contact information
180 * associated with this number has changed.
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800181 * <P>Type: INTEGER</P>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 */
183 public static final String CACHED_NUMBER_TYPE = "numbertype";
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 /**
186 * The cached number label, for a custom number type, associated with the
187 * phone number, if it exists.
188 * This value is not guaranteed to be current, if the contact information
189 * associated with this number has changed.
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800190 * <P>Type: TEXT</P>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 */
192 public static final String CACHED_NUMBER_LABEL = "numberlabel";
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 /**
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100195 * URI of the voicemail entry. Populated only for {@link #VOICEMAIL_TYPE}.
196 * <P>Type: TEXT</P>
197 * @hide
198 */
199 public static final String VOICEMAIL_URI = "voicemail_uri";
200
201 /**
Flavio Lerda651212d2011-07-21 16:53:59 +0100202 * Whether this item has been read or otherwise consumed by the user.
203 * <p>
204 * Unlike the {@link #NEW} field, which requires the user to have acknowledged the
205 * existence of the entry, this implies the user has interacted with the entry.
206 * <P>Type: INTEGER (boolean)</P>
Flavio Lerda651212d2011-07-21 16:53:59 +0100207 */
208 public static final String IS_READ = "is_read";
209
210 /**
Flavio Lerda270f9302011-08-09 12:01:13 +0100211 * A geocoded location for the number associated with this call.
212 * <p>
213 * The string represents a city, state, or country associated with the number.
214 * <P>Type: TEXT</P>
215 * @hide
216 */
217 public static final String GEOCODED_LOCATION = "geocoded_location";
218
219 /**
Flavio Lerda2d538d42011-08-16 08:29:06 +0100220 * The cached URI to look up the contact associated with the 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: TEXT</P>
224 * @hide
225 */
226 public static final String CACHED_LOOKUP_URI = "lookup_uri";
227
228 /**
229 * The cached phone number of the contact which matches this entry, 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 * @hide
234 */
235 public static final String CACHED_MATCHED_NUMBER = "matched_number";
236
237 /**
238 * The cached normalized version of the phone number, if it exists.
239 * This value is not guaranteed to be current, if the contact information
240 * associated with this number has changed.
241 * <P>Type: TEXT</P>
242 * @hide
243 */
244 public static final String CACHED_NORMALIZED_NUMBER = "normalized_number";
245
246 /**
247 * The cached photo id of the picture associated with the phone number, if it exists.
248 * This value is not guaranteed to be current, if the contact information
249 * associated with this number has changed.
250 * <P>Type: INTEGER (long)</P>
251 * @hide
252 */
253 public static final String CACHED_PHOTO_ID = "photo_id";
254
255 /**
Flavio Lerda0fce15b2011-10-01 18:55:33 +0100256 * The cached formatted phone number.
257 * This value is not guaranteed to be present.
258 * <P>Type: TEXT</P>
259 * @hide
260 */
261 public static final String CACHED_FORMATTED_NUMBER = "formatted_number";
262
263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * Adds a call to the call log.
265 *
266 * @param ci the CallerInfo object to get the target contact from. Can be null
267 * if the contact is unknown.
268 * @param context the context used to get the ContentResolver
269 * @param number the phone number to be added to the calls db
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800270 * @param presentation the number presenting rules set by the network for
The Android Open Source Project10592532009-03-18 17:39:46 -0700271 * "allowed", "payphone", "restricted" or "unknown"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 * @param callType enumerated values for "incoming", "outgoing", or "missed"
273 * @param start time stamp for the call in milliseconds
274 * @param duration call duration in seconds
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800275 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 * {@hide}
277 */
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800278 public static Uri addCall(CallerInfo ci, Context context, String number,
The Android Open Source Project10592532009-03-18 17:39:46 -0700279 int presentation, int callType, long start, int duration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 final ContentResolver resolver = context.getContentResolver();
281
Pauyl l Bermane1dc2ba2009-07-10 18:27:36 -0400282 // If this is a private number then set the number to Private, otherwise check
283 // if the number field is empty and set the number to Unavailable
Wink Savillea639b312012-07-10 12:37:54 -0700284 if (presentation == PhoneConstants.PRESENTATION_RESTRICTED) {
Wink Savilledda53912009-05-28 17:32:34 -0700285 number = CallerInfo.PRIVATE_NUMBER;
Pauyl l Bermane1dc2ba2009-07-10 18:27:36 -0400286 if (ci != null) ci.name = "";
Wink Savillea639b312012-07-10 12:37:54 -0700287 } else if (presentation == PhoneConstants.PRESENTATION_PAYPHONE) {
Wink Savilledda53912009-05-28 17:32:34 -0700288 number = CallerInfo.PAYPHONE_NUMBER;
Pauyl l Bermane1dc2ba2009-07-10 18:27:36 -0400289 if (ci != null) ci.name = "";
290 } else if (TextUtils.isEmpty(number)
Wink Savillea639b312012-07-10 12:37:54 -0700291 || presentation == PhoneConstants.PRESENTATION_UNKNOWN) {
Wink Savilledda53912009-05-28 17:32:34 -0700292 number = CallerInfo.UNKNOWN_NUMBER;
Pauyl l Bermane1dc2ba2009-07-10 18:27:36 -0400293 if (ci != null) ci.name = "";
Wink Savilledda53912009-05-28 17:32:34 -0700294 }
Pauyl l Bermane1dc2ba2009-07-10 18:27:36 -0400295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 ContentValues values = new ContentValues(5);
297
298 values.put(NUMBER, number);
299 values.put(TYPE, Integer.valueOf(callType));
300 values.put(DATE, Long.valueOf(start));
301 values.put(DURATION, Long.valueOf(duration));
302 values.put(NEW, Integer.valueOf(1));
Debashish Chatterjee4efaf4b2011-08-11 16:47:16 +0100303 if (callType == MISSED_TYPE) {
304 values.put(IS_READ, Integer.valueOf(0));
305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 if (ci != null) {
307 values.put(CACHED_NAME, ci.name);
308 values.put(CACHED_NUMBER_TYPE, ci.numberType);
309 values.put(CACHED_NUMBER_LABEL, ci.numberLabel);
310 }
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 if ((ci != null) && (ci.person_id > 0)) {
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700313 // Update usage information for the number associated with the contact ID.
314 // We need to use both the number and the ID for obtaining a data ID since other
315 // contacts may have the same number.
316
317 final Cursor cursor;
318
319 // We should prefer normalized one (probably coming from
320 // Phone.NORMALIZED_NUMBER column) first. If it isn't available try others.
321 if (ci.normalizedNumber != null) {
322 final String normalizedPhoneNumber = ci.normalizedNumber;
323 cursor = resolver.query(Phone.CONTENT_URI,
324 new String[] { Phone._ID },
325 Phone.CONTACT_ID + " =? AND " + Phone.NORMALIZED_NUMBER + " =?",
326 new String[] { String.valueOf(ci.person_id), normalizedPhoneNumber},
327 null);
328 } else {
329 final String phoneNumber = ci.phoneNumber != null ? ci.phoneNumber : number;
Daisuke Miyakawae23362a2012-05-06 16:54:25 -0700330 cursor = resolver.query(
331 Uri.withAppendedPath(Callable.CONTENT_FILTER_URI,
332 Uri.encode(phoneNumber)),
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700333 new String[] { Phone._ID },
Daisuke Miyakawae23362a2012-05-06 16:54:25 -0700334 Phone.CONTACT_ID + " =?",
335 new String[] { String.valueOf(ci.person_id) },
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700336 null);
337 }
338
339 if (cursor != null) {
340 try {
341 if (cursor.getCount() > 0 && cursor.moveToFirst()) {
342 final Uri feedbackUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
343 .appendPath(cursor.getString(0))
344 .appendQueryParameter(DataUsageFeedback.USAGE_TYPE,
345 DataUsageFeedback.USAGE_TYPE_CALL)
346 .build();
347 resolver.update(feedbackUri, new ContentValues(), null, null);
348 }
349 } finally {
350 cursor.close();
351 }
352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 Uri result = resolver.insert(CONTENT_URI, values);
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 removeExpiredEntries(context);
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 return result;
360 }
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800361
362 /**
363 * Query the call log database for the last dialed number.
364 * @param context Used to get the content resolver.
365 * @return The last phone number dialed (outgoing) or an empty
366 * string if none exist yet.
367 */
368 public static String getLastOutgoingCall(Context context) {
369 final ContentResolver resolver = context.getContentResolver();
370 Cursor c = null;
371 try {
372 c = resolver.query(
373 CONTENT_URI,
374 new String[] {NUMBER},
375 TYPE + " = " + OUTGOING_TYPE,
376 null,
377 DEFAULT_SORT_ORDER + " LIMIT 1");
378 if (c == null || !c.moveToFirst()) {
379 return "";
380 }
381 return c.getString(0);
382 } finally {
383 if (c != null) c.close();
384 }
385 }
386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 private static void removeExpiredEntries(Context context) {
388 final ContentResolver resolver = context.getContentResolver();
389 resolver.delete(CONTENT_URI, "_id IN " +
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800390 "(SELECT _id FROM calls ORDER BY " + DEFAULT_SORT_ORDER
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 + " LIMIT -1 OFFSET 500)", null);
392 }
393 }
394}