From 02d54878c315e765d0264d951caedf1752d3a012 Mon Sep 17 00:00:00 2001 From: sim Date: Wed, 24 Aug 2022 22:50:08 +0200 Subject: [PATCH 2/6] Set distributor dialog always cancelable ATM, it uses the default fallback if cancelled --- .../java/im/vector/app/core/pushers/UnifiedPushHelper.kt | 7 ++----- 1 file changed, 2 insertions(+), 5 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 0993485471..de5b881029 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 @@ -104,8 +104,7 @@ class UnifiedPushHelper @Inject constructor( pushersManager = pushersManager, onDoneRunnable = onDoneRunnable, distributors = distributors, - unregisterFirst = force, - cancellable = !force + unregisterFirst = force ) } } @@ -122,7 +121,6 @@ class UnifiedPushHelper @Inject constructor( pushersManager, onDoneRunnable, distributors, unregisterFirst = true, - cancellable = true, ) } @@ -132,7 +130,6 @@ class UnifiedPushHelper @Inject constructor( onDoneRunnable: Runnable?, distributors: List, unregisterFirst: Boolean, - cancellable: Boolean, ) { val internalDistributorName = stringProvider.getString( if (fcmHelper.isFirebaseAvailable()) { @@ -176,7 +173,7 @@ class UnifiedPushHelper @Inject constructor( UnifiedPush.registerApp(context) onDoneRunnable?.run() } - .setCancelable(cancellable) + .setCancelable(true) .show() } From a4dd08ddb33f97e4e1bd92bd1eeb32a041698898 Mon Sep 17 00:00:00 2001 From: sim Date: Wed, 24 Aug 2022 23:08:19 +0200 Subject: [PATCH 3/6] Always use register to open the distributor dialog The forced unregistration always happens in register function --- .../app/core/pushers/UnifiedPushHelper.kt | 41 +++++++------------ ...rSettingsNotificationPreferenceFragment.kt | 2 +- .../TestEndpointAsTokenRegistration.kt | 2 +- 3 files changed, 16 insertions(+), 29 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 de5b881029..ec646440b6 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 @@ -46,6 +46,7 @@ class UnifiedPushHelper @Inject constructor( private val vectorFeatures: VectorFeatures, private val fcmHelper: FcmHelper, ) { + fun register( activity: FragmentActivity, onDoneRunnable: Runnable? = null, @@ -56,7 +57,14 @@ class UnifiedPushHelper @Inject constructor( ) } - fun reRegister( + // If registration is forced: + // * the current distributor (if any) is removed + // * The dialog is opened + // + // The registration is forced in 2 cases : + // * in the settings + // * in the troubleshoot list (doFix) + fun forceRegister( activity: FragmentActivity, pushersManager: PushersManager, onDoneRunnable: Runnable? = null @@ -86,7 +94,8 @@ class UnifiedPushHelper @Inject constructor( // Un-register first unregister(pushersManager) } - if (UnifiedPush.getDistributor(context).isNotEmpty()) { + // the !force should not be needed + if (!force && UnifiedPush.getDistributor(context).isNotEmpty()) { UnifiedPush.registerApp(context) onDoneRunnable?.run() return@launch @@ -94,42 +103,24 @@ class UnifiedPushHelper @Inject constructor( val distributors = UnifiedPush.getDistributors(context) - if (distributors.size == 1 && !force) { + if (!force && distributors.size == 1) { UnifiedPush.saveDistributor(context, distributors.first()) UnifiedPush.registerApp(context) onDoneRunnable?.run() } else { openDistributorDialogInternal( activity = activity, - pushersManager = pushersManager, onDoneRunnable = onDoneRunnable, - distributors = distributors, - unregisterFirst = force + distributors = distributors ) } } } - fun openDistributorDialog( - activity: FragmentActivity, - pushersManager: PushersManager, - onDoneRunnable: Runnable, - ) { - val distributors = UnifiedPush.getDistributors(activity) - openDistributorDialogInternal( - activity, - pushersManager, - onDoneRunnable, distributors, - unregisterFirst = true, - ) - } - private fun openDistributorDialogInternal( activity: FragmentActivity, - pushersManager: PushersManager?, onDoneRunnable: Runnable?, - distributors: List, - unregisterFirst: Boolean, + distributors: List ) { val internalDistributorName = stringProvider.getString( if (fcmHelper.isFirebaseAvailable()) { @@ -157,10 +148,6 @@ class UnifiedPushHelper @Inject constructor( } activity.lifecycleScope.launch { - if (unregisterFirst) { - // Un-register first - unregister(pushersManager) - } UnifiedPush.saveDistributor(context, distributor) Timber.i("Saving distributor: $distributor") UnifiedPush.registerApp(context) 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 a09bb1e6a4..ab25f83a91 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 @@ -158,7 +158,7 @@ class VectorSettingsNotificationPreferenceFragment : if (vectorFeatures.allowExternalUnifiedPushDistributors()) { it.summary = unifiedPushHelper.getCurrentDistributorName() it.onPreferenceClickListener = Preference.OnPreferenceClickListener { - unifiedPushHelper.openDistributorDialog(requireActivity(), pushersManager) { + unifiedPushHelper.forceRegister(requireActivity(), pushersManager) { it.summary = unifiedPushHelper.getCurrentDistributorName() session.pushersService().refreshPushers() refreshBackgroundSyncPrefs() diff --git a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestEndpointAsTokenRegistration.kt b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestEndpointAsTokenRegistration.kt index 66222f759e..7875e3a21d 100644 --- a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestEndpointAsTokenRegistration.kt +++ b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/TestEndpointAsTokenRegistration.kt @@ -60,7 +60,7 @@ class TestEndpointAsTokenRegistration @Inject constructor( ) quickFix = object : TroubleshootQuickFix(R.string.settings_troubleshoot_test_endpoint_registration_quick_fix) { override fun doFix() { - unifiedPushHelper.reRegister( + unifiedPushHelper.forceRegister( context, pushersManager ) From 42c580c249a753603eed224087bdb578e4e53305 Mon Sep 17 00:00:00 2001 From: sim Date: Thu, 25 Aug 2022 08:57:27 +0200 Subject: [PATCH 4/6] Add comments --- .../main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt | 4 ++++ 1 file changed, 4 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 ec646440b6..c65bc0db5c 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 @@ -47,6 +47,8 @@ class UnifiedPushHelper @Inject constructor( private val fcmHelper: FcmHelper, ) { + // Called when the home activity starts + // or when notifications are enabled fun register( activity: FragmentActivity, onDoneRunnable: Runnable? = null, @@ -117,6 +119,8 @@ class UnifiedPushHelper @Inject constructor( } } + // There is no case where this function is called + // with a saved distributor and/or a pusher private fun openDistributorDialogInternal( activity: FragmentActivity, onDoneRunnable: Runnable?, From e2646c3243551e32123ffb706046678b36401f10 Mon Sep 17 00:00:00 2001 From: sim Date: Thu, 25 Aug 2022 08:58:42 +0200 Subject: [PATCH 5/6] Remove never-matched if --- .../main/java/im/vector/app/core/pushers/UnifiedPushHelper.kt | 4 ---- 1 file changed, 4 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 c65bc0db5c..8d8c2d5097 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 @@ -146,10 +146,6 @@ class UnifiedPushHelper @Inject constructor( .setTitle(stringProvider.getString(R.string.unifiedpush_getdistributors_dialog_title)) .setItems(distributorsName.toTypedArray()) { _, which -> val distributor = distributors[which] - if (distributor == UnifiedPush.getDistributor(context)) { - Timber.d("Same distributor selected again, no action") - return@setItems - } activity.lifecycleScope.launch { UnifiedPush.saveDistributor(context, distributor) From f7ae377874e561cbb31dd80443b83195a74816a3 Mon Sep 17 00:00:00 2001 From: sim Date: Thu, 25 Aug 2022 09:33:26 +0200 Subject: [PATCH 6/6] Add changelog Signed-off-by: sim --- changelog.d/6936.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/6936.misc diff --git a/changelog.d/6936.misc b/changelog.d/6936.misc new file mode 100644 index 0000000000..f032ad9805 --- /dev/null +++ b/changelog.d/6936.misc @@ -0,0 +1 @@ +Smaff refactor of UnifiedPushHelper