Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.Fragment; |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 20 | import android.content.Context; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 21 | import android.os.Bundle; |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 22 | import android.os.Handler; |
| 23 | import android.os.Message; |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 24 | import android.text.Editable; |
| 25 | import android.text.TextWatcher; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.view.View.OnClickListener; |
| 30 | import android.view.LayoutInflater; |
Ben Murdoch | 815752a | 2011-06-16 19:47:08 +0100 | [diff] [blame^] | 31 | import android.view.Menu; |
| 32 | import android.view.MenuInflater; |
| 33 | import android.view.MenuItem; |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 34 | import android.view.inputmethod.InputMethodManager; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 35 | import android.webkit.WebSettings.AutoFillProfile; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 36 | import android.widget.Button; |
| 37 | import android.widget.EditText; |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 38 | import android.widget.Toast; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 39 | |
| 40 | public class AutoFillSettingsFragment extends Fragment { |
| 41 | |
| 42 | private static final String LOGTAG = "AutoFillSettingsFragment"; |
| 43 | |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 44 | private EditText mFullNameEdit; |
| 45 | private EditText mEmailEdit; |
| 46 | private EditText mCompanyEdit; |
| 47 | private EditText mAddressLine1Edit; |
| 48 | private EditText mAddressLine2Edit; |
| 49 | private EditText mCityEdit; |
| 50 | private EditText mStateEdit; |
| 51 | private EditText mZipEdit; |
| 52 | private EditText mCountryEdit; |
| 53 | private EditText mPhoneEdit; |
| 54 | |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 55 | private Button mSaveButton; |
| 56 | |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 57 | // Used to display toast after DB interactions complete. |
| 58 | private Handler mHandler; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 59 | private BrowserSettings mSettings; |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 60 | |
| 61 | private final static int PROFILE_SAVED_MSG = 100; |
| 62 | private final static int PROFILE_DELETED_MSG = 101; |
| 63 | |
Ben Murdoch | 6fa32ba | 2010-10-20 14:01:25 +0100 | [diff] [blame] | 64 | // For now we support just one profile so it's safe to hardcode the |
| 65 | // id to 1 here. In the future this unique identifier will be set |
| 66 | // dynamically. |
| 67 | private int mUniqueId = 1; |
| 68 | |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 69 | private class PhoneNumberValidator implements TextWatcher { |
| 70 | // Keep in sync with kPhoneNumberLength in chrome/browser/autofill/phone_number.cc |
| 71 | private static final int PHONE_NUMBER_LENGTH = 7; |
Ben Murdoch | 366824d | 2011-01-18 19:42:08 +0000 | [diff] [blame] | 72 | private static final String PHONE_NUMBER_SEPARATORS_REGEX = "[\\s\\.\\(\\)-]"; |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 73 | |
| 74 | public void afterTextChanged(Editable s) { |
Ben Murdoch | 366824d | 2011-01-18 19:42:08 +0000 | [diff] [blame] | 75 | String phoneNumber = s.toString(); |
| 76 | int phoneNumberLength = phoneNumber.length(); |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 77 | |
Ben Murdoch | 366824d | 2011-01-18 19:42:08 +0000 | [diff] [blame] | 78 | // Strip out any phone number separators. |
| 79 | phoneNumber = phoneNumber.replaceAll(PHONE_NUMBER_SEPARATORS_REGEX, ""); |
| 80 | |
| 81 | int strippedPhoneNumberLength = phoneNumber.length(); |
| 82 | |
| 83 | if (phoneNumberLength > 0 && strippedPhoneNumberLength < PHONE_NUMBER_LENGTH) { |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 84 | mPhoneEdit.setError(getResources().getText( |
| 85 | R.string.autofill_profile_editor_phone_number_invalid)); |
| 86 | } else { |
| 87 | mPhoneEdit.setError(null); |
| 88 | } |
| 89 | |
| 90 | updateButtonState(); |
| 91 | } |
| 92 | |
| 93 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 94 | } |
| 95 | |
| 96 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private class FieldChangedListener implements TextWatcher { |
| 101 | public void afterTextChanged(Editable s) { |
| 102 | updateButtonState(); |
| 103 | } |
| 104 | |
| 105 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 106 | } |
| 107 | |
| 108 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | private TextWatcher mFieldChangedListener = new FieldChangedListener(); |
| 114 | |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 115 | public AutoFillSettingsFragment() { |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 116 | mHandler = new Handler() { |
| 117 | @Override |
| 118 | public void handleMessage(Message msg) { |
| 119 | switch (msg.what) { |
| 120 | case PROFILE_SAVED_MSG: |
| 121 | Toast.makeText(getActivity(), R.string.autofill_profile_successful_save, |
| 122 | Toast.LENGTH_SHORT).show(); |
| 123 | break; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 124 | |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 125 | case PROFILE_DELETED_MSG: |
| 126 | Toast.makeText(getActivity(), R.string.autofill_profile_successful_delete, |
| 127 | Toast.LENGTH_SHORT).show(); |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | }; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void onCreate(Bundle savedState) { |
| 136 | super.onCreate(savedState); |
Ben Murdoch | 815752a | 2011-06-16 19:47:08 +0100 | [diff] [blame^] | 137 | setHasOptionsMenu(true); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 138 | mSettings = BrowserSettings.getInstance(); |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
Ben Murdoch | 815752a | 2011-06-16 19:47:08 +0100 | [diff] [blame^] | 142 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 143 | inflater.inflate(R.menu.autofill_profile_editor, menu); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public boolean onOptionsItemSelected(MenuItem item) { |
| 148 | if (item.getItemId() == R.id.autofill_profile_editor_delete_profile_menu_id) { |
| 149 | // Clear the UI. |
| 150 | mFullNameEdit.setText(""); |
| 151 | mEmailEdit.setText(""); |
| 152 | mCompanyEdit.setText(""); |
| 153 | mAddressLine1Edit.setText(""); |
| 154 | mAddressLine2Edit.setText(""); |
| 155 | mCityEdit.setText(""); |
| 156 | mStateEdit.setText(""); |
| 157 | mZipEdit.setText(""); |
| 158 | mCountryEdit.setText(""); |
| 159 | mPhoneEdit.setText(""); |
| 160 | |
| 161 | // Update browser settings and native with a null profile. This will |
| 162 | // trigger the current profile to get deleted from the DB. |
| 163 | mSettings.setAutoFillProfile(null, |
| 164 | mHandler.obtainMessage(PROFILE_DELETED_MSG)); |
| 165 | updateButtonState(); |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | @Override |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 172 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 173 | Bundle savedInstanceState) { |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 174 | View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false); |
| 175 | |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 176 | mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit); |
| 177 | mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit); |
| 178 | mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit); |
| 179 | mAddressLine1Edit = (EditText)v.findViewById( |
| 180 | R.id.autofill_profile_editor_address_line_1_edit); |
| 181 | mAddressLine2Edit = (EditText)v.findViewById( |
| 182 | R.id.autofill_profile_editor_address_line_2_edit); |
| 183 | mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit); |
| 184 | mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit); |
| 185 | mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit); |
| 186 | mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit); |
| 187 | mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit); |
| 188 | |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 189 | mFullNameEdit.addTextChangedListener(mFieldChangedListener); |
| 190 | mEmailEdit.addTextChangedListener(mFieldChangedListener); |
| 191 | mCompanyEdit.addTextChangedListener(mFieldChangedListener); |
| 192 | mAddressLine1Edit.addTextChangedListener(mFieldChangedListener); |
| 193 | mAddressLine2Edit.addTextChangedListener(mFieldChangedListener); |
| 194 | mCityEdit.addTextChangedListener(mFieldChangedListener); |
| 195 | mStateEdit.addTextChangedListener(mFieldChangedListener); |
| 196 | mZipEdit.addTextChangedListener(mFieldChangedListener); |
| 197 | mCountryEdit.addTextChangedListener(mFieldChangedListener); |
| 198 | mPhoneEdit.addTextChangedListener(new PhoneNumberValidator()); |
| 199 | |
| 200 | mSaveButton = (Button)v.findViewById(R.id.autofill_profile_editor_save_button); |
| 201 | mSaveButton.setOnClickListener(new OnClickListener() { |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 202 | public void onClick(View button) { |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 203 | AutoFillProfile newProfile = new AutoFillProfile( |
| 204 | mUniqueId, |
| 205 | mFullNameEdit.getText().toString(), |
| 206 | mEmailEdit.getText().toString(), |
| 207 | mCompanyEdit.getText().toString(), |
| 208 | mAddressLine1Edit.getText().toString(), |
| 209 | mAddressLine2Edit.getText().toString(), |
| 210 | mCityEdit.getText().toString(), |
| 211 | mStateEdit.getText().toString(), |
| 212 | mZipEdit.getText().toString(), |
| 213 | mCountryEdit.getText().toString(), |
| 214 | mPhoneEdit.getText().toString()); |
| 215 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 216 | mSettings.setAutoFillProfile(newProfile, |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 217 | mHandler.obtainMessage(PROFILE_SAVED_MSG)); |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 218 | closeEditor(); |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 219 | } |
| 220 | }); |
| 221 | |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 222 | // Populate the text boxes with any pre existing AutoFill data. |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 223 | AutoFillProfile activeProfile = mSettings.getAutoFillProfile(); |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 224 | if (activeProfile != null) { |
| 225 | mFullNameEdit.setText(activeProfile.getFullName()); |
| 226 | mEmailEdit.setText(activeProfile.getEmailAddress()); |
| 227 | mCompanyEdit.setText(activeProfile.getCompanyName()); |
| 228 | mAddressLine1Edit.setText(activeProfile.getAddressLine1()); |
| 229 | mAddressLine2Edit.setText(activeProfile.getAddressLine2()); |
| 230 | mCityEdit.setText(activeProfile.getCity()); |
| 231 | mStateEdit.setText(activeProfile.getState()); |
| 232 | mZipEdit.setText(activeProfile.getZipCode()); |
| 233 | mCountryEdit.setText(activeProfile.getCountry()); |
| 234 | mPhoneEdit.setText(activeProfile.getPhoneNumber()); |
| 235 | } |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 236 | |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 237 | updateButtonState(); |
| 238 | |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 239 | return v; |
| 240 | } |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 241 | |
| 242 | public void updateButtonState() { |
| 243 | |
| 244 | boolean valid = (mFullNameEdit.getText().toString().length() > 0 || |
| 245 | mEmailEdit.getText().toString().length() > 0 || |
| 246 | mCompanyEdit.getText().toString().length() > 0 || |
| 247 | mAddressLine1Edit.getText().toString().length() > 0 || |
| 248 | mAddressLine2Edit.getText().toString().length() > 0 || |
| 249 | mCityEdit.getText().toString().length() > 0 || |
| 250 | mStateEdit.getText().toString().length() > 0 || |
| 251 | mZipEdit.getText().toString().length() > 0 || |
| 252 | mCountryEdit.getText().toString().length() > 0) && |
| 253 | mPhoneEdit.getError() == null; |
| 254 | |
| 255 | // Only enable the save buttons if we have at least one field completed |
| 256 | // and the phone number (if present is valid). |
| 257 | mSaveButton.setEnabled(valid); |
| 258 | } |
| 259 | |
| 260 | private void closeEditor() { |
| 261 | // Hide the IME if the user wants to close while an EditText has focus |
| 262 | InputMethodManager imm = (InputMethodManager) getActivity().getSystemService( |
| 263 | Context.INPUT_METHOD_SERVICE); |
| 264 | imm.hideSoftInputFromWindow(getView().getWindowToken(), 0); |
| 265 | if (getFragmentManager().getBackStackEntryCount() > 0) { |
| 266 | getFragmentManager().popBackStack(); |
| 267 | } else { |
| 268 | getActivity().finish(); |
| 269 | } |
| 270 | } |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 271 | } |