From 90c6559e0f783883e0c0024a47d5a07e4d2dbc04 Mon Sep 17 00:00:00 2001 From: lisonge Date: Thu, 3 Oct 2024 21:49:40 +0800 Subject: [PATCH] perf: KClass componentName --- app/src/main/kotlin/li/songe/gkd/App.kt | 5 +++-- app/src/main/kotlin/li/songe/gkd/MainActivity.kt | 4 ++-- .../li/songe/gkd/debug/ScreenshotService.kt | 4 ++-- .../li/songe/gkd/service/GkdTileService.kt | 6 ++---- .../kotlin/li/songe/gkd/shizuku/ShizukuApi.kt | 16 +++++++--------- .../main/kotlin/li/songe/gkd/ui/AdvancedPage.kt | 2 +- app/src/main/kotlin/li/songe/gkd/util/Others.kt | 10 ++++++++++ 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/app/src/main/kotlin/li/songe/gkd/App.kt b/app/src/main/kotlin/li/songe/gkd/App.kt index c8d74bd..06f7845 100644 --- a/app/src/main/kotlin/li/songe/gkd/App.kt +++ b/app/src/main/kotlin/li/songe/gkd/App.kt @@ -24,6 +24,7 @@ import li.songe.gkd.notif.initChannel import li.songe.gkd.permission.shizukuOkState import li.songe.gkd.service.A11yService import li.songe.gkd.util.SafeR +import li.songe.gkd.util.componentName import li.songe.gkd.util.initAppState import li.songe.gkd.util.initFolder import li.songe.gkd.util.initStore @@ -56,7 +57,7 @@ data object META { val debuggable by lazy { applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0 } val versionCode by lazy { selfAppInfo.versionCode.toInt() } val versionName by lazy { selfAppInfo.versionName!! } - val appId by lazy { selfAppInfo.id } + val appId by lazy { app.packageName!! } val appName by lazy { app.getString(SafeR.app_name) } } @@ -163,7 +164,7 @@ private fun getA11yServiceEnabled(): Boolean { if (value.isNullOrEmpty()) return false val colonSplitter = TextUtils.SimpleStringSplitter(':') colonSplitter.setString(value) - val name = ComponentName(app, A11yService::class.java) + val name = A11yService::class.componentName while (colonSplitter.hasNext()) { if (ComponentName.unflattenFromString(colonSplitter.next()) == name) { return true diff --git a/app/src/main/kotlin/li/songe/gkd/MainActivity.kt b/app/src/main/kotlin/li/songe/gkd/MainActivity.kt index 4924e87..2be2f98 100644 --- a/app/src/main/kotlin/li/songe/gkd/MainActivity.kt +++ b/app/src/main/kotlin/li/songe/gkd/MainActivity.kt @@ -2,7 +2,6 @@ package li.songe.gkd import android.app.Activity import android.app.ActivityManager -import android.content.ComponentName import android.content.Context import android.content.Intent import android.graphics.Color @@ -48,6 +47,7 @@ import li.songe.gkd.ui.theme.AppTheme import li.songe.gkd.util.LocalNavController import li.songe.gkd.util.UpgradeDialog import li.songe.gkd.util.appInfoCacheFlow +import li.songe.gkd.util.componentName import li.songe.gkd.util.initFolder import li.songe.gkd.util.launchTry import li.songe.gkd.util.map @@ -136,7 +136,7 @@ fun isActivityVisible() = activityVisibleFlow.value > 0 fun Activity.navToMainActivity() { val intent = this.intent?.cloneFilter() if (intent != null) { - intent.component = ComponentName(this, MainActivity::class.java) + intent.component = MainActivity::class.componentName intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK intent.putExtra("source", this::class.qualifiedName) startActivity(intent) diff --git a/app/src/main/kotlin/li/songe/gkd/debug/ScreenshotService.kt b/app/src/main/kotlin/li/songe/gkd/debug/ScreenshotService.kt index 27756d3..a0d4298 100644 --- a/app/src/main/kotlin/li/songe/gkd/debug/ScreenshotService.kt +++ b/app/src/main/kotlin/li/songe/gkd/debug/ScreenshotService.kt @@ -2,7 +2,6 @@ package li.songe.gkd.debug import android.annotation.SuppressLint import android.app.Service -import android.content.ComponentName import android.content.Context import android.content.Intent import com.blankj.utilcode.util.LogUtils @@ -11,6 +10,7 @@ import li.songe.gkd.app import li.songe.gkd.notif.notifyService import li.songe.gkd.notif.screenshotNotif import li.songe.gkd.util.ScreenshotUtil +import li.songe.gkd.util.componentName class ScreenshotService : Service() { override fun onBind(intent: Intent?) = null @@ -47,7 +47,7 @@ class ScreenshotService : Service() { private var screenshotUtil: ScreenshotUtil? = null fun start(context: Context = app, intent: Intent) { - intent.component = ComponentName(context, ScreenshotService::class.java) + intent.component = ScreenshotService::class.componentName context.startForegroundService(intent) } diff --git a/app/src/main/kotlin/li/songe/gkd/service/GkdTileService.kt b/app/src/main/kotlin/li/songe/gkd/service/GkdTileService.kt index c3db991..20eefe5 100644 --- a/app/src/main/kotlin/li/songe/gkd/service/GkdTileService.kt +++ b/app/src/main/kotlin/li/songe/gkd/service/GkdTileService.kt @@ -1,6 +1,5 @@ package li.songe.gkd.service -import android.content.ComponentName import android.os.Handler import android.os.Looper import android.provider.Settings @@ -9,6 +8,7 @@ import android.service.quicksettings.TileService import kotlinx.coroutines.flow.update import li.songe.gkd.app import li.songe.gkd.permission.writeSecureSettingsState +import li.songe.gkd.util.componentName import li.songe.gkd.util.lastRestartA11yServiceTimeFlow import li.songe.gkd.util.storeFlow import li.songe.gkd.util.toast @@ -135,6 +135,4 @@ fun fixRestartService(): Boolean { return false } -private val a11yClsName by lazy { - ComponentName(app, A11yService::class.java).flattenToShortString() -} \ No newline at end of file +val a11yClsName by lazy { A11yService::class.componentName.flattenToShortString() } diff --git a/app/src/main/kotlin/li/songe/gkd/shizuku/ShizukuApi.kt b/app/src/main/kotlin/li/songe/gkd/shizuku/ShizukuApi.kt index 4fb49c1..5fcd323 100644 --- a/app/src/main/kotlin/li/songe/gkd/shizuku/ShizukuApi.kt +++ b/app/src/main/kotlin/li/songe/gkd/shizuku/ShizukuApi.kt @@ -18,11 +18,11 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import li.songe.gkd.META -import li.songe.gkd.app import li.songe.gkd.appScope import li.songe.gkd.data.DeviceInfo import li.songe.gkd.permission.shizukuOkState import li.songe.gkd.service.TopActivity +import li.songe.gkd.util.componentName import li.songe.gkd.util.json import li.songe.gkd.util.storeFlow import li.songe.gkd.util.toast @@ -168,14 +168,12 @@ data class UserServiceWrapper( } private suspend fun serviceWrapper(): UserServiceWrapper = suspendCoroutine { continuation -> - val serviceArgs = Shizuku.UserServiceArgs( - ComponentName( - app, - UserService::class.java - ) - ).daemon(false).processNameSuffix( - "service-for-${if (META.debuggable) "gkd-debug" else "gkd-release"}" - ).debuggable(META.debuggable).version(META.versionCode) + val serviceArgs = Shizuku + .UserServiceArgs(UserService::class.componentName) + .daemon(false) + .processNameSuffix("shizuku-user-service") + .debuggable(META.debuggable) + .version(META.versionCode) var resumeFc: ((UserServiceWrapper) -> Unit)? = { continuation.resume(it) } diff --git a/app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt b/app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt index 3164ced..81ce97d 100644 --- a/app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt +++ b/app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt @@ -393,7 +393,7 @@ fun AdvancedPage() { TextSwitch( title = "清除订阅", - subtitle = "当HTTP服务关闭时,删除内存订阅", + subtitle = "服务关闭时,删除内存订阅", checked = store.autoClearMemorySubs ) { storeFlow.value = store.copy( diff --git a/app/src/main/kotlin/li/songe/gkd/util/Others.kt b/app/src/main/kotlin/li/songe/gkd/util/Others.kt index 9b93376..ab2a67e 100644 --- a/app/src/main/kotlin/li/songe/gkd/util/Others.kt +++ b/app/src/main/kotlin/li/songe/gkd/util/Others.kt @@ -1,5 +1,15 @@ package li.songe.gkd.util +import android.content.ComponentName +import li.songe.gkd.META +import kotlin.reflect.KClass +import kotlin.reflect.jvm.jvmName + inline fun Iterable.mapHashCode(transform: (T) -> R): Int { return fold(0) { acc, t -> 31 * acc + transform(t).hashCode() } } + +private val componentNameCache by lazy { HashMap() } + +val KClass<*>.componentName + get() = componentNameCache.getOrPut(jvmName) { ComponentName(META.appId, jvmName) }