Skip to content

Commit

Permalink
feat: improve omapi detection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
septs committed Oct 28, 2024
1 parent 73dd0b5 commit 55c5d87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/app/septs/euiccprobe/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ class MainActivity : AppCompatActivity() {
}
if (result.state == OpenMobileAPI.State.Available) {
for (slot in result.slots) {
val state = if (slot.value) "Available" else "Unavailable"
appendLine("- ${slot.key} Slot: $state")
appendLine("- ${slot.key} Slot: ${slot.value}")
}
}
}
Expand Down
29 changes: 21 additions & 8 deletions app/src/main/java/app/septs/euiccprobe/OpenMobileAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object OpenMobileAPI {
data class Result(
val backend: Backend,
val state: State,
val slots: Map<String, Boolean>
val slots: Map<String, SlotState>
)

enum class Backend {
Expand All @@ -32,6 +32,13 @@ object OpenMobileAPI {
Available,
}

enum class SlotState {
NotConnectable,
Connectable,
Available,
Unavailable,
}

enum class SEBypass {
Unavailable,
CannotBeBypassed,
Expand Down Expand Up @@ -92,19 +99,22 @@ object OpenMobileAPI {
return Result(Backend.Builtin, State.UnableToConnect, emptyMap())
}
val slots = buildMap {
var state: SlotState
for (reader in service.readers) {
if (!reader.name.startsWith("SIM")) continue
state = SlotState.Connectable
try {
val session = reader.openSession()
val channel = session.openLogicalChannel(ISD_R_APPLET_ID) ?: continue
put(normalizeName(reader.name), channel.isOpen)
state = if (channel.isOpen) SlotState.Available else SlotState.Unavailable
if (channel.isOpen) channel.close()
if (!session.isClosed) session.closeChannels()
} catch (_: SecurityException) {
put(normalizeName(reader.name), true)
state = SlotState.Available
} catch (e: Throwable) {
Log.e(javaClass.name, "${reader.name} = ${e.message}")
}
put(normalizeName(reader.name), state)
}
service.shutdown()
}
Expand All @@ -124,36 +134,39 @@ object OpenMobileAPI {
return Result(Backend.SIMAlliance, State.UnableToConnect, emptyMap())
}
val slots = buildMap {
var state: SlotState
for (reader in service.readers) {
if (!reader.name.startsWith("SIM")) continue
state = SlotState.Connectable
try {
val session = reader.openSession()
val channel = session.openLogicalChannel(ISD_R_APPLET_ID)
put(normalizeName(reader.name), !channel.isClosed)
state = if (channel.isClosed) SlotState.Unavailable else SlotState.Available
if (!channel.isClosed) channel.close()
if (!session.isClosed) session.closeChannels()
} catch (_: SecurityException) {
put(normalizeName(reader.name), true)
state = SlotState.Available
} catch (e: Throwable) {
Log.e(javaClass.name, "${reader.name} = ${e.message}")
}
put(normalizeName(reader.name), state)
}
service.shutdown()
}
val state = if (slots.isEmpty()) State.Unavailable else State.Available
return Result(Backend.SIMAlliance, state, slots)
}

private fun getCardSlots(context: Context): Map<String, Boolean> {
private fun getCardSlots(context: Context): Map<String, SlotState> {
val service = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
val count = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> service.activeModemCount
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> @Suppress("DEPRECATION") service.phoneCount
else -> return mapOf(Pair("SIM1", false))
else -> return mapOf(Pair("SIM1", SlotState.NotConnectable))
}
return buildMap {
for (index in 1..count) {
put("SIM$index", false)
put("SIM$index", SlotState.NotConnectable)
}
}
}
Expand Down

0 comments on commit 55c5d87

Please sign in to comment.