blob: 7c37a65380a4dc2ae541f2d0953bdd45d7226d14 [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
17package com.android.internal.telephony;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.graphics.drawable.Drawable;
David Brown94202fe2011-06-10 16:24:05 -070022import android.location.CountryDetector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.net.Uri;
Dmitri Plotnikov3c513ed2009-08-19 15:56:30 -070024import android.provider.ContactsContract.CommonDataKinds.Phone;
David Brown85e0ff82010-10-22 12:54:42 -070025import android.provider.ContactsContract.Data;
26import android.provider.ContactsContract.PhoneLookup;
27import android.provider.ContactsContract.RawContacts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.telephony.PhoneNumberUtils;
David Brown85e0ff82010-10-22 12:54:42 -070029import android.telephony.TelephonyManager;
30import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.Log;
32
Shaopeng Jiae7135762011-08-12 13:25:41 +020033import com.android.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
34import com.android.i18n.phonenumbers.NumberParseException;
35import com.android.i18n.phonenumbers.PhoneNumberUtil;
36import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
David Brown94202fe2011-06-10 16:24:05 -070037
38import java.util.Locale;
39
Wink Saville2e27a0b2010-10-07 08:28:34 -070040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/**
42 * Looks up caller information for the given phone number.
43 *
44 * {@hide}
45 */
46public class CallerInfo {
47 private static final String TAG = "CallerInfo";
Wink Saville2e27a0b2010-10-07 08:28:34 -070048 private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50 public static final String UNKNOWN_NUMBER = "-1";
51 public static final String PRIVATE_NUMBER = "-2";
The Android Open Source Project10592532009-03-18 17:39:46 -070052 public static final String PAYPHONE_NUMBER = "-3";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 /**
55 * Please note that, any one of these member variables can be null,
56 * and any accesses to them should be prepared to handle such a case.
57 *
58 * Also, it is implied that phoneNumber is more often populated than
59 * name is, (think of calls being dialed/received using numbers where
60 * names are not known to the device), so phoneNumber should serve as
61 * a dependable fallback when name is unavailable.
62 *
63 * One other detail here is that this CallerInfo object reflects
64 * information found on a connection, it is an OUTPUT that serves
65 * mainly to display information to the user. In no way is this object
66 * used as input to make a connection, so we can choose to display
67 * whatever human-readable text makes sense to the user for a
68 * connection. This is especially relevant for the phone number field,
69 * since it is the one field that is most likely exposed to the user.
70 *
71 * As an example:
72 * 1. User dials "911"
73 * 2. Device recognizes that this is an emergency number
74 * 3. We use the "Emergency Number" string instead of "911" in the
75 * phoneNumber field.
76 *
77 * What we're really doing here is treating phoneNumber as an essential
78 * field here, NOT name. We're NOT always guaranteed to have a name
79 * for a connection, but the number should be displayable.
80 */
81 public String name;
82 public String phoneNumber;
David Brown94202fe2011-06-10 16:24:05 -070083 public String normalizedNumber;
84 public String geoDescription;
Wink Savilledda53912009-05-28 17:32:34 -070085
86 public String cnapName;
87 public int numberPresentation;
88 public int namePresentation;
89 public boolean contactExists;
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 public String phoneLabel;
92 /* Split up the phoneLabel into number type and label name */
93 public int numberType;
94 public String numberLabel;
Wink Saville2563a3a2009-06-09 10:30:03 -070095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 public int photoResource;
97 public long person_id;
98 public boolean needUpdate;
99 public Uri contactRefUri;
Wink Saville2563a3a2009-06-09 10:30:03 -0700100
101 // fields to hold individual contact preference data,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 // including the send to voicemail flag and the ringtone
103 // uri reference.
104 public Uri contactRingtoneUri;
105 public boolean shouldSendToVoicemail;
106
107 /**
108 * Drawable representing the caller image. This is essentially
109 * a cache for the image data tied into the connection /
110 * callerinfo object. The isCachedPhotoCurrent flag indicates
111 * if the image data needs to be reloaded.
112 */
113 public Drawable cachedPhoto;
114 public boolean isCachedPhotoCurrent;
115
Nicolas Cataniae2241582009-09-14 19:01:43 -0700116 private boolean mIsEmergency;
Nicolas Catania60d45f02009-09-15 18:32:02 -0700117 private boolean mIsVoiceMail;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
119 public CallerInfo() {
Nicolas Cataniae2241582009-09-14 19:01:43 -0700120 // TODO: Move all the basic initialization here?
121 mIsEmergency = false;
Nicolas Catania60d45f02009-09-15 18:32:02 -0700122 mIsVoiceMail = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 }
124
125 /**
126 * getCallerInfo given a Cursor.
127 * @param context the context used to retrieve string constants
128 * @param contactRef the URI to attach to this CallerInfo object
129 * @param cursor the first object in the cursor is used to build the CallerInfo object.
130 * @return the CallerInfo which contains the caller id for the given
131 * number. The returned CallerInfo is null if no number is supplied.
132 */
133 public static CallerInfo getCallerInfo(Context context, Uri contactRef, Cursor cursor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 CallerInfo info = new CallerInfo();
135 info.photoResource = 0;
136 info.phoneLabel = null;
137 info.numberType = 0;
138 info.numberLabel = null;
139 info.cachedPhoto = null;
140 info.isCachedPhotoCurrent = false;
Wink Savilledda53912009-05-28 17:32:34 -0700141 info.contactExists = false;
Wink Saville2563a3a2009-06-09 10:30:03 -0700142
David Brown94202fe2011-06-10 16:24:05 -0700143 if (VDBG) Log.v(TAG, "getCallerInfo() based on cursor...");
Wink Saville2563a3a2009-06-09 10:30:03 -0700144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 if (cursor != null) {
146 if (cursor.moveToFirst()) {
Nicolas Cataniac72509b2010-01-05 16:03:08 -0800147 // TODO: photo_id is always available but not taken
148 // care of here. Maybe we should store it in the
149 // CallerInfo object as well.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
151 int columnIndex;
152
153 // Look for the name
Dmitri Plotnikov3c513ed2009-08-19 15:56:30 -0700154 columnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 if (columnIndex != -1) {
156 info.name = cursor.getString(columnIndex);
157 }
158
159 // Look for the number
Dmitri Plotnikov3c513ed2009-08-19 15:56:30 -0700160 columnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 if (columnIndex != -1) {
162 info.phoneNumber = cursor.getString(columnIndex);
163 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700164
Bai Tao6a3d1882010-08-31 17:46:33 +0800165 // Look for the normalized number
166 columnIndex = cursor.getColumnIndex(PhoneLookup.NORMALIZED_NUMBER);
167 if (columnIndex != -1) {
David Brown94202fe2011-06-10 16:24:05 -0700168 info.normalizedNumber = cursor.getString(columnIndex);
Bai Tao6a3d1882010-08-31 17:46:33 +0800169 }
170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 // Look for the label/type combo
Dmitri Plotnikov3c513ed2009-08-19 15:56:30 -0700172 columnIndex = cursor.getColumnIndex(PhoneLookup.LABEL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 if (columnIndex != -1) {
Dmitri Plotnikov3c513ed2009-08-19 15:56:30 -0700174 int typeColumnIndex = cursor.getColumnIndex(PhoneLookup.TYPE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 if (typeColumnIndex != -1) {
176 info.numberType = cursor.getInt(typeColumnIndex);
177 info.numberLabel = cursor.getString(columnIndex);
Dmitri Plotnikov3c513ed2009-08-19 15:56:30 -0700178 info.phoneLabel = Phone.getDisplayLabel(context,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 info.numberType, info.numberLabel)
180 .toString();
181 }
182 }
183
David Brown85e0ff82010-10-22 12:54:42 -0700184 // Look for the person_id.
185 columnIndex = getColumnIndexForPersonId(contactRef, cursor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 if (columnIndex != -1) {
187 info.person_id = cursor.getLong(columnIndex);
David Brown85e0ff82010-10-22 12:54:42 -0700188 if (VDBG) Log.v(TAG, "==> got info.person_id: " + info.person_id);
Nicolas Cataniac72509b2010-01-05 16:03:08 -0800189 } else {
David Brown85e0ff82010-10-22 12:54:42 -0700190 // No valid columnIndex, so we can't look up person_id.
191 Log.w(TAG, "Couldn't find person_id column for " + contactRef);
192 // Watch out: this means that anything that depends on
193 // person_id will be broken (like contact photo lookups in
194 // the in-call UI, for example.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 // look for the custom ringtone, create from the string stored
198 // in the database.
Dmitri Plotnikov3c513ed2009-08-19 15:56:30 -0700199 columnIndex = cursor.getColumnIndex(PhoneLookup.CUSTOM_RINGTONE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) {
201 info.contactRingtoneUri = Uri.parse(cursor.getString(columnIndex));
202 } else {
203 info.contactRingtoneUri = null;
204 }
205
206 // look for the send to voicemail flag, set it to true only
207 // under certain circumstances.
Dmitri Plotnikov3c513ed2009-08-19 15:56:30 -0700208 columnIndex = cursor.getColumnIndex(PhoneLookup.SEND_TO_VOICEMAIL);
Wink Saville2563a3a2009-06-09 10:30:03 -0700209 info.shouldSendToVoicemail = (columnIndex != -1) &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 ((cursor.getInt(columnIndex)) == 1);
Wink Savilledda53912009-05-28 17:32:34 -0700211 info.contactExists = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
213 cursor.close();
214 }
215
216 info.needUpdate = false;
217 info.name = normalize(info.name);
218 info.contactRefUri = contactRef;
219
220 return info;
221 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 /**
224 * getCallerInfo given a URI, look up in the call-log database
225 * for the uri unique key.
226 * @param context the context used to get the ContentResolver
227 * @param contactRef the URI used to lookup caller id
228 * @return the CallerInfo which contains the caller id for the given
229 * number. The returned CallerInfo is null if no number is supplied.
230 */
231 public static CallerInfo getCallerInfo(Context context, Uri contactRef) {
Wink Saville2563a3a2009-06-09 10:30:03 -0700232
233 return getCallerInfo(context, contactRef,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 context.getContentResolver().query(contactRef, null, null, null, null));
235 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 /**
238 * getCallerInfo given a phone number, look up in the call-log database
239 * for the matching caller id info.
240 * @param context the context used to get the ContentResolver
241 * @param number the phone number used to lookup caller id
242 * @return the CallerInfo which contains the caller id for the given
243 * number. The returned CallerInfo is null if no number is supplied. If
244 * a matching number is not found, then a generic caller info is returned,
245 * with all relevant fields empty or null.
246 */
247 public static CallerInfo getCallerInfo(Context context, String number) {
David Brown94202fe2011-06-10 16:24:05 -0700248 if (VDBG) Log.v(TAG, "getCallerInfo() based on number...");
249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 if (TextUtils.isEmpty(number)) {
251 return null;
Nicolas Catania60d45f02009-09-15 18:32:02 -0700252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Nicolas Catania60d45f02009-09-15 18:32:02 -0700254 // Change the callerInfo number ONLY if it is an emergency number
255 // or if it is the voicemail number. If it is either, take a
256 // shortcut and skip the query.
Shaopeng Jia9683f992011-09-07 14:07:15 +0200257 Locale locale = context.getResources().getConfiguration().locale;
258 String countryIso = getCurrentCountryIso(context, locale);
259 if (PhoneNumberUtils.isEmergencyNumber(number, countryIso)) {
Nicolas Catania60d45f02009-09-15 18:32:02 -0700260 return new CallerInfo().markAsEmergency(context);
261 } else if (PhoneNumberUtils.isVoiceMailNumber(number)) {
262 return new CallerInfo().markAsVoiceMail();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 }
264
Dmitri Plotnikov84b4d372009-09-09 19:35:55 -0700265 Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Wink Saville2563a3a2009-06-09 10:30:03 -0700266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 CallerInfo info = getCallerInfo(context, contactUri);
Hung-ying Tyan6fe795e2010-10-20 11:12:02 +0800268 info = doSecondaryLookupIfNecessary(context, number, info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269
Wink Saville2563a3a2009-06-09 10:30:03 -0700270 // if no query results were returned with a viable number,
271 // fill in the original number value we used to query with.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 if (TextUtils.isEmpty(info.phoneNumber)) {
273 info.phoneNumber = number;
274 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 return info;
277 }
278
279 /**
Hung-ying Tyan6fe795e2010-10-20 11:12:02 +0800280 * Performs another lookup if previous lookup fails and it's a SIP call
281 * and the peer's username is all numeric. Look up the username as it
282 * could be a PSTN number in the contact database.
283 *
284 * @param context the query context
285 * @param number the original phone number, could be a SIP URI
286 * @param previousResult the result of previous lookup
287 * @return previousResult if it's not the case
288 */
289 static CallerInfo doSecondaryLookupIfNecessary(Context context,
290 String number, CallerInfo previousResult) {
291 if (!previousResult.contactExists
292 && PhoneNumberUtils.isUriNumber(number)) {
293 String username = number.substring(0, number.indexOf('@'));
294 if (PhoneNumberUtils.isGlobalPhoneNumber(username)) {
295 previousResult = getCallerInfo(context,
296 Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
297 Uri.encode(username)));
298 }
299 }
300 return previousResult;
301 }
302
303 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 * getCallerId: a convenience method to get the caller id for a given
305 * number.
306 *
307 * @param context the context used to get the ContentResolver.
308 * @param number a phone number.
309 * @return if the number belongs to a contact, the contact's name is
310 * returned; otherwise, the number itself is returned.
Wink Saville2563a3a2009-06-09 10:30:03 -0700311 *
312 * TODO NOTE: This MAY need to refer to the Asynchronous Query API
313 * [startQuery()], instead of getCallerInfo, but since it looks like
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 * it is only being used by the provider calls in the messaging app:
315 * 1. android.provider.Telephony.Mms.getDisplayAddress()
316 * 2. android.provider.Telephony.Sms.getDisplayAddress()
317 * We may not need to make the change.
318 */
319 public static String getCallerId(Context context, String number) {
320 CallerInfo info = getCallerInfo(context, number);
321 String callerID = null;
322
323 if (info != null) {
324 String name = info.name;
325
326 if (!TextUtils.isEmpty(name)) {
327 callerID = name;
328 } else {
329 callerID = number;
330 }
331 }
332
333 return callerID;
334 }
335
Nicolas Cataniae2241582009-09-14 19:01:43 -0700336 // Accessors
337
338 /**
339 * @return true if the caller info is an emergency number.
340 */
341 public boolean isEmergencyNumber() {
342 return mIsEmergency;
343 }
344
345 /**
Nicolas Catania60d45f02009-09-15 18:32:02 -0700346 * @return true if the caller info is a voicemail number.
347 */
348 public boolean isVoiceMailNumber() {
349 return mIsVoiceMail;
350 }
351
352 /**
Nicolas Cataniae2241582009-09-14 19:01:43 -0700353 * Mark this CallerInfo as an emergency call.
354 * @param context To lookup the localized 'Emergency Number' string.
355 * @return this instance.
356 */
357 // TODO: Note we're setting the phone number here (refer to
358 // javadoc comments at the top of CallerInfo class) to a localized
359 // string 'Emergency Number'. This is pretty bad because we are
360 // making UI work here instead of just packaging the data. We
361 // should set the phone number to the dialed number and name to
362 // 'Emergency Number' and let the UI make the decision about what
363 // should be displayed.
364 /* package */ CallerInfo markAsEmergency(Context context) {
365 phoneNumber = context.getString(
366 com.android.internal.R.string.emergency_call_dialog_number_for_display);
367 photoResource = com.android.internal.R.drawable.picture_emergency;
368 mIsEmergency = true;
369 return this;
370 }
371
Nicolas Catania60d45f02009-09-15 18:32:02 -0700372
373 /**
374 * Mark this CallerInfo as a voicemail call. The voicemail label
375 * is obtained from the telephony manager. Caller must hold the
376 * READ_PHONE_STATE permission otherwise the phoneNumber will be
377 * set to null.
378 * @return this instance.
379 */
380 // TODO: As in the emergency number handling, we end up writing a
381 // string in the phone number field.
382 /* package */ CallerInfo markAsVoiceMail() {
383 mIsVoiceMail = true;
384
385 try {
386 String voiceMailLabel = TelephonyManager.getDefault().getVoiceMailAlphaTag();
387
388 phoneNumber = voiceMailLabel;
389 } catch (SecurityException se) {
390 // Should never happen: if this process does not have
391 // permission to retrieve VM tag, it should not have
392 // permission to retrieve VM number and would not call
393 // this method.
394 // Leave phoneNumber untouched.
395 Log.e(TAG, "Cannot access VoiceMail.", se);
396 }
397 // TODO: There is no voicemail picture?
398 // FIXME: FIND ANOTHER ICON
399 // photoResource = android.R.drawable.badge_voicemail;
400 return this;
401 }
402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 private static String normalize(String s) {
404 if (s == null || s.length() > 0) {
405 return s;
406 } else {
407 return null;
408 }
409 }
Nicolas Catania60d45f02009-09-15 18:32:02 -0700410
411 /**
David Brown85e0ff82010-10-22 12:54:42 -0700412 * Returns the column index to use to find the "person_id" field in
413 * the specified cursor, based on the contact URI that was originally
414 * queried.
415 *
416 * This is a helper function for the getCallerInfo() method that takes
417 * a Cursor. Looking up the person_id is nontrivial (compared to all
418 * the other CallerInfo fields) since the column we need to use
419 * depends on what query we originally ran.
420 *
421 * Watch out: be sure to not do any database access in this method, since
422 * it's run from the UI thread (see comments below for more info.)
423 *
424 * @return the columnIndex to use (with cursor.getLong()) to get the
425 * person_id, or -1 if we couldn't figure out what colum to use.
426 *
427 * TODO: Add a unittest for this method. (This is a little tricky to
428 * test, since we'll need a live contacts database to test against,
429 * preloaded with at least some phone numbers and SIP addresses. And
430 * we'll probably have to hardcode the column indexes we expect, so
431 * the test might break whenever the contacts schema changes. But we
432 * can at least make sure we handle all the URI patterns we claim to,
433 * and that the mime types match what we expect...)
434 */
435 private static int getColumnIndexForPersonId(Uri contactRef, Cursor cursor) {
436 // TODO: This is pretty ugly now, see bug 2269240 for
437 // more details. The column to use depends upon the type of URL:
438 // - content://com.android.contacts/data/phones ==> use the "contact_id" column
439 // - content://com.android.contacts/phone_lookup ==> use the "_ID" column
440 // - content://com.android.contacts/data ==> use the "contact_id" column
441 // If it's none of the above, we leave columnIndex=-1 which means
442 // that the person_id field will be left unset.
443 //
444 // The logic here *used* to be based on the mime type of contactRef
445 // (for example Phone.CONTENT_ITEM_TYPE would tell us to use the
446 // RawContacts.CONTACT_ID column). But looking up the mime type requires
447 // a call to context.getContentResolver().getType(contactRef), which
448 // isn't safe to do from the UI thread since it can cause an ANR if
449 // the contacts provider is slow or blocked (like during a sync.)
450 //
451 // So instead, figure out the column to use for person_id by just
452 // looking at the URI itself.
453
454 if (VDBG) Log.v(TAG, "- getColumnIndexForPersonId: contactRef URI = '"
455 + contactRef + "'...");
456 // Warning: Do not enable the following logging (due to ANR risk.)
457 // if (VDBG) Log.v(TAG, "- MIME type: "
458 // + context.getContentResolver().getType(contactRef));
459
460 String url = contactRef.toString();
461 String columnName = null;
462 if (url.startsWith("content://com.android.contacts/data/phones")) {
463 // Direct lookup in the Phone table.
464 // MIME type: Phone.CONTENT_ITEM_TYPE (= "vnd.android.cursor.item/phone_v2")
465 if (VDBG) Log.v(TAG, "'data/phones' URI; using RawContacts.CONTACT_ID");
466 columnName = RawContacts.CONTACT_ID;
467 } else if (url.startsWith("content://com.android.contacts/data")) {
468 // Direct lookup in the Data table.
469 // MIME type: Data.CONTENT_TYPE (= "vnd.android.cursor.dir/data")
470 if (VDBG) Log.v(TAG, "'data' URI; using Data.CONTACT_ID");
471 // (Note Data.CONTACT_ID and RawContacts.CONTACT_ID are equivalent.)
472 columnName = Data.CONTACT_ID;
473 } else if (url.startsWith("content://com.android.contacts/phone_lookup")) {
474 // Lookup in the PhoneLookup table, which provides "fuzzy matching"
475 // for phone numbers.
476 // MIME type: PhoneLookup.CONTENT_TYPE (= "vnd.android.cursor.dir/phone_lookup")
477 if (VDBG) Log.v(TAG, "'phone_lookup' URI; using PhoneLookup._ID");
478 columnName = PhoneLookup._ID;
479 } else {
480 Log.w(TAG, "Unexpected prefix for contactRef '" + url + "'");
481 }
482 int columnIndex = (columnName != null) ? cursor.getColumnIndex(columnName) : -1;
483 if (VDBG) Log.v(TAG, "==> Using column '" + columnName
484 + "' (columnIndex = " + columnIndex + ") for person_id lookup...");
485 return columnIndex;
486 }
487
488 /**
David Brown94202fe2011-06-10 16:24:05 -0700489 * Updates this CallerInfo's geoDescription field, based on the raw
490 * phone number in the phoneNumber field.
491 *
492 * (Note that the various getCallerInfo() methods do *not* set the
493 * geoDescription automatically; you need to call this method
494 * explicitly to get it.)
495 *
496 * @param context the context used to look up the current locale / country
497 * @param fallbackNumber if this CallerInfo's phoneNumber field is empty,
498 * this specifies a fallback number to use instead.
499 */
500 public void updateGeoDescription(Context context, String fallbackNumber) {
501 String number = TextUtils.isEmpty(phoneNumber) ? fallbackNumber : phoneNumber;
502 geoDescription = getGeoDescription(context, number);
503 }
504
505 /**
506 * @return a geographical description string for the specified number.
Shaopeng Jiae7135762011-08-12 13:25:41 +0200507 * @see com.android.i18n.phonenumbers.PhoneNumberOfflineGeocoder
David Brown94202fe2011-06-10 16:24:05 -0700508 */
509 private static String getGeoDescription(Context context, String number) {
510 if (VDBG) Log.v(TAG, "getGeoDescription('" + number + "')...");
511
512 if (TextUtils.isEmpty(number)) {
513 return null;
514 }
515
516 PhoneNumberUtil util = PhoneNumberUtil.getInstance();
517 PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
518
David Brown94202fe2011-06-10 16:24:05 -0700519 Locale locale = context.getResources().getConfiguration().locale;
Shaopeng Jia9683f992011-09-07 14:07:15 +0200520 String countryIso = getCurrentCountryIso(context, locale);
David Brown94202fe2011-06-10 16:24:05 -0700521 PhoneNumber pn = null;
522 try {
David Browncec25c42011-06-23 14:17:27 -0700523 if (VDBG) Log.v(TAG, "parsing '" + number
524 + "' for countryIso '" + countryIso + "'...");
David Brown94202fe2011-06-10 16:24:05 -0700525 pn = util.parse(number, countryIso);
526 if (VDBG) Log.v(TAG, "- parsed number: " + pn);
527 } catch (NumberParseException e) {
528 Log.w(TAG, "getGeoDescription: NumberParseException for incoming number '" + number + "'");
529 }
530
531 if (pn != null) {
532 String description = geocoder.getDescriptionForNumber(pn, locale);
533 if (VDBG) Log.v(TAG, "- got description: '" + description + "'");
534 return description;
535 } else {
536 return null;
537 }
538 }
539
540 /**
Shaopeng Jia9683f992011-09-07 14:07:15 +0200541 * @return The ISO 3166-1 two letters country code of the country the user
542 * is in.
543 */
544 private static String getCurrentCountryIso(Context context, Locale locale) {
545 String countryIso;
546 CountryDetector detector = (CountryDetector) context.getSystemService(
547 Context.COUNTRY_DETECTOR);
548 if (detector != null) {
549 countryIso = detector.detectCountry().getCountryIso();
550 } else {
551 countryIso = locale.getCountry();
552 Log.w(TAG, "No CountryDetector; falling back to countryIso based on locale: "
553 + countryIso);
554 }
555 return countryIso;
556 }
557
558 /**
Nicolas Catania60d45f02009-09-15 18:32:02 -0700559 * @return a string debug representation of this instance.
560 */
561 public String toString() {
David Brown04639ba2010-11-30 15:31:15 -0800562 // Warning: never check in this file with VERBOSE_DEBUG = true
563 // because that will result in PII in the system log.
564 final boolean VERBOSE_DEBUG = false;
565
566 if (VERBOSE_DEBUG) {
567 return new StringBuilder(384)
David Brown94202fe2011-06-10 16:24:05 -0700568 .append(super.toString() + " { ")
David Brown04639ba2010-11-30 15:31:15 -0800569 .append("\nname: " + name)
570 .append("\nphoneNumber: " + phoneNumber)
David Brown94202fe2011-06-10 16:24:05 -0700571 .append("\nnormalizedNumber: " + normalizedNumber)
572 .append("\ngeoDescription: " + geoDescription)
David Brown04639ba2010-11-30 15:31:15 -0800573 .append("\ncnapName: " + cnapName)
574 .append("\nnumberPresentation: " + numberPresentation)
575 .append("\nnamePresentation: " + namePresentation)
576 .append("\ncontactExits: " + contactExists)
577 .append("\nphoneLabel: " + phoneLabel)
578 .append("\nnumberType: " + numberType)
579 .append("\nnumberLabel: " + numberLabel)
580 .append("\nphotoResource: " + photoResource)
581 .append("\nperson_id: " + person_id)
582 .append("\nneedUpdate: " + needUpdate)
583 .append("\ncontactRefUri: " + contactRefUri)
584 .append("\ncontactRingtoneUri: " + contactRefUri)
585 .append("\nshouldSendToVoicemail: " + shouldSendToVoicemail)
586 .append("\ncachedPhoto: " + cachedPhoto)
587 .append("\nisCachedPhotoCurrent: " + isCachedPhotoCurrent)
588 .append("\nemergency: " + mIsEmergency)
589 .append("\nvoicemail " + mIsVoiceMail)
590 .append("\ncontactExists " + contactExists)
David Brown94202fe2011-06-10 16:24:05 -0700591 .append(" }")
David Brown04639ba2010-11-30 15:31:15 -0800592 .toString();
593 } else {
594 return new StringBuilder(128)
David Brown94202fe2011-06-10 16:24:05 -0700595 .append(super.toString() + " { ")
David Brown04639ba2010-11-30 15:31:15 -0800596 .append("name " + ((name == null) ? "null" : "non-null"))
597 .append(", phoneNumber " + ((phoneNumber == null) ? "null" : "non-null"))
598 .append(" }")
599 .toString();
600 }
Nicolas Catania60d45f02009-09-15 18:32:02 -0700601 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700602}