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