blob: 7be657dd42c52413ebeb51e41a5609a97a31086a [file] [log] [blame]
Ben Murdochaf554522010-09-10 22:09:30 +01001/*
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
17package com.android.browser;
18
19import android.app.Fragment;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000020import android.content.Context;
Ben Murdochaf554522010-09-10 22:09:30 +010021import android.os.Bundle;
Ben Murdoch23da30e2010-10-26 15:18:44 +010022import android.os.Handler;
23import android.os.Message;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000024import android.text.Editable;
25import android.text.TextWatcher;
Ben Murdochaf554522010-09-10 22:09:30 +010026import android.util.Log;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.View.OnClickListener;
30import android.view.LayoutInflater;
Ben Murdoch815752a2011-06-16 19:47:08 +010031import android.view.Menu;
32import android.view.MenuInflater;
33import android.view.MenuItem;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000034import android.view.inputmethod.InputMethodManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010035import android.webkit.WebSettings.AutoFillProfile;
Ben Murdochaf554522010-09-10 22:09:30 +010036import android.widget.Button;
37import android.widget.EditText;
Ben Murdoch36a23dd2010-10-13 13:20:06 +010038import android.widget.Toast;
Ben Murdochaf554522010-09-10 22:09:30 +010039
40public class AutoFillSettingsFragment extends Fragment {
41
42 private static final String LOGTAG = "AutoFillSettingsFragment";
43
Ben Murdoch36a23dd2010-10-13 13:20:06 +010044 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 Murdoche4c59f92011-10-18 12:01:07 +010055 private MenuItem mSaveMenuItem;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000056
Ben Murdoch6f336402011-10-27 13:57:33 +010057 private boolean mInitialised;
58
Ben Murdoch23da30e2010-10-26 15:18:44 +010059 // Used to display toast after DB interactions complete.
60 private Handler mHandler;
John Reck35e9dd62011-04-25 09:01:54 -070061 private BrowserSettings mSettings;
Ben Murdoch23da30e2010-10-26 15:18:44 +010062
63 private final static int PROFILE_SAVED_MSG = 100;
64 private final static int PROFILE_DELETED_MSG = 101;
65
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010066 // For now we support just one profile so it's safe to hardcode the
67 // id to 1 here. In the future this unique identifier will be set
68 // dynamically.
69 private int mUniqueId = 1;
70
Ben Murdoch9d9306d2011-01-18 16:06:00 +000071 private class PhoneNumberValidator implements TextWatcher {
72 // Keep in sync with kPhoneNumberLength in chrome/browser/autofill/phone_number.cc
73 private static final int PHONE_NUMBER_LENGTH = 7;
Ben Murdoch366824d2011-01-18 19:42:08 +000074 private static final String PHONE_NUMBER_SEPARATORS_REGEX = "[\\s\\.\\(\\)-]";
Ben Murdoch9d9306d2011-01-18 16:06:00 +000075
76 public void afterTextChanged(Editable s) {
Ben Murdoch366824d2011-01-18 19:42:08 +000077 String phoneNumber = s.toString();
78 int phoneNumberLength = phoneNumber.length();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000079
Ben Murdoch366824d2011-01-18 19:42:08 +000080 // Strip out any phone number separators.
81 phoneNumber = phoneNumber.replaceAll(PHONE_NUMBER_SEPARATORS_REGEX, "");
82
83 int strippedPhoneNumberLength = phoneNumber.length();
84
85 if (phoneNumberLength > 0 && strippedPhoneNumberLength < PHONE_NUMBER_LENGTH) {
Ben Murdoch9d9306d2011-01-18 16:06:00 +000086 mPhoneEdit.setError(getResources().getText(
87 R.string.autofill_profile_editor_phone_number_invalid));
88 } else {
89 mPhoneEdit.setError(null);
90 }
91
Ben Murdoche4c59f92011-10-18 12:01:07 +010092 updateSaveMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000093 }
94
95 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
96 }
97
98 public void onTextChanged(CharSequence s, int start, int before, int count) {
99 }
100 }
101
102 private class FieldChangedListener implements TextWatcher {
103 public void afterTextChanged(Editable s) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100104 updateSaveMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000105 }
106
107 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
108 }
109
110 public void onTextChanged(CharSequence s, int start, int before, int count) {
111 }
112
113 }
114
115 private TextWatcher mFieldChangedListener = new FieldChangedListener();
116
Ben Murdochaf554522010-09-10 22:09:30 +0100117 public AutoFillSettingsFragment() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100118 mHandler = new Handler() {
119 @Override
120 public void handleMessage(Message msg) {
Ben Murdoch381280d2011-08-11 10:39:59 +0100121 Context c = getActivity();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100122 switch (msg.what) {
123 case PROFILE_SAVED_MSG:
Ben Murdoch381280d2011-08-11 10:39:59 +0100124 if (c != null) {
125 Toast.makeText(c, R.string.autofill_profile_successful_save,
126 Toast.LENGTH_SHORT).show();
127 closeEditor();
128 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100129 break;
Ben Murdochaf554522010-09-10 22:09:30 +0100130
Ben Murdoch23da30e2010-10-26 15:18:44 +0100131 case PROFILE_DELETED_MSG:
Ben Murdoch381280d2011-08-11 10:39:59 +0100132 if (c != null) {
133 Toast.makeText(c, R.string.autofill_profile_successful_delete,
134 Toast.LENGTH_SHORT).show();
135 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100136 break;
137 }
138 }
139 };
Ben Murdochaf554522010-09-10 22:09:30 +0100140 }
141
142 @Override
143 public void onCreate(Bundle savedState) {
144 super.onCreate(savedState);
Ben Murdoch815752a2011-06-16 19:47:08 +0100145 setHasOptionsMenu(true);
John Reck35e9dd62011-04-25 09:01:54 -0700146 mSettings = BrowserSettings.getInstance();
Ben Murdochaf554522010-09-10 22:09:30 +0100147 }
148
149 @Override
Ben Murdoch815752a2011-06-16 19:47:08 +0100150 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
151 inflater.inflate(R.menu.autofill_profile_editor, menu);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100152 mSaveMenuItem = menu.findItem(R.id.autofill_profile_editor_save_profile_menu_id);
153 updateSaveMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100154 }
155
156 @Override
157 public boolean onOptionsItemSelected(MenuItem item) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100158 switch (item.getItemId()) {
159 case R.id.autofill_profile_editor_delete_profile_menu_id:
Ben Murdoch815752a2011-06-16 19:47:08 +0100160 // Clear the UI.
161 mFullNameEdit.setText("");
162 mEmailEdit.setText("");
163 mCompanyEdit.setText("");
164 mAddressLine1Edit.setText("");
165 mAddressLine2Edit.setText("");
166 mCityEdit.setText("");
167 mStateEdit.setText("");
168 mZipEdit.setText("");
169 mCountryEdit.setText("");
170 mPhoneEdit.setText("");
171
172 // Update browser settings and native with a null profile. This will
173 // trigger the current profile to get deleted from the DB.
174 mSettings.setAutoFillProfile(null,
175 mHandler.obtainMessage(PROFILE_DELETED_MSG));
Ben Murdoche4c59f92011-10-18 12:01:07 +0100176 updateSaveMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100177 return true;
Ben Murdoche4c59f92011-10-18 12:01:07 +0100178
179 case R.id.autofill_profile_editor_save_profile_menu_id:
180 AutoFillProfile newProfile = new AutoFillProfile(
181 mUniqueId,
182 mFullNameEdit.getText().toString(),
183 mEmailEdit.getText().toString(),
184 mCompanyEdit.getText().toString(),
185 mAddressLine1Edit.getText().toString(),
186 mAddressLine2Edit.getText().toString(),
187 mCityEdit.getText().toString(),
188 mStateEdit.getText().toString(),
189 mZipEdit.getText().toString(),
190 mCountryEdit.getText().toString(),
191 mPhoneEdit.getText().toString());
192
193 mSettings.setAutoFillProfile(newProfile,
194 mHandler.obtainMessage(PROFILE_SAVED_MSG));
195 return true;
196
197 default:
198 return false;
Ben Murdoch815752a2011-06-16 19:47:08 +0100199 }
Ben Murdoch815752a2011-06-16 19:47:08 +0100200 }
201
202 @Override
Ben Murdochaf554522010-09-10 22:09:30 +0100203 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100204 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +0100205 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
206
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100207 mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit);
208 mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit);
209 mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit);
210 mAddressLine1Edit = (EditText)v.findViewById(
211 R.id.autofill_profile_editor_address_line_1_edit);
212 mAddressLine2Edit = (EditText)v.findViewById(
213 R.id.autofill_profile_editor_address_line_2_edit);
214 mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit);
215 mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit);
216 mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit);
217 mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit);
218 mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit);
219
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000220 mFullNameEdit.addTextChangedListener(mFieldChangedListener);
221 mEmailEdit.addTextChangedListener(mFieldChangedListener);
222 mCompanyEdit.addTextChangedListener(mFieldChangedListener);
223 mAddressLine1Edit.addTextChangedListener(mFieldChangedListener);
224 mAddressLine2Edit.addTextChangedListener(mFieldChangedListener);
225 mCityEdit.addTextChangedListener(mFieldChangedListener);
226 mStateEdit.addTextChangedListener(mFieldChangedListener);
227 mZipEdit.addTextChangedListener(mFieldChangedListener);
228 mCountryEdit.addTextChangedListener(mFieldChangedListener);
229 mPhoneEdit.addTextChangedListener(new PhoneNumberValidator());
230
Ben Murdoch0cb81892010-10-08 12:41:33 +0100231 // Populate the text boxes with any pre existing AutoFill data.
John Reck35e9dd62011-04-25 09:01:54 -0700232 AutoFillProfile activeProfile = mSettings.getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100233 if (activeProfile != null) {
234 mFullNameEdit.setText(activeProfile.getFullName());
235 mEmailEdit.setText(activeProfile.getEmailAddress());
236 mCompanyEdit.setText(activeProfile.getCompanyName());
237 mAddressLine1Edit.setText(activeProfile.getAddressLine1());
238 mAddressLine2Edit.setText(activeProfile.getAddressLine2());
239 mCityEdit.setText(activeProfile.getCity());
240 mStateEdit.setText(activeProfile.getState());
241 mZipEdit.setText(activeProfile.getZipCode());
242 mCountryEdit.setText(activeProfile.getCountry());
243 mPhoneEdit.setText(activeProfile.getPhoneNumber());
244 }
Ben Murdochaf554522010-09-10 22:09:30 +0100245
Ben Murdoch6f336402011-10-27 13:57:33 +0100246 mInitialised = true;
247
Ben Murdoche4c59f92011-10-18 12:01:07 +0100248 updateSaveMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000249
Ben Murdochaf554522010-09-10 22:09:30 +0100250 return v;
251 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000252
Ben Murdoche4c59f92011-10-18 12:01:07 +0100253 private void updateSaveMenuItemState() {
254 if (mSaveMenuItem == null) {
255 return;
256 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000257
Ben Murdoch6f336402011-10-27 13:57:33 +0100258 if (!mInitialised) {
259 mSaveMenuItem.setEnabled(false);
260 return;
261 }
262
Ben Murdoche4c59f92011-10-18 12:01:07 +0100263 boolean currentState = mSaveMenuItem.isEnabled();
264 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000265 mEmailEdit.getText().toString().length() > 0 ||
266 mCompanyEdit.getText().toString().length() > 0 ||
267 mAddressLine1Edit.getText().toString().length() > 0 ||
268 mAddressLine2Edit.getText().toString().length() > 0 ||
269 mCityEdit.getText().toString().length() > 0 ||
270 mStateEdit.getText().toString().length() > 0 ||
271 mZipEdit.getText().toString().length() > 0 ||
272 mCountryEdit.getText().toString().length() > 0) &&
273 mPhoneEdit.getError() == null;
274
Ben Murdoche4c59f92011-10-18 12:01:07 +0100275 if (currentState != newState) {
276 mSaveMenuItem.setEnabled(newState);
277 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000278 }
279
280 private void closeEditor() {
281 // Hide the IME if the user wants to close while an EditText has focus
282 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
283 Context.INPUT_METHOD_SERVICE);
284 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
285 if (getFragmentManager().getBackStackEntryCount() > 0) {
286 getFragmentManager().popBackStack();
287 } else {
288 getActivity().finish();
289 }
290 }
Ben Murdochaf554522010-09-10 22:09:30 +0100291}