Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/preferences/autofill/AutofillProfileEditor.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/autofill/AutofillProfileEditor.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/autofill/AutofillProfileEditor.java |
| index ce69ae098bcdd9bfb081506686314d6ed4f8658c..b19676dadcd9b78695e144b3a2769796c0d8398c 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/autofill/AutofillProfileEditor.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/autofill/AutofillProfileEditor.java |
| @@ -7,26 +7,29 @@ package org.chromium.chrome.browser.preferences.autofill; |
| import android.app.Fragment; |
| import android.os.Bundle; |
| import android.text.Editable; |
| +import android.text.InputType; |
| import android.text.TextUtils; |
| import android.text.TextWatcher; |
| +import android.util.Pair; |
| import android.view.LayoutInflater; |
| import android.view.View; |
| import android.view.ViewGroup; |
| import android.widget.AdapterView; |
| import android.widget.AdapterView.OnItemSelectedListener; |
| +import android.widget.ArrayAdapter; |
| import android.widget.Button; |
| import android.widget.EditText; |
| import android.widget.Spinner; |
| -import com.android.i18n.addressinput.AddressData; |
| -import com.android.i18n.addressinput.AddressField; |
| -import com.android.i18n.addressinput.AddressWidget; |
| -import com.android.i18n.addressinput.FormOptions; |
| -import com.android.i18n.addressinput.SimpleClientCacheManager; |
| - |
| import org.chromium.chrome.R; |
| import org.chromium.chrome.browser.autofill.PersonalDataManager; |
| import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; |
| +import org.chromium.chrome.browser.widget.FloatLabelLayout; |
| + |
| +import java.util.ArrayList; |
| +import java.util.HashMap; |
| +import java.util.List; |
| +import java.util.Locale; |
| /** |
| * Provides the Java-ui for editing a Profile autofill entry. |
| @@ -37,11 +40,17 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher, |
| // May be the empty string if creating a new profile. |
| private String mGUID; |
| - private AddressWidget mAddressWidget; |
| private boolean mNoCountryItemIsSelected; |
| + private boolean mSaveButtonEnabled; |
| + private LayoutInflater mInflater; |
| private EditText mPhoneText; |
| private EditText mEmailText; |
| private String mLanguageCodeString; |
| + private List<String> mCountries; |
| + private int mCurrentCountryPos; |
| + private Spinner mCountriesSpinner; |
| + private ViewGroup mWidgetRoot; |
| + private HashMap<String, EditText> mAddressFields; |
| @Override |
| public void onCreate(Bundle savedState) { |
| @@ -53,11 +62,6 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher, |
| Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| - View v = inflater.inflate(R.layout.autofill_profile_editor, container, false); |
| - |
| - mPhoneText = (EditText) v.findViewById(R.id.autofill_profile_editor_phone_number_edit); |
| - mEmailText = (EditText) v.findViewById(R.id.autofill_profile_editor_email_address_edit); |
| - |
| // We know which profile to edit based on the GUID stuffed in |
| // our extras by AutofillPreferences. |
| Bundle extras = getArguments(); |
| @@ -71,8 +75,19 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher, |
| getActivity().setTitle(R.string.autofill_edit_profile); |
| } |
| - addProfileDataToEditFields(v); |
| + mInflater = inflater; |
| + mAddressFields = new HashMap<String, EditText>(); |
| + View v = mInflater.inflate(R.layout.autofill_profile_editor, container, false); |
| + |
| + mPhoneText = (EditText) v.findViewById(R.id.autofill_profile_editor_phone_number_edit); |
| + mEmailText = (EditText) v.findViewById(R.id.autofill_profile_editor_email_address_edit); |
| + mWidgetRoot = (ViewGroup) v.findViewById(R.id.autofill_profile_widget_root); |
| + mCountriesSpinner = (Spinner) v.findViewById(R.id.countries); |
| + |
| + populateCountriesSpinner(); |
| + createAndPopulateEditFields(v); |
| hookupSaveCancelDeleteButtons(v); |
| + |
| return v; |
| } |
| @@ -84,26 +99,57 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher, |
| @Override |
| public void onTextChanged(CharSequence s, int start, int before, int count) { |
|
newt (away)
2015/01/27 01:45:06
The current logic seems unnecessarily tricky. How
Theresa
2015/01/28 02:05:40
Done.
|
| - enableSaveButton(); |
| + if (mNoCountryItemIsSelected && mSaveButtonEnabled && TextUtils.isEmpty(s)) { |
| + if (allFieldsEmpty()) { |
| + enableSaveButton(false); |
| + } |
| + } else if (!mSaveButtonEnabled) { |
| + enableSaveButton(true); |
| + } |
| + } |
| + |
| + private boolean allFieldsEmpty() { |
| + if (!TextUtils.isEmpty(mPhoneText.getText()) |
| + || !TextUtils.isEmpty(mEmailText.getText())) { |
| + return false; |
| + } |
| + for (String fieldId : mAddressFields.keySet()) { |
|
newt (away)
2015/01/27 01:45:06
You can iterate directly over the HashMap values:
Theresa
2015/01/28 02:05:40
Done.
|
| + if (!TextUtils.isEmpty(mAddressFields.get(fieldId).getText())) { |
| + return false; |
| + } |
| + } |
| + |
| + return true; |
| } |
| @Override |
| public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| - mAddressWidget.onItemSelected(parent, view, position, id); |
| - |
| - if (mNoCountryItemIsSelected) { |
| + if (position != mCurrentCountryPos) { |
| + mCurrentCountryPos = position; |
| + resetFormFields(position); |
| + mWidgetRoot.requestFocus(); |
|
newt (away)
2015/01/27 01:45:06
what happens if you omit mWidgetRoot.requestFocus(
Theresa
2015/01/28 02:05:40
The first EditText box doesn't get focused if this
|
| mNoCountryItemIsSelected = false; |
| - return; |
| + enableSaveButton(true); |
| } |
| - |
| - enableSaveButton(); |
| } |
| @Override |
| public void onNothingSelected(AdapterView<?> parent) {} |
| - private void addProfileDataToEditFields(View v) { |
| - AddressData.Builder address = new AddressData.Builder(); |
| + private void populateCountriesSpinner() { |
| + mCountries = AutofillProfileBridge.getAvailableCountries(); |
| + List<String> countryNames = new ArrayList<String>(); |
| + for (String country : mCountries) { |
| + countryNames.add(new Locale("", country).getDisplayCountry(Locale.getDefault())); |
| + } |
| + |
| + ArrayAdapter<String> countriesAdapter = new ArrayAdapter<String>(getActivity(), |
| + android.R.layout.simple_spinner_item, countryNames); |
| + countriesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
| + mCountriesSpinner.setAdapter(countriesAdapter); |
| + } |
| + |
| + private void createAndPopulateEditFields(View v) { |
| AutofillProfile profile = PersonalDataManager.getInstance().getProfile(mGUID); |
| if (profile != null) { |
| @@ -111,51 +157,88 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher, |
| mEmailText.setText(profile.getEmailAddress()); |
| mLanguageCodeString = profile.getLanguageCode(); |
| - address.setAdminArea(profile.getRegion()); |
| - address.setLocality(profile.getLocality()); |
| - address.setRecipient(profile.getFullName()); |
| - address.setOrganization(profile.getCompanyName()); |
| - address.setDependentLocality(profile.getDependentLocality()); |
| - address.setPostalCode(profile.getPostalCode()); |
| - address.setSortingCode(profile.getSortingCode()); |
| - address.setAddress(profile.getStreetAddress()); |
| - address.setCountry(profile.getCountryCode()); |
| + mCurrentCountryPos = mCountries.indexOf(profile.getCountryCode()); |
| + resetFormFields(mCurrentCountryPos); |
|
newt (away)
2015/01/27 01:45:06
what happens if mCurrentCountryPos is -1?
Theresa
2015/01/28 02:05:41
Currently, it would probably throw errors. I don't
|
| + |
| + setFieldText("admin_area", profile.getRegion()); |
|
newt (away)
2015/01/27 01:45:06
These strings should be turned into constants. Ide
Theresa
2015/01/28 02:05:41
Done.
|
| + setFieldText("locality", profile.getLocality()); |
| + setFieldText("dependent_locality", profile.getDependentLocality()); |
| + setFieldText("sorting_code", profile.getSortingCode()); |
| + setFieldText("postal_code", profile.getPostalCode()); |
| + setFieldText("street_address", profile.getStreetAddress()); |
| + setFieldText("organization", profile.getCompanyName()); |
| + setFieldText("recipient", profile.getFullName()); |
| + } else { |
| + mCurrentCountryPos = mCountries.indexOf(Locale.getDefault().getCountry()); |
| + resetFormFields(mCurrentCountryPos); |
| + mWidgetRoot.requestFocus(); |
| } |
| + } |
| + |
| + private void resetFormFields(int countryCodeIndex) { |
| + mWidgetRoot.removeAllViews(); |
| + mAddressFields.clear(); |
| + |
| + mCountriesSpinner.setSelection(countryCodeIndex); |
| + List<Pair<String, String>> fields = AutofillProfileBridge.getAddressUiComponents( |
| + mCountries.get(countryCodeIndex), Locale.getDefault().toString()); |
| + |
| + for (Pair<String, String> field : fields) { |
| + String fieldId = field.first; |
| + String fieldLabel = field.second; |
| + FloatLabelLayout fieldFloatLabel = (FloatLabelLayout) mInflater.inflate( |
| + R.layout.preference_float_label_layout, mWidgetRoot, false); |
| - ViewGroup widgetRoot = (ViewGroup) v.findViewById(R.id.autofill_profile_widget_root); |
| - mAddressWidget = new AddressWidget(getActivity(), widgetRoot, |
| - (new FormOptions.Builder()).build(), |
| - new SimpleClientCacheManager(), |
| - address.build(), |
| - new ChromeAddressWidgetUiComponentProvider(getActivity())); |
| + EditText fieldEditText = new EditText(getActivity()); |
|
newt (away)
2015/01/27 01:45:06
You could add this EditText to the preference_floa
Theresa
2015/01/28 02:05:41
Done.
|
| + fieldEditText.setHint(fieldLabel); |
| + fieldEditText.setContentDescription(fieldLabel); |
| + fieldEditText.setSingleLine(true); |
| + // equivalent to android:inputType="textPostalAddress | textAllCaps" |
| + fieldEditText.setInputType(InputType.TYPE_CLASS_TEXT |
| + | InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS |
|
newt (away)
2015/01/27 01:45:06
Should all fields really be treated as postal addr
Theresa
2015/01/28 02:05:41
Yes, it is (see inputType on this file: https://cs
|
| + | InputType.TYPE_TEXT_FLAG_CAP_WORDS); |
| + fieldEditText.addTextChangedListener(this); |
| - if (profile == null) { |
| - widgetRoot.requestFocus(); |
| + mAddressFields.put(fieldId, fieldEditText); |
| + fieldFloatLabel.addView(fieldEditText); |
| + mWidgetRoot.addView(fieldFloatLabel); |
| } |
| } |
| // Read edited data; save in the associated Chrome profile. |
| // Ignore empty fields. |
| private void saveProfile() { |
| - AddressData input = mAddressWidget.getAddressData(); |
| AutofillProfile profile = new PersonalDataManager.AutofillProfile( |
| mGUID, |
| AutofillPreferences.SETTINGS_ORIGIN, |
| - input.getRecipient(), |
| - input.getOrganization(), |
| - TextUtils.join("\n", input.getAddressLines()), |
| - input.getAdministrativeArea(), |
| - input.getLocality(), |
| - input.getDependentLocality(), |
| - input.getPostalCode(), |
| - input.getSortingCode(), |
| - input.getPostalCountry(), |
| + getFieldText("recipient"), |
| + getFieldText("orgnaization"), |
|
newt (away)
2015/01/27 01:45:06
typo :/ That's why these constants should be defi
Theresa
2015/01/28 02:05:41
Acknowledged.
|
| + getFieldText("street_address"), |
| + getFieldText("admin_area"), |
| + getFieldText("locality"), |
| + getFieldText("dependent_locality"), |
| + getFieldText("postal_code"), |
| + getFieldText("sorting_code"), |
| + mCountries.get(mCurrentCountryPos), |
| mPhoneText.getText().toString(), |
| mEmailText.getText().toString(), |
| mLanguageCodeString); |
| PersonalDataManager.getInstance().setProfile(profile); |
| } |
| + private String getFieldText(String fieldId) { |
| + if (mAddressFields.containsKey(fieldId)) { |
| + return mAddressFields.get(fieldId).getText().toString(); |
| + } |
| + return null; |
| + } |
| + |
| + private void setFieldText(String fieldId, String text) { |
| + if (mAddressFields.containsKey(fieldId)) { |
| + mAddressFields.get(fieldId).setText(text); |
| + } |
| + } |
| + |
| private void deleteProfile() { |
| if (AutofillProfileEditor.this.mGUID != null) { |
| PersonalDataManager.getInstance().deleteProfile(mGUID); |
| @@ -196,20 +279,15 @@ public class AutofillProfileEditor extends Fragment implements TextWatcher, |
| // Listen for changes to inputs. Enable the save button after something has changed. |
| mPhoneText.addTextChangedListener(this); |
| mEmailText.addTextChangedListener(this); |
| + mCountriesSpinner.setOnItemSelectedListener(this); |
| mNoCountryItemIsSelected = true; |
| - |
| - for (AddressField field : AddressField.values()) { |
| - View input = mAddressWidget.getViewForField(field); |
| - if (input instanceof EditText) { |
| - ((EditText) input).addTextChangedListener(this); |
| - } else if (input instanceof Spinner) { |
| - ((Spinner) input).setOnItemSelectedListener(this); |
| - } |
| - } |
| } |
| - private void enableSaveButton() { |
| - Button button = (Button) getView().findViewById(R.id.autofill_profile_save); |
| - button.setEnabled(true); |
| + private void enableSaveButton(boolean enabled) { |
| + if (getView() != null) { |
| + Button button = (Button) getView().findViewById(R.id.autofill_profile_save); |
| + button.setEnabled(enabled); |
| + mSaveButtonEnabled = enabled; |
| + } |
| } |
| } |