blob: d0ca9461bde1e3ff9f5a32a09cfebdba6d282dfb [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080018
19import org.codeaurora.swe.AutoFillProfile;
20
Bijan Amirzada41242f22014-03-21 12:12:18 -070021import com.android.browser.R;
Ben Murdochaf554522010-09-10 22:09:30 +010022
Pankaj Garg458d35b2014-12-11 10:18:03 -080023import android.app.ActionBar;
Ben Murdochaf554522010-09-10 22:09:30 +010024import android.app.Fragment;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000025import android.content.Context;
Ben Murdochaf554522010-09-10 22:09:30 +010026import android.os.Bundle;
Ben Murdoch23da30e2010-10-26 15:18:44 +010027import android.os.Handler;
28import android.os.Message;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000029import android.text.Editable;
30import android.text.TextWatcher;
Ben Murdochaf554522010-09-10 22:09:30 +010031import android.util.Log;
32import android.view.View;
33import android.view.ViewGroup;
34import android.view.View.OnClickListener;
35import android.view.LayoutInflater;
Ben Murdoch815752a2011-06-16 19:47:08 +010036import android.view.Menu;
37import android.view.MenuInflater;
38import android.view.MenuItem;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000039import android.view.inputmethod.InputMethodManager;
Ben Murdochaf554522010-09-10 22:09:30 +010040import android.widget.Button;
41import android.widget.EditText;
Ben Murdoch36a23dd2010-10-13 13:20:06 +010042import android.widget.Toast;
Ben Murdochaf554522010-09-10 22:09:30 +010043
44public class AutoFillSettingsFragment extends Fragment {
45
46 private static final String LOGTAG = "AutoFillSettingsFragment";
47
Ben Murdoch36a23dd2010-10-13 13:20:06 +010048 private EditText mFullNameEdit;
49 private EditText mEmailEdit;
50 private EditText mCompanyEdit;
51 private EditText mAddressLine1Edit;
52 private EditText mAddressLine2Edit;
53 private EditText mCityEdit;
54 private EditText mStateEdit;
55 private EditText mZipEdit;
56 private EditText mCountryEdit;
57 private EditText mPhoneEdit;
58
Ben Murdoche4c59f92011-10-18 12:01:07 +010059 private MenuItem mSaveMenuItem;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070060 private MenuItem mDeleteMenuItem;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000061
Ben Murdoch6f336402011-10-27 13:57:33 +010062 private boolean mInitialised;
63
Ben Murdoch23da30e2010-10-26 15:18:44 +010064 // Used to display toast after DB interactions complete.
65 private Handler mHandler;
John Reck35e9dd62011-04-25 09:01:54 -070066 private BrowserSettings mSettings;
Ben Murdoch23da30e2010-10-26 15:18:44 +010067
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010068 // For now we support just one profile so it's safe to hardcode the
69 // id to 1 here. In the future this unique identifier will be set
70 // dynamically.
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010071
Ben Murdoch9d9306d2011-01-18 16:06:00 +000072 private class PhoneNumberValidator implements TextWatcher {
73 // Keep in sync with kPhoneNumberLength in chrome/browser/autofill/phone_number.cc
74 private static final int PHONE_NUMBER_LENGTH = 7;
Ben Murdoch366824d2011-01-18 19:42:08 +000075 private static final String PHONE_NUMBER_SEPARATORS_REGEX = "[\\s\\.\\(\\)-]";
Ben Murdoch9d9306d2011-01-18 16:06:00 +000076
77 public void afterTextChanged(Editable s) {
Ben Murdoch366824d2011-01-18 19:42:08 +000078 String phoneNumber = s.toString();
79 int phoneNumberLength = phoneNumber.length();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000080
Ben Murdoch366824d2011-01-18 19:42:08 +000081 // Strip out any phone number separators.
82 phoneNumber = phoneNumber.replaceAll(PHONE_NUMBER_SEPARATORS_REGEX, "");
83
84 int strippedPhoneNumberLength = phoneNumber.length();
85
86 if (phoneNumberLength > 0 && strippedPhoneNumberLength < PHONE_NUMBER_LENGTH) {
Ben Murdoch9d9306d2011-01-18 16:06:00 +000087 mPhoneEdit.setError(getResources().getText(
88 R.string.autofill_profile_editor_phone_number_invalid));
89 } else {
90 mPhoneEdit.setError(null);
91 }
92
Ben Murdoche4c59f92011-10-18 12:01:07 +010093 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070094 updateDeleteMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000095 }
96
97 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
98 }
99
100 public void onTextChanged(CharSequence s, int start, int before, int count) {
101 }
102 }
103
104 private class FieldChangedListener implements TextWatcher {
105 public void afterTextChanged(Editable s) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100106 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700107 updateDeleteMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000108 }
109
110 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
111 }
112
113 public void onTextChanged(CharSequence s, int start, int before, int count) {
114 }
115
116 }
117
118 private TextWatcher mFieldChangedListener = new FieldChangedListener();
119
Ben Murdochaf554522010-09-10 22:09:30 +0100120 @Override
121 public void onCreate(Bundle savedState) {
122 super.onCreate(savedState);
Ben Murdoch815752a2011-06-16 19:47:08 +0100123 setHasOptionsMenu(true);
John Reck35e9dd62011-04-25 09:01:54 -0700124 mSettings = BrowserSettings.getInstance();
Ben Murdochaf554522010-09-10 22:09:30 +0100125 }
126
127 @Override
Pankaj Garg458d35b2014-12-11 10:18:03 -0800128 public void onResume() {
129 super.onResume();
130 ActionBar bar = getActivity().getActionBar();
131 if (bar != null) {
132 bar.setTitle(R.string.pref_general_autofill_title);
133 bar.setDisplayHomeAsUpEnabled(false);
134 }
135 }
136
137 @Override
Ben Murdoch815752a2011-06-16 19:47:08 +0100138 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
139 inflater.inflate(R.menu.autofill_profile_editor, menu);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100140 mSaveMenuItem = menu.findItem(R.id.autofill_profile_editor_save_profile_menu_id);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700141 mDeleteMenuItem = menu.findItem(R.id.autofill_profile_editor_delete_profile_menu_id);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100142 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700143 updateDeleteMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100144 }
145
146 @Override
147 public boolean onOptionsItemSelected(MenuItem item) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100148 switch (item.getItemId()) {
149 case R.id.autofill_profile_editor_delete_profile_menu_id:
Ben Murdoch815752a2011-06-16 19:47:08 +0100150 // Clear the UI.
151 mFullNameEdit.setText("");
152 mEmailEdit.setText("");
153 mCompanyEdit.setText("");
154 mAddressLine1Edit.setText("");
155 mAddressLine2Edit.setText("");
156 mCityEdit.setText("");
157 mStateEdit.setText("");
158 mZipEdit.setText("");
159 mCountryEdit.setText("");
160 mPhoneEdit.setText("");
161
162 // Update browser settings and native with a null profile. This will
163 // trigger the current profile to get deleted from the DB.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800164 mSettings.updateAutoFillProfile(null);
165
Ben Murdoche4c59f92011-10-18 12:01:07 +0100166 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700167 updateDeleteMenuItemState();
Vivek Sekhar3e5e42d2014-06-05 12:17:59 -0700168 Toast.makeText(getActivity(), R.string.autofill_profile_successful_delete,
169 Toast.LENGTH_SHORT).show();
Ben Murdoch815752a2011-06-16 19:47:08 +0100170 return true;
Ben Murdoche4c59f92011-10-18 12:01:07 +0100171
172 case R.id.autofill_profile_editor_save_profile_menu_id:
173 AutoFillProfile newProfile = new AutoFillProfile(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800174 mSettings.getAutoFillProfileId(),
Ben Murdoche4c59f92011-10-18 12:01:07 +0100175 mFullNameEdit.getText().toString(),
176 mEmailEdit.getText().toString(),
177 mCompanyEdit.getText().toString(),
178 mAddressLine1Edit.getText().toString(),
179 mAddressLine2Edit.getText().toString(),
180 mCityEdit.getText().toString(),
181 mStateEdit.getText().toString(),
182 mZipEdit.getText().toString(),
183 mCountryEdit.getText().toString(),
184 mPhoneEdit.getText().toString());
185
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800186 mSettings.updateAutoFillProfile(newProfile);
Vivek Sekhar3e5e42d2014-06-05 12:17:59 -0700187 Toast.makeText(getActivity(), R.string.autofill_profile_successful_save,
188 Toast.LENGTH_SHORT).show();
189 closeEditor();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800190
Ben Murdoche4c59f92011-10-18 12:01:07 +0100191 return true;
192
193 default:
194 return false;
Ben Murdoch815752a2011-06-16 19:47:08 +0100195 }
Ben Murdoch815752a2011-06-16 19:47:08 +0100196 }
197
198 @Override
Ben Murdochaf554522010-09-10 22:09:30 +0100199 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100200 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +0100201 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
202
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100203 mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit);
204 mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit);
205 mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit);
206 mAddressLine1Edit = (EditText)v.findViewById(
207 R.id.autofill_profile_editor_address_line_1_edit);
208 mAddressLine2Edit = (EditText)v.findViewById(
209 R.id.autofill_profile_editor_address_line_2_edit);
210 mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit);
211 mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit);
212 mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit);
213 mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit);
214 mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit);
215
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000216 mFullNameEdit.addTextChangedListener(mFieldChangedListener);
217 mEmailEdit.addTextChangedListener(mFieldChangedListener);
218 mCompanyEdit.addTextChangedListener(mFieldChangedListener);
219 mAddressLine1Edit.addTextChangedListener(mFieldChangedListener);
220 mAddressLine2Edit.addTextChangedListener(mFieldChangedListener);
221 mCityEdit.addTextChangedListener(mFieldChangedListener);
222 mStateEdit.addTextChangedListener(mFieldChangedListener);
223 mZipEdit.addTextChangedListener(mFieldChangedListener);
224 mCountryEdit.addTextChangedListener(mFieldChangedListener);
225 mPhoneEdit.addTextChangedListener(new PhoneNumberValidator());
226
Ben Murdoch0cb81892010-10-08 12:41:33 +0100227 // Populate the text boxes with any pre existing AutoFill data.
John Reck35e9dd62011-04-25 09:01:54 -0700228 AutoFillProfile activeProfile = mSettings.getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100229 if (activeProfile != null) {
230 mFullNameEdit.setText(activeProfile.getFullName());
231 mEmailEdit.setText(activeProfile.getEmailAddress());
232 mCompanyEdit.setText(activeProfile.getCompanyName());
233 mAddressLine1Edit.setText(activeProfile.getAddressLine1());
234 mAddressLine2Edit.setText(activeProfile.getAddressLine2());
235 mCityEdit.setText(activeProfile.getCity());
236 mStateEdit.setText(activeProfile.getState());
237 mZipEdit.setText(activeProfile.getZipCode());
238 mCountryEdit.setText(activeProfile.getCountry());
239 mPhoneEdit.setText(activeProfile.getPhoneNumber());
240 }
Ben Murdochaf554522010-09-10 22:09:30 +0100241
Ben Murdoch6f336402011-10-27 13:57:33 +0100242 mInitialised = true;
243
Ben Murdoche4c59f92011-10-18 12:01:07 +0100244 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700245 updateDeleteMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000246
Ben Murdochaf554522010-09-10 22:09:30 +0100247 return v;
248 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000249
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700250 private void updateDeleteMenuItemState() {
251 if (mDeleteMenuItem == null) {
252 return;
253 }
254
255 if (!mInitialised) {
256 mDeleteMenuItem.setEnabled(false);
257 return;
258 }
259
260 boolean currentState = mDeleteMenuItem.isEnabled();
261 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
262 mEmailEdit.getText().toString().length() > 0 ||
263 mCompanyEdit.getText().toString().length() > 0 ||
264 mAddressLine1Edit.getText().toString().length() > 0 ||
265 mAddressLine2Edit.getText().toString().length() > 0 ||
266 mCityEdit.getText().toString().length() > 0 ||
267 mStateEdit.getText().toString().length() > 0 ||
268 mZipEdit.getText().toString().length() > 0 ||
269 mCountryEdit.getText().toString().length() > 0) &&
270 mPhoneEdit.getError() == null;
271
272 if (currentState != newState) {
273 mDeleteMenuItem.setEnabled(newState);
274 }
275 }
276
Ben Murdoche4c59f92011-10-18 12:01:07 +0100277 private void updateSaveMenuItemState() {
278 if (mSaveMenuItem == null) {
279 return;
280 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000281
Ben Murdoch6f336402011-10-27 13:57:33 +0100282 if (!mInitialised) {
283 mSaveMenuItem.setEnabled(false);
284 return;
285 }
286
Ben Murdoche4c59f92011-10-18 12:01:07 +0100287 boolean currentState = mSaveMenuItem.isEnabled();
288 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000289 mEmailEdit.getText().toString().length() > 0 ||
290 mCompanyEdit.getText().toString().length() > 0 ||
291 mAddressLine1Edit.getText().toString().length() > 0 ||
292 mAddressLine2Edit.getText().toString().length() > 0 ||
293 mCityEdit.getText().toString().length() > 0 ||
294 mStateEdit.getText().toString().length() > 0 ||
295 mZipEdit.getText().toString().length() > 0 ||
296 mCountryEdit.getText().toString().length() > 0) &&
297 mPhoneEdit.getError() == null;
298
Ben Murdoche4c59f92011-10-18 12:01:07 +0100299 if (currentState != newState) {
300 mSaveMenuItem.setEnabled(newState);
301 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000302 }
303
304 private void closeEditor() {
305 // Hide the IME if the user wants to close while an EditText has focus
306 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
307 Context.INPUT_METHOD_SERVICE);
308 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
309 if (getFragmentManager().getBackStackEntryCount() > 0) {
310 getFragmentManager().popBackStack();
311 } else {
312 getActivity().finish();
313 }
314 }
Ben Murdochaf554522010-09-10 22:09:30 +0100315}