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; ...@@ -22,11 +22,21 @@ import java.util.List;
/** Handles user interactions with a user dialog that lets them pick a default search engine. */ /** Handles user interactions with a user dialog that lets them pick a default search engine. */
public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener, OnClickListener { public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener, OnClickListener {
/** Handles interactions with the TemplateUrlService. */ /** Handles interactions with the TemplateUrlService and LocaleManager. */
public static class TemplateUrlServiceDelegate { 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. */ /** Determine what search engines will be listed. */
protected List<TemplateUrl> getSearchEngines(int dialogType) { protected List<TemplateUrl> getSearchEngines() {
if (dialogType == LocaleManager.SEARCH_ENGINE_PROMO_SHOW_EXISTING) { if (mDialogType == LocaleManager.SEARCH_ENGINE_PROMO_SHOW_EXISTING) {
TemplateUrlService instance = TemplateUrlService.getInstance(); TemplateUrlService instance = TemplateUrlService.getInstance();
assert instance.isLoaded(); assert instance.isLoaded();
return instance.getSearchEngines(); return instance.getSearchEngines();
...@@ -38,12 +48,13 @@ public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener, ...@@ -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. */ /** Called when the search engine the user selected is confirmed to be the one they want. */
protected void setSearchEngine(String keyword) { protected void onUserSeachEngineChoice(String keyword) {
TemplateUrlService.getInstance().setSearchEngine(keyword); LocaleManager.getInstance().onUserSearchEngineChoiceFromPromoDialog(
mDialogType, keyword);
} }
} }
private final TemplateUrlServiceDelegate mDelegate; private final HelperDelegate mDelegate;
private final Runnable mFinishRunnable; private final Runnable mFinishRunnable;
private final Button mConfirmButton; private final Button mConfirmButton;
...@@ -67,10 +78,10 @@ public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener, ...@@ -67,10 +78,10 @@ public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener,
mConfirmButton = confirmButton; mConfirmButton = confirmButton;
mConfirmButton.setOnClickListener(this); mConfirmButton.setOnClickListener(this);
mFinishRunnable = finishRunnable; mFinishRunnable = finishRunnable;
mDelegate = createDelegate(); mDelegate = createDelegate(dialogType);
// Shuffle up the engines. // Shuffle up the engines.
List<TemplateUrl> engines = mDelegate.getSearchEngines(dialogType); List<TemplateUrl> engines = mDelegate.getSearchEngines();
List<CharSequence> engineNames = new ArrayList<>(); List<CharSequence> engineNames = new ArrayList<>();
List<String> engineKeywords = new ArrayList<>(); List<String> engineKeywords = new ArrayList<>();
Collections.shuffle(engines); Collections.shuffle(engines);
...@@ -115,14 +126,13 @@ public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener, ...@@ -115,14 +126,13 @@ public class DefaultSearchEngineDialogHelper implements OnCheckedChangeListener,
return; return;
} }
// TODO(dfalcantara): Prevent the dialog from appearing again. mDelegate.onUserSeachEngineChoice(mCurrentlySelectedKeyword.toString());
mDelegate.setSearchEngine(mCurrentlySelectedKeyword.toString());
mFinishRunnable.run(); mFinishRunnable.run();
} }
/** Creates the delegate that interacts with the TemplateUrlService. */ /** Creates the delegate that interacts with the TemplateUrlService. */
protected TemplateUrlServiceDelegate createDelegate() { protected HelperDelegate createDelegate(@SearchEnginePromoType int dialogType) {
return new TemplateUrlServiceDelegate(); return new HelperDelegate(dialogType);
} }
/** Prevent the user from moving forward until they've clicked a search engine. */ /** Prevent the user from moving forward until they've clicked a search engine. */
......
...@@ -19,6 +19,7 @@ import org.chromium.chrome.browser.AppHooks; ...@@ -19,6 +19,7 @@ import org.chromium.chrome.browser.AppHooks;
import org.chromium.chrome.browser.ChromeFeatureList; import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.preferences.PreferencesLauncher; import org.chromium.chrome.browser.preferences.PreferencesLauncher;
import org.chromium.chrome.browser.preferences.SearchEnginePreference; 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.Snackbar;
import org.chromium.chrome.browser.snackbar.SnackbarManager; import org.chromium.chrome.browser.snackbar.SnackbarManager;
import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController;
...@@ -270,6 +271,20 @@ public class LocaleManager { ...@@ -270,6 +271,20 @@ public class LocaleManager {
return SEARCH_ENGINE_PROMO_SHOW_SOGOU; 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() { private SpecialLocaleHandler getSpecialLocaleHandler() {
if (mLocaleHandler == null) mLocaleHandler = new SpecialLocaleHandler(getSpecialLocaleId()); if (mLocaleHandler == null) mLocaleHandler = new SpecialLocaleHandler(getSpecialLocaleId());
return mLocaleHandler; return mLocaleHandler;
......
...@@ -34,17 +34,20 @@ import java.util.List; ...@@ -34,17 +34,20 @@ import java.util.List;
*/ */
@RunWith(ChromeJUnit4ClassRunner.class) @RunWith(ChromeJUnit4ClassRunner.class)
public class DefaultSearchEngineDialogHelperTest { public class DefaultSearchEngineDialogHelperTest {
private class TestDelegate extends DefaultSearchEngineDialogHelper.TemplateUrlServiceDelegate { private class TestDelegate extends DefaultSearchEngineDialogHelper.HelperDelegate {
public TestDelegate(int dialogType) {
super(dialogType);
}
public String chosenKeyword; public String chosenKeyword;
@Override @Override
protected List<TemplateUrl> getSearchEngines(int dialogType) { protected List<TemplateUrl> getSearchEngines() {
Assert.assertEquals(mDialogType, dialogType);
return mTemplateUrls; return mTemplateUrls;
} }
@Override @Override
protected void setSearchEngine(String keyword) { protected void onUserSeachEngineChoice(String keyword) {
chosenKeyword = keyword; chosenKeyword = keyword;
} }
} }
...@@ -58,8 +61,8 @@ public class DefaultSearchEngineDialogHelperTest { ...@@ -58,8 +61,8 @@ public class DefaultSearchEngineDialogHelperTest {
} }
@Override @Override
protected TemplateUrlServiceDelegate createDelegate() { protected HelperDelegate createDelegate(int dialogType) {
delegate = new TestDelegate(); delegate = new TestDelegate(mDialogType);
return delegate; return delegate;
} }
} }
...@@ -80,7 +83,7 @@ public class DefaultSearchEngineDialogHelperTest { ...@@ -80,7 +83,7 @@ public class DefaultSearchEngineDialogHelperTest {
private final List<TemplateUrl> mTemplateUrls = new ArrayList<>(); private final List<TemplateUrl> mTemplateUrls = new ArrayList<>();
private Context mContext; private Context mContext;
private int mDialogType; private @SearchEnginePromoType int mDialogType;
@Before @Before
public void setUp() throws Exception { 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