From 4d37aa232fff711fa05800ac37ff42acfb2f4ce5 Mon Sep 17 00:00:00 2001
From: Florian Renaud <florianr@element.io>
Date: Mon, 28 Mar 2022 14:32:28 +0200
Subject: [PATCH] Replace "open settings" button by "disable" action in
 RageShake dialog if there is no session

---
 changelog.d/4445.bugfix                           |  1 +
 .../im/vector/app/features/rageshake/RageShake.kt | 15 ++++++++++++++-
 .../app/features/settings/VectorPreferences.kt    | 11 +++++++++++
 vector/src/main/res/values/strings.xml            |  1 +
 4 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 changelog.d/4445.bugfix

diff --git a/changelog.d/4445.bugfix b/changelog.d/4445.bugfix
new file mode 100644
index 0000000000..bf8859ff0f
--- /dev/null
+++ b/changelog.d/4445.bugfix
@@ -0,0 +1 @@
+Replace "open settings" button by "disable" action in RageShake dialog if there is no session
\ No newline at end of file
diff --git a/vector/src/main/java/im/vector/app/features/rageshake/RageShake.kt b/vector/src/main/java/im/vector/app/features/rageshake/RageShake.kt
index b4dcb07349..b5dd3f1ef0 100644
--- a/vector/src/main/java/im/vector/app/features/rageshake/RageShake.kt
+++ b/vector/src/main/java/im/vector/app/features/rageshake/RageShake.kt
@@ -24,6 +24,7 @@ import androidx.fragment.app.FragmentActivity
 import com.google.android.material.dialog.MaterialAlertDialogBuilder
 import com.squareup.seismic.ShakeDetector
 import im.vector.app.R
+import im.vector.app.core.di.ActiveSessionHolder
 import im.vector.app.core.hardware.vibrate
 import im.vector.app.features.navigation.Navigator
 import im.vector.app.features.settings.VectorPreferences
@@ -33,6 +34,7 @@ import javax.inject.Inject
 class RageShake @Inject constructor(private val activity: FragmentActivity,
                                     private val bugReporter: BugReporter,
                                     private val navigator: Navigator,
+                                    private val sessionHolder: ActiveSessionHolder,
                                     private val vectorPreferences: VectorPreferences) : ShakeDetector.Listener {
 
     private var shakeDetector: ShakeDetector? = null
@@ -75,7 +77,13 @@ class RageShake @Inject constructor(private val activity: FragmentActivity,
             MaterialAlertDialogBuilder(activity)
                     .setMessage(R.string.send_bug_report_alert_message)
                     .setPositiveButton(R.string.yes) { _, _ -> openBugReportScreen() }
-                    .setNeutralButton(R.string.settings) { _, _ -> openSettings() }
+                    .also {
+                        if (sessionHolder.hasActiveSession()) {
+                            it.setNeutralButton(R.string.settings) { _, _ -> openSettings() }
+                        } else {
+                            it.setNeutralButton(R.string.action_disable) { _, _ -> disableRageShake() }
+                        }
+                    }
                     .setOnDismissListener { dialogDisplayed = false }
                     .setNegativeButton(R.string.no, null)
                     .show()
@@ -90,6 +98,11 @@ class RageShake @Inject constructor(private val activity: FragmentActivity,
         navigator.openSettings(activity, VectorSettingsActivity.EXTRA_DIRECT_ACCESS_ADVANCED_SETTINGS)
     }
 
+    private fun disableRageShake() {
+        vectorPreferences.setRageshakeEnabled(false)
+        stop()
+    }
+
     companion object {
         /**
          * Check if the feature is available
diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt
index 8fce936428..74fcc0be5d 100755
--- a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt
+++ b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt
@@ -865,6 +865,17 @@ class VectorPreferences @Inject constructor(private val context: Context) {
         return defaultPrefs.getBoolean(SETTINGS_INTERFACE_BUBBLE_KEY, getDefault(R.bool.settings_interface_bubble_default))
     }
 
+    /**
+     * Update the rage shake enabled status.
+     *
+     * @param isEnabled true to enable rage shake.
+     */
+    fun setRageshakeEnabled(isEnabled: Boolean) {
+        defaultPrefs.edit {
+            putBoolean(SETTINGS_USE_RAGE_SHAKE_KEY, isEnabled)
+        }
+    }
+
     /**
      * Tells if the rage shake is used.
      *
diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml
index c585d574c1..9bdbb291a5 100644
--- a/vector/src/main/res/values/strings.xml
+++ b/vector/src/main/res/values/strings.xml
@@ -358,6 +358,7 @@
     <string name="action_switch">Switch</string>
     <string name="action_unpublish">Unpublish</string>
     <string name="action_enable">Enable</string>
+    <string name="action_disable">Disable</string>
     <string name="action_not_now">Not now</string>
     <string name="action_agree">Agree</string>
     <string name="action_change">"Change"</string>