Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.locale; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.content.DialogInterface; | |
| 9 import android.os.Bundle; | |
| 10 import android.support.annotation.Nullable; | |
| 11 import android.view.View; | |
| 12 import android.widget.RadioButton; | |
| 13 import android.widget.RadioGroup; | |
| 14 import android.widget.RadioGroup.OnCheckedChangeListener; | |
| 15 | |
| 16 import org.chromium.base.Callback; | |
| 17 import org.chromium.base.library_loader.LibraryLoader; | |
| 18 import org.chromium.chrome.R; | |
| 19 import org.chromium.chrome.browser.infobar.InfoBarControlLayout; | |
| 20 import org.chromium.chrome.browser.search_engines.TemplateUrlService; | |
| 21 import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl ; | |
| 22 import org.chromium.chrome.browser.widget.PromoDialog; | |
| 23 | |
| 24 import java.util.ArrayList; | |
| 25 import java.util.Collections; | |
| 26 import java.util.List; | |
| 27 | |
| 28 /** A dialog that forces the user to choose a default search engine. */ | |
| 29 public class DefaultSearchEnginePromoDialog extends PromoDialog implements OnChe ckedChangeListener { | |
| 30 /** Used to determine the promo dialog contents. */ | |
| 31 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.
| |
| 32 | |
| 33 /** Called when the dialog is dismissed after the user has chosen a search e ngine. */ | |
| 34 private final Callback<Boolean> mOnDismissed; | |
| 35 | |
| 36 private RadioGroup mRadioGroup; | |
| 37 private String mSelectedKeyword; | |
| 38 | |
| 39 /** | |
| 40 * Construct and show the dialog. Will be asynchronous if the TemplateUrlSe rvice has not yet | |
| 41 * 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.
| |
| 42 */ | |
| 43 public static void show(final Context context, final int dialogType, | |
| 44 @Nullable final Callback<Boolean> onDismissed) { | |
| 45 assert LibraryLoader.isInitialized(); | |
| 46 | |
| 47 // Load up the search engines. | |
| 48 final TemplateUrlService instance = TemplateUrlService.getInstance(); | |
| 49 instance.registerLoadListener(new TemplateUrlService.LoadListener() { | |
| 50 @Override | |
| 51 public void onTemplateUrlServiceLoaded() { | |
| 52 instance.unregisterLoadListener(this); | |
| 53 new DefaultSearchEnginePromoDialog(context, dialogType, onDismis sed).show(); | |
| 54 } | |
| 55 }); | |
| 56 if (!instance.isLoaded()) instance.load(); | |
| 57 } | |
| 58 | |
| 59 private DefaultSearchEnginePromoDialog( | |
| 60 Context context, int dialogType, @Nullable Callback<Boolean> onDismi ssed) { | |
| 61 super(context); | |
| 62 mDialogType = dialogType; | |
| 63 mOnDismissed = onDismissed; | |
| 64 setOnDismissListener(this); | |
| 65 | |
| 66 // No one should be able to bypass this dialog by clicking outside or by hitting back. | |
| 67 setCancelable(false); | |
| 68 setCanceledOnTouchOutside(false); | |
| 69 } | |
| 70 | |
| 71 @Override | |
| 72 protected DialogParams getDialogParams() { | |
| 73 PromoDialog.DialogParams params = new PromoDialog.DialogParams(); | |
| 74 params.headerStringResource = R.string.search_engine_dialog_title; | |
| 75 params.footerStringResource = R.string.search_engine_dialog_footer; | |
| 76 params.primaryButtonStringResource = R.string.ok; | |
| 77 return params; | |
| 78 } | |
| 79 | |
| 80 @Override | |
| 81 protected void onCreate(Bundle savedInstanceState) { | |
| 82 super.onCreate(savedInstanceState); | |
| 83 | |
| 84 // Determine what search engines will be listed. | |
| 85 TemplateUrlService instance = TemplateUrlService.getInstance(); | |
| 86 assert instance.isLoaded(); | |
| 87 List<TemplateUrl> engines = null; | |
| 88 if (mDialogType == LocaleManager.SEARCH_ENGINE_PROMO_SHOW_EXISTING) { | |
| 89 engines = instance.getSearchEngines(); | |
| 90 } else { | |
| 91 // TODO(dfalcantara): Handle the new user case. | |
| 92 assert false; | |
| 93 } | |
| 94 | |
| 95 // Shuffle up the engines. | |
| 96 List<CharSequence> engineNames = new ArrayList<>(); | |
| 97 List<String> engineKeywords = new ArrayList<>(); | |
| 98 Collections.shuffle(engines); | |
| 99 for (int i = 0; i < engines.size(); i++) { | |
| 100 TemplateUrl engine = engines.get(i); | |
| 101 engineNames.add(engine.getShortName()); | |
| 102 engineKeywords.add(engine.getKeyword()); | |
| 103 } | |
| 104 | |
| 105 // Add the search engines to the dialog. | |
| 106 InfoBarControlLayout controls = addControlLayout(); | |
| 107 mRadioGroup = controls.addRadioButtons( | |
| 108 engineNames, engineKeywords, InfoBarControlLayout.INVALID_INDEX) ; | |
| 109 mRadioGroup.setOnCheckedChangeListener(this); | |
| 110 updateButtonState(); | |
| 111 } | |
| 112 | |
| 113 @Override | |
| 114 public void onCheckedChanged(RadioGroup group, int checkedId) { | |
| 115 mSelectedKeyword = null; | |
|
Ted C
2017/04/27 16:40:45
can the contents of this method be replaced with:
| |
| 116 | |
| 117 // Run through the RadioGroup to figure out which one was checked. | |
| 118 for (int i = 0; i < group.getChildCount(); i++) { | |
| 119 View child = group.getChildAt(i); | |
| 120 assert child instanceof RadioButton; | |
| 121 | |
| 122 RadioButton button = (RadioButton) child; | |
| 123 if (button.isChecked()) { | |
| 124 assert mSelectedKeyword == null; | |
| 125 mSelectedKeyword = (String) button.getTag(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 updateButtonState(); | |
| 130 } | |
| 131 | |
| 132 @Override | |
| 133 public void onClick(View view) { | |
| 134 if (view.getId() == R.id.button_primary) { | |
| 135 if (mSelectedKeyword == null) { | |
| 136 updateButtonState(); | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 dismiss(); | |
| 141 } else { | |
| 142 assert false : "Unhandled click."; | |
| 143 } | |
| 144 | |
| 145 // Don't propagate the click to the parent to prevent circumventing the dialog. | |
| 146 } | |
| 147 | |
| 148 @Override | |
| 149 public void onDismiss(DialogInterface dialog) { | |
| 150 if (mSelectedKeyword == null) { | |
| 151 // This shouldn't happen, but in case it does, finish the Activity s o that the user has | |
| 152 // to respond to the dialog next time. | |
| 153 if (getOwnerActivity() != null) getOwnerActivity().finish(); | |
| 154 } else { | |
| 155 TemplateUrlService.getInstance().setSearchEngine(mSelectedKeyword.to String()); | |
| 156 // TODO(dfalcantara): Prevent the dialog from appearing again. | |
| 157 } | |
| 158 | |
| 159 if (mOnDismissed != null) mOnDismissed.onResult(mSelectedKeyword != null ); | |
| 160 } | |
| 161 | |
| 162 private void updateButtonState() { | |
| 163 findViewById(R.id.button_primary).setEnabled(mSelectedKeyword != null); | |
| 164 } | |
| 165 } | |
| OLD | NEW |