Add saved Search queries feature
- update SearchResultsSummary fragment to have two lists: one for Search suggestions (saved queries) and one for Search results - a tap on a saved query will launch that Search query - show the list of saved queries when tapping on the SearchView - do some fancy hidding / unhidding of the saved queries list and results list Change-Id: If15055ab78b0ec5eef4e543173dc7b866bd08e27
This commit is contained in:
@@ -28,7 +28,6 @@ import android.database.sqlite.SQLiteDatabase;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -38,6 +37,7 @@ import android.widget.AdapterView;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.SearchView;
|
||||
import android.widget.TextView;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
@@ -54,17 +54,23 @@ public class SearchResultsSummary extends Fragment {
|
||||
|
||||
private static final String LOG_TAG = "SearchResultsSummary";
|
||||
|
||||
private static final String EMPTY_QUERY = "";
|
||||
private static char ELLIPSIS = '\u2026';
|
||||
|
||||
private ListView mListView;
|
||||
private SearchView mSearchView;
|
||||
|
||||
private SearchResultsAdapter mAdapter;
|
||||
private ListView mResultsListView;
|
||||
private SearchResultsAdapter mResultsAdapter;
|
||||
private UpdateSearchResultsTask mUpdateSearchResultsTask;
|
||||
|
||||
private String mQuery;
|
||||
private SaveSearchQueryTask mSaveSearchQueryTask;
|
||||
private ListView mSuggestionsListView;
|
||||
private SuggestionsAdapter mSuggestionsAdapter;
|
||||
private UpdateSuggestionsTask mUpdateSuggestionsTask;
|
||||
|
||||
private static long MAX_SAVED_SEARCH_QUERY = 5;
|
||||
private ViewGroup mLayoutSuggestions;
|
||||
private ViewGroup mLayoutResults;
|
||||
|
||||
private String mQuery;
|
||||
|
||||
/**
|
||||
* A basic AsyncTask for updating the query results cursor
|
||||
@@ -78,7 +84,7 @@ public class SearchResultsSummary extends Fragment {
|
||||
@Override
|
||||
protected void onPostExecute(Cursor cursor) {
|
||||
if (!isCancelled()) {
|
||||
setCursor(cursor);
|
||||
setResultsCursor(cursor);
|
||||
} else if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
@@ -86,37 +92,21 @@ public class SearchResultsSummary extends Fragment {
|
||||
}
|
||||
|
||||
/**
|
||||
* A basic AsynTask for saving the Search query into the database
|
||||
* A basic AsyncTask for updating the suggestions cursor
|
||||
*/
|
||||
private class SaveSearchQueryTask extends AsyncTask<String, Void, Long> {
|
||||
private class UpdateSuggestionsTask extends AsyncTask<String, Void, Cursor> {
|
||||
@Override
|
||||
protected Cursor doInBackground(String... params) {
|
||||
return Index.getInstance(getActivity()).getSuggestions(params[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Long doInBackground(String... params) {
|
||||
final long now = new Date().getTime();
|
||||
|
||||
final ContentValues values = new ContentValues();
|
||||
values.put(SavedQueriesColums.QUERY, params[0]);
|
||||
values.put(SavedQueriesColums.TIME_STAMP, now);
|
||||
|
||||
SQLiteDatabase database = IndexDatabaseHelper.getInstance(
|
||||
getActivity()).getWritableDatabase();
|
||||
|
||||
long lastInsertedRowId = -1;
|
||||
try {
|
||||
lastInsertedRowId =
|
||||
database.insert(Tables.TABLE_SAVED_QUERIES, null, values);
|
||||
|
||||
final long delta = lastInsertedRowId - MAX_SAVED_SEARCH_QUERY;
|
||||
if (delta > 0) {
|
||||
int count = database.delete(Tables.TABLE_SAVED_QUERIES, "rowId <= ?",
|
||||
new String[] { Long.toString(delta) });
|
||||
Log.d(LOG_TAG, "Deleted '" + count + "' saved Search query(ies)");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.d(LOG_TAG, "Cannot update saved Search queries", e);
|
||||
protected void onPostExecute(Cursor cursor) {
|
||||
if (!isCancelled()) {
|
||||
setSuggestionsCursor(cursor);
|
||||
} else if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return lastInsertedRowId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,22 +114,30 @@ public class SearchResultsSummary extends Fragment {
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mAdapter = new SearchResultsAdapter(getActivity());
|
||||
mResultsAdapter = new SearchResultsAdapter(getActivity());
|
||||
mSuggestionsAdapter = new SuggestionsAdapter(getActivity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
|
||||
clearSuggestions();
|
||||
clearResults();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
mListView = null;
|
||||
mAdapter = null;
|
||||
mResultsListView = null;
|
||||
mResultsAdapter = null;
|
||||
mUpdateSearchResultsTask = null;
|
||||
|
||||
mSuggestionsListView = null;
|
||||
mSuggestionsAdapter = null;
|
||||
mUpdateSuggestionsTask = null;
|
||||
|
||||
mSearchView = null;
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@@ -147,14 +145,17 @@ public class SearchResultsSummary extends Fragment {
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
final View view = inflater.inflate(R.layout.search_results, container, false);
|
||||
final View view = inflater.inflate(R.layout.search_panel, container, false);
|
||||
|
||||
mListView = (ListView) view.findViewById(R.id.list_results);
|
||||
mListView.setAdapter(mAdapter);
|
||||
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
mLayoutSuggestions = (ViewGroup) view.findViewById(R.id.layout_suggestions);
|
||||
mLayoutResults = (ViewGroup) view.findViewById(R.id.layout_results);
|
||||
|
||||
mResultsListView = (ListView) view.findViewById(R.id.list_results);
|
||||
mResultsListView.setAdapter(mResultsAdapter);
|
||||
mResultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
final Cursor cursor = mAdapter.mCursor;
|
||||
final Cursor cursor = mResultsAdapter.mCursor;
|
||||
cursor.moveToPosition(position);
|
||||
|
||||
final String className = cursor.getString(Index.COLUMN_INDEX_CLASS_NAME);
|
||||
@@ -192,33 +193,85 @@ public class SearchResultsSummary extends Fragment {
|
||||
}
|
||||
});
|
||||
|
||||
mSuggestionsListView = (ListView) view.findViewById(R.id.list_suggestions);
|
||||
mSuggestionsListView.setAdapter(mSuggestionsAdapter);
|
||||
mSuggestionsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
final Cursor cursor = mSuggestionsAdapter.mCursor;
|
||||
cursor.moveToPosition(position);
|
||||
|
||||
mQuery = cursor.getString(0);
|
||||
mSearchView.setQuery(mQuery, false);
|
||||
setSuggestionsVisibility(false);
|
||||
}
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void saveQueryToDatabase() {
|
||||
if (mSaveSearchQueryTask != null) {
|
||||
mSaveSearchQueryTask.cancel(false);
|
||||
mSaveSearchQueryTask = null;
|
||||
}
|
||||
if (!TextUtils.isEmpty(mQuery)) {
|
||||
mSaveSearchQueryTask = new SaveSearchQueryTask();
|
||||
mSaveSearchQueryTask.execute(mQuery);
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
showSomeSuggestions();
|
||||
}
|
||||
|
||||
public void setSearchView(SearchView searchView) {
|
||||
mSearchView = searchView;
|
||||
}
|
||||
|
||||
private void setSuggestionsVisibility(boolean visible) {
|
||||
if (mLayoutSuggestions != null) {
|
||||
mLayoutSuggestions.setVisibility(visible ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
private void setResultsVisibility(boolean visible) {
|
||||
if (mLayoutResults != null) {
|
||||
mLayoutResults.setVisibility(visible ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveQueryToDatabase() {
|
||||
Index.getInstance(getActivity()).addSavedQuery(mQuery);
|
||||
}
|
||||
|
||||
public boolean onQueryTextSubmit(String query) {
|
||||
updateSearchResults(query);
|
||||
mQuery = getFilteredQueryString(query);
|
||||
updateSearchResults();
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean onQueryTextChange(String query) {
|
||||
updateSearchResults(query);
|
||||
mQuery = getFilteredQueryString(query);
|
||||
updateSuggestions();
|
||||
updateSearchResults();
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean onClose() {
|
||||
clearResults();
|
||||
return false;
|
||||
public void showSomeSuggestions() {
|
||||
setResultsVisibility(false);
|
||||
mQuery = EMPTY_QUERY;
|
||||
updateSuggestions();
|
||||
}
|
||||
|
||||
private void clearSuggestions() {
|
||||
if (mUpdateSuggestionsTask != null) {
|
||||
mUpdateSuggestionsTask.cancel(false);
|
||||
mUpdateSuggestionsTask = null;
|
||||
}
|
||||
setSuggestionsCursor(null);
|
||||
}
|
||||
|
||||
private void setSuggestionsCursor(Cursor cursor) {
|
||||
if (mSuggestionsAdapter == null) {
|
||||
return;
|
||||
}
|
||||
Cursor oldCursor = mSuggestionsAdapter.swapCursor(cursor);
|
||||
if (oldCursor != null) {
|
||||
oldCursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void clearResults() {
|
||||
@@ -226,20 +279,23 @@ public class SearchResultsSummary extends Fragment {
|
||||
mUpdateSearchResultsTask.cancel(false);
|
||||
mUpdateSearchResultsTask = null;
|
||||
}
|
||||
setCursor(null);
|
||||
setResultsCursor(null);
|
||||
}
|
||||
|
||||
private void setCursor(Cursor cursor) {
|
||||
if (mAdapter == null) {
|
||||
private void setResultsCursor(Cursor cursor) {
|
||||
if (mResultsAdapter == null) {
|
||||
return;
|
||||
}
|
||||
Cursor oldCursor = mAdapter.swapCursor(cursor);
|
||||
Cursor oldCursor = mResultsAdapter.swapCursor(cursor);
|
||||
if (oldCursor != null) {
|
||||
oldCursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
private String getFilteredQueryString(CharSequence query) {
|
||||
if (query == null) {
|
||||
return null;
|
||||
}
|
||||
final StringBuilder filtered = new StringBuilder();
|
||||
for (int n = 0; n < query.length(); n++) {
|
||||
char c = query.charAt(n);
|
||||
@@ -251,20 +307,123 @@ public class SearchResultsSummary extends Fragment {
|
||||
return filtered.toString();
|
||||
}
|
||||
|
||||
private void updateSearchResults(CharSequence cs) {
|
||||
private void updateSuggestions() {
|
||||
if (mUpdateSuggestionsTask != null) {
|
||||
mUpdateSuggestionsTask.cancel(false);
|
||||
mUpdateSuggestionsTask = null;
|
||||
}
|
||||
if (mQuery == null) {
|
||||
setSuggestionsCursor(null);
|
||||
} else {
|
||||
setSuggestionsVisibility(true);
|
||||
mUpdateSuggestionsTask = new UpdateSuggestionsTask();
|
||||
mUpdateSuggestionsTask.execute(mQuery);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateSearchResults() {
|
||||
if (mUpdateSearchResultsTask != null) {
|
||||
mUpdateSearchResultsTask.cancel(false);
|
||||
mUpdateSearchResultsTask = null;
|
||||
}
|
||||
mQuery = getFilteredQueryString(cs);
|
||||
if (TextUtils.isEmpty(mQuery)) {
|
||||
setCursor(null);
|
||||
setResultsVisibility(false);
|
||||
setResultsCursor(null);
|
||||
} else {
|
||||
setResultsVisibility(true);
|
||||
mUpdateSearchResultsTask = new UpdateSearchResultsTask();
|
||||
mUpdateSearchResultsTask.execute(mQuery);
|
||||
}
|
||||
}
|
||||
|
||||
private static class SuggestionItem {
|
||||
public String query;
|
||||
|
||||
public SuggestionItem(String query) {
|
||||
this.query = query;
|
||||
}
|
||||
}
|
||||
|
||||
private static class SuggestionsAdapter extends BaseAdapter {
|
||||
|
||||
private static final int COLUMN_SUGGESTION_QUERY = 0;
|
||||
private static final int COLUMN_SUGGESTION_TIMESTAMP = 1;
|
||||
|
||||
private Context mContext;
|
||||
private Cursor mCursor;
|
||||
private LayoutInflater mInflater;
|
||||
private boolean mDataValid = false;
|
||||
|
||||
public SuggestionsAdapter(Context context) {
|
||||
mContext = context;
|
||||
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
mDataValid = false;
|
||||
}
|
||||
|
||||
public Cursor swapCursor(Cursor newCursor) {
|
||||
if (newCursor == mCursor) {
|
||||
return null;
|
||||
}
|
||||
Cursor oldCursor = mCursor;
|
||||
mCursor = newCursor;
|
||||
if (newCursor != null) {
|
||||
mDataValid = true;
|
||||
notifyDataSetChanged();
|
||||
} else {
|
||||
mDataValid = false;
|
||||
notifyDataSetInvalidated();
|
||||
}
|
||||
return oldCursor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
if (!mDataValid || mCursor == null || mCursor.isClosed()) return 0;
|
||||
return mCursor.getCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int position) {
|
||||
if (mDataValid && mCursor.moveToPosition(position)) {
|
||||
final String query = mCursor.getString(COLUMN_SUGGESTION_QUERY);
|
||||
|
||||
return new SuggestionItem(query);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
if (!mDataValid && convertView == null) {
|
||||
throw new IllegalStateException(
|
||||
"this should only be called when the cursor is valid");
|
||||
}
|
||||
if (!mCursor.moveToPosition(position)) {
|
||||
throw new IllegalStateException("couldn't move cursor to position " + position);
|
||||
}
|
||||
|
||||
View view;
|
||||
|
||||
if (convertView == null) {
|
||||
view = mInflater.inflate(R.layout.search_suggestion_item, parent, false);
|
||||
} else {
|
||||
view = convertView;
|
||||
}
|
||||
|
||||
TextView query = (TextView) view.findViewById(R.id.title);
|
||||
|
||||
SuggestionItem item = (SuggestionItem) getItem(position);
|
||||
query.setText(item.query);
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
private static class SearchResult {
|
||||
public Context context;
|
||||
public String title;
|
||||
@@ -288,10 +447,10 @@ public class SearchResultsSummary extends Fragment {
|
||||
|
||||
private static class SearchResultsAdapter extends BaseAdapter {
|
||||
|
||||
private Context mContext;
|
||||
private Cursor mCursor;
|
||||
private LayoutInflater mInflater;
|
||||
private boolean mDataValid;
|
||||
private Context mContext;
|
||||
private HashMap<String, Context> mContextMap = new HashMap<String, Context>();
|
||||
|
||||
private static final String PERCENT_RECLACE = "%s";
|
||||
@@ -299,7 +458,7 @@ public class SearchResultsSummary extends Fragment {
|
||||
|
||||
public SearchResultsAdapter(Context context) {
|
||||
mContext = context;
|
||||
mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
mDataValid = false;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user