Compare commits

...

1 commit

Author SHA1 Message Date
6cdbcca741
refactor: improve partner customization 2025-07-15 12:25:41 +08:00

View file

@ -9,40 +9,43 @@ import im.angry.openeuicc.BuildConfig
class Partner { class Partner {
companion object { companion object {
private val intent = Intent("com.google.android.euicc.action.PARTNER_CUSTOMIZATION") private const val ACTION = "com.google.android.euicc.action.PARTNER_CUSTOMIZATION"
fun getInstance(context: Context): Partner? { private val instances = mutableMapOf<String, Partner?>()
var flags = if (BuildConfig.DEBUG)
PackageManager.MATCH_UNINSTALLED_PACKAGES else private val packageFlags: Int
PackageManager.MATCH_SYSTEM_ONLY get() {
flags = flags or PackageManager.MATCH_DISABLED_COMPONENTS val flags = if (BuildConfig.DEBUG)
val apps = context.packageManager PackageManager.MATCH_UNINSTALLED_PACKAGES else
.queryBroadcastReceivers(intent, flags) PackageManager.MATCH_SYSTEM_ONLY
.mapNotNull { it.activityInfo?.applicationInfo } return flags or PackageManager.MATCH_DISABLED_COMPONENTS
for (app in apps) {
try {
val resources = context.packageManager.getResourcesForApplication(app)
return Partner(resources)
} catch (_: PackageManager.NameNotFoundException) {
continue
}
} }
return null
fun getInstance(context: Context, action: String = ACTION) = instances.getOrPut(action) {
context.packageManager
.queryBroadcastReceivers(Intent(action), packageFlags)
.mapNotNull { it.activityInfo?.applicationInfo }
.firstNotNullOfOrNull {
try {
context.packageManager.getResourcesForApplication(it)
} catch (_: PackageManager.NameNotFoundException) {
null
}
}
?.let(::Partner)
} }
} }
private val resources: Resources private val resources: Resources
private val ids = mutableMapOf<String, Int>()
private constructor(resources: Resources) { private constructor(resources: Resources) {
this.resources = resources this.resources = resources
} }
private fun getIdentifier(name: String) = ids.getOrPut(name) { private fun getIdentifier(name: String) =
resources.getIdentifier(name, null, null) resources.getIdentifier(name, null, null).takeIf { it != 0 }
}
fun getString(name: String) = getIdentifier(name).let(resources::getString) fun getString(name: String) = getIdentifier(name)?.let(resources::getString)
fun getBoolean(name: String) = getIdentifier(name).let(resources::getBoolean) fun getBoolean(name: String) = getIdentifier(name)?.let(resources::getBoolean)
} }