Compare commits

..

1 commit

Author SHA1 Message Date
9673de4d07
refactor: sim toolkit 2025-03-05 19:32:54 +08:00
3 changed files with 38 additions and 33 deletions

View file

@ -1,7 +1,12 @@
package im.angry.openeuicc.ui
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.provider.Settings
import android.view.Menu
import android.view.MenuInflater
import android.widget.Toast
import im.angry.easyeuicc.R
import im.angry.openeuicc.util.SIMToolkit
import im.angry.openeuicc.util.newInstanceEuicc
@ -25,8 +30,22 @@ class UnprivilegedEuiccManagementFragment : EuiccManagementFragment() {
inflater.inflate(R.menu.fragment_sim_toolkit, menu)
menu.findItem(R.id.open_sim_toolkit).apply {
val slot = stk[slotId] ?: return@apply
isVisible = slot.available
setOnMenuItemClickListener { slot.launch() }
isVisible = slot.intent != null
setOnMenuItemClickListener {
val intent = slot.intent ?: return@setOnMenuItemClickListener false
if (intent.action == Settings.ACTION_APPLICATION_DETAILS_SETTINGS) {
val packageName = intent.data!!.schemeSpecificPart
val label = requireContext().packageManager.getApplicationLabel(packageName)
val message = requireContext().getString(R.string.toast_prompt_to_enable_sim_toolkit, label)
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
}
startActivity(intent)
true
}
}
}
}
}
private fun PackageManager.getApplicationLabel(packageName: String): CharSequence =
getApplicationLabel(getApplicationInfo(packageName, 0))

View file

@ -43,10 +43,7 @@ class SIMToolkit(private val context: Context) {
get() = packageNames.flatMap(packageManager::getActivities)
.filter(ActivityInfo::exported).map { ComponentName(it.packageName, it.name) }
val available: Boolean
get() = getIntent() != null || getDisabledPackageName() != null
private fun getIntent(): Intent? {
private fun getActivityIntent(): Intent? {
for (activity in activities) {
if (!components.contains(activity)) continue
if (isDisabledState(packageManager.getComponentEnabledSetting(activity))) continue
@ -55,31 +52,23 @@ class SIMToolkit(private val context: Context) {
return launchIntent
}
private fun getDisabledPackageName() = packageNames.find {
try {
isDisabledState(packageManager.getApplicationEnabledSetting(it))
} catch (_: IllegalArgumentException) {
false
private fun getDisabledPackageIntent(): Intent? {
val disabledPackageName = packageNames.find {
try {
isDisabledState(packageManager.getApplicationEnabledSetting(it))
} catch (_: IllegalArgumentException) {
false
}
}
if (disabledPackageName == null) return null
return Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", disabledPackageName, null)
)
}
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,
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
}
val intent: Intent?
get() = getActivityIntent() ?: getDisabledPackageIntent()
}
}
@ -89,9 +78,6 @@ private fun isDisabledState(state: Int) = when (state) {
else -> false
}
private fun PackageManager.getApplicationLabel(packageName: String): CharSequence =
getApplicationLabel(getApplicationInfo(packageName, 0))
private fun PackageManager.getLaunchIntent(packageName: String) = try {
getLaunchIntentForPackage(packageName)
} catch (_: PackageManager.NameNotFoundException) {

View file

@ -9,7 +9,7 @@
<!-- Toast -->
<string name="toast_ara_m_copied">ARA-M SHA-1 copied to clipboard</string>
<string name="toast_prompt_to_enable_sim_toolkit">Please ENABLE your \&quot;%s\&quot; application</string>
<string name="toast_prompt_to_enable_sim_toolkit">Please ENABLE your \"%s\" application</string>
<!-- Compatibility Check Descriptions -->
<string name="compatibility_check_system_features">System Features</string>