blob: 6bde1a2c141998fea97838c69586149aa3492de1 [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);
Pankaj Garg59608b72014-12-15 11:22:23 -0800134 bar.setHomeButtonEnabled(false);
Pankaj Garg458d35b2014-12-11 10:18:03 -0800135 }
136 }
137
138 @Override
Ben Murdoch815752a2011-06-16 19:47:08 +0100139 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
140 inflater.inflate(R.menu.autofill_profile_editor, menu);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100141 mSaveMenuItem = menu.findItem(R.id.autofill_profile_editor_save_profile_menu_id);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700142 mDeleteMenuItem = menu.findItem(R.id.autofill_profile_editor_delete_profile_menu_id);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100143 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700144 updateDeleteMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100145 }
146
147 @Override
148 public boolean onOptionsItemSelected(MenuItem item) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100149 switch (item.getItemId()) {
150 case R.id.autofill_profile_editor_delete_profile_menu_id:
Ben Murdoch815752a2011-06-16 19:47:08 +0100151 // Clear the UI.
152 mFullNameEdit.setText("");
153 mEmailEdit.setText("");
154 mCompanyEdit.setText("");
155 mAddressLine1Edit.setText("");
156 mAddressLine2Edit.setText("");
157 mCityEdit.setText("");
158 mStateEdit.setText("");
159 mZipEdit.setText("");
160 mCountryEdit.setText("");
161 mPhoneEdit.setText("");
162
163 // Update browser settings and native with a null profile. This will
164 // trigger the current profile to get deleted from the DB.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800165 mSettings.updateAutoFillProfile(null);
166
Ben Murdoche4c59f92011-10-18 12:01:07 +0100167 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700168 updateDeleteMenuItemState();
Vivek Sekhar3e5e42d2014-06-05 12:17:59 -0700169 Toast.makeText(getActivity(), R.string.autofill_profile_successful_delete,
170 Toast.LENGTH_SHORT).show();
Ben Murdoch815752a2011-06-16 19:47:08 +0100171 return true;
Ben Murdoche4c59f92011-10-18 12:01:07 +0100172
173 case R.id.autofill_profile_editor_save_profile_menu_id:
174 AutoFillProfile newProfile = new AutoFillProfile(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800175 mSettings.getAutoFillProfileId(),
Ben Murdoche4c59f92011-10-18 12:01:07 +0100176 mFullNameEdit.getText().toString(),
177 mEmailEdit.getText().toString(),
178 mCompanyEdit.getText().toString(),
179 mAddressLine1Edit.getText().toString(),
180 mAddressLine2Edit.getText().toString(),
181 mCityEdit.getText().toString(),
182 mStateEdit.getText().toString(),
183 mZipEdit.getText().toString(),
184 mCountryEdit.getText().toString(),
185 mPhoneEdit.getText().toString());
186
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800187 mSettings.updateAutoFillProfile(newProfile);
Vivek Sekhar3e5e42d2014-06-05 12:17:59 -0700188 Toast.makeText(getActivity(), R.string.autofill_profile_successful_save,
189 Toast.LENGTH_SHORT).show();
190 closeEditor();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800191
Ben Murdoche4c59f92011-10-18 12:01:07 +0100192 return true;
193
194 default:
195 return false;
Ben Murdoch815752a2011-06-16 19:47:08 +0100196 }
Ben Murdoch815752a2011-06-16 19:47:08 +0100197 }
198
199 @Override
Ben Murdochaf554522010-09-10 22:09:30 +0100200 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100201 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +0100202 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
203
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100204 mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit);
205 mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit);
206 mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit);
207 mAddressLine1Edit = (EditText)v.findViewById(
208 R.id.autofill_profile_editor_address_line_1_edit);
209 mAddressLine2Edit = (EditText)v.findViewById(
210 R.id.autofill_profile_editor_address_line_2_edit);
211 mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit);
212 mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit);
213 mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit);
214 mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit);
215 mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit);
216
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000217 mFullNameEdit.addTextChangedListener(mFieldChangedListener);
218 mEmailEdit.addTextChangedListener(mFieldChangedListener);
219 mCompanyEdit.addTextChangedListener(mFieldChangedListener);
220 mAddressLine1Edit.addTextChangedListener(mFieldChangedListener);
221 mAddressLine2Edit.addTextChangedListener(mFieldChangedListener);
222 mCityEdit.addTextChangedListener(mFieldChangedListener);
223 mStateEdit.addTextChangedListener(mFieldChangedListener);
224 mZipEdit.addTextChangedListener(mFieldChangedListener);
225 mCountryEdit.addTextChangedListener(mFieldChangedListener);
226 mPhoneEdit.addTextChangedListener(new PhoneNumberValidator());
227
Ben Murdoch0cb81892010-10-08 12:41:33 +0100228 // Populate the text boxes with any pre existing AutoFill data.
John Reck35e9dd62011-04-25 09:01:54 -0700229 AutoFillProfile activeProfile = mSettings.getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100230 if (activeProfile != null) {
231 mFullNameEdit.setText(activeProfile.getFullName());
232 mEmailEdit.setText(activeProfile.getEmailAddress());
233 mCompanyEdit.setText(activeProfile.getCompanyName());
234 mAddressLine1Edit.setText(activeProfile.getAddressLine1());
235 mAddressLine2Edit.setText(activeProfile.getAddressLine2());
236 mCityEdit.setText(activeProfile.getCity());
237 mStateEdit.setText(activeProfile.getState());
238 mZipEdit.setText(activeProfile.getZipCode());
239 mCountryEdit.setText(activeProfile.getCountry());
240 mPhoneEdit.setText(activeProfile.getPhoneNumber());
241 }
Ben Murdochaf554522010-09-10 22:09:30 +0100242
Ben Murdoch6f336402011-10-27 13:57:33 +0100243 mInitialised = true;
244
Ben Murdoche4c59f92011-10-18 12:01:07 +0100245 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700246 updateDeleteMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000247
Ben Murdochaf554522010-09-10 22:09:30 +0100248 return v;
249 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000250
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700251 private void updateDeleteMenuItemState() {
252 if (mDeleteMenuItem == null) {
253 return;
254 }
255
256 if (!mInitialised) {
257 mDeleteMenuItem.setEnabled(false);
258 return;
259 }
260
261 boolean currentState = mDeleteMenuItem.isEnabled();
262 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
263 mEmailEdit.getText().toString().length() > 0 ||
264 mCompanyEdit.getText().toString().length() > 0 ||
265 mAddressLine1Edit.getText().toString().length() > 0 ||
266 mAddressLine2Edit.getText().toString().length() > 0 ||
267 mCityEdit.getText().toString().length() > 0 ||
268 mStateEdit.getText().toString().length() > 0 ||
269 mZipEdit.getText().toString().length() > 0 ||
270 mCountryEdit.getText().toString().length() > 0) &&
271 mPhoneEdit.getError() == null;
272
273 if (currentState != newState) {
274 mDeleteMenuItem.setEnabled(newState);
275 }
276 }
277
Ben Murdoche4c59f92011-10-18 12:01:07 +0100278 private void updateSaveMenuItemState() {
279 if (mSaveMenuItem == null) {
280 return;
281 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000282
Ben Murdoch6f336402011-10-27 13:57:33 +0100283 if (!mInitialised) {
284 mSaveMenuItem.setEnabled(false);
285 return;
286 }
287
Ben Murdoche4c59f92011-10-18 12:01:07 +0100288 boolean currentState = mSaveMenuItem.isEnabled();
289 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000290 mEmailEdit.getText().toString().length() > 0 ||
291 mCompanyEdit.getText().toString().length() > 0 ||
292 mAddressLine1Edit.getText().toString().length() > 0 ||
293 mAddressLine2Edit.getText().toString().length() > 0 ||
294 mCityEdit.getText().toString().length() > 0 ||
295 mStateEdit.getText().toString().length() > 0 ||
296 mZipEdit.getText().toString().length() > 0 ||
297 mCountryEdit.getText().toString().length() > 0) &&
298 mPhoneEdit.getError() == null;
299
Ben Murdoche4c59f92011-10-18 12:01:07 +0100300 if (currentState != newState) {
301 mSaveMenuItem.setEnabled(newState);
302 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000303 }
304
305 private void closeEditor() {
306 // Hide the IME if the user wants to close while an EditText has focus
307 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
308 Context.INPUT_METHOD_SERVICE);
309 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
310 if (getFragmentManager().getBackStackEntryCount() > 0) {
311 getFragmentManager().popBackStack();
312 } else {
313 getActivity().finish();
314 }
315 }
Ben Murdochaf554522010-09-10 22:09:30 +0100316}