Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 50 additions & 44 deletions app/src/main/java/me/bmax/apatch/ui/viewmodel/SuperUserViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ class SuperUserViewModel : ViewModel() {
else -> 2
}
}.then(compareBy(Collator.getInstance(Locale.getDefault()), AppInfo::label))
apps.sortedWith(comparator).also {
isRefreshing = false
}
apps.sortedWith(comparator)
}

val appList by derivedStateOf {
Expand Down Expand Up @@ -115,52 +113,60 @@ class SuperUserViewModel : ViewModel() {
suspend fun fetchAppList() {
isRefreshing = true

val result = connectRootService {
Log.w(TAG, "RootService disconnected")
}
try {
val result = connectRootService {
Log.w(TAG, "RootService disconnected")
}

withContext(Dispatchers.IO) {
val binder = result.first
val allPackages = IAPRootService.Stub.asInterface(binder).getPackages(0)
withContext(Dispatchers.IO) {
val binder = result.first
val allPackages = IAPRootService.Stub.asInterface(binder).getPackages(0)

withContext(Dispatchers.Main) {
stopRootService()
}
val uids = Natives.suUids().toList()
Log.d(TAG, "all allows: $uids")

var configs: HashMap<Int, PkgConfig.Config> = HashMap()
thread {
Natives.su()
configs = PkgConfig.readConfigs()
}.join()

Log.d(TAG, "all configs: $configs")

val newApps = allPackages.list.map {
val appInfo = it.applicationInfo
val uid = appInfo!!.uid
val actProfile = if (uids.contains(uid)) Natives.suProfile(uid) else null
val config = configs.getOrDefault(
uid, PkgConfig.Config(appInfo.packageName, Natives.isUidExcluded(uid), 0, Natives.Profile(uid = uid))
)
config.allow = 0

// from kernel
if (actProfile != null) {
config.allow = 1
config.profile = actProfile
withContext(Dispatchers.Main) {
stopRootService()
}
val uids = Natives.suUids().toList()
Log.d(TAG, "all allows: $uids")

var configs: HashMap<Int, PkgConfig.Config> = HashMap()
thread {
Natives.su()
configs = PkgConfig.readConfigs()
}.join()

Log.d(TAG, "all configs: $configs")

val newApps = allPackages.list.map {
val appInfo = it.applicationInfo
val uid = appInfo!!.uid
val actProfile = if (uids.contains(uid)) Natives.suProfile(uid) else null
val config = configs.getOrDefault(
uid, PkgConfig.Config(appInfo.packageName, Natives.isUidExcluded(uid), 0, Natives.Profile(uid = uid))
)
config.allow = 0

// from kernel
if (actProfile != null) {
config.allow = 1
config.profile = actProfile
}
AppInfo(
label = appInfo.loadLabel(apApp.packageManager).toString(),
packageInfo = it,
config = config
)
}
AppInfo(
label = appInfo.loadLabel(apApp.packageManager).toString(),
packageInfo = it,
config = config
)
}

synchronized(appsLock) {
apps = newApps
withContext(Dispatchers.Main) {
synchronized(appsLock) {
apps = newApps
}
}
}
} catch (e: Exception) {
Log.e(TAG, "Failed to fetch app list", e)
} finally {
isRefreshing = false
}
}
}