瀏覽代碼

Implement profile deletion

Peter Cai 3 年之前
父節點
當前提交
3c88abfd02

+ 6 - 0
app/src/main/java/im/angry/openeuicc/ui/EuiccManagementFragment.kt

@@ -158,6 +158,7 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
                 inflate(R.menu.profile_options)
                 if (isEnabled()) {
                     menu.findItem(R.id.enable).isVisible = false
+                    menu.findItem(R.id.delete).isVisible = false
                 }
                 show()
             }
@@ -174,6 +175,11 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
                         .show(childFragmentManager, ProfileRenameFragment.TAG)
                     true
                 }
+                R.id.delete -> {
+                    ProfileDeleteFragment.newInstance(slotId, profile[ICCID.name]!!, getName())
+                        .show(childFragmentManager, ProfileDeleteFragment.TAG)
+                    true
+                }
                 else -> false
             }
     }

+ 77 - 0
app/src/main/java/im/angry/openeuicc/ui/ProfileDeleteFragment.kt

@@ -0,0 +1,77 @@
+package im.angry.openeuicc.ui
+
+import android.app.Dialog
+import android.os.Bundle
+import android.util.Log
+import androidx.appcompat.app.AlertDialog
+import androidx.fragment.app.DialogFragment
+import androidx.lifecycle.lifecycleScope
+import com.truphone.lpad.progress.Progress
+import im.angry.openeuicc.R
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+import java.lang.Exception
+
+class ProfileDeleteFragment : DialogFragment(), EuiccFragmentMarker {
+    companion object {
+        const val TAG = "ProfileDeleteFragment"
+
+        fun newInstance(slotId: Int, iccid: String, name: String): ProfileDeleteFragment {
+            val instance = newInstanceEuicc(ProfileDeleteFragment::class.java, slotId)
+            instance.requireArguments().apply {
+                putString("iccid", iccid)
+                putString("name", name)
+            }
+            return instance
+        }
+    }
+
+    private var deleting = false
+
+    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
+        return AlertDialog.Builder(requireContext()).apply {
+            setMessage(getString(R.string.profile_delete_confirm, requireArguments().getString("name")))
+            setPositiveButton(android.R.string.ok, null) // Set listener to null to prevent auto closing
+            setNegativeButton(android.R.string.cancel, null)
+        }.create()
+    }
+
+    override fun onResume() {
+        super.onResume()
+        val alertDialog = dialog!! as AlertDialog
+        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
+            if (!deleting) delete()
+        }
+        alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener {
+            if (!deleting) dismiss()
+        }
+    }
+
+    private fun delete() {
+        deleting = true
+        val alertDialog = dialog!! as AlertDialog
+        alertDialog.setCanceledOnTouchOutside(false)
+        alertDialog.setCancelable(false)
+        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false
+        alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).isEnabled = false
+
+        lifecycleScope.launch {
+            try {
+                doDelete()
+            } catch (e: Exception) {
+                Log.d(ProfileDownloadFragment.TAG, "Error deleting profile")
+                Log.d(ProfileDownloadFragment.TAG, Log.getStackTraceString(e))
+            } finally {
+                if (parentFragment is EuiccProfilesChangedListener) {
+                    (parentFragment as EuiccProfilesChangedListener).onEuiccProfilesChanged()
+                }
+                dismiss()
+            }
+        }
+    }
+
+    private suspend fun doDelete() = withContext(Dispatchers.IO) {
+        channel.lpa.deleteProfile(requireArguments().getString("iccid"), Progress())
+    }
+}

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

@@ -24,4 +24,6 @@
     <string name="profile_download_failed">Failed to download eSIM. Check your activation / QR code.</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>
 </resources>

+ 15 - 0
app/src/main/res/values/themes.xml

@@ -13,6 +13,7 @@
         <!-- Status bar color. -->
         <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
         <!-- Customize your theme here. -->
+        <item name="alertDialogTheme">@style/AlertDialogTheme</item>
     </style>
 
     <style name="Theme.OpenEUICC.Input.Cursor" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
@@ -25,5 +26,19 @@
         <item name="hintTextColor">?attr/colorSecondary</item>
     </style>
 
+    <style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
+        <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
+        <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
+    </style>
+
+    <style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
+        <item name="android:textColor">?attr/colorSecondary</item>
+    </style>
+
+    <style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
+        <item name="android:textColor">?attr/colorSecondary</item>
+    </style>
+
+
 
 </resources>