2013-06-11 09:57:45 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-06-25 10:00:09 -07:00
|
|
|
package org.mozilla.gecko.home;
|
2013-06-11 09:57:45 -07:00
|
|
|
|
2013-07-11 06:46:44 -07:00
|
|
|
import org.mozilla.gecko.AutocompleteHandler;
|
2013-06-25 10:00:09 -07:00
|
|
|
import org.mozilla.gecko.GeckoAppShell;
|
|
|
|
import org.mozilla.gecko.GeckoEvent;
|
|
|
|
import org.mozilla.gecko.R;
|
|
|
|
import org.mozilla.gecko.Tab;
|
|
|
|
import org.mozilla.gecko.Tabs;
|
2013-06-11 09:57:45 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserDB.URLColumns;
|
2013-06-11 09:57:45 -07:00
|
|
|
import org.mozilla.gecko.gfx.BitmapUtils;
|
2013-07-12 14:51:28 -07:00
|
|
|
import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
|
2013-07-22 12:49:36 -07:00
|
|
|
import org.mozilla.gecko.home.SearchLoader.SearchCursorLoader;
|
2013-06-11 10:01:35 -07:00
|
|
|
import org.mozilla.gecko.util.GeckoEventListener;
|
2013-07-11 06:46:44 -07:00
|
|
|
import org.mozilla.gecko.util.StringUtils;
|
2013-06-11 09:57:45 -07:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2013-06-11 10:01:35 -07:00
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2013-06-11 09:57:45 -07:00
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
2013-06-11 09:57:45 -07:00
|
|
|
import android.graphics.Bitmap;
|
2013-07-11 06:46:44 -07:00
|
|
|
import android.net.Uri;
|
2013-06-11 09:57:45 -07:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
2013-06-11 10:01:35 -07:00
|
|
|
import android.support.v4.content.AsyncTaskLoader;
|
2013-06-11 09:57:45 -07:00
|
|
|
import android.support.v4.content.Loader;
|
2013-06-28 10:56:35 -07:00
|
|
|
import android.support.v4.widget.SimpleCursorAdapter;
|
2013-06-11 09:57:45 -07:00
|
|
|
import android.text.TextUtils;
|
2013-06-11 10:01:35 -07:00
|
|
|
import android.util.Log;
|
2013-07-22 12:49:36 -07:00
|
|
|
import android.view.LayoutInflater;
|
2013-06-11 09:57:45 -07:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ListView;
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
import java.util.ArrayList;
|
2013-07-11 06:46:44 -07:00
|
|
|
import java.util.List;
|
2013-06-11 09:57:45 -07:00
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
/**
|
|
|
|
* Fragment that displays frecency search results in a ListView.
|
|
|
|
*/
|
2013-06-14 09:44:00 -07:00
|
|
|
public class BrowserSearch extends HomeFragment
|
2013-06-25 09:51:57 -07:00
|
|
|
implements GeckoEventListener {
|
2013-06-11 10:01:35 -07:00
|
|
|
// Logging tag name
|
|
|
|
private static final String LOGTAG = "GeckoBrowserSearch";
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
// Cursor loader ID for search query
|
|
|
|
private static final int SEARCH_LOADER_ID = 0;
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
// Cursor loader ID for favicons query
|
|
|
|
private static final int FAVICONS_LOADER_ID = 1;
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// AsyncTask loader ID for suggestion query
|
|
|
|
private static final int SUGGESTION_LOADER_ID = 2;
|
|
|
|
|
|
|
|
// Timeout for the suggestion client to respond
|
|
|
|
private static final int SUGGESTION_TIMEOUT = 3000;
|
|
|
|
|
|
|
|
// Maximum number of results returned by the suggestion client
|
|
|
|
private static final int SUGGESTION_MAX = 3;
|
|
|
|
|
2013-07-11 06:46:44 -07:00
|
|
|
// The maximum number of rows deep in a search we'll dig
|
|
|
|
// for an autocomplete result
|
|
|
|
private static final int MAX_AUTOCOMPLETE_SEARCH = 20;
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
// Holds the current search term to use in the query
|
|
|
|
private String mSearchTerm;
|
|
|
|
|
|
|
|
// Adapter for the list of search results
|
|
|
|
private SearchAdapter mAdapter;
|
|
|
|
|
|
|
|
// The view shown by the fragment.
|
|
|
|
private ListView mList;
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// Client that performs search suggestion queries
|
|
|
|
private SuggestClient mSuggestClient;
|
|
|
|
|
|
|
|
// List of search engines from gecko
|
|
|
|
private ArrayList<SearchEngine> mSearchEngines;
|
|
|
|
|
|
|
|
// Whether search suggestions are enabled or not
|
|
|
|
private boolean mSuggestionsEnabled;
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// Callbacks used for the search and favicon cursor loaders
|
|
|
|
private CursorLoaderCallbacks mCursorLoaderCallbacks;
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// Callbacks used for the search suggestion loader
|
|
|
|
private SuggestionLoaderCallbacks mSuggestionLoaderCallbacks;
|
|
|
|
|
|
|
|
// Inflater used by the adapter
|
|
|
|
private LayoutInflater mInflater;
|
|
|
|
|
2013-07-11 06:46:44 -07:00
|
|
|
// Autocomplete handler used when filtering results
|
|
|
|
private AutocompleteHandler mAutocompleteHandler;
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
// On URL open listener
|
|
|
|
private OnUrlOpenListener mUrlOpenListener;
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// On search listener
|
|
|
|
private OnSearchListener mSearchListener;
|
|
|
|
|
|
|
|
// On edit suggestion listener
|
|
|
|
private OnEditSuggestionListener mEditSuggestionListener;
|
|
|
|
|
|
|
|
public interface OnSearchListener {
|
2013-06-25 10:04:00 -07:00
|
|
|
public void onSearch(String engineId, String text);
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnEditSuggestionListener {
|
|
|
|
public void onEditSuggestion(String suggestion);
|
|
|
|
}
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
public static BrowserSearch newInstance() {
|
|
|
|
return new BrowserSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
public BrowserSearch() {
|
|
|
|
mSearchTerm = "";
|
|
|
|
}
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
registerEventListener("SearchEngines:Data");
|
|
|
|
|
|
|
|
// The search engines list is reused beyond the life-cycle of
|
|
|
|
// this fragment.
|
|
|
|
if (mSearchEngines == null) {
|
|
|
|
mSearchEngines = new ArrayList<SearchEngine>();
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:Get", null));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
unregisterEventListener("SearchEngines:Data");
|
|
|
|
}
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
|
|
|
|
try {
|
|
|
|
mUrlOpenListener = (OnUrlOpenListener) activity;
|
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new ClassCastException(activity.toString()
|
|
|
|
+ " must implement BrowserSearch.OnUrlOpenListener");
|
|
|
|
}
|
2013-06-11 10:01:35 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
mSearchListener = (OnSearchListener) activity;
|
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new ClassCastException(activity.toString()
|
|
|
|
+ " must implement BrowserSearch.OnSearchListener");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
mEditSuggestionListener = (OnEditSuggestionListener) activity;
|
|
|
|
} catch (ClassCastException e) {
|
|
|
|
throw new ClassCastException(activity.toString()
|
|
|
|
+ " must implement BrowserSearch.OnEditSuggestionListener");
|
|
|
|
}
|
|
|
|
|
|
|
|
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetach() {
|
|
|
|
super.onDetach();
|
|
|
|
|
2013-06-25 09:51:57 -07:00
|
|
|
mInflater = null;
|
2013-07-11 06:46:44 -07:00
|
|
|
mAutocompleteHandler = null;
|
2013-06-11 09:57:45 -07:00
|
|
|
mUrlOpenListener = null;
|
2013-06-11 10:01:35 -07:00
|
|
|
mSearchListener = null;
|
|
|
|
mEditSuggestionListener = null;
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
// All list views are styled to look the same with a global activity theme.
|
|
|
|
// If the style of the list changes, inflate it from an XML.
|
2013-06-14 09:44:00 -07:00
|
|
|
mList = new HomeListView(container.getContext());
|
2013-06-11 09:57:45 -07:00
|
|
|
return mList;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
|
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
|
2013-06-25 09:51:57 -07:00
|
|
|
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
final Cursor c = mAdapter.getCursor();
|
|
|
|
if (c == null || !c.moveToPosition(position)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
|
|
|
|
mUrlOpenListener.onUrlOpen(url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-14 09:44:00 -07:00
|
|
|
registerForContextMenu(mList);
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
|
|
|
// Intialize the search adapter
|
|
|
|
mAdapter = new SearchAdapter(getActivity());
|
|
|
|
mList.setAdapter(mAdapter);
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// Only create an instance when we need it
|
|
|
|
mSuggestionLoaderCallbacks = null;
|
|
|
|
|
|
|
|
// Create callbacks before the initial loader is started
|
2013-06-11 10:01:35 -07:00
|
|
|
mCursorLoaderCallbacks = new CursorLoaderCallbacks();
|
2013-06-11 09:57:45 -07:00
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// Reconnect to the loader only if present
|
|
|
|
getLoaderManager().initLoader(SEARCH_LOADER_ID, null, mCursorLoaderCallbacks);
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
@Override
|
|
|
|
public void handleMessage(String event, final JSONObject message) {
|
|
|
|
if (event.equals("SearchEngines:Data")) {
|
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
setSearchEngines(message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 06:46:44 -07:00
|
|
|
private void handleAutocomplete(String searchTerm, Cursor c) {
|
|
|
|
if (TextUtils.isEmpty(mSearchTerm) || c == null || mAutocompleteHandler == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid searching the path if we don't have to. Currently just
|
|
|
|
// decided by if there is a '/' character in the string.
|
|
|
|
final boolean searchPath = (searchTerm.indexOf("/") > 0);
|
|
|
|
final String autocompletion = findAutocompletion(searchTerm, c, searchPath);
|
|
|
|
|
|
|
|
if (autocompletion != null && mAutocompleteHandler != null) {
|
|
|
|
mAutocompleteHandler.onAutocomplete(autocompletion);
|
|
|
|
mAutocompleteHandler = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String findAutocompletion(String searchTerm, Cursor c, boolean searchPath) {
|
|
|
|
if (!c.moveToFirst()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final int urlIndex = c.getColumnIndexOrThrow(URLColumns.URL);
|
|
|
|
int searchCount = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
final Uri url = Uri.parse(c.getString(urlIndex));
|
|
|
|
final String host = StringUtils.stripCommonSubdomains(url.getHost());
|
|
|
|
|
|
|
|
// Host may be null for about pages
|
|
|
|
if (host == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
final StringBuilder hostBuilder = new StringBuilder(host);
|
|
|
|
if (hostBuilder.indexOf(searchTerm) == 0) {
|
|
|
|
return hostBuilder.append("/").toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (searchPath) {
|
|
|
|
final List<String> path = url.getPathSegments();
|
|
|
|
|
|
|
|
for (String s : path) {
|
|
|
|
hostBuilder.append("/").append(s);
|
|
|
|
|
|
|
|
if (hostBuilder.indexOf(searchTerm) == 0) {
|
|
|
|
return hostBuilder.append("/").toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
searchCount++;
|
|
|
|
} while (searchCount < MAX_AUTOCOMPLETE_SEARCH && c.moveToNext());
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
private void filterSuggestions() {
|
|
|
|
if (mSuggestClient == null || !mSuggestionsEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSuggestionLoaderCallbacks == null) {
|
|
|
|
mSuggestionLoaderCallbacks = new SuggestionLoaderCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
getLoaderManager().restartLoader(SUGGESTION_LOADER_ID, null, mSuggestionLoaderCallbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setSuggestions(ArrayList<String> suggestions) {
|
|
|
|
mSearchEngines.get(0).suggestions = suggestions;
|
|
|
|
mAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setSearchEngines(JSONObject data) {
|
|
|
|
try {
|
|
|
|
final JSONObject suggest = data.getJSONObject("suggest");
|
|
|
|
final String suggestEngine = suggest.optString("engine", null);
|
|
|
|
final String suggestTemplate = suggest.optString("template", null);
|
|
|
|
final boolean suggestionsPrompted = suggest.getBoolean("prompted");
|
|
|
|
final JSONArray engines = data.getJSONArray("searchEngines");
|
|
|
|
|
|
|
|
mSuggestionsEnabled = suggest.getBoolean("enabled");
|
|
|
|
|
|
|
|
ArrayList<SearchEngine> searchEngines = new ArrayList<SearchEngine>();
|
|
|
|
for (int i = 0; i < engines.length(); i++) {
|
|
|
|
final JSONObject engineJSON = engines.getJSONObject(i);
|
|
|
|
final String name = engineJSON.getString("name");
|
|
|
|
final String identifier = engineJSON.getString("identifier");
|
|
|
|
final String iconURI = engineJSON.getString("iconURI");
|
|
|
|
final Bitmap icon = BitmapUtils.getBitmapFromDataURI(iconURI);
|
|
|
|
|
|
|
|
if (name.equals(suggestEngine) && suggestTemplate != null) {
|
|
|
|
// Suggest engine should be at the front of the list
|
|
|
|
searchEngines.add(0, new SearchEngine(name, identifier, icon));
|
|
|
|
|
|
|
|
// The only time Tabs.getInstance().getSelectedTab() should
|
|
|
|
// be null is when we're restoring after a crash. We should
|
|
|
|
// never restore private tabs when that happens, so it
|
|
|
|
// should be safe to assume that null means non-private.
|
|
|
|
Tab tab = Tabs.getInstance().getSelectedTab();
|
|
|
|
if (tab == null || !tab.isPrivate()) {
|
|
|
|
mSuggestClient = new SuggestClient(getActivity(), suggestTemplate,
|
|
|
|
SUGGESTION_TIMEOUT, SUGGESTION_MAX);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
searchEngines.add(new SearchEngine(name, identifier, icon));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mSearchEngines = searchEngines;
|
|
|
|
|
|
|
|
if (mAdapter != null) {
|
|
|
|
mAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: restore suggestion opt-in UI
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOGTAG, "Error getting search engine JSON", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
filterSuggestions();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void registerEventListener(String eventName) {
|
2013-06-25 10:00:09 -07:00
|
|
|
GeckoAppShell.registerEventListener(eventName, this);
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void unregisterEventListener(String eventName) {
|
2013-06-25 10:00:09 -07:00
|
|
|
GeckoAppShell.unregisterEventListener(eventName, this);
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
|
2013-07-11 06:46:44 -07:00
|
|
|
public void filter(String searchTerm, AutocompleteHandler handler) {
|
2013-06-11 09:57:45 -07:00
|
|
|
if (TextUtils.isEmpty(searchTerm)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TextUtils.equals(mSearchTerm, searchTerm)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSearchTerm = searchTerm;
|
2013-07-11 06:46:44 -07:00
|
|
|
mAutocompleteHandler = handler;
|
2013-06-11 09:57:45 -07:00
|
|
|
|
|
|
|
if (isVisible()) {
|
2013-06-11 10:01:35 -07:00
|
|
|
// The adapter depends on the search term to determine its number
|
|
|
|
// of items. Make it we notify the view about it.
|
|
|
|
mAdapter.notifyDataSetChanged();
|
2013-06-11 10:01:35 -07:00
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// Restart loaders with the new search term
|
2013-07-22 12:49:36 -07:00
|
|
|
SearchLoader.restart(getLoaderManager(), SEARCH_LOADER_ID, mCursorLoaderCallbacks, mSearchTerm, false);
|
2013-06-11 10:01:35 -07:00
|
|
|
filterSuggestions();
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
private static class SuggestionAsyncLoader extends AsyncTaskLoader<ArrayList<String>> {
|
|
|
|
private final SuggestClient mSuggestClient;
|
|
|
|
private final String mSearchTerm;
|
|
|
|
private ArrayList<String> mSuggestions;
|
|
|
|
|
|
|
|
public SuggestionAsyncLoader(Context context, SuggestClient suggestClient, String searchTerm) {
|
|
|
|
super(context);
|
|
|
|
mSuggestClient = suggestClient;
|
|
|
|
mSearchTerm = searchTerm;
|
|
|
|
mSuggestions = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ArrayList<String> loadInBackground() {
|
|
|
|
return mSuggestClient.query(mSearchTerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deliverResult(ArrayList<String> suggestions) {
|
|
|
|
mSuggestions = suggestions;
|
|
|
|
|
|
|
|
if (isStarted()) {
|
|
|
|
super.deliverResult(mSuggestions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStartLoading() {
|
|
|
|
if (mSuggestions != null) {
|
|
|
|
deliverResult(mSuggestions);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (takeContentChanged() || mSuggestions == null) {
|
|
|
|
forceLoad();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStopLoading() {
|
|
|
|
cancelLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onReset() {
|
|
|
|
super.onReset();
|
|
|
|
|
|
|
|
onStopLoading();
|
|
|
|
mSuggestions = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
private class SearchAdapter extends SimpleCursorAdapter {
|
2013-06-11 10:01:35 -07:00
|
|
|
private static final int ROW_SEARCH = 0;
|
|
|
|
private static final int ROW_STANDARD = 1;
|
|
|
|
private static final int ROW_SUGGEST = 2;
|
|
|
|
|
|
|
|
private static final int ROW_TYPE_COUNT = 3;
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
public SearchAdapter(Context context) {
|
|
|
|
super(context, -1, null, new String[] {}, new int[] {});
|
|
|
|
}
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
final int engine = getEngineIndex(position);
|
|
|
|
|
|
|
|
if (engine == -1) {
|
|
|
|
return ROW_STANDARD;
|
|
|
|
} else if (engine == 0 && mSuggestionsEnabled) {
|
|
|
|
// Give suggestion views their own type to prevent them from
|
|
|
|
// sharing other recycled search engine views. Using other
|
|
|
|
// recycled views for the suggestion row can break animations
|
|
|
|
// (bug 815937).
|
|
|
|
return ROW_SUGGEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ROW_SEARCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getViewTypeCount() {
|
|
|
|
// view can be either a standard awesomebar row, a search engine
|
|
|
|
// row, or a suggestion row
|
|
|
|
return ROW_TYPE_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled(int position) {
|
|
|
|
// If the suggestion row only contains one item (the user-entered
|
|
|
|
// query), allow the entire row to be clickable; clicking the row
|
|
|
|
// has the same effect as clicking the single suggestion. If the
|
|
|
|
// row contains multiple items, clicking the row will do nothing.
|
|
|
|
final int index = getEngineIndex(position);
|
|
|
|
if (index != -1) {
|
|
|
|
return mSearchEngines.get(index).suggestions.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the search engines to the number of reported results.
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
final int resultCount = super.getCount();
|
|
|
|
|
|
|
|
// Don't show search engines or suggestions if search field is empty
|
|
|
|
if (TextUtils.isEmpty(mSearchTerm)) {
|
|
|
|
return resultCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultCount + mSearchEngines.size();
|
|
|
|
}
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
2013-06-11 10:01:35 -07:00
|
|
|
final int type = getItemViewType(position);
|
|
|
|
|
|
|
|
if (type == ROW_SEARCH || type == ROW_SUGGEST) {
|
|
|
|
final SearchEngineRow row;
|
|
|
|
if (convertView == null) {
|
|
|
|
row = (SearchEngineRow) mInflater.inflate(R.layout.home_search_item_row, mList, false);
|
|
|
|
row.setOnUrlOpenListener(mUrlOpenListener);
|
|
|
|
row.setOnSearchListener(mSearchListener);
|
|
|
|
row.setOnEditSuggestionListener(mEditSuggestionListener);
|
|
|
|
} else {
|
|
|
|
row = (SearchEngineRow) convertView;
|
|
|
|
}
|
|
|
|
|
|
|
|
row.setSearchTerm(mSearchTerm);
|
|
|
|
|
|
|
|
final SearchEngine engine = mSearchEngines.get(getEngineIndex(position));
|
|
|
|
row.updateFromSearchEngine(engine);
|
|
|
|
|
|
|
|
return row;
|
2013-06-11 09:57:45 -07:00
|
|
|
} else {
|
2013-06-11 10:01:35 -07:00
|
|
|
final TwoLinePageRow row;
|
|
|
|
if (convertView == null) {
|
|
|
|
row = (TwoLinePageRow) mInflater.inflate(R.layout.home_item_row, mList, false);
|
|
|
|
} else {
|
|
|
|
row = (TwoLinePageRow) convertView;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Account for the search engines
|
|
|
|
position -= getSuggestEngineCount();
|
|
|
|
|
|
|
|
final Cursor c = getCursor();
|
|
|
|
if (!c.moveToPosition(position)) {
|
|
|
|
throw new IllegalStateException("Couldn't move cursor to position " + position);
|
|
|
|
}
|
2013-06-11 09:57:45 -07:00
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
row.updateFromCursor(c);
|
|
|
|
|
|
|
|
return row;
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
2013-06-11 09:57:45 -07:00
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
private int getSuggestEngineCount() {
|
|
|
|
return (TextUtils.isEmpty(mSearchTerm) || mSuggestClient == null || !mSuggestionsEnabled) ? 0 : 1;
|
|
|
|
}
|
2013-06-11 09:57:45 -07:00
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
private int getEngineIndex(int position) {
|
|
|
|
final int resultCount = super.getCount();
|
|
|
|
final int suggestEngineCount = getSuggestEngineCount();
|
2013-06-11 09:57:45 -07:00
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// Return suggest engine index
|
|
|
|
if (position < suggestEngineCount) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not an engine
|
|
|
|
if (position - suggestEngineCount < resultCount) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return search engine index
|
|
|
|
return position - resultCount;
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
}
|
2013-06-11 10:01:35 -07:00
|
|
|
|
|
|
|
private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
|
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
|
|
|
switch(id) {
|
|
|
|
case SEARCH_LOADER_ID:
|
2013-07-22 12:49:36 -07:00
|
|
|
return SearchLoader.createInstance(getActivity(), args);
|
2013-06-11 10:01:35 -07:00
|
|
|
|
|
|
|
case FAVICONS_LOADER_ID:
|
2013-06-25 09:51:57 -07:00
|
|
|
return FaviconsLoader.createInstance(getActivity(), args);
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
|
|
|
|
final int loaderId = loader.getId();
|
|
|
|
switch(loaderId) {
|
|
|
|
case SEARCH_LOADER_ID:
|
|
|
|
mAdapter.swapCursor(c);
|
|
|
|
|
2013-07-11 06:46:44 -07:00
|
|
|
// We should handle autocompletion based on the search term
|
|
|
|
// associated with the currently loader that has just provided
|
|
|
|
// the results.
|
|
|
|
SearchCursorLoader searchLoader = (SearchCursorLoader) loader;
|
|
|
|
handleAutocomplete(searchLoader.getSearchTerm(), c);
|
|
|
|
|
2013-06-25 09:51:57 -07:00
|
|
|
FaviconsLoader.restartFromCursor(getLoaderManager(), FAVICONS_LOADER_ID,
|
|
|
|
mCursorLoaderCallbacks, c);
|
2013-06-11 10:01:35 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FAVICONS_LOADER_ID:
|
|
|
|
// Causes the listview to recreate its children and use the
|
|
|
|
// now in-memory favicons.
|
2013-06-14 02:01:25 -07:00
|
|
|
mAdapter.notifyDataSetChanged();
|
2013-06-11 10:01:35 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
|
|
|
final int loaderId = loader.getId();
|
|
|
|
switch(loaderId) {
|
|
|
|
case SEARCH_LOADER_ID:
|
|
|
|
mAdapter.swapCursor(null);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FAVICONS_LOADER_ID:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-11 10:01:35 -07:00
|
|
|
|
|
|
|
private class SuggestionLoaderCallbacks implements LoaderCallbacks<ArrayList<String>> {
|
|
|
|
@Override
|
|
|
|
public Loader<ArrayList<String>> onCreateLoader(int id, Bundle args) {
|
|
|
|
return new SuggestionAsyncLoader(getActivity(), mSuggestClient, mSearchTerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<ArrayList<String>> loader, ArrayList<String> suggestions) {
|
|
|
|
setSuggestions(suggestions);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<ArrayList<String>> loader) {
|
|
|
|
setSuggestions(new ArrayList<String>());
|
|
|
|
}
|
|
|
|
}
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|