Save Search queries
- update database model for adding a table for saving a query and its timestamp - update database version - save the query when the User tap on a Search result - remove old queries (no more than 5 are saved) Change-Id: I4ddff0ad660944c7fd53be64ac95397850dc60d0
This commit is contained in:
@@ -18,14 +18,17 @@ package com.android.settings.dashboard;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.content.ComponentName;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Resources;
|
||||
import android.database.Cursor;
|
||||
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;
|
||||
@@ -39,9 +42,14 @@ import android.widget.TextView;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.search.Index;
|
||||
import com.android.settings.search.IndexDatabaseHelper;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static com.android.settings.search.IndexDatabaseHelper.SavedQueriesColums;
|
||||
import static com.android.settings.search.IndexDatabaseHelper.Tables;
|
||||
|
||||
public class SearchResultsSummary extends Fragment {
|
||||
|
||||
private static final String LOG_TAG = "SearchResultsSummary";
|
||||
@@ -53,6 +61,10 @@ public class SearchResultsSummary extends Fragment {
|
||||
private SearchResultsAdapter mAdapter;
|
||||
private UpdateSearchResultsTask mUpdateSearchResultsTask;
|
||||
|
||||
private String mQuery;
|
||||
private SaveSearchQueryTask mSaveSearchQueryTask;
|
||||
|
||||
private static long MAX_SAVED_SEARCH_QUERY = 5;
|
||||
|
||||
/**
|
||||
* A basic AsyncTask for updating the query results cursor
|
||||
@@ -73,6 +85,41 @@ public class SearchResultsSummary extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A basic AsynTask for saving the Search query into the database
|
||||
*/
|
||||
private class SaveSearchQueryTask extends AsyncTask<String, Void, Long> {
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
return lastInsertedRowId;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -140,12 +187,25 @@ public class SearchResultsSummary extends Fragment {
|
||||
|
||||
sa.startActivity(intent);
|
||||
}
|
||||
|
||||
saveQueryToDatabase();
|
||||
}
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void saveQueryToDatabase() {
|
||||
if (mSaveSearchQueryTask != null) {
|
||||
mSaveSearchQueryTask.cancel(false);
|
||||
mSaveSearchQueryTask = null;
|
||||
}
|
||||
if (!TextUtils.isEmpty(mQuery)) {
|
||||
mSaveSearchQueryTask = new SaveSearchQueryTask();
|
||||
mSaveSearchQueryTask.execute(mQuery);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean onQueryTextSubmit(String query) {
|
||||
updateSearchResults(query);
|
||||
return true;
|
||||
@@ -196,12 +256,12 @@ public class SearchResultsSummary extends Fragment {
|
||||
mUpdateSearchResultsTask.cancel(false);
|
||||
mUpdateSearchResultsTask = null;
|
||||
}
|
||||
final String query = getFilteredQueryString(cs);
|
||||
if (TextUtils.isEmpty(query)) {
|
||||
mQuery = getFilteredQueryString(cs);
|
||||
if (TextUtils.isEmpty(mQuery)) {
|
||||
setCursor(null);
|
||||
} else {
|
||||
mUpdateSearchResultsTask = new UpdateSearchResultsTask();
|
||||
mUpdateSearchResultsTask.execute(query);
|
||||
mUpdateSearchResultsTask.execute(mQuery);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user