Skip to content

Commit

Permalink
manager: Use uid as package specified identifier
Browse files Browse the repository at this point in the history
uid is special one but package name maybe multipled.
Fix multi work profile.

Signed-off-by: GarfieldHan <[email protected]>
  • Loading branch information
pomelohan committed May 30, 2024
1 parent fdcfd65 commit 504d20b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SuperUserViewModel : ViewModel() {
val uids = Natives.suUids().toList()
Log.d(TAG, "all allows: $uids")

var configs: HashMap<String, PkgConfig.Config> = HashMap()
var configs: HashMap<Int, PkgConfig.Config> = HashMap()
thread {
Natives.su()
configs = PkgConfig.readConfigs()
Expand All @@ -133,8 +133,7 @@ class SuperUserViewModel : ViewModel() {
val uid = appInfo.uid
val actProfile = if (uids.contains(uid)) Natives.suProfile(uid) else null
val config = configs.getOrDefault(
appInfo.packageName,
PkgConfig.Config(appInfo.packageName, 0, 0, Natives.Profile(uid))
appInfo.uid, PkgConfig.Config(appInfo.packageName, 0, 0, Natives.Profile(uid))
)
config.allow = 0

Expand Down
25 changes: 8 additions & 17 deletions app/src/main/java/me/bmax/apatch/util/PkgConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ object PkgConfig {
}
}

fun readConfigs(): HashMap<String, Config> {
val configs = HashMap<String, Config>()
fun readConfigs(): HashMap<Int, Config> {
val configs = HashMap<Int, Config>()
val file = File(APApplication.PACKAGE_CONFIG_FILE)
if (file.exists()) {
file.readLines().drop(1).filter { it.isNotEmpty() }.forEach {
Log.d(TAG, it)
val p = Config.fromLine(it)
if (!p.isDefault()) {
configs[p.pkg] = p
configs[p.profile.uid] = p
}
}
}
return configs
}

private fun writeConfigs(configs: HashMap<String, Config>) {
private fun writeConfigs(configs: HashMap<Int, Config>) {
val file = File(APApplication.PACKAGE_CONFIG_FILE)
if (!file.parentFile?.exists()!!) file.parentFile?.mkdirs()
val writer = FileWriter(file, false)
Expand All @@ -73,25 +73,16 @@ object PkgConfig {
synchronized(PkgConfig.javaClass) {
Natives.su()
val configs = readConfigs()
val pkg = config.pkg
val uid = config.profile.uid
// Root App should not be excluded
if (config.allow == 1) {
config.exclude = 0
}
// Make sure pkgName and uid matched
val filteredConfigs =
configs.filter { it.key == pkg && it.value.profile.uid == uid }
if (config.allow == 0 && configs[pkg] != null && config.exclude != 0) {
filteredConfigs.forEach {
Log.d(TAG, "remove config: $it")
configs.remove(it.key)
}
if (config.allow == 0 && configs[uid] != null && config.exclude != 0) {
configs.remove(uid)
} else {
filteredConfigs.forEach {
Log.d(TAG, "change config: $config")
configs[it.key] = config
}
Log.d(TAG, "change config: $config")
configs[config.profile.uid] = config
}
writeConfigs(configs)
}
Expand Down

0 comments on commit 504d20b

Please sign in to comment.