blob: cc7710282df48097b2b29574f0b6ce09fdb181bd [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
67 private final static int PROFILE_SAVED_MSG = 100;
68 private final static int PROFILE_DELETED_MSG = 101;
69
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010070 // For now we support just one profile so it's safe to hardcode the
71 // id to 1 here. In the future this unique identifier will be set
72 // dynamically.
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010073
Ben Murdoch9d9306d2011-01-18 16:06:00 +000074 private class PhoneNumberValidator implements TextWatcher {
75 // Keep in sync with kPhoneNumberLength in chrome/browser/autofill/phone_number.cc
76 private static final int PHONE_NUMBER_LENGTH = 7;
Ben Murdoch366824d2011-01-18 19:42:08 +000077 private static final String PHONE_NUMBER_SEPARATORS_REGEX = "[\\s\\.\\(\\)-]";
Ben Murdoch9d9306d2011-01-18 16:06:00 +000078
79 public void afterTextChanged(Editable s) {
Ben Murdoch366824d2011-01-18 19:42:08 +000080 String phoneNumber = s.toString();
81 int phoneNumberLength = phoneNumber.length();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000082
Ben Murdoch366824d2011-01-18 19:42:08 +000083 // Strip out any phone number separators.
84 phoneNumber = phoneNumber.replaceAll(PHONE_NUMBER_SEPARATORS_REGEX, "");
85
86 int strippedPhoneNumberLength = phoneNumber.length();
87
88 if (phoneNumberLength > 0 && strippedPhoneNumberLength < PHONE_NUMBER_LENGTH) {
Ben Murdoch9d9306d2011-01-18 16:06:00 +000089 mPhoneEdit.setError(getResources().getText(
90 R.string.autofill_profile_editor_phone_number_invalid));
91 } else {
92 mPhoneEdit.setError(null);
93 }
94
Ben Murdoche4c59f92011-10-18 12:01:07 +010095 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070096 updateDeleteMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000097 }
98
99 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
100 }
101
102 public void onTextChanged(CharSequence s, int start, int before, int count) {
103 }
104 }
105
106 private class FieldChangedListener implements TextWatcher {
107 public void afterTextChanged(Editable s) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100108 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700109 updateDeleteMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000110 }
111
112 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
113 }
114
115 public void onTextChanged(CharSequence s, int start, int before, int count) {
116 }
117
118 }
119
120 private TextWatcher mFieldChangedListener = new FieldChangedListener();
121
Ben Murdochaf554522010-09-10 22:09:30 +0100122 public AutoFillSettingsFragment() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100123 mHandler = new Handler() {
124 @Override
125 public void handleMessage(Message msg) {
Ben Murdoch381280d2011-08-11 10:39:59 +0100126 Context c = getActivity();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100127 switch (msg.what) {
128 case PROFILE_SAVED_MSG:
Ben Murdoch381280d2011-08-11 10:39:59 +0100129 if (c != null) {
130 Toast.makeText(c, R.string.autofill_profile_successful_save,
131 Toast.LENGTH_SHORT).show();
132 closeEditor();
133 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100134 break;
Ben Murdochaf554522010-09-10 22:09:30 +0100135
Ben Murdoch23da30e2010-10-26 15:18:44 +0100136 case PROFILE_DELETED_MSG:
Ben Murdoch381280d2011-08-11 10:39:59 +0100137 if (c != null) {
138 Toast.makeText(c, R.string.autofill_profile_successful_delete,
139 Toast.LENGTH_SHORT).show();
140 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100141 break;
142 }
143 }
144 };
Ben Murdochaf554522010-09-10 22:09:30 +0100145 }
146
147 @Override
148 public void onCreate(Bundle savedState) {
149 super.onCreate(savedState);
Ben Murdoch815752a2011-06-16 19:47:08 +0100150 setHasOptionsMenu(true);
John Reck35e9dd62011-04-25 09:01:54 -0700151 mSettings = BrowserSettings.getInstance();
Ben Murdochaf554522010-09-10 22:09:30 +0100152 }
153
154 @Override
Ben Murdoch815752a2011-06-16 19:47:08 +0100155 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
156 inflater.inflate(R.menu.autofill_profile_editor, menu);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100157 mSaveMenuItem = menu.findItem(R.id.autofill_profile_editor_save_profile_menu_id);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700158 mDeleteMenuItem = menu.findItem(R.id.autofill_profile_editor_delete_profile_menu_id);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100159 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700160 updateDeleteMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100161 }
162
163 @Override
164 public boolean onOptionsItemSelected(MenuItem item) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100165 switch (item.getItemId()) {
166 case R.id.autofill_profile_editor_delete_profile_menu_id:
Ben Murdoch815752a2011-06-16 19:47:08 +0100167 // Clear the UI.
168 mFullNameEdit.setText("");
169 mEmailEdit.setText("");
170 mCompanyEdit.setText("");
171 mAddressLine1Edit.setText("");
172 mAddressLine2Edit.setText("");
173 mCityEdit.setText("");
174 mStateEdit.setText("");
175 mZipEdit.setText("");
176 mCountryEdit.setText("");
177 mPhoneEdit.setText("");
178
179 // Update browser settings and native with a null profile. This will
180 // trigger the current profile to get deleted from the DB.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800181 mSettings.updateAutoFillProfile(null);
182
Ben Murdoche4c59f92011-10-18 12:01:07 +0100183 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700184 updateDeleteMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100185 return true;
Ben Murdoche4c59f92011-10-18 12:01:07 +0100186
187 case R.id.autofill_profile_editor_save_profile_menu_id:
188 AutoFillProfile newProfile = new AutoFillProfile(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800189 mSettings.getAutoFillProfileId(),
Ben Murdoche4c59f92011-10-18 12:01:07 +0100190 mFullNameEdit.getText().toString(),
191 mEmailEdit.getText().toString(),
192 mCompanyEdit.getText().toString(),
193 mAddressLine1Edit.getText().toString(),
194 mAddressLine2Edit.getText().toString(),
195 mCityEdit.getText().toString(),
196 mStateEdit.getText().toString(),
197 mZipEdit.getText().toString(),
198 mCountryEdit.getText().toString(),
199 mPhoneEdit.getText().toString());
200
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800201 mSettings.updateAutoFillProfile(newProfile);
202
Ben Murdoche4c59f92011-10-18 12:01:07 +0100203 return true;
204
205 default:
206 return false;
Ben Murdoch815752a2011-06-16 19:47:08 +0100207 }
Ben Murdoch815752a2011-06-16 19:47:08 +0100208 }
209
210 @Override
Ben Murdochaf554522010-09-10 22:09:30 +0100211 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100212 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +0100213 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
214
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100215 mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit);
216 mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit);
217 mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit);
218 mAddressLine1Edit = (EditText)v.findViewById(
219 R.id.autofill_profile_editor_address_line_1_edit);
220 mAddressLine2Edit = (EditText)v.findViewById(
221 R.id.autofill_profile_editor_address_line_2_edit);
222 mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit);
223 mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit);
224 mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit);
225 mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit);
226 mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit);
227
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000228 mFullNameEdit.addTextChangedListener(mFieldChangedListener);
229 mEmailEdit.addTextChangedListener(mFieldChangedListener);
230 mCompanyEdit.addTextChangedListener(mFieldChangedListener);
231 mAddressLine1Edit.addTextChangedListener(mFieldChangedListener);
232 mAddressLine2Edit.addTextChangedListener(mFieldChangedListener);
233 mCityEdit.addTextChangedListener(mFieldChangedListener);
234 mStateEdit.addTextChangedListener(mFieldChangedListener);
235 mZipEdit.addTextChangedListener(mFieldChangedListener);
236 mCountryEdit.addTextChangedListener(mFieldChangedListener);
237 mPhoneEdit.addTextChangedListener(new PhoneNumberValidator());
238
Ben Murdoch0cb81892010-10-08 12:41:33 +0100239 // Populate the text boxes with any pre existing AutoFill data.
John Reck35e9dd62011-04-25 09:01:54 -0700240 AutoFillProfile activeProfile = mSettings.getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100241 if (activeProfile != null) {
242 mFullNameEdit.setText(activeProfile.getFullName());
243 mEmailEdit.setText(activeProfile.getEmailAddress());
244 mCompanyEdit.setText(activeProfile.getCompanyName());
245 mAddressLine1Edit.setText(activeProfile.getAddressLine1());
246 mAddressLine2Edit.setText(activeProfile.getAddressLine2());
247 mCityEdit.setText(activeProfile.getCity());
248 mStateEdit.setText(activeProfile.getState());
249 mZipEdit.setText(activeProfile.getZipCode());
250 mCountryEdit.setText(activeProfile.getCountry());
251 mPhoneEdit.setText(activeProfile.getPhoneNumber());
252 }
Ben Murdochaf554522010-09-10 22:09:30 +0100253
Ben Murdoch6f336402011-10-27 13:57:33 +0100254 mInitialised = true;
255
Ben Murdoche4c59f92011-10-18 12:01:07 +0100256 updateSaveMenuItemState();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700257 updateDeleteMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000258
Ben Murdochaf554522010-09-10 22:09:30 +0100259 return v;
260 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000261
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700262 private void updateDeleteMenuItemState() {
263 if (mDeleteMenuItem == null) {
264 return;
265 }
266
267 if (!mInitialised) {
268 mDeleteMenuItem.setEnabled(false);
269 return;
270 }
271
272 boolean currentState = mDeleteMenuItem.isEnabled();
273 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
274 mEmailEdit.getText().toString().length() > 0 ||
275 mCompanyEdit.getText().toString().length() > 0 ||
276 mAddressLine1Edit.getText().toString().length() > 0 ||
277 mAddressLine2Edit.getText().toString().length() > 0 ||
278 mCityEdit.getText().toString().length() > 0 ||
279 mStateEdit.getText().toString().length() > 0 ||
280 mZipEdit.getText().toString().length() > 0 ||
281 mCountryEdit.getText().toString().length() > 0) &&
282 mPhoneEdit.getError() == null;
283
284 if (currentState != newState) {
285 mDeleteMenuItem.setEnabled(newState);
286 }
287 }
288
Ben Murdoche4c59f92011-10-18 12:01:07 +0100289 private void updateSaveMenuItemState() {
290 if (mSaveMenuItem == null) {
291 return;
292 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000293
Ben Murdoch6f336402011-10-27 13:57:33 +0100294 if (!mInitialised) {
295 mSaveMenuItem.setEnabled(false);
296 return;
297 }
298
Ben Murdoche4c59f92011-10-18 12:01:07 +0100299 boolean currentState = mSaveMenuItem.isEnabled();
300 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000301 mEmailEdit.getText().toString().length() > 0 ||
302 mCompanyEdit.getText().toString().length() > 0 ||
303 mAddressLine1Edit.getText().toString().length() > 0 ||
304 mAddressLine2Edit.getText().toString().length() > 0 ||
305 mCityEdit.getText().toString().length() > 0 ||
306 mStateEdit.getText().toString().length() > 0 ||
307 mZipEdit.getText().toString().length() > 0 ||
308 mCountryEdit.getText().toString().length() > 0) &&
309 mPhoneEdit.getError() == null;
310
Ben Murdoche4c59f92011-10-18 12:01:07 +0100311 if (currentState != newState) {
312 mSaveMenuItem.setEnabled(newState);
313 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000314 }
315
316 private void closeEditor() {
317 // Hide the IME if the user wants to close while an EditText has focus
318 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
319 Context.INPUT_METHOD_SERVICE);
320 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
321 if (getFragmentManager().getBackStackEntryCount() > 0) {
322 getFragmentManager().popBackStack();
323 } else {
324 getActivity().finish();
325 }
326 }
Ben Murdochaf554522010-09-10 22:09:30 +0100327}