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)
|
super.onCreateOptionsMenu(menu, inflater)
|
||||||
inflater.inflate(R.menu.fragment_sim_toolkit, menu)
|
inflater.inflate(R.menu.fragment_sim_toolkit, menu)
|
||||||
menu.findItem(R.id.open_sim_toolkit).apply {
|
menu.findItem(R.id.open_sim_toolkit).apply {
|
||||||
isVisible = stk.isAvailable(slotId)
|
val slot = stk[slotId]
|
||||||
setOnMenuItemClickListener { stk.launch(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.ComponentName
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.ActivityInfo
|
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
|
@ -21,36 +20,37 @@ class SIMToolkit(private val context: Context) {
|
||||||
put(1, getComponentNames(R.array.sim_toolkit_slot_2))
|
put(1, getComponentNames(R.array.sim_toolkit_slot_2))
|
||||||
}
|
}
|
||||||
|
|
||||||
private val packageNames: Iterable<String>
|
operator fun get(slotId: Int): Slot? = when (slotId) {
|
||||||
get() = slots.values.flatten().map { it.packageName }.toSet()
|
-1, EuiccChannelManager.USB_CHANNEL_ID -> null
|
||||||
|
else -> Slot(context, buildSet {
|
||||||
|
addAll(slots.getOrDefault(slotId, emptySet()))
|
||||||
|
addAll(slots.getOrDefault(-1, emptySet()))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
private val activities: Iterable<ComponentName>
|
data class Slot(private val context: Context, private val components: Set<ComponentName>) {
|
||||||
get() = packageNames.flatMap(context.packageManager::getActivities)
|
private val packageNames: Iterable<String>
|
||||||
.filter { it.exported }
|
get() = components.map { it.packageName }.toSet()
|
||||||
.map { ComponentName(it.packageName, it.name) }
|
|
||||||
|
|
||||||
private val launchIntent: Intent?
|
private val launchIntent: Intent?
|
||||||
get() = packageNames.firstNotNullOfOrNull(context.packageManager::getLaunchIntent)
|
get() = packageNames.firstNotNullOfOrNull(context.packageManager::getLaunchIntent)
|
||||||
|
|
||||||
private fun getComponentsBySlotId(slotId: Int) = buildSet {
|
private val activities: Iterable<ComponentName>
|
||||||
addAll(slots.getOrDefault(slotId, emptySet()))
|
get() = packageNames.flatMap(context.packageManager::getActivities)
|
||||||
addAll(slots.getOrDefault(-1, emptySet()))
|
.filter { it.exported }.map { ComponentName(it.packageName, it.name) }
|
||||||
}
|
|
||||||
|
|
||||||
private fun intentActivity(slotId: Int): Intent? {
|
private fun getIntent(): Intent? {
|
||||||
val component = getComponentsBySlotId(slotId).find(activities::contains)
|
try {
|
||||||
?: return launchIntent
|
val component = components.find(activities::contains) ?: return launchIntent
|
||||||
val disabled = try {
|
if (isDisabledState(context.packageManager.getComponentEnabledSetting(component)))
|
||||||
isDisabledState(context.packageManager.getComponentEnabledSetting(component))
|
return null
|
||||||
} catch (e: IllegalArgumentException) {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
if (disabled) return null
|
|
||||||
return Intent.makeMainActivity(component)
|
return Intent.makeMainActivity(component)
|
||||||
|
} catch (e: IllegalArgumentException) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getDisabledPackageName(slotId: Int) =
|
private fun getDisabledPackageName() = packageNames.find {
|
||||||
getComponentsBySlotId(slotId).map { it.packageName }.toSet().find {
|
|
||||||
try {
|
try {
|
||||||
isDisabledState(context.packageManager.getApplicationEnabledSetting(it))
|
isDisabledState(context.packageManager.getApplicationEnabledSetting(it))
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
|
@ -58,16 +58,10 @@ class SIMToolkit(private val context: Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isAvailable(slotId: Int) = when (slotId) {
|
fun launch(): Boolean {
|
||||||
-1 -> false
|
var intent = getIntent()
|
||||||
EuiccChannelManager.USB_CHANNEL_ID -> false
|
|
||||||
else -> intentActivity(slotId) != null || getDisabledPackageName(slotId) != null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun launch(slotId: Int): Boolean {
|
|
||||||
var intent = intentActivity(slotId)
|
|
||||||
if (intent == null) {
|
if (intent == null) {
|
||||||
val pkgName = getDisabledPackageName(slotId) ?: return false
|
val pkgName = getDisabledPackageName() ?: return false
|
||||||
val message = context.getString(
|
val message = context.getString(
|
||||||
R.string.toast_prompt_to_enable_sim_toolkit,
|
R.string.toast_prompt_to_enable_sim_toolkit,
|
||||||
context.packageManager.getApplicationLabel(pkgName)
|
context.packageManager.getApplicationLabel(pkgName)
|
||||||
|
@ -82,6 +76,7 @@ class SIMToolkit(private val context: Context) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun isDisabledState(state: Int) = when (state) {
|
private fun isDisabledState(state: Int) = when (state) {
|
||||||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED -> true
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED -> true
|
||||||
|
@ -102,5 +97,5 @@ private fun PackageManager.getActivities(packageName: String) = try {
|
||||||
getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
|
getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
|
||||||
.activities?.toList() ?: emptyList()
|
.activities?.toList() ?: emptyList()
|
||||||
} catch (_: PackageManager.NameNotFoundException) {
|
} catch (_: PackageManager.NameNotFoundException) {
|
||||||
emptyList<ActivityInfo>()
|
emptyList()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue