blob: 9c02c078f421aa891335f09a10813c60012c417f [file] [log] [blame]
Ryan O'Leary9461eca2022-08-09 19:18:13 +00001/*
2 * Copyright (C) 2022 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 */
Ryan O'Learyf29a6662022-08-09 19:22:51 +000016package com.android.quicksearchbox
Ryan O'Leary9461eca2022-08-09 19:18:13 +000017
Ryan O'Learyf29a6662022-08-09 19:22:51 +000018import android.os.Handler
Ryan O'Leary481c23e2022-08-09 21:04:06 +000019import android.util.Log
Ryan O'Learyf29a6662022-08-09 19:22:51 +000020import com.android.quicksearchbox.util.Consumer
21import com.android.quicksearchbox.util.NamedTaskExecutor
Ryan O'Leary481c23e2022-08-09 21:04:06 +000022import com.android.quicksearchbox.util.NoOpConsumer
Ryan O'Leary9461eca2022-08-09 19:18:13 +000023
24/**
25 * Suggestions provider implementation.
26 *
Ryan O'Leary2860bfe2022-09-12 00:15:50 +000027 * The provider will only handle a single query at a time. If a new query comes in, the old one is
28 * cancelled.
Ryan O'Leary9461eca2022-08-09 19:18:13 +000029 */
Ryan O'Learyf29a6662022-08-09 19:22:51 +000030class SuggestionsProviderImpl(
Ryan O'Leary2860bfe2022-09-12 00:15:50 +000031 private val mConfig: Config,
32 private val mQueryExecutor: NamedTaskExecutor,
33 publishThread: Handler?,
34 logger: Logger?
Ryan O'Learyf29a6662022-08-09 19:22:51 +000035) : SuggestionsProvider {
Ryan O'Leary481c23e2022-08-09 21:04:06 +000036
Ryan O'Leary2860bfe2022-09-12 00:15:50 +000037 private val mPublishThread: Handler?
Ryan O'Leary481c23e2022-08-09 21:04:06 +000038
Ryan O'Leary2860bfe2022-09-12 00:15:50 +000039 private val mLogger: Logger?
Ryan O'Leary481c23e2022-08-09 21:04:06 +000040
Ryan O'Leary2860bfe2022-09-12 00:15:50 +000041 @Override override fun close() {}
42
43 @Override
44 override fun getSuggestions(query: String, source: Source): Suggestions {
45 if (DBG) Log.d(TAG, "getSuggestions($query)")
46 val suggestions = Suggestions(query, source)
47 Log.i(TAG, "chars:" + query.length.toString() + ",source:" + source)
48 val receiver: Consumer<SourceResult?>
49 if (shouldDisplayResults(query)) {
50 receiver = SuggestionCursorReceiver(suggestions)
51 } else {
52 receiver = NoOpConsumer()
53 suggestions.done()
54 }
55 val maxResults: Int = mConfig.maxResultsPerSource
56 QueryTask.startQuery(query, maxResults, source, mQueryExecutor, mPublishThread, receiver)
57 return suggestions
58 }
59
60 private fun shouldDisplayResults(query: String): Boolean {
61 return !(query.isEmpty() && !mConfig.showSuggestionsForZeroQuery())
62 }
63
64 private inner class SuggestionCursorReceiver(private val mSuggestions: Suggestions) :
65 Consumer<SourceResult?> {
Ryan O'Learyf29a6662022-08-09 19:22:51 +000066 @Override
Ryan O'Leary2860bfe2022-09-12 00:15:50 +000067 override fun consume(value: SourceResult?): Boolean {
68 if (DBG) {
69 Log.d(
70 TAG,
71 "SuggestionCursorReceiver.consume(" +
72 value +
73 ") corpus=" +
74 value?.source +
75 " count = " +
76 value?.count
Ryan O'Learyf29a6662022-08-09 19:22:51 +000077 )
Ryan O'Leary2860bfe2022-09-12 00:15:50 +000078 }
79 // publish immediately
80 if (DBG) Log.d(TAG, "Publishing results")
81 mSuggestions.addResults(value)
82 if (value != null && mLogger != null) {
83 mLogger.logLatency(value)
84 }
85 return true
Ryan O'Leary9461eca2022-08-09 19:18:13 +000086 }
Ryan O'Leary2860bfe2022-09-12 00:15:50 +000087 }
Ryan O'Leary9461eca2022-08-09 19:18:13 +000088
Ryan O'Leary2860bfe2022-09-12 00:15:50 +000089 companion object {
90 private const val DBG = false
91 private const val TAG = "QSB.SuggestionsProviderImpl"
92 }
Ryan O'Leary9461eca2022-08-09 19:18:13 +000093
Ryan O'Leary2860bfe2022-09-12 00:15:50 +000094 init {
95 mPublishThread = publishThread
96 mLogger = logger
97 }
98}