From 228fae84ded7c02112e3edfea350d3d913743e51 Mon Sep 17 00:00:00 2001 From: SuperDragonXD <70206496+SuperDragonXD@users.noreply.github.com> Date: Thu, 24 Jul 2025 15:42:38 +0800 Subject: [PATCH] feat(search): Implement and encapsulate HistorySearchProvider --- .../engine/provider/HistorySearchProvider.kt | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 lawnchair/src/app/lawnchair/search/engine/provider/HistorySearchProvider.kt diff --git a/lawnchair/src/app/lawnchair/search/engine/provider/HistorySearchProvider.kt b/lawnchair/src/app/lawnchair/search/engine/provider/HistorySearchProvider.kt new file mode 100644 index 0000000000..47cf8ec4e0 --- /dev/null +++ b/lawnchair/src/app/lawnchair/search/engine/provider/HistorySearchProvider.kt @@ -0,0 +1,56 @@ +package app.lawnchair.search.engine.provider + +import android.content.ContentResolver +import android.content.Context +import android.database.Cursor +import android.net.Uri +import android.util.Log +import androidx.core.net.toUri +import app.lawnchair.search.LawnchairRecentSuggestionProvider +import app.lawnchair.search.algorithms.data.RecentKeyword +import app.lawnchair.search.engine.SearchResult +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +object HistorySearchProvider { + /** + * Fetches recent search keywords from the suggestion provider. + * This is a suspend function that performs its work on the IO dispatcher. + * + * @param context The application context. + * @param maxResults The maximum number of keywords to return. + * @return A list of [SearchResult.History] items. + */ + suspend fun getRecentKeywords(context: Context, maxResults: Int): List { + return withContext(Dispatchers.IO) { + try { + val contentResolver: ContentResolver = context.contentResolver + val uri: Uri = "content://${LawnchairRecentSuggestionProvider.AUTHORITY}/suggestions".toUri() + + val cursor: Cursor? = contentResolver.query(uri, null, null, null, null) + val recentKeywords = mutableListOf() + + cursor?.use { + val columnCount = it.columnCount + while (it.moveToNext()) { + val recentKeywordData = mutableMapOf() + for (i in 0 until columnCount) { + val columnName = it.getColumnName(i) + val columnValue = it.getString(i) ?: "" + recentKeywordData[columnName] = columnValue + } + recentKeywords.add(RecentKeyword(recentKeywordData)) + } + } + + recentKeywords.asReversed() + .take(maxResults) + .map { SearchResult.History(data = it) } + + } catch (e: Exception) { + Log.e("HistorySearchProvider", "Error during recent keyword retrieval", e) + emptyList() + } + } + } +}