Add UnifiedPush settings

This commit is contained in:
sim 2022-02-27 19:29:37 +01:00 committed by Benoit Marty
parent 04b297b261
commit 848adc415f
6 changed files with 66 additions and 26 deletions

View file

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

View file

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

View file

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

View file

@ -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<SwitchPreference>(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<VectorPreference>(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

View file

@ -3067,4 +3067,5 @@
<string name="unifiedpush_getdistributors_dialog_title">Choose how to receive notifications</string>
<string name="unifiedpush_getdistributors_dialog_fcm_fallback">Google Services</string>
<string name="unifiedpush_getdistributors_dialog_background_sync">Background synchronization</string>
<string name="settings_unifiedpush_reregister">Notification method</string>
</resources>

View file

@ -77,6 +77,11 @@
android:summary="@string/settings_system_preferences_summary"
android:title="@string/settings_call_notifications_preferences" />
<im.vector.app.core.preference.VectorPreference
android:persistent="false"
android:key="SETTINGS_UNIFIED_PUSH_RE_REGISTER_KEY"
android:title="@string/settings_unifiedpush_reregister" />
</im.vector.app.core.preference.VectorPreferenceCategory>
<im.vector.app.core.preference.VectorPreferenceCategory