Browse Source

feat: low nvram alert (#62)

If free nvram available is less 30 kib, then alert dialog

Reviewed-on: https://gitea.angry.im/PeterCxy/OpenEUICC/pulls/62
Co-authored-by: septs <github@septs.pw>
Co-committed-by: septs <github@septs.pw>
septs 1 year ago
parent
commit
506b0e530a

+ 18 - 2
app-common/src/main/java/im/angry/openeuicc/ui/ProfileDownloadFragment.kt

@@ -1,6 +1,7 @@
 package im.angry.openeuicc.ui
 
 import android.annotation.SuppressLint
+import android.app.AlertDialog
 import android.app.Dialog
 import android.content.DialogInterface
 import android.graphics.BitmapFactory
@@ -25,13 +26,14 @@ import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
-import kotlin.Exception
 
 class ProfileDownloadFragment : BaseMaterialDialogFragment(),
     Toolbar.OnMenuItemClickListener, EuiccChannelFragmentMarker {
     companion object {
         const val TAG = "ProfileDownloadFragment"
 
+        const val LOW_NVRAM_THRESHOLD = 30 * 1024 // < 30 KiB, the alert may fail
+
         fun newInstance(slotId: Int, portId: Int, finishWhenDone: Boolean = false): ProfileDownloadFragment =
             newInstanceEuicc(ProfileDownloadFragment::class.java, slotId, portId) {
                 putBoolean("finishWhenDone", finishWhenDone)
@@ -135,7 +137,21 @@ class ProfileDownloadFragment : BaseMaterialDialogFragment(),
                 true
             }
             R.id.ok -> {
-                startDownloadProfile()
+                if (freeNvram > LOW_NVRAM_THRESHOLD) {
+                    startDownloadProfile()
+                } else {
+                    AlertDialog.Builder(requireContext()).apply {
+                        setTitle(R.string.profile_download_low_nvram_title)
+                        setMessage(R.string.profile_download_low_nvram_message)
+                        setIcon(android.R.drawable.ic_dialog_alert)
+                        setCancelable(true)
+                        setPositiveButton(android.R.string.ok) { _, _ ->
+                            startDownloadProfile()
+                        }
+                        setNegativeButton(android.R.string.cancel, null)
+                        show()
+                    }
+                }
                 true
             }
             else -> false

+ 3 - 0
app-common/src/main/res/values/strings.xml

@@ -55,6 +55,9 @@
     <string name="profile_download_ok">Download</string>
     <string name="profile_download_failed">Failed to download eSIM. Check your activation / QR code.</string>
 
+    <string name="profile_download_low_nvram_title">This download may fail</string>
+    <string name="profile_download_low_nvram_message">This download may fail due to low remaining capacity.</string>
+
     <string name="profile_rename_new_name">New nickname</string>
 
     <string name="profile_delete_confirm">Are you sure you want to delete the profile %s? This operation is irreversible.</string>