| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.telecom; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.ComponentName; |
| 22 |
import android.content.ContentResolver; |
| 23 |
import android.content.Context; |
| 24 |
import android.database.Cursor; |
| 25 |
import android.graphics.Bitmap; |
| 26 |
import android.graphics.drawable.Drawable; |
| 27 |
import android.location.Country; |
| 28 |
import android.location.CountryDetector; |
| 29 |
import android.net.Uri; |
| 30 |
import android.os.Build; |
| 31 |
import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 32 |
import android.provider.ContactsContract.Contacts; |
| 33 |
import android.provider.ContactsContract.Data; |
| 34 |
import android.provider.ContactsContract.PhoneLookup; |
| 35 |
import android.provider.ContactsContract.RawContacts; |
| 36 |
import android.telephony.PhoneNumberUtils; |
| 37 |
import android.telephony.SubscriptionManager; |
| 38 |
import android.telephony.TelephonyManager; |
| 39 |
import android.text.TextUtils; |
| 40 |
|
| 41 |
import com.android.i18n.phonenumbers.NumberParseException; |
| 42 |
import com.android.i18n.phonenumbers.PhoneNumberUtil; |
| 43 |
import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber; |
| 44 |
import com.android.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder; |
| 45 |
import com.android.internal.annotations.VisibleForTesting; |
| 46 |
|
| 47 |
import java.util.Locale; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class CallerInfo { |
| 56 |
private static final String TAG = "CallerInfo"; |
| 57 |
private static final boolean VDBG = Log.VERBOSE; |
| 58 |
|
| 59 |
|
| 60 |
public static final long USER_TYPE_CURRENT = 0; |
| 61 |
|
| 62 |
public static final long USER_TYPE_WORK = 1; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private String name; |
| 92 |
private String phoneNumber; |
| 93 |
|
| 94 |
public String normalizedNumber; |
| 95 |
|
| 96 |
public String geoDescription; |
| 97 |
|
| 98 |
public String cnapName; |
| 99 |
|
| 100 |
public int numberPresentation; |
| 101 |
|
| 102 |
public int namePresentation; |
| 103 |
|
| 104 |
public boolean contactExists; |
| 105 |
|
| 106 |
public String phoneLabel; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
@UnsupportedAppUsage |
| 112 |
public int numberType; |
| 113 |
|
| 114 |
@UnsupportedAppUsage |
| 115 |
public String numberLabel; |
| 116 |
|
| 117 |
public int photoResource; |
| 118 |
|
| 119 |
|
| 120 |
private long contactIdOrZero; |
| 121 |
|
| 122 |
public boolean needUpdate; |
| 123 |
|
| 124 |
public Uri contactRefUri; |
| 125 |
|
| 126 |
public String lookupKey; |
| 127 |
|
| 128 |
public ComponentName preferredPhoneAccountComponent; |
| 129 |
|
| 130 |
public String preferredPhoneAccountId; |
| 131 |
|
| 132 |
public long userType; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private Uri contactDisplayPhotoUri; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public Uri contactRingtoneUri; |
| 145 |
|
| 146 |
public boolean shouldSendToVoicemail; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public Drawable cachedPhoto; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public Bitmap cachedPhotoIcon; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public boolean isCachedPhotoCurrent; |
| 185 |
|
| 186 |
private boolean mIsEmergency; |
| 187 |
private boolean mIsVoiceMail; |
| 188 |
|
| 189 |
|
| 190 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 191 |
public CallerInfo() { |
| 192 |
|
| 193 |
mIsEmergency = false; |
| 194 |
mIsVoiceMail = false; |
| 195 |
userType = USER_TYPE_CURRENT; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public static CallerInfo getCallerInfo(Context context, Uri contactRef, Cursor cursor) { |
| 209 |
CallerInfo info = new CallerInfo(); |
| 210 |
info.photoResource = 0; |
| 211 |
info.phoneLabel = null; |
| 212 |
info.numberType = 0; |
| 213 |
info.numberLabel = null; |
| 214 |
info.cachedPhoto = null; |
| 215 |
info.isCachedPhotoCurrent = false; |
| 216 |
info.contactExists = false; |
| 217 |
info.userType = USER_TYPE_CURRENT; |
| 218 |
|
| 219 |
if (VDBG) Log.v(TAG, "getCallerInfo() based on cursor..."); |
| 220 |
|
| 221 |
if (cursor != null) { |
| 222 |
if (cursor.moveToFirst()) { |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
int columnIndex; |
| 228 |
|
| 229 |
|
| 230 |
columnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); |
| 231 |
if (columnIndex != -1) { |
| 232 |
info.name = cursor.getString(columnIndex); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
columnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER); |
| 237 |
if (columnIndex != -1) { |
| 238 |
info.phoneNumber = cursor.getString(columnIndex); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
columnIndex = cursor.getColumnIndex(PhoneLookup.NORMALIZED_NUMBER); |
| 243 |
if (columnIndex != -1) { |
| 244 |
info.normalizedNumber = cursor.getString(columnIndex); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
columnIndex = cursor.getColumnIndex(PhoneLookup.LABEL); |
| 249 |
if (columnIndex != -1) { |
| 250 |
int typeColumnIndex = cursor.getColumnIndex(PhoneLookup.TYPE); |
| 251 |
if (typeColumnIndex != -1) { |
| 252 |
info.numberType = cursor.getInt(typeColumnIndex); |
| 253 |
info.numberLabel = cursor.getString(columnIndex); |
| 254 |
info.phoneLabel = Phone.getDisplayLabel(context, |
| 255 |
info.numberType, info.numberLabel) |
| 256 |
.toString(); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
columnIndex = getColumnIndexForPersonId(contactRef, cursor); |
| 262 |
if (columnIndex != -1) { |
| 263 |
final long contactId = cursor.getLong(columnIndex); |
| 264 |
if (contactId != 0 && !Contacts.isEnterpriseContactId(contactId)) { |
| 265 |
info.contactIdOrZero = contactId; |
| 266 |
if (VDBG) { |
| 267 |
Log.v(TAG, "==> got info.contactIdOrZero: " + info.contactIdOrZero); |
| 268 |
} |
| 269 |
} |
| 270 |
if (Contacts.isEnterpriseContactId(contactId)) { |
| 271 |
info.userType = USER_TYPE_WORK; |
| 272 |
} |
| 273 |
} else { |
| 274 |
|
| 275 |
Log.w(TAG, "Couldn't find contact_id column for " + contactRef); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
columnIndex = cursor.getColumnIndex(PhoneLookup.LOOKUP_KEY); |
| 283 |
if (columnIndex != -1) { |
| 284 |
info.lookupKey = cursor.getString(columnIndex); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
columnIndex = cursor.getColumnIndex(PhoneLookup.PHOTO_URI); |
| 289 |
if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) { |
| 290 |
info.contactDisplayPhotoUri = Uri.parse(cursor.getString(columnIndex)); |
| 291 |
} else { |
| 292 |
info.contactDisplayPhotoUri = null; |
| 293 |
} |
| 294 |
|
| 295 |
columnIndex = cursor.getColumnIndex(Data.PREFERRED_PHONE_ACCOUNT_COMPONENT_NAME); |
| 296 |
if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) { |
| 297 |
info.preferredPhoneAccountComponent = |
| 298 |
ComponentName.unflattenFromString(cursor.getString(columnIndex)); |
| 299 |
} |
| 300 |
|
| 301 |
columnIndex = cursor.getColumnIndex(Data.PREFERRED_PHONE_ACCOUNT_ID); |
| 302 |
if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) { |
| 303 |
info.preferredPhoneAccountId = cursor.getString(columnIndex); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
columnIndex = cursor.getColumnIndex(PhoneLookup.CUSTOM_RINGTONE); |
| 313 |
if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) { |
| 314 |
if (TextUtils.isEmpty(cursor.getString(columnIndex))) { |
| 315 |
info.contactRingtoneUri = Uri.EMPTY; |
| 316 |
} else { |
| 317 |
info.contactRingtoneUri = Uri.parse(cursor.getString(columnIndex)); |
| 318 |
} |
| 319 |
} else { |
| 320 |
info.contactRingtoneUri = null; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
columnIndex = cursor.getColumnIndex(PhoneLookup.SEND_TO_VOICEMAIL); |
| 326 |
info.shouldSendToVoicemail = (columnIndex != -1) && |
| 327 |
((cursor.getInt(columnIndex)) == 1); |
| 328 |
info.contactExists = true; |
| 329 |
} |
| 330 |
cursor.close(); |
| 331 |
cursor = null; |
| 332 |
} |
| 333 |
|
| 334 |
info.needUpdate = false; |
| 335 |
info.name = normalize(info.name); |
| 336 |
info.contactRefUri = contactRef; |
| 337 |
|
| 338 |
return info; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 352 |
public static CallerInfo getCallerInfo(Context context, Uri contactRef) { |
| 353 |
CallerInfo info = null; |
| 354 |
ContentResolver cr = CallerInfoAsyncQuery.getCurrentProfileContentResolver(context); |
| 355 |
if (cr != null) { |
| 356 |
try { |
| 357 |
info = getCallerInfo(context, contactRef, |
| 358 |
cr.query(contactRef, null, null, null, null)); |
| 359 |
} catch (RuntimeException re) { |
| 360 |
Log.e(TAG, re, "Error getting caller info."); |
| 361 |
} |
| 362 |
} |
| 363 |
return info; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 379 |
public static CallerInfo getCallerInfo(Context context, String number) { |
| 380 |
if (VDBG) Log.v(TAG, "getCallerInfo() based on number..."); |
| 381 |
|
| 382 |
int subId = SubscriptionManager.getDefaultSubscriptionId(); |
| 383 |
return getCallerInfo(context, number, subId); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 400 |
public static CallerInfo getCallerInfo(Context context, String number, int subId) { |
| 401 |
|
| 402 |
if (TextUtils.isEmpty(number)) { |
| 403 |
return null; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
TelephonyManager tm = context.getSystemService(TelephonyManager.class); |
| 410 |
if (tm.isEmergencyNumber(number)) { |
| 411 |
return new CallerInfo().markAsEmergency(context); |
| 412 |
} else if (PhoneNumberUtils.isVoiceMailNumber(null, subId, number)) { |
| 413 |
return new CallerInfo().markAsVoiceMail(context, subId); |
| 414 |
} |
| 415 |
|
| 416 |
Uri contactUri = Uri.withAppendedPath(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, |
| 417 |
Uri.encode(number)); |
| 418 |
|
| 419 |
CallerInfo info = getCallerInfo(context, contactUri); |
| 420 |
info = doSecondaryLookupIfNecessary(context, number, info); |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
if (TextUtils.isEmpty(info.phoneNumber)) { |
| 425 |
info.phoneNumber = number; |
| 426 |
} |
| 427 |
|
| 428 |
return info; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
@Nullable |
| 435 |
public String getName() { |
| 436 |
return name; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public void setName(@Nullable String name) { |
| 446 |
this.name = name; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
@Nullable |
| 453 |
public String getPhoneNumber() { |
| 454 |
return phoneNumber; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
public void setPhoneNumber(String number) { |
| 459 |
phoneNumber = number; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public long getContactId() { |
| 466 |
return contactIdOrZero; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
@Nullable |
| 474 |
public Uri getContactDisplayPhotoUri() { |
| 475 |
return contactDisplayPhotoUri; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
@VisibleForTesting |
| 480 |
public void SetContactDisplayPhotoUri(Uri photoUri) { |
| 481 |
contactDisplayPhotoUri = photoUri; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
static CallerInfo doSecondaryLookupIfNecessary(Context context, |
| 495 |
String number, CallerInfo previousResult) { |
| 496 |
if (!previousResult.contactExists |
| 497 |
&& PhoneNumberUtils.isUriNumber(number)) { |
| 498 |
String username = PhoneNumberUtils.getUsernameFromUriNumber(number); |
| 499 |
if (PhoneNumberUtils.isGlobalPhoneNumber(username)) { |
| 500 |
previousResult = getCallerInfo(context, |
| 501 |
Uri.withAppendedPath(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, |
| 502 |
Uri.encode(username))); |
| 503 |
} |
| 504 |
} |
| 505 |
return previousResult; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
public boolean isEmergencyNumber() { |
| 515 |
return mIsEmergency; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
public boolean isVoiceMailNumber() { |
| 523 |
return mIsVoiceMail; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
CallerInfo markAsEmergency(Context context) { |
| 539 |
phoneNumber = context.getString( |
| 540 |
com.android.internal.R.string.emergency_call_dialog_number_for_display); |
| 541 |
photoResource = com.android.internal.R.drawable.picture_emergency; |
| 542 |
mIsEmergency = true; |
| 543 |
return this; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
CallerInfo markAsVoiceMail(Context context, int subId) { |
| 548 |
mIsVoiceMail = true; |
| 549 |
|
| 550 |
try { |
| 551 |
phoneNumber = context.getSystemService(TelephonyManager.class) |
| 552 |
.createForSubscriptionId(subId) |
| 553 |
.getVoiceMailAlphaTag(); |
| 554 |
} catch (SecurityException se) { |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
Log.e(TAG, se, "Cannot access VoiceMail."); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
return this; |
| 566 |
} |
| 567 |
|
| 568 |
private static String normalize(String s) { |
| 569 |
if (s == null || s.length() > 0) { |
| 570 |
return s; |
| 571 |
} else { |
| 572 |
return null; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
private static int getColumnIndexForPersonId(Uri contactRef, Cursor cursor) { |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
if (VDBG) Log.v(TAG, "- getColumnIndexForPersonId: contactRef URI = '" |
| 620 |
+ contactRef + "'..."); |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
String url = contactRef.toString(); |
| 626 |
String columnName = null; |
| 627 |
if (url.startsWith("content://com.android.contacts/data/phones")) { |
| 628 |
|
| 629 |
|
| 630 |
if (VDBG) Log.v(TAG, "'data/phones' URI; using RawContacts.CONTACT_ID"); |
| 631 |
columnName = RawContacts.CONTACT_ID; |
| 632 |
} else if (url.startsWith("content://com.android.contacts/data")) { |
| 633 |
|
| 634 |
|
| 635 |
if (VDBG) Log.v(TAG, "'data' URI; using Data.CONTACT_ID"); |
| 636 |
|
| 637 |
columnName = Data.CONTACT_ID; |
| 638 |
} else if (url.startsWith("content://com.android.contacts/phone_lookup")) { |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
if (VDBG) Log.v(TAG, "'phone_lookup' URI; using PhoneLookup._ID"); |
| 643 |
columnName = PhoneLookup._ID; |
| 644 |
} else { |
| 645 |
Log.w(TAG, "Unexpected prefix for contactRef '" + url + "'"); |
| 646 |
} |
| 647 |
int columnIndex = (columnName != null) ? cursor.getColumnIndex(columnName) : -1; |
| 648 |
if (VDBG) Log.v(TAG, "==> Using column '" + columnName |
| 649 |
+ "' (columnIndex = " + columnIndex + ") for person_id lookup..."); |
| 650 |
return columnIndex; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
public void updateGeoDescription(Context context, String fallbackNumber) { |
| 667 |
String number = TextUtils.isEmpty(phoneNumber) ? fallbackNumber : phoneNumber; |
| 668 |
geoDescription = getGeoDescription(context, number); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
public static String getGeoDescription(Context context, String number) { |
| 678 |
if (VDBG) Log.v(TAG, "getGeoDescription('" + number + "')..."); |
| 679 |
|
| 680 |
if (TextUtils.isEmpty(number)) { |
| 681 |
return null; |
| 682 |
} |
| 683 |
|
| 684 |
PhoneNumberUtil util = PhoneNumberUtil.getInstance(); |
| 685 |
PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance(); |
| 686 |
|
| 687 |
Locale locale = context.getResources().getConfiguration().locale; |
| 688 |
String countryIso = getCurrentCountryIso(context, locale); |
| 689 |
PhoneNumber pn = null; |
| 690 |
try { |
| 691 |
if (VDBG) Log.v(TAG, "parsing '" + number |
| 692 |
+ "' for countryIso '" + countryIso + "'..."); |
| 693 |
pn = util.parse(number, countryIso); |
| 694 |
if (VDBG) Log.v(TAG, "- parsed number: " + pn); |
| 695 |
} catch (NumberParseException e) { |
| 696 |
Log.w(TAG, "getGeoDescription: NumberParseException for incoming number '" |
| 697 |
+ Log.pii(number) + "'"); |
| 698 |
} |
| 699 |
|
| 700 |
if (pn != null) { |
| 701 |
String description = geocoder.getDescriptionForNumber(pn, locale); |
| 702 |
if (VDBG) Log.v(TAG, "- got description: '" + description + "'"); |
| 703 |
return description; |
| 704 |
} else { |
| 705 |
return null; |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
private static String getCurrentCountryIso(Context context, Locale locale) { |
| 714 |
String countryIso = null; |
| 715 |
CountryDetector detector = (CountryDetector) context.getSystemService( |
| 716 |
Context.COUNTRY_DETECTOR); |
| 717 |
if (detector != null) { |
| 718 |
Country country = detector.detectCountry(); |
| 719 |
if (country != null) { |
| 720 |
countryIso = country.getCountryIso(); |
| 721 |
} else { |
| 722 |
Log.e(TAG, new Exception(), "CountryDetector.detectCountry() returned null."); |
| 723 |
} |
| 724 |
} |
| 725 |
if (countryIso == null) { |
| 726 |
countryIso = locale.getCountry(); |
| 727 |
Log.w(TAG, "No CountryDetector; falling back to countryIso based on locale: " |
| 728 |
+ countryIso); |
| 729 |
} |
| 730 |
return countryIso; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
protected static String getCurrentCountryIso(Context context) { |
| 735 |
return getCurrentCountryIso(context, Locale.getDefault()); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
@Override |
| 742 |
public String toString() { |
| 743 |
|
| 744 |
|
| 745 |
final boolean VERBOSE_DEBUG = false; |
| 746 |
|
| 747 |
if (VERBOSE_DEBUG) { |
| 748 |
return new StringBuilder(384) |
| 749 |
.append(super.toString() + " { ") |
| 750 |
.append("\nname: " + name) |
| 751 |
.append("\nphoneNumber: " + phoneNumber) |
| 752 |
.append("\nnormalizedNumber: " + normalizedNumber) |
| 753 |
.append("\ngeoDescription: " + geoDescription) |
| 754 |
.append("\ncnapName: " + cnapName) |
| 755 |
.append("\nnumberPresentation: " + numberPresentation) |
| 756 |
.append("\nnamePresentation: " + namePresentation) |
| 757 |
.append("\ncontactExits: " + contactExists) |
| 758 |
.append("\nphoneLabel: " + phoneLabel) |
| 759 |
.append("\nnumberType: " + numberType) |
| 760 |
.append("\nnumberLabel: " + numberLabel) |
| 761 |
.append("\nphotoResource: " + photoResource) |
| 762 |
.append("\ncontactIdOrZero: " + contactIdOrZero) |
| 763 |
.append("\nneedUpdate: " + needUpdate) |
| 764 |
.append("\ncontactRingtoneUri: " + contactRingtoneUri) |
| 765 |
.append("\ncontactDisplayPhotoUri: " + contactDisplayPhotoUri) |
| 766 |
.append("\nshouldSendToVoicemail: " + shouldSendToVoicemail) |
| 767 |
.append("\ncachedPhoto: " + cachedPhoto) |
| 768 |
.append("\nisCachedPhotoCurrent: " + isCachedPhotoCurrent) |
| 769 |
.append("\nemergency: " + mIsEmergency) |
| 770 |
.append("\nvoicemail " + mIsVoiceMail) |
| 771 |
.append("\ncontactExists " + contactExists) |
| 772 |
.append("\nuserType " + userType) |
| 773 |
.append(" }") |
| 774 |
.toString(); |
| 775 |
} else { |
| 776 |
return new StringBuilder(128) |
| 777 |
.append(super.toString() + " { ") |
| 778 |
.append("name " + ((name == null) ? "null" : "non-null")) |
| 779 |
.append(", phoneNumber " + ((phoneNumber == null) ? "null" : "non-null")) |
| 780 |
.append(" }") |
| 781 |
.toString(); |
| 782 |
} |
| 783 |
} |
| 784 |
} |