Troubleshoot notifications screen
This commit is contained in:
parent
6c9ea2b920
commit
2bfe125a77
80 changed files with 3086 additions and 99 deletions
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.push.impl
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.push.api.GetCurrentPushProvider
|
||||
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
|
||||
import io.element.android.services.appnavstate.api.AppNavigationStateService
|
||||
import io.element.android.services.appnavstate.api.currentSessionId
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultGetCurrentPushProvider @Inject constructor(
|
||||
private val pushStoreFactory: UserPushStoreFactory,
|
||||
private val appNavigationStateService: AppNavigationStateService,
|
||||
) : GetCurrentPushProvider {
|
||||
override suspend fun getCurrentPushProvider(): String? {
|
||||
return appNavigationStateService
|
||||
.appNavigationState
|
||||
.value
|
||||
.navigationState
|
||||
.currentSessionId()
|
||||
?.let { pushStoreFactory.create(it) }
|
||||
?.getPushProviderName()
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ package io.element.android.libraries.push.impl
|
|||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.push.api.GetCurrentPushProvider
|
||||
import io.element.android.libraries.push.api.PushService
|
||||
import io.element.android.libraries.push.impl.notifications.DefaultNotificationDrawerManager
|
||||
import io.element.android.libraries.pushproviders.api.Distributor
|
||||
|
|
@ -32,6 +33,7 @@ class DefaultPushService @Inject constructor(
|
|||
private val pushersManager: PushersManager,
|
||||
private val userPushStoreFactory: UserPushStoreFactory,
|
||||
private val pushProviders: Set<@JvmSuppressWildcards PushProvider>,
|
||||
private val getCurrentPushProvider: GetCurrentPushProvider,
|
||||
) : PushService {
|
||||
override fun notificationStyleChanged() {
|
||||
defaultNotificationDrawerManager.notificationStyleChanged()
|
||||
|
|
@ -58,7 +60,11 @@ class DefaultPushService @Inject constructor(
|
|||
userPushStore.setPushProviderName(pushProvider.name)
|
||||
}
|
||||
|
||||
override suspend fun testPush() {
|
||||
pushersManager.testPush()
|
||||
override suspend fun testPush(): Boolean {
|
||||
val currentPushProvider = getCurrentPushProvider.getCurrentPushProvider()
|
||||
val pushProvider = pushProviders.find { it.name == currentPushProvider } ?: return false
|
||||
val config = pushProvider.getCurrentUserPushConfig() ?: return false
|
||||
pushersManager.testPush(config)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,9 +23,11 @@ import io.element.android.libraries.core.meta.BuildMeta
|
|||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.matrix.api.core.EventId
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.matrix.api.pusher.SetHttpPusherData
|
||||
import io.element.android.libraries.push.impl.pushgateway.PushGatewayNotifyRequest
|
||||
import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig
|
||||
import io.element.android.libraries.pushproviders.api.PusherSubscriber
|
||||
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
|
||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret
|
||||
|
|
@ -45,16 +47,14 @@ class PushersManager @Inject constructor(
|
|||
private val pushClientSecret: PushClientSecret,
|
||||
private val userPushStoreFactory: UserPushStoreFactory,
|
||||
) : PusherSubscriber {
|
||||
// TODO Move this to the PushProvider API
|
||||
suspend fun testPush() {
|
||||
suspend fun testPush(config: CurrentUserPushConfig) {
|
||||
pushGatewayNotifyRequest.execute(
|
||||
PushGatewayNotifyRequest.Params(
|
||||
// unifiedPushHelper.getPushGateway() ?: return
|
||||
url = "TODO",
|
||||
url = config.url,
|
||||
appId = PushConfig.PUSHER_APP_ID,
|
||||
// unifiedPushHelper.getEndpointOrToken().orEmpty()
|
||||
pushKey = "TODO",
|
||||
eventId = TEST_EVENT_ID
|
||||
pushKey = config.pushKey,
|
||||
eventId = TEST_EVENT_ID,
|
||||
roomId = TEST_ROOM_ID,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
@ -112,5 +112,6 @@ class PushersManager @Inject constructor(
|
|||
|
||||
companion object {
|
||||
val TEST_EVENT_ID = EventId("\$THIS_IS_A_FAKE_EVENT_ID")
|
||||
val TEST_ROOM_ID = RoomId("!room:domain")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
package io.element.android.libraries.push.impl.notifications
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Notification
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
|
|
@ -32,12 +31,13 @@ class NotificationDisplayer @Inject constructor(
|
|||
) {
|
||||
private val notificationManager = NotificationManagerCompat.from(context)
|
||||
|
||||
fun showNotificationMessage(tag: String?, id: Int, notification: Notification) {
|
||||
fun showNotificationMessage(tag: String?, id: Int, notification: Notification): Boolean {
|
||||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
|
||||
Timber.w("Not allowed to notify.")
|
||||
return
|
||||
return false
|
||||
}
|
||||
notificationManager.notify(tag, id, notification)
|
||||
return true
|
||||
}
|
||||
|
||||
fun cancelNotificationMessage(tag: String?, id: Int) {
|
||||
|
|
@ -53,15 +53,21 @@ class NotificationDisplayer @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressLint("LaunchActivityFromNotification")
|
||||
fun displayDiagnosticNotification(notification: Notification) {
|
||||
showNotificationMessage(
|
||||
fun displayDiagnosticNotification(notification: Notification): Boolean {
|
||||
return showNotificationMessage(
|
||||
tag = "DIAGNOSTIC",
|
||||
id = NOTIFICATION_ID_DIAGNOSTIC,
|
||||
notification = notification
|
||||
)
|
||||
}
|
||||
|
||||
fun dismissDiagnosticNotification() {
|
||||
cancelNotificationMessage(
|
||||
tag = "DIAGNOSTIC",
|
||||
id = NOTIFICATION_ID_DIAGNOSTIC
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel the foreground notification service.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -19,9 +19,15 @@ package io.element.android.libraries.push.impl.notifications
|
|||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import io.element.android.libraries.architecture.bindings
|
||||
import io.element.android.libraries.push.impl.troubleshoot.NotificationClickHandler
|
||||
import javax.inject.Inject
|
||||
|
||||
class TestNotificationReceiver : BroadcastReceiver() {
|
||||
@Inject lateinit var notificationClickHandler: NotificationClickHandler
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
// TODO The test notification has been clicked, notify the ui
|
||||
context.bindings<TestNotificationReceiverBinding>().inject(this)
|
||||
notificationClickHandler.handleNotificationClick()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.push.impl.notifications
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesTo
|
||||
import io.element.android.libraries.di.AppScope
|
||||
|
||||
@ContributesTo(AppScope::class)
|
||||
interface TestNotificationReceiverBinding {
|
||||
fun inject(service: TestNotificationReceiver)
|
||||
}
|
||||
|
|
@ -299,6 +299,7 @@ class NotificationCreator @Inject constructor(
|
|||
}
|
||||
|
||||
fun createDiagnosticNotification(): Notification {
|
||||
val intent = pendingIntentFactory.createTestPendingIntent()
|
||||
return NotificationCompat.Builder(context, notificationChannels.getChannelIdForTest())
|
||||
.setContentTitle(buildMeta.applicationName)
|
||||
.setContentText(stringProvider.getString(R.string.notification_test_push_notification_content))
|
||||
|
|
@ -308,7 +309,8 @@ class NotificationCreator @Inject constructor(
|
|||
.setPriority(NotificationCompat.PRIORITY_MAX)
|
||||
.setCategory(NotificationCompat.CATEGORY_STATUS)
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(pendingIntentFactory.createTestPendingIntent())
|
||||
.setContentIntent(intent)
|
||||
.setDeleteIntent(intent)
|
||||
.build()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import io.element.android.libraries.push.impl.PushersManager
|
|||
import io.element.android.libraries.push.impl.notifications.DefaultNotificationDrawerManager
|
||||
import io.element.android.libraries.push.impl.notifications.NotifiableEventResolver
|
||||
import io.element.android.libraries.push.impl.store.DefaultPushDataStore
|
||||
import io.element.android.libraries.push.impl.troubleshoot.DiagnosticPushHandler
|
||||
import io.element.android.libraries.pushproviders.api.PushData
|
||||
import io.element.android.libraries.pushproviders.api.PushHandler
|
||||
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
|
||||
|
|
@ -51,6 +52,7 @@ class DefaultPushHandler @Inject constructor(
|
|||
// private val actionIds: NotificationActionIds,
|
||||
private val buildMeta: BuildMeta,
|
||||
private val matrixAuthenticationService: MatrixAuthenticationService,
|
||||
private val diagnosticPushHandler: DiagnosticPushHandler,
|
||||
) : PushHandler {
|
||||
private val coroutineScope = CoroutineScope(SupervisorJob())
|
||||
|
||||
|
|
@ -75,8 +77,7 @@ class DefaultPushHandler @Inject constructor(
|
|||
|
||||
// Diagnostic Push
|
||||
if (pushData.eventId == PushersManager.TEST_EVENT_ID) {
|
||||
// val intent = Intent(actionIds.push)
|
||||
// TODO The test push has been received, notify the ui
|
||||
diagnosticPushHandler.handlePush()
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@ import kotlinx.serialization.Serializable
|
|||
internal data class PushGatewayNotification(
|
||||
@SerialName("event_id")
|
||||
val eventId: String,
|
||||
@SerialName("room_id")
|
||||
val roomId: String,
|
||||
/**
|
||||
* Required. This is an array of devices that the notification should be sent to.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
package io.element.android.libraries.push.impl.pushgateway
|
||||
|
||||
import io.element.android.libraries.matrix.api.core.EventId
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.network.RetrofitFactory
|
||||
import io.element.android.libraries.push.api.gateway.PushGatewayFailure
|
||||
import javax.inject.Inject
|
||||
|
|
@ -27,7 +28,8 @@ class PushGatewayNotifyRequest @Inject constructor(
|
|||
val url: String,
|
||||
val appId: String,
|
||||
val pushKey: String,
|
||||
val eventId: EventId
|
||||
val eventId: EventId,
|
||||
val roomId: RoomId,
|
||||
)
|
||||
|
||||
suspend fun execute(params: Params) {
|
||||
|
|
@ -40,6 +42,7 @@ class PushGatewayNotifyRequest @Inject constructor(
|
|||
PushGatewayNotifyBody(
|
||||
PushGatewayNotification(
|
||||
eventId = params.eventId.value,
|
||||
roomId = params.roomId.value,
|
||||
devices = listOf(
|
||||
PushGatewayDevice(
|
||||
params.appId,
|
||||
|
|
@ -51,7 +54,7 @@ class PushGatewayNotifyRequest @Inject constructor(
|
|||
)
|
||||
|
||||
if (response.rejectedPushKeys.contains(params.pushKey)) {
|
||||
throw PushGatewayFailure.PusherRejected
|
||||
throw PushGatewayFailure.PusherRejected()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.push.impl.troubleshoot
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesMultibinding
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTest
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestDelegate
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.push.api.GetCurrentPushProvider
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesMultibinding(AppScope::class)
|
||||
class CurrentPushProviderTest @Inject constructor(
|
||||
private val getCurrentPushProvider: GetCurrentPushProvider,
|
||||
) : NotificationTroubleshootTest {
|
||||
override val order = 110
|
||||
private val delegate = NotificationTroubleshootTestDelegate(
|
||||
defaultName = "Current push provider",
|
||||
defaultDescription = "Get the name of the current provider.",
|
||||
fakeDelay = NotificationTroubleshootTestDelegate.SHORT_DELAY,
|
||||
)
|
||||
override val state: StateFlow<NotificationTroubleshootTestState> = delegate.state
|
||||
|
||||
override suspend fun run(coroutineScope: CoroutineScope) {
|
||||
delegate.start()
|
||||
val provider = getCurrentPushProvider.getCurrentPushProvider()
|
||||
if (provider != null) {
|
||||
delegate.updateState(
|
||||
description = "Current push provider: $provider",
|
||||
status = NotificationTroubleshootTestState.Status.Success
|
||||
)
|
||||
} else {
|
||||
delegate.updateState(
|
||||
description = "No push providers selected",
|
||||
status = NotificationTroubleshootTestState.Status.Failure(false)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun reset() = delegate.reset()
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.push.impl.troubleshoot
|
||||
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.SingleIn
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import javax.inject.Inject
|
||||
|
||||
@SingleIn(AppScope::class)
|
||||
class DiagnosticPushHandler @Inject constructor() {
|
||||
private val _state = MutableSharedFlow<Unit>()
|
||||
val state: SharedFlow<Unit> = _state
|
||||
|
||||
suspend fun handlePush() {
|
||||
_state.emit(Unit)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.push.impl.troubleshoot
|
||||
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.SingleIn
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import javax.inject.Inject
|
||||
|
||||
@SingleIn(AppScope::class)
|
||||
class NotificationClickHandler @Inject constructor() {
|
||||
private val _state = MutableSharedFlow<Unit>(extraBufferCapacity = 1)
|
||||
val state: SharedFlow<Unit> = _state
|
||||
|
||||
fun handleNotificationClick() {
|
||||
_state.tryEmit(Unit)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.push.impl.troubleshoot
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesMultibinding
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTest
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestDelegate
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.push.impl.notifications.NotificationDisplayer
|
||||
import io.element.android.libraries.push.impl.notifications.factories.NotificationCreator
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
@ContributesMultibinding(AppScope::class)
|
||||
class NotificationTest @Inject constructor(
|
||||
private val notificationCreator: NotificationCreator,
|
||||
private val notificationDisplayer: NotificationDisplayer,
|
||||
private val notificationClickHandler: NotificationClickHandler
|
||||
) : NotificationTroubleshootTest {
|
||||
override val order = 50
|
||||
private val delegate = NotificationTroubleshootTestDelegate(
|
||||
defaultName = "Display notification",
|
||||
defaultDescription = "Check that the application can display notification",
|
||||
fakeDelay = NotificationTroubleshootTestDelegate.SHORT_DELAY,
|
||||
)
|
||||
override val state: StateFlow<NotificationTroubleshootTestState> = delegate.state
|
||||
|
||||
override suspend fun run(coroutineScope: CoroutineScope) {
|
||||
delegate.start()
|
||||
val notification = notificationCreator.createDiagnosticNotification()
|
||||
val result = notificationDisplayer.displayDiagnosticNotification(notification)
|
||||
if (result) {
|
||||
coroutineScope.listenToNotificationClick()
|
||||
delegate.updateState(
|
||||
description = "Please click on the notification to continue the test.",
|
||||
status = NotificationTroubleshootTestState.Status.WaitingForUser
|
||||
)
|
||||
} else {
|
||||
delegate.updateState(
|
||||
description = "Cannot display the notification.",
|
||||
status = NotificationTroubleshootTestState.Status.Failure(false)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun CoroutineScope.listenToNotificationClick() = launch {
|
||||
val job = launch {
|
||||
notificationClickHandler.state.first()
|
||||
Timber.d("Notification clicked!")
|
||||
}
|
||||
val s = withTimeoutOrNull(30.seconds) {
|
||||
job.join()
|
||||
}
|
||||
job.cancel()
|
||||
if (s == null) {
|
||||
notificationDisplayer.dismissDiagnosticNotification()
|
||||
delegate.updateState(
|
||||
description = "The notification has not been clicked.",
|
||||
status = NotificationTroubleshootTestState.Status.Failure(false)
|
||||
)
|
||||
} else {
|
||||
delegate.updateState(
|
||||
description = "The notification has been clicked!",
|
||||
status = NotificationTroubleshootTestState.Status.Success
|
||||
)
|
||||
}
|
||||
}.invokeOnCompletion {
|
||||
// Ensure that the notification is cancelled when the screen is left
|
||||
notificationDisplayer.dismissDiagnosticNotification()
|
||||
}
|
||||
|
||||
override fun reset() = delegate.reset()
|
||||
}
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.push.impl.troubleshoot
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesMultibinding
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTest
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestDelegate
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.push.api.PushService
|
||||
import io.element.android.libraries.push.api.gateway.PushGatewayFailure
|
||||
import io.element.android.services.toolbox.api.systemclock.SystemClock
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
@ContributesMultibinding(AppScope::class)
|
||||
class PushLoopbackTest @Inject constructor(
|
||||
private val pushService: PushService,
|
||||
private val diagnosticPushHandler: DiagnosticPushHandler,
|
||||
private val clock: SystemClock,
|
||||
) : NotificationTroubleshootTest {
|
||||
override val order = 500
|
||||
private val delegate = NotificationTroubleshootTestDelegate(
|
||||
defaultName = "Test Push loopback",
|
||||
defaultDescription = "Ensure that the application is receiving push.",
|
||||
)
|
||||
override val state: StateFlow<NotificationTroubleshootTestState> = delegate.state
|
||||
|
||||
override suspend fun run(coroutineScope: CoroutineScope) {
|
||||
delegate.start()
|
||||
val startTime = clock.epochMillis()
|
||||
val completable = CompletableDeferred<Long>()
|
||||
val job = coroutineScope.launch {
|
||||
diagnosticPushHandler.state.first()
|
||||
completable.complete(clock.epochMillis() - startTime)
|
||||
}
|
||||
val testPushResult = try {
|
||||
pushService.testPush()
|
||||
} catch (pusherRejected: PushGatewayFailure.PusherRejected) {
|
||||
delegate.updateState(
|
||||
description = "Error: pusher has rejected the request.",
|
||||
status = NotificationTroubleshootTestState.Status.Failure(false)
|
||||
)
|
||||
job.cancel()
|
||||
return
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Failed to test push")
|
||||
delegate.updateState(
|
||||
description = "Error: ${e.message}.",
|
||||
status = NotificationTroubleshootTestState.Status.Failure(false)
|
||||
)
|
||||
job.cancel()
|
||||
return
|
||||
}
|
||||
if (!testPushResult) {
|
||||
delegate.updateState(
|
||||
description = "Error, cannot test push.",
|
||||
status = NotificationTroubleshootTestState.Status.Failure(false)
|
||||
)
|
||||
job.cancel()
|
||||
return
|
||||
}
|
||||
val result = withTimeoutOrNull(10.seconds) {
|
||||
completable.await()
|
||||
}
|
||||
job.cancel()
|
||||
if (result == null) {
|
||||
delegate.updateState(
|
||||
description = "Error, timeout waiting for push.",
|
||||
status = NotificationTroubleshootTestState.Status.Failure(false)
|
||||
)
|
||||
} else {
|
||||
delegate.updateState(
|
||||
description = "Push loopback took $result ms",
|
||||
status = NotificationTroubleshootTestState.Status.Success
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun reset() = delegate.reset()
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.push.impl.troubleshoot
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesMultibinding
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTest
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestDelegate
|
||||
import io.element.android.libraries.core.notifications.NotificationTroubleshootTestState
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.pushproviders.api.PushProvider
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesMultibinding(AppScope::class)
|
||||
class PushProvidersTest @Inject constructor(
|
||||
pushProviders: Set<@JvmSuppressWildcards PushProvider>,
|
||||
) : NotificationTroubleshootTest {
|
||||
private val sortedPushProvider = pushProviders.sortedBy { it.index }
|
||||
override val order = 100
|
||||
private val delegate = NotificationTroubleshootTestDelegate(
|
||||
defaultName = "Detect push providers",
|
||||
defaultDescription = "Ensure that the application has at least one push provider.",
|
||||
fakeDelay = NotificationTroubleshootTestDelegate.SHORT_DELAY,
|
||||
)
|
||||
override val state: StateFlow<NotificationTroubleshootTestState> = delegate.state
|
||||
|
||||
override suspend fun run(coroutineScope: CoroutineScope) {
|
||||
delegate.start()
|
||||
val result = sortedPushProvider.isNotEmpty()
|
||||
if (result) {
|
||||
delegate.updateState(
|
||||
description = "Found ${sortedPushProvider.size} push providers: ${sortedPushProvider.joinToString { it.name }}",
|
||||
status = NotificationTroubleshootTestState.Status.Success
|
||||
)
|
||||
} else {
|
||||
delegate.updateState(
|
||||
description = "No push providers found",
|
||||
status = NotificationTroubleshootTestState.Status.Failure(false)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun reset() = delegate.reset()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue