From ab9bbe9a4862b8450ec90803325ee76341056b02 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 8 Oct 2020 21:40:53 +0200 Subject: [PATCH] Cleanup and cancel request properly --- .../sdk/api/session/pushers/PushersService.kt | 2 +- .../session/pushers/DefaultPushersService.kt | 4 +-- .../troubleshoot/TestPushFromPushGateway.kt | 9 ++++- .../vector/app/core/pushers/PushersManager.kt | 5 +-- ...ttingsNotificationsTroubleshootFragment.kt | 20 +++-------- .../NotificationTroubleshootTestManager.kt | 35 ++++++++++++------- 6 files changed, 41 insertions(+), 34 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 09cbe5a89b..3993422b1d 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 @@ -79,7 +79,7 @@ interface PushersService { appId: String, pushkey: String, eventId: String, - callback: MatrixCallback) + callback: MatrixCallback): Cancelable /** * Remove the http pusher 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 1ded11de8f..e239182b4a 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 @@ -51,8 +51,8 @@ internal class DefaultPushersService @Inject constructor( appId: String, pushkey: String, eventId: String, - callback: MatrixCallback) { - pushGatewayNotifyTask + callback: MatrixCallback): Cancelable { + return pushGatewayNotifyTask .configureWith(PushGatewayNotifyTask.Params(url, appId, pushkey, eventId)) { this.callback = callback } 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 19a4e3a841..bbe3f35fb5 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 @@ -26,6 +26,7 @@ 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 javax.inject.Inject /** @@ -37,12 +38,14 @@ 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 + override fun perform(activityResultLauncher: ActivityResultLauncher) { val fcmToken = FcmHelper.getFcmToken(context) ?: run { status = TestStatus.FAILED return } - pushersManager.testPush(fcmToken, object : MatrixCallback { + 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) @@ -64,4 +67,8 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_notification_clicked) status = TestStatus.SUCCESS } + + override fun cancel() { + action?.cancel() + } } 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 cfdc61a045..5fe30141d9 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 @@ -22,6 +22,7 @@ import im.vector.app.core.resources.AppNameProvider import im.vector.app.core.resources.LocaleProvider import im.vector.app.core.resources.StringProvider import org.matrix.android.sdk.api.MatrixCallback +import org.matrix.android.sdk.api.util.Cancelable import java.util.UUID import javax.inject.Inject import kotlin.math.abs @@ -34,10 +35,10 @@ class PushersManager @Inject constructor( private val stringProvider: StringProvider, private val appNameProvider: AppNameProvider ) { - fun testPush(pushKey: String, callback: MatrixCallback) { + fun testPush(pushKey: String, callback: MatrixCallback): Cancelable { val currentSession = activeSessionHolder.getActiveSession() - currentSession.testPush( + return currentSession.testPush( stringProvider.getString(R.string.pusher_http_url), stringProvider.getString(R.string.pusher_app_id), pushKey, diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt index 2adc6ccdcf..ea9e73f160 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt @@ -41,6 +41,7 @@ import im.vector.app.features.rageshake.BugReporter import im.vector.app.features.settings.troubleshoot.NotificationTroubleshootTestManager import im.vector.app.features.settings.troubleshoot.TroubleshootTest import im.vector.app.push.fcm.NotificationTroubleshootTestManagerFactory +import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.extensions.tryOrNull import javax.inject.Inject @@ -92,8 +93,7 @@ class VectorSettingsNotificationsTroubleshootFragment @Inject constructor( } private fun startUI() { - mSummaryDescription.text = getString(R.string.settings_troubleshoot_diagnostic_running_status, - 0, 0) + mSummaryDescription.text = getString(R.string.settings_troubleshoot_diagnostic_running_status, 0, 0) testManager = testManagerFactory.create(this) testManager?.statusListener = { troubleshootTestManager -> if (isAdded) { @@ -105,9 +105,8 @@ class VectorSettingsNotificationsTroubleshootFragment @Inject constructor( mRunButton.visibility = View.VISIBLE } TroubleshootTest.TestStatus.RUNNING -> { - // Forces int type because it's breaking lint - val size: Int = troubleshootTestManager.testList.size - val currentTestIndex: Int = troubleshootTestManager.currentTestIndex + val size = troubleshootTestManager.testListSize + val currentTestIndex = troubleshootTestManager.currentTestIndex mSummaryDescription.text = getString( R.string.settings_troubleshoot_diagnostic_running_status, currentTestIndex, @@ -118,16 +117,7 @@ class VectorSettingsNotificationsTroubleshootFragment @Inject constructor( } TroubleshootTest.TestStatus.FAILED -> { // check if there are quick fixes - // TODO Rewrite using firstOrNull - var hasQuickFix = false - testManager?.testList?.let { - for (test in it) { - if (test.status == TroubleshootTest.TestStatus.FAILED && test.quickFix != null) { - hasQuickFix = true - break - } - } - } + val hasQuickFix = testManager?.hasQuickFix().orFalse() if (hasQuickFix) { mSummaryDescription.text = getString(R.string.settings_troubleshoot_diagnostic_failure_status_with_quickfix) } else { diff --git a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/NotificationTroubleshootTestManager.kt b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/NotificationTroubleshootTestManager.kt index 0c77caf635..566cb6d846 100644 --- a/vector/src/main/java/im/vector/app/features/settings/troubleshoot/NotificationTroubleshootTestManager.kt +++ b/vector/src/main/java/im/vector/app/features/settings/troubleshoot/NotificationTroubleshootTestManager.kt @@ -23,13 +23,19 @@ import androidx.fragment.app.Fragment import kotlin.properties.Delegates class NotificationTroubleshootTestManager(val fragment: Fragment) { + private val testList = ArrayList() + + val testListSize: Int + get() = testList.size - val testList = ArrayList() var isCancelled = false + private set var currentTestIndex by Delegates.observable(0) { _, _, _ -> statusListener?.invoke(this) } + private set + val adapter = NotificationTroubleshootRecyclerViewAdapter(testList) var statusListener: ((NotificationTroubleshootTestManager) -> Unit)? = null @@ -37,6 +43,7 @@ class NotificationTroubleshootTestManager(val fragment: Fragment) { var diagStatus: TroubleshootTest.TestStatus by Delegates.observable(TroubleshootTest.TestStatus.NOT_STARTED) { _, _, _ -> statusListener?.invoke(this) } + private set fun addTest(test: TroubleshootTest) { testList.add(test) @@ -79,25 +86,27 @@ class NotificationTroubleshootTestManager(val fragment: Fragment) { } fun retry(activityResultLauncher: ActivityResultLauncher) { - for (test in testList) { - test.cancel() - test.description = null - test.quickFix = null - test.status = TroubleshootTest.TestStatus.NOT_STARTED + testList.forEach { + it.cancel() + it.description = null + it.quickFix = null + it.status = TroubleshootTest.TestStatus.NOT_STARTED } runDiagnostic(activityResultLauncher) } - fun cancel() { - isCancelled = true - for (test in testList) { - test.cancel() + fun hasQuickFix(): Boolean { + return testList.any { test -> + test.status == TroubleshootTest.TestStatus.FAILED && test.quickFix != null } } + fun cancel() { + isCancelled = true + testList.forEach { it.cancel() } + } + fun onDiagnosticNotificationClicked() { - testList.forEach { - it.onNotificationClicked() - } + testList.forEach { it.onNotificationClicked() } } }