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(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(VectorPreferences.SETTINGS_BACKGROUND_SYNC_PREFERENCE_KEY)?.let { - it.isVisible = !UPHelper.hasEndpoint(requireContext()) + it.isVisible = UPHelper.allowBackgroundSync(requireContext()) } findPreference(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(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 @@ Force custom push gateway Only enable this if your push endpoint does not require an external gateway. Re-register push distributor + Allow background sync in addition to push + Only meaningful while debugging your push setup! No push distributor found! Registered push distributor %1$s 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" /> + + - +