ソースを参照

refactor: beginTrackedOperation should be a LPA extension

Peter Cai 1 年間 前
コミット
f73af48b59

+ 4 - 32
app-common/src/main/java/im/angry/openeuicc/util/EuiccChannelFragmentUtils.kt

@@ -1,15 +1,10 @@
 package im.angry.openeuicc.util
 
 import android.os.Bundle
-import android.util.Log
 import androidx.fragment.app.Fragment
 import im.angry.openeuicc.core.EuiccChannel
 import im.angry.openeuicc.core.EuiccChannelManager
 import im.angry.openeuicc.ui.BaseEuiccAccessActivity
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.withContext
-
-private const val TAG = "EuiccChannelFragmentUtils"
 
 interface EuiccChannelFragmentMarker: OpenEuiccContextMarker
 
@@ -37,32 +32,9 @@ val <T> T.channel: EuiccChannel where T: Fragment, T: EuiccChannelFragmentMarker
     get() =
         euiccChannelManager.findEuiccChannelByPortBlocking(slotId, portId)!!
 
-/*
- * Begin a "tracked" operation where notifications may be generated by the eSIM
- * Automatically handle any newly generated notification during the operation
- * if the function "op" returns true.
- */
-suspend fun <T> T.beginTrackedOperation(op: suspend () -> Boolean) where T : Fragment, T : EuiccChannelFragmentMarker =
-    withContext(Dispatchers.IO) {
-        val latestSeq = channel.lpa.notifications.firstOrNull()?.seqNumber ?: 0
-        Log.d(TAG, "Latest notification is $latestSeq before operation")
-        if (op()) {
-            Log.d(TAG, "Operation has requested notification handling")
-            try {
-                // Note that the exact instance of "channel" might have changed here if reconnected;
-                // so we MUST use the automatic getter for "channel"
-                channel.lpa.notifications.filter { it.seqNumber > latestSeq }.forEach {
-                    Log.d(TAG, "Handling notification $it")
-                    channel.lpa.handleNotification(it.seqNumber)
-                }
-            } catch (e: Exception) {
-                // Ignore any error during notification handling
-                e.printStackTrace()
-            }
-        }
-        Log.d(TAG, "Operation complete")
-    }
-
 interface EuiccProfilesChangedListener {
     fun onEuiccProfilesChanged()
-}
+}
+
+suspend fun <T> T.beginTrackedOperation(op: suspend () -> Boolean) where T: Fragment, T: EuiccChannelFragmentMarker =
+    channel.lpa.beginTrackedOperation(op)

+ 32 - 2
app-common/src/main/java/im/angry/openeuicc/util/LPAUtils.kt

@@ -2,9 +2,13 @@ package im.angry.openeuicc.util
 
 import android.util.Log
 import im.angry.openeuicc.core.EuiccChannel
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
 import net.typeblog.lpac_jni.LocalProfileAssistant
 import net.typeblog.lpac_jni.LocalProfileInfo
 
+private const val TAG = "LPAUtils"
+
 val LocalProfileInfo.displayName: String
     get() = nickName.ifEmpty { name }
 
@@ -26,7 +30,7 @@ val List<EuiccChannel>.hasMultipleChips: Boolean
  */
 fun LocalProfileAssistant.disableActiveProfile(refresh: Boolean): Boolean =
     profiles.find { it.isEnabled }?.let {
-        Log.i("LPAUtils", "Disabling active profile ${it.iccid}")
+        Log.i(TAG, "Disabling active profile ${it.iccid}")
         disableProfile(it.iccid, refresh)
     } ?: true
 
@@ -40,4 +44,30 @@ fun LocalProfileAssistant.disableActiveProfileWithUndo(refreshOnDisable: Boolean
     profiles.find { it.isEnabled }?.let {
         disableProfile(it.iccid, refreshOnDisable)
         return { enableProfile(it.iccid) }
-    } ?: { }
+    } ?: { }
+
+/**
+ * Begin a "tracked" operation where notifications may be generated by the eSIM
+ * Automatically handle any newly generated notification during the operation
+ * if the function "op" returns true.
+ */
+suspend fun LocalProfileAssistant.beginTrackedOperation(op: suspend () -> Boolean) =
+    withContext(Dispatchers.IO) {
+        val latestSeq = notifications.firstOrNull()?.seqNumber ?: 0
+        Log.d(TAG, "Latest notification is $latestSeq before operation")
+        if (op()) {
+            Log.d(TAG, "Operation has requested notification handling")
+            try {
+                // Note that the exact instance of "channel" might have changed here if reconnected;
+                // so we MUST use the automatic getter for "channel"
+                notifications.filter { it.seqNumber > latestSeq }.forEach {
+                    Log.d(TAG, "Handling notification $it")
+                    handleNotification(it.seqNumber)
+                }
+            } catch (e: Exception) {
+                // Ignore any error during notification handling
+                e.printStackTrace()
+            }
+        }
+        Log.d(TAG, "Operation complete")
+    }