Merge pull request #3077 from Dominaezzz/suspend_functions_8
Convert PushersService to suspend functions
This commit is contained in:
commit
f2f735ace1
@ -16,8 +16,6 @@
|
|||||||
package org.matrix.android.sdk.api.session.pushers
|
package org.matrix.android.sdk.api.session.pushers
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import org.matrix.android.sdk.api.MatrixCallback
|
|
||||||
import org.matrix.android.sdk.api.util.Cancelable
|
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
|
|
||||||
interface PushersService {
|
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.
|
* @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.
|
* In case of error, PusherRejected failure can happen. In this case it means that the pushkey is not valid.
|
||||||
*/
|
*/
|
||||||
fun testPush(url: String,
|
suspend fun testPush(url: String,
|
||||||
appId: String,
|
appId: String,
|
||||||
pushkey: String,
|
pushkey: String,
|
||||||
eventId: String,
|
eventId: String)
|
||||||
callback: MatrixCallback<Unit>): Cancelable
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the http pusher
|
* Remove the http pusher
|
||||||
*/
|
*/
|
||||||
fun removeHttpPusher(pushkey: String, appId: String, callback: MatrixCallback<Unit>): Cancelable
|
suspend fun removeHttpPusher(pushkey: String, appId: String)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current pushers, as a LiveData
|
* Get the current pushers, as a LiveData
|
||||||
|
@ -18,10 +18,8 @@ package org.matrix.android.sdk.internal.session.pushers
|
|||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.work.BackoffPolicy
|
import androidx.work.BackoffPolicy
|
||||||
import com.zhuinden.monarchy.Monarchy
|
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.Pusher
|
||||||
import org.matrix.android.sdk.api.session.pushers.PushersService
|
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.mapper.asDomain
|
||||||
import org.matrix.android.sdk.internal.database.model.PusherEntity
|
import org.matrix.android.sdk.internal.database.model.PusherEntity
|
||||||
import org.matrix.android.sdk.internal.database.query.where
|
import org.matrix.android.sdk.internal.database.query.where
|
||||||
@ -47,16 +45,11 @@ internal class DefaultPushersService @Inject constructor(
|
|||||||
private val taskExecutor: TaskExecutor
|
private val taskExecutor: TaskExecutor
|
||||||
) : PushersService {
|
) : PushersService {
|
||||||
|
|
||||||
override fun testPush(url: String,
|
override suspend fun testPush(url: String,
|
||||||
appId: String,
|
appId: String,
|
||||||
pushkey: String,
|
pushkey: String,
|
||||||
eventId: String,
|
eventId: String) {
|
||||||
callback: MatrixCallback<Unit>): Cancelable {
|
pushGatewayNotifyTask.execute(PushGatewayNotifyTask.Params(url, appId, pushkey, eventId))
|
||||||
return pushGatewayNotifyTask
|
|
||||||
.configureWith(PushGatewayNotifyTask.Params(url, appId, pushkey, eventId)) {
|
|
||||||
this.callback = callback
|
|
||||||
}
|
|
||||||
.executeBy(taskExecutor)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun refreshPushers() {
|
override fun refreshPushers() {
|
||||||
@ -102,14 +95,9 @@ internal class DefaultPushersService @Inject constructor(
|
|||||||
return request.id
|
return request.id
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun removeHttpPusher(pushkey: String, appId: String, callback: MatrixCallback<Unit>): Cancelable {
|
override suspend fun removeHttpPusher(pushkey: String, appId: String) {
|
||||||
val params = RemovePusherTask.Params(pushkey, appId)
|
val params = RemovePusherTask.Params(pushkey, appId)
|
||||||
return removePusherTask
|
removePusherTask.execute(params)
|
||||||
.configureWith(params) {
|
|
||||||
this.callback = callback
|
|
||||||
}
|
|
||||||
// .enableRetry() ??
|
|
||||||
.executeBy(taskExecutor)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getPushersLive(): LiveData<List<Pusher>> {
|
override fun getPushersLive(): LiveData<List<Pusher>> {
|
||||||
|
@ -24,9 +24,8 @@ import im.vector.app.core.pushers.PushersManager
|
|||||||
import im.vector.app.core.resources.StringProvider
|
import im.vector.app.core.resources.StringProvider
|
||||||
import im.vector.app.features.settings.troubleshoot.TroubleshootTest
|
import im.vector.app.features.settings.troubleshoot.TroubleshootTest
|
||||||
import im.vector.app.push.fcm.FcmHelper
|
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.session.pushers.PushGatewayFailure
|
||||||
import org.matrix.android.sdk.api.util.Cancelable
|
import kotlinx.coroutines.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,29 +37,31 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat
|
|||||||
private val pushersManager: PushersManager)
|
private val pushersManager: PushersManager)
|
||||||
: TroubleshootTest(R.string.settings_troubleshoot_test_push_loop_title) {
|
: TroubleshootTest(R.string.settings_troubleshoot_test_push_loop_title) {
|
||||||
|
|
||||||
private var action: Cancelable? = null
|
private var action: Job? = null
|
||||||
|
|
||||||
override fun perform(activityResultLauncher: ActivityResultLauncher<Intent>) {
|
override fun perform(activityResultLauncher: ActivityResultLauncher<Intent>) {
|
||||||
val fcmToken = FcmHelper.getFcmToken(context) ?: run {
|
val fcmToken = FcmHelper.getFcmToken(context) ?: run {
|
||||||
status = TestStatus.FAILED
|
status = TestStatus.FAILED
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
action = pushersManager.testPush(fcmToken, object : MatrixCallback<Unit> {
|
action = GlobalScope.launch {
|
||||||
override fun onFailure(failure: Throwable) {
|
runCatching { pushersManager.testPush(fcmToken) }
|
||||||
description = if (failure is PushGatewayFailure.PusherRejected) {
|
.fold(
|
||||||
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
|
// Wait for the push to be received
|
||||||
description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_waiting_for_push)
|
description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_waiting_for_push)
|
||||||
status = TestStatus.RUNNING
|
status = TestStatus.RUNNING
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description = if (it is PushGatewayFailure.PusherRejected) {
|
||||||
|
stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_failed)
|
||||||
|
} else {
|
||||||
|
errorFormatter.toHumanReadable(it)
|
||||||
|
}
|
||||||
|
status = TestStatus.FAILED
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPushReceived() {
|
override fun onPushReceived() {
|
||||||
|
@ -35,15 +35,14 @@ class PushersManager @Inject constructor(
|
|||||||
private val stringProvider: StringProvider,
|
private val stringProvider: StringProvider,
|
||||||
private val appNameProvider: AppNameProvider
|
private val appNameProvider: AppNameProvider
|
||||||
) {
|
) {
|
||||||
fun testPush(pushKey: String, callback: MatrixCallback<Unit>): Cancelable {
|
suspend fun testPush(pushKey: String) {
|
||||||
val currentSession = activeSessionHolder.getActiveSession()
|
val currentSession = activeSessionHolder.getActiveSession()
|
||||||
|
|
||||||
return currentSession.testPush(
|
currentSession.testPush(
|
||||||
stringProvider.getString(R.string.pusher_http_url),
|
stringProvider.getString(R.string.pusher_http_url),
|
||||||
stringProvider.getString(R.string.pusher_app_id),
|
stringProvider.getString(R.string.pusher_app_id),
|
||||||
pushKey,
|
pushKey,
|
||||||
TEST_EVENT_ID,
|
TEST_EVENT_ID
|
||||||
callback
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,9 +63,9 @@ class PushersManager @Inject constructor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun unregisterPusher(pushKey: String, callback: MatrixCallback<Unit>) {
|
suspend fun unregisterPusher(pushKey: String) {
|
||||||
val currentSession = activeSessionHolder.getSafeActiveSession() ?: return
|
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 {
|
companion object {
|
||||||
|
@ -39,7 +39,6 @@ import im.vector.app.core.utils.requestDisablingBatteryOptimization
|
|||||||
import im.vector.app.features.notifications.NotificationUtils
|
import im.vector.app.features.notifications.NotificationUtils
|
||||||
import im.vector.app.push.fcm.FcmHelper
|
import im.vector.app.push.fcm.FcmHelper
|
||||||
import kotlinx.coroutines.launch
|
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.extensions.tryOrNull
|
||||||
import org.matrix.android.sdk.api.pushrules.RuleIds
|
import org.matrix.android.sdk.api.pushrules.RuleIds
|
||||||
import org.matrix.android.sdk.api.pushrules.RuleKind
|
import org.matrix.android.sdk.api.pushrules.RuleKind
|
||||||
@ -295,20 +294,20 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
FcmHelper.getFcmToken(requireContext())?.let {
|
FcmHelper.getFcmToken(requireContext())?.let {
|
||||||
pushManager.unregisterPusher(it, object : MatrixCallback<Unit> {
|
lifecycleScope.launch {
|
||||||
override fun onSuccess(data: Unit) {
|
runCatching { pushManager.unregisterPusher(it) }
|
||||||
session.refreshPushers()
|
.fold(
|
||||||
}
|
{ session.refreshPushers() },
|
||||||
|
{
|
||||||
override fun onFailure(failure: Throwable) {
|
|
||||||
if (!isAdded) {
|
if (!isAdded) {
|
||||||
return
|
return@fold
|
||||||
}
|
}
|
||||||
// revert the check box
|
// revert the check box
|
||||||
switchPref.isChecked = !switchPref.isChecked
|
switchPref.isChecked = !switchPref.isChecked
|
||||||
Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user