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) { |
Ben Murdoch | 381280d | 2011-08-11 10:39:59 +0100 | [diff] [blame] | 119 | Context c = getActivity(); |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 120 | switch (msg.what) { |
| 121 | case PROFILE_SAVED_MSG: |
Ben Murdoch | 381280d | 2011-08-11 10:39:59 +0100 | [diff] [blame] | 122 | if (c != null) { |
| 123 | Toast.makeText(c, R.string.autofill_profile_successful_save, |
| 124 | Toast.LENGTH_SHORT).show(); |
| 125 | closeEditor(); |
| 126 | } |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 127 | break; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 128 | |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 129 | case PROFILE_DELETED_MSG: |
Ben Murdoch | 381280d | 2011-08-11 10:39:59 +0100 | [diff] [blame] | 130 | if (c != null) { |
| 131 | Toast.makeText(c, R.string.autofill_profile_successful_delete, |
| 132 | Toast.LENGTH_SHORT).show(); |
| 133 | } |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | } |
| 137 | }; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void onCreate(Bundle savedState) { |
| 142 | super.onCreate(savedState); |
Ben Murdoch | 815752a | 2011-06-16 19:47:08 +0100 | [diff] [blame] | 143 | setHasOptionsMenu(true); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 144 | mSettings = BrowserSettings.getInstance(); |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
Ben Murdoch | 815752a | 2011-06-16 19:47:08 +0100 | [diff] [blame] | 148 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 149 | inflater.inflate(R.menu.autofill_profile_editor, menu); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public boolean onOptionsItemSelected(MenuItem item) { |
| 154 | if (item.getItemId() == R.id.autofill_profile_editor_delete_profile_menu_id) { |
| 155 | // Clear the UI. |
| 156 | mFullNameEdit.setText(""); |
| 157 | mEmailEdit.setText(""); |
| 158 | mCompanyEdit.setText(""); |
| 159 | mAddressLine1Edit.setText(""); |
| 160 | mAddressLine2Edit.setText(""); |
| 161 | mCityEdit.setText(""); |
| 162 | mStateEdit.setText(""); |
| 163 | mZipEdit.setText(""); |
| 164 | mCountryEdit.setText(""); |
| 165 | mPhoneEdit.setText(""); |
| 166 | |
| 167 | // Update browser settings and native with a null profile. This will |
| 168 | // trigger the current profile to get deleted from the DB. |
| 169 | mSettings.setAutoFillProfile(null, |
| 170 | mHandler.obtainMessage(PROFILE_DELETED_MSG)); |
| 171 | updateButtonState(); |
| 172 | return true; |
| 173 | } |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | @Override |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 178 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 179 | Bundle savedInstanceState) { |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 180 | View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false); |
| 181 | |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 182 | mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit); |
| 183 | mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit); |
| 184 | mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit); |
| 185 | mAddressLine1Edit = (EditText)v.findViewById( |
| 186 | R.id.autofill_profile_editor_address_line_1_edit); |
| 187 | mAddressLine2Edit = (EditText)v.findViewById( |
| 188 | R.id.autofill_profile_editor_address_line_2_edit); |
| 189 | mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit); |
| 190 | mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit); |
| 191 | mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit); |
| 192 | mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit); |
| 193 | mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit); |
| 194 | |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 195 | mFullNameEdit.addTextChangedListener(mFieldChangedListener); |
| 196 | mEmailEdit.addTextChangedListener(mFieldChangedListener); |
| 197 | mCompanyEdit.addTextChangedListener(mFieldChangedListener); |
| 198 | mAddressLine1Edit.addTextChangedListener(mFieldChangedListener); |
| 199 | mAddressLine2Edit.addTextChangedListener(mFieldChangedListener); |
| 200 | mCityEdit.addTextChangedListener(mFieldChangedListener); |
| 201 | mStateEdit.addTextChangedListener(mFieldChangedListener); |
| 202 | mZipEdit.addTextChangedListener(mFieldChangedListener); |
| 203 | mCountryEdit.addTextChangedListener(mFieldChangedListener); |
| 204 | mPhoneEdit.addTextChangedListener(new PhoneNumberValidator()); |
| 205 | |
| 206 | mSaveButton = (Button)v.findViewById(R.id.autofill_profile_editor_save_button); |
| 207 | mSaveButton.setOnClickListener(new OnClickListener() { |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 208 | public void onClick(View button) { |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 209 | AutoFillProfile newProfile = new AutoFillProfile( |
| 210 | mUniqueId, |
| 211 | mFullNameEdit.getText().toString(), |
| 212 | mEmailEdit.getText().toString(), |
| 213 | mCompanyEdit.getText().toString(), |
| 214 | mAddressLine1Edit.getText().toString(), |
| 215 | mAddressLine2Edit.getText().toString(), |
| 216 | mCityEdit.getText().toString(), |
| 217 | mStateEdit.getText().toString(), |
| 218 | mZipEdit.getText().toString(), |
| 219 | mCountryEdit.getText().toString(), |
| 220 | mPhoneEdit.getText().toString()); |
| 221 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 222 | mSettings.setAutoFillProfile(newProfile, |
Ben Murdoch | 23da30e | 2010-10-26 15:18:44 +0100 | [diff] [blame] | 223 | mHandler.obtainMessage(PROFILE_SAVED_MSG)); |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 224 | } |
| 225 | }); |
| 226 | |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 227 | // Populate the text boxes with any pre existing AutoFill data. |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 228 | AutoFillProfile activeProfile = mSettings.getAutoFillProfile(); |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame] | 229 | if (activeProfile != null) { |
| 230 | mFullNameEdit.setText(activeProfile.getFullName()); |
| 231 | mEmailEdit.setText(activeProfile.getEmailAddress()); |
| 232 | mCompanyEdit.setText(activeProfile.getCompanyName()); |
| 233 | mAddressLine1Edit.setText(activeProfile.getAddressLine1()); |
| 234 | mAddressLine2Edit.setText(activeProfile.getAddressLine2()); |
| 235 | mCityEdit.setText(activeProfile.getCity()); |
| 236 | mStateEdit.setText(activeProfile.getState()); |
| 237 | mZipEdit.setText(activeProfile.getZipCode()); |
| 238 | mCountryEdit.setText(activeProfile.getCountry()); |
| 239 | mPhoneEdit.setText(activeProfile.getPhoneNumber()); |
| 240 | } |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 241 | |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 242 | updateButtonState(); |
| 243 | |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 244 | return v; |
| 245 | } |
Ben Murdoch | 9d9306d | 2011-01-18 16:06:00 +0000 | [diff] [blame] | 246 | |
| 247 | public void updateButtonState() { |
| 248 | |
| 249 | boolean valid = (mFullNameEdit.getText().toString().length() > 0 || |
| 250 | mEmailEdit.getText().toString().length() > 0 || |
| 251 | mCompanyEdit.getText().toString().length() > 0 || |
| 252 | mAddressLine1Edit.getText().toString().length() > 0 || |
| 253 | mAddressLine2Edit.getText().toString().length() > 0 || |
| 254 | mCityEdit.getText().toString().length() > 0 || |
| 255 | mStateEdit.getText().toString().length() > 0 || |
| 256 | mZipEdit.getText().toString().length() > 0 || |
| 257 | mCountryEdit.getText().toString().length() > 0) && |
| 258 | mPhoneEdit.getError() == null; |
| 259 | |
| 260 | // Only enable the save buttons if we have at least one field completed |
| 261 | // and the phone number (if present is valid). |
| 262 | mSaveButton.setEnabled(valid); |
| 263 | } |
| 264 | |
| 265 | private void closeEditor() { |
| 266 | // Hide the IME if the user wants to close while an EditText has focus |
| 267 | InputMethodManager imm = (InputMethodManager) getActivity().getSystemService( |
| 268 | Context.INPUT_METHOD_SERVICE); |
| 269 | imm.hideSoftInputFromWindow(getView().getWindowToken(), 0); |
| 270 | if (getFragmentManager().getBackStackEntryCount() > 0) { |
| 271 | getFragmentManager().popBackStack(); |
| 272 | } else { |
| 273 | getActivity().finish(); |
| 274 | } |
| 275 | } |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 276 | } |