EMBEDDED_SUBSCRIPTION_RESULT_ERROR in eSIM implementation on Android using kotlin

512 Views Asked by At

EMBEDDED_SUBSCRIPTION_RESULT_ERROR

Hi all, I'm trying to implement eSim installation app using kotlin. I've been looking at google's documentation (https://source.android.com/devices/tech/connect/esim-overview?hl=en) about it and yet I have serval unfigured questions. Right now, In my app I'm getting the broadcast with onReceive function and I'm starting the flow by giving permission on errorCode 1 and then I'm stuck on errorCode 2 - > EMBEDDED_SUBSCRIPTION_RESULT_ERROR I can't figure out how to pass this error and what am I doing wrong. Additionally, when I'm passing to the registerReceiver function the LPA_DECLARED_PERMISSION with my bundle id the app doesn't go into the onReceive function. and my last unfigured question is what should I do when I need to pass a custom confirmation code for installing the eSim.

This is my application code:

package com.example.testesimsdk

//import android.telephony.euicc.DownloadableSubscription
//import android.telephony.euicc.EuiccManager
import android.R.attr.action
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.telephony.euicc.DownloadableSubscription
import android.telephony.euicc.EuiccManager
import android.telephony.euicc.EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE
//import android.telephony.euicc.*
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity(), View.OnClickListener {
    private val ACTIVATION_CODE = "LPA:1ֿ\$tl2.prod.ondemandconnectivity.com\$XWSAGDHOHV2VBGLN"
    private val Success = "Success"
    private val ACTIVATION_CODE2 = "LPA:1\$tl2.prod.ondemandconnectivity.com\$TRN3M6IW62V7ET5B"
    private val ACTION_DOWNLOAD_SUBSCRIPTION = "download_subscription"
    private val ACTION_DELETE_SUBSCRIPTION = "delete_subscription"
    private val LPA_DECLARED_PERMISSION = "com.example.testesimsdk.lpa.permission.BROADCAST"


    private lateinit var resultIntent: Intent
    private var mgr: EuiccManager? = null
    private lateinit var broadcastReceiver: BroadcastReceiver
    private lateinit var btn: Button;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Toast.makeText(applicationContext, ACTIVATION_CODE, Toast.LENGTH_LONG).show()
        implementEsim()
        btn = findViewById(R.id.button)
        btn.setOnClickListener(this)
    }


    private fun implementEsim() {
        setupEsim()
    }

    private fun initMgr() {
        if (mgr == null) {
            mgr = this.getSystemService(EUICC_SERVICE) as EuiccManager
        }
    }

    fun isEsimSupported(): Boolean {
        initMgr()
        return mgr?.isEnabled ?: false
    }

    fun setupEsim() {
        initMgr()
        if (isEsimSupported()) {
            val broadcastReceiver = object : BroadcastReceiver() {
                override fun onReceive(context: Context, intent: Intent) {
                    if (ACTION_DOWNLOAD_SUBSCRIPTION != intent.action) {
                        Log.d(
                            "[MainActivity lala]: ",
                            "Can't setup eSim du to wrong Intent:" + intent.getAction() + " instead of "
                                    + ACTION_DOWNLOAD_SUBSCRIPTION
                        );
                        return;
                    }
                    val resultCode: Int = resultCode
                    val detailedCode = intent.getIntExtra(
                        EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,
                        0 /* defaultValue*/
                    );
                    if (resultCode == EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR && mgr != null) {
                        Toast.makeText(applicationContext, "EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR", Toast.LENGTH_LONG).show()
                        Log.d(
                            "[MainActivity lala]:",
                            "EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR"
                        )
                        handleResolvableError(intent, resultCode)
                    } else if (resultCode == EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK) {
                        Toast.makeText(applicationContext, Success, Toast.LENGTH_LONG).show()
                        Log.d(
                            "[MainActivity lala]:",
                            "EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK"
                        )
                    } else if (resultCode == EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR) {
                        Toast.makeText(applicationContext, "EMBEDDED_SUBSCRIPTION_RESULT_ERROR", Toast.LENGTH_LONG).show()
                        // Embedded Subscription Error
                        handleResolvableError(intent, resultCode)
                        Log.d(
                            "[MainActivity lala]:",
                            "EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR"
                        )
                    } else {
                        // Unknown Error
                        Log.d(
                            "[MainActivity lala]:",
                            "Can't add an Esim subscription due to unknown error, resultCode is:$resultCode"
                        )
                    }
                }
            }

            this.registerReceiver(
                broadcastReceiver,
                IntentFilter(ACTION_DOWNLOAD_SUBSCRIPTION),
                null,
                null
            )
//        this.registerReceiver(broadcastReceiver, IntentFilter(ACTION_DOWNLOAD_SUBSCRIPTION), LPA_DECLARED_PERMISSION, null)

            val sub = DownloadableSubscription.forActivationCode(ACTIVATION_CODE)
            val intent = Intent(ACTION_DOWNLOAD_SUBSCRIPTION)
            val callbackIntent = PendingIntent.getBroadcast(
                this, 0 /* requestCode */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            mgr?.downloadSubscription(sub, true, callbackIntent);
        }
    }

    private fun handleResolvableError(intent: Intent, resCode: Int) {
        try {
            // Resolvable error, attempt to resolve it by a user action
            // FIXME: review logic of resolve functions
            val resolutionRequestCode = resCode
            val callbackIntent = PendingIntent.getBroadcast(
                this, resolutionRequestCode,
                Intent(ACTION_DOWNLOAD_SUBSCRIPTION), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
            )
            mgr?.startResolutionActivity(
                this,
                resolutionRequestCode,
                intent,
                callbackIntent
            )
        } catch (e: Exception) {
            Log.d(
                "[MainActivity lala]:",
                "EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR - Can't setup eSim du to Activity error "
                        + e.localizedMessage
            )
        }
    }

    private fun deleteSubs(){
        val intent = Intent(ACTION_DELETE_SUBSCRIPTION)
        val callbackIntent = PendingIntent.getBroadcast(
            this, 0 /* requestCode */, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )
        mgr?.deleteSubscription(1 /* subscriptionId */, callbackIntent);
    }

    override fun onClick(p0: View) {
        when (p0.id) {
            R.id.button -> deleteSubs()
        }
    }

    override fun onDestroy() {
        if (broadcastReceiver != null) {
            this.unregisterReceiver(broadcastReceiver)
        }
        super.onDestroy()
    }

}

I will be happy to get examples of implementing it correctly. Thank you!

0

There are 0 best solutions below