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.Context; 20 import android.database.sqlite.SQLiteDatabase; 21 import android.database.sqlite.SQLiteException; 22 import android.util.Log; 23 24 import com.android.settings.search.IndexDatabaseHelper; 25 import com.android.settings.utils.AsyncLoader; 26 27 import static com.android.settings.search.IndexDatabaseHelper.Tables.TABLE_SAVED_QUERIES; 28 29 public class SavedQueryRemover extends AsyncLoader<Void> { 30 31 private static final String LOG_TAG = "SavedQueryRemover"; 32 33 private final String mQuery; 34 SavedQueryRemover(Context context, String query)35 public SavedQueryRemover(Context context, String query) { 36 super(context); 37 mQuery = query; 38 } 39 40 @Override loadInBackground()41 public Void loadInBackground() { 42 final SQLiteDatabase database = getWritableDatabase(); 43 try { 44 // First, delete all saved queries that are the same 45 database.delete(TABLE_SAVED_QUERIES, 46 IndexDatabaseHelper.SavedQueriesColumns.QUERY + " = ?", 47 new String[]{mQuery}); 48 } catch (Exception e) { 49 Log.d(LOG_TAG, "Cannot update saved Search queries", e); 50 } 51 return null; 52 } 53 54 @Override onDiscardResult(Void result)55 protected void onDiscardResult(Void result) { 56 57 } 58 getWritableDatabase()59 private SQLiteDatabase getWritableDatabase() { 60 try { 61 return IndexDatabaseHelper.getInstance(getContext()).getWritableDatabase(); 62 } catch (SQLiteException e) { 63 Log.e(LOG_TAG, "Cannot open writable database", e); 64 return null; 65 } 66 } 67 } 68