| 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 int defaultSearchEngineIndex = templateUrlService.getDefaultSearchEngine
Index(); |
| 125 | 168 |
| 126 // Convert the TemplateUrl index into an index of mSearchEngines. | 169 // Convert the TemplateUrl index into an index of mSearchEngines. |
| 127 mSelectedSearchEnginePosition = -1; | 170 mSelectedSearchEnginePosition = -1; |
| 128 for (int i = 0; i < mPrepopulatedSearchEngines.size(); ++i) { | 171 for (int i = 0; i < mPrepopulatedSearchEngines.size(); ++i) { |
| 129 if (mPrepopulatedSearchEngines.get(i).getIndex() == defaultSearchEng
ineIndex) { | 172 if (mPrepopulatedSearchEngines.get(i).getIndex() == defaultSearchEng
ineIndex) { |
| 130 mSelectedSearchEnginePosition = i; | 173 mSelectedSearchEnginePosition = i; |
| 131 } | 174 } |
| 132 } | 175 } |
| 133 | 176 |
| 134 for (int i = 0; i < mRecentSearchEngines.size(); ++i) { | 177 for (int i = 0; i < mRecentSearchEngines.size(); ++i) { |
| 135 if (mRecentSearchEngines.get(i).getIndex() == defaultSearchEngineInd
ex) { | 178 if (mRecentSearchEngines.get(i).getIndex() == defaultSearchEngineInd
ex) { |
| 136 // Add one to offset the title for the recent search engine list
. | 179 // Add one to offset the title for the recent search engine list
. |
| 137 mSelectedSearchEnginePosition = i + computeStartIndexForRecentSe
archEngines(); | 180 mSelectedSearchEnginePosition = i + computeStartIndexForRecentSe
archEngines(); |
| 138 } | 181 } |
| 139 } | 182 } |
| 140 | 183 |
| 184 if (mSelectedSearchEnginePosition == -1) { |
| 185 throw new IllegalStateException( |
| 186 "Default search engine index did not match any available sea
rch engines."); |
| 187 } |
| 188 |
| 141 mInitialEnginePosition = mSelectedSearchEnginePosition; | 189 mInitialEnginePosition = mSelectedSearchEnginePosition; |
| 142 | 190 |
| 143 TemplateUrlService.getInstance().setSearchEngine(toKeyword(mSelectedSear
chEnginePosition)); | 191 notifyDataSetChanged(); |
| 144 } | 192 } |
| 145 | 193 |
| 146 private String toKeyword(int position) { | 194 private String toKeyword(int position) { |
| 147 if (position < mPrepopulatedSearchEngines.size()) { | 195 if (position < mPrepopulatedSearchEngines.size()) { |
| 148 return mPrepopulatedSearchEngines.get(position).getKeyword(); | 196 return mPrepopulatedSearchEngines.get(position).getKeyword(); |
| 149 } else { | 197 } else { |
| 150 position -= computeStartIndexForRecentSearchEngines(); | 198 position -= computeStartIndexForRecentSearchEngines(); |
| 151 return mRecentSearchEngines.get(position).getKeyword(); | 199 return mRecentSearchEngines.get(position).getKeyword(); |
| 152 } | 200 } |
| 153 } | 201 } |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 } | 336 } |
| 289 | 337 |
| 290 return view; | 338 return view; |
| 291 } | 339 } |
| 292 | 340 |
| 293 // TemplateUrlService.LoadListener | 341 // TemplateUrlService.LoadListener |
| 294 | 342 |
| 295 @Override | 343 @Override |
| 296 public void onTemplateUrlServiceLoaded() { | 344 public void onTemplateUrlServiceLoaded() { |
| 297 TemplateUrlService.getInstance().unregisterLoadListener(this); | 345 TemplateUrlService.getInstance().unregisterLoadListener(this); |
| 298 initEntries(); | 346 mHasLoadObserver = false; |
| 299 notifyDataSetChanged(); | 347 refreshData(); |
| 348 } |
| 349 |
| 350 @Override |
| 351 public void onTemplateURLServiceChanged() { |
| 352 refreshData(); |
| 300 } | 353 } |
| 301 | 354 |
| 302 // OnClickListener: | 355 // OnClickListener: |
| 303 | 356 |
| 304 @Override | 357 @Override |
| 305 public void onClick(View view) { | 358 public void onClick(View view) { |
| 306 if (view.getTag() == null) { | 359 if (view.getTag() == null) { |
| 307 onLocationLinkClicked(); | 360 onLocationLinkClicked(); |
| 308 } else { | 361 } else { |
| 309 searchEngineSelected((int) view.getTag()); | 362 searchEngineSelected((int) view.getTag()); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 private int computeStartIndexForRecentSearchEngines() { | 443 private int computeStartIndexForRecentSearchEngines() { |
| 391 // If there are custom search engines to show, add 1 for showing the "R
ecently visited" | 444 // If there are custom search engines to show, add 1 for showing the "R
ecently visited" |
| 392 // header. | 445 // header. |
| 393 if (mRecentSearchEngines.size() > 0) { | 446 if (mRecentSearchEngines.size() > 0) { |
| 394 return mPrepopulatedSearchEngines.size() + 1; | 447 return mPrepopulatedSearchEngines.size() + 1; |
| 395 } else { | 448 } else { |
| 396 return mPrepopulatedSearchEngines.size(); | 449 return mPrepopulatedSearchEngines.size(); |
| 397 } | 450 } |
| 398 } | 451 } |
| 399 } | 452 } |
| OLD | NEW |