Initial commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="de.mm20.launcher2.appsearch">
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,69 @@
|
||||
package de.mm20.launcher2.appsearch
|
||||
|
||||
import android.app.appsearch.AppSearchManager
|
||||
import android.app.appsearch.GlobalSearchSession
|
||||
import android.app.appsearch.SearchResult
|
||||
import android.app.appsearch.SearchSpec
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.MediatorLiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import de.mm20.launcher2.hiddenitems.HiddenItemsRepository
|
||||
import de.mm20.launcher2.ktx.isAtLeastApiLevel
|
||||
import de.mm20.launcher2.search.BaseSearchableRepository
|
||||
import de.mm20.launcher2.search.data.AppSearchResult
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.concurrent.Executors
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
class AppSearchRepository private constructor(val context: Context) : BaseSearchableRepository() {
|
||||
|
||||
private var session: GlobalSearchSession? = null
|
||||
|
||||
val appSearchResults = MediatorLiveData<List<AppSearchResult>?>()
|
||||
|
||||
private val allAppSearchResults = MutableLiveData<List<AppSearchResult>?>(emptyList())
|
||||
private val hiddenItemKeys = HiddenItemsRepository.getInstance(context).hiddenItemsKeys
|
||||
|
||||
init {
|
||||
appSearchResults.addSource(hiddenItemKeys) { keys ->
|
||||
appSearchResults.value = allAppSearchResults.value?.filter { !keys.contains(it.key) }
|
||||
}
|
||||
appSearchResults.addSource(allAppSearchResults) { c ->
|
||||
appSearchResults.value = c?.filter { hiddenItemKeys.value?.contains(it.key) != true }
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun search(query: String) {
|
||||
val results = emptyList<AppSearchResult>()
|
||||
if (!isAtLeastApiLevel(31)) return
|
||||
val executor = Executors.newSingleThreadExecutor()
|
||||
if (session == null) {
|
||||
val appSearchManager = context.getSystemService(AppSearchManager::class.java)
|
||||
session = suspendCoroutine { cont ->
|
||||
appSearchManager.createGlobalSearchSession(executor) {
|
||||
cont.resumeWith(Result.success(it.resultValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
val searchResults = session?.search(query, SearchSpec.Builder().build())
|
||||
val page = suspendCoroutine<List<SearchResult>?> { cont ->
|
||||
searchResults?.getNextPage(executor) {
|
||||
cont.resumeWith(Result.success(it.resultValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
allAppSearchResults.value = results
|
||||
}
|
||||
|
||||
companion object {
|
||||
private lateinit var instance: AppSearchRepository
|
||||
fun getInstance(context: Context): AppSearchRepository {
|
||||
if (!::instance.isInitialized) instance = AppSearchRepository(context.applicationContext)
|
||||
return instance
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package de.mm20.launcher2.appsearch
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import de.mm20.launcher2.search.data.AppSearchResult
|
||||
|
||||
class AppSearchViewModel(app: Application) : AndroidViewModel(app) {
|
||||
private val repository = AppSearchRepository.getInstance(app)
|
||||
private val appSearch: LiveData<List<AppSearchResult>?> = repository.appSearchResults
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.mm20.launcher2.search.data
|
||||
|
||||
import android.content.Context
|
||||
import de.mm20.launcher2.icons.LauncherIcon
|
||||
|
||||
class AppSearchResult: Searchable() {
|
||||
override val key: String
|
||||
get() = TODO("Not yet implemented")
|
||||
override val label: String
|
||||
get() = TODO("Not yet implemented")
|
||||
|
||||
override fun getPlaceholderIcon(context: Context): LauncherIcon {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user