Compare commits
1 commit
45e443a622
...
6cdbcca741
Author | SHA1 | Date | |
---|---|---|---|
6cdbcca741 |
1 changed files with 26 additions and 23 deletions
|
@ -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)
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue