Compare commits
1 commit
40d97f0291
...
d613ba403e
Author | SHA1 | Date | |
---|---|---|---|
d613ba403e |
2 changed files with 48 additions and 51 deletions
|
@ -24,8 +24,10 @@ class UnprivilegedEuiccManagementFragment : EuiccManagementFragment() {
|
|||
super.onCreateOptionsMenu(menu, inflater)
|
||||
inflater.inflate(R.menu.fragment_sim_toolkit, menu)
|
||||
menu.findItem(R.id.open_sim_toolkit).apply {
|
||||
isVisible = stk.isAvailable(slotId)
|
||||
setOnMenuItemClickListener { stk.launch(slotId) }
|
||||
val slot = stk[slotId]
|
||||
if (slot == null) return@apply
|
||||
isVisible = true
|
||||
setOnMenuItemClickListener { slot.launch() }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,7 +3,6 @@ package im.angry.openeuicc.util
|
|||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.provider.Settings
|
||||
|
@ -21,36 +20,37 @@ class SIMToolkit(private val context: Context) {
|
|||
put(1, getComponentNames(R.array.sim_toolkit_slot_2))
|
||||
}
|
||||
|
||||
private val packageNames: Iterable<String>
|
||||
get() = slots.values.flatten().map { it.packageName }.toSet()
|
||||
|
||||
private val activities: Iterable<ComponentName>
|
||||
get() = packageNames.flatMap(context.packageManager::getActivities)
|
||||
.filter { it.exported }
|
||||
.map { ComponentName(it.packageName, it.name) }
|
||||
|
||||
private val launchIntent: Intent?
|
||||
get() = packageNames.firstNotNullOfOrNull(context.packageManager::getLaunchIntent)
|
||||
|
||||
private fun getComponentsBySlotId(slotId: Int) = buildSet {
|
||||
addAll(slots.getOrDefault(slotId, emptySet()))
|
||||
addAll(slots.getOrDefault(-1, emptySet()))
|
||||
operator fun get(slotId: Int): Slot? = when (slotId) {
|
||||
-1, EuiccChannelManager.USB_CHANNEL_ID -> null
|
||||
else -> Slot(context, buildSet {
|
||||
addAll(slots.getOrDefault(slotId, emptySet()))
|
||||
addAll(slots.getOrDefault(-1, emptySet()))
|
||||
})
|
||||
}
|
||||
|
||||
private fun intentActivity(slotId: Int): Intent? {
|
||||
val component = getComponentsBySlotId(slotId).find(activities::contains)
|
||||
?: return launchIntent
|
||||
val disabled = try {
|
||||
isDisabledState(context.packageManager.getComponentEnabledSetting(component))
|
||||
} catch (e: IllegalArgumentException) {
|
||||
true
|
||||
data class Slot(private val context: Context, private val components: Set<ComponentName>) {
|
||||
private val packageNames: Iterable<String>
|
||||
get() = components.map { it.packageName }.toSet()
|
||||
|
||||
private val launchIntent: Intent?
|
||||
get() = packageNames.firstNotNullOfOrNull(context.packageManager::getLaunchIntent)
|
||||
|
||||
private val activities: Iterable<ComponentName>
|
||||
get() = packageNames.flatMap(context.packageManager::getActivities)
|
||||
.filter { it.exported }.map { ComponentName(it.packageName, it.name) }
|
||||
|
||||
private fun getIntent(): Intent? {
|
||||
try {
|
||||
val component = components.find(activities::contains) ?: return launchIntent
|
||||
if (isDisabledState(context.packageManager.getComponentEnabledSetting(component)))
|
||||
return null
|
||||
return Intent.makeMainActivity(component)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
if (disabled) return null
|
||||
return Intent.makeMainActivity(component)
|
||||
}
|
||||
|
||||
private fun getDisabledPackageName(slotId: Int) =
|
||||
getComponentsBySlotId(slotId).map { it.packageName }.toSet().find {
|
||||
private fun getDisabledPackageName() = packageNames.find {
|
||||
try {
|
||||
isDisabledState(context.packageManager.getApplicationEnabledSetting(it))
|
||||
} catch (e: IllegalArgumentException) {
|
||||
|
@ -58,28 +58,23 @@ class SIMToolkit(private val context: Context) {
|
|||
}
|
||||
}
|
||||
|
||||
fun isAvailable(slotId: Int) = when (slotId) {
|
||||
-1 -> false
|
||||
EuiccChannelManager.USB_CHANNEL_ID -> false
|
||||
else -> intentActivity(slotId) != null || getDisabledPackageName(slotId) != null
|
||||
}
|
||||
|
||||
fun launch(slotId: Int): Boolean {
|
||||
var intent = intentActivity(slotId)
|
||||
if (intent == null) {
|
||||
val pkgName = getDisabledPackageName(slotId) ?: return false
|
||||
val message = context.getString(
|
||||
R.string.toast_prompt_to_enable_sim_toolkit,
|
||||
context.packageManager.getApplicationLabel(pkgName)
|
||||
)
|
||||
intent = Intent(
|
||||
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
||||
Uri.fromParts("package", pkgName, null)
|
||||
)
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
|
||||
fun launch(): Boolean {
|
||||
var intent = getIntent()
|
||||
if (intent == null) {
|
||||
val pkgName = getDisabledPackageName() ?: return false
|
||||
val message = context.getString(
|
||||
R.string.toast_prompt_to_enable_sim_toolkit,
|
||||
context.packageManager.getApplicationLabel(pkgName)
|
||||
)
|
||||
intent = Intent(
|
||||
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
||||
Uri.fromParts("package", pkgName, null)
|
||||
)
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
context.startActivity(intent)
|
||||
return true
|
||||
}
|
||||
context.startActivity(intent)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,5 +97,5 @@ private fun PackageManager.getActivities(packageName: String) = try {
|
|||
getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
|
||||
.activities?.toList() ?: emptyList()
|
||||
} catch (_: PackageManager.NameNotFoundException) {
|
||||
emptyList<ActivityInfo>()
|
||||
emptyList()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue