Skip to content

Commit d7e0d0c

Browse files
committed
fix: Improve running app detection with root ps command
1 parent d380265 commit d7e0d0c

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

app/src/main/java/com/appcontrolx/service/AppFetcher.kt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.content.pm.ApplicationInfo
77
import android.content.pm.PackageManager
88
import android.os.Build
99
import com.appcontrolx.model.AppInfo
10+
import com.topjohnwu.superuser.Shell
1011

1112
class AppFetcher(private val context: Context) {
1213

@@ -45,15 +46,36 @@ class AppFetcher(private val context: Context) {
4546
private fun getRunningPackages(): Set<String> {
4647
val running = mutableSetOf<String>()
4748

49+
// Method 1: Try root command first (most accurate)
50+
try {
51+
if (Shell.isAppGrantedRoot() == true) {
52+
val result = Shell.cmd("ps -A -o NAME").exec()
53+
if (result.isSuccess) {
54+
result.out.forEach { line ->
55+
val processName = line.trim()
56+
if (processName.isNotEmpty() && processName.contains(".")) {
57+
running.add(processName)
58+
}
59+
}
60+
}
61+
}
62+
} catch (e: Exception) {
63+
// Root not available, continue with other methods
64+
}
65+
66+
// Method 2: runningAppProcesses (limited on Android 10+, but still useful)
4867
try {
49-
// Method 1: runningAppProcesses (limited on Android 10+)
5068
activityManager.runningAppProcesses?.forEach { process ->
5169
process.pkgList?.forEach { pkg ->
5270
running.add(pkg)
5371
}
5472
}
55-
56-
// Method 2: getRunningServices (deprecated but still works for some cases)
73+
} catch (e: Exception) {
74+
// Ignore
75+
}
76+
77+
// Method 3: getRunningServices (deprecated but still works)
78+
try {
5779
@Suppress("DEPRECATION")
5880
activityManager.getRunningServices(Int.MAX_VALUE)?.forEach { service ->
5981
running.add(service.service.packageName)

0 commit comments

Comments
 (0)