Bug 1030896 - Add swipe-to-dismiss to PreSearchFragment list. r=margaret

This commit is contained in:
Brian Nicholson 2014-09-22 15:20:31 -07:00
parent e74295936a
commit 696c189f1f

View File

@ -8,6 +8,7 @@ import android.app.Activity;
import android.database.Cursor;
import android.graphics.Rect;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
@ -15,6 +16,7 @@ import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -28,6 +30,8 @@ import org.mozilla.gecko.Telemetry;
import org.mozilla.gecko.TelemetryContract;
import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.db.BrowserContract.SearchHistory;
import org.mozilla.gecko.widget.SwipeDismissListViewTouchListener;
import org.mozilla.gecko.widget.SwipeDismissListViewTouchListener.OnDismissCallback;
import org.mozilla.search.AcceptsSearchQuery.SuggestionAnimation;
/**
@ -35,6 +39,8 @@ import org.mozilla.search.AcceptsSearchQuery.SuggestionAnimation;
*/
public class PreSearchFragment extends Fragment {
private static final String LOG_TAG = "PreSearchFragment";
private AcceptsSearchQuery searchListener;
private SimpleCursorAdapter cursorAdapter;
@ -97,11 +103,7 @@ public class PreSearchFragment extends Fragment {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Cursor c = cursorAdapter.getCursor();
if (c == null || !c.moveToPosition(position)) {
return;
}
final String query = c.getString(c.getColumnIndexOrThrow(SearchHistory.QUERY));
final String query = getQueryAtPosition(position);
if (!TextUtils.isEmpty(query)) {
final Rect startBounds = new Rect();
view.getGlobalVisibleRect(startBounds);
@ -118,9 +120,49 @@ public class PreSearchFragment extends Fragment {
}
});
// Create a ListView-specific touch listener. ListViews are given special treatment because
// by default they handle touches for their list items... i.e. they're in charge of drawing
// the pressed state (the list selector), handling list item clicks, etc.
final SwipeDismissListViewTouchListener touchListener = new SwipeDismissListViewTouchListener(listView, new OnDismissCallback() {
@Override
public void onDismiss(ListView listView, final int position) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
final String query = getQueryAtPosition(position);
final int deleted = getActivity().getContentResolver().delete(
SearchHistory.CONTENT_URI,
SearchHistory.QUERY + " = ?",
new String[] { query });
if (deleted < 1) {
Log.w(LOG_TAG, "Search query not deleted: " + query);
}
return null;
}
}.execute();
}
});
listView.setOnTouchListener(touchListener);
// Setting this scroll listener is required to ensure that during ListView scrolling,
// we don't look for swipes.
listView.setOnScrollListener(touchListener.makeScrollListener());
// Setting this recycler listener is required to make sure animated views are reset.
listView.setRecyclerListener(touchListener.makeRecyclerListener());
return mainView;
}
private String getQueryAtPosition(int position) {
final Cursor c = cursorAdapter.getCursor();
if (c == null || !c.moveToPosition(position)) {
return null;
}
return c.getString(c.getColumnIndexOrThrow(SearchHistory.QUERY));
}
@Override
public void onDestroyView() {
super.onDestroyView();