浏览代码

Remove more EuiccChannel usage in PrivilegedEuiccManagementFragment

Peter Cai 1 年之前
父节点
当前提交
3d4704e77b

+ 22 - 18
app-common/src/main/java/im/angry/openeuicc/ui/EuiccManagementFragment.kt

@@ -150,32 +150,36 @@ open class EuiccManagementFragment : Fragment(), EuiccProfilesChangedListener,
             listOf()
         }
 
-    @SuppressLint("NotifyDataSetChanged")
     private fun refresh() {
         if (invalid) return
         swipeRefresh.isRefreshing = true
 
         lifecycleScope.launch {
-            ensureEuiccChannelManager()
-            euiccChannelManagerService.waitForForegroundTask()
+            doRefresh()
+        }
+    }
 
-            if (!this@EuiccManagementFragment::disableSafeguardFlow.isInitialized) {
-                disableSafeguardFlow =
-                    preferenceRepository.disableSafeguardFlow.stateIn(lifecycleScope)
-            }
+    @SuppressLint("NotifyDataSetChanged")
+    protected open suspend fun doRefresh() {
+        ensureEuiccChannelManager()
+        euiccChannelManagerService.waitForForegroundTask()
 
-            val profiles = withEuiccChannel { channel ->
-                logicalSlotId = channel.logicalSlotId
-                euiccChannelManager.notifyEuiccProfilesChanged(channel.logicalSlotId)
-                channel.lpa.profiles.operational
-            }
+        if (!this@EuiccManagementFragment::disableSafeguardFlow.isInitialized) {
+            disableSafeguardFlow =
+                preferenceRepository.disableSafeguardFlow.stateIn(lifecycleScope)
+        }
 
-            withContext(Dispatchers.Main) {
-                adapter.profiles = profiles
-                adapter.footerViews = onCreateFooterViews(profileList, profiles)
-                adapter.notifyDataSetChanged()
-                swipeRefresh.isRefreshing = false
-            }
+        val profiles = withEuiccChannel { channel ->
+            logicalSlotId = channel.logicalSlotId
+            euiccChannelManager.notifyEuiccProfilesChanged(channel.logicalSlotId)
+            channel.lpa.profiles.operational
+        }
+
+        withContext(Dispatchers.Main) {
+            adapter.profiles = profiles
+            adapter.footerViews = onCreateFooterViews(profileList, profiles)
+            adapter.notifyDataSetChanged()
+            swipeRefresh.isRefreshing = false
         }
     }
 

+ 13 - 6
app/src/main/java/im/angry/openeuicc/ui/PrivilegedEuiccManagementFragment.kt

@@ -6,8 +6,6 @@ import android.widget.Button
 import android.widget.PopupMenu
 import im.angry.openeuicc.R
 import im.angry.openeuicc.util.*
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.withContext
 import net.typeblog.lpac_jni.LocalProfileInfo
 
 class PrivilegedEuiccManagementFragment: EuiccManagementFragment() {
@@ -16,14 +14,23 @@ class PrivilegedEuiccManagementFragment: EuiccManagementFragment() {
             newInstanceEuicc(PrivilegedEuiccManagementFragment::class.java, slotId, portId)
     }
 
+    private var isMEP = false
+    private var isRemovable = false
+
+    override suspend fun doRefresh() {
+        super.doRefresh()
+        withEuiccChannel { channel ->
+            isMEP = channel.isMEP
+            isRemovable = channel.removable
+        }
+    }
+
     override suspend fun onCreateFooterViews(
         parent: ViewGroup,
         profiles: List<LocalProfileInfo>
     ): List<View> =
         super.onCreateFooterViews(parent, profiles).let { footers ->
-            // isMEP can map to a slow operation (UiccCardInfo.isMultipleEnabledProfilesSupported())
-            // so let's do it in the IO context
-            if (withContext(Dispatchers.IO) { channel.isMEP }) {
+            if (isMEP) {
                 val view = layoutInflater.inflate(R.layout.footer_mep, parent, false)
                 view.requireViewById<Button>(R.id.footer_mep_slot_mapping).setOnClickListener {
                     (requireActivity() as PrivilegedMainActivity).showSlotMappingFragment()
@@ -36,7 +43,7 @@ class PrivilegedEuiccManagementFragment: EuiccManagementFragment() {
 
     override fun populatePopupWithProfileActions(popup: PopupMenu, profile: LocalProfileInfo) {
         super.populatePopupWithProfileActions(popup, profile)
-        if (profile.isEnabled && !channel.removable) {
+        if (profile.isEnabled && !isRemovable) {
             // Only show the disable option for non-removable eUICCs
             // Some devices without internal eUICCs have the "optimization" of ignoring SIM
             // slots without a valid profile. This can lead to "bricking" of external eUICCs