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
|
|
|
|
2014-05-02 11:34:40 -07:00
|
|
|
import org.mozilla.gecko.EventDispatcher;
|
2013-06-25 10:00:09 -07:00
|
|
|
import org.mozilla.gecko.GeckoAppShell;
|
|
|
|
import org.mozilla.gecko.GeckoEvent;
|
2013-07-25 11:08:37 -07:00
|
|
|
import org.mozilla.gecko.PrefsHelper;
|
2013-06-25 10:00:09 -07:00
|
|
|
import org.mozilla.gecko.R;
|
|
|
|
import org.mozilla.gecko.Tab;
|
|
|
|
import org.mozilla.gecko.Tabs;
|
2014-04-23 16:08:05 -07:00
|
|
|
import org.mozilla.gecko.Telemetry;
|
|
|
|
import org.mozilla.gecko.TelemetryContract;
|
2013-06-11 09:57:45 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserDB.URLColumns;
|
2013-07-12 14:51:28 -07:00
|
|
|
import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
|
2013-10-18 13:13:37 -07:00
|
|
|
import org.mozilla.gecko.home.SearchEngine;
|
2013-07-22 12:49:36 -07:00
|
|
|
import org.mozilla.gecko.home.SearchLoader.SearchCursorLoader;
|
2013-11-18 21:09:00 -08:00
|
|
|
import org.mozilla.gecko.mozglue.RobocopTarget;
|
2013-11-19 07:46:48 -08:00
|
|
|
import org.mozilla.gecko.toolbar.AutocompleteHandler;
|
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-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;
|
|
|
|
import android.text.TextUtils;
|
2013-08-15 09:33:27 -07:00
|
|
|
import android.util.AttributeSet;
|
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-08-15 09:33:27 -07:00
|
|
|
import android.view.MotionEvent;
|
2013-06-11 09:57:45 -07:00
|
|
|
import android.view.View;
|
2013-07-25 11:08:37 -07:00
|
|
|
import android.view.View.OnClickListener;
|
2013-06-11 09:57:45 -07:00
|
|
|
import android.view.ViewGroup;
|
2013-07-25 11:08:37 -07:00
|
|
|
import android.view.ViewStub;
|
2014-01-22 17:28:08 -08:00
|
|
|
import android.view.WindowManager;
|
2013-07-25 11:08:37 -07:00
|
|
|
import android.view.WindowManager.LayoutParams;
|
|
|
|
import android.view.animation.AccelerateInterpolator;
|
|
|
|
import android.view.animation.Animation;
|
|
|
|
import android.view.animation.TranslateAnimation;
|
2013-06-11 09:57:45 -07:00
|
|
|
import android.widget.AdapterView;
|
2013-07-25 11:08:37 -07:00
|
|
|
import android.widget.LinearLayout;
|
2013-06-11 09:57:45 -07:00
|
|
|
import android.widget.ListView;
|
2013-07-25 11:08:37 -07:00
|
|
|
import android.widget.TextView;
|
2013-06-11 09:57:45 -07:00
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
import java.util.ArrayList;
|
2013-08-12 11:44:46 -07:00
|
|
|
import java.util.EnumSet;
|
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
|
2013-07-29 15:05:23 -07:00
|
|
|
private static final int LOADER_ID_SEARCH = 0;
|
2013-06-11 09:57:45 -07:00
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// AsyncTask loader ID for suggestion query
|
2013-07-29 15:05:23 -07:00
|
|
|
private static final int LOADER_ID_SUGGESTION = 1;
|
2013-06-11 10:01:35 -07:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2014-04-16 12:53:03 -07:00
|
|
|
// Length of https:// + 1 required to make autocomplete
|
|
|
|
// fill in the domain, for both http:// and https://
|
|
|
|
private static final int HTTPS_PREFIX_LENGTH = 9;
|
|
|
|
|
2013-07-25 11:08:37 -07:00
|
|
|
// Duration for fade-in animation
|
|
|
|
private static final int ANIMATION_DURATION = 250;
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
// Holds the current search term to use in the query
|
2014-04-02 06:39:44 -07:00
|
|
|
private volatile String mSearchTerm;
|
2013-06-11 09:57:45 -07:00
|
|
|
|
|
|
|
// Adapter for the list of search results
|
|
|
|
private SearchAdapter mAdapter;
|
|
|
|
|
2013-07-25 11:08:37 -07:00
|
|
|
// The view shown by the fragment
|
|
|
|
private LinearLayout mView;
|
|
|
|
|
|
|
|
// The list showing search results
|
2013-08-28 17:42:32 -07:00
|
|
|
private HomeListView mList;
|
2013-06-11 09:57:45 -07:00
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// Client that performs search suggestion queries
|
2013-07-25 11:08:37 -07:00
|
|
|
private volatile SuggestClient mSuggestClient;
|
2013-06-11 10:01:35 -07:00
|
|
|
|
|
|
|
// List of search engines from gecko
|
|
|
|
private ArrayList<SearchEngine> mSearchEngines;
|
|
|
|
|
|
|
|
// Whether search suggestions are enabled or not
|
|
|
|
private boolean mSuggestionsEnabled;
|
|
|
|
|
2013-09-18 09:58:25 -07:00
|
|
|
// Callbacks used for the search loader
|
2013-06-11 10:01:35 -07:00
|
|
|
private CursorLoaderCallbacks mCursorLoaderCallbacks;
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// Callbacks used for the search suggestion loader
|
|
|
|
private SuggestionLoaderCallbacks mSuggestionLoaderCallbacks;
|
|
|
|
|
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;
|
|
|
|
|
2013-07-25 11:08:37 -07:00
|
|
|
// Whether the suggestions will fade in when shown
|
|
|
|
private boolean mAnimateSuggestions;
|
|
|
|
|
|
|
|
// Opt-in prompt view for search suggestions
|
|
|
|
private View mSuggestionsOptInPrompt;
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
public interface OnSearchListener {
|
2013-10-18 13:13:37 -07:00
|
|
|
public void onSearch(SearchEngine engine, 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() {
|
2013-10-16 02:30:40 -07:00
|
|
|
BrowserSearch browserSearch = new BrowserSearch();
|
|
|
|
|
|
|
|
final Bundle args = new Bundle();
|
|
|
|
args.putBoolean(HomePager.CAN_LOAD_ARG, true);
|
|
|
|
browserSearch.setArguments(args);
|
|
|
|
|
|
|
|
return browserSearch;
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public BrowserSearch() {
|
|
|
|
mSearchTerm = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
@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");
|
|
|
|
}
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetach() {
|
|
|
|
super.onDetach();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-07-26 10:42:38 -07:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
mSearchEngines = new ArrayList<SearchEngine>();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
|
|
|
|
mSearchEngines = null;
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:28:08 -08:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
|
|
|
|
// Adjusting the window size when showing the keyboard results in the underlying
|
|
|
|
// activity being painted when the keyboard is hidden (bug 933422). This can be
|
|
|
|
// prevented by not resizing the window.
|
|
|
|
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
|
|
|
|
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
|
|
|
|
}
|
|
|
|
|
2014-04-23 16:08:05 -07:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
Telemetry.startUISession(TelemetryContract.Session.FRECENCY);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
|
|
|
|
Telemetry.stopUISession(TelemetryContract.Session.FRECENCY);
|
|
|
|
}
|
|
|
|
|
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-07-25 11:08:37 -07:00
|
|
|
mView = (LinearLayout) inflater.inflate(R.layout.browser_search, container, false);
|
2013-08-28 17:42:32 -07:00
|
|
|
mList = (HomeListView) mView.findViewById(R.id.home_list_view);
|
2013-07-25 11:08:37 -07:00
|
|
|
|
|
|
|
return mView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
super.onDestroyView();
|
|
|
|
|
2014-05-02 11:34:40 -07:00
|
|
|
EventDispatcher.getInstance().unregisterGeckoThreadListener(this,
|
|
|
|
"SearchEngines:Data");
|
2013-07-25 11:08:37 -07:00
|
|
|
|
2013-10-10 13:54:49 -07:00
|
|
|
mList.setAdapter(null);
|
2013-07-25 11:08:37 -07:00
|
|
|
mList = null;
|
2013-10-10 13:54:49 -07:00
|
|
|
|
|
|
|
mView = null;
|
2013-07-25 11:08:37 -07:00
|
|
|
mSuggestionsOptInPrompt = null;
|
|
|
|
mSuggestClient = null;
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
|
|
|
super.onViewCreated(view, savedInstanceState);
|
2013-09-12 06:08:43 -07:00
|
|
|
mList.setTag(HomePager.LIST_TAG_BROWSER_SEARCH);
|
2013-06-11 09:57:45 -07:00
|
|
|
|
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) {
|
2013-09-04 15:10:13 -07:00
|
|
|
// Perform the user-entered search if the user clicks on a search engine row.
|
|
|
|
// This row will be disabled if suggestions (in addition to the user-entered term) are showing.
|
|
|
|
if (view instanceof SearchEngineRow) {
|
|
|
|
((SearchEngineRow) view).performUserEnteredSearch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Account for the search engine rows.
|
2013-08-05 12:00:18 -07:00
|
|
|
position -= getSuggestEngineCount();
|
2013-08-07 08:50:52 -07:00
|
|
|
final Cursor c = mAdapter.getCursor(position);
|
2013-06-25 09:51:57 -07:00
|
|
|
final String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
|
2013-08-12 11:44:46 -07:00
|
|
|
|
2014-04-23 16:08:05 -07:00
|
|
|
// The "urlbar" and "frecency" sessions can be open at the same time. Use the LIST_ITEM
|
|
|
|
// method to set this LOAD_URL event apart from the case where the user commits what's in
|
|
|
|
// the url bar.
|
|
|
|
Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, TelemetryContract.Method.LIST_ITEM);
|
|
|
|
|
2013-08-12 11:44:46 -07:00
|
|
|
// This item is a TwoLinePageRow, so we allow switch-to-tab.
|
|
|
|
mUrlOpenListener.onUrlOpen(url, EnumSet.of(OnUrlOpenListener.Flags.ALLOW_SWITCH_TO_TAB));
|
2013-06-25 09:51:57 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-28 17:42:32 -07:00
|
|
|
mList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
|
2013-09-04 15:10:13 -07:00
|
|
|
// Don't do anything when the user long-clicks on a search engine row.
|
|
|
|
if (view instanceof SearchEngineRow) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-28 17:42:32 -07:00
|
|
|
|
2013-09-04 15:10:13 -07:00
|
|
|
// Account for the search engine rows.
|
|
|
|
position -= getSuggestEngineCount();
|
2013-08-28 17:42:32 -07:00
|
|
|
return mList.onItemLongClick(parent, view, position, id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-26 11:21:39 -07:00
|
|
|
final ListSelectionListener listener = new ListSelectionListener();
|
|
|
|
mList.setOnItemSelectedListener(listener);
|
|
|
|
mList.setOnFocusChangeListener(listener);
|
|
|
|
|
|
|
|
mList.setOnKeyListener(new View.OnKeyListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onKey(View v, int keyCode, android.view.KeyEvent event) {
|
|
|
|
final View selected = mList.getSelectedView();
|
|
|
|
|
|
|
|
if (selected instanceof SearchEngineRow) {
|
|
|
|
return selected.onKeyDown(keyCode, event);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-14 09:44:00 -07:00
|
|
|
registerForContextMenu(mList);
|
2014-05-02 11:34:40 -07:00
|
|
|
EventDispatcher.getInstance().registerGeckoThreadListener(this,
|
|
|
|
"SearchEngines:Data");
|
2013-07-25 11:08:37 -07:00
|
|
|
|
2013-10-15 22:02:16 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:GetVisible", null));
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
|
|
|
// Intialize the search adapter
|
2013-09-18 09:58:25 -07:00
|
|
|
mAdapter = new SearchAdapter(getActivity());
|
2013-06-11 09:57:45 -07:00
|
|
|
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-09-18 09:58:25 -07:00
|
|
|
mCursorLoaderCallbacks = new CursorLoaderCallbacks();
|
2013-07-26 04:46:28 -07:00
|
|
|
loadIfVisible();
|
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-26 04:46:28 -07:00
|
|
|
@Override
|
|
|
|
protected void load() {
|
2013-10-16 09:02:02 -07:00
|
|
|
SearchLoader.init(getLoaderManager(), LOADER_ID_SEARCH, mCursorLoaderCallbacks, mSearchTerm);
|
2013-07-26 04:46:28 -07:00
|
|
|
}
|
|
|
|
|
2013-07-11 06:46:44 -07:00
|
|
|
private void handleAutocomplete(String searchTerm, Cursor c) {
|
2013-12-03 22:02:28 -08:00
|
|
|
if (c == null ||
|
|
|
|
mAutocompleteHandler == null ||
|
2013-12-05 20:50:13 -08:00
|
|
|
TextUtils.isEmpty(searchTerm)) {
|
2013-07-11 06:46:44 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid searching the path if we don't have to. Currently just
|
2013-12-03 22:02:28 -08:00
|
|
|
// decided by whether there is a '/' character in the string.
|
|
|
|
final boolean searchPath = searchTerm.indexOf('/') > 0;
|
2013-07-11 06:46:44 -07:00
|
|
|
final String autocompletion = findAutocompletion(searchTerm, c, searchPath);
|
|
|
|
|
2013-12-03 22:02:28 -08:00
|
|
|
if (autocompletion == null || mAutocompleteHandler == null) {
|
|
|
|
return;
|
2013-12-03 22:02:28 -08:00
|
|
|
}
|
2013-12-03 22:02:28 -08:00
|
|
|
|
2013-12-05 20:50:23 -08:00
|
|
|
// Prefetch auto-completed domain since it's a likely target
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Session:Prefetch", "http://" + autocompletion));
|
|
|
|
|
2013-12-03 22:02:28 -08:00
|
|
|
mAutocompleteHandler.onAutocomplete(autocompletion);
|
|
|
|
mAutocompleteHandler = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the substring of a provided URI, starting at the given offset,
|
|
|
|
* and extending up to the end of the path segment in which the provided
|
|
|
|
* index is found.
|
|
|
|
*
|
|
|
|
* For example, given
|
|
|
|
*
|
|
|
|
* "www.reddit.com/r/boop/abcdef", 0, ?
|
|
|
|
*
|
|
|
|
* this method returns
|
|
|
|
*
|
|
|
|
* ?=2: "www.reddit.com/"
|
|
|
|
* ?=17: "www.reddit.com/r/boop/"
|
|
|
|
* ?=21: "www.reddit.com/r/boop/"
|
|
|
|
* ?=22: "www.reddit.com/r/boop/abcdef"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static String uriSubstringUpToMatchedPath(final String url, final int offset, final int begin) {
|
|
|
|
final int afterEnd = url.length();
|
|
|
|
|
|
|
|
// We want to include the trailing slash, but not other characters.
|
|
|
|
int chop = url.indexOf('/', begin);
|
|
|
|
if (chop != -1) {
|
|
|
|
++chop;
|
|
|
|
if (chop < offset) {
|
|
|
|
// This isn't supposed to happen. Fall back to returning the whole damn thing.
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chop = url.indexOf('?', begin);
|
|
|
|
if (chop == -1) {
|
|
|
|
chop = url.indexOf('#', begin);
|
|
|
|
}
|
|
|
|
if (chop == -1) {
|
|
|
|
chop = afterEnd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.substring(offset, chop);
|
2013-07-11 06:46:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private String findAutocompletion(String searchTerm, Cursor c, boolean searchPath) {
|
|
|
|
if (!c.moveToFirst()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:02:28 -08:00
|
|
|
final int searchLength = searchTerm.length();
|
2013-07-11 06:46:44 -07:00
|
|
|
final int urlIndex = c.getColumnIndexOrThrow(URLColumns.URL);
|
|
|
|
int searchCount = 0;
|
|
|
|
|
|
|
|
do {
|
2013-12-03 22:02:28 -08:00
|
|
|
final String url = c.getString(urlIndex);
|
2013-12-03 22:02:28 -08:00
|
|
|
|
2013-12-05 20:50:23 -08:00
|
|
|
if (searchCount == 0) {
|
|
|
|
// Prefetch the first item in the list since it's weighted the highest
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Session:Prefetch", url.toString()));
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:02:28 -08:00
|
|
|
// Does the completion match against the whole URL? This will match
|
|
|
|
// about: pages, as well as user input including "http://...".
|
|
|
|
if (url.startsWith(searchTerm)) {
|
2014-04-16 12:53:03 -07:00
|
|
|
return uriSubstringUpToMatchedPath(url, 0,
|
|
|
|
(searchLength > HTTPS_PREFIX_LENGTH) ? searchLength : HTTPS_PREFIX_LENGTH);
|
2013-12-03 22:02:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
final Uri uri = Uri.parse(url);
|
|
|
|
final String host = uri.getHost();
|
|
|
|
|
|
|
|
// Host may be null for about pages.
|
2013-07-11 06:46:44 -07:00
|
|
|
if (host == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:02:28 -08:00
|
|
|
if (host.startsWith(searchTerm)) {
|
|
|
|
return host + "/";
|
2013-12-03 22:02:28 -08:00
|
|
|
}
|
2013-07-11 06:46:44 -07:00
|
|
|
|
2013-12-03 22:02:28 -08:00
|
|
|
final String strippedHost = StringUtils.stripCommonSubdomains(host);
|
|
|
|
if (strippedHost.startsWith(searchTerm)) {
|
|
|
|
return strippedHost + "/";
|
|
|
|
}
|
2013-07-11 06:46:44 -07:00
|
|
|
|
2013-12-03 22:02:28 -08:00
|
|
|
++searchCount;
|
2013-07-11 06:46:44 -07:00
|
|
|
|
2013-12-03 22:02:28 -08:00
|
|
|
if (!searchPath) {
|
|
|
|
continue;
|
2013-12-03 22:02:28 -08:00
|
|
|
}
|
|
|
|
|
2013-12-03 22:02:28 -08:00
|
|
|
// Otherwise, if we're matching paths, let's compare against the string itself.
|
|
|
|
final int hostOffset = url.indexOf(strippedHost);
|
|
|
|
if (hostOffset == -1) {
|
|
|
|
// This was a URL string that parsed to a different host (normalized?).
|
|
|
|
// Give up.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We already matched the non-stripped host, so now we're
|
|
|
|
// substring-searching in the part of the URL without the common
|
|
|
|
// subdomains.
|
|
|
|
if (url.startsWith(searchTerm, hostOffset)) {
|
|
|
|
// Great! Return including the rest of the path segment.
|
|
|
|
return uriSubstringUpToMatchedPath(url, hostOffset, hostOffset + searchLength);
|
|
|
|
}
|
2013-07-11 06:46:44 -07:00
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
|
2013-07-29 15:05:23 -07:00
|
|
|
getLoaderManager().restartLoader(LOADER_ID_SUGGESTION, null, mSuggestionLoaderCallbacks);
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setSuggestions(ArrayList<String> suggestions) {
|
2013-10-18 13:13:37 -07:00
|
|
|
mSearchEngines.get(0).setSuggestions(suggestions);
|
2013-07-26 10:42:38 -07:00
|
|
|
mAdapter.notifyDataSetChanged();
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setSearchEngines(JSONObject data) {
|
2013-07-25 11:08:37 -07:00
|
|
|
// This method is called via a Runnable posted from the Gecko thread, so
|
|
|
|
// it's possible the fragment and/or its view has been destroyed by the
|
|
|
|
// time we get here. If so, just abort.
|
|
|
|
if (mView == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
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);
|
2013-10-18 13:13:37 -07:00
|
|
|
final SearchEngine engine = new SearchEngine(engineJSON);
|
2013-06-11 10:01:35 -07:00
|
|
|
|
2013-10-18 13:13:37 -07:00
|
|
|
if (engine.name.equals(suggestEngine) && suggestTemplate != null) {
|
|
|
|
// Suggest engine should be at the front of the list.
|
|
|
|
searchEngines.add(0, engine);
|
2013-06-11 10:01:35 -07:00
|
|
|
|
|
|
|
// 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();
|
2013-08-14 14:53:26 -07:00
|
|
|
final boolean isPrivate = (tab != null && tab.isPrivate());
|
|
|
|
|
|
|
|
// Only create a new instance of SuggestClient if it hasn't been
|
|
|
|
// set yet. e.g. Robocop tests might set it directly before search
|
|
|
|
// engines are loaded.
|
|
|
|
if (mSuggestClient == null && !isPrivate) {
|
2014-02-14 09:46:55 -08:00
|
|
|
setSuggestClient(new SuggestClient(getActivity(), suggestTemplate,
|
|
|
|
SUGGESTION_TIMEOUT, SUGGESTION_MAX));
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
} else {
|
2013-10-18 13:13:37 -07:00
|
|
|
searchEngines.add(engine);
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mSearchEngines = searchEngines;
|
|
|
|
|
|
|
|
if (mAdapter != null) {
|
|
|
|
mAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2014-02-03 12:35:00 -08:00
|
|
|
// Show suggestions opt-in prompt only if suggestions are not enabled yet,
|
|
|
|
// user hasn't been prompted and we're not on a private browsing tab.
|
|
|
|
if (!mSuggestionsEnabled && !suggestionsPrompted && mSuggestClient != null) {
|
2013-07-25 11:08:37 -07:00
|
|
|
showSuggestionsOptIn();
|
|
|
|
}
|
2013-06-11 10:01:35 -07:00
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOGTAG, "Error getting search engine JSON", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
filterSuggestions();
|
|
|
|
}
|
|
|
|
|
2014-02-14 09:46:55 -08:00
|
|
|
/**
|
|
|
|
* Sets the private SuggestClient instance. Should only be called if the suggestClient is
|
|
|
|
* null (i.e. has not yet been initialized or has been nulled). Non-private access is
|
|
|
|
* for testing purposes only.
|
|
|
|
*/
|
|
|
|
@RobocopTarget
|
|
|
|
public void setSuggestClient(final SuggestClient client) {
|
|
|
|
if (mSuggestClient != null) {
|
|
|
|
throw new IllegalStateException("Can only set the SuggestClient if it has not " +
|
|
|
|
"yet been initialized!");
|
|
|
|
}
|
|
|
|
mSuggestClient = client;
|
|
|
|
}
|
|
|
|
|
2013-07-25 11:08:37 -07:00
|
|
|
private void showSuggestionsOptIn() {
|
2013-09-10 10:00:47 -07:00
|
|
|
// Return if the ViewStub was already inflated - an inflated ViewStub is removed from the
|
|
|
|
// View hierarchy so a second call to findViewById will return null.
|
|
|
|
if (mSuggestionsOptInPrompt != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-25 11:08:37 -07:00
|
|
|
mSuggestionsOptInPrompt = ((ViewStub) mView.findViewById(R.id.suggestions_opt_in_prompt)).inflate();
|
|
|
|
|
|
|
|
TextView promptText = (TextView) mSuggestionsOptInPrompt.findViewById(R.id.suggestions_prompt_title);
|
2014-03-12 08:57:30 -07:00
|
|
|
promptText.setText(getResources().getString(R.string.suggestions_prompt));
|
2013-07-25 11:08:37 -07:00
|
|
|
|
|
|
|
final View yesButton = mSuggestionsOptInPrompt.findViewById(R.id.suggestions_prompt_yes);
|
|
|
|
final View noButton = mSuggestionsOptInPrompt.findViewById(R.id.suggestions_prompt_no);
|
|
|
|
|
|
|
|
final OnClickListener listener = new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
// Prevent the buttons from being clicked multiple times (bug 816902)
|
|
|
|
yesButton.setOnClickListener(null);
|
|
|
|
noButton.setOnClickListener(null);
|
|
|
|
|
|
|
|
setSuggestionsEnabled(v == yesButton);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
yesButton.setOnClickListener(listener);
|
|
|
|
noButton.setOnClickListener(listener);
|
2013-07-26 11:21:39 -07:00
|
|
|
|
|
|
|
// If the prompt gains focus, automatically pass focus to the
|
|
|
|
// yes button in the prompt.
|
|
|
|
final View prompt = mSuggestionsOptInPrompt.findViewById(R.id.prompt);
|
|
|
|
prompt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onFocusChange(View v, boolean hasFocus) {
|
|
|
|
if (hasFocus) {
|
|
|
|
yesButton.requestFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-07-25 11:08:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setSuggestionsEnabled(final boolean enabled) {
|
|
|
|
// Clicking the yes/no buttons quickly can cause the click events be
|
|
|
|
// queued before the listeners are removed above, so it's possible
|
|
|
|
// setSuggestionsEnabled() can be called twice. mSuggestionsOptInPrompt
|
|
|
|
// can be null if this happens (bug 828480).
|
|
|
|
if (mSuggestionsOptInPrompt == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make suggestions appear immediately after the user opts in
|
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
SuggestClient client = mSuggestClient;
|
|
|
|
if (client != null) {
|
|
|
|
client.query(mSearchTerm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pref observer in gecko will also set prompted = true
|
|
|
|
PrefsHelper.setPref("browser.search.suggest.enabled", enabled);
|
|
|
|
|
|
|
|
TranslateAnimation slideAnimation = new TranslateAnimation(0, mSuggestionsOptInPrompt.getWidth(), 0, 0);
|
|
|
|
slideAnimation.setDuration(ANIMATION_DURATION);
|
|
|
|
slideAnimation.setInterpolator(new AccelerateInterpolator());
|
|
|
|
slideAnimation.setFillAfter(true);
|
|
|
|
final View prompt = mSuggestionsOptInPrompt.findViewById(R.id.prompt);
|
|
|
|
|
|
|
|
TranslateAnimation shrinkAnimation = new TranslateAnimation(0, 0, 0, -1 * mSuggestionsOptInPrompt.getHeight());
|
|
|
|
shrinkAnimation.setDuration(ANIMATION_DURATION);
|
|
|
|
shrinkAnimation.setFillAfter(true);
|
|
|
|
shrinkAnimation.setStartOffset(slideAnimation.getDuration());
|
|
|
|
shrinkAnimation.setAnimationListener(new Animation.AnimationListener() {
|
|
|
|
@Override
|
|
|
|
public void onAnimationStart(Animation a) {
|
|
|
|
// Increase the height of the view so a gap isn't shown during animation
|
|
|
|
mView.getLayoutParams().height = mView.getHeight() +
|
|
|
|
mSuggestionsOptInPrompt.getHeight();
|
|
|
|
mView.requestLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationRepeat(Animation a) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationEnd(Animation a) {
|
|
|
|
// Removing the view immediately results in a NPE in
|
|
|
|
// dispatchDraw(), possibly because this callback executes
|
|
|
|
// before drawing is finished. Posting this as a Runnable fixes
|
|
|
|
// the issue.
|
|
|
|
mView.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
mView.removeView(mSuggestionsOptInPrompt);
|
|
|
|
mList.clearAnimation();
|
|
|
|
mSuggestionsOptInPrompt = null;
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
// Reset the view height
|
|
|
|
mView.getLayoutParams().height = LayoutParams.MATCH_PARENT;
|
|
|
|
|
|
|
|
mSuggestionsEnabled = enabled;
|
|
|
|
mAnimateSuggestions = true;
|
|
|
|
mAdapter.notifyDataSetChanged();
|
|
|
|
filterSuggestions();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
prompt.startAnimation(slideAnimation);
|
|
|
|
mSuggestionsOptInPrompt.startAnimation(shrinkAnimation);
|
|
|
|
mList.startAnimation(shrinkAnimation);
|
|
|
|
}
|
|
|
|
|
2013-08-05 12:00:18 -07:00
|
|
|
private int getSuggestEngineCount() {
|
|
|
|
return (TextUtils.isEmpty(mSearchTerm) || mSuggestClient == null || !mSuggestionsEnabled) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2014-04-02 06:39:45 -07:00
|
|
|
private void restartSearchLoader() {
|
|
|
|
SearchLoader.restart(getLoaderManager(), LOADER_ID_SEARCH, mCursorLoaderCallbacks, mSearchTerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initSearchLoader() {
|
|
|
|
SearchLoader.init(getLoaderManager(), LOADER_ID_SEARCH, mCursorLoaderCallbacks, mSearchTerm);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-02 06:39:45 -07:00
|
|
|
final boolean isNewFilter = !TextUtils.equals(mSearchTerm, searchTerm);
|
2013-06-11 09:57:45 -07:00
|
|
|
|
|
|
|
mSearchTerm = searchTerm;
|
2013-07-11 06:46:44 -07:00
|
|
|
mAutocompleteHandler = handler;
|
2013-06-11 09:57:45 -07:00
|
|
|
|
|
|
|
if (isVisible()) {
|
2014-04-02 06:39:45 -07:00
|
|
|
if (isNewFilter) {
|
|
|
|
// 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
|
|
|
|
2014-04-02 06:39:45 -07:00
|
|
|
// Restart loaders with the new search term
|
|
|
|
restartSearchLoader();
|
|
|
|
filterSuggestions();
|
|
|
|
} else {
|
|
|
|
// The search term hasn't changed, simply reuse any existing
|
|
|
|
// loader for the current search term. This will ensure autocompletion
|
|
|
|
// is consistently triggered (see bug 933739).
|
|
|
|
initSearchLoader();
|
|
|
|
}
|
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-08-07 08:50:52 -07:00
|
|
|
private class SearchAdapter extends MultiTypeCursorAdapter {
|
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;
|
|
|
|
|
2013-06-11 09:57:45 -07:00
|
|
|
public SearchAdapter(Context context) {
|
2013-08-07 08:50:52 -07:00
|
|
|
super(context, null, new int[] { ROW_STANDARD,
|
|
|
|
ROW_SEARCH,
|
|
|
|
ROW_SUGGEST },
|
|
|
|
new int[] { R.layout.home_item_row,
|
|
|
|
R.layout.home_search_item_row,
|
|
|
|
R.layout.home_search_item_row });
|
2013-06-11 09:57:45 -07:00
|
|
|
}
|
|
|
|
|
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 boolean isEnabled(int position) {
|
2013-07-26 11:21:39 -07:00
|
|
|
// If we're using a gamepad or keyboard, allow the row to be
|
|
|
|
// focused so it can pass the focus to its child suggestion views.
|
|
|
|
if (!mList.isInTouchMode()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-11 10:01:35 -07:00
|
|
|
// 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) {
|
2013-10-18 13:13:37 -07:00
|
|
|
return !mSearchEngines.get(index).hasSuggestions();
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2013-08-07 08:50:52 -07:00
|
|
|
public void bindView(View view, Context context, int position) {
|
2013-06-11 10:01:35 -07:00
|
|
|
final int type = getItemViewType(position);
|
|
|
|
|
|
|
|
if (type == ROW_SEARCH || type == ROW_SUGGEST) {
|
2013-08-07 08:50:52 -07:00
|
|
|
final SearchEngineRow row = (SearchEngineRow) view;
|
|
|
|
row.setOnUrlOpenListener(mUrlOpenListener);
|
|
|
|
row.setOnSearchListener(mSearchListener);
|
|
|
|
row.setOnEditSuggestionListener(mEditSuggestionListener);
|
2013-06-11 10:01:35 -07:00
|
|
|
row.setSearchTerm(mSearchTerm);
|
|
|
|
|
|
|
|
final SearchEngine engine = mSearchEngines.get(getEngineIndex(position));
|
2013-10-18 13:13:37 -07:00
|
|
|
final boolean animate = (mAnimateSuggestions && engine.hasSuggestions());
|
2013-07-25 11:08:37 -07:00
|
|
|
row.updateFromSearchEngine(engine, animate);
|
|
|
|
if (animate) {
|
|
|
|
// Only animate suggestions the first time they are shown
|
|
|
|
mAnimateSuggestions = false;
|
|
|
|
}
|
2013-06-11 09:57:45 -07:00
|
|
|
} else {
|
2013-06-11 10:01:35 -07:00
|
|
|
// Account for the search engines
|
|
|
|
position -= getSuggestEngineCount();
|
|
|
|
|
2013-08-07 08:50:52 -07:00
|
|
|
final Cursor c = getCursor(position);
|
|
|
|
final TwoLinePageRow row = (TwoLinePageRow) view;
|
2013-07-29 12:07:56 -07:00
|
|
|
row.updateFromCursor(c);
|
|
|
|
}
|
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 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
|
|
|
|
2013-09-18 09:58:25 -07:00
|
|
|
private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
|
2013-06-11 10:01:35 -07:00
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
2013-09-18 09:58:25 -07:00
|
|
|
return SearchLoader.createInstance(getActivity(), args);
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
|
2013-09-18 09:58:25 -07:00
|
|
|
mAdapter.swapCursor(c);
|
2013-07-11 06:46:44 -07:00
|
|
|
|
2013-09-18 09:58:25 -07:00
|
|
|
// We should handle autocompletion based on the search term
|
2013-12-03 22:02:28 -08:00
|
|
|
// associated with the loader that has just provided
|
2013-09-18 09:58:25 -07:00
|
|
|
// the results.
|
|
|
|
SearchCursorLoader searchLoader = (SearchCursorLoader) loader;
|
|
|
|
handleAutocomplete(searchLoader.getSearchTerm(), c);
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) {
|
2013-09-18 09:58:25 -07:00
|
|
|
mAdapter.swapCursor(null);
|
2013-07-29 15:05:23 -07:00
|
|
|
}
|
2013-06-11 10:01:35 -07:00
|
|
|
}
|
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) {
|
2013-07-25 11:08:37 -07:00
|
|
|
// mSuggestClient is set to null in onDestroyView(), so using it
|
|
|
|
// safely here relies on the fact that onCreateLoader() is called
|
|
|
|
// synchronously in restartLoader().
|
2013-06-11 10:01:35 -07:00
|
|
|
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-07-26 11:21:39 -07:00
|
|
|
|
|
|
|
private static class ListSelectionListener implements View.OnFocusChangeListener,
|
|
|
|
AdapterView.OnItemSelectedListener {
|
|
|
|
private SearchEngineRow mSelectedEngineRow;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFocusChange(View v, boolean hasFocus) {
|
|
|
|
if (hasFocus) {
|
|
|
|
View selectedRow = ((ListView) v).getSelectedView();
|
|
|
|
if (selectedRow != null) {
|
|
|
|
selectRow(selectedRow);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deselectRow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
deselectRow();
|
|
|
|
selectRow(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNothingSelected(AdapterView<?> parent) {
|
|
|
|
deselectRow();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void selectRow(View row) {
|
|
|
|
if (row instanceof SearchEngineRow) {
|
|
|
|
mSelectedEngineRow = (SearchEngineRow) row;
|
|
|
|
mSelectedEngineRow.onSelected();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void deselectRow() {
|
|
|
|
if (mSelectedEngineRow != null) {
|
|
|
|
mSelectedEngineRow.onDeselected();
|
|
|
|
mSelectedEngineRow = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-15 09:33:27 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HomeSearchListView is a list view for displaying search engine results on the awesome screen.
|
|
|
|
*/
|
|
|
|
public static class HomeSearchListView extends HomeListView {
|
|
|
|
public HomeSearchListView(Context context, AttributeSet attrs) {
|
|
|
|
this(context, attrs, R.attr.homeListViewStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public HomeSearchListView(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-10-29 04:26:25 -07:00
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
2013-08-15 09:33:27 -07:00
|
|
|
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
|
|
|
|
// Dismiss the soft keyboard.
|
|
|
|
requestFocus();
|
|
|
|
}
|
|
|
|
|
2013-10-29 04:26:25 -07:00
|
|
|
return super.onTouchEvent(event);
|
2013-08-15 09:33:27 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-07 08:50:52 -07:00
|
|
|
}
|