CallerInfo.java revision 6a3d188f18b5ae278c802c8bbd1e0a44da555cdf
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 com.android.internal.telephony; 18 19import android.content.Context; 20import android.database.Cursor; 21import android.graphics.drawable.Drawable; 22import android.net.Uri; 23import android.provider.ContactsContract.PhoneLookup; 24import android.provider.ContactsContract.CommonDataKinds.Phone; 25import static android.provider.ContactsContract.RawContacts; 26import android.text.TextUtils; 27import android.telephony.TelephonyManager; 28import android.telephony.PhoneNumberUtils; 29import android.util.Config; 30import android.util.Log; 31 32/** 33 * Looks up caller information for the given phone number. 34 * 35 * {@hide} 36 */ 37public class CallerInfo { 38 private static final String TAG = "CallerInfo"; 39 40 public static final String UNKNOWN_NUMBER = "-1"; 41 public static final String PRIVATE_NUMBER = "-2"; 42 public static final String PAYPHONE_NUMBER = "-3"; 43 44 /** 45 * Please note that, any one of these member variables can be null, 46 * and any accesses to them should be prepared to handle such a case. 47 * 48 * Also, it is implied that phoneNumber is more often populated than 49 * name is, (think of calls being dialed/received using numbers where 50 * names are not known to the device), so phoneNumber should serve as 51 * a dependable fallback when name is unavailable. 52 * 53 * One other detail here is that this CallerInfo object reflects 54 * information found on a connection, it is an OUTPUT that serves 55 * mainly to display information to the user. In no way is this object 56 * used as input to make a connection, so we can choose to display 57 * whatever human-readable text makes sense to the user for a 58 * connection. This is especially relevant for the phone number field, 59 * since it is the one field that is most likely exposed to the user. 60 * 61 * As an example: 62 * 1. User dials "911" 63 * 2. Device recognizes that this is an emergency number 64 * 3. We use the "Emergency Number" string instead of "911" in the 65 * phoneNumber field. 66 * 67 * What we're really doing here is treating phoneNumber as an essential 68 * field here, NOT name. We're NOT always guaranteed to have a name 69 * for a connection, but the number should be displayable. 70 */ 71 public String name; 72 public String phoneNumber; 73 public String nomalizedNumber; 74 75 public String cnapName; 76 public int numberPresentation; 77 public int namePresentation; 78 public boolean contactExists; 79 80 public String phoneLabel; 81 /* Split up the phoneLabel into number type and label name */ 82 public int numberType; 83 public String numberLabel; 84 85 public int photoResource; 86 public long person_id; 87 public boolean needUpdate; 88 public Uri contactRefUri; 89 90 // fields to hold individual contact preference data, 91 // including the send to voicemail flag and the ringtone 92 // uri reference. 93 public Uri contactRingtoneUri; 94 public boolean shouldSendToVoicemail; 95 96 /** 97 * Drawable representing the caller image. This is essentially 98 * a cache for the image data tied into the connection / 99 * callerinfo object. The isCachedPhotoCurrent flag indicates 100 * if the image data needs to be reloaded. 101 */ 102 public Drawable cachedPhoto; 103 public boolean isCachedPhotoCurrent; 104 105 private boolean mIsEmergency; 106 private boolean mIsVoiceMail; 107 108 public CallerInfo() { 109 // TODO: Move all the basic initialization here? 110 mIsEmergency = false; 111 mIsVoiceMail = false; 112 } 113 114 /** 115 * getCallerInfo given a Cursor. 116 * @param context the context used to retrieve string constants 117 * @param contactRef the URI to attach to this CallerInfo object 118 * @param cursor the first object in the cursor is used to build the CallerInfo object. 119 * @return the CallerInfo which contains the caller id for the given 120 * number. The returned CallerInfo is null if no number is supplied. 121 */ 122 public static CallerInfo getCallerInfo(Context context, Uri contactRef, Cursor cursor) { 123 CallerInfo info = new CallerInfo(); 124 info.photoResource = 0; 125 info.phoneLabel = null; 126 info.numberType = 0; 127 info.numberLabel = null; 128 info.cachedPhoto = null; 129 info.isCachedPhotoCurrent = false; 130 info.contactExists = false; 131 132 if (Config.LOGV) Log.v(TAG, "construct callerInfo from cursor"); 133 134 if (cursor != null) { 135 if (cursor.moveToFirst()) { 136 // TODO: photo_id is always available but not taken 137 // care of here. Maybe we should store it in the 138 // CallerInfo object as well. 139 140 int columnIndex; 141 142 // Look for the name 143 columnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); 144 if (columnIndex != -1) { 145 info.name = cursor.getString(columnIndex); 146 } 147 148 // Look for the number 149 columnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER); 150 if (columnIndex != -1) { 151 info.phoneNumber = cursor.getString(columnIndex); 152 } 153 154 // Look for the normalized number 155 columnIndex = cursor.getColumnIndex(PhoneLookup.NORMALIZED_NUMBER); 156 if (columnIndex != -1) { 157 info.nomalizedNumber = cursor.getString(columnIndex); 158 } 159 160 // Look for the label/type combo 161 columnIndex = cursor.getColumnIndex(PhoneLookup.LABEL); 162 if (columnIndex != -1) { 163 int typeColumnIndex = cursor.getColumnIndex(PhoneLookup.TYPE); 164 if (typeColumnIndex != -1) { 165 info.numberType = cursor.getInt(typeColumnIndex); 166 info.numberLabel = cursor.getString(columnIndex); 167 info.phoneLabel = Phone.getDisplayLabel(context, 168 info.numberType, info.numberLabel) 169 .toString(); 170 } 171 } 172 173 // Look for the person ID. 174 175 // TODO: This is pretty ugly now, see bug 2269240 for 176 // more details. With tel: URI the contact id is in 177 // col "_id" while when we use a 178 // content://contacts/data/phones URI, the contact id 179 // is col "contact_id". As a work around we use the 180 // type of the contact url to figure out which column 181 // we should look at to get the contact_id. 182 183 final String mimeType = context.getContentResolver().getType(contactRef); 184 185 columnIndex = -1; 186 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)) { 187 // content://com.android.contacts/data/phones URL 188 columnIndex = cursor.getColumnIndex(RawContacts.CONTACT_ID); 189 } else { 190 // content://com.android.contacts/phone_lookup URL 191 // TODO: mime type is null here so we cannot test 192 // if we have the right url type. phone_lookup URL 193 // should resolve to a mime type. 194 columnIndex = cursor.getColumnIndex(PhoneLookup._ID); 195 } 196 197 if (columnIndex != -1) { 198 info.person_id = cursor.getLong(columnIndex); 199 } else { 200 Log.e(TAG, "Column missing for " + contactRef); 201 } 202 203 // look for the custom ringtone, create from the string stored 204 // in the database. 205 columnIndex = cursor.getColumnIndex(PhoneLookup.CUSTOM_RINGTONE); 206 if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) { 207 info.contactRingtoneUri = Uri.parse(cursor.getString(columnIndex)); 208 } else { 209 info.contactRingtoneUri = null; 210 } 211 212 // look for the send to voicemail flag, set it to true only 213 // under certain circumstances. 214 columnIndex = cursor.getColumnIndex(PhoneLookup.SEND_TO_VOICEMAIL); 215 info.shouldSendToVoicemail = (columnIndex != -1) && 216 ((cursor.getInt(columnIndex)) == 1); 217 info.contactExists = true; 218 } 219 cursor.close(); 220 } 221 222 info.needUpdate = false; 223 info.name = normalize(info.name); 224 info.contactRefUri = contactRef; 225 226 return info; 227 } 228 229 /** 230 * getCallerInfo given a URI, look up in the call-log database 231 * for the uri unique key. 232 * @param context the context used to get the ContentResolver 233 * @param contactRef the URI used to lookup caller id 234 * @return the CallerInfo which contains the caller id for the given 235 * number. The returned CallerInfo is null if no number is supplied. 236 */ 237 public static CallerInfo getCallerInfo(Context context, Uri contactRef) { 238 239 return getCallerInfo(context, contactRef, 240 context.getContentResolver().query(contactRef, null, null, null, null)); 241 } 242 243 /** 244 * getCallerInfo given a phone number, look up in the call-log database 245 * for the matching caller id info. 246 * @param context the context used to get the ContentResolver 247 * @param number the phone number used to lookup caller id 248 * @return the CallerInfo which contains the caller id for the given 249 * number. The returned CallerInfo is null if no number is supplied. If 250 * a matching number is not found, then a generic caller info is returned, 251 * with all relevant fields empty or null. 252 */ 253 public static CallerInfo getCallerInfo(Context context, String number) { 254 if (TextUtils.isEmpty(number)) { 255 return null; 256 } 257 258 // Change the callerInfo number ONLY if it is an emergency number 259 // or if it is the voicemail number. If it is either, take a 260 // shortcut and skip the query. 261 if (PhoneNumberUtils.isEmergencyNumber(number)) { 262 return new CallerInfo().markAsEmergency(context); 263 } else if (PhoneNumberUtils.isVoiceMailNumber(number)) { 264 return new CallerInfo().markAsVoiceMail(); 265 } 266 267 Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 268 269 CallerInfo info = getCallerInfo(context, contactUri); 270 271 // if no query results were returned with a viable number, 272 // fill in the original number value we used to query with. 273 if (TextUtils.isEmpty(info.phoneNumber)) { 274 info.phoneNumber = number; 275 } 276 277 return info; 278 } 279 280 /** 281 * getCallerId: a convenience method to get the caller id for a given 282 * number. 283 * 284 * @param context the context used to get the ContentResolver. 285 * @param number a phone number. 286 * @return if the number belongs to a contact, the contact's name is 287 * returned; otherwise, the number itself is returned. 288 * 289 * TODO NOTE: This MAY need to refer to the Asynchronous Query API 290 * [startQuery()], instead of getCallerInfo, but since it looks like 291 * it is only being used by the provider calls in the messaging app: 292 * 1. android.provider.Telephony.Mms.getDisplayAddress() 293 * 2. android.provider.Telephony.Sms.getDisplayAddress() 294 * We may not need to make the change. 295 */ 296 public static String getCallerId(Context context, String number) { 297 CallerInfo info = getCallerInfo(context, number); 298 String callerID = null; 299 300 if (info != null) { 301 String name = info.name; 302 303 if (!TextUtils.isEmpty(name)) { 304 callerID = name; 305 } else { 306 callerID = number; 307 } 308 } 309 310 return callerID; 311 } 312 313 // Accessors 314 315 /** 316 * @return true if the caller info is an emergency number. 317 */ 318 public boolean isEmergencyNumber() { 319 return mIsEmergency; 320 } 321 322 /** 323 * @return true if the caller info is a voicemail number. 324 */ 325 public boolean isVoiceMailNumber() { 326 return mIsVoiceMail; 327 } 328 329 /** 330 * Mark this CallerInfo as an emergency call. 331 * @param context To lookup the localized 'Emergency Number' string. 332 * @return this instance. 333 */ 334 // TODO: Note we're setting the phone number here (refer to 335 // javadoc comments at the top of CallerInfo class) to a localized 336 // string 'Emergency Number'. This is pretty bad because we are 337 // making UI work here instead of just packaging the data. We 338 // should set the phone number to the dialed number and name to 339 // 'Emergency Number' and let the UI make the decision about what 340 // should be displayed. 341 /* package */ CallerInfo markAsEmergency(Context context) { 342 phoneNumber = context.getString( 343 com.android.internal.R.string.emergency_call_dialog_number_for_display); 344 photoResource = com.android.internal.R.drawable.picture_emergency; 345 mIsEmergency = true; 346 return this; 347 } 348 349 350 /** 351 * Mark this CallerInfo as a voicemail call. The voicemail label 352 * is obtained from the telephony manager. Caller must hold the 353 * READ_PHONE_STATE permission otherwise the phoneNumber will be 354 * set to null. 355 * @return this instance. 356 */ 357 // TODO: As in the emergency number handling, we end up writing a 358 // string in the phone number field. 359 /* package */ CallerInfo markAsVoiceMail() { 360 mIsVoiceMail = true; 361 362 try { 363 String voiceMailLabel = TelephonyManager.getDefault().getVoiceMailAlphaTag(); 364 365 phoneNumber = voiceMailLabel; 366 } catch (SecurityException se) { 367 // Should never happen: if this process does not have 368 // permission to retrieve VM tag, it should not have 369 // permission to retrieve VM number and would not call 370 // this method. 371 // Leave phoneNumber untouched. 372 Log.e(TAG, "Cannot access VoiceMail.", se); 373 } 374 // TODO: There is no voicemail picture? 375 // FIXME: FIND ANOTHER ICON 376 // photoResource = android.R.drawable.badge_voicemail; 377 return this; 378 } 379 380 private static String normalize(String s) { 381 if (s == null || s.length() > 0) { 382 return s; 383 } else { 384 return null; 385 } 386 } 387 388 /** 389 * @return a string debug representation of this instance. 390 */ 391 public String toString() { 392 return new StringBuilder(384) 393 .append("\nname: " + name) 394 .append("\nphoneNumber: " + phoneNumber) 395 .append("\ncnapName: " + cnapName) 396 .append("\nnumberPresentation: " + numberPresentation) 397 .append("\nnamePresentation: " + namePresentation) 398 .append("\ncontactExits: " + contactExists) 399 .append("\nphoneLabel: " + phoneLabel) 400 .append("\nnumberType: " + numberType) 401 .append("\nnumberLabel: " + numberLabel) 402 .append("\nphotoResource: " + photoResource) 403 .append("\nperson_id: " + person_id) 404 .append("\nneedUpdate: " + needUpdate) 405 .append("\ncontactRefUri: " + contactRefUri) 406 .append("\ncontactRingtoneUri: " + contactRefUri) 407 .append("\nshouldSendToVoicemail: " + shouldSendToVoicemail) 408 .append("\ncachedPhoto: " + cachedPhoto) 409 .append("\nisCachedPhotoCurrent: " + isCachedPhotoCurrent) 410 .append("\nemergency: " + mIsEmergency) 411 .append("\nvoicemail " + mIsVoiceMail) 412 .toString(); 413 } 414} 415