@@ -7,6 +7,7 @@ import android.content.pm.ApplicationInfo
77import android.content.pm.PackageManager
88import android.os.Build
99import com.appcontrolx.model.AppInfo
10+ import com.topjohnwu.superuser.Shell
1011
1112class 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