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.Cursor; 21 import android.database.sqlite.SQLiteDatabase; 22 import android.support.annotation.VisibleForTesting; 23 24 import com.android.settings.search.IndexDatabaseHelper; 25 import com.android.settings.search.IndexDatabaseHelper.SavedQueriesColumns; 26 import com.android.settings.utils.AsyncLoader; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 /** 32 * Loader for recently searched queries. 33 */ 34 public class SavedQueryLoader extends AsyncLoader<List<? extends SearchResult>> { 35 36 // Max number of proposed suggestions 37 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) 38 static final int MAX_PROPOSED_SUGGESTIONS = 5; 39 40 private final SQLiteDatabase mDatabase; 41 SavedQueryLoader(Context context)42 public SavedQueryLoader(Context context) { 43 super(context); 44 mDatabase = IndexDatabaseHelper.getInstance(context).getReadableDatabase(); 45 } 46 47 @Override onDiscardResult(List<? extends SearchResult> result)48 protected void onDiscardResult(List<? extends SearchResult> result) { 49 50 } 51 52 @Override loadInBackground()53 public List<? extends SearchResult> loadInBackground() { 54 Cursor cursor = mDatabase.query(IndexDatabaseHelper.Tables.TABLE_SAVED_QUERIES /* table */, 55 new String[]{SavedQueriesColumns.QUERY} /* columns */, 56 null /* selection */, 57 null /* selectionArgs */, 58 null /* groupBy */, 59 null /* having */, 60 "rowId DESC" /* orderBy */, 61 String.valueOf(MAX_PROPOSED_SUGGESTIONS) /* limit */); 62 return convertCursorToResult(cursor); 63 } 64 convertCursorToResult(Cursor cursor)65 private List<SearchResult> convertCursorToResult(Cursor cursor) { 66 final List<SearchResult> results = new ArrayList<>(); 67 while (cursor.moveToNext()) { 68 final SavedQueryPayload payload = new SavedQueryPayload( 69 cursor.getString(cursor.getColumnIndex(SavedQueriesColumns.QUERY))); 70 results.add(new SearchResult.Builder() 71 .addTitle(payload.query) 72 .addPayload(payload) 73 .build()); 74 } 75 return results; 76 } 77 } 78