Add Pinyin support to app search

Implement #8
This commit is contained in:
MM20 2022-03-05 13:54:07 +01:00
parent d2cdbbc7c4
commit b6f6c0c584
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
4 changed files with 28 additions and 4 deletions

View File

@ -44,6 +44,8 @@ dependencies {
implementation(libs.commons.text)
implementation(libs.tinypinyin)
implementation(project(":search"))
implementation(project(":base"))
implementation(project(":preferences"))

View File

@ -11,6 +11,7 @@ import android.os.Process
import android.os.UserHandle
import android.os.UserManager
import android.util.Log
import com.github.promeg.pinyinhelper.Pinyin
import de.mm20.launcher2.hiddenitems.HiddenItemsRepository
import de.mm20.launcher2.search.data.AppInstallation
import de.mm20.launcher2.search.data.Application
@ -189,18 +190,17 @@ internal class AppRepositoryImpl(
override fun search(query: String): Flow<List<Application>> = channelFlow {
merge(installedApps, hiddenItems, installations).collectLatest {
withContext(Dispatchers.IO) {
val fuzzyScore = FuzzyScore(Locale.getDefault())
withContext(Dispatchers.Default) {
val appResults = mutableListOf<Application>()
if (query.isEmpty()) {
appResults.addAll(installedApps.value)
appResults.addAll(installations.value)
} else {
appResults.addAll(installedApps.value.filter {
fuzzyScore.fuzzyScore(it.label, query) >= query.length * 1.5
matches(it.label, query)
})
appResults.addAll(installations.value.filter {
fuzzyScore.fuzzyScore(it.label, query) >= query.length * 1.5
matches(it.label, query)
})
}
@ -216,6 +216,17 @@ internal class AppRepositoryImpl(
}
}
private fun matches(label: String, query: String): Boolean {
val labelLatin = romanize(label)
val fuzzyScore = FuzzyScore(Locale.getDefault())
return fuzzyScore.fuzzyScore(label, query) >= query.length * 1.5 ||
fuzzyScore.fuzzyScore(labelLatin, query) >= query.length * 1.5
}
private fun romanize(label: String): String {
return Pinyin.toPinyin(label, "").lowercase(Locale.getDefault())
}
private fun getActivityByComponentName(componentName: ComponentName?): Application? {
componentName ?: return null
val intent = Intent().setComponent(componentName)

View File

@ -194,4 +194,11 @@ val OpenSourceLicenses = arrayOf(
licenseText = R.raw.license_apache_2,
url = "https://commons.apache.org/proper/commons-text/"
),
OpenSourceLibrary(
name = "TinyPinyin",
description = "A fast, low-memory Chinese character-to-pinyin library for Java and Android.",
licenseName = R.string.apache_license_name,
licenseText = R.raw.license_apache_2,
url = "https://github.com/promeG/TinyPinyin"
),
)

View File

@ -375,6 +375,10 @@ dependencyResolutionManagement {
alias("koin.androidxcompose")
.to("io.insert-koin", "koin-androidx-compose")
.versionRef("koin")
alias("tinypinyin")
.to("com.github.promeg", "tinypinyin")
.version("2.0.2")
}
}
}