ProfileDeleteFragment.kt 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package im.angry.openeuicc.ui
  2. import android.app.Dialog
  3. import android.os.Bundle
  4. import android.util.Log
  5. import androidx.appcompat.app.AlertDialog
  6. import androidx.fragment.app.DialogFragment
  7. import androidx.lifecycle.lifecycleScope
  8. import im.angry.openeuicc.common.R
  9. import im.angry.openeuicc.util.preferenceRepository
  10. import kotlinx.coroutines.Dispatchers
  11. import kotlinx.coroutines.flow.first
  12. import kotlinx.coroutines.launch
  13. import kotlinx.coroutines.withContext
  14. import net.typeblog.lpac_jni.LocalProfileNotification
  15. import java.lang.Exception
  16. class ProfileDeleteFragment : DialogFragment(), EuiccFragmentMarker {
  17. companion object {
  18. const val TAG = "ProfileDeleteFragment"
  19. fun newInstance(slotId: Int, portId: Int, iccid: String, name: String): ProfileDeleteFragment {
  20. val instance = newInstanceEuicc(ProfileDeleteFragment::class.java, slotId, portId)
  21. instance.requireArguments().apply {
  22. putString("iccid", iccid)
  23. putString("name", name)
  24. }
  25. return instance
  26. }
  27. }
  28. private var deleting = false
  29. override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
  30. return AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme).apply {
  31. setMessage(getString(R.string.profile_delete_confirm, requireArguments().getString("name")))
  32. setPositiveButton(android.R.string.ok, null) // Set listener to null to prevent auto closing
  33. setNegativeButton(android.R.string.cancel, null)
  34. }.create()
  35. }
  36. override fun onResume() {
  37. super.onResume()
  38. val alertDialog = dialog!! as AlertDialog
  39. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
  40. if (!deleting) delete()
  41. }
  42. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener {
  43. if (!deleting) dismiss()
  44. }
  45. }
  46. private fun delete() {
  47. deleting = true
  48. val alertDialog = dialog!! as AlertDialog
  49. alertDialog.setCanceledOnTouchOutside(false)
  50. alertDialog.setCancelable(false)
  51. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false
  52. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).isEnabled = false
  53. lifecycleScope.launch {
  54. try {
  55. doDelete()
  56. } catch (e: Exception) {
  57. Log.d(ProfileDownloadFragment.TAG, "Error deleting profile")
  58. Log.d(ProfileDownloadFragment.TAG, Log.getStackTraceString(e))
  59. } finally {
  60. if (parentFragment is EuiccProfilesChangedListener) {
  61. (parentFragment as EuiccProfilesChangedListener).onEuiccProfilesChanged()
  62. }
  63. dismiss()
  64. }
  65. }
  66. }
  67. private suspend fun doDelete() = channel.lpa.beginOperation {
  68. channel.lpa.deleteProfile(requireArguments().getString("iccid")!!)
  69. preferenceRepository.notificationDeleteFlow.first()
  70. }
  71. }