Commit ff1c7739 authored by yusufo's avatar yusufo Committed by Commit bot

Change search selection action on the dialog

Adds a more generic call to the helper delegate and calls out to the
LocaleManager. Then LocaleManager does the actual search engine setting
and any other potential fallout of the search engine choice.

BUG=712836, 712833

Review-Url: https://codereview.chromium.org/2860643003
Cr-Commit-Position: refs/heads/master@{#469484}
parent a038875d
......@@ -22,11 +22,21 @@ import java.util.List;
/** Handles user interactions with a user dialog that lets them pick a default search engine. */
public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener, OnClickListener {
/** Handles interactions with the TemplateUrlService. */
public static class TemplateUrlServiceDelegate {
/** Handles interactions with the TemplateUrlService and LocaleManager. */
public static class HelperDelegate {
private final int mDialogType;
/**
* Basic constructor for the delegate.
* @param dialogType {@link SearchEnginePromoType} for the dialog this delegate belongs to.
*/
public HelperDelegate(@SearchEnginePromoType int dialogType) {
mDialogType = dialogType;
}
/** Determine what search engines will be listed. */
protected List<TemplateUrl> getSearchEngines(int dialogType) {
if (dialogType == LocaleManager.SEARCH_ENGINE_PROMO_SHOW_EXISTING) {
protected List<TemplateUrl> getSearchEngines() {
if (mDialogType == LocaleManager.SEARCH_ENGINE_PROMO_SHOW_EXISTING) {
TemplateUrlService instance = TemplateUrlService.getInstance();
assert instance.isLoaded();
return instance.getSearchEngines();
......@@ -38,12 +48,13 @@ public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener,
}
/** Called when the search engine the user selected is confirmed to be the one they want. */
protected void setSearchEngine(String keyword) {
TemplateUrlService.getInstance().setSearchEngine(keyword);
protected void onUserSeachEngineChoice(String keyword) {
LocaleManager.getInstance().onUserSearchEngineChoiceFromPromoDialog(
mDialogType, keyword);
}
}
private final TemplateUrlServiceDelegate mDelegate;
private final HelperDelegate mDelegate;
private final Runnable mFinishRunnable;
private final Button mConfirmButton;
......@@ -67,10 +78,10 @@ public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener,
mConfirmButton = confirmButton;
mConfirmButton.setOnClickListener(this);
mFinishRunnable = finishRunnable;
mDelegate = createDelegate();
mDelegate = createDelegate(dialogType);
// Shuffle up the engines.
List<TemplateUrl> engines = mDelegate.getSearchEngines(dialogType);
List<TemplateUrl> engines = mDelegate.getSearchEngines();
List<CharSequence> engineNames = new ArrayList<>();
List<String> engineKeywords = new ArrayList<>();
Collections.shuffle(engines);
......@@ -115,14 +126,13 @@ public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener,
return;
}
// TODO(dfalcantara): Prevent the dialog from appearing again.
mDelegate.setSearchEngine(mCurrentlySelectedKeyword.toString());
mDelegate.onUserSeachEngineChoice(mCurrentlySelectedKeyword.toString());
mFinishRunnable.run();
}
/** Creates the delegate that interacts with the TemplateUrlService. */
protected TemplateUrlServiceDelegate createDelegate() {
return new TemplateUrlServiceDelegate();
protected HelperDelegate createDelegate(@SearchEnginePromoType int dialogType) {
return new HelperDelegate(dialogType);
}
/** Prevent the user from moving forward until they've clicked a search engine. */
......
......@@ -19,6 +19,7 @@ import org.chromium.chrome.browser.AppHooks;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.preferences.PreferencesLauncher;
import org.chromium.chrome.browser.preferences.SearchEnginePreference;
import org.chromium.chrome.browser.search_engines.TemplateUrlService;
import org.chromium.chrome.browser.snackbar.Snackbar;
import org.chromium.chrome.browser.snackbar.SnackbarManager;
import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController;
......@@ -270,6 +271,20 @@ public class LocaleManager {
return SEARCH_ENGINE_PROMO_SHOW_SOGOU;
}
/**
* To be called after the user has made a selection from a search engine promo dialog.
* @param type The type of search engine promo dialog that was shown.
* @param keyword The keyword for the search engine chosen.
*/
protected void onUserSearchEngineChoiceFromPromoDialog(
@SearchEnginePromoType int type, String keyword) {
TemplateUrlService.getInstance().setSearchEngine(keyword);
ContextUtils.getAppSharedPreferences()
.edit()
.putInt(KEY_SEARCH_ENGINE_PROMO_SHOW_STATE, SEARCH_ENGINE_PROMO_CHECKED_AND_SHOWN)
.apply();
}
private SpecialLocaleHandler getSpecialLocaleHandler() {
if (mLocaleHandler == null) mLocaleHandler = new SpecialLocaleHandler(getSpecialLocaleId());
return mLocaleHandler;
......
......@@ -34,17 +34,20 @@ import java.util.List;
*/
@RunWith(ChromeJUnit4ClassRunner.class)
public class DefaultSearchEngineDialogHelperTest {
private class TestDelegate extends DefaultSearchEngineDialogHelper.TemplateUrlServiceDelegate {
private class TestDelegate extends DefaultSearchEngineDialogHelper.HelperDelegate {
public TestDelegate(int dialogType) {
super(dialogType);
}
public String chosenKeyword;
@Override
protected List<TemplateUrl> getSearchEngines(int dialogType) {
Assert.assertEquals(mDialogType, dialogType);
protected List<TemplateUrl> getSearchEngines() {
return mTemplateUrls;
}
@Override
protected void setSearchEngine(String keyword) {
protected void onUserSeachEngineChoice(String keyword) {
chosenKeyword = keyword;
}
}
......@@ -58,8 +61,8 @@ public class DefaultSearchEngineDialogHelperTest {
}
@Override
protected TemplateUrlServiceDelegate createDelegate() {
delegate = new TestDelegate();
protected HelperDelegate createDelegate(int dialogType) {
delegate = new TestDelegate(mDialogType);
return delegate;
}
}
......@@ -80,7 +83,7 @@ public class DefaultSearchEngineDialogHelperTest {
private final List<TemplateUrl> mTemplateUrls = new ArrayList<>();
private Context mContext;
private int mDialogType;
private @SearchEnginePromoType int mDialogType;
@Before
public void setUp() throws Exception {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment