Modern Apps git

← Modern-Apps.git

euicc: add system LPA EuiccService with SystemApi stubs

Commit
5cc368a6930663f2702ba08fcb07eda378208479
Author
Vayun Mathur <[email protected]>
When
2026-08-12 16:49 UTC (3 months ago)
Parents
500f7ab

Diff

euicc/build.gradle.kts
diff --git a/euicc/build.gradle.kts b/euicc/build.gradle.ktsindex e3f8619d1b..4018589a55 100644--- a/euicc/build.gradle.kts+++ b/euicc/build.gradle.kts@@ -28,6 +28,10 @@ androidComponents { rustNativeLib("euicc")  dependencies {+    // Compile-only stubs for the framework's @SystemApi eSIM LPA classes+    // (android.service.euicc.*, EuiccProfileInfo). Provided by the framework at+    // runtime on a system image; must NOT be packaged.+    compileOnly(project(":library:euicc-stubs"))     // Rust reaches the SM-DP+ over HTTP through the flat-framed JNI bridge in     // library:network (NativeHttpBridge), so libeuicc.so links no TLS of its own.     implementation(project(":library:network"))
euicc/src/main/AndroidManifest.xml
diff --git a/euicc/src/main/AndroidManifest.xml b/euicc/src/main/AndroidManifest.xmlindex 8d4c10a2f7..344e0ec521 100644--- a/euicc/src/main/AndroidManifest.xml+++ b/euicc/src/main/AndroidManifest.xml@@ -5,6 +5,10 @@          privileges or a platform-signed system app holding these permissions. -->     <uses-permission android:name="android.permission.READ_PHONE_STATE" />     <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />+    <!-- Privileged: manage embedded (eSIM) subscriptions as the system LPA. Granted+         only to a platform-signed priv-app whitelisted in privapp-permissions*.xml. -->+    <uses-permission android:name="android.permission.WRITE_EMBEDDED_SUBSCRIPTIONS" />+    <uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />     <!-- Reaching the SM-DP+ for profile download. -->     <uses-permission android:name="android.permission.INTERNET" />     <!-- Scanning eSIM activation-code QR codes. -->@@ -28,7 +32,25 @@                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>+            <!-- Local User Interface (LUI) the framework starts for eSIM management. -->+            <intent-filter android:priority="10">+                <action android:name="android.service.euicc.action.MANAGE_EMBEDDED_SUBSCRIPTIONS" />+                <action android:name="android.service.euicc.action.PROVISION_EMBEDDED_SUBSCRIPTION" />+                <category android:name="android.intent.category.DEFAULT" />+            </intent-filter>         </activity>++        <!-- System LPA service. The platform's EuiccManager binds this via the+             EuiccService action once this app is the configured LPA; access is+             gated by the system-only BIND_EUICC_SERVICE permission. -->+        <service+            android:name=".service.EuiccManagerService"+            android:exported="true"+            android:permission="android.permission.BIND_EUICC_SERVICE">+            <intent-filter android:priority="10">+                <action android:name="android.service.euicc.EuiccService" />+            </intent-filter>+        </service>     </application>  </manifest>
euicc/src/main/java/com/vayunmathur/euicc/service/EuiccManagerService.kt
diff --git a/euicc/src/main/java/com/vayunmathur/euicc/service/EuiccManagerService.kt b/euicc/src/main/java/com/vayunmathur/euicc/service/EuiccManagerService.ktnew file mode 100644index 0000000000..844f9696cd--- /dev/null+++ b/euicc/src/main/java/com/vayunmathur/euicc/service/EuiccManagerService.kt@@ -0,0 +1,147 @@+package com.vayunmathur.euicc.service++import android.service.euicc.EuiccService+import android.service.euicc.EuiccService.OTA_STATUS_NOT_UPDATED+import android.service.euicc.EuiccService.OtaStatusChangedCallback+import android.service.euicc.EuiccService.RESULT_FIRST_USER+import android.service.euicc.EuiccService.RESULT_MUST_DEACTIVATE_SIM+import android.service.euicc.EuiccService.RESULT_OK+import android.service.euicc.GetDefaultDownloadableSubscriptionListResult+import android.service.euicc.GetDownloadableSubscriptionMetadataResult+import android.service.euicc.GetEuiccProfileInfoListResult+import android.telephony.euicc.DownloadableSubscription+import android.telephony.euicc.EuiccInfo as FrameworkEuiccInfo+import android.telephony.euicc.EuiccProfileInfo+import com.vayunmathur.euicc.EuiccNative+import com.vayunmathur.euicc.data.EuiccInfo+import com.vayunmathur.euicc.data.Profile+import com.vayunmathur.euicc.telephony.EuiccChannelManager+import kotlinx.serialization.json.Json++/**+ * The system LPA entry point: the platform's `EuiccManager` binds this service+ * (once the app is the configured LPA and is platform-signed / carrier-privileged)+ * and routes all eSIM operations through it. Each callback drives the built-in+ * eUICC over the ISD-R logical channel via [EuiccChannelManager] and the native+ * SGP.22 core.+ *+ * There is a single soldered eUICC, so `slotId` is ignored. Live behavior requires+ * a platform-signed priv-app install and a real eUICC; it cannot be exercised here.+ */+class EuiccManagerService : EuiccService() {+    private val channelManager by lazy { EuiccChannelManager(applicationContext) }+    private val json = Json { ignoreUnknownKeys = true }++    override fun onGetEid(slotId: Int): String? =+        runCatching { channelManager.withIsdrChannel { EuiccNative.nativeGetEid() } }.getOrNull()++    override fun onGetOtaStatus(slotId: Int): Int = OTA_STATUS_NOT_UPDATED++    override fun onStartOtaIfNecessary(slotId: Int, statusChangedCallback: OtaStatusChangedCallback) {+        // No OTA channel for this LPA; report the terminal state and return.+        statusChangedCallback.onOtaStatusChanged(OTA_STATUS_NOT_UPDATED)+    }++    override fun onGetEuiccProfileInfoList(slotId: Int): GetEuiccProfileInfoListResult {+        val profiles = runCatching { loadProfiles() }.getOrNull()+            ?: return GetEuiccProfileInfoListResult(RESULT_MUST_DEACTIVATE_SIM, null, false)+        val infos = profiles.map { it.toFrameworkProfileInfo() }.toTypedArray()+        // Embedded (non-removable) eUICC.+        return GetEuiccProfileInfoListResult(RESULT_OK, infos, false)+    }++    override fun onGetEuiccInfo(slotId: Int): FrameworkEuiccInfo {+        val svn = runCatching {+            channelManager.withIsdrChannel {+                EuiccNative.nativeGetEuiccInfo()?.let { json.decodeFromString<EuiccInfo>(it).svn }+            }+        }.getOrNull().orEmpty()+        return FrameworkEuiccInfo(svn)+    }++    override fun onDeleteSubscription(slotId: Int, iccid: String): Int = runProfileOp(iccid) { raw ->+        EuiccNative.nativeDeleteProfile(raw)+    }++    override fun onSwitchToSubscription(slotId: Int, iccid: String?, forceDeactivateSim: Boolean): Int {+        if (iccid == null) return RESULT_FIRST_USER+        return runProfileOp(iccid) { raw -> EuiccNative.nativeEnableProfile(raw) }+    }++    override fun onUpdateSubscriptionNickname(slotId: Int, iccid: String, nickname: String?): Int =+        runProfileOp(iccid) { raw -> EuiccNative.nativeSetNickname(raw, nickname.orEmpty()) }++    override fun onDownloadSubscription(+        slotId: Int,+        subscription: DownloadableSubscription,+        switchAfterDownload: Boolean,+        forceDeactivateSim: Boolean,+    ): Int {+        val code = subscription.encodedActivationCode ?: return RESULT_FIRST_USER+        return runCatching {+            val raw = channelManager.withIsdrChannel { EuiccNative.nativeDownloadProfile(code) }+            val result = json.decodeFromString<DownloadOutcome>(raw)+            if (result.success) RESULT_OK else RESULT_FIRST_USER+        }.getOrDefault(RESULT_FIRST_USER)+    }++    override fun onEraseSubscriptions(slotId: Int): Int = RESULT_FIRST_USER++    override fun onRetainSubscriptionsForFactoryReset(slotId: Int): Int = RESULT_FIRST_USER++    override fun onGetDownloadableSubscriptionMetadata(+        slotId: Int,+        subscription: DownloadableSubscription,+        forceDeactivateSim: Boolean,+    ): GetDownloadableSubscriptionMetadataResult =+        // Resolving metadata requires the ES9+ round trip; return the request as-is.+        GetDownloadableSubscriptionMetadataResult(RESULT_OK, subscription)++    override fun onGetDefaultDownloadableSubscriptionList(+        slotId: Int,+        forceDeactivateSim: Boolean,+    ): GetDefaultDownloadableSubscriptionListResult =+        // No SM-DS discovery configured.+        GetDefaultDownloadableSubscriptionListResult(RESULT_OK, emptyArray())++    // --- helpers ---++    private fun loadProfiles(): List<Profile> =+        channelManager.withIsdrChannel {+            EuiccNative.nativeGetProfiles()?.let { json.decodeFromString<List<Profile>>(it) } ?: emptyList()+        }++    /** Resolves the framework ICCID to our raw ICCID and runs [op] in one channel session. */+    private fun runProfileOp(iccid: String, op: (String) -> Int): Int = runCatching {+        channelManager.withIsdrChannel {+            val raw = EuiccNative.nativeGetProfiles()+                ?.let { json.decodeFromString<List<Profile>>(it) }+                ?.firstOrNull { it.iccidDisplay == iccid || it.iccid == iccid }+                ?.iccid+                ?: return@withIsdrChannel RESULT_FIRST_USER+            if (op(raw) == 0) RESULT_OK else RESULT_FIRST_USER+        }+    }.getOrDefault(RESULT_FIRST_USER)++    private fun Profile.toFrameworkProfileInfo(): EuiccProfileInfo =+        EuiccProfileInfo.Builder(iccidDisplay)+            .setNickname(nickname)+            .setServiceProviderName(serviceProvider)+            .setProfileName(name)+            .setState(+                if (isEnabled) EuiccProfileInfo.PROFILE_STATE_ENABLED+                else EuiccProfileInfo.PROFILE_STATE_DISABLED,+            )+            .setProfileClass(+                when (profileClass) {+                    "test" -> EuiccProfileInfo.PROFILE_CLASS_TESTING+                    "provisioning" -> EuiccProfileInfo.PROFILE_CLASS_PROVISIONING+                    "operational" -> EuiccProfileInfo.PROFILE_CLASS_OPERATIONAL+                    else -> EuiccProfileInfo.PROFILE_CLASS_UNSET+                },+            )+            .build()++    @kotlinx.serialization.Serializable+    private data class DownloadOutcome(val success: Boolean = false, val message: String = "")+}
library/euicc-stubs/build.gradle.kts
diff --git a/library/euicc-stubs/build.gradle.kts b/library/euicc-stubs/build.gradle.ktsnew file mode 100644index 0000000000..d7f4fe5ac6--- /dev/null+++ b/library/euicc-stubs/build.gradle.kts@@ -0,0 +1,12 @@+plugins {+    id("common-conventions-library")+}++// Compile-only stubs for the framework's @SystemApi eSIM LPA classes+// (`android.service.euicc.*`, `android.telephony.euicc.EuiccProfileInfo`). These+// classes exist in the base framework at runtime on every device but are hidden+// from the public SDK, so this module is depended on with `compileOnly` and its+// classes must NOT be packaged into any APK.+//+// Signatures mirror AOSP so `EuiccManagerService` overrides the real abstract+// methods exactly. Live behavior requires a platform-signed priv-app install.
library/euicc-stubs/src/main/java/android/service/euicc/EuiccService.java
diff --git a/library/euicc-stubs/src/main/java/android/service/euicc/EuiccService.java b/library/euicc-stubs/src/main/java/android/service/euicc/EuiccService.javanew file mode 100644index 0000000000..a14c88538c--- /dev/null+++ b/library/euicc-stubs/src/main/java/android/service/euicc/EuiccService.java@@ -0,0 +1,61 @@+package android.service.euicc;++import android.app.Service;+import android.content.Intent;+import android.os.IBinder;+import android.telephony.euicc.DownloadableSubscription;+import android.telephony.euicc.EuiccInfo;++/**+ * Compile-only stub of the framework's {@code @SystemApi}+ * {@code android.service.euicc.EuiccService}. Mirrors the AOSP abstract surface+ * the LPA must implement. Not packaged; the real class is provided by the+ * framework at runtime.+ */+public abstract class EuiccService extends Service {+    public static final int RESULT_OK = 0;+    public static final int RESULT_MUST_DEACTIVATE_SIM = 1;+    public static final int RESULT_RESOLVABLE_ERRORS = 2;+    public static final int RESULT_FIRST_USER = 10;++    public static final int OTA_STATUS_NOT_UPDATED = 1;++    public interface OtaStatusChangedCallback {+        void onOtaStatusChanged(int status);+    }++    @Override+    public IBinder onBind(Intent intent) {+        return null;+    }++    public abstract String onGetEid(int slotId);++    public abstract int onGetOtaStatus(int slotId);++    public abstract void onStartOtaIfNecessary(int slotId, OtaStatusChangedCallback statusChangedCallback);++    public abstract GetDownloadableSubscriptionMetadataResult onGetDownloadableSubscriptionMetadata(+            int slotId, DownloadableSubscription subscription, boolean forceDeactivateSim);++    public abstract GetDefaultDownloadableSubscriptionListResult onGetDefaultDownloadableSubscriptionList(+            int slotId, boolean forceDeactivateSim);++    public abstract GetEuiccProfileInfoListResult onGetEuiccProfileInfoList(int slotId);++    public abstract EuiccInfo onGetEuiccInfo(int slotId);++    public abstract int onDeleteSubscription(int slotId, String iccid);++    public abstract int onSwitchToSubscription(int slotId, String iccid, boolean forceDeactivateSim);++    public abstract int onUpdateSubscriptionNickname(int slotId, String iccid, String nickname);++    public abstract int onEraseSubscriptions(int slotId);++    public abstract int onRetainSubscriptionsForFactoryReset(int slotId);++    public abstract int onDownloadSubscription(+            int slotId, DownloadableSubscription subscription, boolean switchAfterDownload,+            boolean forceDeactivateSim);+}
library/euicc-stubs/src/main/java/android/service/euicc/GetDefaultDownloadableSubscriptionListResult.java
diff --git a/library/euicc-stubs/src/main/java/android/service/euicc/GetDefaultDownloadableSubscriptionListResult.java b/library/euicc-stubs/src/main/java/android/service/euicc/GetDefaultDownloadableSubscriptionListResult.javanew file mode 100644index 0000000000..73e6242668--- /dev/null+++ b/library/euicc-stubs/src/main/java/android/service/euicc/GetDefaultDownloadableSubscriptionListResult.java@@ -0,0 +1,8 @@+package android.service.euicc;++import android.telephony.euicc.DownloadableSubscription;++/** Compile-only stub of the {@code @SystemApi} default-list result. */+public final class GetDefaultDownloadableSubscriptionListResult {+    public GetDefaultDownloadableSubscriptionListResult(int result, DownloadableSubscription[] subscriptions) {}+}
library/euicc-stubs/src/main/java/android/service/euicc/GetDownloadableSubscriptionMetadataResult.java
diff --git a/library/euicc-stubs/src/main/java/android/service/euicc/GetDownloadableSubscriptionMetadataResult.java b/library/euicc-stubs/src/main/java/android/service/euicc/GetDownloadableSubscriptionMetadataResult.javanew file mode 100644index 0000000000..6f7fe31707--- /dev/null+++ b/library/euicc-stubs/src/main/java/android/service/euicc/GetDownloadableSubscriptionMetadataResult.java@@ -0,0 +1,8 @@+package android.service.euicc;++import android.telephony.euicc.DownloadableSubscription;++/** Compile-only stub of the {@code @SystemApi} metadata-resolution result. */+public final class GetDownloadableSubscriptionMetadataResult {+    public GetDownloadableSubscriptionMetadataResult(int result, DownloadableSubscription subscription) {}+}
library/euicc-stubs/src/main/java/android/service/euicc/GetEuiccProfileInfoListResult.java
diff --git a/library/euicc-stubs/src/main/java/android/service/euicc/GetEuiccProfileInfoListResult.java b/library/euicc-stubs/src/main/java/android/service/euicc/GetEuiccProfileInfoListResult.javanew file mode 100644index 0000000000..4ed1588b18--- /dev/null+++ b/library/euicc-stubs/src/main/java/android/service/euicc/GetEuiccProfileInfoListResult.java@@ -0,0 +1,8 @@+package android.service.euicc;++import android.telephony.euicc.EuiccProfileInfo;++/** Compile-only stub of the {@code @SystemApi} ES10 profile-list result. */+public final class GetEuiccProfileInfoListResult {+    public GetEuiccProfileInfoListResult(int result, EuiccProfileInfo[] profiles, boolean isRemovable) {}+}
library/euicc-stubs/src/main/java/android/telephony/euicc/EuiccProfileInfo.java
diff --git a/library/euicc-stubs/src/main/java/android/telephony/euicc/EuiccProfileInfo.java b/library/euicc-stubs/src/main/java/android/telephony/euicc/EuiccProfileInfo.javanew file mode 100644index 0000000000..d3b88da858--- /dev/null+++ b/library/euicc-stubs/src/main/java/android/telephony/euicc/EuiccProfileInfo.java@@ -0,0 +1,45 @@+package android.telephony.euicc;++/**+ * Compile-only stub of the {@code @SystemApi}+ * {@code android.telephony.euicc.EuiccProfileInfo}. Only the members the LPA+ * constructs are declared. Not packaged; the framework provides the real class.+ */+public final class EuiccProfileInfo {+    public static final int PROFILE_STATE_UNSET = -1;+    public static final int PROFILE_STATE_DISABLED = 0;+    public static final int PROFILE_STATE_ENABLED = 1;++    public static final int PROFILE_CLASS_UNSET = -1;+    public static final int PROFILE_CLASS_TESTING = 0;+    public static final int PROFILE_CLASS_PROVISIONING = 1;+    public static final int PROFILE_CLASS_OPERATIONAL = 2;++    public static final class Builder {+        public Builder(String iccid) {}++        public Builder setNickname(String nickname) {+            return this;+        }++        public Builder setServiceProviderName(String serviceProviderName) {+            return this;+        }++        public Builder setProfileName(String profileName) {+            return this;+        }++        public Builder setState(int state) {+            return this;+        }++        public Builder setProfileClass(int profileClass) {+            return this;+        }++        public EuiccProfileInfo build() {+            return null;+        }+    }+}
settings.gradle.kts
diff --git a/settings.gradle.kts b/settings.gradle.ktsindex 3742d704fb..1e7d4edee1 100644--- a/settings.gradle.kts+++ b/settings.gradle.kts@@ -89,6 +89,7 @@ include(":vpn") include(":web") include(":appstore") include(":library:locationprovider")+include(":library:euicc-stubs") include(":networklocation") include(":fooddelivery") include(":musicbrainz")