blob: b282789020d1d7b07080070b3e3413920506040c [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 Murdoch9d9306d2011-01-18 16:06:00 +000055 private Button mSaveButton;
56
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
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 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) {
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 Murdochaf554522010-09-10 22:09:30 +0100124
Ben Murdoch23da30e2010-10-26 15:18:44 +0100125 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 Murdochaf554522010-09-10 22:09:30 +0100132 }
133
134 @Override
135 public void onCreate(Bundle savedState) {
136 super.onCreate(savedState);
Ben Murdoch815752a2011-06-16 19:47:08 +0100137 setHasOptionsMenu(true);
John Reck35e9dd62011-04-25 09:01:54 -0700138 mSettings = BrowserSettings.getInstance();
Ben Murdochaf554522010-09-10 22:09:30 +0100139 }
140
141 @Override
Ben Murdoch815752a2011-06-16 19:47:08 +0100142 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 Murdochaf554522010-09-10 22:09:30 +0100172 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100173 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +0100174 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
175
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100176 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 Murdoch9d9306d2011-01-18 16:06:00 +0000189 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 Murdochaf554522010-09-10 22:09:30 +0100202 public void onClick(View button) {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100203 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 Reck35e9dd62011-04-25 09:01:54 -0700216 mSettings.setAutoFillProfile(newProfile,
Ben Murdoch23da30e2010-10-26 15:18:44 +0100217 mHandler.obtainMessage(PROFILE_SAVED_MSG));
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000218 closeEditor();
Ben Murdochaf554522010-09-10 22:09:30 +0100219 }
220 });
221
Ben Murdoch0cb81892010-10-08 12:41:33 +0100222 // Populate the text boxes with any pre existing AutoFill data.
John Reck35e9dd62011-04-25 09:01:54 -0700223 AutoFillProfile activeProfile = mSettings.getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100224 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 Murdochaf554522010-09-10 22:09:30 +0100236
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000237 updateButtonState();
238
Ben Murdochaf554522010-09-10 22:09:30 +0100239 return v;
240 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000241
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 Murdochaf554522010-09-10 22:09:30 +0100271}