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

...and add some logs
This commit is contained in:
Peter Cai 2023-12-31 20:36:51 -05:00
parent 3357a90f91
commit d3db8c6df5
6 changed files with 48 additions and 23 deletions

View file

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

View file

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

View file

@ -171,9 +171,6 @@ class ProfileDownloadFragment : DialogFragment(), EuiccFragmentMarker, Toolbar.O
lifecycleScope.launch {
try {
doDownloadProfile(server, code, confirmationCode, imei)
if (preferenceRepository.notificationDownloadFlow.first()) {
channel.lpa.handleLatestNotification(LocalProfileNotification.Operation.Install)
}
} catch (e: Exception) {
Log.d(TAG, "Error downloading profile")
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)
}
}
}

View file

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

View file

@ -15,7 +15,7 @@ class LocalProfileAssistantImpl(
httpInterface: HttpInterface
): LocalProfileAssistant {
companion object {
val TAG = "LocalProfileAssistantImpl"
private const val TAG = "LocalProfileAssistantImpl"
}
private val contextHandle: Long = LpacJni.createContext(apduInterface, httpInterface)
@ -60,7 +60,9 @@ class LocalProfileAssistantImpl(
LpacJni.es10bDeleteNotification(contextHandle, seqNumber) == 0
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) {
notifications.find { it.profileManagementOperation == operation }?.let {

View file

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