blob: e87cb892486bd148bd74228bf57914508426b581 [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;
Ben Murdoch9d9306d2011-01-18 16:06:00 +000059
Ben Murdoch6f336402011-10-27 13:57:33 +010060 private boolean mInitialised;
61
Ben Murdoch23da30e2010-10-26 15:18:44 +010062 // Used to display toast after DB interactions complete.
63 private Handler mHandler;
John Reck35e9dd62011-04-25 09:01:54 -070064 private BrowserSettings mSettings;
Ben Murdoch23da30e2010-10-26 15:18:44 +010065
66 private final static int PROFILE_SAVED_MSG = 100;
67 private final static int PROFILE_DELETED_MSG = 101;
68
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010069 // For now we support just one profile so it's safe to hardcode the
70 // id to 1 here. In the future this unique identifier will be set
71 // dynamically.
Ben Murdoch6fa32ba2010-10-20 14:01:25 +010072
Ben Murdoch9d9306d2011-01-18 16:06:00 +000073 private class PhoneNumberValidator implements TextWatcher {
74 // Keep in sync with kPhoneNumberLength in chrome/browser/autofill/phone_number.cc
75 private static final int PHONE_NUMBER_LENGTH = 7;
Ben Murdoch366824d2011-01-18 19:42:08 +000076 private static final String PHONE_NUMBER_SEPARATORS_REGEX = "[\\s\\.\\(\\)-]";
Ben Murdoch9d9306d2011-01-18 16:06:00 +000077
78 public void afterTextChanged(Editable s) {
Ben Murdoch366824d2011-01-18 19:42:08 +000079 String phoneNumber = s.toString();
80 int phoneNumberLength = phoneNumber.length();
Ben Murdoch9d9306d2011-01-18 16:06:00 +000081
Ben Murdoch366824d2011-01-18 19:42:08 +000082 // Strip out any phone number separators.
83 phoneNumber = phoneNumber.replaceAll(PHONE_NUMBER_SEPARATORS_REGEX, "");
84
85 int strippedPhoneNumberLength = phoneNumber.length();
86
87 if (phoneNumberLength > 0 && strippedPhoneNumberLength < PHONE_NUMBER_LENGTH) {
Ben Murdoch9d9306d2011-01-18 16:06:00 +000088 mPhoneEdit.setError(getResources().getText(
89 R.string.autofill_profile_editor_phone_number_invalid));
90 } else {
91 mPhoneEdit.setError(null);
92 }
93
Ben Murdoche4c59f92011-10-18 12:01:07 +010094 updateSaveMenuItemState();
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();
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 public AutoFillSettingsFragment() {
Ben Murdoch23da30e2010-10-26 15:18:44 +0100120 mHandler = new Handler() {
121 @Override
122 public void handleMessage(Message msg) {
Ben Murdoch381280d2011-08-11 10:39:59 +0100123 Context c = getActivity();
Ben Murdoch23da30e2010-10-26 15:18:44 +0100124 switch (msg.what) {
125 case PROFILE_SAVED_MSG:
Ben Murdoch381280d2011-08-11 10:39:59 +0100126 if (c != null) {
127 Toast.makeText(c, R.string.autofill_profile_successful_save,
128 Toast.LENGTH_SHORT).show();
129 closeEditor();
130 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100131 break;
Ben Murdochaf554522010-09-10 22:09:30 +0100132
Ben Murdoch23da30e2010-10-26 15:18:44 +0100133 case PROFILE_DELETED_MSG:
Ben Murdoch381280d2011-08-11 10:39:59 +0100134 if (c != null) {
135 Toast.makeText(c, R.string.autofill_profile_successful_delete,
136 Toast.LENGTH_SHORT).show();
137 }
Ben Murdoch23da30e2010-10-26 15:18:44 +0100138 break;
139 }
140 }
141 };
Ben Murdochaf554522010-09-10 22:09:30 +0100142 }
143
144 @Override
145 public void onCreate(Bundle savedState) {
146 super.onCreate(savedState);
Ben Murdoch815752a2011-06-16 19:47:08 +0100147 setHasOptionsMenu(true);
John Reck35e9dd62011-04-25 09:01:54 -0700148 mSettings = BrowserSettings.getInstance();
Ben Murdochaf554522010-09-10 22:09:30 +0100149 }
150
151 @Override
Ben Murdoch815752a2011-06-16 19:47:08 +0100152 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
153 inflater.inflate(R.menu.autofill_profile_editor, menu);
Ben Murdoche4c59f92011-10-18 12:01:07 +0100154 mSaveMenuItem = menu.findItem(R.id.autofill_profile_editor_save_profile_menu_id);
155 updateSaveMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100156 }
157
158 @Override
159 public boolean onOptionsItemSelected(MenuItem item) {
Ben Murdoche4c59f92011-10-18 12:01:07 +0100160 switch (item.getItemId()) {
161 case R.id.autofill_profile_editor_delete_profile_menu_id:
Ben Murdoch815752a2011-06-16 19:47:08 +0100162 // Clear the UI.
163 mFullNameEdit.setText("");
164 mEmailEdit.setText("");
165 mCompanyEdit.setText("");
166 mAddressLine1Edit.setText("");
167 mAddressLine2Edit.setText("");
168 mCityEdit.setText("");
169 mStateEdit.setText("");
170 mZipEdit.setText("");
171 mCountryEdit.setText("");
172 mPhoneEdit.setText("");
173
174 // Update browser settings and native with a null profile. This will
175 // trigger the current profile to get deleted from the DB.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800176 mSettings.updateAutoFillProfile(null);
177
Ben Murdoche4c59f92011-10-18 12:01:07 +0100178 updateSaveMenuItemState();
Ben Murdoch815752a2011-06-16 19:47:08 +0100179 return true;
Ben Murdoche4c59f92011-10-18 12:01:07 +0100180
181 case R.id.autofill_profile_editor_save_profile_menu_id:
182 AutoFillProfile newProfile = new AutoFillProfile(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800183 mSettings.getAutoFillProfileId(),
Ben Murdoche4c59f92011-10-18 12:01:07 +0100184 mFullNameEdit.getText().toString(),
185 mEmailEdit.getText().toString(),
186 mCompanyEdit.getText().toString(),
187 mAddressLine1Edit.getText().toString(),
188 mAddressLine2Edit.getText().toString(),
189 mCityEdit.getText().toString(),
190 mStateEdit.getText().toString(),
191 mZipEdit.getText().toString(),
192 mCountryEdit.getText().toString(),
193 mPhoneEdit.getText().toString());
194
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800195 mSettings.updateAutoFillProfile(newProfile);
196
Ben Murdoche4c59f92011-10-18 12:01:07 +0100197 return true;
198
199 default:
200 return false;
Ben Murdoch815752a2011-06-16 19:47:08 +0100201 }
Ben Murdoch815752a2011-06-16 19:47:08 +0100202 }
203
204 @Override
Ben Murdochaf554522010-09-10 22:09:30 +0100205 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100206 Bundle savedInstanceState) {
Ben Murdochaf554522010-09-10 22:09:30 +0100207 View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
208
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100209 mFullNameEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_name_edit);
210 mEmailEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_email_address_edit);
211 mCompanyEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_company_name_edit);
212 mAddressLine1Edit = (EditText)v.findViewById(
213 R.id.autofill_profile_editor_address_line_1_edit);
214 mAddressLine2Edit = (EditText)v.findViewById(
215 R.id.autofill_profile_editor_address_line_2_edit);
216 mCityEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_city_edit);
217 mStateEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_state_edit);
218 mZipEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_zip_code_edit);
219 mCountryEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_country_edit);
220 mPhoneEdit = (EditText)v.findViewById(R.id.autofill_profile_editor_phone_number_edit);
221
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000222 mFullNameEdit.addTextChangedListener(mFieldChangedListener);
223 mEmailEdit.addTextChangedListener(mFieldChangedListener);
224 mCompanyEdit.addTextChangedListener(mFieldChangedListener);
225 mAddressLine1Edit.addTextChangedListener(mFieldChangedListener);
226 mAddressLine2Edit.addTextChangedListener(mFieldChangedListener);
227 mCityEdit.addTextChangedListener(mFieldChangedListener);
228 mStateEdit.addTextChangedListener(mFieldChangedListener);
229 mZipEdit.addTextChangedListener(mFieldChangedListener);
230 mCountryEdit.addTextChangedListener(mFieldChangedListener);
231 mPhoneEdit.addTextChangedListener(new PhoneNumberValidator());
232
Ben Murdoch0cb81892010-10-08 12:41:33 +0100233 // Populate the text boxes with any pre existing AutoFill data.
John Reck35e9dd62011-04-25 09:01:54 -0700234 AutoFillProfile activeProfile = mSettings.getAutoFillProfile();
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100235 if (activeProfile != null) {
236 mFullNameEdit.setText(activeProfile.getFullName());
237 mEmailEdit.setText(activeProfile.getEmailAddress());
238 mCompanyEdit.setText(activeProfile.getCompanyName());
239 mAddressLine1Edit.setText(activeProfile.getAddressLine1());
240 mAddressLine2Edit.setText(activeProfile.getAddressLine2());
241 mCityEdit.setText(activeProfile.getCity());
242 mStateEdit.setText(activeProfile.getState());
243 mZipEdit.setText(activeProfile.getZipCode());
244 mCountryEdit.setText(activeProfile.getCountry());
245 mPhoneEdit.setText(activeProfile.getPhoneNumber());
246 }
Ben Murdochaf554522010-09-10 22:09:30 +0100247
Ben Murdoch6f336402011-10-27 13:57:33 +0100248 mInitialised = true;
249
Ben Murdoche4c59f92011-10-18 12:01:07 +0100250 updateSaveMenuItemState();
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000251
Ben Murdochaf554522010-09-10 22:09:30 +0100252 return v;
253 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000254
Ben Murdoche4c59f92011-10-18 12:01:07 +0100255 private void updateSaveMenuItemState() {
256 if (mSaveMenuItem == null) {
257 return;
258 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000259
Ben Murdoch6f336402011-10-27 13:57:33 +0100260 if (!mInitialised) {
261 mSaveMenuItem.setEnabled(false);
262 return;
263 }
264
Ben Murdoche4c59f92011-10-18 12:01:07 +0100265 boolean currentState = mSaveMenuItem.isEnabled();
266 boolean newState = (mFullNameEdit.getText().toString().length() > 0 ||
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000267 mEmailEdit.getText().toString().length() > 0 ||
268 mCompanyEdit.getText().toString().length() > 0 ||
269 mAddressLine1Edit.getText().toString().length() > 0 ||
270 mAddressLine2Edit.getText().toString().length() > 0 ||
271 mCityEdit.getText().toString().length() > 0 ||
272 mStateEdit.getText().toString().length() > 0 ||
273 mZipEdit.getText().toString().length() > 0 ||
274 mCountryEdit.getText().toString().length() > 0) &&
275 mPhoneEdit.getError() == null;
276
Ben Murdoche4c59f92011-10-18 12:01:07 +0100277 if (currentState != newState) {
278 mSaveMenuItem.setEnabled(newState);
279 }
Ben Murdoch9d9306d2011-01-18 16:06:00 +0000280 }
281
282 private void closeEditor() {
283 // Hide the IME if the user wants to close while an EditText has focus
284 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
285 Context.INPUT_METHOD_SERVICE);
286 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
287 if (getFragmentManager().getBackStackEntryCount() > 0) {
288 getFragmentManager().popBackStack();
289 } else {
290 getActivity().finish();
291 }
292 }
Ben Murdochaf554522010-09-10 22:09:30 +0100293}