Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/locale/DefaultSearchEnginePromoDialog.java

Issue 2838833002: 🔍 Introduce default search engine dialog (Closed)
Patch Set: COmments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.RadioGroup;
13 import android.widget.RadioGroup.OnCheckedChangeListener;
14
15 import org.chromium.base.Callback;
16 import org.chromium.base.library_loader.LibraryLoader;
17 import org.chromium.chrome.R;
18 import org.chromium.chrome.browser.infobar.InfoBarControlLayout;
19 import org.chromium.chrome.browser.locale.LocaleManager.SearchEnginePromoType;
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 @SearchEnginePromoType
32 private final int mDialogType;
33
34 /** Called when the dialog is dismissed after the user has chosen a search e ngine. */
35 private final Callback<Boolean> mOnDismissed;
36
37 private RadioGroup mRadioGroup;
38 private String mSelectedKeyword;
39
40 /**
41 * Construct and show the dialog. Will be asynchronous if the TemplateUrlSe rvice has not yet
42 * been loaded.
43 *
44 * @param context Context to build the dialog with.
45 * @param dialogType Type of dialog to show.
46 * @param onDismissed Notified about whether the user chose an engine when i t got dismissed.
47 */
48 public static void show(final Context context, @SearchEnginePromoType final int dialogType,
49 @Nullable final Callback<Boolean> onDismissed) {
50 assert LibraryLoader.isInitialized();
51
52 // Load up the search engines.
53 final TemplateUrlService instance = TemplateUrlService.getInstance();
54 instance.registerLoadListener(new TemplateUrlService.LoadListener() {
55 @Override
56 public void onTemplateUrlServiceLoaded() {
57 instance.unregisterLoadListener(this);
58 new DefaultSearchEnginePromoDialog(context, dialogType, onDismis sed).show();
59 }
60 });
61 if (!instance.isLoaded()) instance.load();
62 }
63
64 private DefaultSearchEnginePromoDialog(
65 Context context, int dialogType, @Nullable Callback<Boolean> onDismi ssed) {
66 super(context);
67 mDialogType = dialogType;
68 mOnDismissed = onDismissed;
69 setOnDismissListener(this);
70
71 // No one should be able to bypass this dialog by clicking outside or by hitting back.
72 setCancelable(false);
73 setCanceledOnTouchOutside(false);
74 }
75
76 @Override
77 protected DialogParams getDialogParams() {
78 PromoDialog.DialogParams params = new PromoDialog.DialogParams();
79 params.headerStringResource = R.string.search_engine_dialog_title;
80 params.footerStringResource = R.string.search_engine_dialog_footer;
81 params.primaryButtonStringResource = R.string.ok;
82 return params;
83 }
84
85 @Override
86 protected void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88
89 // Determine what search engines will be listed.
90 TemplateUrlService instance = TemplateUrlService.getInstance();
91 assert instance.isLoaded();
92 List<TemplateUrl> engines = null;
93 if (mDialogType == LocaleManager.SEARCH_ENGINE_PROMO_SHOW_EXISTING) {
94 engines = instance.getSearchEngines();
95 } else {
96 // TODO(dfalcantara): Handle the new user case.
97 assert false;
98 }
99
100 // Shuffle up the engines.
101 List<CharSequence> engineNames = new ArrayList<>();
102 List<String> engineKeywords = new ArrayList<>();
103 Collections.shuffle(engines);
104 for (int i = 0; i < engines.size(); i++) {
105 TemplateUrl engine = engines.get(i);
106 engineNames.add(engine.getShortName());
107 engineKeywords.add(engine.getKeyword());
108 }
109
110 // Add the search engines to the dialog.
111 InfoBarControlLayout controls = addControlLayout();
112 mRadioGroup = controls.addRadioButtons(
113 engineNames, engineKeywords, InfoBarControlLayout.INVALID_INDEX) ;
114 mRadioGroup.setOnCheckedChangeListener(this);
115 updateButtonState();
116 }
117
118 @Override
119 public void onCheckedChanged(RadioGroup group, int checkedId) {
120 mSelectedKeyword = (String) group.findViewById(checkedId).getTag();
121 updateButtonState();
122 }
123
124 @Override
125 public void onClick(View view) {
126 if (view.getId() == R.id.button_primary) {
127 if (mSelectedKeyword == null) {
128 updateButtonState();
129 return;
130 }
131
132 dismiss();
133 } else {
134 assert false : "Unhandled click.";
135 }
136
137 // Don't propagate the click to the parent to prevent circumventing the dialog.
138 }
139
140 @Override
141 public void onDismiss(DialogInterface dialog) {
142 if (mSelectedKeyword == null) {
143 // This shouldn't happen, but in case it does, finish the Activity s o that the user has
144 // to respond to the dialog next time.
145 if (getOwnerActivity() != null) getOwnerActivity().finish();
146 } else {
147 TemplateUrlService.getInstance().setSearchEngine(mSelectedKeyword.to String());
148 // TODO(dfalcantara): Prevent the dialog from appearing again.
149 }
150
151 if (mOnDismissed != null) mOnDismissed.onResult(mSelectedKeyword != null );
152 }
153
154 private void updateButtonState() {
155 findViewById(R.id.button_primary).setEnabled(mSelectedKeyword != null);
156 }
157 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698