瀏覽代碼

Move stale TM channel clean-up to EuiccChannelManager

Peter Cai 3 年之前
父節點
當前提交
6b1056d907

+ 1 - 13
app/src/main/java/im/angry/openeuicc/OpenEuiccApplication.kt

@@ -4,8 +4,6 @@ import android.app.Application
 import android.telephony.SubscriptionManager
 import android.telephony.SubscriptionManager
 import android.telephony.TelephonyManager
 import android.telephony.TelephonyManager
 import im.angry.openeuicc.core.EuiccChannelManager
 import im.angry.openeuicc.core.EuiccChannelManager
-import im.angry.openeuicc.util.*
-import java.lang.Exception
 
 
 class OpenEuiccApplication : Application() {
 class OpenEuiccApplication : Application() {
     val telephonyManager by lazy {
     val telephonyManager by lazy {
@@ -22,16 +20,6 @@ class OpenEuiccApplication : Application() {
 
 
     override fun onCreate() {
     override fun onCreate() {
         super.onCreate()
         super.onCreate()
-        // Clean up channels left open in TelephonyManager
-        // due to a (potentially) forced restart
-        for (slotId in 0 until EuiccChannelManager.MAX_SIMS) {
-            for (channel in 0 until 10) {
-                try {
-                    telephonyManager.iccCloseLogicalChannelBySlot(slotId, channel)
-                } catch (_: Exception) {
-                    // We do not care
-                }
-            }
-        }
+        euiccChannelManager.closeAllStaleChannels()
     }
     }
 }
 }

+ 17 - 1
app/src/main/java/im/angry/openeuicc/core/EuiccChannelManager.kt

@@ -7,18 +7,19 @@ import android.se.omapi.SEService
 import android.telephony.UiccCardInfo
 import android.telephony.UiccCardInfo
 import android.util.Log
 import android.util.Log
 import im.angry.openeuicc.OpenEuiccApplication
 import im.angry.openeuicc.OpenEuiccApplication
+import im.angry.openeuicc.util.*
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.runBlocking
 import kotlinx.coroutines.runBlocking
 import kotlinx.coroutines.sync.Mutex
 import kotlinx.coroutines.sync.Mutex
 import kotlinx.coroutines.sync.withLock
 import kotlinx.coroutines.sync.withLock
 import kotlinx.coroutines.withContext
 import kotlinx.coroutines.withContext
+import java.lang.Exception
 import kotlin.coroutines.resume
 import kotlin.coroutines.resume
 import kotlin.coroutines.suspendCoroutine
 import kotlin.coroutines.suspendCoroutine
 
 
 class EuiccChannelManager(private val context: Context) {
 class EuiccChannelManager(private val context: Context) {
     companion object {
     companion object {
         const val TAG = "EuiccChannelManager"
         const val TAG = "EuiccChannelManager"
-        const val MAX_SIMS = 3
     }
     }
 
 
     private val channels = mutableListOf<EuiccChannel>()
     private val channels = mutableListOf<EuiccChannel>()
@@ -124,4 +125,19 @@ class EuiccChannelManager(private val context: Context) {
         seService?.shutdown()
         seService?.shutdown()
         seService = null
         seService = null
     }
     }
+
+    // Clean up channels left open in TelephonyManager
+    // due to a (potentially) forced restart
+    // This should be called every time the application is restarted
+    fun closeAllStaleChannels() {
+        for (card in tm.uiccCardsInfo) {
+            for (channel in 0 until 10) {
+                try {
+                    tm.iccCloseLogicalChannelBySlot(card.slotIndex, channel)
+                } catch (_: Exception) {
+                    // We do not care
+                }
+            }
+        }
+    }
 }
 }