Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/preferences/SearchEngineAdapter.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/SearchEngineAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/SearchEngineAdapter.java |
| index 792ea9b31ae8182bf99982129943194274e2f44c..a6e64ba2afe2290d6b169b522cfb8fbd0b3612c4 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/SearchEngineAdapter.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/SearchEngineAdapter.java |
| @@ -25,7 +25,9 @@ import android.widget.TextView; |
| import org.chromium.base.ApiCompatibilityUtils; |
| import org.chromium.base.ContextUtils; |
| +import org.chromium.base.metrics.RecordUserAction; |
| import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.locale.LocaleManager; |
| import org.chromium.chrome.browser.omnibox.geo.GeolocationHeader; |
| import org.chromium.chrome.browser.preferences.website.ContentSetting; |
| import org.chromium.chrome.browser.preferences.website.GeolocationInfo; |
| @@ -38,23 +40,15 @@ import org.chromium.components.location.LocationUtils; |
| import org.chromium.ui.text.SpanApplier; |
| import org.chromium.ui.text.SpanApplier.SpanInfo; |
| +import java.util.ArrayList; |
| import java.util.List; |
| /** |
| * A custom adapter for listing search engines. |
| */ |
| public class SearchEngineAdapter extends BaseAdapter implements LoadListener, OnClickListener { |
| - /** |
| - * A callback for reporting progress to the owner. |
| - */ |
| - public interface SelectSearchEngineCallback { |
| - /** |
| - * Called when the search engine data has loaded and we've determined the currently active |
| - * one. |
| - * @param name Provides the name of it (with a simplified URL in parenthesis). |
| - */ |
| - void currentSearchEngineDetermined(int selectedIndex); |
| - } |
| + private static final int VIEW_TYPE_ITEM = 0; |
| + private static final int VIEW_TYPE_DIVIDER = 1; |
| // The current context. |
| private Context mContext; |
| @@ -62,13 +56,13 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On |
| // The layout inflater to use for the custom views. |
| private LayoutInflater mLayoutInflater; |
| - // The callback to use for notifying caller of progress. |
| - private SelectSearchEngineCallback mCallback; |
| - |
| - // The list of available search engines. |
| - private List<TemplateUrl> mSearchEngines; |
| - // The position (index into mSearchEngines) of the currently selected search engine. Can be -1 |
| - // if current search engine is managed and set to something other than the pre-populated values. |
| + // The list of prepopluated and default search engines. |
| + private List<TemplateUrl> mPrepopulatedSearchEngines = new ArrayList<TemplateUrl>(); |
|
gone
2016/10/31 22:31:28
Add newlines between the previous variable and the
ltian
2016/11/01 18:08:48
Done.
|
| + // The list of recently visited search engines. |
| + private List<TemplateUrl> mRecentSearchEngines = new ArrayList<TemplateUrl>(); |
| + // The position (index into mPrepopulatedSearchEngines) of the currently selected search engine. |
| + // Can be -1 if current search engine is managed and set to something other than the |
| + // pre-populated values. |
| private int mSelectedSearchEnginePosition = -1; |
| // The position of the default search engine before user's action. |
| @@ -77,32 +71,27 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On |
| /** |
| * Construct a SearchEngineAdapter. |
| * @param context The current context. |
| - * @param callback The callback to use to communicate back. |
| */ |
| - public SearchEngineAdapter(Context context, SelectSearchEngineCallback callback) { |
| + public SearchEngineAdapter(Context context) { |
| mContext = context; |
| mLayoutInflater = (LayoutInflater) mContext.getSystemService( |
| Context.LAYOUT_INFLATER_SERVICE); |
| - mCallback = callback; |
| initEntries(); |
| } |
| - /** |
| - * @return The index of the selected engine before user's action. |
| - */ |
| - public int getInitialSearchEnginePosition() { |
| - return mInitialEnginePosition; |
| - } |
| - |
| // Used for testing. |
| String getValueForTesting() { |
| return Integer.toString(mSelectedSearchEnginePosition); |
| } |
| - void setValueForTesting(String value) { |
| - searchEngineSelected(Integer.parseInt(value)); |
| + String setValueForTesting(String value) { |
| + return searchEngineSelected(Integer.parseInt(value)); |
| + } |
| + |
| + String getKeywordForTesting(int index) { |
| + return toKeyword(index); |
| } |
| /** |
| @@ -116,37 +105,63 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On |
| return; // Flow continues in onTemplateUrlServiceLoaded below. |
| } |
| - // Fetch all the search engine info and the currently active one. |
| - mSearchEngines = templateUrlService.getLocalizedSearchEngines(); |
| - int searchEngineIndex = templateUrlService.getDefaultSearchEngineIndex(); |
| - // Convert the TemplateUrl index into an index into mSearchEngines. |
| + int defaultSearchEngineIndex = templateUrlService.getDefaultSearchEngineIndex(); |
| + for (TemplateUrl templateUrl : templateUrlService.getSearchEngines()) { |
| + if (templateUrl.getType() == TemplateUrlService.TYPE_PREPOPULATED |
| + || templateUrl.getType() == TemplateUrlService.TYPE_DEFAULT) { |
| + mPrepopulatedSearchEngines.add(templateUrl); |
| + } else { |
| + mRecentSearchEngines.add(templateUrl); |
| + } |
| + } |
| + |
| + // Convert the TemplateUrl index into an index of mSearchEngines. |
| mSelectedSearchEnginePosition = -1; |
| - for (int i = 0; i < mSearchEngines.size(); ++i) { |
| - if (mSearchEngines.get(i).getIndex() == searchEngineIndex) { |
| + for (int i = 0; i < mPrepopulatedSearchEngines.size(); ++i) { |
| + if (mPrepopulatedSearchEngines.get(i).getIndex() == defaultSearchEngineIndex) { |
| mSelectedSearchEnginePosition = i; |
| } |
| } |
| + |
| + for (int i = 0; i < mRecentSearchEngines.size(); ++i) { |
| + if (mRecentSearchEngines.get(i).getIndex() == defaultSearchEngineIndex) { |
| + // Add one to offset the title for recent search engine list |
| + mSelectedSearchEnginePosition = i + mPrepopulatedSearchEngines.size() + 1; |
| + } |
| + } |
| + |
| mInitialEnginePosition = mSelectedSearchEnginePosition; |
| - // Report back what is selected. |
| - mCallback.currentSearchEngineDetermined(toIndex(mSelectedSearchEnginePosition)); |
| + TemplateUrlService.getInstance().setSearchEngine(toKeyword(mSelectedSearchEnginePosition)); |
| } |
| - private int toIndex(int position) { |
| - return mSearchEngines.get(position).getIndex(); |
| + private String toKeyword(int position) { |
| + if (position < mPrepopulatedSearchEngines.size()) { |
| + return mPrepopulatedSearchEngines.get(position).getKeyword(); |
| + } else { |
| + position -= mPrepopulatedSearchEngines.size() + 1; |
| + return mRecentSearchEngines.get(position).getKeyword(); |
| + } |
| } |
| // BaseAdapter: |
| @Override |
| public int getCount() { |
| - return mSearchEngines == null ? 0 : mSearchEngines.size(); |
| + return mPrepopulatedSearchEngines == null |
| + ? 0 |
| + : mPrepopulatedSearchEngines.size() + mRecentSearchEngines.size() + 1; |
| } |
| @Override |
| public Object getItem(int pos) { |
| - TemplateUrl templateUrl = mSearchEngines.get(pos); |
| - return templateUrl.getShortName(); |
| + if (pos < mPrepopulatedSearchEngines.size()) { |
| + return mPrepopulatedSearchEngines.get(pos); |
| + } else if (pos > mPrepopulatedSearchEngines.size()) { |
| + pos -= mPrepopulatedSearchEngines.size() + 1; |
| + return mRecentSearchEngines.get(pos); |
| + } |
| + return null; |
| } |
| @Override |
| @@ -155,10 +170,29 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On |
| } |
| @Override |
| + public int getItemViewType(int position) { |
| + if (position == mPrepopulatedSearchEngines.size()) { |
| + return VIEW_TYPE_DIVIDER; |
| + } else { |
| + return VIEW_TYPE_ITEM; |
| + } |
| + } |
| + |
| + @Override |
| public View getView(int position, View convertView, ViewGroup parent) { |
| View view = convertView; |
| + TemplateUrl templateUrl = (TemplateUrl) getItem(position); |
| + int itemViewType = getItemViewType(position); |
| if (convertView == null) { |
| - view = mLayoutInflater.inflate(R.layout.search_engine, null); |
| + if (itemViewType == VIEW_TYPE_DIVIDER) { |
| + view = mLayoutInflater.inflate(R.layout.search_engine_recent_title, null); |
| + } else { |
| + view = mLayoutInflater.inflate(R.layout.search_engine, null); |
| + } |
| + } |
| + if (itemViewType == VIEW_TYPE_DIVIDER) { |
| + view.setClickable(false); |
|
Ian Wen
2016/10/28 22:17:39
BTW this can be done in xml, so you don't need to
ltian
2016/10/31 21:40:16
Done.
|
| + return view; |
| } |
| view.setOnClickListener(this); |
| @@ -179,10 +213,17 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On |
| radioButton.setChecked(selected); |
| TextView description = (TextView) view.findViewById(R.id.description); |
| - TemplateUrl templateUrl = mSearchEngines.get(position); |
| Resources resources = mContext.getResources(); |
| description.setText(templateUrl.getShortName()); |
| + TextView url = (TextView) view.findViewById(R.id.url); |
| + url.setText(templateUrl.getUrl()); |
| + if (templateUrl.getType() == TemplateUrlService.TYPE_PREPOPULATED |
| + || templateUrl.getType() == TemplateUrlService.TYPE_DEFAULT |
| + || templateUrl.getUrl().length() == 0) { |
| + url.setVisibility(View.GONE); |
| + } |
| + |
| // To improve the explore-by-touch experience, the radio button is hidden from accessibility |
| // and instead, "checked" or "not checked" is read along with the search engine's name, e.g. |
| // "google.com checked" or "google.com not checked". |
| @@ -247,7 +288,7 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On |
| } |
| } |
| - private void searchEngineSelected(int position) { |
| + private String searchEngineSelected(int position) { |
| // First clean up any automatically added permissions (if any) for the previously selected |
| // search engine. |
| SharedPreferences sharedPreferences = |
| @@ -255,7 +296,7 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On |
| if (sharedPreferences.getBoolean(PrefServiceBridge.LOCATION_AUTO_ALLOWED, false)) { |
| if (locationEnabled(mSelectedSearchEnginePosition, false)) { |
| String url = TemplateUrlService.getInstance().getSearchEngineUrlFromTemplateUrl( |
| - toIndex(mSelectedSearchEnginePosition)); |
| + toKeyword(mSelectedSearchEnginePosition)); |
| WebsitePreferenceBridge.nativeSetGeolocationSettingForOrigin( |
| url, url, ContentSetting.DEFAULT.toInt(), false); |
| } |
| @@ -265,10 +306,17 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On |
| // Record the change in search engine. |
| mSelectedSearchEnginePosition = position; |
| - // Report the change back. |
| - mCallback.currentSearchEngineDetermined(toIndex(mSelectedSearchEnginePosition)); |
| + String keyword = toKeyword(mSelectedSearchEnginePosition); |
| + TemplateUrlService.getInstance().setSearchEngine(keyword); |
| + // If the user has manually set the default search engine, disable auto switching. |
| + boolean manualSwitch = mSelectedSearchEnginePosition != mInitialEnginePosition; |
| + if (manualSwitch) { |
| + RecordUserAction.record("SearchEngine_ManualChange"); |
| + LocaleManager.getInstance().setSearchEngineAutoSwitch(false); |
| + } |
| notifyDataSetChanged(); |
| + return keyword; |
| } |
| private void onLocationLinkClicked() { |
| @@ -278,7 +326,7 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On |
| Intent settingsIntent = PreferencesLauncher.createIntentForSettingsPage( |
| mContext, SingleWebsitePreferences.class.getName()); |
| String url = TemplateUrlService.getInstance().getSearchEngineUrlFromTemplateUrl( |
| - toIndex(mSelectedSearchEnginePosition)); |
| + toKeyword(mSelectedSearchEnginePosition)); |
| Bundle fragmentArgs = SingleWebsitePreferences.createFragmentArgsForSite(url); |
| fragmentArgs.putBoolean(SingleWebsitePreferences.EXTRA_LOCATION, |
| locationEnabled(mSelectedSearchEnginePosition, true)); |
| @@ -291,7 +339,7 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On |
| if (position == -1) return false; |
| String url = TemplateUrlService.getInstance().getSearchEngineUrlFromTemplateUrl( |
| - toIndex(position)); |
| + toKeyword(position)); |
| GeolocationInfo locationSettings = new GeolocationInfo(url, null, false); |
| ContentSetting locationPermission = locationSettings.getContentSetting(); |
| // Handle the case where the geoHeader being sent when no permission has been specified. |