OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.autofill; | 5 package org.chromium.chrome.browser.autofill; |
6 | 6 |
7 import java.util.ArrayList; | |
8 import java.util.List; | |
9 | |
10 import android.app.AlertDialog; | 7 import android.app.AlertDialog; |
11 import android.content.Context; | 8 import android.content.Context; |
12 import android.content.DialogInterface; | 9 import android.content.DialogInterface; |
13 | 10 |
14 import android.content.DialogInterface.OnClickListener; | 11 import android.content.DialogInterface.OnClickListener; |
15 import android.text.TextUtils; | 12 import android.text.TextUtils; |
16 import android.view.View; | 13 import android.view.View; |
17 import android.widget.AdapterView; | 14 import android.widget.AdapterView; |
18 import android.widget.AdapterView.OnItemSelectedListener; | 15 import android.widget.AdapterView.OnItemSelectedListener; |
19 import android.widget.EditText; | 16 import android.widget.EditText; |
20 import android.widget.ScrollView; | 17 import android.widget.ScrollView; |
21 | 18 |
22 import org.chromium.chrome.R; | 19 import org.chromium.chrome.R; |
23 | 20 |
21 import java.util.ArrayList; | |
22 import java.util.Arrays; | |
23 import java.util.List; | |
24 | |
24 /** | 25 /** |
25 * This is the dialog that will act as the java side controller between the | 26 * This is the dialog that will act as the java side controller between the |
26 * AutofillDialogGlue object and the UI elements on the page. It contains the | 27 * AutofillDialogGlue object and the UI elements on the page. It contains the |
27 * title and the content views and relays messages from the backend to them. | 28 * title and the content views and relays messages from the backend to them. |
28 */ | 29 */ |
29 public class AutofillDialog extends AlertDialog | 30 public class AutofillDialog extends AlertDialog |
30 implements OnClickListener, OnItemSelectedListener { | 31 implements OnClickListener, OnItemSelectedListener { |
32 private static final int ADD_MENU_ITEM_INDEX = -1; | |
33 private static final int EDIT_MENU_ITEM_INDEX = -2; | |
Ted C
2013/03/20 19:04:31
blank line after the statics
Yusuf
2013/03/21 00:53:17
Done.
| |
31 private AutofillDialogContentView mContentView; | 34 private AutofillDialogContentView mContentView; |
32 private AutofillDialogTitleView mTitleView; | 35 private AutofillDialogTitleView mTitleView; |
33 private AutofillDialogField[][] mAutofillSectionFieldData = | 36 private AutofillDialogField[][] mAutofillSectionFieldData = |
34 new AutofillDialogField[AutofillDialogConstants.NUM_SECTIONS][]; | 37 new AutofillDialogField[AutofillDialogConstants.NUM_SECTIONS][]; |
35 private AutofillDialogMenuItem[][] mAutofillSectionMenuData = | 38 private AutofillDialogMenuItem[][] mAutofillSectionMenuData = |
36 new AutofillDialogMenuItem[AutofillDialogConstants.NUM_SECTIONS][]; | 39 new AutofillDialogMenuItem[AutofillDialogConstants.NUM_SECTIONS][]; |
40 private final AutofillDialogMenuItem[][] mDefaultMenuItems = | |
41 new AutofillDialogMenuItem[AutofillDialogConstants.NUM_SECTIONS][]; | |
42 private static final List<String> mDefaultAccountItems = new ArrayList<Strin g>(); | |
Ted C
2013/03/20 19:04:31
static?
Yusuf
2013/03/21 00:53:17
Done.
| |
43 private AutofillDialogDelegate mDelegate; | |
37 private boolean mWillDismiss = false; | 44 private boolean mWillDismiss = false; |
38 | 45 |
39 /** | 46 /** |
40 * An interface to handle the interaction with an AutofillDialog object. | 47 * An interface to handle the interaction with an AutofillDialog object. |
41 */ | 48 */ |
42 public interface AutofillDialogDelegate { | 49 public interface AutofillDialogDelegate { |
43 | 50 |
44 /** | 51 /** |
45 * Informs AutofillDialog controller that a menu item was selected. | 52 * Informs AutofillDialog controller that a menu item was selected. |
46 * @param section Section in which a menu item was selected. Should matc h one of the values | 53 * @param section Section in which a menu item was selected. Should matc h one of the values |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 * Informs AutofillDialog controller that the user clicked on the submit button. | 87 * Informs AutofillDialog controller that the user clicked on the submit button. |
81 */ | 88 */ |
82 public void dialogSubmit(); | 89 public void dialogSubmit(); |
83 | 90 |
84 /** | 91 /** |
85 * Informs AutofillDialog controller that the user clicked on the cancel button. | 92 * Informs AutofillDialog controller that the user clicked on the cancel button. |
86 */ | 93 */ |
87 public void dialogCancel(); | 94 public void dialogCancel(); |
88 } | 95 } |
89 | 96 |
90 protected AutofillDialog(Context context) { | 97 protected AutofillDialog(Context context, AutofillDialogDelegate delegate) { |
91 super(context); | 98 super(context); |
99 mDelegate = delegate; | |
92 | 100 |
93 addTitleWithPlaceholders(); | 101 addTitleWithPlaceholders(); |
94 | 102 |
95 ScrollView scroll = new ScrollView(context); | 103 ScrollView scroll = new ScrollView(context); |
96 mContentView = (AutofillDialogContentView) getLayoutInflater(). | 104 mContentView = (AutofillDialogContentView) getLayoutInflater(). |
97 inflate(R.layout.autofill_dialog_content, null); | 105 inflate(R.layout.autofill_dialog_content, null); |
98 scroll.addView(mContentView); | 106 scroll.addView(mContentView); |
99 setView(scroll); | 107 setView(scroll); |
100 | 108 |
101 setButton(AlertDialog.BUTTON_NEGATIVE, | 109 setButton(AlertDialog.BUTTON_NEGATIVE, |
102 getContext().getResources().getString(R.string.autofill_negative _button), this); | 110 getContext().getResources().getString(R.string.autofill_negative _button), this); |
103 setButton(AlertDialog.BUTTON_POSITIVE, | 111 setButton(AlertDialog.BUTTON_POSITIVE, |
104 getContext().getResources().getString(R.string.autofill_positive _button), this); | 112 getContext().getResources().getString(R.string.autofill_positive _button), this); |
105 | 113 |
106 mContentView.setOnItemSelectedListener(this); | 114 mContentView.setOnItemSelectedListener(this); |
115 | |
116 AutofillDialogMenuItem[] billingItems = { | |
117 new AutofillDialogMenuItem(ADD_MENU_ITEM_INDEX, | |
118 getContext().getResources().getString(R.string.autofill_ new_billing)), | |
119 new AutofillDialogMenuItem(EDIT_MENU_ITEM_INDEX, | |
120 getContext().getResources().getString(R.string.autofill_ edit_billing)) | |
121 }; | |
122 AutofillDialogMenuItem[] shippingItems = { | |
123 new AutofillDialogMenuItem(ADD_MENU_ITEM_INDEX, | |
124 getContext().getResources().getString(R.string.autofill_ new_shipping)), | |
125 new AutofillDialogMenuItem(EDIT_MENU_ITEM_INDEX, | |
126 getContext().getResources().getString(R.string.autofill_ edit_shipping)) | |
127 }; | |
128 | |
129 mDefaultMenuItems[AutofillDialogConstants.SECTION_CC_BILLING] = billingI tems; | |
130 mDefaultMenuItems[AutofillDialogConstants.SECTION_SHIPPING] = shippingIt ems; | |
131 | |
132 mDefaultAccountItems.add(getContext(). | |
133 getResources().getString(R.string.autofill_new_account)); | |
134 mDefaultAccountItems.add(getContext(). | |
135 getResources().getString(R.string.autofill_use_local)); | |
Ted C
2013/03/20 19:04:31
everywhere in this method, you should be using con
Yusuf
2013/03/21 00:53:17
Done.
| |
107 } | 136 } |
108 | 137 |
109 @Override | 138 @Override |
110 public void dismiss() { | 139 public void dismiss() { |
111 if (mWillDismiss) super.dismiss(); | 140 if (mWillDismiss) super.dismiss(); |
112 } | 141 } |
113 | 142 |
114 @Override | 143 @Override |
115 public void onClick(DialogInterface dialog, int which) { | 144 public void onClick(DialogInterface dialog, int which) { |
116 if (mContentView.getCurrentLayout() == AutofillDialogContentView.LAYOUT_ STEADY) { | 145 if (!mContentView.isInEditingMode()) { |
117 mWillDismiss = true; | 146 mWillDismiss = true; |
147 if (which == AlertDialog.BUTTON_POSITIVE) mDelegate.dialogSubmit(); | |
148 else mDelegate.dialogCancel(); | |
118 return; | 149 return; |
119 } | 150 } |
120 | 151 |
152 int section = mContentView.getCurrentSection(); | |
153 assert(section != AutofillDialogUtils.INVALID_SECTION); | |
154 | |
155 if (which == AlertDialog.BUTTON_POSITIVE) mDelegate.editingComplete(sect ion); | |
156 else mDelegate.editingCancel(section); | |
157 | |
121 mContentView.changeLayoutTo(AutofillDialogContentView.LAYOUT_STEADY); | 158 mContentView.changeLayoutTo(AutofillDialogContentView.LAYOUT_STEADY); |
122 getButton(BUTTON_POSITIVE).setText(R.string.autofill_positive_button); | 159 getButton(BUTTON_POSITIVE).setText(R.string.autofill_positive_button); |
123 mWillDismiss = false; | 160 mWillDismiss = false; |
124 } | 161 } |
125 | 162 |
126 @Override | 163 @Override |
127 public void onItemSelected(AdapterView<?> spinner, View view, int position, | 164 public void onItemSelected(AdapterView<?> spinner, View view, int position, |
128 long id) { | 165 long id) { |
Ted C
2013/03/20 19:04:31
since you're here, will long id fit on the previou
Yusuf
2013/03/21 00:53:17
Done.
| |
129 if (spinner.getId() == R.id.accounts_spinner) { | 166 if (spinner.getId() == R.id.accounts_spinner) return; |
130 if (spinner.getItemAtPosition(position).toString().equals( | 167 |
131 getContext().getResources().getString(R.string.autofill_use_ local))) { | 168 int section = AutofillDialogUtils.getSectionForSpinnerID(spinner.getId() ); |
132 mContentView.changeLayoutTo(AutofillDialogContentView.LAYOUT_STE ADY); | 169 |
133 } else { | 170 if (!selectionShouldChangeLayout(spinner, section, position)) { |
134 mContentView.changeLayoutTo(AutofillDialogContentView.LAYOUT_FET CHING); | 171 mDelegate.itemSelected(section, position); |
135 } | |
136 return; | 172 return; |
137 } | 173 } |
138 | 174 |
139 int section = AutofillDialogUtils.getSectionForSpinnerID(spinner.getId() ); | 175 mDelegate.editingStart(section); |
140 | 176 AutofillDialogMenuItem currentItem = |
141 if (!mContentView.selectionShouldChangeLayout(spinner, section, position )) return; | 177 (AutofillDialogMenuItem) spinner.getItemAtPosition(position); |
142 | 178 if (currentItem.mIndex == ADD_MENU_ITEM_INDEX) clearAutofillSectionField Values(section); |
143 mContentView.changeLayoutTo(AutofillDialogContentView.getLayoutModeForSe ction(section)); | 179 mContentView.changeLayoutTo(AutofillDialogContentView.getLayoutModeForSe ction(section)); |
144 spinner.setSelection(0); | |
145 getButton(BUTTON_POSITIVE).setText(R.string.autofill_positive_button_edi ting); | 180 getButton(BUTTON_POSITIVE).setText(R.string.autofill_positive_button_edi ting); |
146 } | 181 } |
147 | 182 |
148 @Override | 183 @Override |
149 public void onNothingSelected(AdapterView<?> spinner) { | 184 public void onNothingSelected(AdapterView<?> spinner) { |
150 } | 185 } |
151 | 186 |
187 /** | |
188 * @param spinner The dropdown that was selected by the user. | |
189 * @param section The section that the dropdown corresponds to. | |
190 * @param position The position for the selected item in the dropdown. | |
191 * @return Whether the selection should cause a layout state change. | |
192 */ | |
193 private boolean selectionShouldChangeLayout(AdapterView<?> spinner, int sect ion, int position) { | |
194 int numDefaultItems = mDefaultMenuItems[section] != null ? | |
195 mDefaultMenuItems[section].length : 0; | |
196 return position >= spinner.getCount() - numDefaultItems; | |
197 } | |
198 | |
152 //TODO(yusufo): Remove this. updateAccountChooserAndAddTitle will get initia ted from glue. | 199 //TODO(yusufo): Remove this. updateAccountChooserAndAddTitle will get initia ted from glue. |
153 private void addTitleWithPlaceholders() { | 200 private void addTitleWithPlaceholders() { |
154 List<String> accounts = new ArrayList<String>(); | 201 String[] accounts = { |
155 accounts.add("placeholder@gmail.com"); | 202 "placeholder@gmail.com", |
156 accounts.add("placeholder@google.com"); | 203 "placeholder@google.com" |
157 updateAccountChooserAndAddTitle(accounts); | 204 }; |
205 updateAccountChooserAndAddTitle(accounts, 0); | |
158 } | 206 } |
159 | 207 |
160 /** | 208 /** |
161 * Update account chooser dropdown with given accounts and create the title view if needed. | 209 * Update account chooser dropdown with given accounts and create the title view if needed. |
162 * @param accounts The accounts to be used for the dropdown. | 210 * @param accounts The accounts to be used for the dropdown. |
211 * @param selectedAccountIndex The index of a currently selected account or -1 | |
212 * if the local payments should be used. | |
163 */ | 213 */ |
164 public void updateAccountChooserAndAddTitle(List<String> accounts) { | 214 public void updateAccountChooserAndAddTitle(String[] accounts, int selectedA ccountIndex) { |
215 ArrayList<String> combinedItems = | |
216 new ArrayList<String>(accounts.length + mDefaultAccountItems.siz e()); | |
217 combinedItems.addAll(Arrays.asList(accounts)); | |
218 combinedItems.addAll(mDefaultAccountItems); | |
165 if (mTitleView == null) { | 219 if (mTitleView == null) { |
166 mTitleView = new AutofillDialogTitleView(getContext(), accounts); | 220 mTitleView = new AutofillDialogTitleView(getContext(), combinedItems ); |
167 mTitleView.setOnItemSelectedListener(this); | 221 mTitleView.setOnItemSelectedListener(this); |
168 setCustomTitle(mTitleView); | 222 setCustomTitle(mTitleView); |
169 return; | 223 return; |
170 } | 224 } |
171 // TODO(yusufo): Add code to update accounts after title is created. | 225 mTitleView.updateAccountsAndSelect(combinedItems, selectedAccountIndex); |
172 } | 226 } |
173 | 227 |
174 /** | 228 /** |
229 * Notifies the dialog that the underlying model is changed and all sections will be updated. | |
230 * Any editing should be invalidated. | |
231 * The dialog should either go to the FETCHING or to the STEADY mode. | |
232 * @param fetchingIsActive If true, the data is being fetched and is not yet available. | |
233 */ | |
234 public void modelChanged(boolean fetchingIsActive) { | |
235 if (fetchingIsActive) { | |
236 mContentView.changeLayoutTo(AutofillDialogContentView.LAYOUT_FETCHIN G); | |
237 } else { | |
238 mContentView.changeLayoutTo(AutofillDialogContentView.LAYOUT_STEADY) ; | |
239 } | |
240 } | |
241 | |
242 /** | |
175 * Update given section with the data provided. This fills out the related { @link EditText} | 243 * Update given section with the data provided. This fills out the related { @link EditText} |
176 * fields in the layout corresponding to the section. | 244 * fields in the layout corresponding to the section. |
177 * @param section The section to update with the given data. | 245 * @param section The section to update with the given data. |
178 * @param visible Whether the section should be visible. | 246 * @param visible Whether the section should be visible. |
179 * @param dialogInputs The array that contains the data for each field in th e section. | 247 * @param dialogInputs The array that contains the data for each field in th e section. |
180 * @param menuItems The array that contains the dropdown items to be shown f or the section. | 248 * @param menuItems The array that contains the dropdown items to be shown f or the section. |
181 */ | 249 */ |
182 public void updateSection(int section, boolean visible, AutofillDialogField[ ] dialogInputs, | 250 public void updateSection(int section, boolean visible, AutofillDialogField[ ] dialogInputs, |
183 AutofillDialogMenuItem[] menuItems) { | 251 AutofillDialogMenuItem[] menuItems) { |
184 EditText currentField; | 252 EditText currentField; |
185 String inputValue; | 253 String inputValue; |
186 for (int i = 0; i < dialogInputs.length; i++) { | 254 for (int i = 0; i < dialogInputs.length; i++) { |
187 currentField = (EditText) findViewById(AutofillDialogUtils.getEditTe xtIDForField( | 255 currentField = (EditText) findViewById(AutofillDialogUtils.getEditTe xtIDForField( |
188 section, dialogInputs[i].mFieldType)); | 256 section, dialogInputs[i].mFieldType)); |
189 if (currentField == null) continue; | 257 if (currentField == null) continue; |
190 currentField.setHint(dialogInputs[i].mPlaceholder); | 258 currentField.setHint(dialogInputs[i].mPlaceholder); |
191 inputValue = dialogInputs[i].getValue(); | 259 inputValue = dialogInputs[i].getValue(); |
192 if (TextUtils.isEmpty(inputValue)) currentField.setText(""); | 260 if (TextUtils.isEmpty(inputValue)) currentField.setText(""); |
193 else currentField.setText(inputValue); | 261 else currentField.setText(inputValue); |
194 } | 262 } |
195 mAutofillSectionFieldData[section] = dialogInputs; | 263 mAutofillSectionFieldData[section] = dialogInputs; |
196 mContentView.setVisibilityForSection(section, visible); | 264 mContentView.setVisibilityForSection(section, visible); |
197 mContentView.updateMenuItemsForSection(section, menuItems); | 265 List<AutofillDialogMenuItem> combinedItems; |
266 if (mDefaultMenuItems[section] == null) { | |
267 combinedItems = Arrays.asList(menuItems); | |
268 } else { | |
269 combinedItems = new ArrayList<AutofillDialogMenuItem>( | |
270 menuItems.length + mDefaultMenuItems[section].length); | |
271 combinedItems.addAll(Arrays.asList(menuItems)); | |
272 combinedItems.addAll(Arrays.asList(mDefaultMenuItems[section])); | |
273 } | |
274 mContentView.updateMenuItemsForSection(section, combinedItems); | |
198 mAutofillSectionMenuData[section] = menuItems; | 275 mAutofillSectionMenuData[section] = menuItems; |
199 } | 276 } |
200 | 277 |
201 /** | 278 /** |
202 * Clears the field values for the given section. | 279 * Clears the field values for the given section. |
203 * @param section The section to clear the field values for. | 280 * @param section The section to clear the field values for. |
204 */ | 281 */ |
205 public void clearAutofillSectionFieldValues(int section) { | 282 public void clearAutofillSectionFieldData(int section) { |
206 AutofillDialogField[] fieldData = mAutofillSectionFieldData[section]; | 283 AutofillDialogField[] fieldData = mAutofillSectionFieldData[section]; |
207 if (fieldData == null) return; | 284 if (fieldData == null) return; |
208 | 285 |
209 for (int i = 0; i < fieldData.length; i++) fieldData[i].setValue(""); | 286 for (int i = 0; i < fieldData.length; i++) fieldData[i].setValue(""); |
210 } | 287 } |
211 | 288 |
289 private void clearAutofillSectionFieldValues(int section) { | |
290 AutofillDialogField[] fieldData = mAutofillSectionFieldData[section]; | |
291 if (fieldData == null) return; | |
292 | |
293 EditText currentField; | |
294 for (int i = 0; i < fieldData.length; i++) { | |
295 currentField = (EditText) findViewById(AutofillDialogUtils.getEditTe xtIDForField( | |
296 section, fieldData[i].mFieldType)); | |
297 if (currentField == null) continue; | |
298 | |
299 currentField.setText(""); | |
300 } | |
301 } | |
302 | |
212 /** | 303 /** |
213 * Return the array that holds all the data about the fields in the given se ction. | 304 * Return the array that holds all the data about the fields in the given se ction. |
214 * @param section The section to return the data for. | 305 * @param section The section to return the data for. |
215 * @return An array containing the data for each field in the given section. | 306 * @return An array containing the data for each field in the given section. |
216 */ | 307 */ |
217 public AutofillDialogField[] getSection(int section) { | 308 public AutofillDialogField[] getSection(int section) { |
218 AutofillDialogField[] fieldData = mAutofillSectionFieldData[section]; | 309 AutofillDialogField[] fieldData = mAutofillSectionFieldData[section]; |
219 if (fieldData == null) return null; | 310 if (fieldData == null) return null; |
220 | 311 |
221 EditText currentField; | 312 EditText currentField; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 return false; | 353 return false; |
263 } | 354 } |
264 | 355 |
265 /** | 356 /** |
266 * Updates the progress for the final transaction with the given value. | 357 * Updates the progress for the final transaction with the given value. |
267 * @param value The current progress percentage value. | 358 * @param value The current progress percentage value. |
268 */ | 359 */ |
269 public void updateProgressBar(double value) { | 360 public void updateProgressBar(double value) { |
270 } | 361 } |
271 } | 362 } |
OLD | NEW |