blob: 639fcdc9a5025cbaa2dc4e7722a446eacb781e9e [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 Murdoch23da30e2010-10-26 15:18:44 +010057 // Used to display toast after DB interactions complete.
58 private Handler mHandler;
John Reck35e9dd62011-04-25 09:01:54 -070059 private BrowserSettings mSettings;
Ben Murdoch23da30e2010-10-26 15:18:44 +010060
61 private final static int PROFILE_SAVED_MSG = 100;
62 private final static int PROFILE_DELETED_MSG = 101;
63
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010064 // 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 Murdoch9d9306d2011-01-18 16:06:00 +000069 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 Murdoch366824d2011-01-18 19:42:08 +000072 private static final String PHONE_NUMBER_SEPARATORS_REGEX = "[\\s\\.\\(\\)-]";
Ben Murdoch9d9306d2011-01-18 16:06:00 +000073
74 public void afterTextChanged(Editable s) {
Ben Murdoch366824d2011-01-18 19:42:08 +000075 String phoneNumber = s.toString();
76 int phoneNumberLength = phoneNumber.length();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000077
Ben Murdoch366824d2011-01-18 19:42:08 +000078 // 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 Murdoch9d9306d2011-01-18 16:06:00 +000084 mPhoneEdit.setError(getResources().getText(
85 R.string.autofill_profile_editor_phone_number_invalid));
86 } else {
87 mPhoneEdit.setError(null);
88 }
89
Ben Murdoche4c59f92011-10-18 12:01:07 +010090 updateSaveMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000091 }
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) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100102 updateSaveMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000103 }
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 Murdochaf554522010-09-10 22:09:30 +0100115 public AutoFillSettingsFragment() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100116 mHandler = new Handler() {
117 @Override
118 public void handleMessage(Message msg) {
Ben Murdoch381280d2011-08-11 10:39:59 +0100119 Context c = getActivity();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100120 switch (msg.what) {
121 case PROFILE_SAVED_MSG:
Ben Murdoch381280d2011-08-11 10:39:59 +0100122 if (c != null) {
123 Toast.makeText(c, R.string.autofill_profile_successful_save,
124 Toast.LENGTH_SHORT).show();
125 closeEditor();
126 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100127 break;
Ben Murdochaf554522010-09-10 22:09:30 +0100128
Ben Murdoch23da30e2010-10-26 15:18:44 +0100129 case PROFILE_DELETED_MSG:
Ben Murdoch381280d2011-08-11 10:39:59 +0100130 if (c != null) {
131 Toast.makeText(c, R.string.autofill_profile_successful_delete,
132 Toast.LENGTH_SHORT).show();
133 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100134 break;
135 }
136 }
137 };
Ben Murdochaf554522010-09-10 22:09:30 +0100138 }
139
140 @Override
141 public void onCreate(Bundle savedState) {
142 super.onCreate(savedState);
Ben Murdoch815752a2011-06-16 19:47:08 +0100143 setHasOptionsMenu(true);
John Reck35e9dd62011-04-25 09:01:54 -0700144 mSettings = BrowserSettings.getInstance();
Ben Murdochaf554522010-09-10 22:09:30 +0100145 }
146
147 @Override
Ben Murdoch815752a2011-06-16 19:47:08 +0100148 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
149 inflater.inflate(R.menu.autofill_profile_editor, menu);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100150 mSaveMenuItem = menu.findItem(R.id.autofill_profile_editor_save_profile_menu_id);
151 updateSaveMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100152 }
153
154 @Override
155 public boolean onOptionsItemSelected(MenuItem item) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100156 switch (item.getItemId()) {
157 case R.id.autofill_profile_editor_delete_profile_menu_id:
Ben Murdoch815752a2011-06-16 19:47:08 +0100158 // Clear the UI.
159 mFullNameEdit.setText("");
160 mEmailEdit.setText("");
161 mCompanyEdit.setText("");
162 mAddressLine1Edit.setText("");
163 mAddressLine2Edit.setText("");
164 mCityEdit.setText("");
165 mStateEdit.setText("");
166 mZipEdit.setText("");
167 mCountryEdit.setText("");
168 mPhoneEdit.setText("");
169
170 // Update browser settings and native with a null profile. This will
171 // trigger the current profile to get deleted from the DB.
172 mSettings.setAutoFillProfile(null,
173 mHandler.obtainMessage(PROFILE_DELETED_MSG));
Ben Murdoche4c59f92011-10-18 12:01:07 +0100174 updateSaveMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100175 return true;
Ben Murdoche4c59f92011-10-18 12:01:07 +0100176
177 case R.id.autofill_profile_editor_save_profile_menu_id:
178 AutoFillProfile newProfile = new AutoFillProfile(
179 mUniqueId,
180 mFullNameEdit.getText().toString(),
181 mEmailEdit.getText().toString(),
182 mCompanyEdit.getText().toString(),
183 mAddressLine1Edit.getText().toString(),
184 mAddressLine2Edit.getText().toString(),
185 mCityEdit.getText().toString(),
186 mStateEdit.getText().toString(),
187 mZipEdit.getText().toString(),
188 mCountryEdit.getText().toString(),
189 mPhoneEdit.getText().toString());
190
191 mSettings.setAutoFillProfile(newProfile,
192 mHandler.obtainMessage(PROFILE_SAVED_MSG));
193 return true;
194
195 default:
196 return false;
Ben Murdoch815752a2011-06-16 19:47:08 +0100197 }
Ben Murdoch815752a2011-06-16 19:47:08 +0100198 }
199
200 @Override
Ben Murdochaf554522010-09-10 22:09:30 +0100201 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100202 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +0100203 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
204
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100205 mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit);
206 mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit);
207 mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit);
208 mAddressLine1Edit = (EditText)v.findViewById(
209 R.id.autofill_profile_editor_address_line_1_edit);
210 mAddressLine2Edit = (EditText)v.findViewById(
211 R.id.autofill_profile_editor_address_line_2_edit);
212 mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit);
213 mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit);
214 mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit);
215 mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit);
216 mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit);
217
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000218 mFullNameEdit.addTextChangedListener(mFieldChangedListener);
219 mEmailEdit.addTextChangedListener(mFieldChangedListener);
220 mCompanyEdit.addTextChangedListener(mFieldChangedListener);
221 mAddressLine1Edit.addTextChangedListener(mFieldChangedListener);
222 mAddressLine2Edit.addTextChangedListener(mFieldChangedListener);
223 mCityEdit.addTextChangedListener(mFieldChangedListener);
224 mStateEdit.addTextChangedListener(mFieldChangedListener);
225 mZipEdit.addTextChangedListener(mFieldChangedListener);
226 mCountryEdit.addTextChangedListener(mFieldChangedListener);
227 mPhoneEdit.addTextChangedListener(new PhoneNumberValidator());
228
Ben Murdoch0cb81892010-10-08 12:41:33 +0100229 // Populate the text boxes with any pre existing AutoFill data.
John Reck35e9dd62011-04-25 09:01:54 -0700230 AutoFillProfile activeProfile = mSettings.getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100231 if (activeProfile != null) {
232 mFullNameEdit.setText(activeProfile.getFullName());
233 mEmailEdit.setText(activeProfile.getEmailAddress());
234 mCompanyEdit.setText(activeProfile.getCompanyName());
235 mAddressLine1Edit.setText(activeProfile.getAddressLine1());
236 mAddressLine2Edit.setText(activeProfile.getAddressLine2());
237 mCityEdit.setText(activeProfile.getCity());
238 mStateEdit.setText(activeProfile.getState());
239 mZipEdit.setText(activeProfile.getZipCode());
240 mCountryEdit.setText(activeProfile.getCountry());
241 mPhoneEdit.setText(activeProfile.getPhoneNumber());
242 }
Ben Murdochaf554522010-09-10 22:09:30 +0100243
Ben Murdoche4c59f92011-10-18 12:01:07 +0100244 updateSaveMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000245
Ben Murdochaf554522010-09-10 22:09:30 +0100246 return v;
247 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000248
Ben Murdoche4c59f92011-10-18 12:01:07 +0100249 private void updateSaveMenuItemState() {
250 if (mSaveMenuItem == null) {
251 return;
252 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000253
Ben Murdoche4c59f92011-10-18 12:01:07 +0100254 boolean currentState = mSaveMenuItem.isEnabled();
255 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000256 mEmailEdit.getText().toString().length() > 0 ||
257 mCompanyEdit.getText().toString().length() > 0 ||
258 mAddressLine1Edit.getText().toString().length() > 0 ||
259 mAddressLine2Edit.getText().toString().length() > 0 ||
260 mCityEdit.getText().toString().length() > 0 ||
261 mStateEdit.getText().toString().length() > 0 ||
262 mZipEdit.getText().toString().length() > 0 ||
263 mCountryEdit.getText().toString().length() > 0) &&
264 mPhoneEdit.getError() == null;
265
Ben Murdoche4c59f92011-10-18 12:01:07 +0100266 if (currentState != newState) {
267 mSaveMenuItem.setEnabled(newState);
268 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000269 }
270
271 private void closeEditor() {
272 // Hide the IME if the user wants to close while an EditText has focus
273 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
274 Context.INPUT_METHOD_SERVICE);
275 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
276 if (getFragmentManager().getBackStackEntryCount() > 0) {
277 getFragmentManager().popBackStack();
278 } else {
279 getActivity().finish();
280 }
281 }
Ben Murdochaf554522010-09-10 22:09:30 +0100282}