Browse Source

fixup: Profile notifications need to be run in the I/O context

...and add some logs
Peter Cai 2 years ago
parent
commit
d3db8c6df5

+ 6 - 2
app-common/src/main/java/im/angry/openeuicc/ui/NotificationsActivity.kt

@@ -176,14 +176,18 @@ class NotificationsActivity: AppCompatActivity() {
             when (item.itemId) {
             when (item.itemId) {
                 R.id.notification_process -> {
                 R.id.notification_process -> {
                     launchTask {
                     launchTask {
-                        euiccChannel.lpa.handleNotification(notification.inner.seqNumber)
+                        withContext(Dispatchers.IO) {
+                            euiccChannel.lpa.handleNotification(notification.inner.seqNumber)
+                        }
                     }
                     }
                     refresh()
                     refresh()
                     true
                     true
                 }
                 }
                 R.id.notification_delete -> {
                 R.id.notification_delete -> {
                     launchTask {
                     launchTask {
-                        euiccChannel.lpa.deleteNotification(notification.inner.seqNumber)
+                        withContext(Dispatchers.IO) {
+                            euiccChannel.lpa.deleteNotification(notification.inner.seqNumber)
+                        }
                     }
                     }
                     refresh()
                     refresh()
                     true
                     true

+ 3 - 3
app-common/src/main/java/im/angry/openeuicc/ui/ProfileDeleteFragment.kt

@@ -61,9 +61,6 @@ class ProfileDeleteFragment : DialogFragment(), EuiccFragmentMarker {
         lifecycleScope.launch {
         lifecycleScope.launch {
             try {
             try {
                 doDelete()
                 doDelete()
-                if (preferenceRepository.notificationDeleteFlow.first()) {
-                    channel.lpa.handleLatestNotification(LocalProfileNotification.Operation.Delete)
-                }
             } catch (e: Exception) {
             } catch (e: Exception) {
                 Log.d(ProfileDownloadFragment.TAG, "Error deleting profile")
                 Log.d(ProfileDownloadFragment.TAG, "Error deleting profile")
                 Log.d(ProfileDownloadFragment.TAG, Log.getStackTraceString(e))
                 Log.d(ProfileDownloadFragment.TAG, Log.getStackTraceString(e))
@@ -78,5 +75,8 @@ class ProfileDeleteFragment : DialogFragment(), EuiccFragmentMarker {
 
 
     private suspend fun doDelete() = withContext(Dispatchers.IO) {
     private suspend fun doDelete() = withContext(Dispatchers.IO) {
         channel.lpa.deleteProfile(requireArguments().getString("iccid")!!)
         channel.lpa.deleteProfile(requireArguments().getString("iccid")!!)
+        if (preferenceRepository.notificationDeleteFlow.first()) {
+            channel.lpa.handleLatestNotification(LocalProfileNotification.Operation.Delete)
+        }
     }
     }
 }
 }

+ 5 - 3
app-common/src/main/java/im/angry/openeuicc/ui/ProfileDownloadFragment.kt

@@ -171,9 +171,6 @@ class ProfileDownloadFragment : DialogFragment(), EuiccFragmentMarker, Toolbar.O
         lifecycleScope.launch {
         lifecycleScope.launch {
             try {
             try {
                 doDownloadProfile(server, code, confirmationCode, imei)
                 doDownloadProfile(server, code, confirmationCode, imei)
-                if (preferenceRepository.notificationDownloadFlow.first()) {
-                    channel.lpa.handleLatestNotification(LocalProfileNotification.Operation.Install)
-                }
             } catch (e: Exception) {
             } catch (e: Exception) {
                 Log.d(TAG, "Error downloading profile")
                 Log.d(TAG, "Error downloading profile")
                 Log.d(TAG, Log.getStackTraceString(e))
                 Log.d(TAG, Log.getStackTraceString(e))
@@ -196,5 +193,10 @@ class ProfileDownloadFragment : DialogFragment(), EuiccFragmentMarker, Toolbar.O
                 }
                 }
             }
             }
         })
         })
+
+        // If we get here, we are successful
+        if (preferenceRepository.notificationDownloadFlow.first()) {
+            channel.lpa.handleLatestNotification(LocalProfileNotification.Operation.Install)
+        }
     }
     }
 }
 }

+ 27 - 13
libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/HttpInterfaceImpl.kt

@@ -1,29 +1,43 @@
 package net.typeblog.lpac_jni.impl
 package net.typeblog.lpac_jni.impl
 
 
+import android.util.Log
 import net.typeblog.lpac_jni.HttpInterface
 import net.typeblog.lpac_jni.HttpInterface
 import java.net.HttpURLConnection
 import java.net.HttpURLConnection
 import java.net.URL
 import java.net.URL
 
 
 class HttpInterfaceImpl: HttpInterface {
 class HttpInterfaceImpl: HttpInterface {
+    companion object {
+        private const val TAG = "HttpInterfaceImpl"
+    }
+
     override fun transmit(
     override fun transmit(
         url: String,
         url: String,
         tx: ByteArray,
         tx: ByteArray,
         headers: Array<String>
         headers: Array<String>
     ): HttpInterface.HttpResponse {
     ): HttpInterface.HttpResponse {
-        val conn = URL(url).openConnection() as HttpURLConnection
-        conn.requestMethod = "POST"
-        conn.doInput = true
-        conn.doOutput = true
-
-        for (h in headers) {
-            val s = h.split(":", limit = 2)
-            conn.setRequestProperty(s[0], s[1])
-        }
+        Log.d(TAG, "transmit(url = $url)")
+
+        try {
+            val conn = URL(url).openConnection() as HttpURLConnection
+            conn.requestMethod = "POST"
+            conn.doInput = true
+            conn.doOutput = true
 
 
-        conn.outputStream.write(tx)
-        conn.outputStream.flush()
-        conn.outputStream.close()
+            for (h in headers) {
+                val s = h.split(":", limit = 2)
+                conn.setRequestProperty(s[0], s[1])
+            }
 
 
-        return HttpInterface.HttpResponse(conn.responseCode, conn.inputStream.readBytes())
+            conn.outputStream.write(tx)
+            conn.outputStream.flush()
+            conn.outputStream.close()
+
+            Log.d(TAG, "transmit responseCode = ${conn.responseCode}")
+
+            return HttpInterface.HttpResponse(conn.responseCode, conn.inputStream.readBytes())
+        } catch (e: Exception) {
+            e.printStackTrace()
+            throw e
+        }
     }
     }
 }
 }

+ 4 - 2
libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/LocalProfileAssistantImpl.kt

@@ -15,7 +15,7 @@ class LocalProfileAssistantImpl(
     httpInterface: HttpInterface
     httpInterface: HttpInterface
 ): LocalProfileAssistant {
 ): LocalProfileAssistant {
     companion object {
     companion object {
-        val TAG = "LocalProfileAssistantImpl"
+        private const val TAG = "LocalProfileAssistantImpl"
     }
     }
 
 
     private val contextHandle: Long = LpacJni.createContext(apduInterface, httpInterface)
     private val contextHandle: Long = LpacJni.createContext(apduInterface, httpInterface)
@@ -60,7 +60,9 @@ class LocalProfileAssistantImpl(
         LpacJni.es10bDeleteNotification(contextHandle, seqNumber) == 0
         LpacJni.es10bDeleteNotification(contextHandle, seqNumber) == 0
 
 
     override fun handleNotification(seqNumber: Long): Boolean =
     override fun handleNotification(seqNumber: Long): Boolean =
-        LpacJni.handleNotification(contextHandle, seqNumber) == 0
+        LpacJni.handleNotification(contextHandle, seqNumber).also {
+            Log.d(TAG, "handleNotification $seqNumber = $it")
+        } == 0
 
 
     override fun handleLatestNotification(operation: LocalProfileNotification.Operation) {
     override fun handleLatestNotification(operation: LocalProfileNotification.Operation) {
         notifications.find { it.profileManagementOperation == operation }?.let {
         notifications.find { it.profileManagementOperation == operation }?.let {

+ 3 - 0
libs/lpac-jni/src/main/jni/lpac-jni/lpac-notifications.c

@@ -2,6 +2,7 @@
 #include <euicc/es9p.h>
 #include <euicc/es9p.h>
 #include <euicc/es10b.h>
 #include <euicc/es10b.h>
 #include <malloc.h>
 #include <malloc.h>
+#include <syslog.h>
 
 
 jclass local_profile_notification_class;
 jclass local_profile_notification_class;
 jmethodID local_profile_notification_constructor;
 jmethodID local_profile_notification_constructor;
@@ -74,10 +75,12 @@ Java_net_typeblog_lpac_1jni_LpacJni_handleNotification(JNIEnv *env, jobject thiz
     int res;
     int res;
 
 
     res = es10b_retrieve_notification(ctx, &b64_payload, &receiver, (unsigned long) seq_number);
     res = es10b_retrieve_notification(ctx, &b64_payload, &receiver, (unsigned long) seq_number);
+    syslog(LOG_DEBUG, "es10b_retrieve_notification = %d", res);
     if (res < 0)
     if (res < 0)
         goto out;
         goto out;
 
 
     res = es9p_handle_notification(ctx, receiver, b64_payload);
     res = es9p_handle_notification(ctx, receiver, b64_payload);
+    syslog(LOG_DEBUG, "es9p_handle_notification = %d", res);
     if (res < 0)
     if (res < 0)
         goto out;
         goto out;