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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -28,11 +28,12 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
|
||||
private static final String TAG = "IndexDatabaseHelper";
|
||||
|
||||
private static final String DATABASE_NAME = "search_index.db";
|
||||
private static final int DATABASE_VERSION = 111;
|
||||
private static final int DATABASE_VERSION = 112;
|
||||
|
||||
public interface Tables {
|
||||
public static final String TABLE_PREFS_INDEX = "prefs_index";
|
||||
public static final String TABLE_META_INDEX = "meta_index";
|
||||
public static final String TABLE_SAVED_QUERIES = "saved_queries";
|
||||
}
|
||||
|
||||
public interface IndexColumns {
|
||||
@@ -61,6 +62,11 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
|
||||
public static final String BUILD = "build";
|
||||
}
|
||||
|
||||
public interface SavedQueriesColums {
|
||||
public static final String QUERY = "query";
|
||||
public static final String TIME_STAMP = "timestamp";
|
||||
}
|
||||
|
||||
private static final String CREATE_INDEX_TABLE =
|
||||
"CREATE VIRTUAL TABLE " + Tables.TABLE_PREFS_INDEX + " USING fts4" +
|
||||
"(" +
|
||||
@@ -107,6 +113,14 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
|
||||
MetaColumns.BUILD + " VARCHAR(32) NOT NULL" +
|
||||
")";
|
||||
|
||||
private static final String CREATE_SAVED_QUERIES_TABLE =
|
||||
"CREATE TABLE " + Tables.TABLE_SAVED_QUERIES +
|
||||
"(" +
|
||||
SavedQueriesColums.QUERY + " VARCHAR(64) NOT NULL" +
|
||||
", " +
|
||||
SavedQueriesColums.TIME_STAMP + " INTEGER" +
|
||||
")";
|
||||
|
||||
private static final String INSERT_BUILD_VERSION =
|
||||
"INSERT INTO " + Tables.TABLE_META_INDEX +
|
||||
" VALUES ('" + Build.VERSION.INCREMENTAL + "');";
|
||||
@@ -135,6 +149,7 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
|
||||
private void bootstrapDB(SQLiteDatabase db) {
|
||||
db.execSQL(CREATE_INDEX_TABLE);
|
||||
db.execSQL(CREATE_META_TABLE);
|
||||
db.execSQL(CREATE_SAVED_QUERIES_TABLE);
|
||||
db.execSQL(INSERT_BUILD_VERSION);
|
||||
Log.i(TAG, "Bootstrapped database");
|
||||
}
|
||||
@@ -200,5 +215,6 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
|
||||
private void dropTables(SQLiteDatabase db) {
|
||||
db.execSQL("DROP TABLE IF EXISTS " + Tables.TABLE_META_INDEX);
|
||||
db.execSQL("DROP TABLE IF EXISTS " + Tables.TABLE_PREFS_INDEX);
|
||||
db.execSQL("DROP TABLE IF EXISTS " + Tables.TABLE_SAVED_QUERIES);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user