CallLog.java revision 105925376f8d0f6b318c9938c7b83ef7fef094da
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 17package android.provider; 18 19import android.content.ContentResolver; 20import android.content.ContentValues; 21import android.content.Context; 22import android.net.Uri; 23import android.provider.Contacts.People; 24import com.android.internal.telephony.CallerInfo; 25import com.android.internal.telephony.Connection; 26 27import android.text.TextUtils; 28import android.util.Log; 29 30/** 31 * The CallLog provider contains information about placed and received calls. 32 */ 33public class CallLog { 34 public static final String AUTHORITY = "call_log"; 35 36 /** 37 * The content:// style URL for this provider 38 */ 39 public static final Uri CONTENT_URI = 40 Uri.parse("content://" + AUTHORITY); 41 42 /** 43 * Contains the recent calls. 44 */ 45 public static class Calls implements BaseColumns { 46 /** 47 * The content:// style URL for this table 48 */ 49 public static final Uri CONTENT_URI = 50 Uri.parse("content://call_log/calls"); 51 52 /** 53 * The content:// style URL for filtering this table on phone numbers 54 */ 55 public static final Uri CONTENT_FILTER_URI = 56 Uri.parse("content://call_log/calls/filter"); 57 58 /** 59 * The default sort order for this table 60 */ 61 public static final String DEFAULT_SORT_ORDER = "date DESC"; 62 63 /** 64 * The MIME type of {@link #CONTENT_URI} and {@link #CONTENT_FILTER_URI} 65 * providing a directory of calls. 66 */ 67 public static final String CONTENT_TYPE = "vnd.android.cursor.dir/calls"; 68 69 /** 70 * The MIME type of a {@link #CONTENT_URI} sub-directory of a single 71 * call. 72 */ 73 public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/calls"; 74 75 /** 76 * The type of the the phone number. 77 * <P>Type: INTEGER (int)</P> 78 */ 79 public static final String TYPE = "type"; 80 81 public static final int INCOMING_TYPE = 1; 82 public static final int OUTGOING_TYPE = 2; 83 public static final int MISSED_TYPE = 3; 84 85 /** 86 * The phone number as the user entered it. 87 * <P>Type: TEXT</P> 88 */ 89 public static final String NUMBER = "number"; 90 91 /** 92 * The date the call occured, in milliseconds since the epoch 93 * <P>Type: INTEGER (long)</P> 94 */ 95 public static final String DATE = "date"; 96 97 /** 98 * The duration of the call in seconds 99 * <P>Type: INTEGER (long)</P> 100 */ 101 public static final String DURATION = "duration"; 102 103 /** 104 * Whether or not the call has been acknowledged 105 * <P>Type: INTEGER (boolean)</P> 106 */ 107 public static final String NEW = "new"; 108 109 /** 110 * The cached name associated with the phone number, if it exists. 111 * This value is not guaranteed to be current, if the contact information 112 * associated with this number has changed. 113 * <P>Type: TEXT</P> 114 */ 115 public static final String CACHED_NAME = "name"; 116 117 /** 118 * The cached number type (Home, Work, etc) associated with the 119 * phone number, if it exists. 120 * This value is not guaranteed to be current, if the contact information 121 * associated with this number has changed. 122 * <P>Type: INTEGER</P> 123 */ 124 public static final String CACHED_NUMBER_TYPE = "numbertype"; 125 126 /** 127 * The cached number label, for a custom number type, associated with the 128 * phone number, if it exists. 129 * This value is not guaranteed to be current, if the contact information 130 * associated with this number has changed. 131 * <P>Type: TEXT</P> 132 */ 133 public static final String CACHED_NUMBER_LABEL = "numberlabel"; 134 135 /** 136 * Adds a call to the call log. 137 * 138 * @param ci the CallerInfo object to get the target contact from. Can be null 139 * if the contact is unknown. 140 * @param context the context used to get the ContentResolver 141 * @param number the phone number to be added to the calls db 142 * @param presentation the number presenting rules set by the network for 143 * "allowed", "payphone", "restricted" or "unknown" 144 * @param callType enumerated values for "incoming", "outgoing", or "missed" 145 * @param start time stamp for the call in milliseconds 146 * @param duration call duration in seconds 147 * 148 * {@hide} 149 */ 150 public static Uri addCall(CallerInfo ci, Context context, String number, 151 int presentation, int callType, long start, int duration) { 152 final ContentResolver resolver = context.getContentResolver(); 153 154 if (TextUtils.isEmpty(number)) { 155 if (presentation == Connection.PRESENTATION_RESTRICTED) { 156 number = CallerInfo.PRIVATE_NUMBER; 157 } else if (presentation == Connection.PRESENTATION_PAYPHONE) { 158 number = CallerInfo.PAYPHONE_NUMBER; 159 } else { 160 number = CallerInfo.UNKNOWN_NUMBER; 161 } 162 } 163 164 ContentValues values = new ContentValues(5); 165 166 values.put(NUMBER, number); 167 values.put(TYPE, Integer.valueOf(callType)); 168 values.put(DATE, Long.valueOf(start)); 169 values.put(DURATION, Long.valueOf(duration)); 170 values.put(NEW, Integer.valueOf(1)); 171 if (ci != null) { 172 values.put(CACHED_NAME, ci.name); 173 values.put(CACHED_NUMBER_TYPE, ci.numberType); 174 values.put(CACHED_NUMBER_LABEL, ci.numberLabel); 175 } 176 177 if ((ci != null) && (ci.person_id > 0)) { 178 People.markAsContacted(resolver, ci.person_id); 179 } 180 181 Uri result = resolver.insert(CONTENT_URI, values); 182 183 removeExpiredEntries(context); 184 185 return result; 186 } 187 188 private static void removeExpiredEntries(Context context) { 189 final ContentResolver resolver = context.getContentResolver(); 190 resolver.delete(CONTENT_URI, "_id IN " + 191 "(SELECT _id FROM calls ORDER BY " + DEFAULT_SORT_ORDER 192 + " LIMIT -1 OFFSET 500)", null); 193 } 194 } 195} 196