From 848adc415f13e09f67efdc6bd7a57d7e8ba0c53b Mon Sep 17 00:00:00 2001 From: sim Date: Sun, 27 Feb 2022 19:29:37 +0100 Subject: [PATCH] Add UnifiedPush settings --- .../app/core/pushers/UnifiedPushHelper.kt | 32 ++++++++++-- .../core/pushers/VectorMessagingReceiver.kt | 2 +- .../features/settings/VectorPreferences.kt | 3 ++ ...rSettingsNotificationPreferenceFragment.kt | 49 ++++++++++--------- vector/src/main/res/values/strings.xml | 1 + .../res/xml/vector_settings_notifications.xml | 5 ++ 6 files changed, 66 insertions(+), 26 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt b/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt index 62b174b0f5..a629792204 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt @@ -24,7 +24,10 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder import im.vector.app.BuildConfig import im.vector.app.R import im.vector.app.core.di.DefaultSharedPreferences +import im.vector.app.features.settings.BackgroundSyncMode +import im.vector.app.features.settings.VectorPreferences import im.vector.app.push.fcm.FcmHelper +import kotlinx.coroutines.runBlocking import org.unifiedpush.android.connector.UnifiedPush import timber.log.Timber import java.net.URI @@ -81,7 +84,10 @@ object UnifiedPushHelper { } } - fun register(context: Context, force: Boolean = false, onDoneRunnable: Runnable? = null) { + fun register(context: Context, + force: Boolean = false, + pushersManager: PushersManager? = null, + onDoneRunnable: Runnable? = null) { if (!BuildConfig.ALLOW_EXTERNAL_UNIFIEDPUSH_DISTRIB) { up.saveDistributor(context, context.packageName) up.registerApp(context) @@ -90,14 +96,21 @@ object UnifiedPushHelper { } if (force) { // Un-register first + runBlocking { + pushersManager?.unregisterPusher(getEndpointOrToken(context) ?: "") + } up.unregisterApp(context) storeUpEndpoint(context, null) storePushGateway(context, null) - } else if (up.getDistributor(context).isNotEmpty()) { + } + if (up.getDistributor(context).isNotEmpty()) { up.registerApp(context) onDoneRunnable?.run() return } + + // By default, use internal solution (fcm/background sync) + up.saveDistributor(context, context.packageName) val distributors = up.getDistributors(context).toMutableList() val internalDistributorName = if (!FcmHelper.isPushSupported()) { @@ -148,7 +161,20 @@ object UnifiedPushHelper { } } - fun unregister(context: Context) { + fun unregister( + context: Context, + pushersManager: PushersManager? = null, + vectorPreferences: VectorPreferences? = null + ) { + val mode = BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME + vectorPreferences?.setFdroidSyncBackgroundMode(mode) + runBlocking { + try { + pushersManager?.unregisterPusher(getEndpointOrToken(context) ?: "") + } catch (e: Exception) { + Timber.d("Probably unregistering a non existant pusher") + } + } up.unregisterApp(context) } 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 4b30f802e3..57953c1ca7 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt @@ -176,7 +176,7 @@ class VectorMessagingReceiver : MessagingReceiver() { override fun onUnregistered(context: Context, instance: String) { Timber.tag(loggerTag.value).d("Unifiedpush: Unregistered") - val mode = BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_BATTERY + val mode = BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME vectorPreferences.setFdroidSyncBackgroundMode(mode) runBlocking { try { 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 ce9c068c9c..1b61eb9bcf 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 @@ -141,6 +141,9 @@ class VectorPreferences @Inject constructor( const val SETTINGS_SET_SYNC_TIMEOUT_PREFERENCE_KEY = "SETTINGS_SET_SYNC_TIMEOUT_PREFERENCE_KEY" const val SETTINGS_SET_SYNC_DELAY_PREFERENCE_KEY = "SETTINGS_SET_SYNC_DELAY_PREFERENCE_KEY" + // notification method + const val SETTINGS_UNIFIED_PUSH_RE_REGISTER_KEY = "SETTINGS_UNIFIED_PUSH_RE_REGISTER_KEY" + // Calls const val SETTINGS_CALL_PREVENT_ACCIDENTAL_CALL_KEY = "SETTINGS_CALL_PREVENT_ACCIDENTAL_CALL_KEY" const val SETTINGS_CALL_RINGTONE_USE_RIOT_PREFERENCE_KEY = "SETTINGS_CALL_RINGTONE_USE_RIOT_PREFERENCE_KEY" 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 6a40dd2311..216b645726 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 @@ -30,6 +30,7 @@ import androidx.lifecycle.lifecycleScope import androidx.lifecycle.map import androidx.preference.Preference import androidx.preference.SwitchPreference +import im.vector.app.BuildConfig import im.vector.app.R import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.extensions.registerStartForActivityResult @@ -58,7 +59,6 @@ import org.matrix.android.sdk.api.session.identity.ThreePid import org.matrix.android.sdk.api.session.pushers.Pusher import org.matrix.android.sdk.api.session.pushrules.RuleIds import org.matrix.android.sdk.api.session.pushrules.RuleKind -import timber.log.Timber import javax.inject.Inject // Referenced in vector_settings_preferences_root.xml @@ -98,7 +98,16 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( findPreference(VectorPreferences.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY)?.let { it.setTransactionalSwitchChangeListener(lifecycleScope) { isChecked -> - updateEnabledForDevice(isChecked) + if (isChecked) { + UnifiedPushHelper.register(requireContext()) + } else { + UnifiedPushHelper.unregister( + requireContext(), + pushManager, + vectorPreferences + ) + session.pushersService().refreshPushers() + } } } @@ -140,6 +149,22 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( } } + findPreference(VectorPreferences.SETTINGS_UNIFIED_PUSH_RE_REGISTER_KEY)?.let { + if (BuildConfig.ALLOW_EXTERNAL_UNIFIEDPUSH_DISTRIB) { + it.onPreferenceClickListener = Preference.OnPreferenceClickListener { + UnifiedPushHelper.register( + requireContext(), + force = true, + pushManager + ) + true + } + session.pushersService().refreshPushers() + } else { + it.isVisible = false + } + } + bindEmailNotifications() refreshBackgroundSyncPrefs() @@ -356,26 +381,6 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( } } - private suspend fun updateEnabledForDevice(enabled: Boolean) { - if (enabled) { - UnifiedPushHelper.register(requireContext()) - } else { - UnifiedPushHelper.getEndpointOrToken(requireContext())?.let { - try { - pushManager.unregisterPusher(it) - } catch (e: Exception) { - Timber.d("Probably unregistering a non existant pusher") - } - try { - UnifiedPushHelper.unregister(requireContext()) - } catch (e: Exception) { - Timber.d("Probably unregistering to a non-saved distributor") - } - session.pushersService().refreshPushers() - } - } - } - private fun updateEnabledForAccount(preference: Preference?) { val pushRuleService = session.pushRuleService() val switchPref = preference as SwitchPreference diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml index 71ed348094..412156be2a 100644 --- a/vector/src/main/res/values/strings.xml +++ b/vector/src/main/res/values/strings.xml @@ -3067,4 +3067,5 @@ Choose how to receive notifications Google Services Background synchronization + Notification method diff --git a/vector/src/main/res/xml/vector_settings_notifications.xml b/vector/src/main/res/xml/vector_settings_notifications.xml index 66ac93a4f9..c8434b6920 100644 --- a/vector/src/main/res/xml/vector_settings_notifications.xml +++ b/vector/src/main/res/xml/vector_settings_notifications.xml @@ -77,6 +77,11 @@ android:summary="@string/settings_system_preferences_summary" android:title="@string/settings_call_notifications_preferences" /> + +