浏览代码

Move EuiccChannelManagerService to withEuiccChannel()

Peter Cai 1 年之前
父节点
当前提交
6a2d4d66dd
共有 1 个文件被更改,包括 28 次插入23 次删除
  1. 28 23
      app-common/src/main/java/im/angry/openeuicc/service/EuiccChannelManagerService.kt

+ 28 - 23
app-common/src/main/java/im/angry/openeuicc/service/EuiccChannelManagerService.kt

@@ -287,19 +287,20 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
             R.drawable.ic_task_sim_card_download
         ) {
             euiccChannelManager.beginTrackedOperation(slotId, portId) {
-                val channel = euiccChannelManager.findEuiccChannelByPort(slotId, portId)
-                val res = channel!!.lpa.downloadProfile(
-                    smdp,
-                    matchingId,
-                    imei,
-                    confirmationCode,
-                    object : ProfileDownloadCallback {
-                        override fun onStateUpdate(state: ProfileDownloadCallback.DownloadState) {
-                            if (state.progress == 0) return
-                            foregroundTaskState.value =
-                                ForegroundTaskState.InProgress(state.progress)
-                        }
-                    })
+                val res = euiccChannelManager.withEuiccChannel(slotId, portId) { channel ->
+                    channel.lpa.downloadProfile(
+                        smdp,
+                        matchingId,
+                        imei,
+                        confirmationCode,
+                        object : ProfileDownloadCallback {
+                            override fun onStateUpdate(state: ProfileDownloadCallback.DownloadState) {
+                                if (state.progress == 0) return
+                                foregroundTaskState.value =
+                                    ForegroundTaskState.InProgress(state.progress)
+                            }
+                        })
+                }
 
                 if (!res) {
                     // TODO: Provide more details on the error
@@ -321,10 +322,12 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
             getString(R.string.task_profile_rename_failure),
             R.drawable.ic_task_rename
         ) {
-            val res = euiccChannelManager.findEuiccChannelByPort(slotId, portId)!!.lpa.setNickname(
-                iccid,
-                name
-            )
+            val res = euiccChannelManager.withEuiccChannel(slotId, portId) { channel ->
+                channel.lpa.setNickname(
+                    iccid,
+                    name
+                )
+            }
 
             if (!res) {
                 throw RuntimeException("Profile not renamed")
@@ -342,10 +345,9 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
             R.drawable.ic_task_delete
         ) {
             euiccChannelManager.beginTrackedOperation(slotId, portId) {
-                euiccChannelManager.findEuiccChannelByPort(
-                    slotId,
-                    portId
-                )!!.lpa.deleteProfile(iccid)
+                euiccChannelManager.withEuiccChannel(slotId, portId) { channel ->
+                    channel.lpa.deleteProfile(iccid)
+                }
 
                 preferenceRepository.notificationDeleteFlow.first()
             }
@@ -366,8 +368,10 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
             R.drawable.ic_task_switch
         ) {
             euiccChannelManager.beginTrackedOperation(slotId, portId) {
-                val channel = euiccChannelManager.findEuiccChannelByPort(slotId, portId)!!
-                val (res, refreshed) =
+                val (res, refreshed) = euiccChannelManager.withEuiccChannel(
+                    slotId,
+                    portId
+                ) { channel ->
                     if (!channel.lpa.switchProfile(iccid, enable, refresh = true)) {
                         // Sometimes, we *can* enable or disable the profile, but we cannot
                         // send the refresh command to the modem because the profile somehow
@@ -378,6 +382,7 @@ class EuiccChannelManagerService : LifecycleService(), OpenEuiccContextMarker {
                     } else {
                         Pair(true, true)
                     }
+                }
 
                 if (!res) {
                     throw RuntimeException("Could not switch profile")