Browse Source

fix: notification handling in multiple se (#269)

Reviewed-on: https://gitea.angry.im/PeterCxy/OpenEUICC/pulls/269
Co-authored-by: septs <github@septs.pw>
Co-committed-by: septs <github@septs.pw>
septs 2 months ago
parent
commit
22a7a5145c

+ 3 - 12
app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelManager.kt

@@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.Flow
  * or when this instance is destroyed.
  * or when this instance is destroyed.
  *
  *
  * To precisely control the lifecycle of this object itself (and thus its cached channels),
  * To precisely control the lifecycle of this object itself (and thus its cached channels),
- * all other compoents must access EuiccChannelManager objects through EuiccChannelManagerService.
+ * all other components must access EuiccChannelManager objects through EuiccChannelManagerService.
  * Holding references independent of EuiccChannelManagerService is unsupported.
  * Holding references independent of EuiccChannelManagerService is unsupported.
  */
  */
 interface EuiccChannelManager {
 interface EuiccChannelManager {
@@ -86,21 +86,12 @@ interface EuiccChannelManager {
      *
      *
      * If a channel for that slot / port is not found, EuiccChannelNotFoundException is thrown
      * If a channel for that slot / port is not found, EuiccChannelNotFoundException is thrown
      */
      */
-    suspend fun <R> withEuiccChannel(
-        physicalSlotId: Int,
-        portId: Int,
-        seId: EuiccChannel.SecureElementId = EuiccChannel.SecureElementId.DEFAULT,
-        fn: suspend (EuiccChannel) -> R
-    ): R
+    suspend fun <R> withEuiccChannel(physicalSlotId: Int, portId: Int, seId: EuiccChannel.SecureElementId, fn: suspend (EuiccChannel) -> R): R
 
 
     /**
     /**
      * Same as withEuiccChannel(Int, Int, SecureElementId, (EuiccChannel) -> R) but instead uses logical slot ID
      * Same as withEuiccChannel(Int, Int, SecureElementId, (EuiccChannel) -> R) but instead uses logical slot ID
      */
      */
-    suspend fun <R> withEuiccChannel(
-        logicalSlotId: Int,
-        seId: EuiccChannel.SecureElementId = EuiccChannel.SecureElementId.DEFAULT,
-        fn: suspend (EuiccChannel) -> R
-    ): R
+    suspend fun <R> withEuiccChannel(logicalSlotId: Int, seId: EuiccChannel.SecureElementId, fn: suspend (EuiccChannel) -> R): R
 
 
     /**
     /**
      * Invalidate all EuiccChannels previously cached by this Manager
      * Invalidate all EuiccChannels previously cached by this Manager

+ 19 - 23
app-common/src/main/java/im/angry/openeuicc/ui/NotificationsActivity.kt

@@ -52,16 +52,13 @@ class NotificationsActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker
     }
     }
 
 
     override fun onInit() {
     override fun onInit() {
-        notificationList.layoutManager =
-            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
-        notificationList.addItemDecoration(
-            DividerItemDecoration(
-                this,
-                LinearLayoutManager.VERTICAL
-            )
-        )
-        notificationList.adapter = notificationAdapter
-        registerForContextMenu(notificationList)
+        notificationList.apply {
+            val context = this@NotificationsActivity
+            adapter = notificationAdapter
+            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
+            addItemDecoration(DividerItemDecoration(context, LinearLayoutManager.VERTICAL))
+            registerForContextMenu(this)
+        }
 
 
         logicalSlotId = intent.getIntExtra("logicalSlotId", 0)
         logicalSlotId = intent.getIntExtra("logicalSlotId", 0)
         seId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
         seId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
@@ -131,21 +128,20 @@ class NotificationsActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker
 
 
     private fun refresh() {
     private fun refresh() {
         launchTask {
         launchTask {
-            notificationAdapter.notifications =
-                euiccChannelManager.withEuiccChannel(logicalSlotId, seId) { channel ->
-                    val nameMap = buildMap {
-                        for (profile in channel.lpa.profiles) {
-                            put(profile.iccid, profile.displayName)
-                        }
-                    }
+            notificationAdapter.notifications = withEuiccChannel { channel ->
+                val nameMap = channel.lpa.profiles
+                    .associate { Pair(it.iccid, it.displayName) }
 
 
-                    channel.lpa.notifications.map {
-                        LocalProfileNotificationWrapper(it, nameMap[it.iccid] ?: "???")
-                    }
+                channel.lpa.notifications.map {
+                    LocalProfileNotificationWrapper(it, nameMap[it.iccid] ?: "???")
                 }
                 }
+            }
         }
         }
     }
     }
 
 
+    private suspend fun <R> withEuiccChannel(fn: suspend (EuiccChannel) -> R) =
+        euiccChannelManager.withEuiccChannel(logicalSlotId, seId, fn)
+
     data class LocalProfileNotificationWrapper(
     data class LocalProfileNotificationWrapper(
         val inner: LocalProfileNotification,
         val inner: LocalProfileNotification,
         val profileName: String
         val profileName: String
@@ -224,7 +220,7 @@ class NotificationsActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker
                 R.id.notification_process -> {
                 R.id.notification_process -> {
                     launchTask {
                     launchTask {
                         withContext(Dispatchers.IO) {
                         withContext(Dispatchers.IO) {
-                            euiccChannelManager.withEuiccChannel(logicalSlotId) { channel ->
+                            withEuiccChannel { channel ->
                                 channel.lpa.handleNotification(notification.inner.seqNumber)
                                 channel.lpa.handleNotification(notification.inner.seqNumber)
                             }
                             }
                         }
                         }
@@ -237,7 +233,7 @@ class NotificationsActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker
                 R.id.notification_delete -> {
                 R.id.notification_delete -> {
                     launchTask {
                     launchTask {
                         withContext(Dispatchers.IO) {
                         withContext(Dispatchers.IO) {
-                            euiccChannelManager.withEuiccChannel(logicalSlotId) { channel ->
+                            withEuiccChannel { channel ->
                                 channel.lpa.deleteNotification(notification.inner.seqNumber)
                                 channel.lpa.deleteNotification(notification.inner.seqNumber)
                             }
                             }
                         }
                         }
@@ -271,4 +267,4 @@ class NotificationsActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker
             holder.updateNotification(notifications[position])
             holder.updateNotification(notifications[position])
 
 
     }
     }
-}
+}