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