From 509eabfe5c6d7e24a4205c08672e5f3ae07c96d6 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Sat, 26 Nov 2022 18:20:59 +0100 Subject: [PATCH] Avoid unifiedpush selection spam Change-Id: Id6836b3683414ca7d8dcc3f52c0c4fc3fa202db0 --- .../vector/app/core/pushers/UnifiedPushHelper.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 6050016025..d6e70eefe7 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 @@ -35,6 +35,7 @@ import org.matrix.android.sdk.api.util.MatrixJsonParser import org.unifiedpush.android.connector.UnifiedPush import timber.log.Timber import java.net.URL +import java.util.concurrent.atomic.AtomicBoolean import javax.inject.Inject class UnifiedPushHelper @Inject constructor( @@ -47,12 +48,20 @@ class UnifiedPushHelper @Inject constructor( private val fcmHelper: FcmHelper, ) { + private val isRegistering = AtomicBoolean(false) + // Called when the home activity starts // or when notifications are enabled fun register( activity: FragmentActivity, onDoneRunnable: Runnable? = null, ) { + if (!isRegistering.compareAndSet(false, true)) { + // There's already a register() in progress, possibly because user hasn't reacted to the dialog yet + Timber.i("Skipping register() call with runnable $onDoneRunnable, still in register call") + onDoneRunnable?.run() + return + } registerInternal( activity, onDoneRunnable = onDoneRunnable @@ -90,6 +99,7 @@ class UnifiedPushHelper @Inject constructor( UnifiedPush.saveDistributor(context, context.packageName) UnifiedPush.registerApp(context) onDoneRunnable?.run() + isRegistering.set(false) return@launch } if (force) { @@ -100,6 +110,7 @@ class UnifiedPushHelper @Inject constructor( if (!force && UnifiedPush.getDistributor(context).isNotEmpty()) { UnifiedPush.registerApp(context) onDoneRunnable?.run() + isRegistering.set(false) return@launch } @@ -109,6 +120,7 @@ class UnifiedPushHelper @Inject constructor( UnifiedPush.saveDistributor(context, distributors.first()) UnifiedPush.registerApp(context) onDoneRunnable?.run() + isRegistering.set(false) } else { openDistributorDialogInternal( activity = activity, @@ -152,6 +164,7 @@ class UnifiedPushHelper @Inject constructor( Timber.i("Saving distributor: $distributor") UnifiedPush.registerApp(context) onDoneRunnable?.run() + isRegistering.set(false) } } .setOnCancelListener { @@ -159,6 +172,7 @@ class UnifiedPushHelper @Inject constructor( UnifiedPush.saveDistributor(context, context.packageName) UnifiedPush.registerApp(context) onDoneRunnable?.run() + isRegistering.set(false) } .setCancelable(true) .show() @@ -200,6 +214,7 @@ class UnifiedPushHelper @Inject constructor( if (UnifiedPush.getDistributor(context) == context.packageName) { unifiedPushStore.storePushGateway(stringProvider.getString(R.string.pusher_http_url)) onDoneRunnable?.run() + isRegistering.set(false) return } // else, unifiedpush, and pushkey is an endpoint @@ -216,6 +231,7 @@ class UnifiedPushHelper @Inject constructor( Timber.d("Using custom gateway") unifiedPushStore.storePushGateway(custom) onDoneRunnable?.run() + isRegistering.set(false) return } }