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