OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.preferences; | 5 package org.chromium.chrome.browser.preferences; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.Intent; | 8 import android.content.Intent; |
9 import android.content.SharedPreferences; | 9 import android.content.SharedPreferences; |
10 import android.content.res.Resources; | 10 import android.content.res.Resources; |
(...skipping 18 matching lines...) Expand all Loading... | |
29 import org.chromium.base.metrics.RecordUserAction; | 29 import org.chromium.base.metrics.RecordUserAction; |
30 import org.chromium.chrome.R; | 30 import org.chromium.chrome.R; |
31 import org.chromium.chrome.browser.ChromeFeatureList; | 31 import org.chromium.chrome.browser.ChromeFeatureList; |
32 import org.chromium.chrome.browser.locale.LocaleManager; | 32 import org.chromium.chrome.browser.locale.LocaleManager; |
33 import org.chromium.chrome.browser.omnibox.geo.GeolocationHeader; | 33 import org.chromium.chrome.browser.omnibox.geo.GeolocationHeader; |
34 import org.chromium.chrome.browser.preferences.website.ContentSetting; | 34 import org.chromium.chrome.browser.preferences.website.ContentSetting; |
35 import org.chromium.chrome.browser.preferences.website.GeolocationInfo; | 35 import org.chromium.chrome.browser.preferences.website.GeolocationInfo; |
36 import org.chromium.chrome.browser.preferences.website.SingleWebsitePreferences; | 36 import org.chromium.chrome.browser.preferences.website.SingleWebsitePreferences; |
37 import org.chromium.chrome.browser.preferences.website.WebsitePreferenceBridge; | 37 import org.chromium.chrome.browser.preferences.website.WebsitePreferenceBridge; |
38 import org.chromium.chrome.browser.search_engines.TemplateUrlService; | 38 import org.chromium.chrome.browser.search_engines.TemplateUrlService; |
39 import org.chromium.chrome.browser.search_engines.TemplateUrlService.LoadListene r; | |
40 import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl ; | 39 import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl ; |
41 import org.chromium.components.location.LocationUtils; | 40 import org.chromium.components.location.LocationUtils; |
42 import org.chromium.ui.text.SpanApplier; | 41 import org.chromium.ui.text.SpanApplier; |
43 import org.chromium.ui.text.SpanApplier.SpanInfo; | 42 import org.chromium.ui.text.SpanApplier.SpanInfo; |
44 | 43 |
45 import java.util.ArrayList; | 44 import java.util.ArrayList; |
46 import java.util.List; | 45 import java.util.List; |
47 | 46 |
48 /** | 47 /** |
49 * A custom adapter for listing search engines. | 48 * A custom adapter for listing search engines. |
50 */ | 49 */ |
51 public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On ClickListener { | 50 public class SearchEngineAdapter extends BaseAdapter |
51 implements TemplateUrlService.LoadListener, TemplateUrlService.TemplateU rlServiceObserver, | |
52 OnClickListener { | |
52 private static final int VIEW_TYPE_ITEM = 0; | 53 private static final int VIEW_TYPE_ITEM = 0; |
53 private static final int VIEW_TYPE_DIVIDER = 1; | 54 private static final int VIEW_TYPE_DIVIDER = 1; |
54 private static final int VIEW_TYPE_COUNT = 2; | 55 private static final int VIEW_TYPE_COUNT = 2; |
55 | 56 |
56 /** The current context. */ | 57 /** The current context. */ |
57 private Context mContext; | 58 private Context mContext; |
58 | 59 |
59 /** The layout inflater to use for the custom views. */ | 60 /** The layout inflater to use for the custom views. */ |
60 private LayoutInflater mLayoutInflater; | 61 private LayoutInflater mLayoutInflater; |
61 | 62 |
62 /** The list of prepopluated and default search engines. */ | 63 /** The list of prepopluated and default search engines. */ |
63 private List<TemplateUrl> mPrepopulatedSearchEngines = new ArrayList<>(); | 64 private List<TemplateUrl> mPrepopulatedSearchEngines = new ArrayList<>(); |
64 | 65 |
65 /** The list of recently visited search engines. */ | 66 /** The list of recently visited search engines. */ |
66 private List<TemplateUrl> mRecentSearchEngines = new ArrayList<>(); | 67 private List<TemplateUrl> mRecentSearchEngines = new ArrayList<>(); |
67 | 68 |
68 /** | 69 /** |
69 * The position (index into mPrepopulatedSearchEngines) of the currently sel ected search engine. | 70 * The position (index into mPrepopulatedSearchEngines) of the currently sel ected search engine. |
70 * Can be -1 if current search engine is managed and set to something other than the | 71 * Can be -1 if current search engine is managed and set to something other than the |
71 * pre-populated values. | 72 * pre-populated values. |
72 */ | 73 */ |
73 private int mSelectedSearchEnginePosition = -1; | 74 private int mSelectedSearchEnginePosition = -1; |
74 | 75 |
75 /** The position of the default search engine before user's action. */ | 76 /** The position of the default search engine before user's action. */ |
76 private int mInitialEnginePosition = -1; | 77 private int mInitialEnginePosition = -1; |
77 | 78 |
79 private boolean mHasLoadObserver; | |
80 | |
78 /** | 81 /** |
79 * Construct a SearchEngineAdapter. | 82 * Construct a SearchEngineAdapter. |
80 * @param context The current context. | 83 * @param context The current context. |
81 */ | 84 */ |
82 public SearchEngineAdapter(Context context) { | 85 public SearchEngineAdapter(Context context) { |
83 mContext = context; | 86 mContext = context; |
84 mLayoutInflater = (LayoutInflater) mContext.getSystemService( | 87 mLayoutInflater = (LayoutInflater) mContext.getSystemService( |
85 Context.LAYOUT_INFLATER_SERVICE); | 88 Context.LAYOUT_INFLATER_SERVICE); |
89 } | |
86 | 90 |
87 initEntries(); | 91 /** |
92 * Start the adapter to gather the available search engines and listen for u pdates. | |
93 */ | |
94 public void start() { | |
95 refreshData(); | |
96 TemplateUrlService.getInstance().addObserver(this); | |
97 } | |
98 | |
99 /** | |
100 * Stop the adapter from listening for future search engine updates. | |
101 */ | |
102 public void stop() { | |
103 if (mHasLoadObserver) { | |
104 TemplateUrlService.getInstance().unregisterLoadListener(this); | |
105 mHasLoadObserver = false; | |
106 } | |
107 TemplateUrlService.getInstance().removeObserver(this); | |
88 } | 108 } |
89 | 109 |
90 @VisibleForTesting | 110 @VisibleForTesting |
91 String getValueForTesting() { | 111 String getValueForTesting() { |
92 return Integer.toString(mSelectedSearchEnginePosition); | 112 return Integer.toString(mSelectedSearchEnginePosition); |
93 } | 113 } |
94 | 114 |
95 @VisibleForTesting | 115 @VisibleForTesting |
96 String setValueForTesting(String value) { | 116 String setValueForTesting(String value) { |
97 return searchEngineSelected(Integer.parseInt(value)); | 117 return searchEngineSelected(Integer.parseInt(value)); |
98 } | 118 } |
99 | 119 |
100 @VisibleForTesting | 120 @VisibleForTesting |
101 String getKeywordForTesting(int index) { | 121 String getKeywordForTesting(int index) { |
102 return toKeyword(index); | 122 return toKeyword(index); |
103 } | 123 } |
104 | 124 |
105 /** | 125 private void initializeSearchEngineGroups(List<TemplateUrl> templateUrls) { |
106 * Initialize the search engine list. | 126 mPrepopulatedSearchEngines = new ArrayList<>(); |
107 */ | 127 mRecentSearchEngines = new ArrayList<>(); |
108 private void initEntries() { | |
109 TemplateUrlService templateUrlService = TemplateUrlService.getInstance() ; | |
110 if (!templateUrlService.isLoaded()) { | |
111 templateUrlService.registerLoadListener(this); | |
112 templateUrlService.load(); | |
113 return; // Flow continues in onTemplateUrlServiceLoaded below. | |
114 } | |
115 | 128 |
116 int defaultSearchEngineIndex = templateUrlService.getDefaultSearchEngine Index(); | 129 for (int i = 0; i < templateUrls.size(); i++) { |
117 for (TemplateUrl templateUrl : templateUrlService.getSearchEngines()) { | 130 TemplateUrl templateUrl = templateUrls.get(i); |
118 if (templateUrl.getType() == TemplateUrlService.TYPE_PREPOPULATED | 131 if (templateUrl.getType() == TemplateUrlService.TYPE_PREPOPULATED |
119 || templateUrl.getType() == TemplateUrlService.TYPE_DEFAULT) { | 132 || templateUrl.getType() == TemplateUrlService.TYPE_DEFAULT) { |
120 mPrepopulatedSearchEngines.add(templateUrl); | 133 mPrepopulatedSearchEngines.add(templateUrl); |
121 } else { | 134 } else { |
122 mRecentSearchEngines.add(templateUrl); | 135 mRecentSearchEngines.add(templateUrl); |
123 } | 136 } |
124 } | 137 } |
138 } | |
139 | |
140 /** | |
141 * Initialize the search engine list. | |
142 */ | |
143 private void refreshData() { | |
144 TemplateUrlService templateUrlService = TemplateUrlService.getInstance() ; | |
145 if (!templateUrlService.isLoaded()) { | |
146 mHasLoadObserver = true; | |
147 templateUrlService.registerLoadListener(this); | |
148 templateUrlService.load(); | |
149 return; // Flow continues in onTemplateUrlServiceLoaded below. | |
150 } | |
151 | |
152 List<TemplateUrl> templateUrls = templateUrlService.getSearchEngines(); | |
153 boolean searchEnginesChanged = templateUrls.size() | |
154 != mPrepopulatedSearchEngines.size() + mRecentSearchEngines.size (); | |
155 if (!searchEnginesChanged) { | |
156 for (int i = 0; i < templateUrls.size(); i++) { | |
157 TemplateUrl templateUrl = templateUrls.get(i); | |
158 if (!mPrepopulatedSearchEngines.contains(templateUrl) | |
159 && !mRecentSearchEngines.contains(templateUrl)) { | |
160 searchEnginesChanged = true; | |
161 break; | |
162 } | |
163 } | |
164 } | |
165 if (searchEnginesChanged) initializeSearchEngineGroups(templateUrls); | |
166 | |
167 mSelectedSearchEnginePosition = -1; | |
168 mInitialEnginePosition = -1; | |
Theresa
2017/01/27 23:25:44
Since this is set below, on line 192, does setting
Ted C
2017/01/28 00:01:30
Nah, not needed...removed. Used to reset all the
| |
169 | |
170 int defaultSearchEngineIndex = templateUrlService.getDefaultSearchEngine Index(); | |
125 | 171 |
126 // Convert the TemplateUrl index into an index of mSearchEngines. | 172 // Convert the TemplateUrl index into an index of mSearchEngines. |
127 mSelectedSearchEnginePosition = -1; | 173 mSelectedSearchEnginePosition = -1; |
Theresa
2017/01/27 23:25:43
nit: this is a no-op since line 167 sets it to -1
Ted C
2017/01/28 00:01:30
now that I removed above, this is now the only lin
| |
128 for (int i = 0; i < mPrepopulatedSearchEngines.size(); ++i) { | 174 for (int i = 0; i < mPrepopulatedSearchEngines.size(); ++i) { |
129 if (mPrepopulatedSearchEngines.get(i).getIndex() == defaultSearchEng ineIndex) { | 175 if (mPrepopulatedSearchEngines.get(i).getIndex() == defaultSearchEng ineIndex) { |
130 mSelectedSearchEnginePosition = i; | 176 mSelectedSearchEnginePosition = i; |
131 } | 177 } |
132 } | 178 } |
133 | 179 |
134 for (int i = 0; i < mRecentSearchEngines.size(); ++i) { | 180 for (int i = 0; i < mRecentSearchEngines.size(); ++i) { |
135 if (mRecentSearchEngines.get(i).getIndex() == defaultSearchEngineInd ex) { | 181 if (mRecentSearchEngines.get(i).getIndex() == defaultSearchEngineInd ex) { |
136 // Add one to offset the title for the recent search engine list . | 182 // Add one to offset the title for the recent search engine list . |
137 mSelectedSearchEnginePosition = i + computeStartIndexForRecentSe archEngines(); | 183 mSelectedSearchEnginePosition = i + computeStartIndexForRecentSe archEngines(); |
138 } | 184 } |
139 } | 185 } |
140 | 186 |
187 if (mSelectedSearchEnginePosition == -1) { | |
188 throw new IllegalStateException( | |
189 "Default search engine index did not match any available sea rch engines."); | |
190 } | |
191 | |
141 mInitialEnginePosition = mSelectedSearchEnginePosition; | 192 mInitialEnginePosition = mSelectedSearchEnginePosition; |
142 | 193 |
143 TemplateUrlService.getInstance().setSearchEngine(toKeyword(mSelectedSear chEnginePosition)); | 194 notifyDataSetChanged(); |
144 } | 195 } |
145 | 196 |
146 private String toKeyword(int position) { | 197 private String toKeyword(int position) { |
147 if (position < mPrepopulatedSearchEngines.size()) { | 198 if (position < mPrepopulatedSearchEngines.size()) { |
148 return mPrepopulatedSearchEngines.get(position).getKeyword(); | 199 return mPrepopulatedSearchEngines.get(position).getKeyword(); |
149 } else { | 200 } else { |
150 position -= computeStartIndexForRecentSearchEngines(); | 201 position -= computeStartIndexForRecentSearchEngines(); |
151 return mRecentSearchEngines.get(position).getKeyword(); | 202 return mRecentSearchEngines.get(position).getKeyword(); |
152 } | 203 } |
153 } | 204 } |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 } | 339 } |
289 | 340 |
290 return view; | 341 return view; |
291 } | 342 } |
292 | 343 |
293 // TemplateUrlService.LoadListener | 344 // TemplateUrlService.LoadListener |
294 | 345 |
295 @Override | 346 @Override |
296 public void onTemplateUrlServiceLoaded() { | 347 public void onTemplateUrlServiceLoaded() { |
297 TemplateUrlService.getInstance().unregisterLoadListener(this); | 348 TemplateUrlService.getInstance().unregisterLoadListener(this); |
298 initEntries(); | 349 mHasLoadObserver = false; |
299 notifyDataSetChanged(); | 350 refreshData(); |
351 } | |
352 | |
353 @Override | |
354 public void onTemplateURLServiceChanged() { | |
355 refreshData(); | |
300 } | 356 } |
301 | 357 |
302 // OnClickListener: | 358 // OnClickListener: |
303 | 359 |
304 @Override | 360 @Override |
305 public void onClick(View view) { | 361 public void onClick(View view) { |
306 if (view.getTag() == null) { | 362 if (view.getTag() == null) { |
307 onLocationLinkClicked(); | 363 onLocationLinkClicked(); |
308 } else { | 364 } else { |
309 searchEngineSelected((int) view.getTag()); | 365 searchEngineSelected((int) view.getTag()); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 private int computeStartIndexForRecentSearchEngines() { | 446 private int computeStartIndexForRecentSearchEngines() { |
391 // If there are custom search engines to show, add 1 for showing the "R ecently visited" | 447 // If there are custom search engines to show, add 1 for showing the "R ecently visited" |
392 // header. | 448 // header. |
393 if (mRecentSearchEngines.size() > 0) { | 449 if (mRecentSearchEngines.size() > 0) { |
394 return mPrepopulatedSearchEngines.size() + 1; | 450 return mPrepopulatedSearchEngines.size() + 1; |
395 } else { | 451 } else { |
396 return mPrepopulatedSearchEngines.size(); | 452 return mPrepopulatedSearchEngines.size(); |
397 } | 453 } |
398 } | 454 } |
399 } | 455 } |
OLD | NEW |