OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.locale; | 5 package org.chromium.chrome.browser.locale; |
6 | 6 |
7 import android.support.annotation.Nullable; | 7 import android.support.annotation.Nullable; |
8 import android.view.View; | 8 import android.view.View; |
9 import android.view.View.OnClickListener; | 9 import android.view.View.OnClickListener; |
10 import android.widget.Button; | 10 import android.widget.Button; |
(...skipping 21 matching lines...) Expand all Loading... |
32 public HelperDelegate(@SearchEnginePromoType int dialogType) { | 32 public HelperDelegate(@SearchEnginePromoType int dialogType) { |
33 mDialogType = dialogType; | 33 mDialogType = dialogType; |
34 } | 34 } |
35 | 35 |
36 /** Determine what search engines will be listed. */ | 36 /** Determine what search engines will be listed. */ |
37 protected List<TemplateUrl> getSearchEngines() { | 37 protected List<TemplateUrl> getSearchEngines() { |
38 return LocaleManager.getInstance().getSearchEnginesForPromoDialog(mD
ialogType); | 38 return LocaleManager.getInstance().getSearchEnginesForPromoDialog(mD
ialogType); |
39 } | 39 } |
40 | 40 |
41 /** Called when the search engine the user selected is confirmed to be t
he one they want. */ | 41 /** Called when the search engine the user selected is confirmed to be t
he one they want. */ |
42 protected void onUserSeachEngineChoice(String keyword) { | 42 protected void onUserSeachEngineChoice(List<String> keywords, String key
word) { |
43 LocaleManager.getInstance().onUserSearchEngineChoiceFromPromoDialog( | 43 LocaleManager.getInstance().onUserSearchEngineChoiceFromPromoDialog( |
44 mDialogType, keyword); | 44 mDialogType, keywords, keyword); |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 private final HelperDelegate mDelegate; | 48 private final HelperDelegate mDelegate; |
49 private final Runnable mFinishRunnable; | 49 private final Runnable mFinishRunnable; |
50 private final Button mConfirmButton; | 50 private final Button mConfirmButton; |
51 | 51 |
52 /** | 52 /** |
| 53 * List of search engine keywords in the order shown to the user. |
| 54 */ |
| 55 private final List<String> mSearchEngineKeywords; |
| 56 |
| 57 /** |
53 * Keyword for the search engine that is selected in the RadioButtonLayout. | 58 * Keyword for the search engine that is selected in the RadioButtonLayout. |
54 * This value is not locked into the TemplateUrlService until the user confi
rms it by clicking | 59 * This value is not locked into the TemplateUrlService until the user confi
rms it by clicking |
55 * on {@link #mConfirmButton}. | 60 * on {@link #mConfirmButton}. |
56 */ | 61 */ |
57 private String mCurrentlySelectedKeyword; | 62 private String mCurrentlySelectedKeyword; |
58 | 63 |
59 /** | 64 /** |
60 * Constructs a DefaultSearchEngineDialogHelper. | 65 * Constructs a DefaultSearchEngineDialogHelper. |
61 * | 66 * |
62 * @param dialogType Dialog type to show. | 67 * @param dialogType Dialog type to show. |
63 * @param controls {@link RadioButtonLayout} that will contains all th
e engine options. | 68 * @param controls {@link RadioButtonLayout} that will contains all th
e engine options. |
64 * @param confirmButton Button that the user clicks on to confirm their sel
ection. | 69 * @param confirmButton Button that the user clicks on to confirm their sel
ection. |
65 * @param finishRunnable Runs after the user has confirmed their selection. | 70 * @param finishRunnable Runs after the user has confirmed their selection. |
66 */ | 71 */ |
67 public DefaultSearchEngineDialogHelper(@SearchEnginePromoType int dialogType
, | 72 public DefaultSearchEngineDialogHelper(@SearchEnginePromoType int dialogType
, |
68 RadioButtonLayout controls, Button confirmButton, Runnable finishRun
nable) { | 73 RadioButtonLayout controls, Button confirmButton, Runnable finishRun
nable) { |
69 mConfirmButton = confirmButton; | 74 mConfirmButton = confirmButton; |
70 mConfirmButton.setOnClickListener(this); | 75 mConfirmButton.setOnClickListener(this); |
71 mFinishRunnable = finishRunnable; | 76 mFinishRunnable = finishRunnable; |
72 mDelegate = createDelegate(dialogType); | 77 mDelegate = createDelegate(dialogType); |
73 | 78 |
74 // Shuffle up the engines. | 79 // Shuffle up the engines. |
75 List<TemplateUrl> engines = mDelegate.getSearchEngines(); | 80 List<TemplateUrl> engines = mDelegate.getSearchEngines(); |
76 List<CharSequence> engineNames = new ArrayList<>(); | 81 List<CharSequence> engineNames = new ArrayList<>(); |
77 List<String> engineKeywords = new ArrayList<>(); | 82 mSearchEngineKeywords = new ArrayList<>(); |
78 Collections.shuffle(engines); | 83 Collections.shuffle(engines); |
79 for (int i = 0; i < engines.size(); i++) { | 84 for (int i = 0; i < engines.size(); i++) { |
80 TemplateUrl engine = engines.get(i); | 85 TemplateUrl engine = engines.get(i); |
81 engineNames.add(engine.getShortName()); | 86 engineNames.add(engine.getShortName()); |
82 engineKeywords.add(engine.getKeyword()); | 87 mSearchEngineKeywords.add(engine.getKeyword()); |
83 } | 88 } |
84 | 89 |
85 // Add the search engines to the dialog without any of them being select
ed by default. | 90 // Add the search engines to the dialog without any of them being select
ed by default. |
86 controls.addOptions(engineNames, engineKeywords); | 91 controls.addOptions(engineNames, mSearchEngineKeywords); |
87 controls.selectChildAtIndex(RadioButtonLayout.INVALID_INDEX); | 92 controls.selectChildAtIndex(RadioButtonLayout.INVALID_INDEX); |
88 controls.setOnCheckedChangeListener(this); | 93 controls.setOnCheckedChangeListener(this); |
89 | 94 |
90 // Disable the button until the user selects an option. | 95 // Disable the button until the user selects an option. |
91 updateButtonState(); | 96 updateButtonState(); |
92 } | 97 } |
93 | 98 |
94 /** @return Keyword that corresponds to the search engine that is currently
selected. */ | 99 /** @return Keyword that corresponds to the search engine that is currently
selected. */ |
95 @Nullable | 100 @Nullable |
96 public final String getCurrentlySelectedKeyword() { | 101 public final String getCurrentlySelectedKeyword() { |
(...skipping 13 matching lines...) Expand all Loading... |
110 assert false : "Unhandled click."; | 115 assert false : "Unhandled click."; |
111 return; | 116 return; |
112 } | 117 } |
113 | 118 |
114 if (mCurrentlySelectedKeyword == null) { | 119 if (mCurrentlySelectedKeyword == null) { |
115 // The user clicked on the button, but they haven't clicked on an op
tion, yet. | 120 // The user clicked on the button, but they haven't clicked on an op
tion, yet. |
116 updateButtonState(); | 121 updateButtonState(); |
117 return; | 122 return; |
118 } | 123 } |
119 | 124 |
120 mDelegate.onUserSeachEngineChoice(mCurrentlySelectedKeyword.toString()); | 125 mDelegate.onUserSeachEngineChoice( |
| 126 mSearchEngineKeywords, mCurrentlySelectedKeyword.toString()); |
121 mFinishRunnable.run(); | 127 mFinishRunnable.run(); |
122 } | 128 } |
123 | 129 |
124 /** Creates the delegate that interacts with the TemplateUrlService. */ | 130 /** Creates the delegate that interacts with the TemplateUrlService. */ |
125 protected HelperDelegate createDelegate(@SearchEnginePromoType int dialogTyp
e) { | 131 protected HelperDelegate createDelegate(@SearchEnginePromoType int dialogTyp
e) { |
126 return new HelperDelegate(dialogType); | 132 return new HelperDelegate(dialogType); |
127 } | 133 } |
128 | 134 |
129 /** Prevent the user from moving forward until they've clicked a search engi
ne. */ | 135 /** Prevent the user from moving forward until they've clicked a search engi
ne. */ |
130 private final void updateButtonState() { | 136 private final void updateButtonState() { |
131 mConfirmButton.setEnabled(mCurrentlySelectedKeyword != null); | 137 mConfirmButton.setEnabled(mCurrentlySelectedKeyword != null); |
132 } | 138 } |
133 } | 139 } |
OLD | NEW |