Compare commits

..

2 commits

Author SHA1 Message Date
3b868e4f9a Move some fragments to withEuiccChannel()
All checks were successful
/ build-debug (push) Successful in 4m26s
2024-10-26 21:41:56 -04:00
95b24e6151 Add withEuiccChannel helper for EuiccChannelFragment 2024-10-26 21:29:09 -04:00
5 changed files with 42 additions and 22 deletions

View file

@ -162,13 +162,15 @@ open class DefaultEuiccChannelManager(
override suspend fun <R> withEuiccChannel( override suspend fun <R> withEuiccChannel(
physicalSlotId: Int, physicalSlotId: Int,
portId: Int, portId: Int,
fn: (EuiccChannel) -> R fn: suspend (EuiccChannel) -> R
): R { ): R {
val channel = findEuiccChannelByPortBlocking(physicalSlotId, portId) val channel = findEuiccChannelByPortBlocking(physicalSlotId, portId)
?: throw EuiccChannelManager.EuiccChannelNotFoundException() ?: throw EuiccChannelManager.EuiccChannelNotFoundException()
val wrapper = EuiccChannelWrapper(channel) val wrapper = EuiccChannelWrapper(channel)
try { try {
return fn(wrapper) return withContext(Dispatchers.IO) {
fn(wrapper)
}
} finally { } finally {
wrapper.invalidateWrapper() wrapper.invalidateWrapper()
} }

View file

@ -71,9 +71,15 @@ interface EuiccChannelManager {
* The reference is not supposed to be held outside of the callback. This is enforced via * The reference is not supposed to be held outside of the callback. This is enforced via
* a wrapper object. * a wrapper object.
* *
* The callback is run on Dispatchers.IO by default.
*
* 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, fn: (EuiccChannel) -> R): R suspend fun <R> withEuiccChannel(
physicalSlotId: Int,
portId: Int,
fn: suspend (EuiccChannel) -> R
): R
/** /**
* Invalidate all EuiccChannels previously cached by this Manager * Invalidate all EuiccChannels previously cached by this Manager

View file

@ -6,7 +6,6 @@ import android.content.ClipboardManager
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.text.method.PasswordTransformationMethod import android.text.method.PasswordTransformationMethod
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
@ -52,6 +51,7 @@ open class EuiccManagementFragment : Fragment(), EuiccProfilesChangedListener,
private lateinit var swipeRefresh: SwipeRefreshLayout private lateinit var swipeRefresh: SwipeRefreshLayout
private lateinit var fab: FloatingActionButton private lateinit var fab: FloatingActionButton
private lateinit var profileList: RecyclerView private lateinit var profileList: RecyclerView
private var logicalSlotId: Int = -1
private val adapter = EuiccProfileAdapter() private val adapter = EuiccProfileAdapter()
@ -127,9 +127,11 @@ open class EuiccManagementFragment : Fragment(), EuiccProfilesChangedListener,
override fun onOptionsItemSelected(item: MenuItem): Boolean = override fun onOptionsItemSelected(item: MenuItem): Boolean =
when (item.itemId) { when (item.itemId) {
R.id.show_notifications -> { R.id.show_notifications -> {
Intent(requireContext(), NotificationsActivity::class.java).apply { if (logicalSlotId != -1) {
putExtra("logicalSlotId", channel.logicalSlotId) Intent(requireContext(), NotificationsActivity::class.java).apply {
startActivity(this) putExtra("logicalSlotId", logicalSlotId)
startActivity(this)
}
} }
true true
} }
@ -162,7 +164,8 @@ open class EuiccManagementFragment : Fragment(), EuiccProfilesChangedListener,
preferenceRepository.disableSafeguardFlow.stateIn(lifecycleScope) preferenceRepository.disableSafeguardFlow.stateIn(lifecycleScope)
} }
val profiles = withContext(Dispatchers.IO) { val profiles = withEuiccChannel { channel ->
logicalSlotId = channel.logicalSlotId
euiccChannelManager.notifyEuiccProfilesChanged(channel.logicalSlotId) euiccChannelManager.notifyEuiccProfilesChanged(channel.logicalSlotId)
channel.lpa.profiles.operational channel.lpa.profiles.operational
} }

View file

@ -159,22 +159,26 @@ class ProfileDownloadFragment : BaseMaterialDialogFragment(),
return@launch return@launch
} }
val imei = try { withEuiccChannel { channel ->
telephonyManager.getImei(channel.logicalSlotId) ?: "" val imei = try {
} catch (e: Exception) { telephonyManager.getImei(channel.logicalSlotId) ?: ""
"" } catch (e: Exception) {
} ""
}
// Fetch remaining NVRAM // Fetch remaining NVRAM
val str = channel.lpa.euiccInfo2?.freeNvram?.also { val str = channel.lpa.euiccInfo2?.freeNvram?.also {
freeNvram = it freeNvram = it
}?.let { formatFreeSpace(it) } }?.let { formatFreeSpace(it) }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
profileDownloadFreeSpace.text = getString(R.string.profile_download_free_space, profileDownloadFreeSpace.text = getString(
str ?: getText(R.string.unknown)) R.string.profile_download_free_space,
profileDownloadIMEI.editText!!.text = str ?: getText(R.string.unknown)
Editable.Factory.getInstance().newEditable(imei) )
profileDownloadIMEI.editText!!.text =
Editable.Factory.getInstance().newEditable(imei)
}
} }
} }
} }

View file

@ -40,6 +40,11 @@ val <T> T.channel: EuiccChannel where T: Fragment, T: EuiccChannelFragmentMarker
get() = get() =
euiccChannelManager.findEuiccChannelByPortBlocking(slotId, portId)!! euiccChannelManager.findEuiccChannelByPortBlocking(slotId, portId)!!
suspend fun <T, R> T.withEuiccChannel(fn: suspend (EuiccChannel) -> R): R where T : Fragment, T : EuiccChannelFragmentMarker {
ensureEuiccChannelManager()
return euiccChannelManager.withEuiccChannel(slotId, portId, fn)
}
suspend fun <T> T.ensureEuiccChannelManager() where T: Fragment, T: EuiccChannelFragmentMarker = suspend fun <T> T.ensureEuiccChannelManager() where T: Fragment, T: EuiccChannelFragmentMarker =
(requireActivity() as BaseEuiccAccessActivity).euiccChannelManagerLoaded.await() (requireActivity() as BaseEuiccAccessActivity).euiccChannelManagerLoaded.await()