From 7fbe485603e1ffdd605e62f9235fff194f24aff7 Mon Sep 17 00:00:00 2001 From: Dominic Fischer Date: Sat, 27 Mar 2021 20:44:07 +0000 Subject: [PATCH 1/3] Convert PushersService to suspend functions Signed-off-by: Dominic Fischer --- .../sdk/api/session/pushers/PushersService.kt | 13 ++++----- .../session/pushers/DefaultPushersService.kt | 26 +++++------------ .../vector/app/core/pushers/PushersManager.kt | 11 ++++--- ...rSettingsNotificationPreferenceFragment.kt | 29 +++++++++---------- 4 files changed, 31 insertions(+), 48 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushers/PushersService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushers/PushersService.kt index 3993422b1d1..9ea820f5b3d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushers/PushersService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushers/PushersService.kt @@ -16,8 +16,6 @@ package org.matrix.android.sdk.api.session.pushers import androidx.lifecycle.LiveData -import org.matrix.android.sdk.api.MatrixCallback -import org.matrix.android.sdk.api.util.Cancelable import java.util.UUID interface PushersService { @@ -75,16 +73,15 @@ interface PushersService { * @param callback callback to know if the push gateway has accepted the request. In this case, the app should receive a Push with the provided eventId. * In case of error, PusherRejected failure can happen. In this case it means that the pushkey is not valid. */ - fun testPush(url: String, - appId: String, - pushkey: String, - eventId: String, - callback: MatrixCallback): Cancelable + suspend fun testPush(url: String, + appId: String, + pushkey: String, + eventId: String) /** * Remove the http pusher */ - fun removeHttpPusher(pushkey: String, appId: String, callback: MatrixCallback): Cancelable + suspend fun removeHttpPusher(pushkey: String, appId: String) /** * Get the current pushers, as a LiveData diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/DefaultPushersService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/DefaultPushersService.kt index d290bb1a037..a772cf5ebb1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/DefaultPushersService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/DefaultPushersService.kt @@ -18,10 +18,8 @@ package org.matrix.android.sdk.internal.session.pushers import androidx.lifecycle.LiveData import androidx.work.BackoffPolicy import com.zhuinden.monarchy.Monarchy -import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.session.pushers.Pusher import org.matrix.android.sdk.api.session.pushers.PushersService -import org.matrix.android.sdk.api.util.Cancelable import org.matrix.android.sdk.internal.database.mapper.asDomain import org.matrix.android.sdk.internal.database.model.PusherEntity import org.matrix.android.sdk.internal.database.query.where @@ -47,16 +45,11 @@ internal class DefaultPushersService @Inject constructor( private val taskExecutor: TaskExecutor ) : PushersService { - override fun testPush(url: String, - appId: String, - pushkey: String, - eventId: String, - callback: MatrixCallback): Cancelable { - return pushGatewayNotifyTask - .configureWith(PushGatewayNotifyTask.Params(url, appId, pushkey, eventId)) { - this.callback = callback - } - .executeBy(taskExecutor) + override suspend fun testPush(url: String, + appId: String, + pushkey: String, + eventId: String) { + pushGatewayNotifyTask.execute(PushGatewayNotifyTask.Params(url, appId, pushkey, eventId)) } override fun refreshPushers() { @@ -102,14 +95,9 @@ internal class DefaultPushersService @Inject constructor( return request.id } - override fun removeHttpPusher(pushkey: String, appId: String, callback: MatrixCallback): Cancelable { + override suspend fun removeHttpPusher(pushkey: String, appId: String) { val params = RemovePusherTask.Params(pushkey, appId) - return removePusherTask - .configureWith(params) { - this.callback = callback - } - // .enableRetry() ?? - .executeBy(taskExecutor) + removePusherTask.execute(params) } override fun getPushersLive(): LiveData> { diff --git a/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt b/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt index 5fe30141d97..dda8b70b084 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt @@ -35,15 +35,14 @@ class PushersManager @Inject constructor( private val stringProvider: StringProvider, private val appNameProvider: AppNameProvider ) { - fun testPush(pushKey: String, callback: MatrixCallback): Cancelable { + suspend fun testPush(pushKey: String) { val currentSession = activeSessionHolder.getActiveSession() - return currentSession.testPush( + currentSession.testPush( stringProvider.getString(R.string.pusher_http_url), stringProvider.getString(R.string.pusher_app_id), pushKey, - TEST_EVENT_ID, - callback + TEST_EVENT_ID ) } @@ -64,9 +63,9 @@ class PushersManager @Inject constructor( ) } - fun unregisterPusher(pushKey: String, callback: MatrixCallback) { + suspend fun unregisterPusher(pushKey: String) { val currentSession = activeSessionHolder.getSafeActiveSession() ?: return - currentSession.removeHttpPusher(pushKey, stringProvider.getString(R.string.pusher_app_id), callback) + currentSession.removeHttpPusher(pushKey, stringProvider.getString(R.string.pusher_app_id)) } companion object { diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt index 47868eed51f..fd1f406bcbe 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt @@ -39,7 +39,6 @@ import im.vector.app.core.utils.requestDisablingBatteryOptimization import im.vector.app.features.notifications.NotificationUtils import im.vector.app.push.fcm.FcmHelper import kotlinx.coroutines.launch -import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.pushrules.RuleIds import org.matrix.android.sdk.api.pushrules.RuleKind @@ -295,20 +294,20 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( } } else { FcmHelper.getFcmToken(requireContext())?.let { - pushManager.unregisterPusher(it, object : MatrixCallback { - override fun onSuccess(data: Unit) { - session.refreshPushers() - } - - override fun onFailure(failure: Throwable) { - if (!isAdded) { - return - } - // revert the check box - switchPref.isChecked = !switchPref.isChecked - Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show() - } - }) + lifecycleScope.launch { + runCatching { pushManager.unregisterPusher(it) } + .fold( + { session.refreshPushers() }, + { + if (!isAdded) { + return@fold + } + // revert the check box + switchPref.isChecked = !switchPref.isChecked + Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show() + } + ) + } } } } From 1e58767374502cd721b7fd3f0e3b8fe15131fd57 Mon Sep 17 00:00:00 2001 From: Dominic Fischer Date: Mon, 29 Mar 2021 20:02:03 +0100 Subject: [PATCH 2/3] Missing file Signed-off-by: Dominic Fischer --- .../troubleshoot/TestPushFromPushGateway.kt | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt index da93d54075c..6cf7d68561d 100644 --- a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt +++ b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt @@ -24,9 +24,8 @@ import im.vector.app.core.pushers.PushersManager import im.vector.app.core.resources.StringProvider import im.vector.app.features.settings.troubleshoot.TroubleshootTest import im.vector.app.push.fcm.FcmHelper -import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.session.pushers.PushGatewayFailure -import org.matrix.android.sdk.api.util.Cancelable +import kotlinx.coroutines.* import javax.inject.Inject /** @@ -38,29 +37,31 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat private val pushersManager: PushersManager) : TroubleshootTest(R.string.settings_troubleshoot_test_push_loop_title) { - private var action: Cancelable? = null + private var action: Job? = null override fun perform(activityResultLauncher: ActivityResultLauncher) { val fcmToken = FcmHelper.getFcmToken(context) ?: run { status = TestStatus.FAILED return } - action = pushersManager.testPush(fcmToken, object : MatrixCallback { - override fun onFailure(failure: Throwable) { - description = if (failure is PushGatewayFailure.PusherRejected) { - stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_failed) - } else { - errorFormatter.toHumanReadable(failure) - } - status = TestStatus.FAILED - } - - override fun onSuccess(data: Unit) { - // Wait for the push to be received - description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_waiting_for_push) - status = TestStatus.RUNNING - } - }) + action = GlobalScope.launch { + runCatching { pushersManager.testPush(fcmToken) } + .fold( + { + // Wait for the push to be received + description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_waiting_for_push) + status = TestStatus.RUNNING + }, + { + description = if (failure is PushGatewayFailure.PusherRejected) { + stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_failed) + } else { + errorFormatter.toHumanReadable(it) + } + status = TestStatus.FAILED + } + ) + } } override fun onPushReceived() { @@ -69,6 +70,6 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat } override fun cancel() { - action?.cancel() + job?.cancel() } } From 6226938c6f64a626f7e43cad7ccd472859c901b6 Mon Sep 17 00:00:00 2001 From: Dominic Fischer Date: Tue, 30 Mar 2021 19:13:16 +0100 Subject: [PATCH 3/3] Missing file Signed-off-by: Dominic Fischer --- .../features/settings/troubleshoot/TestPushFromPushGateway.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt index 6cf7d68561d..6ce944d214b 100644 --- a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt +++ b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt @@ -53,7 +53,7 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat status = TestStatus.RUNNING }, { - description = if (failure is PushGatewayFailure.PusherRejected) { + description = if (it is PushGatewayFailure.PusherRejected) { stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_failed) } else { errorFormatter.toHumanReadable(it) @@ -70,6 +70,6 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat } override fun cancel() { - job?.cancel() + action?.cancel() } }