chore: improve readability #95

Merged
PeterCxy merged 7 commits from septs/OpenEUICC:improve-readability into master 2024-12-08 18:51:50 +01:00
6 changed files with 41 additions and 36 deletions

View file

@ -66,8 +66,9 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
private val usbReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == UsbManager.ACTION_USB_DEVICE_ATTACHED || intent?.action == UsbManager.ACTION_USB_DEVICE_DETACHED) {
refresh(true)
when (intent?.action) {
UsbManager.ACTION_USB_DEVICE_ATTACHED -> refresh(fromUsbEvent = true)
UsbManager.ACTION_USB_DEVICE_DETACHED -> refresh(fromUsbEvent = true)
}
}
}
@ -126,10 +127,10 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
}
private fun ensureNotificationPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissions(
arrayOf(android.Manifest.permission.POST_NOTIFICATIONS),
PERMISSION_REQUEST_CODE
PERMISSION_REQUEST_CODE,
android.Manifest.permission.POST_NOTIFICATIONS
)
}
}
@ -160,38 +161,29 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
// but it could change in the future
euiccChannelManager.notifyEuiccProfilesChanged(channel.logicalSlotId)
newPages.add(
Page(
channel.logicalSlotId,
getString(R.string.channel_name_format, channel.logicalSlotId)
) {
appContainer.uiComponentFactory.createEuiccManagementFragment(
slotId,
portId
)
})
val channelName = getString(R.string.channel_name_format, channel.logicalSlotId)
newPages.add(Page(channel.logicalSlotId, channelName) {
appContainer.uiComponentFactory.createEuiccManagementFragment(slotId, portId)
})
}
}.collect()
// If USB readers exist, add them at the very last
// We use a wrapper fragment to handle logic specific to USB readers
usbDevice?.let {
newPages.add(
Page(
EuiccChannelManager.USB_CHANNEL_ID,
it.productName ?: getString(R.string.usb)
) { UsbCcidReaderFragment() })
val productName = it.productName ?: getString(R.string.usb)
newPages.add(Page(EuiccChannelManager.USB_CHANNEL_ID, productName) {
UsbCcidReaderFragment()
})
}
viewPager.visibility = View.VISIBLE
if (newPages.size > 1) {
tabs.visibility = View.VISIBLE
} else if (newPages.isEmpty()) {
newPages.add(
Page(
-1,
""
) { appContainer.uiComponentFactory.createNoEuiccPlaceholderFragment() })
newPages.add(Page(-1, "") {
appContainer.uiComponentFactory.createNoEuiccPlaceholderFragment()
})
}
newPages.sortBy { it.logicalSlotId }

View file

@ -39,12 +39,13 @@ class ProfileDeleteFragment : DialogFragment(), EuiccChannelFragmentMarker {
private var deleting = false
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme).apply {
AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme).apply {
setMessage(getString(R.string.profile_delete_confirm, requireArguments().getString("name")))
setView(editText)
setPositiveButton(android.R.string.ok, null) // Set listener to null to prevent auto closing
setNegativeButton(android.R.string.cancel, null)
}.create()
return create()
}
}
override fun onResume() {

View file

@ -120,7 +120,7 @@ class UsbCcidReaderFragment : Fragment(), OpenEuiccContextMarker {
try {
requireContext().unregisterReceiver(usbPermissionReceiver)
} catch (_: Exception) {
// ignore
}
}
@ -129,7 +129,7 @@ class UsbCcidReaderFragment : Fragment(), OpenEuiccContextMarker {
try {
requireContext().unregisterReceiver(usbPermissionReceiver)
} catch (_: Exception) {
// ignore
}
}
@ -155,8 +155,8 @@ class UsbCcidReaderFragment : Fragment(), OpenEuiccContextMarker {
replace(
R.id.child_container,
appContainer.uiComponentFactory.createEuiccManagementFragment(
EuiccChannelManager.USB_CHANNEL_ID,
0
slotId = EuiccChannelManager.USB_CHANNEL_ID,
portId = 0
)
)
}

View file

@ -0,0 +1,13 @@
package im.angry.openeuicc.util
import android.app.Activity
import android.content.pm.PackageManager
fun Activity.requestPermissions(requestCode: Int, vararg permissions: String) {
val deniedPermissions = permissions.filter { name ->
checkSelfPermission(name) == PackageManager.PERMISSION_DENIED
}
if (deniedPermissions.isNotEmpty()) {
requestPermissions(deniedPermissions.toTypedArray(), requestCode)
}
}

View file

@ -110,7 +110,7 @@ class OpenEuiccService : EuiccService(), OpenEuiccContextMarker {
telephonyManager.simSlotMapping = mappings
return
} catch (_: Exception) {
// ignore
}
// Sometimes hardware supports one ordering but not the reverse

View file

@ -16,10 +16,9 @@ class PrivilegedMainActivity : MainActivity() {
menu.findItem(R.id.slot_mapping).isVisible = false
}
if (tm.supportsDSDS) {
val dsds = menu.findItem(R.id.dsds)
dsds.isVisible = true
dsds.isChecked = tm.dsdsEnabled
menu.findItem(R.id.dsds).apply {
isVisible = tm.supportsDSDS
isChecked = tm.dsdsEnabled
}
return true