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; | 7 import java.util.ArrayList; |
8 import java.util.List; | |
9 | 8 |
10 import android.content.Context; | 9 import android.content.Context; |
11 import android.view.LayoutInflater; | 10 import android.view.LayoutInflater; |
12 import android.widget.ArrayAdapter; | 11 import android.widget.ArrayAdapter; |
13 import android.widget.FrameLayout; | 12 import android.widget.FrameLayout; |
14 import android.widget.Spinner; | 13 import android.widget.Spinner; |
15 import android.widget.AdapterView.OnItemSelectedListener; | 14 import android.widget.AdapterView.OnItemSelectedListener; |
16 | 15 |
17 import org.chromium.chrome.R; | 16 import org.chromium.chrome.R; |
18 | 17 |
19 /** | 18 /** |
20 * This is the layout that contains the title items for the autofill dialog. | 19 * This is the layout that contains the title items for the autofill dialog. |
21 * In principle it shouldn't contain any logic related with the | 20 * In principle it shouldn't contain any logic related with the |
22 * actual workflow, but rather respond to any UI update messages coming to it | 21 * actual workflow, but rather respond to any UI update messages coming to it |
23 * from the AutofillDialog. It should also be not dependent on the UI state of | 22 * from the AutofillDialog. It should also be not dependent on the UI state of |
24 * the content. | 23 * the content. |
25 */ | 24 */ |
26 public class AutofillDialogTitleView extends FrameLayout { | 25 public class AutofillDialogTitleView extends FrameLayout { |
27 private static final List<String> mConstantItems = new ArrayList<String>(); | |
28 | 26 |
29 private List<String> mAccountNames; | 27 private ArrayList<String> mAccountNames; |
30 private ArrayAdapter<String> mAdapter; | 28 private ArrayAdapter<String> mAdapter; |
31 | 29 |
32 /** | 30 /** |
33 * Create a title using the given context with only the default dropdown ite
ms. | 31 * Create a title using the given context with only the default dropdown ite
ms. |
34 * @param context The context to create the title within. | 32 * @param context The context to create the title within. |
35 */ | 33 */ |
36 public AutofillDialogTitleView(Context context) { | 34 public AutofillDialogTitleView(Context context) { |
37 super(context); | 35 super(context); |
38 if (mConstantItems.isEmpty()) { | |
39 mConstantItems.add(getResources().getString(R.string.autofill_new_ac
count)); | |
40 mConstantItems.add(getResources().getString(R.string.autofill_use_lo
cal)); | |
41 } | |
42 | 36 |
43 LayoutInflater.from(context).inflate(R.layout.autofill_dialog_title, thi
s, true); | 37 LayoutInflater.from(context).inflate(R.layout.autofill_dialog_title, thi
s, true); |
44 Spinner accounts_spinner = (Spinner)findViewById(R.id.accounts_spinner); | 38 Spinner accountsSpinner = (Spinner)findViewById(R.id.accounts_spinner); |
45 mAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spi
nner_item); | 39 mAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spi
nner_item); |
46 mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdow
n_item); | 40 mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdow
n_item); |
47 mAdapter.addAll(mConstantItems); | 41 accountsSpinner.setAdapter(mAdapter); |
48 accounts_spinner.setAdapter(mAdapter); | |
49 } | 42 } |
50 | 43 |
51 /** | 44 /** |
52 * Create a title using the given context with the given dropdown items. | 45 * Create a title using the given context with the given dropdown items. |
53 * @param context The context to create the title within. | 46 * @param context The context to create the title within. |
54 * @param accountNames The dropdown items to be listed. | 47 * @param accountNames The dropdown items to be listed. |
55 */ | 48 */ |
56 public AutofillDialogTitleView(Context context, List<String> accountNames) { | 49 public AutofillDialogTitleView(Context context, ArrayList<String> accountNam
es) { |
57 this(context); | 50 this(context); |
58 mAccountNames = accountNames; | 51 updateAccountsAndSelect(accountNames, 0); |
59 mAdapter.clear(); | |
60 mAdapter.addAll(mAccountNames); | |
61 mAdapter.addAll(mConstantItems); | |
62 } | 52 } |
63 | 53 |
64 /** | 54 /** |
| 55 * Update account chooser dropdown with given accounts and create the title
view if needed. |
| 56 * @param accountNames The dropdown items to be listed. |
| 57 * @param selectedAccountIndex The index of a currently selected account or
-1 |
| 58 * if the local payments should be used. |
| 59 */ |
| 60 public void updateAccountsAndSelect(ArrayList<String> accountNames, |
| 61 int selectedAccountIndex) { |
| 62 mAdapter.clear(); |
| 63 mAccountNames = accountNames; |
| 64 mAdapter.addAll(mAccountNames); |
| 65 Spinner accountsSpinner = (Spinner)findViewById(R.id.accounts_spinner); |
| 66 if (selectedAccountIndex >= 0) { |
| 67 accountsSpinner.setSelection(selectedAccountIndex); |
| 68 } else { |
| 69 accountsSpinner.setSelection(accountsSpinner.getCount() - 1); |
| 70 } |
| 71 } |
| 72 |
| 73 /** |
65 * Set the listener for all the dropdown members in the layout. | 74 * Set the listener for all the dropdown members in the layout. |
66 * @param listener The listener object to attach to the dropdowns. | 75 * @param listener The listener object to attach to the dropdowns. |
67 */ | 76 */ |
68 public void setOnItemSelectedListener(OnItemSelectedListener listener) { | 77 public void setOnItemSelectedListener(OnItemSelectedListener listener) { |
69 Spinner accounts_spinner = (Spinner)findViewById(R.id.accounts_spinner); | 78 Spinner accounts_spinner = (Spinner)findViewById(R.id.accounts_spinner); |
70 accounts_spinner.setOnItemSelectedListener(listener); | 79 accounts_spinner.setOnItemSelectedListener(listener); |
71 } | 80 } |
72 } | 81 } |
OLD | NEW |