Ryan O'Leary | 9461eca | 2022-08-09 19:18:13 +0000 | [diff] [blame] | 1 | /* |
| 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'Leary | f29a666 | 2022-08-09 19:22:51 +0000 | [diff] [blame] | 16 | package com.android.quicksearchbox |
Ryan O'Leary | 9461eca | 2022-08-09 19:18:13 +0000 | [diff] [blame] | 17 | |
Ryan O'Leary | f29a666 | 2022-08-09 19:22:51 +0000 | [diff] [blame] | 18 | import android.os.Handler |
Ryan O'Leary | 481c23e | 2022-08-09 21:04:06 +0000 | [diff] [blame] | 19 | import android.util.Log |
Ryan O'Leary | f29a666 | 2022-08-09 19:22:51 +0000 | [diff] [blame] | 20 | import com.android.quicksearchbox.util.Consumer |
| 21 | import com.android.quicksearchbox.util.NamedTaskExecutor |
Ryan O'Leary | 481c23e | 2022-08-09 21:04:06 +0000 | [diff] [blame] | 22 | import com.android.quicksearchbox.util.NoOpConsumer |
Ryan O'Leary | 9461eca | 2022-08-09 19:18:13 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Suggestions provider implementation. |
| 26 | * |
Ryan O'Leary | 2860bfe | 2022-09-12 00:15:50 +0000 | [diff] [blame] | 27 | * 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'Leary | 9461eca | 2022-08-09 19:18:13 +0000 | [diff] [blame] | 29 | */ |
Ryan O'Leary | f29a666 | 2022-08-09 19:22:51 +0000 | [diff] [blame] | 30 | class SuggestionsProviderImpl( |
Ryan O'Leary | 2860bfe | 2022-09-12 00:15:50 +0000 | [diff] [blame] | 31 | private val mConfig: Config, |
| 32 | private val mQueryExecutor: NamedTaskExecutor, |
| 33 | publishThread: Handler?, |
| 34 | logger: Logger? |
Ryan O'Leary | f29a666 | 2022-08-09 19:22:51 +0000 | [diff] [blame] | 35 | ) : SuggestionsProvider { |
Ryan O'Leary | 481c23e | 2022-08-09 21:04:06 +0000 | [diff] [blame] | 36 | |
Ryan O'Leary | 2860bfe | 2022-09-12 00:15:50 +0000 | [diff] [blame] | 37 | private val mPublishThread: Handler? |
Ryan O'Leary | 481c23e | 2022-08-09 21:04:06 +0000 | [diff] [blame] | 38 | |
Ryan O'Leary | 2860bfe | 2022-09-12 00:15:50 +0000 | [diff] [blame] | 39 | private val mLogger: Logger? |
Ryan O'Leary | 481c23e | 2022-08-09 21:04:06 +0000 | [diff] [blame] | 40 | |
Ryan O'Leary | 2860bfe | 2022-09-12 00:15:50 +0000 | [diff] [blame] | 41 | @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'Leary | f29a666 | 2022-08-09 19:22:51 +0000 | [diff] [blame] | 66 | @Override |
Ryan O'Leary | 2860bfe | 2022-09-12 00:15:50 +0000 | [diff] [blame] | 67 | 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'Leary | f29a666 | 2022-08-09 19:22:51 +0000 | [diff] [blame] | 77 | ) |
Ryan O'Leary | 2860bfe | 2022-09-12 00:15:50 +0000 | [diff] [blame] | 78 | } |
| 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'Leary | 9461eca | 2022-08-09 19:18:13 +0000 | [diff] [blame] | 86 | } |
Ryan O'Leary | 2860bfe | 2022-09-12 00:15:50 +0000 | [diff] [blame] | 87 | } |
Ryan O'Leary | 9461eca | 2022-08-09 19:18:13 +0000 | [diff] [blame] | 88 | |
Ryan O'Leary | 2860bfe | 2022-09-12 00:15:50 +0000 | [diff] [blame] | 89 | companion object { |
| 90 | private const val DBG = false |
| 91 | private const val TAG = "QSB.SuggestionsProviderImpl" |
| 92 | } |
Ryan O'Leary | 9461eca | 2022-08-09 19:18:13 +0000 | [diff] [blame] | 93 | |
Ryan O'Leary | 2860bfe | 2022-09-12 00:15:50 +0000 | [diff] [blame] | 94 | init { |
| 95 | mPublishThread = publishThread |
| 96 | mLogger = logger |
| 97 | } |
| 98 | } |