Add option to force allow background sync in addition to push

Change-Id: I545475f1f8b36a43712613493173c9a6638cc7f6
This commit is contained in:
SpiritCroc 2021-09-16 10:41:18 +02:00
parent adfa2b36af
commit 8005f44768
7 changed files with 43 additions and 12 deletions

View File

@ -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()

View File

@ -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) {

View File

@ -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 {

View File

@ -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

View File

@ -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()
}

View File

@ -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>

View File

@ -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>