Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1207)

Unified Diff: chrome/browser/autofill/android/phone_number_util_android.cc

Issue 2924513002: use user chosen country code to format and validate phone number for addresses. (Closed)
Patch Set: add comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/autofill/android/phone_number_util_android.cc
diff --git a/chrome/browser/autofill/android/phone_number_util_android.cc b/chrome/browser/autofill/android/phone_number_util_android.cc
index 6aff62a456b6b6eaf8f3d93cb57a335ebb696e08..7f7ed9c7ee713757de05dae04219083d06278acf 100644
--- a/chrome/browser/autofill/android/phone_number_util_android.cc
+++ b/chrome/browser/autofill/android/phone_number_util_android.cc
@@ -21,18 +21,15 @@ using ::base::android::ScopedJavaLocalRef;
using ::i18n::phonenumbers::PhoneNumber;
using ::i18n::phonenumbers::PhoneNumberUtil;
-// Formats the |phone_number| to the specified |format|. Returns the original
-// number if the operation is not possible.
-std::string FormatPhoneNumber(const std::string& phone_number,
- PhoneNumberUtil::PhoneNumberFormat format) {
- const std::string default_region_code =
- autofill::AutofillCountry::CountryCodeForLocale(
- g_browser_process->GetApplicationLocale());
-
+// Formats the |phone_number| to the specified |format| based on a given
+// country code. Returns the original number if the operation is not possible.
+std::string FormatPhoneNumberWithCountryCode(
+ const std::string& phone_number,
+ const std::string& countryCode,
+ PhoneNumberUtil::PhoneNumberFormat format) {
PhoneNumber parsed_number;
PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
- if (phone_number_util->Parse(phone_number, default_region_code,
- &parsed_number) !=
+ if (phone_number_util->Parse(phone_number, countryCode, &parsed_number) !=
PhoneNumberUtil::NO_PARSING_ERROR) {
return phone_number;
}
@@ -42,11 +39,53 @@ std::string FormatPhoneNumber(const std::string& phone_number,
return formatted_number;
}
+// Formats the |phone_number| to the specified |format|. Use application locale
+// to determine country code. Returns the original number if the operation is
+// not possible.
+std::string FormatPhoneNumber(const std::string& phone_number,
+ PhoneNumberUtil::PhoneNumberFormat format) {
+ return FormatPhoneNumberWithCountryCode(
+ phone_number,
+ autofill::AutofillCountry::CountryCodeForLocale(
+ g_browser_process->GetApplicationLocale()),
+ format);
+}
+
+// Checks whether the given number |jphone_number| is valid by using
+// i18n::phonenumbers::PhoneNumberUtil::IsValidNumber.
+bool IsValidNumberImpl(const std::string& phone_number,
+ const std::string& country_code) {
+ PhoneNumber parsed_number;
+ PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
+ if (phone_number_util->Parse(phone_number, country_code, &parsed_number) !=
+ PhoneNumberUtil::NO_PARSING_ERROR) {
+ return false;
+ }
+
+ return phone_number_util->IsValidNumber(parsed_number);
+}
+
} // namespace
-// Formats the given number |jphone_number| to
+// Formats the given number |jphone_number| for a given country to
// i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format
// by using i18n::phonenumbers::PhoneNumberUtil::Format.
+ScopedJavaLocalRef<jstring> FormatForDisplayWithCountryCode(
+ JNIEnv* env,
+ const base::android::JavaParamRef<jclass>& jcaller,
+ const JavaParamRef<jstring>& jphone_number,
+ const JavaParamRef<jstring>& jcountry_code) {
+ return ConvertUTF8ToJavaString(
+ env, FormatPhoneNumberWithCountryCode(
+ ConvertJavaStringToUTF8(env, jphone_number),
+ ConvertJavaStringToUTF8(env, jcountry_code),
+ PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL));
+}
+
+// Formats the given number |jphone_number|
gogerald1 2017/06/08 16:20:43 * to *
wuandy1 2017/06/08 20:34:58 Done.
+// i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format
+// by using i18n::phonenumbers::PhoneNumberUtil::Format. It uses application
+// locale's country code.
ScopedJavaLocalRef<jstring> FormatForDisplay(
JNIEnv* env,
const base::android::JavaParamRef<jclass>& jcaller,
@@ -72,7 +111,8 @@ ScopedJavaLocalRef<jstring> FormatForResponse(
}
// Checks whether the given number |jphone_number| is valid by using
-// i18n::phonenumbers::PhoneNumberUtil::IsValidNumber.
+// i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. It uses application's
+// locale's country code.
jboolean IsValidNumber(JNIEnv* env,
const base::android::JavaParamRef<jclass>& jcaller,
const JavaParamRef<jstring>& jphone_number) {
@@ -81,15 +121,20 @@ jboolean IsValidNumber(JNIEnv* env,
autofill::AutofillCountry::CountryCodeForLocale(
g_browser_process->GetApplicationLocale());
- PhoneNumber parsed_number;
- PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
- if (phone_number_util->Parse(phone_number, default_region_code,
- &parsed_number) !=
- PhoneNumberUtil::NO_PARSING_ERROR) {
- return false;
- }
+ return IsValidNumberImpl(phone_number, default_region_code);
+}
- return phone_number_util->IsValidNumber(parsed_number);
+// Checks whether the given number |jphone_number| is valid for a given country
+// by using i18n::phonenumbers::PhoneNumberUtil::IsValidNumber.
+jboolean IsValidNumberWithCountryCode(
+ JNIEnv* env,
+ const base::android::JavaParamRef<jclass>& jcaller,
+ const JavaParamRef<jstring>& jphone_number,
+ const JavaParamRef<jstring>& jcountry_code) {
+ const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number);
+ const std::string country_code = ConvertJavaStringToUTF8(env, jcountry_code);
+
+ return IsValidNumberImpl(phone_number, country_code);
}
} // namespace autofill

Powered by Google App Engine
This is Rietveld 408576698