Skip to content

Commit

Permalink
"pidof" based activity check is kinda slow. Elevated pkgops access ca…
Browse files Browse the repository at this point in the history
…n use `runningAppProcesses` which gets us the same info, but faster.
  • Loading branch information
d4rken committed Sep 24, 2023
1 parent 6229ee0 commit 0ba0adf
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import eu.darken.rxshell.cmd.Cmd
import eu.darken.sdmse.common.debug.Bugs
import eu.darken.sdmse.common.debug.logging.Logging.Priority.ERROR
import eu.darken.sdmse.common.debug.logging.Logging.Priority.VERBOSE
import eu.darken.sdmse.common.debug.logging.asLog
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
import eu.darken.sdmse.common.ipc.IpcHostModule
Expand All @@ -33,6 +34,9 @@ class PkgOpsHost @Inject constructor(
private val pm: PackageManager
get() = context.packageManager

private val am: ActivityManager
get() = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager

override fun getUserNameForUID(uid: Int): String? = try {
libcoreTool.getNameForUid(uid)
} catch (e: Exception) {
Expand All @@ -48,10 +52,19 @@ class PkgOpsHost @Inject constructor(
}

override fun isRunning(packageName: String): Boolean = try {
val result = runBlocking {
sharedShell.useRes {
Cmd.builder("pidof $packageName").execute(it)
}.exitCode == Cmd.ExitCode.OK
val result = try {
val runningAppProcesses = am.runningAppProcesses
?.flatMap { it.pkgList.toList() }
?.distinct()
?: emptyList()
runningAppProcesses.any { it == packageName }
} catch (e: Exception) {
log(TAG, ERROR) { "isRunning($packageName): runningAppProcesses failed due to ${e.asLog()} " }
runBlocking {
sharedShell.useRes {
Cmd.builder("pidof $packageName").execute(it)
}.exitCode == Cmd.ExitCode.OK
}
}
log(TAG, VERBOSE) { "isRunning(packageName=$packageName)=$result" }
result
Expand Down

0 comments on commit 0ba0adf

Please sign in to comment.