Browse Source

Implement profile disabling

Because we cannot directly delete an active profile
Peter Cai 3 years ago
parent
commit
331137a5ee

+ 19 - 4
app/src/main/java/im/angry/openeuicc/ui/EuiccManagementFragment.kt

@@ -85,19 +85,23 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
         }
         }
     }
     }
 
 
-    private fun enableProfile(iccid: String) {
+    private fun enableOrDisableProfile(iccid: String, enable: Boolean) {
         binding.swipeRefresh.isRefreshing = true
         binding.swipeRefresh.isRefreshing = true
         binding.swipeRefresh.isEnabled = false
         binding.swipeRefresh.isEnabled = false
         binding.fab.isEnabled = false
         binding.fab.isEnabled = false
 
 
         lifecycleScope.launch {
         lifecycleScope.launch {
             try {
             try {
-                doEnableProfile(iccid)
+                if (enable) {
+                    doEnableProfile(iccid)
+                } else {
+                    doDisableProfile(iccid)
+                }
                 Toast.makeText(context, R.string.toast_profile_enabled, Toast.LENGTH_LONG).show()
                 Toast.makeText(context, R.string.toast_profile_enabled, Toast.LENGTH_LONG).show()
                 // The APDU channel will be invalid when the SIM reboots. For now, just exit the app
                 // The APDU channel will be invalid when the SIM reboots. For now, just exit the app
                 requireActivity().finish()
                 requireActivity().finish()
             } catch (e: Exception) {
             } catch (e: Exception) {
-                Log.d(TAG, "Failed to enable profile $iccid")
+                Log.d(TAG, "Failed to enable / disable profile $iccid")
                 Log.d(TAG, Log.getStackTraceString(e))
                 Log.d(TAG, Log.getStackTraceString(e))
                 binding.fab.isEnabled = true
                 binding.fab.isEnabled = true
                 binding.swipeRefresh.isEnabled = true
                 binding.swipeRefresh.isEnabled = true
@@ -111,6 +115,11 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
             channel.lpa.enableProfile(iccid, Progress())
             channel.lpa.enableProfile(iccid, Progress())
         }
         }
 
 
+    private suspend fun doDisableProfile(iccid: String) =
+        withContext(Dispatchers.IO) {
+            channel.lpa.disableProfile(iccid, Progress())
+        }
+
     inner class ViewHolder(private val binding: EuiccProfileBinding) : RecyclerView.ViewHolder(binding.root) {
     inner class ViewHolder(private val binding: EuiccProfileBinding) : RecyclerView.ViewHolder(binding.root) {
         init {
         init {
             binding.iccid.setOnClickListener {
             binding.iccid.setOnClickListener {
@@ -159,6 +168,8 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
                 if (isEnabled()) {
                 if (isEnabled()) {
                     menu.findItem(R.id.enable).isVisible = false
                     menu.findItem(R.id.enable).isVisible = false
                     menu.findItem(R.id.delete).isVisible = false
                     menu.findItem(R.id.delete).isVisible = false
+                } else {
+                    menu.findItem(R.id.disable).isVisible = false
                 }
                 }
                 show()
                 show()
             }
             }
@@ -167,7 +178,11 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
         private fun onMenuItemClicked(item: MenuItem): Boolean =
         private fun onMenuItemClicked(item: MenuItem): Boolean =
             when (item.itemId) {
             when (item.itemId) {
                 R.id.enable -> {
                 R.id.enable -> {
-                    enableProfile(profile[ICCID.name]!!)
+                    enableOrDisableProfile(profile[ICCID.name]!!, true)
+                    true
+                }
+                R.id.disable -> {
+                    enableOrDisableProfile(profile[ICCID.name]!!, false)
                     true
                     true
                 }
                 }
                 R.id.rename -> {
                 R.id.rename -> {

+ 4 - 0
app/src/main/res/menu/profile_options.xml

@@ -4,6 +4,10 @@
         android:id="@+id/enable"
         android:id="@+id/enable"
         android:title="@string/enable"/>
         android:title="@string/enable"/>
 
 
+    <item
+        android:id="@+id/disable"
+        android:title="@string/disable"/>
+
     <item
     <item
         android:id="@+id/rename"
         android:id="@+id/rename"
         android:title="@string/rename"/>
         android:title="@string/rename"/>

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

@@ -9,6 +9,7 @@
     <string name="iccid">ICCID:</string>
     <string name="iccid">ICCID:</string>
 
 
     <string name="enable">Enable</string>
     <string name="enable">Enable</string>
+    <string name="disable">Disable</string>
     <string name="delete">Delete</string>
     <string name="delete">Delete</string>
     <string name="rename">Rename</string>
     <string name="rename">Rename</string>