Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
septs committed Sep 15, 2024
1 parent f885916 commit a67b564
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
applicationId = "app.septs.euiccprobe"
minSdk = 21
targetSdk = 34
versionCode = 6
versionCode = 7
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
21 changes: 17 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<uses-permission android:name="org.simalliance.openmobileapi.SMARTCARD" />
android:name="org.simalliance.openmobileapi.SMARTCARD"
android:maxSdkVersion="28" />

<application
android:allowBackup="true"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
Expand All @@ -32,4 +31,18 @@
</activity>
</application>

<queries>
<!-- https://developer.android.com/reference/android/se/omapi/package-summary -->
<package android:name="com.android.se" />
<!-- https://github.com/seek-for-android -->
<package android:name="org.simalliance.openmobileapi.service" />
<!-- Google SIM Manager -->
<package android:name="com.google.android.euicc" />
<!-- MIUI LPA -->
<package android:name="com.miui.euicc" />
<!-- Samsung LPA -->
<package android:name="com.samsung.euicc" />
<!-- https://github.com/QueallyTech/OMAPI-Bypass -->
<package android:name="com.queallytech.omapi" />
</queries>
</manifest>
4 changes: 3 additions & 1 deletion app/src/main/java/app/septs/euiccprobe/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ class MainActivity : AppCompatActivity() {
if (label != null) {
appendLine("- $label (${pkg.packageName})")
} else {
appendLine("- ${pkg.packageName} [uninstalled]")
appendLine("- ${pkg.packageName}")
}
}
}
val properties = arrayOf(
"esim.enable_esim_system_ui_by_default",
"ro.telephony.sim_slots.count",
"ro.setupwizard.esim_cid_ignore",
// Xiaomi Vendor
"ro.vendor.miui.support_esim"
)
SystemProperties.pick(*properties).let {
if (it.isEmpty()) return@let
Expand Down
25 changes: 14 additions & 11 deletions app/src/main/java/app/septs/euiccprobe/OpenMobileAPI.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app.septs.euiccprobe

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.telephony.TelephonyManager
import android.util.Log
Expand Down Expand Up @@ -33,27 +32,31 @@ object OpenMobileAPI {
Available,
}


enum class SEBypass {
Unavailable,
CannotBeBypassed,
CanBeBypassed,
FullAccess,
TemporaryFullAccess,
PersistentFullAccess
}

@Suppress("SpellCheckingInspection")
@RequiresApi(Build.VERSION_CODES.P)
fun getBypassState(context: Context): SEBypass {
val pkgName = "com.android.se"
if (!SystemService.hasService(context, pkgName)) {
if (!SystemService.hasService(context, "com.android.se")) {
return SEBypass.Unavailable
}
if (SystemProperties["ro.debuggable"].toInt() != 1) {
return SEBypass.CannotBeBypassed
val isDebuggable = SystemProperties.isEnabled("ro.debuggable")
val isFullAccess = SystemProperties.boolean("service.seek") {
it.contains("fullaccess")
}
val isPersistFullAccess = SystemProperties.boolean("persist.service.seek") {
it.contains("fullaccess")
}
val isFullAccess = SystemProperties["service.seek"]
.ifEmpty { SystemProperties["persist.service.seek"] }
.contains("fullaccess")
return if (isFullAccess) SEBypass.FullAccess else SEBypass.CanBeBypassed
if (!isDebuggable) return SEBypass.CannotBeBypassed
if (isFullAccess) return SEBypass.TemporaryFullAccess
if (isPersistFullAccess) return SEBypass.PersistentFullAccess
return SEBypass.CanBeBypassed
}

suspend fun getSlots(context: Context): Result {
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/app/septs/euiccprobe/PrivAppPermissionParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.util.Xml
import app.septs.euiccprobe.PrivAppPermissionParser.Companion.PrivAppPermission
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.nio.charset.Charset
Expand All @@ -15,6 +16,30 @@ class PrivAppPermissionParser : Iterable<PrivAppPermission> {
val allowedPermissions: MutableSet<String>,
val deniedPermissions: MutableSet<String>
)

fun loadPermissions(): List<PrivAppPermission> {
val permissions = listOf(
File("/etc/permissions/"),
File("/system/etc/permissions/"),
File("/vendor/etc/permissions/"),
File("/product/etc/permissions/"),
)
val parser = PrivAppPermissionParser()
for (permission in permissions) {
if (!permission.exists()) continue
val files = permission.listFiles() ?: continue
for (file in files) {
if (!file.canRead()) continue
if (file.extension != "xml") continue
try {
file.inputStream().use(parser::parse)
} catch (e: Exception) {
// ignore
}
}
}
return parser.permissions.values.toList()
}
}

private val namespace: String? = null
Expand Down
19 changes: 3 additions & 16 deletions app/src/main/java/app/septs/euiccprobe/SystemApps.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package app.septs.euiccprobe

import android.content.Context
import android.content.pm.PackageManager
import java.io.File

object SystemApps {
private val requiredPermissions = setOf(
Expand All @@ -12,25 +10,14 @@ object SystemApps {
)

private val optionalPermissions = setOf(
"android.permission.BIND_EUICC_SERVICE",
"android.permission.SECURE_ELEMENT_PRIVILEGED_OPERATION",
"com.android.permission.WRITE_EMBEDDED_SUBSCRIPTIONS",
)

fun getSystemLPAs(): List<PrivAppPermissionParser.Companion.PrivAppPermission> {
val directories = listOf("/", "/system", "/vendor", "/product")
val parser = PrivAppPermissionParser()
for (directory in directories) {
val permissions = File(directory, "etc/permissions/")
if (!permissions.exists()) continue
val files = permissions.listFiles() ?: continue
for (file in files) {
if (!file.canRead()) continue
if (!file.name.startsWith("privapp-permissions")) continue
if (file.extension != "xml") continue
file.inputStream().use(parser::parse)
}
}
return parser.filter { perm ->
val permissions = PrivAppPermissionParser.loadPermissions()
return permissions.filter { perm ->
perm.allowedPermissions.containsAll(requiredPermissions) &&
perm.allowedPermissions.any(optionalPermissions::contains)
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/app/septs/euiccprobe/SystemProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ object SystemProperties {
put(name, properties[name])
}
}

fun boolean(name: String, matcher: (value: String) -> Boolean): Boolean {
return matcher(properties[name] ?: return false)
}

fun isEnabled(name: String) = boolean(name) {
val value = it.lowercase()
value == "1" || value == "y" || value == "true" || value == "yes" || value == "on"
}
}
9 changes: 2 additions & 7 deletions app/src/main/java/app/septs/euiccprobe/SystemService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object SystemService {
"android.hardware.telephony.euicc",
"android.hardware.telephony.euicc.mep",
"android.hardware.se.omapi.uicc",
"android.hardware.usb.host",
)
return buildMap {
for (feature in features) {
Expand All @@ -41,14 +42,8 @@ object SystemService {
}

fun hasService(context: Context, name: String): Boolean {
val pm = context.packageManager
val flags = 0
return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
pm.getApplicationInfo(name, PackageManager.ApplicationInfoFlags.of(flags.toLong()))
} else {
pm.getApplicationInfo(name, flags)
}
context.packageManager.getApplicationInfo(name, 0)
true
} catch (_: PackageManager.NameNotFoundException) {
false
Expand Down

0 comments on commit a67b564

Please sign in to comment.