Index: chrome/android/java/src/org/chromium/chrome/browser/locale/DefaultSearchEnginePromoDialog.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/locale/DefaultSearchEnginePromoDialog.java b/chrome/android/java/src/org/chromium/chrome/browser/locale/DefaultSearchEnginePromoDialog.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e20df7951e0f0faa26203a17ed11b151bb78bcd6 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/locale/DefaultSearchEnginePromoDialog.java |
@@ -0,0 +1,165 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser.locale; |
+ |
+import android.content.Context; |
+import android.content.DialogInterface; |
+import android.os.Bundle; |
+import android.support.annotation.Nullable; |
+import android.view.View; |
+import android.widget.RadioButton; |
+import android.widget.RadioGroup; |
+import android.widget.RadioGroup.OnCheckedChangeListener; |
+ |
+import org.chromium.base.Callback; |
+import org.chromium.base.library_loader.LibraryLoader; |
+import org.chromium.chrome.R; |
+import org.chromium.chrome.browser.infobar.InfoBarControlLayout; |
+import org.chromium.chrome.browser.search_engines.TemplateUrlService; |
+import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl; |
+import org.chromium.chrome.browser.widget.PromoDialog; |
+ |
+import java.util.ArrayList; |
+import java.util.Collections; |
+import java.util.List; |
+ |
+/** A dialog that forces the user to choose a default search engine. */ |
+public class DefaultSearchEnginePromoDialog extends PromoDialog implements OnCheckedChangeListener { |
+ /** Used to determine the promo dialog contents. */ |
+ private final int mDialogType; |
Ted C
2017/04/27 16:40:45
I think my comment from the previous CL was lost i
gone
2017/04/27 17:48:52
Done.
|
+ |
+ /** Called when the dialog is dismissed after the user has chosen a search engine. */ |
+ private final Callback<Boolean> mOnDismissed; |
+ |
+ private RadioGroup mRadioGroup; |
+ private String mSelectedKeyword; |
+ |
+ /** |
+ * Construct and show the dialog. Will be asynchronous if the TemplateUrlService has not yet |
+ * been loaded. |
Ted C
2017/04/27 16:40:45
I think we should have javadoc for the params (in
gone
2017/04/27 17:48:52
Done.
|
+ */ |
+ public static void show(final Context context, final int dialogType, |
+ @Nullable final Callback<Boolean> onDismissed) { |
+ assert LibraryLoader.isInitialized(); |
+ |
+ // Load up the search engines. |
+ final TemplateUrlService instance = TemplateUrlService.getInstance(); |
+ instance.registerLoadListener(new TemplateUrlService.LoadListener() { |
+ @Override |
+ public void onTemplateUrlServiceLoaded() { |
+ instance.unregisterLoadListener(this); |
+ new DefaultSearchEnginePromoDialog(context, dialogType, onDismissed).show(); |
+ } |
+ }); |
+ if (!instance.isLoaded()) instance.load(); |
+ } |
+ |
+ private DefaultSearchEnginePromoDialog( |
+ Context context, int dialogType, @Nullable Callback<Boolean> onDismissed) { |
+ super(context); |
+ mDialogType = dialogType; |
+ mOnDismissed = onDismissed; |
+ setOnDismissListener(this); |
+ |
+ // No one should be able to bypass this dialog by clicking outside or by hitting back. |
+ setCancelable(false); |
+ setCanceledOnTouchOutside(false); |
+ } |
+ |
+ @Override |
+ protected DialogParams getDialogParams() { |
+ PromoDialog.DialogParams params = new PromoDialog.DialogParams(); |
+ params.headerStringResource = R.string.search_engine_dialog_title; |
+ params.footerStringResource = R.string.search_engine_dialog_footer; |
+ params.primaryButtonStringResource = R.string.ok; |
+ return params; |
+ } |
+ |
+ @Override |
+ protected void onCreate(Bundle savedInstanceState) { |
+ super.onCreate(savedInstanceState); |
+ |
+ // Determine what search engines will be listed. |
+ TemplateUrlService instance = TemplateUrlService.getInstance(); |
+ assert instance.isLoaded(); |
+ List<TemplateUrl> engines = null; |
+ if (mDialogType == LocaleManager.SEARCH_ENGINE_PROMO_SHOW_EXISTING) { |
+ engines = instance.getSearchEngines(); |
+ } else { |
+ // TODO(dfalcantara): Handle the new user case. |
+ assert false; |
+ } |
+ |
+ // Shuffle up the engines. |
+ List<CharSequence> engineNames = new ArrayList<>(); |
+ List<String> engineKeywords = new ArrayList<>(); |
+ Collections.shuffle(engines); |
+ for (int i = 0; i < engines.size(); i++) { |
+ TemplateUrl engine = engines.get(i); |
+ engineNames.add(engine.getShortName()); |
+ engineKeywords.add(engine.getKeyword()); |
+ } |
+ |
+ // Add the search engines to the dialog. |
+ InfoBarControlLayout controls = addControlLayout(); |
+ mRadioGroup = controls.addRadioButtons( |
+ engineNames, engineKeywords, InfoBarControlLayout.INVALID_INDEX); |
+ mRadioGroup.setOnCheckedChangeListener(this); |
+ updateButtonState(); |
+ } |
+ |
+ @Override |
+ public void onCheckedChanged(RadioGroup group, int checkedId) { |
+ mSelectedKeyword = null; |
Ted C
2017/04/27 16:40:45
can the contents of this method be replaced with:
|
+ |
+ // Run through the RadioGroup to figure out which one was checked. |
+ for (int i = 0; i < group.getChildCount(); i++) { |
+ View child = group.getChildAt(i); |
+ assert child instanceof RadioButton; |
+ |
+ RadioButton button = (RadioButton) child; |
+ if (button.isChecked()) { |
+ assert mSelectedKeyword == null; |
+ mSelectedKeyword = (String) button.getTag(); |
+ } |
+ } |
+ |
+ updateButtonState(); |
+ } |
+ |
+ @Override |
+ public void onClick(View view) { |
+ if (view.getId() == R.id.button_primary) { |
+ if (mSelectedKeyword == null) { |
+ updateButtonState(); |
+ return; |
+ } |
+ |
+ dismiss(); |
+ } else { |
+ assert false : "Unhandled click."; |
+ } |
+ |
+ // Don't propagate the click to the parent to prevent circumventing the dialog. |
+ } |
+ |
+ @Override |
+ public void onDismiss(DialogInterface dialog) { |
+ if (mSelectedKeyword == null) { |
+ // This shouldn't happen, but in case it does, finish the Activity so that the user has |
+ // to respond to the dialog next time. |
+ if (getOwnerActivity() != null) getOwnerActivity().finish(); |
+ } else { |
+ TemplateUrlService.getInstance().setSearchEngine(mSelectedKeyword.toString()); |
+ // TODO(dfalcantara): Prevent the dialog from appearing again. |
+ } |
+ |
+ if (mOnDismissed != null) mOnDismissed.onResult(mSelectedKeyword != null); |
+ } |
+ |
+ private void updateButtonState() { |
+ findViewById(R.id.button_primary).setEnabled(mSelectedKeyword != null); |
+ } |
+} |