Skip to content

Commit

Permalink
perf: KClass componentName
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Oct 3, 2024
1 parent 9b51845 commit 90c6559
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 20 deletions.
5 changes: 3 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/debug/ScreenshotService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 2 additions & 4 deletions app/src/main/kotlin/li/songe/gkd/service/GkdTileService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -135,6 +135,4 @@ fun fixRestartService(): Boolean {
return false
}

private val a11yClsName by lazy {
ComponentName(app, A11yService::class.java).flattenToShortString()
}
val a11yClsName by lazy { A11yService::class.componentName.flattenToShortString() }
16 changes: 7 additions & 9 deletions app/src/main/kotlin/li/songe/gkd/shizuku/ShizukuApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ fun AdvancedPage() {

TextSwitch(
title = "清除订阅",
subtitle = "当HTTP服务关闭时,删除内存订阅",
subtitle = "服务关闭时,删除内存订阅",
checked = store.autoClearMemorySubs
) {
storeFlow.value = store.copy(
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/util/Others.kt
Original file line number Diff line number Diff line change
@@ -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 <T, R> Iterable<T>.mapHashCode(transform: (T) -> R): Int {
return fold(0) { acc, t -> 31 * acc + transform(t).hashCode() }
}

private val componentNameCache by lazy { HashMap<String, ComponentName>() }

val KClass<*>.componentName
get() = componentNameCache.getOrPut(jvmName) { ComponentName(META.appId, jvmName) }

0 comments on commit 90c6559

Please sign in to comment.