From 8005f44768d67194e69636c0e871bfd4e8a6e86d Mon Sep 17 00:00:00 2001
From: SpiritCroc <dev@spiritcroc.de>
Date: Thu, 16 Sep 2021 10:41:18 +0200
Subject: [PATCH] Add option to force allow background sync in addition to push

Change-Id: I545475f1f8b36a43712613493173c9a6638cc7f6
---
 .../java/im/vector/app/core/pushers/UPHelper.kt    | 10 +++++++++-
 .../app/core/pushers/VectorMessagingReceiver.kt    |  6 ++++--
 .../app/features/call/webrtc/WebRtcCallManager.kt  |  4 ++--
 .../app/features/settings/VectorPreferences.kt     |  7 ++++++-
 ...VectorSettingsNotificationPreferenceFragment.kt | 12 ++++++++++--
 vector/src/main/res/values/strings_sc.xml          |  2 ++
 .../main/res/xml/vector_settings_notifications.xml | 14 ++++++++++----
 7 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt b/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt
index 8f2fbc49a1..8f4e91f485 100644
--- a/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt
+++ b/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt
@@ -182,7 +182,7 @@ object UPHelper {
              * return custom
              */
             val vectorPreferences = VectorPreferences(context)
-            if (vectorPreferences.forceUsCustomUpGateway()) {
+            if (vectorPreferences.forceUseCustomUpGateway()) {
                 return custom
             }
         }
@@ -196,6 +196,14 @@ object UPHelper {
         return false
     }
 
+    fun allowBackgroundSync(context: Context): Boolean {
+        if (!hasEndpoint(context)) {
+            return true
+        }
+        val vectorPreferences = VectorPreferences(context)
+        return vectorPreferences.forceAllowBackgroundSync()
+    }
+
     fun distributorExists(context: Context): Boolean {
         val up = Registration()
         return up.getDistributor(context).isNotEmpty()
diff --git a/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt b/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt
index 9b2935bafa..09e9a7e8e4 100755
--- a/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt
+++ b/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt
@@ -169,8 +169,10 @@ val upHandler = object: MessagingReceiverHandler {
                 Timber.i("onNewEndpoint: skipped")
             }
         }
-        val mode = BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_DISABLED
-        vectorPreferences.setFdroidSyncBackgroundMode(mode)
+        if (!UPHelper.allowBackgroundSync(context)) {
+            val mode = BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_DISABLED
+            vectorPreferences.setFdroidSyncBackgroundMode(mode)
+        }
     }
 
     override fun onRegistrationFailed(context: Context?, instance: String) {
diff --git a/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt b/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt
index 767ef59aa6..3b69d1124a 100644
--- a/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt
+++ b/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt
@@ -262,7 +262,7 @@ class WebRtcCallManager @Inject constructor(
             audioManager.setMode(CallAudioManager.Mode.DEFAULT)
             // did we start background sync? so we should stop it
             if (isInBackground) {
-                if (UPHelper.hasEndpoint(context)) {
+                if (!UPHelper.allowBackgroundSync(context)) {
                     currentSession?.stopAnyBackgroundSync()
                 } else {
                     // for fdroid we should not stop, it should continue syncing
@@ -367,7 +367,7 @@ class WebRtcCallManager @Inject constructor(
         // and thus won't be able to received events. For example if the call is
         // accepted on an other session this device will continue ringing
         if (isInBackground) {
-            if (UPHelper.hasEndpoint(context)) {
+            if (!UPHelper.allowBackgroundSync(context)) {
                 // only for push version as fdroid version is already doing it?
                 currentSession?.startAutomaticBackgroundSync(30, 0)
             } else {
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 b6ab287037..cf8ef251b1 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
@@ -206,6 +206,7 @@ class VectorPreferences @Inject constructor(private val context: Context) {
         private const val SETTINGS_OPEN_CHATS_AT_FIRST_UNREAD = "SETTINGS_OPEN_CHATS_AT_FIRST_UNREAD"
         const val SETTINGS_VOICE_MESSAGE = "SETTINGS_VOICE_MESSAGE"
         const val SETTINGS_UNIFIED_PUSH_FORCE_CUSTOM_GATEWAY = "SETTINGS_UNIFIED_PUSH_FORCE_CUSTOM_GATEWAY"
+        const val SETTINGS_FORCE_ALLOW_BACKGROUND_SYNC = "SETTINGS_FORCE_ALLOW_BACKGROUND_SYNC"
 
         private const val DID_ASK_TO_ENABLE_SESSION_PUSH = "DID_ASK_TO_ENABLE_SESSION_PUSH"
         private const val DID_PROMOTE_NEW_RESTRICTED_JOIN_RULE = "DID_PROMOTE_NEW_RESTRICTED_JOIN_RULE"
@@ -1022,10 +1023,14 @@ class VectorPreferences @Inject constructor(private val context: Context) {
         return Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP && defaultPrefs.getBoolean(SETTINGS_VOICE_MESSAGE, true)
     }
 
-    fun forceUsCustomUpGateway(): Boolean {
+    fun forceUseCustomUpGateway(): Boolean {
         return defaultPrefs.getBoolean(SETTINGS_UNIFIED_PUSH_FORCE_CUSTOM_GATEWAY, false)
     }
 
+    fun forceAllowBackgroundSync(): Boolean {
+        return defaultPrefs.getBoolean(SETTINGS_FORCE_ALLOW_BACKGROUND_SYNC, false)
+    }
+
     /**
      * I likely do more fresh installs of the app than anyone else, so a shortcut to change some of the default settings to
      * my preferred values can safe me some time
diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt
index 9c4ad682b1..91bb37ab8d 100644
--- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt
+++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt
@@ -134,6 +134,14 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
             true
         }
 
+        findPreference<VectorSwitchPreference>(VectorPreferences.SETTINGS_FORCE_ALLOW_BACKGROUND_SYNC)?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
+            if (newValue != true && UPHelper.hasEndpoint(requireContext())) {
+                onOptionSelected(BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_DISABLED)
+            }
+            Handler(Looper.getMainLooper()).postDelayed({ refreshBackgroundSyncPrefs() } , 500)
+            true
+        }
+
         refreshBackgroundSyncPrefs()
 
         handleSystemPreference()
@@ -168,7 +176,7 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
         }
 
         findPreference<VectorPreferenceCategory>(VectorPreferences.SETTINGS_BACKGROUND_SYNC_PREFERENCE_KEY)?.let {
-            it.isVisible = !UPHelper.hasEndpoint(requireContext())
+            it.isVisible = UPHelper.allowBackgroundSync(requireContext())
         }
 
         findPreference<VectorEditTextPreference>(VectorPreferences.SETTINGS_SET_SYNC_TIMEOUT_PREFERENCE_KEY)?.let {
@@ -272,7 +280,7 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
 
     private fun refreshPref() {
         // This pref may have change from troubleshoot pref fragment
-        if (!UPHelper.hasEndpoint(requireContext())) {
+        if (UPHelper.allowBackgroundSync(requireContext())) {
             findPreference<VectorSwitchPreference>(VectorPreferences.SETTINGS_START_ON_BOOT_PREFERENCE_KEY)
                     ?.isChecked = vectorPreferences.autoStartOnBoot()
         }
diff --git a/vector/src/main/res/values/strings_sc.xml b/vector/src/main/res/values/strings_sc.xml
index e110957f12..e4a019fbca 100644
--- a/vector/src/main/res/values/strings_sc.xml
+++ b/vector/src/main/res/values/strings_sc.xml
@@ -137,6 +137,8 @@
     <string name="settings_unifiedpush_force_custom_gateway">Force custom push gateway</string>
     <string name="settings_unifiedpush_force_custom_gateway_summary">Only enable this if your push endpoint does not require an external gateway.</string>
     <string name="settings_unifiedpush_reregister">Re-register push distributor</string>
+    <string name="settings_force_allow_background_sync">Allow background sync in addition to push</string>
+    <string name="settings_force_allow_background_sync_summary">Only meaningful while debugging your push setup!</string>
     <string name="toast_unifiedpush_no_push_distributor">No push distributor found!</string>
     <string name="toast_unifiedpush_one_push_distributor">Registered push distributor %1$s</string>
 
diff --git a/vector/src/main/res/xml/vector_settings_notifications.xml b/vector/src/main/res/xml/vector_settings_notifications.xml
index 40fa60fa3a..4865aa5a03 100644
--- a/vector/src/main/res/xml/vector_settings_notifications.xml
+++ b/vector/src/main/res/xml/vector_settings_notifications.xml
@@ -138,16 +138,22 @@
             android:key="SETTINGS_UNIFIED_PUSH_RE_REGISTER"
             android:title="@string/settings_unifiedpush_reregister" />
 
+        <im.vector.app.core.preference.VectorPreference
+            android:persistent="false"
+            android:title="@string/settings_notifications_targets"
+            app:fragment="im.vector.app.features.settings.push.PushGatewaysFragment" />
+
         <im.vector.app.core.preference.VectorSwitchPreference
             android:key="SETTINGS_UNIFIED_PUSH_FORCE_CUSTOM_GATEWAY"
             android:title="@string/settings_unifiedpush_force_custom_gateway"
             android:summary="@string/settings_unifiedpush_force_custom_gateway_summary"
             android:defaultValue="false" />
 
-        <im.vector.app.core.preference.VectorPreference
-            android:persistent="false"
-            android:title="@string/settings_notifications_targets"
-            app:fragment="im.vector.app.features.settings.push.PushGatewaysFragment" />
+        <im.vector.app.core.preference.VectorSwitchPreference
+            android:key="SETTINGS_FORCE_ALLOW_BACKGROUND_SYNC"
+            android:title="@string/settings_force_allow_background_sync"
+            android:summary="@string/settings_force_allow_background_sync_summary"
+            android:defaultValue="false" />
 
     </im.vector.app.core.preference.VectorPreferenceCategory>