Home | History | Annotate | Download | only in search2
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.search2;
     18 
     19 import android.content.ContentValues;
     20 import android.content.Context;
     21 import android.database.sqlite.SQLiteDatabase;
     22 import android.database.sqlite.SQLiteException;
     23 import android.util.Log;
     24 
     25 import com.android.settings.search.IndexDatabaseHelper;
     26 import com.android.settings.utils.AsyncLoader;
     27 
     28 import static com.android.settings.search.IndexDatabaseHelper.Tables.TABLE_SAVED_QUERIES;
     29 
     30 /**
     31  * A background task to update saved queries.
     32  */
     33 public class SavedQueryRecorder extends AsyncLoader<Void> {
     34 
     35     private static final String LOG_TAG = "SavedQueryRecorder";
     36 
     37     // Max number of saved search queries (who will be used for proposing suggestions)
     38     private static long MAX_SAVED_SEARCH_QUERY = 64;
     39 
     40     private final String mQuery;
     41 
     42     public SavedQueryRecorder(Context context, String query) {
     43         super(context);
     44         mQuery = query;
     45     }
     46 
     47     @Override
     48     protected void onDiscardResult(Void result) {
     49 
     50     }
     51 
     52     @Override
     53     public Void loadInBackground() {
     54         final long now = System.currentTimeMillis();
     55 
     56         final ContentValues values = new ContentValues();
     57         values.put(IndexDatabaseHelper.SavedQueriesColumns.QUERY, mQuery);
     58         values.put(IndexDatabaseHelper.SavedQueriesColumns.TIME_STAMP, now);
     59 
     60         final SQLiteDatabase database = getWritableDatabase();
     61         if (database == null) {
     62             return null;
     63         }
     64 
     65         long lastInsertedRowId;
     66         try {
     67             // First, delete all saved queries that are the same
     68             database.delete(TABLE_SAVED_QUERIES,
     69                     IndexDatabaseHelper.SavedQueriesColumns.QUERY + " = ?",
     70                     new String[]{mQuery});
     71 
     72             // Second, insert the saved query
     73             lastInsertedRowId = database.insertOrThrow(TABLE_SAVED_QUERIES, null, values);
     74 
     75             // Last, remove "old" saved queries
     76             final long delta = lastInsertedRowId - MAX_SAVED_SEARCH_QUERY;
     77             if (delta > 0) {
     78                 int count = database.delete(TABLE_SAVED_QUERIES,
     79                         "rowId <= ?",
     80                         new String[]{Long.toString(delta)});
     81                 Log.d(LOG_TAG, "Deleted '" + count + "' saved Search query(ies)");
     82             }
     83         } catch (Exception e) {
     84             Log.d(LOG_TAG, "Cannot update saved Search queries", e);
     85         }
     86         return null;
     87     }
     88 
     89     private SQLiteDatabase getWritableDatabase() {
     90         try {
     91             return IndexDatabaseHelper.getInstance(getContext()).getWritableDatabase();
     92         } catch (SQLiteException e) {
     93             Log.e(LOG_TAG, "Cannot open writable database", e);
     94             return null;
     95         }
     96     }
     97 }
     98