blob: 5a97edea101765a533aab40a1ef88cc91f189859 [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
23import android.app.Fragment;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000024import android.content.Context;
Ben Murdochaf554522010-09-10 22:09:30 +010025import android.os.Bundle;
Ben Murdoch23da30e2010-10-26 15:18:44 +010026import android.os.Handler;
27import android.os.Message;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000028import android.text.Editable;
29import android.text.TextWatcher;
Ben Murdochaf554522010-09-10 22:09:30 +010030import android.util.Log;
31import android.view.View;
32import android.view.ViewGroup;
33import android.view.View.OnClickListener;
34import android.view.LayoutInflater;
Ben Murdoch815752a2011-06-16 19:47:08 +010035import android.view.Menu;
36import android.view.MenuInflater;
37import android.view.MenuItem;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000038import android.view.inputmethod.InputMethodManager;
Ben Murdochaf554522010-09-10 22:09:30 +010039import android.widget.Button;
40import android.widget.EditText;
Ben Murdoch36a23dd2010-10-13 13:20:06 +010041import android.widget.Toast;
Ben Murdochaf554522010-09-10 22:09:30 +010042
43public class AutoFillSettingsFragment extends Fragment {
44
45 private static final String LOGTAG = "AutoFillSettingsFragment";
46
Ben Murdoch36a23dd2010-10-13 13:20:06 +010047 private EditText mFullNameEdit;
48 private EditText mEmailEdit;
49 private EditText mCompanyEdit;
50 private EditText mAddressLine1Edit;
51 private EditText mAddressLine2Edit;
52 private EditText mCityEdit;
53 private EditText mStateEdit;
54 private EditText mZipEdit;
55 private EditText mCountryEdit;
56 private EditText mPhoneEdit;
57
Ben Murdoche4c59f92011-10-18 12:01:07 +010058 private MenuItem mSaveMenuItem;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070059 private MenuItem mDeleteMenuItem;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000060
Ben Murdoch6f336402011-10-27 13:57:33 +010061 private boolean mInitialised;
62
Ben Murdoch23da30e2010-10-26 15:18:44 +010063 // Used to display toast after DB interactions complete.
64 private Handler mHandler;
John Reck35e9dd62011-04-25 09:01:54 -070065 private BrowserSettings mSettings;
Ben Murdoch23da30e2010-10-26 15:18:44 +010066
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010067 // For now we support just one profile so it's safe to hardcode the
68 // id to 1 here. In the future this unique identifier will be set
69 // dynamically.
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010070
Ben Murdoch9d9306d2011-01-18 16:06:00 +000071 private class PhoneNumberValidator implements TextWatcher {
72 // Keep in sync with kPhoneNumberLength in chrome/browser/autofill/phone_number.cc
73 private static final int PHONE_NUMBER_LENGTH = 7;
Ben Murdoch366824d2011-01-18 19:42:08 +000074 private static final String PHONE_NUMBER_SEPARATORS_REGEX = "[\\s\\.\\(\\)-]";
Ben Murdoch9d9306d2011-01-18 16:06:00 +000075
76 public void afterTextChanged(Editable s) {
Ben Murdoch366824d2011-01-18 19:42:08 +000077 String phoneNumber = s.toString();
78 int phoneNumberLength = phoneNumber.length();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000079
Ben Murdoch366824d2011-01-18 19:42:08 +000080 // Strip out any phone number separators.
81 phoneNumber = phoneNumber.replaceAll(PHONE_NUMBER_SEPARATORS_REGEX, "");
82
83 int strippedPhoneNumberLength = phoneNumber.length();
84
85 if (phoneNumberLength > 0 && strippedPhoneNumberLength < PHONE_NUMBER_LENGTH) {
Ben Murdoch9d9306d2011-01-18 16:06:00 +000086 mPhoneEdit.setError(getResources().getText(
87 R.string.autofill_profile_editor_phone_number_invalid));
88 } else {
89 mPhoneEdit.setError(null);
90 }
91
Ben Murdoche4c59f92011-10-18 12:01:07 +010092 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070093 updateDeleteMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000094 }
95
96 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
97 }
98
99 public void onTextChanged(CharSequence s, int start, int before, int count) {
100 }
101 }
102
103 private class FieldChangedListener implements TextWatcher {
104 public void afterTextChanged(Editable s) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100105 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700106 updateDeleteMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000107 }
108
109 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
110 }
111
112 public void onTextChanged(CharSequence s, int start, int before, int count) {
113 }
114
115 }
116
117 private TextWatcher mFieldChangedListener = new FieldChangedListener();
118
Ben Murdochaf554522010-09-10 22:09:30 +0100119 @Override
120 public void onCreate(Bundle savedState) {
121 super.onCreate(savedState);
Ben Murdoch815752a2011-06-16 19:47:08 +0100122 setHasOptionsMenu(true);
John Reck35e9dd62011-04-25 09:01:54 -0700123 mSettings = BrowserSettings.getInstance();
Ben Murdochaf554522010-09-10 22:09:30 +0100124 }
125
126 @Override
Ben Murdoch815752a2011-06-16 19:47:08 +0100127 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
128 inflater.inflate(R.menu.autofill_profile_editor, menu);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100129 mSaveMenuItem = menu.findItem(R.id.autofill_profile_editor_save_profile_menu_id);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700130 mDeleteMenuItem = menu.findItem(R.id.autofill_profile_editor_delete_profile_menu_id);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100131 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700132 updateDeleteMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100133 }
134
135 @Override
136 public boolean onOptionsItemSelected(MenuItem item) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100137 switch (item.getItemId()) {
138 case R.id.autofill_profile_editor_delete_profile_menu_id:
Ben Murdoch815752a2011-06-16 19:47:08 +0100139 // Clear the UI.
140 mFullNameEdit.setText("");
141 mEmailEdit.setText("");
142 mCompanyEdit.setText("");
143 mAddressLine1Edit.setText("");
144 mAddressLine2Edit.setText("");
145 mCityEdit.setText("");
146 mStateEdit.setText("");
147 mZipEdit.setText("");
148 mCountryEdit.setText("");
149 mPhoneEdit.setText("");
150
151 // Update browser settings and native with a null profile. This will
152 // trigger the current profile to get deleted from the DB.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800153 mSettings.updateAutoFillProfile(null);
154
Ben Murdoche4c59f92011-10-18 12:01:07 +0100155 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700156 updateDeleteMenuItemState();
Vivek Sekhar3e5e42d2014-06-05 12:17:59 -0700157 Toast.makeText(getActivity(), R.string.autofill_profile_successful_delete,
158 Toast.LENGTH_SHORT).show();
Ben Murdoch815752a2011-06-16 19:47:08 +0100159 return true;
Ben Murdoche4c59f92011-10-18 12:01:07 +0100160
161 case R.id.autofill_profile_editor_save_profile_menu_id:
162 AutoFillProfile newProfile = new AutoFillProfile(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800163 mSettings.getAutoFillProfileId(),
Ben Murdoche4c59f92011-10-18 12:01:07 +0100164 mFullNameEdit.getText().toString(),
165 mEmailEdit.getText().toString(),
166 mCompanyEdit.getText().toString(),
167 mAddressLine1Edit.getText().toString(),
168 mAddressLine2Edit.getText().toString(),
169 mCityEdit.getText().toString(),
170 mStateEdit.getText().toString(),
171 mZipEdit.getText().toString(),
172 mCountryEdit.getText().toString(),
173 mPhoneEdit.getText().toString());
174
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800175 mSettings.updateAutoFillProfile(newProfile);
Vivek Sekhar3e5e42d2014-06-05 12:17:59 -0700176 Toast.makeText(getActivity(), R.string.autofill_profile_successful_save,
177 Toast.LENGTH_SHORT).show();
178 closeEditor();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800179
Ben Murdoche4c59f92011-10-18 12:01:07 +0100180 return true;
181
182 default:
183 return false;
Ben Murdoch815752a2011-06-16 19:47:08 +0100184 }
Ben Murdoch815752a2011-06-16 19:47:08 +0100185 }
186
187 @Override
Ben Murdochaf554522010-09-10 22:09:30 +0100188 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100189 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +0100190 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
191
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100192 mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit);
193 mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit);
194 mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit);
195 mAddressLine1Edit = (EditText)v.findViewById(
196 R.id.autofill_profile_editor_address_line_1_edit);
197 mAddressLine2Edit = (EditText)v.findViewById(
198 R.id.autofill_profile_editor_address_line_2_edit);
199 mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit);
200 mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit);
201 mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit);
202 mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit);
203 mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit);
204
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000205 mFullNameEdit.addTextChangedListener(mFieldChangedListener);
206 mEmailEdit.addTextChangedListener(mFieldChangedListener);
207 mCompanyEdit.addTextChangedListener(mFieldChangedListener);
208 mAddressLine1Edit.addTextChangedListener(mFieldChangedListener);
209 mAddressLine2Edit.addTextChangedListener(mFieldChangedListener);
210 mCityEdit.addTextChangedListener(mFieldChangedListener);
211 mStateEdit.addTextChangedListener(mFieldChangedListener);
212 mZipEdit.addTextChangedListener(mFieldChangedListener);
213 mCountryEdit.addTextChangedListener(mFieldChangedListener);
214 mPhoneEdit.addTextChangedListener(new PhoneNumberValidator());
215
Ben Murdoch0cb81892010-10-08 12:41:33 +0100216 // Populate the text boxes with any pre existing AutoFill data.
John Reck35e9dd62011-04-25 09:01:54 -0700217 AutoFillProfile activeProfile = mSettings.getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100218 if (activeProfile != null) {
219 mFullNameEdit.setText(activeProfile.getFullName());
220 mEmailEdit.setText(activeProfile.getEmailAddress());
221 mCompanyEdit.setText(activeProfile.getCompanyName());
222 mAddressLine1Edit.setText(activeProfile.getAddressLine1());
223 mAddressLine2Edit.setText(activeProfile.getAddressLine2());
224 mCityEdit.setText(activeProfile.getCity());
225 mStateEdit.setText(activeProfile.getState());
226 mZipEdit.setText(activeProfile.getZipCode());
227 mCountryEdit.setText(activeProfile.getCountry());
228 mPhoneEdit.setText(activeProfile.getPhoneNumber());
229 }
Ben Murdochaf554522010-09-10 22:09:30 +0100230
Ben Murdoch6f336402011-10-27 13:57:33 +0100231 mInitialised = true;
232
Ben Murdoche4c59f92011-10-18 12:01:07 +0100233 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700234 updateDeleteMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000235
Ben Murdochaf554522010-09-10 22:09:30 +0100236 return v;
237 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000238
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700239 private void updateDeleteMenuItemState() {
240 if (mDeleteMenuItem == null) {
241 return;
242 }
243
244 if (!mInitialised) {
245 mDeleteMenuItem.setEnabled(false);
246 return;
247 }
248
249 boolean currentState = mDeleteMenuItem.isEnabled();
250 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
251 mEmailEdit.getText().toString().length() > 0 ||
252 mCompanyEdit.getText().toString().length() > 0 ||
253 mAddressLine1Edit.getText().toString().length() > 0 ||
254 mAddressLine2Edit.getText().toString().length() > 0 ||
255 mCityEdit.getText().toString().length() > 0 ||
256 mStateEdit.getText().toString().length() > 0 ||
257 mZipEdit.getText().toString().length() > 0 ||
258 mCountryEdit.getText().toString().length() > 0) &&
259 mPhoneEdit.getError() == null;
260
261 if (currentState != newState) {
262 mDeleteMenuItem.setEnabled(newState);
263 }
264 }
265
Ben Murdoche4c59f92011-10-18 12:01:07 +0100266 private void updateSaveMenuItemState() {
267 if (mSaveMenuItem == null) {
268 return;
269 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000270
Ben Murdoch6f336402011-10-27 13:57:33 +0100271 if (!mInitialised) {
272 mSaveMenuItem.setEnabled(false);
273 return;
274 }
275
Ben Murdoche4c59f92011-10-18 12:01:07 +0100276 boolean currentState = mSaveMenuItem.isEnabled();
277 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000278 mEmailEdit.getText().toString().length() > 0 ||
279 mCompanyEdit.getText().toString().length() > 0 ||
280 mAddressLine1Edit.getText().toString().length() > 0 ||
281 mAddressLine2Edit.getText().toString().length() > 0 ||
282 mCityEdit.getText().toString().length() > 0 ||
283 mStateEdit.getText().toString().length() > 0 ||
284 mZipEdit.getText().toString().length() > 0 ||
285 mCountryEdit.getText().toString().length() > 0) &&
286 mPhoneEdit.getError() == null;
287
Ben Murdoche4c59f92011-10-18 12:01:07 +0100288 if (currentState != newState) {
289 mSaveMenuItem.setEnabled(newState);
290 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000291 }
292
293 private void closeEditor() {
294 // Hide the IME if the user wants to close while an EditText has focus
295 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
296 Context.INPUT_METHOD_SERVICE);
297 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
298 if (getFragmentManager().getBackStackEntryCount() > 0) {
299 getFragmentManager().popBackStack();
300 } else {
301 getActivity().finish();
302 }
303 }
Ben Murdochaf554522010-09-10 22:09:30 +0100304}