summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard MacGregor <rmacgregor@cyngn.com>2016-04-25 16:22:48 -0700
committerRajesh Yengisetty <rajesh@cyngn.com>2016-05-05 12:15:19 -0700
commita24015fd22db52104a70f1f9a9969d3e93fc173b (patch)
treec0a40dd2dde587c934ce596898b3a96fbabde810
parente3b32e4c9931c5f0a5f56b363bd78ea8d0f2a94f (diff)
downloadandroid_packages_apps_ContactsCommon-a24015fd22db52104a70f1f9a9969d3e93fc173b.tar.gz
android_packages_apps_ContactsCommon-a24015fd22db52104a70f1f9a9969d3e93fc173b.tar.bz2
android_packages_apps_ContactsCommon-a24015fd22db52104a70f1f9a9969d3e93fc173b.zip
Add methods to check valid phone numbers and choose label
This is needed to fix call log lookups in Dialer. It also fixes label choice for incall api contacts. Ticket CD-460 Change-Id: I3eae46eec00dd51d67ea3c3ed0022fbb4e49473f (cherry picked from commit dee5d3a87838e05427b14fa6023e4732acb99aba)
-rw-r--r--src/com/android/contacts/common/util/ContactDisplayUtils.java29
-rw-r--r--src/com/android/contacts/common/util/PhoneNumberHelper.java26
2 files changed, 55 insertions, 0 deletions
diff --git a/src/com/android/contacts/common/util/ContactDisplayUtils.java b/src/com/android/contacts/common/util/ContactDisplayUtils.java
index ca910248..e4e24cdf 100644
--- a/src/com/android/contacts/common/util/ContactDisplayUtils.java
+++ b/src/com/android/contacts/common/util/ContactDisplayUtils.java
@@ -22,6 +22,7 @@ import android.content.Context;
import android.telephony.PhoneNumberUtils;
import android.text.Spannable;
import android.text.SpannableString;
+import android.text.TextUtils;
import android.text.style.TtsSpan;
import android.util.Log;
import android.util.Patterns;
@@ -84,6 +85,34 @@ public class ContactDisplayUtils {
}
/**
+ * Gets a display label for a given phone type.
+ *
+ * @param context The application context.
+ * @param number The number associated with this label query
+ * @param type The type of number.
+ * @param customLabel A custom label to use if the phone is determined to be of custom type
+ * determined by {@link #isCustomPhoneType(Integer))}
+ * @param inCallApiPluginName If number being called is an InCallApi plugin contact, use the
+ * plugin name instead of the custom label.
+ * @return An appropriate string label
+ */
+ public static String getLabelForCall(Context context, String number, Integer type,
+ CharSequence customLabel, String inCallApiPluginName) {
+ Preconditions.checkNotNull(context);
+
+ String label = null;
+ if (isCustomPhoneType(type) && !PhoneNumberUtils.isGlobalPhoneNumber(number)) {
+ label = inCallApiPluginName;
+ }
+
+ if (TextUtils.isEmpty(label)) {
+ label = Phone.getTypeLabel(context.getResources(), type, customLabel).toString();
+ }
+
+ return label;
+ }
+
+ /**
* Find a label for calling.
*
* @param type The type of number.
diff --git a/src/com/android/contacts/common/util/PhoneNumberHelper.java b/src/com/android/contacts/common/util/PhoneNumberHelper.java
index 2b6efc66..39f8a39b 100644
--- a/src/com/android/contacts/common/util/PhoneNumberHelper.java
+++ b/src/com/android/contacts/common/util/PhoneNumberHelper.java
@@ -201,4 +201,30 @@ public class PhoneNumberHelper {
}
return number.substring(0, delimiterIndex);
}
+
+ /**
+ * Determine whether a phone number matches a valid phone number pattern for a specified region.
+ *
+ * @param context application context
+ * @param phoneNumber The number to be formatted.
+ * @param defaultCountryIso The ISO 3166-1 two letters country code whose convention will
+ * be used if the given number doesn't have the country code.
+ * @return boolean representing whether valid phone number or not.
+ */
+ public static boolean isValidNumber(Context context, String phoneNumber,
+ String defaultCountryIso) {
+ final String iso = TextUtils.isEmpty(defaultCountryIso) ?
+ GeoUtil.getCurrentCountryIso(context) : defaultCountryIso;
+ final PhoneNumberUtil util = PhoneNumberUtil.getInstance();
+ boolean result = false;
+
+ try {
+ PhoneNumber pn = util.parseAndKeepRawInput(phoneNumber, iso);
+ result = util.isValidNumber(pn);
+ } catch (NumberParseException e) {
+ Log.w(LOG_TAG, "Number could not be parsed with the given country code!", e);
+ }
+
+ return result;
+ }
}