Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ac643962a | ||
|
|
eaaee8ad0b |
No files matched your search
@@ -109,6 +109,9 @@ class VoiceMessageMediaService : MediaSessionService() {
|
||||
mediaSession = MediaSession.Builder(this, player)
|
||||
.setCallback(SessionCallback())
|
||||
.build()
|
||||
// By default Media3 keeps the service in the foreground for 10 minutes after a pause or the end of the
|
||||
// message, and the notification of a foreground service cannot be swiped away.
|
||||
setForegroundServiceTimeoutMs(FOREGROUND_TIMEOUT_AFTER_PAUSE_MS)
|
||||
|
||||
setMediaNotificationProvider(
|
||||
DefaultMediaNotificationProvider.Builder(this).build().apply {
|
||||
@@ -327,6 +330,9 @@ class VoiceMessageMediaService : MediaSessionService() {
|
||||
private const val MAX_AUTOMATIC_RETRIES = 3
|
||||
private const val RETRY_DELAY_MS = 1000L
|
||||
|
||||
/** The service leaves the foreground, and its notification can be swiped away, as soon as playback stops. */
|
||||
const val FOREGROUND_TIMEOUT_AFTER_PAUSE_MS = 0L
|
||||
|
||||
private fun audioAttributesFor(kind: ChatAudioKind): AudioAttributes =
|
||||
AudioAttributes.Builder()
|
||||
.setUsage(C.USAGE_MEDIA)
|
||||
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Nextcloud Talk - Android Client
|
||||
*
|
||||
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
package com.nextcloud.talk.chat.data.io
|
||||
|
||||
import androidx.media3.datasource.cache.CacheDataSource
|
||||
import androidx.media3.session.MediaSessionService
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.nextcloud.talk.application.NextcloudTalkApplication
|
||||
import com.nextcloud.talk.chat.audio.ChatAudioDataSources
|
||||
import com.nextcloud.talk.chat.audio.ChatAudioStore
|
||||
import com.nextcloud.talk.application.NextcloudTalkApplicationComponent
|
||||
import com.nextcloud.talk.data.network.NetworkMonitor
|
||||
import com.nextcloud.talk.utils.preferences.AppPreferences
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.doAnswer
|
||||
import org.mockito.kotlin.doReturn
|
||||
import org.mockito.kotlin.mock
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
/**
|
||||
* Media3 keeps a media service in the foreground, with a notification that cannot be swiped away, for
|
||||
* [MediaSessionService.DEFAULT_FOREGROUND_SERVICE_TIMEOUT_MS] (10 minutes) after the player is paused or ends. The
|
||||
* voice message service must leave the foreground at once.
|
||||
*/
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33])
|
||||
class VoiceMessageMediaServiceForegroundTest {
|
||||
@Before
|
||||
fun setUp() {
|
||||
// the service injects its dependencies from the application component in onCreate()
|
||||
val component = mock<NextcloudTalkApplicationComponent> {
|
||||
on { inject(any<VoiceMessageMediaService>()) } doAnswer {
|
||||
(it.arguments[0] as VoiceMessageMediaService).apply {
|
||||
dataSources = mock<ChatAudioDataSources> {
|
||||
on { cacheDataSourceFactory } doReturn CacheDataSource.Factory()
|
||||
}
|
||||
store = ChatAudioStore(ApplicationProvider.getApplicationContext())
|
||||
waveformLoader = mock()
|
||||
networkMonitor = mock<NetworkMonitor>()
|
||||
appPreferences = mock<AppPreferences> { on { getChatAudioRepeatMode() } doReturn "OFF" }
|
||||
}
|
||||
Unit
|
||||
}
|
||||
}
|
||||
setSharedApplication(mock<NextcloudTalkApplication> { on { componentApplication } doReturn component })
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
setSharedApplication(null)
|
||||
}
|
||||
|
||||
// The setter of sharedApplication is protected; reflection is the only way in from a test.
|
||||
private fun setSharedApplication(application: NextcloudTalkApplication?) {
|
||||
NextcloudTalkApplication.Companion::class.java
|
||||
.getDeclaredMethod("setSharedApplication", NextcloudTalkApplication::class.java)
|
||||
.apply { isAccessible = true }
|
||||
.invoke(NextcloudTalkApplication.Companion, application)
|
||||
}
|
||||
|
||||
/** The timeout Media3 applies to the notification manager of [service], read by reflection (no getter). */
|
||||
private fun foregroundTimeoutMs(service: VoiceMessageMediaService): Long {
|
||||
val getManager = MediaSessionService::class.java.getDeclaredMethod("getMediaNotificationManager")
|
||||
getManager.isAccessible = true
|
||||
val manager = getManager.invoke(service)
|
||||
val field = manager.javaClass.getDeclaredField("userEngagedTimeoutMs")
|
||||
field.isAccessible = true
|
||||
return field.getLong(manager)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `service does not stay in the foreground after the voice message is paused or ends`() {
|
||||
val service = Robolectric.buildService(VoiceMessageMediaService::class.java).create().get()
|
||||
|
||||
assertEquals(0L, foregroundTimeoutMs(service))
|
||||
service.onDestroy()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user