Commit 5c9c165e authored by twellington's avatar twellington Committed by Commit bot

Use/save language code for autofill profiles.

For saved autofill profiles, use the associated language code when retrieving
address ui components. For new autofill profiles, save the best language code
tag returned from libaddressinput's BuildUiComponents.

BUG=454034

Review URL: https://codereview.chromium.org/882123003

Cr-Commit-Position: refs/heads/master@{#314355}
parent e21776bd
......@@ -60,6 +60,8 @@ public class AutofillProfileBridge {
}
}
private String mCurrentBestLanguageCode;
/**
* @return The CLDR region code for the default locale.
*/
......@@ -97,21 +99,30 @@ public class AutofillProfileBridge {
}
/**
* Returns the UI components for the CLDR countryCode provided.
* Returns the UI components for the CLDR countryCode and languageCode provided. If no language
* code is provided, the application's default locale is used instead. Also stores the
* currentBestLanguageCode, retrievable via getCurrentBestLanguageCode, to be used when saving
* an autofill profile.
*
* @param countryCode The CLDR code used to retrieve address components.
* @param languageCode The language code associated with the saved autofill profile that ui
* components are being retrieved for; can be null if ui components are
* being retrieved for a new profile.
* @return A list containing pairs where the first element in the pair is an Integer
* representing the component id (one of the constants in AddressField), and the second
* element in the pair is the localized component name (intended for use as labels in
* the UI). The ordering in the list of pairs specifies the order these components
* should appear in the UI.
*/
static List<Pair<Integer, String>> getAddressUiComponents(String countryCode) {
List<Pair<Integer, String>> getAddressUiComponents(String countryCode,
String languageCode) {
List<Integer> componentIds = new ArrayList<Integer>();
List<String> componentNames = new ArrayList<String>();
List<Pair<Integer, String>> uiComponents = new ArrayList<Pair<Integer, String>>();
nativeGetAddressUiComponents(countryCode, componentIds, componentNames);
mCurrentBestLanguageCode =
nativeGetAddressUiComponents(countryCode, languageCode, componentIds,
componentNames);
for (int i = 0; i < componentIds.size(); i++) {
uiComponents.add(new Pair<Integer, String>(componentIds.get(i), componentNames.get(i)));
......@@ -120,6 +131,14 @@ public class AutofillProfileBridge {
return uiComponents;
}
/**
* @return The language code associated with the most recently retrieved address ui components.
* Will return null if getAddressUiComponents() has not been called yet.
*/
String getCurrentBestLanguageCode() {
return mCurrentBestLanguageCode;
}
@CalledByNative
private static void stringArrayToList(String[] array, List<String> list) {
for (String s : array) {
......@@ -137,6 +156,6 @@ public class AutofillProfileBridge {
private static native String nativeGetDefaultCountryCode();
private static native void nativeGetSupportedCountries(List<String> countryCodes,
List<String> countryNames);
private static native void nativeGetAddressUiComponents(String countryCode,
List<Integer> componentIds, List<String> componentNames);
private static native String nativeGetAddressUiComponents(String countryCode,
String languageCode, List<Integer> componentIds, List<String> componentNames);
}
......@@ -51,6 +51,8 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher,
private Spinner mCountriesSpinner;
private ViewGroup mWidgetRoot;
private FloatLabelLayout[] mAddressFields;
private AutofillProfileBridge mAutofillProfileBridge;
private boolean mUseSavedProfileLanguage;
@Override
public void onCreate(Bundle savedState) {
......@@ -86,6 +88,8 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher,
mWidgetRoot = (ViewGroup) v.findViewById(R.id.autofill_profile_widget_root);
mCountriesSpinner = (Spinner) v.findViewById(R.id.countries);
mAutofillProfileBridge = new AutofillProfileBridge();
populateCountriesSpinner();
createAndPopulateEditFields();
hookupSaveCancelDeleteButtons(v);
......@@ -123,6 +127,7 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher,
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position != mCurrentCountryPos) {
mCurrentCountryPos = position;
mUseSavedProfileLanguage = false;
// If all fields are empty (e.g. the user just entered the form and the first thing
// they did was select a country), focus on the first form element. Otherwise, don't.
resetFormFields(position, allFieldsEmpty());
......@@ -161,6 +166,7 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher,
}
mLanguageCodeString = profile.getLanguageCode();
mUseSavedProfileLanguage = true;
mCurrentCountryPos = mCountryCodes.indexOf(profile.getCountryCode());
if (mCurrentCountryPos == -1) {
......@@ -213,8 +219,12 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher,
mWidgetRoot.removeAllViews();
// Get address fields for the selected country.
List<Pair<Integer, String>> fields = AutofillProfileBridge.getAddressUiComponents(
mCountryCodes.get(countryCodeIndex));
List<Pair<Integer, String>> fields = mAutofillProfileBridge.getAddressUiComponents(
mCountryCodes.get(countryCodeIndex),
mLanguageCodeString);
if (!mUseSavedProfileLanguage) {
mLanguageCodeString = mAutofillProfileBridge.getCurrentBestLanguageCode();
}
// Create form fields and focus the first field if autoFocusFirstField is true.
boolean firstField = true;
......
......@@ -47,10 +47,10 @@ enum AddressField {
static jstring GetDefaultCountryCode(JNIEnv* env,
jclass clazz) {
std::string defaultCountryCode =
std::string default_country_code =
autofill::AutofillCountry::CountryCodeForLocale(
g_browser_process->GetApplicationLocale());
return ConvertUTF8ToJavaString(env, defaultCountryCode).Release();
return ConvertUTF8ToJavaString(env, default_country_code).Release();
}
static void GetSupportedCountries(JNIEnv* env,
......@@ -76,9 +76,10 @@ static void GetSupportedCountries(JNIEnv* env,
j_country_name_list);
}
static void GetAddressUiComponents(JNIEnv* env,
static jstring GetAddressUiComponents(JNIEnv* env,
jclass clazz,
jstring j_country_code,
jstring j_language_code,
jobject j_id_list,
jobject j_name_list) {
std::string best_language_tag;
......@@ -87,10 +88,18 @@ static void GetAddressUiComponents(JNIEnv* env,
::i18n::addressinput::Localization localization;
localization.SetGetter(l10n_util::GetStringUTF8);
std::string language_code;
if (j_language_code != NULL) {
language_code = ConvertJavaStringToUTF8(env, j_language_code);
}
if (language_code.empty()) {
language_code = g_browser_process->GetApplicationLocale();
}
std::vector<::i18n::addressinput::AddressUiComponent> ui_components =
::i18n::addressinput::BuildComponents(
ConvertJavaStringToUTF8(env, j_country_code), localization,
g_browser_process->GetApplicationLocale(), &best_language_tag);
language_code, &best_language_tag);
for (auto ui_component : ui_components) {
component_labels.push_back(ui_component.name);
......@@ -136,6 +145,8 @@ static void GetAddressUiComponents(JNIEnv* env,
ToJavaArrayOfStrings(
env, component_labels).obj(),
j_name_list);
return ConvertUTF8ToJavaString(env, best_language_tag).Release();
}
// static
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment