blob: e87645eea297fd2c39f824c291183ad110120da3 [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 Murdoch9d9306d2011-01-18 16:06:00 +000031import android.view.inputmethod.InputMethodManager;
Ben Murdoch0cb81892010-10-08 12:41:33 +010032import android.webkit.WebSettings.AutoFillProfile;
Ben Murdochaf554522010-09-10 22:09:30 +010033import android.widget.Button;
34import android.widget.EditText;
Ben Murdoch36a23dd2010-10-13 13:20:06 +010035import android.widget.Toast;
Ben Murdochaf554522010-09-10 22:09:30 +010036
37public class AutoFillSettingsFragment extends Fragment {
38
39 private static final String LOGTAG = "AutoFillSettingsFragment";
40
Ben Murdoch36a23dd2010-10-13 13:20:06 +010041 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 Murdoch9d9306d2011-01-18 16:06:00 +000052 private Button mSaveButton;
53
Ben Murdoch23da30e2010-10-26 15:18:44 +010054 // Used to display toast after DB interactions complete.
55 private Handler mHandler;
John Reck35e9dd62011-04-25 09:01:54 -070056 private BrowserSettings mSettings;
Ben Murdoch23da30e2010-10-26 15:18:44 +010057
58 private final static int PROFILE_SAVED_MSG = 100;
59 private final static int PROFILE_DELETED_MSG = 101;
60
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010061 // For now we support just one profile so it's safe to hardcode the
62 // id to 1 here. In the future this unique identifier will be set
63 // dynamically.
64 private int mUniqueId = 1;
65
Ben Murdoch9d9306d2011-01-18 16:06:00 +000066 private class PhoneNumberValidator implements TextWatcher {
67 // Keep in sync with kPhoneNumberLength in chrome/browser/autofill/phone_number.cc
68 private static final int PHONE_NUMBER_LENGTH = 7;
Ben Murdoch366824d2011-01-18 19:42:08 +000069 private static final String PHONE_NUMBER_SEPARATORS_REGEX = "[\\s\\.\\(\\)-]";
Ben Murdoch9d9306d2011-01-18 16:06:00 +000070
71 public void afterTextChanged(Editable s) {
Ben Murdoch366824d2011-01-18 19:42:08 +000072 String phoneNumber = s.toString();
73 int phoneNumberLength = phoneNumber.length();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000074
Ben Murdoch366824d2011-01-18 19:42:08 +000075 // Strip out any phone number separators.
76 phoneNumber = phoneNumber.replaceAll(PHONE_NUMBER_SEPARATORS_REGEX, "");
77
78 int strippedPhoneNumberLength = phoneNumber.length();
79
80 if (phoneNumberLength > 0 && strippedPhoneNumberLength < PHONE_NUMBER_LENGTH) {
Ben Murdoch9d9306d2011-01-18 16:06:00 +000081 mPhoneEdit.setError(getResources().getText(
82 R.string.autofill_profile_editor_phone_number_invalid));
83 } else {
84 mPhoneEdit.setError(null);
85 }
86
87 updateButtonState();
88 }
89
90 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
91 }
92
93 public void onTextChanged(CharSequence s, int start, int before, int count) {
94 }
95 }
96
97 private class FieldChangedListener implements TextWatcher {
98 public void afterTextChanged(Editable s) {
99 updateButtonState();
100 }
101
102 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
103 }
104
105 public void onTextChanged(CharSequence s, int start, int before, int count) {
106 }
107
108 }
109
110 private TextWatcher mFieldChangedListener = new FieldChangedListener();
111
Ben Murdochaf554522010-09-10 22:09:30 +0100112 public AutoFillSettingsFragment() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100113 mHandler = new Handler() {
114 @Override
115 public void handleMessage(Message msg) {
116 switch (msg.what) {
117 case PROFILE_SAVED_MSG:
118 Toast.makeText(getActivity(), R.string.autofill_profile_successful_save,
119 Toast.LENGTH_SHORT).show();
120 break;
Ben Murdochaf554522010-09-10 22:09:30 +0100121
Ben Murdoch23da30e2010-10-26 15:18:44 +0100122 case PROFILE_DELETED_MSG:
123 Toast.makeText(getActivity(), R.string.autofill_profile_successful_delete,
124 Toast.LENGTH_SHORT).show();
125 break;
126 }
127 }
128 };
Ben Murdochaf554522010-09-10 22:09:30 +0100129 }
130
131 @Override
132 public void onCreate(Bundle savedState) {
133 super.onCreate(savedState);
John Reck35e9dd62011-04-25 09:01:54 -0700134 mSettings = BrowserSettings.getInstance();
Ben Murdochaf554522010-09-10 22:09:30 +0100135 }
136
137 @Override
138 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100139 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +0100140 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
141
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100142 mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit);
143 mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit);
144 mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit);
145 mAddressLine1Edit = (EditText)v.findViewById(
146 R.id.autofill_profile_editor_address_line_1_edit);
147 mAddressLine2Edit = (EditText)v.findViewById(
148 R.id.autofill_profile_editor_address_line_2_edit);
149 mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit);
150 mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit);
151 mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit);
152 mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit);
153 mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit);
154
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000155 mFullNameEdit.addTextChangedListener(mFieldChangedListener);
156 mEmailEdit.addTextChangedListener(mFieldChangedListener);
157 mCompanyEdit.addTextChangedListener(mFieldChangedListener);
158 mAddressLine1Edit.addTextChangedListener(mFieldChangedListener);
159 mAddressLine2Edit.addTextChangedListener(mFieldChangedListener);
160 mCityEdit.addTextChangedListener(mFieldChangedListener);
161 mStateEdit.addTextChangedListener(mFieldChangedListener);
162 mZipEdit.addTextChangedListener(mFieldChangedListener);
163 mCountryEdit.addTextChangedListener(mFieldChangedListener);
164 mPhoneEdit.addTextChangedListener(new PhoneNumberValidator());
165
166 mSaveButton = (Button)v.findViewById(R.id.autofill_profile_editor_save_button);
167 mSaveButton.setOnClickListener(new OnClickListener() {
Ben Murdochaf554522010-09-10 22:09:30 +0100168 public void onClick(View button) {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100169 AutoFillProfile newProfile = new AutoFillProfile(
170 mUniqueId,
171 mFullNameEdit.getText().toString(),
172 mEmailEdit.getText().toString(),
173 mCompanyEdit.getText().toString(),
174 mAddressLine1Edit.getText().toString(),
175 mAddressLine2Edit.getText().toString(),
176 mCityEdit.getText().toString(),
177 mStateEdit.getText().toString(),
178 mZipEdit.getText().toString(),
179 mCountryEdit.getText().toString(),
180 mPhoneEdit.getText().toString());
181
John Reck35e9dd62011-04-25 09:01:54 -0700182 mSettings.setAutoFillProfile(newProfile,
Ben Murdoch23da30e2010-10-26 15:18:44 +0100183 mHandler.obtainMessage(PROFILE_SAVED_MSG));
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000184 closeEditor();
Ben Murdochaf554522010-09-10 22:09:30 +0100185 }
186 });
187
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100188 Button deleteButton = (Button)v.findViewById(R.id.autofill_profile_editor_delete_button);
189 deleteButton.setOnClickListener(new OnClickListener() {
190 public void onClick(View button) {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100191 // Clear the UI.
192 mFullNameEdit.setText("");
193 mEmailEdit.setText("");
194 mCompanyEdit.setText("");
195 mAddressLine1Edit.setText("");
196 mAddressLine2Edit.setText("");
197 mCityEdit.setText("");
198 mStateEdit.setText("");
199 mZipEdit.setText("");
200 mCountryEdit.setText("");
201 mPhoneEdit.setText("");
202
203 // Update browser settings and native with a null profile. This will
204 // trigger the current profile to get deleted from the DB.
John Reck35e9dd62011-04-25 09:01:54 -0700205 mSettings.setAutoFillProfile(null,
Ben Murdoch23da30e2010-10-26 15:18:44 +0100206 mHandler.obtainMessage(PROFILE_DELETED_MSG));
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000207
208 updateButtonState();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100209 }
210 });
211
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000212 Button cancelButton = (Button)v.findViewById(R.id.autofill_profile_editor_cancel_button);
213 cancelButton.setOnClickListener(new OnClickListener() {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100214 public void onClick(View button) {
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000215 closeEditor();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100216 }
217 });
218
Ben Murdoch0cb81892010-10-08 12:41:33 +0100219 // Populate the text boxes with any pre existing AutoFill data.
John Reck35e9dd62011-04-25 09:01:54 -0700220 AutoFillProfile activeProfile = mSettings.getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100221 if (activeProfile != null) {
222 mFullNameEdit.setText(activeProfile.getFullName());
223 mEmailEdit.setText(activeProfile.getEmailAddress());
224 mCompanyEdit.setText(activeProfile.getCompanyName());
225 mAddressLine1Edit.setText(activeProfile.getAddressLine1());
226 mAddressLine2Edit.setText(activeProfile.getAddressLine2());
227 mCityEdit.setText(activeProfile.getCity());
228 mStateEdit.setText(activeProfile.getState());
229 mZipEdit.setText(activeProfile.getZipCode());
230 mCountryEdit.setText(activeProfile.getCountry());
231 mPhoneEdit.setText(activeProfile.getPhoneNumber());
232 }
Ben Murdochaf554522010-09-10 22:09:30 +0100233
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000234 updateButtonState();
235
Ben Murdochaf554522010-09-10 22:09:30 +0100236 return v;
237 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000238
239 public void updateButtonState() {
240
241 boolean valid = (mFullNameEdit.getText().toString().length() > 0 ||
242 mEmailEdit.getText().toString().length() > 0 ||
243 mCompanyEdit.getText().toString().length() > 0 ||
244 mAddressLine1Edit.getText().toString().length() > 0 ||
245 mAddressLine2Edit.getText().toString().length() > 0 ||
246 mCityEdit.getText().toString().length() > 0 ||
247 mStateEdit.getText().toString().length() > 0 ||
248 mZipEdit.getText().toString().length() > 0 ||
249 mCountryEdit.getText().toString().length() > 0) &&
250 mPhoneEdit.getError() == null;
251
252 // Only enable the save buttons if we have at least one field completed
253 // and the phone number (if present is valid).
254 mSaveButton.setEnabled(valid);
255 }
256
257 private void closeEditor() {
258 // Hide the IME if the user wants to close while an EditText has focus
259 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
260 Context.INPUT_METHOD_SERVICE);
261 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
262 if (getFragmentManager().getBackStackEntryCount() > 0) {
263 getFragmentManager().popBackStack();
264 } else {
265 getActivity().finish();
266 }
267 }
Ben Murdochaf554522010-09-10 22:09:30 +0100268}