blob: 389be1f7f0282dcc8c7b9a649550259e25c8dfa0 [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;
56
57 private final static int PROFILE_SAVED_MSG = 100;
58 private final static int PROFILE_DELETED_MSG = 101;
59
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010060 // 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 Murdoch9d9306d2011-01-18 16:06:00 +000065 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 Murdochaf554522010-09-10 22:09:30 +0100104 public AutoFillSettingsFragment() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100105 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 Murdochaf554522010-09-10 22:09:30 +0100113
Ben Murdoch23da30e2010-10-26 15:18:44 +0100114 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 Murdochaf554522010-09-10 22:09:30 +0100121 }
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 Murdoch36a23dd2010-10-13 13:20:06 +0100130 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +0100131 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
132
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100133 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 Murdoch9d9306d2011-01-18 16:06:00 +0000146 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 Murdochaf554522010-09-10 22:09:30 +0100159 public void onClick(View button) {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100160 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 Murdoch9d9306d2011-01-18 16:06:00 +0000175 closeEditor();
Ben Murdochaf554522010-09-10 22:09:30 +0100176 }
177 });
178
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100179 Button deleteButton = (Button)v.findViewById(R.id.autofill_profile_editor_delete_button);
180 deleteButton.setOnClickListener(new OnClickListener() {
181 public void onClick(View button) {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100182 // 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 Murdoch9d9306d2011-01-18 16:06:00 +0000198
199 updateButtonState();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100200 }
201 });
202
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000203 Button cancelButton = (Button)v.findViewById(R.id.autofill_profile_editor_cancel_button);
204 cancelButton.setOnClickListener(new OnClickListener() {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100205 public void onClick(View button) {
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000206 closeEditor();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100207 }
208 });
209
Ben Murdoch0cb81892010-10-08 12:41:33 +0100210 // Populate the text boxes with any pre existing AutoFill data.
Ben Murdoch0cb81892010-10-08 12:41:33 +0100211 AutoFillProfile activeProfile = BrowserSettings.getInstance().getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100212 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 Murdochaf554522010-09-10 22:09:30 +0100224
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000225 updateButtonState();
226
Ben Murdochaf554522010-09-10 22:09:30 +0100227 return v;
228 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000229
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 Murdochaf554522010-09-10 22:09:30 +0100259}