|
| 1 | +package im.angry.openeuicc.ui |
| 2 | + |
| 3 | +import android.graphics.Typeface |
| 4 | +import android.os.Bundle |
| 5 | +import android.text.Editable |
| 6 | +import android.util.Log |
| 7 | +import android.widget.EditText |
| 8 | +import android.widget.Toast |
| 9 | +import androidx.appcompat.app.AlertDialog |
| 10 | +import androidx.fragment.app.DialogFragment |
| 11 | +import androidx.fragment.app.Fragment |
| 12 | +import androidx.lifecycle.lifecycleScope |
| 13 | +import im.angry.openeuicc.common.R |
| 14 | +import im.angry.openeuicc.service.EuiccChannelManagerService.Companion.waitDone |
| 15 | +import im.angry.openeuicc.util.EuiccChannelFragmentMarker |
| 16 | +import im.angry.openeuicc.util.EuiccProfilesChangedListener |
| 17 | +import im.angry.openeuicc.util.ensureEuiccChannelManager |
| 18 | +import im.angry.openeuicc.util.euiccChannelManagerService |
| 19 | +import im.angry.openeuicc.util.newInstanceEuicc |
| 20 | +import im.angry.openeuicc.util.notifyEuiccProfilesChanged |
| 21 | +import im.angry.openeuicc.util.portId |
| 22 | +import im.angry.openeuicc.util.slotId |
| 23 | +import kotlinx.coroutines.flow.onStart |
| 24 | +import kotlinx.coroutines.launch |
| 25 | + |
| 26 | +class EuiccMemoryResetFragment : DialogFragment(), EuiccChannelFragmentMarker { |
| 27 | + companion object { |
| 28 | + const val TAG = "EuiccMemoryResetFragment" |
| 29 | + |
| 30 | + private const val FIELD_EID = "eid" |
| 31 | + |
| 32 | + fun newInstance(slotId: Int, portId: Int, eid: String) = |
| 33 | + newInstanceEuicc(EuiccMemoryResetFragment::class.java, slotId, portId) { |
| 34 | + putString(FIELD_EID, eid) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private val eid: String by lazy { requireArguments().getString(FIELD_EID)!! } |
| 39 | + |
| 40 | + private val confirmText: String by lazy { |
| 41 | + getString(R.string.euicc_memory_reset_confirm_text, eid.takeLast(8)) |
| 42 | + } |
| 43 | + |
| 44 | + private inline val isMatched: Boolean |
| 45 | + get() = editText.text.toString() == confirmText |
| 46 | + |
| 47 | + private var confirmed = false |
| 48 | + |
| 49 | + private var toast: Toast? = null |
| 50 | + set(value) { |
| 51 | + toast?.cancel() |
| 52 | + field = value |
| 53 | + value?.show() |
| 54 | + } |
| 55 | + |
| 56 | + private val editText by lazy { |
| 57 | + EditText(requireContext()).apply { |
| 58 | + isLongClickable = false |
| 59 | + typeface = Typeface.MONOSPACE |
| 60 | + hint = Editable.Factory.getInstance() |
| 61 | + .newEditable(getString(R.string.euicc_memory_reset_hint_text, confirmText)) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private inline val alertDialog: AlertDialog |
| 66 | + get() = requireDialog() as AlertDialog |
| 67 | + |
| 68 | + override fun onCreateDialog(savedInstanceState: Bundle?) = |
| 69 | + AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme) |
| 70 | + .setTitle(R.string.euicc_memory_reset_title) |
| 71 | + .setMessage(getString(R.string.euicc_memory_reset_message, eid, confirmText)) |
| 72 | + .setView(editText) |
| 73 | + // Set listener to null to prevent auto closing |
| 74 | + .setNegativeButton(android.R.string.cancel, null) |
| 75 | + .setPositiveButton(R.string.euicc_memory_reset_invoke_button, null) |
| 76 | + .create() |
| 77 | + |
| 78 | + override fun onResume() { |
| 79 | + super.onResume() |
| 80 | + alertDialog.setCanceledOnTouchOutside(false) |
| 81 | + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE) |
| 82 | + .setOnClickListener { if (!confirmed) confirmation() } |
| 83 | + alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE) |
| 84 | + .setOnClickListener { if (!confirmed) dismiss() } |
| 85 | + } |
| 86 | + |
| 87 | + private fun confirmation() { |
| 88 | + toast?.cancel() |
| 89 | + if (!isMatched) { |
| 90 | + Log.d(TAG, buildString { |
| 91 | + appendLine("User input is mismatch:") |
| 92 | + appendLine(editText.text) |
| 93 | + appendLine(confirmText) |
| 94 | + }) |
| 95 | + val resId = R.string.toast_euicc_memory_reset_confirm_text_mismatched |
| 96 | + toast = Toast.makeText(requireContext(), resId, Toast.LENGTH_LONG) |
| 97 | + return |
| 98 | + } |
| 99 | + confirmed = true |
| 100 | + preventUserAction() |
| 101 | + |
| 102 | + requireParentFragment().lifecycleScope.launch { |
| 103 | + ensureEuiccChannelManager() |
| 104 | + euiccChannelManagerService.waitForForegroundTask() |
| 105 | + |
| 106 | + euiccChannelManagerService.launchMemoryReset(slotId, portId) |
| 107 | + .onStart { |
| 108 | + parentFragment?.notifyEuiccProfilesChanged() |
| 109 | + |
| 110 | + val resId = R.string.toast_euicc_memory_reset_finitshed |
| 111 | + toast = Toast.makeText(requireContext(), resId, Toast.LENGTH_LONG) |
| 112 | + |
| 113 | + runCatching(::dismiss) |
| 114 | + } |
| 115 | + .waitDone() |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private fun preventUserAction() { |
| 120 | + editText.isEnabled = false |
| 121 | + alertDialog.setCancelable(false) |
| 122 | + alertDialog.setCanceledOnTouchOutside(false) |
| 123 | + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false |
| 124 | + alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).isEnabled = false |
| 125 | + } |
| 126 | +} |
0 commit comments