blob: c8d0fd50c2d3bd19645731bec2490099cae92f78 [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
Yorke Lee158c5e42014-07-16 15:31:25 -070020import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.ContentResolver;
22import android.content.ContentValues;
23import android.content.Context;
Yorke Lee1854a602014-07-09 14:11:46 -070024import android.content.Intent;
Yorke Lee158c5e42014-07-16 15:31:25 -070025import android.content.pm.UserInfo;
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -080026import android.database.Cursor;
Yorke Lee3dbc1182014-08-21 09:37:22 -070027import android.location.Country;
28import android.location.CountryDetector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.net.Uri;
Yorke Lee158c5e42014-07-16 15:31:25 -070030import android.os.UserHandle;
31import android.os.UserManager;
Daisuke Miyakawae23362a2012-05-06 16:54:25 -070032import android.provider.ContactsContract.CommonDataKinds.Callable;
Daisuke Miyakawaf4685912011-06-25 12:31:13 -070033import android.provider.ContactsContract.CommonDataKinds.Phone;
Yorke Lee3dbc1182014-08-21 09:37:22 -070034import android.provider.ContactsContract.Data;
Daisuke Miyakawaf4685912011-06-25 12:31:13 -070035import android.provider.ContactsContract.DataUsageFeedback;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070036import android.telecom.PhoneAccountHandle;
Yorke Lee3dbc1182014-08-21 09:37:22 -070037import android.telephony.PhoneNumberUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Chiao Cheng509e1f12012-08-01 15:40:55 -070040import com.android.internal.telephony.CallerInfo;
41import com.android.internal.telephony.PhoneConstants;
42
Yorke Lee158c5e42014-07-16 15:31:25 -070043import java.util.List;
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045/**
46 * The CallLog provider contains information about placed and received calls.
47 */
48public class CallLog {
49 public static final String AUTHORITY = "call_log";
50
51 /**
52 * The content:// style URL for this provider
53 */
54 public static final Uri CONTENT_URI =
55 Uri.parse("content://" + AUTHORITY);
56
57 /**
58 * Contains the recent calls.
59 */
60 public static class Calls implements BaseColumns {
61 /**
62 * The content:// style URL for this table
63 */
64 public static final Uri CONTENT_URI =
65 Uri.parse("content://call_log/calls");
66
67 /**
68 * The content:// style URL for filtering this table on phone numbers
69 */
70 public static final Uri CONTENT_FILTER_URI =
71 Uri.parse("content://call_log/calls/filter");
72
73 /**
Chiao Cheng509e1f12012-08-01 15:40:55 -070074 * Query parameter used to limit the number of call logs returned.
75 * <p>
76 * TYPE: integer
77 */
78 public static final String LIMIT_PARAM_KEY = "limit";
79
80 /**
81 * Query parameter used to specify the starting record to return.
82 * <p>
83 * TYPE: integer
84 */
85 public static final String OFFSET_PARAM_KEY = "offset";
86
87 /**
Flavio Lerda9ef78f02011-06-29 10:51:59 +010088 * An optional URI parameter which instructs the provider to allow the operation to be
89 * applied to voicemail records as well.
90 * <p>
91 * TYPE: Boolean
92 * <p>
93 * Using this parameter with a value of {@code true} will result in a security error if the
94 * calling package does not have appropriate permissions to access voicemails.
95 *
96 * @hide
97 */
98 public static final String ALLOW_VOICEMAILS_PARAM_KEY = "allow_voicemails";
99
100 /**
Yorke Lee1854a602014-07-09 14:11:46 -0700101 * An optional extra used with {@link #CONTENT_TYPE Calls.CONTENT_TYPE} and
102 * {@link Intent#ACTION_VIEW} to specify that the presented list of calls should be
103 * filtered for a particular call type.
104 *
105 * Applications implementing a call log UI should check for this extra, and display a
106 * filtered list of calls based on the specified call type. If not applicable within the
107 * application's UI, it should be silently ignored.
108 *
109 * <p>
110 * The following example brings up the call log, showing only missed calls.
111 * <pre>
112 * Intent intent = new Intent(Intent.ACTION_VIEW);
113 * intent.setType(CallLog.Calls.CONTENT_TYPE);
114 * intent.putExtra(CallLog.Calls.EXTRA_CALL_TYPE_FILTER, CallLog.Calls.MISSED_TYPE);
115 * startActivity(intent);
116 * </pre>
117 * </p>
118 */
Yorke Lee78814622014-09-04 16:07:50 -0700119 public static final String EXTRA_CALL_TYPE_FILTER =
120 "android.provider.extra.CALL_TYPE_FILTER";
Yorke Lee1854a602014-07-09 14:11:46 -0700121
122 /**
Yorke Lee0a173932013-12-12 17:29:03 -0800123 * Content uri used to access call log entries, including voicemail records. You must have
Yorke Lee624d0d62014-07-23 15:23:47 -0700124 * the READ_CALL_LOG and WRITE_CALL_LOG permissions to read and write to the call log, as
125 * well as READ_VOICEMAIL and WRITE_VOICEMAIL permissions to read and write voicemails.
Flavio Lerda9ef78f02011-06-29 10:51:59 +0100126 */
127 public static final Uri CONTENT_URI_WITH_VOICEMAIL = CONTENT_URI.buildUpon()
128 .appendQueryParameter(ALLOW_VOICEMAILS_PARAM_KEY, "true")
129 .build();
130
131 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * The default sort order for this table
133 */
134 public static final String DEFAULT_SORT_ORDER = "date DESC";
135
136 /**
137 * The MIME type of {@link #CONTENT_URI} and {@link #CONTENT_FILTER_URI}
138 * providing a directory of calls.
139 */
140 public static final String CONTENT_TYPE = "vnd.android.cursor.dir/calls";
141
142 /**
143 * The MIME type of a {@link #CONTENT_URI} sub-directory of a single
144 * call.
145 */
146 public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/calls";
147
148 /**
Yusuf T. Mobilef6737d32009-05-15 10:56:36 -0700149 * The type of the call (incoming, outgoing or missed).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 * <P>Type: INTEGER (int)</P>
151 */
152 public static final String TYPE = "type";
153
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100154 /** Call log type for incoming calls. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 public static final int INCOMING_TYPE = 1;
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100156 /** Call log type for outgoing calls. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 public static final int OUTGOING_TYPE = 2;
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100158 /** Call log type for missed calls. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public static final int MISSED_TYPE = 3;
Yorke Leea5f6a932013-11-22 18:17:23 -0800160 /** Call log type for voicemails. */
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100161 public static final int VOICEMAIL_TYPE = 4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
163 /**
Tyler Gunne5daf912014-07-08 15:22:55 -0700164 * Bit-mask describing features of the call (e.g. video).
165 *
166 * <P>Type: INTEGER (int)</P>
167 */
168 public static final String FEATURES = "features";
169
Tyler Gunne5daf912014-07-08 15:22:55 -0700170 /** Call had video. */
171 public static final int FEATURES_VIDEO = 0x1;
172
173 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * The phone number as the user entered it.
175 * <P>Type: TEXT</P>
176 */
177 public static final String NUMBER = "number";
178
179 /**
Yorke Lee49e1cf92013-09-10 14:39:24 -0700180 * The number presenting rules set by the network.
181 *
182 * <p>
183 * Allowed values:
184 * <ul>
185 * <li>{@link #PRESENTATION_ALLOWED}</li>
186 * <li>{@link #PRESENTATION_RESTRICTED}</li>
187 * <li>{@link #PRESENTATION_UNKNOWN}</li>
188 * <li>{@link #PRESENTATION_PAYPHONE}</li>
189 * </ul>
190 * </p>
191 *
Jay Shraunerd88eb712013-06-10 10:10:11 -0700192 * <P>Type: INTEGER</P>
193 */
194 public static final String NUMBER_PRESENTATION = "presentation";
195
196 /** Number is allowed to display for caller id. */
197 public static final int PRESENTATION_ALLOWED = 1;
198 /** Number is blocked by user. */
199 public static final int PRESENTATION_RESTRICTED = 2;
200 /** Number is not specified or unknown by network. */
201 public static final int PRESENTATION_UNKNOWN = 3;
202 /** Number is a pay phone. */
203 public static final int PRESENTATION_PAYPHONE = 4;
204
205 /**
Bai Tao224744c2010-08-31 09:59:13 +0800206 * The ISO 3166-1 two letters country code of the country where the
207 * user received or made the call.
208 * <P>
209 * Type: TEXT
210 * </P>
Bai Tao224744c2010-08-31 09:59:13 +0800211 */
212 public static final String COUNTRY_ISO = "countryiso";
213
214 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * The date the call occured, in milliseconds since the epoch
216 * <P>Type: INTEGER (long)</P>
217 */
218 public static final String DATE = "date";
219
220 /**
221 * The duration of the call in seconds
222 * <P>Type: INTEGER (long)</P>
223 */
224 public static final String DURATION = "duration";
225
226 /**
Tyler Gunne5daf912014-07-08 15:22:55 -0700227 * The data usage of the call in bytes.
228 * <P>Type: INTEGER (long)</P>
229 */
230 public static final String DATA_USAGE = "data_usage";
231
232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 * Whether or not the call has been acknowledged
234 * <P>Type: INTEGER (boolean)</P>
235 */
236 public static final String NEW = "new";
237
238 /**
239 * The cached name associated with the phone number, if it exists.
240 * This value is not guaranteed to be current, if the contact information
241 * associated with this number has changed.
242 * <P>Type: TEXT</P>
243 */
244 public static final String CACHED_NAME = "name";
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 /**
247 * The cached number type (Home, Work, etc) associated with the
248 * phone number, if it exists.
249 * This value is not guaranteed to be current, if the contact information
250 * associated with this number has changed.
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800251 * <P>Type: INTEGER</P>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 */
253 public static final String CACHED_NUMBER_TYPE = "numbertype";
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * The cached number label, for a custom number type, associated with the
257 * phone number, if it exists.
258 * This value is not guaranteed to be current, if the contact information
259 * associated with this number has changed.
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800260 * <P>Type: TEXT</P>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 */
262 public static final String CACHED_NUMBER_LABEL = "numberlabel";
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100265 * URI of the voicemail entry. Populated only for {@link #VOICEMAIL_TYPE}.
266 * <P>Type: TEXT</P>
Debashish Chatterjee412359f2011-06-06 17:35:59 +0100267 */
268 public static final String VOICEMAIL_URI = "voicemail_uri";
269
270 /**
Yorke Lee7a5e5722014-07-23 19:43:39 -0700271 * Transcription of the call or voicemail entry. This will only be populated for call log
272 * entries of type {@link #VOICEMAIL_TYPE} that have valid transcriptions.
273 */
274 public static final String TRANSCRIPTION = "transcription";
275
276 /**
Flavio Lerda651212d2011-07-21 16:53:59 +0100277 * Whether this item has been read or otherwise consumed by the user.
278 * <p>
279 * Unlike the {@link #NEW} field, which requires the user to have acknowledged the
280 * existence of the entry, this implies the user has interacted with the entry.
281 * <P>Type: INTEGER (boolean)</P>
Flavio Lerda651212d2011-07-21 16:53:59 +0100282 */
283 public static final String IS_READ = "is_read";
284
285 /**
Flavio Lerda270f9302011-08-09 12:01:13 +0100286 * A geocoded location for the number associated with this call.
287 * <p>
288 * The string represents a city, state, or country associated with the number.
289 * <P>Type: TEXT</P>
Flavio Lerda270f9302011-08-09 12:01:13 +0100290 */
291 public static final String GEOCODED_LOCATION = "geocoded_location";
292
293 /**
Flavio Lerda2d538d42011-08-16 08:29:06 +0100294 * The cached URI to look up the contact associated with the phone number, if it exists.
Yorke Leea5f6a932013-11-22 18:17:23 -0800295 * This value may not be current if the contact information associated with this number
296 * has changed.
Flavio Lerda2d538d42011-08-16 08:29:06 +0100297 * <P>Type: TEXT</P>
Flavio Lerda2d538d42011-08-16 08:29:06 +0100298 */
299 public static final String CACHED_LOOKUP_URI = "lookup_uri";
300
301 /**
302 * The cached phone number of the contact which matches this entry, if it exists.
Yorke Leea5f6a932013-11-22 18:17:23 -0800303 * This value may not be current if the contact information associated with this number
304 * has changed.
Flavio Lerda2d538d42011-08-16 08:29:06 +0100305 * <P>Type: TEXT</P>
Flavio Lerda2d538d42011-08-16 08:29:06 +0100306 */
307 public static final String CACHED_MATCHED_NUMBER = "matched_number";
308
309 /**
Yorke Leea5f6a932013-11-22 18:17:23 -0800310 * The cached normalized(E164) version of the phone number, if it exists.
311 * This value may not be current if the contact information associated with this number
312 * has changed.
Flavio Lerda2d538d42011-08-16 08:29:06 +0100313 * <P>Type: TEXT</P>
Flavio Lerda2d538d42011-08-16 08:29:06 +0100314 */
315 public static final String CACHED_NORMALIZED_NUMBER = "normalized_number";
316
317 /**
318 * The cached photo id of the picture associated with the phone number, if it exists.
Yorke Leea5f6a932013-11-22 18:17:23 -0800319 * This value may not be current if the contact information associated with this number
320 * has changed.
Flavio Lerda2d538d42011-08-16 08:29:06 +0100321 * <P>Type: INTEGER (long)</P>
Flavio Lerda2d538d42011-08-16 08:29:06 +0100322 */
323 public static final String CACHED_PHOTO_ID = "photo_id";
324
325 /**
Yorke Leea5f6a932013-11-22 18:17:23 -0800326 * The cached phone number, formatted with formatting rules based on the country the
327 * user was in when the call was made or received.
328 * This value is not guaranteed to be present, and may not be current if the contact
329 * information associated with this number
330 * has changed.
Flavio Lerda0fce15b2011-10-01 18:55:33 +0100331 * <P>Type: TEXT</P>
Flavio Lerda0fce15b2011-10-01 18:55:33 +0100332 */
333 public static final String CACHED_FORMATTED_NUMBER = "formatted_number";
334
Ihab Awad9c3f1882014-06-30 21:17:13 -0700335 // Note: PHONE_ACCOUNT_* constant values are "subscription_*" due to a historic naming
336 // that was encoded into call log databases.
Nancy Chen5ffbfcc2014-06-25 14:22:55 -0700337
338 /**
Ihab Awad9c3f1882014-06-30 21:17:13 -0700339 * The component name of the account in string form.
Nancy Chen5ffbfcc2014-06-25 14:22:55 -0700340 * <P>Type: TEXT</P>
341 */
Ihab Awad9c3f1882014-06-30 21:17:13 -0700342 public static final String PHONE_ACCOUNT_COMPONENT_NAME = "subscription_component_name";
343
344 /**
345 * The identifier of a account that is unique to a specified component.
346 * <P>Type: TEXT</P>
347 */
348 public static final String PHONE_ACCOUNT_ID = "subscription_id";
Nancy Chen5ffbfcc2014-06-25 14:22:55 -0700349
350 /**
Nancy Chen3a780922014-08-27 18:29:59 -0700351 * The identifier of a account that is unique to a specified component. Equivalent value
352 * to {@link #PHONE_ACCOUNT_ID}. For ContactsProvider internal use only.
353 * <P>Type: INTEGER</P>
354 *
355 * @hide
356 */
357 public static final String SUB_ID = "sub_id";
358
359 /**
Yorke Lee3dbc1182014-08-21 09:37:22 -0700360 * If a successful call is made that is longer than this duration, update the phone number
361 * in the ContactsProvider with the normalized version of the number, based on the user's
362 * current country code.
363 */
364 private static final int MIN_DURATION_FOR_NORMALIZED_NUMBER_UPDATE_MS = 1000 * 10;
365
366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 * Adds a call to the call log.
368 *
369 * @param ci the CallerInfo object to get the target contact from. Can be null
370 * if the contact is unknown.
371 * @param context the context used to get the ContentResolver
372 * @param number the phone number to be added to the calls db
Jay Shraunerd88eb712013-06-10 10:10:11 -0700373 * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which
374 * is set by the network and denotes the number presenting rules for
The Android Open Source Project10592532009-03-18 17:39:46 -0700375 * "allowed", "payphone", "restricted" or "unknown"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 * @param callType enumerated values for "incoming", "outgoing", or "missed"
Tyler Gunne5daf912014-07-08 15:22:55 -0700377 * @param features features of the call (e.g. Video).
Evan Charlton8c8a0622014-07-20 12:31:00 -0700378 * @param accountHandle The accountHandle object identifying the provider of the call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 * @param start time stamp for the call in milliseconds
380 * @param duration call duration in seconds
Tyler Gunne5daf912014-07-08 15:22:55 -0700381 * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for
382 * the call.
Yorke Lee158c5e42014-07-16 15:31:25 -0700383 * @result The URI of the call log entry belonging to the user that made or received this
384 * call.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 * {@hide}
386 */
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800387 public static Uri addCall(CallerInfo ci, Context context, String number,
Evan Charlton8c8a0622014-07-20 12:31:00 -0700388 int presentation, int callType, int features, PhoneAccountHandle accountHandle,
Evan Charlton6eb262c2014-07-19 18:18:19 -0700389 long start, int duration, Long dataUsage) {
Yorke Lee158c5e42014-07-16 15:31:25 -0700390 return addCall(ci, context, number, presentation, callType, features, accountHandle,
391 start, duration, dataUsage, false);
392 }
393
394 /**
395 * Adds a call to the call log.
396 *
397 * @param ci the CallerInfo object to get the target contact from. Can be null
398 * if the contact is unknown.
399 * @param context the context used to get the ContentResolver
400 * @param number the phone number to be added to the calls db
401 * @param presentation enum value from PhoneConstants.PRESENTATION_xxx, which
402 * is set by the network and denotes the number presenting rules for
403 * "allowed", "payphone", "restricted" or "unknown"
404 * @param callType enumerated values for "incoming", "outgoing", or "missed"
405 * @param features features of the call (e.g. Video).
406 * @param accountHandle The accountHandle object identifying the provider of the call
407 * @param start time stamp for the call in milliseconds
408 * @param duration call duration in seconds
409 * @param dataUsage data usage for the call in bytes, null if data usage was not tracked for
410 * the call.
411 * @param addForAllUsers If true, the call is added to the call log of all currently
412 * running users. The caller must have the MANAGE_USERS permission if this is true.
413 *
414 * @result The URI of the call log entry belonging to the user that made or received this
415 * call.
416 * {@hide}
417 */
418 public static Uri addCall(CallerInfo ci, Context context, String number,
419 int presentation, int callType, int features, PhoneAccountHandle accountHandle,
420 long start, int duration, Long dataUsage, boolean addForAllUsers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 final ContentResolver resolver = context.getContentResolver();
Jay Shraunerd88eb712013-06-10 10:10:11 -0700422 int numberPresentation = PRESENTATION_ALLOWED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423
Jay Shraunerd88eb712013-06-10 10:10:11 -0700424 // Remap network specified number presentation types
425 // PhoneConstants.PRESENTATION_xxx to calllog number presentation types
426 // Calls.PRESENTATION_xxx, in order to insulate the persistent calllog
427 // from any future radio changes.
428 // If the number field is empty set the presentation type to Unknown.
Wink Savillea639b312012-07-10 12:37:54 -0700429 if (presentation == PhoneConstants.PRESENTATION_RESTRICTED) {
Jay Shraunerd88eb712013-06-10 10:10:11 -0700430 numberPresentation = PRESENTATION_RESTRICTED;
Wink Savillea639b312012-07-10 12:37:54 -0700431 } else if (presentation == PhoneConstants.PRESENTATION_PAYPHONE) {
Jay Shraunerd88eb712013-06-10 10:10:11 -0700432 numberPresentation = PRESENTATION_PAYPHONE;
Pauyl l Bermane1dc2ba2009-07-10 18:27:36 -0400433 } else if (TextUtils.isEmpty(number)
Wink Savillea639b312012-07-10 12:37:54 -0700434 || presentation == PhoneConstants.PRESENTATION_UNKNOWN) {
Jay Shraunerd88eb712013-06-10 10:10:11 -0700435 numberPresentation = PRESENTATION_UNKNOWN;
436 }
437 if (numberPresentation != PRESENTATION_ALLOWED) {
438 number = "";
439 if (ci != null) {
440 ci.name = "";
441 }
Wink Savilledda53912009-05-28 17:32:34 -0700442 }
Pauyl l Bermane1dc2ba2009-07-10 18:27:36 -0400443
Evan Charlton8c8a0622014-07-20 12:31:00 -0700444 // accountHandle information
Ihab Awad9c3f1882014-06-30 21:17:13 -0700445 String accountComponentString = null;
446 String accountId = null;
Evan Charlton8c8a0622014-07-20 12:31:00 -0700447 if (accountHandle != null) {
448 accountComponentString = accountHandle.getComponentName().flattenToString();
449 accountId = accountHandle.getId();
Nancy Chen5ffbfcc2014-06-25 14:22:55 -0700450 }
451
Jay Shraunerd88eb712013-06-10 10:10:11 -0700452 ContentValues values = new ContentValues(6);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453
454 values.put(NUMBER, number);
Jay Shraunerd88eb712013-06-10 10:10:11 -0700455 values.put(NUMBER_PRESENTATION, Integer.valueOf(numberPresentation));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 values.put(TYPE, Integer.valueOf(callType));
Tyler Gunne5daf912014-07-08 15:22:55 -0700457 values.put(FEATURES, features);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 values.put(DATE, Long.valueOf(start));
459 values.put(DURATION, Long.valueOf(duration));
Tyler Gunne5daf912014-07-08 15:22:55 -0700460 if (dataUsage != null) {
461 values.put(DATA_USAGE, dataUsage);
462 }
Ihab Awad9c3f1882014-06-30 21:17:13 -0700463 values.put(PHONE_ACCOUNT_COMPONENT_NAME, accountComponentString);
464 values.put(PHONE_ACCOUNT_ID, accountId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 values.put(NEW, Integer.valueOf(1));
Debashish Chatterjee4efaf4b2011-08-11 16:47:16 +0100466 if (callType == MISSED_TYPE) {
467 values.put(IS_READ, Integer.valueOf(0));
468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 if (ci != null) {
470 values.put(CACHED_NAME, ci.name);
471 values.put(CACHED_NUMBER_TYPE, ci.numberType);
472 values.put(CACHED_NUMBER_LABEL, ci.numberLabel);
473 }
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800474
Makoto Onukia2295e62014-07-10 15:32:16 -0700475 if ((ci != null) && (ci.contactIdOrZero > 0)) {
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700476 // 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.
479
480 final Cursor cursor;
481
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 + " =?",
Makoto Onukia2295e62014-07-10 15:32:16 -0700489 new String[] { String.valueOf(ci.contactIdOrZero),
490 normalizedPhoneNumber},
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700491 null);
492 } else {
493 final String phoneNumber = ci.phoneNumber != null ? ci.phoneNumber : number;
Daisuke Miyakawae23362a2012-05-06 16:54:25 -0700494 cursor = resolver.query(
495 Uri.withAppendedPath(Callable.CONTENT_FILTER_URI,
496 Uri.encode(phoneNumber)),
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700497 new String[] { Phone._ID },
Daisuke Miyakawae23362a2012-05-06 16:54:25 -0700498 Phone.CONTACT_ID + " =?",
Makoto Onukia2295e62014-07-10 15:32:16 -0700499 new String[] { String.valueOf(ci.contactIdOrZero) },
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700500 null);
501 }
502
503 if (cursor != null) {
504 try {
505 if (cursor.getCount() > 0 && cursor.moveToFirst()) {
Yorke Lee3dbc1182014-08-21 09:37:22 -0700506 final String dataId = cursor.getString(0);
507 updateDataUsageStatForData(resolver, dataId);
508 if (duration >= MIN_DURATION_FOR_NORMALIZED_NUMBER_UPDATE_MS
509 && callType == Calls.OUTGOING_TYPE
510 && TextUtils.isEmpty(ci.normalizedNumber)) {
511 updateNormalizedNumber(context, resolver, dataId, number);
512 }
Daisuke Miyakawaf4685912011-06-25 12:31:13 -0700513 }
514 } finally {
515 cursor.close();
516 }
517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800519
Yorke Lee158c5e42014-07-16 15:31:25 -0700520 Uri result = null;
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800521
Yorke Lee158c5e42014-07-16 15:31:25 -0700522 if (addForAllUsers) {
523 // Insert the entry for all currently running users, in order to trigger any
524 // ContentObservers currently set on the call log.
525 final UserManager userManager = (UserManager) context.getSystemService(
526 Context.USER_SERVICE);
527 List<UserInfo> users = userManager.getUsers(true);
528 final int currentUserId = userManager.getUserHandle();
529 final int count = users.size();
530 for (int i = 0; i < count; i++) {
531 final UserInfo user = users.get(i);
532 final UserHandle userHandle = user.getUserHandle();
Yorke Leea79e2be2014-08-10 13:52:21 -0700533 if (userManager.isUserRunning(userHandle)
534 && !userManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
535 userHandle)
536 && !user.isManagedProfile()) {
Yorke Lee158c5e42014-07-16 15:31:25 -0700537 Uri uri = addEntryAndRemoveExpiredEntries(context,
538 ContentProvider.maybeAddUserId(CONTENT_URI, user.id), values);
539 if (user.id == currentUserId) {
540 result = uri;
541 }
542 }
543 }
544 } else {
545 result = addEntryAndRemoveExpiredEntries(context, CONTENT_URI, values);
546 }
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 return result;
549 }
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800550
551 /**
552 * Query the call log database for the last dialed number.
553 * @param context Used to get the content resolver.
554 * @return The last phone number dialed (outgoing) or an empty
555 * string if none exist yet.
556 */
557 public static String getLastOutgoingCall(Context context) {
558 final ContentResolver resolver = context.getContentResolver();
559 Cursor c = null;
560 try {
561 c = resolver.query(
562 CONTENT_URI,
563 new String[] {NUMBER},
564 TYPE + " = " + OUTGOING_TYPE,
565 null,
566 DEFAULT_SORT_ORDER + " LIMIT 1");
567 if (c == null || !c.moveToFirst()) {
568 return "";
569 }
570 return c.getString(0);
571 } finally {
572 if (c != null) c.close();
573 }
574 }
575
Yorke Lee158c5e42014-07-16 15:31:25 -0700576 private static Uri addEntryAndRemoveExpiredEntries(Context context, Uri uri,
577 ContentValues values) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 final ContentResolver resolver = context.getContentResolver();
Yorke Lee158c5e42014-07-16 15:31:25 -0700579 Uri result = resolver.insert(uri, values);
580 resolver.delete(uri, "_id IN " +
Nicolas Cataniab8a2aaf2010-01-14 10:44:02 -0800581 "(SELECT _id FROM calls ORDER BY " + DEFAULT_SORT_ORDER
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 + " LIMIT -1 OFFSET 500)", null);
Yorke Lee158c5e42014-07-16 15:31:25 -0700583 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
Yorke Lee3dbc1182014-08-21 09:37:22 -0700585
586 private static void updateDataUsageStatForData(ContentResolver resolver, String dataId) {
587 final Uri feedbackUri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
588 .appendPath(dataId)
589 .appendQueryParameter(DataUsageFeedback.USAGE_TYPE,
590 DataUsageFeedback.USAGE_TYPE_CALL)
591 .build();
592 resolver.update(feedbackUri, new ContentValues(), null, null);
593 }
594
595 /**
596 * Update the normalized phone number for the given dataId in the ContactsProvider, based
597 * on the user's current country.
598 */
599 private static void updateNormalizedNumber(Context context, ContentResolver resolver,
600 String dataId, String number) {
601 if (TextUtils.isEmpty(number) || TextUtils.isEmpty(dataId)) {
602 return;
603 }
604
605 final String countryIso = getCurrentCountryIso(context);
606 if (TextUtils.isEmpty(countryIso)) {
607 return;
608 }
609
610 final String normalizedNumber = PhoneNumberUtils.formatNumberToE164(number,
611 getCurrentCountryIso(context));
612 if (TextUtils.isEmpty(normalizedNumber)) {
613 return;
614 }
615
616 final ContentValues values = new ContentValues();
617 values.put(Phone.NORMALIZED_NUMBER, normalizedNumber);
618 resolver.update(Data.CONTENT_URI, values, Data._ID + "=?", new String[] {dataId});
619 }
620
621 private static String getCurrentCountryIso(Context context) {
622 String countryIso = null;
623 final CountryDetector detector = (CountryDetector) context.getSystemService(
624 Context.COUNTRY_DETECTOR);
625 if (detector != null) {
626 final Country country = detector.detectCountry();
627 if (country != null) {
628 countryIso = country.getCountryIso();
629 }
630 }
631 return countryIso;
632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634}