Compare commits
7 commits
d613ba403e
...
40d97f0291
Author | SHA1 | Date | |
---|---|---|---|
40d97f0291 | |||
d9cbef886e | |||
cc5f798d3d | |||
e24523f3e2 | |||
e47eca289b | |||
698021c7f4 | |||
9873634de7 |
2 changed files with 51 additions and 48 deletions
|
@ -24,10 +24,8 @@ 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 {
|
||||||
val slot = stk[slotId]
|
isVisible = stk.isAvailable(slotId)
|
||||||
if (slot == null) return@apply
|
setOnMenuItemClickListener { stk.launch(slotId) }
|
||||||
isVisible = true
|
|
||||||
setOnMenuItemClickListener { slot.launch() }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@ 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
|
||||||
|
@ -20,37 +21,36 @@ class SIMToolkit(private val context: Context) {
|
||||||
put(1, getComponentNames(R.array.sim_toolkit_slot_2))
|
put(1, getComponentNames(R.array.sim_toolkit_slot_2))
|
||||||
}
|
}
|
||||||
|
|
||||||
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()))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
data class Slot(private val context: Context, private val components: Set<ComponentName>) {
|
|
||||||
private val packageNames: Iterable<String>
|
private val packageNames: Iterable<String>
|
||||||
get() = components.map { it.packageName }.toSet()
|
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?
|
private val launchIntent: Intent?
|
||||||
get() = packageNames.firstNotNullOfOrNull(context.packageManager::getLaunchIntent)
|
get() = packageNames.firstNotNullOfOrNull(context.packageManager::getLaunchIntent)
|
||||||
|
|
||||||
private val activities: Iterable<ComponentName>
|
private fun getComponentsBySlotId(slotId: Int) = buildSet {
|
||||||
get() = packageNames.flatMap(context.packageManager::getActivities)
|
addAll(slots.getOrDefault(slotId, emptySet()))
|
||||||
.filter { it.exported }.map { ComponentName(it.packageName, it.name) }
|
addAll(slots.getOrDefault(-1, emptySet()))
|
||||||
|
}
|
||||||
|
|
||||||
private fun getIntent(): Intent? {
|
private fun intentActivity(slotId: Int): Intent? {
|
||||||
try {
|
val component = getComponentsBySlotId(slotId).find(activities::contains)
|
||||||
val component = components.find(activities::contains) ?: return launchIntent
|
?: return launchIntent
|
||||||
if (isDisabledState(context.packageManager.getComponentEnabledSetting(component)))
|
val disabled = try {
|
||||||
return null
|
isDisabledState(context.packageManager.getComponentEnabledSetting(component))
|
||||||
return Intent.makeMainActivity(component)
|
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
return null
|
true
|
||||||
}
|
}
|
||||||
|
if (disabled) return null
|
||||||
|
return Intent.makeMainActivity(component)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getDisabledPackageName() = packageNames.find {
|
private fun getDisabledPackageName(slotId: Int) =
|
||||||
|
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,10 +58,16 @@ class SIMToolkit(private val context: Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun launch(): Boolean {
|
fun isAvailable(slotId: Int) = when (slotId) {
|
||||||
var intent = getIntent()
|
-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) {
|
if (intent == null) {
|
||||||
val pkgName = getDisabledPackageName() ?: return false
|
val pkgName = getDisabledPackageName(slotId) ?: 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)
|
||||||
|
@ -75,7 +81,6 @@ class SIMToolkit(private val context: Context) {
|
||||||
context.startActivity(intent)
|
context.startActivity(intent)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isDisabledState(state: Int) = when (state) {
|
private fun isDisabledState(state: Int) = when (state) {
|
||||||
|
@ -97,5 +102,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()
|
emptyList<ActivityInfo>()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue