summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard MacGregor <rmacgregor@cyngn.com>2016-04-25 16:22:48 -0700
committerRichard MacGregor <rmacgregor@cyngn.com>2016-04-29 14:19:52 -0700
commitdee5d3a87838e05427b14fa6023e4732acb99aba (patch)
tree3acf69cadf309eb0f05833a6299ba2476bef7213
parent849685b3020654a9a93fee81e89ea6012941804d (diff)
downloadpackages_apps_ContactsCommon-dee5d3a87838e05427b14fa6023e4732acb99aba.tar.gz
packages_apps_ContactsCommon-dee5d3a87838e05427b14fa6023e4732acb99aba.tar.bz2
packages_apps_ContactsCommon-dee5d3a87838e05427b14fa6023e4732acb99aba.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
-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;
+ }
}