Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Audio message play in background WPB-11725 #3766

Merged
merged 37 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
88a706f
feat: Audio messages new design
borichellow Nov 29, 2024
25b582b
Merge branch 'develop' into feat/audio_messages_new_design
borichellow Nov 29, 2024
3ffc762
Work in progress
borichellow Nov 29, 2024
3ff7627
Merge branch 'develop' into feat/audio_messages_new_design
borichellow Dec 2, 2024
0e0f03e
Audio messages new design almost ready
borichellow Dec 9, 2024
601ce01
Merge branch 'develop' into feat/audio_messages_new_design
borichellow Dec 9, 2024
0e2dd74
Merge branch 'develop' into feat/audio_messages_new_design
borichellow Dec 9, 2024
c79f7ef
Fixed code style
borichellow Dec 9, 2024
15f3303
Fixed code style and some tests
borichellow Dec 9, 2024
e9ef69c
Fix tests
borichellow Dec 9, 2024
182d04e
Updated tests
borichellow Dec 10, 2024
7260a2b
Merge branch 'develop' into feat/audio_messages_new_design
borichellow Dec 10, 2024
a8ae13c
Code style fixes
borichellow Dec 11, 2024
49c749c
Merge branch 'develop' into feat/audio_messages_new_design
borichellow Dec 16, 2024
b1862e8
Added JumpToPlayingAudio Button
borichellow Dec 16, 2024
9add5f4
Merge branch 'develop' into feat/audio_messages_new_design
borichellow Dec 16, 2024
5fee401
finished JumpToPlayingAudioButton
borichellow Dec 17, 2024
c8149dc
Merge branch 'develop' into feat/audio_messages_new_design
borichellow Dec 17, 2024
6ceb9b2
Review updates
borichellow Dec 17, 2024
6c29136
Code style fix
borichellow Dec 17, 2024
4be2efe
feat: Audio message play in background
borichellow Dec 23, 2024
8711849
Updated kalium
borichellow Dec 23, 2024
5644f0a
Added play control buttons for paginated list too
borichellow Dec 23, 2024
a672a62
Merge develop into feat/audio_messages_play_in_background
borichellow Dec 27, 2024
d0d0ffb
Fix code-style
borichellow Dec 27, 2024
f8dafe2
Fixed some tests
borichellow Jan 2, 2025
849e47b
Merge develop into feat/audio_messages_play_in_background
borichellow Jan 2, 2025
caa70fc
Fixed tests
borichellow Jan 3, 2025
81febf8
Code style fix
borichellow Jan 3, 2025
b0ca7cc
Merge branch 'develop' into feat/audio_messages_play_in_background
yamilmedina Jan 6, 2025
d7f836a
Merge develop into feat/audio_messages_play_in_background
borichellow Jan 15, 2025
4148493
Merge branch 'feat/audio_messages_play_in_background' of github.com:w…
borichellow Jan 15, 2025
a6a3a4f
Merge develop into feat/audio_messages_play_in_background
borichellow Jan 21, 2025
b207f29
feat: AudioMessages play in background part 2 #WPB-11725 (#3802)
borichellow Jan 21, 2025
4fe12ad
Merge branch 'develop' into feat/audio_messages_play_in_background
borichellow Jan 21, 2025
43a91ff
Fixed code style
borichellow Jan 21, 2025
b48337e
Merge branch 'develop' into feat/audio_messages_play_in_background
borichellow Jan 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />

<!-- needed to switch between speaker/earpiece -->
<uses-permission android:name="android.permission.BLUETOOTH" />
Expand Down Expand Up @@ -313,6 +314,12 @@
<receiver
android:name=".notification.broadcastreceivers.EndOngoingCallReceiver"
android:exported="false" />
<receiver
android:name=".notification.broadcastreceivers.PlayPauseAudioMessageReceiver"
android:exported="false" />
<receiver
android:name=".notification.broadcastreceivers.StopAudioMessageReceiver"
android:exported="false" />

<service
android:name=".services.WireFirebaseMessagingService"
Expand Down Expand Up @@ -351,6 +358,11 @@
android:name=".services.CallService"
android:exported="false"
android:foregroundServiceType="phoneCall|microphone" />

<service
android:name=".services.PlayingAudioMessageService"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
</application>

</manifest>
6 changes: 6 additions & 0 deletions app/src/main/kotlin/com/wire/android/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.app.NotificationManager
import android.content.Context
import android.location.Geocoder
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.MediaPlayer
import androidx.core.app.NotificationManagerCompat
import com.wire.android.BuildConfig
Expand All @@ -48,6 +49,7 @@ annotation class CurrentAppVersion

@Module
@InstallIn(SingletonComponent::class)
@Suppress("TooManyFunctions")
object AppModule {

@CurrentAppVersion
Expand Down Expand Up @@ -104,4 +106,8 @@ object AppModule {

@Provides
fun provideAnonymousAnalyticsManager(): AnonymousAnalyticsManager = AnonymousAnalyticsManagerImpl

@Provides
fun provideAudioManager(@ApplicationContext context: Context): AudioManager =
context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
}
32 changes: 29 additions & 3 deletions app/src/main/kotlin/com/wire/android/mapper/ConversationMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.wire.android.mapper

import com.wire.android.media.audiomessage.AudioMediaPlayingState
import com.wire.android.media.audiomessage.PlayingAudioMessage
import com.wire.android.model.ImageAsset.UserAvatarAsset
import com.wire.android.model.NameBasedAvatar
import com.wire.android.model.UserAvatarData
Expand All @@ -25,7 +27,9 @@ import com.wire.android.ui.home.conversationslist.model.BadgeEventType
import com.wire.android.ui.home.conversationslist.model.BlockState
import com.wire.android.ui.home.conversationslist.model.ConversationInfo
import com.wire.android.ui.home.conversationslist.model.ConversationItem
import com.wire.android.ui.home.conversationslist.model.PlayingAudioInConversation
import com.wire.android.ui.home.conversationslist.showLegalHoldIndicator
import com.wire.kalium.logic.data.conversation.ConversationDetails
import com.wire.kalium.logic.data.conversation.ConversationDetails.Connection
import com.wire.kalium.logic.data.conversation.ConversationDetails.Group
import com.wire.kalium.logic.data.conversation.ConversationDetails.OneOne
Expand All @@ -42,7 +46,8 @@ import com.wire.kalium.logic.data.user.UserAvailabilityStatus
fun ConversationDetailsWithEvents.toConversationItem(
userTypeMapper: UserTypeMapper,
searchQuery: String,
selfUserTeamId: TeamId?
selfUserTeamId: TeamId?,
playingAudioMessage: PlayingAudioMessage
): ConversationItem = when (val conversationDetails = this.conversationDetails) {
is Group -> {
ConversationItem.GroupConversation(
Expand All @@ -66,7 +71,8 @@ fun ConversationDetailsWithEvents.toConversationItem(
hasNewActivitiesToShow = hasNewActivitiesToShow,
searchQuery = searchQuery,
isFavorite = conversationDetails.isFavorite,
folder = conversationDetails.folder
folder = conversationDetails.folder,
playingAudio = getPlayingAudioInConversation(playingAudioMessage, conversationDetails)
)
}

Expand Down Expand Up @@ -105,7 +111,8 @@ fun ConversationDetailsWithEvents.toConversationItem(
hasNewActivitiesToShow = hasNewActivitiesToShow,
searchQuery = searchQuery,
isFavorite = conversationDetails.isFavorite,
folder = conversationDetails.folder
folder = conversationDetails.folder,
playingAudio = getPlayingAudioInConversation(playingAudioMessage, conversationDetails)
)
}

Expand Down Expand Up @@ -142,6 +149,25 @@ fun ConversationDetailsWithEvents.toConversationItem(
}
}

private fun getPlayingAudioInConversation(
playingAudioMessage: PlayingAudioMessage,
conversationDetails: ConversationDetails
): PlayingAudioInConversation? =
if (playingAudioMessage is PlayingAudioMessage.Some
&& playingAudioMessage.conversationId == conversationDetails.conversation.id
) {
if (playingAudioMessage.state.isPlaying()) {
PlayingAudioInConversation(playingAudioMessage.messageId, false)
} else if (playingAudioMessage.state.audioMediaPlayingState is AudioMediaPlayingState.Paused) {
PlayingAudioInConversation(playingAudioMessage.messageId, true)
} else {
// states Fetching, Completed, Stopped, etc. should not be shown in ConversationItem
null
}
} else {
null
}

private fun parseConnectionEventType(connectionState: ConnectionState) =
if (connectionState == ConnectionState.SENT) {
BadgeEventType.SentConnectRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Wire
* Copyright (C) 2025 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.media.audiomessage

import android.media.AudioFocusRequest
import android.media.AudioManager
import android.os.Build
import javax.inject.Inject

class AudioFocusHelper @Inject constructor(private val audioManager: AudioManager) {

private var listener: PlayPauseListener? = null

private val onAudioFocusChangeListener by lazy {
AudioManager.OnAudioFocusChangeListener { focusChange ->
when (focusChange) {
AudioManager.AUDIOFOCUS_LOSS -> {
listener?.onPauseCurrentAudio()
}

AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
listener?.onPauseCurrentAudio()
}

AudioManager.AUDIOFOCUS_GAIN -> {
listener?.onResumeCurrentAudio()
}

AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
listener?.onPauseCurrentAudio()
}
}
}
}

private val focusRequest by lazy {
AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)
.setOnAudioFocusChangeListener(onAudioFocusChangeListener)
.apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) setForceDucking(true)
}.build()
}

/**
* Requests the audio focus.
* @return true in case if focus was granted (AudioMessage can be played), false - otherwise
*/
fun request(): Boolean {
return audioManager.requestAudioFocus(focusRequest) != AudioManager.AUDIOFOCUS_REQUEST_FAILED
}

/**
* Abandon the audio focus.
*/
fun abandon() {
audioManager.abandonAudioFocusRequest(focusRequest)
}

fun setListener(onPauseCurrentAudio: () -> Unit, onResumeCurrentAudio: () -> Unit) {
listener = object : PlayPauseListener {
override fun onPauseCurrentAudio() {
onPauseCurrentAudio()
}

override fun onResumeCurrentAudio() {
onResumeCurrentAudio()
}
}
}

interface PlayPauseListener {
fun onPauseCurrentAudio()
fun onResumeCurrentAudio()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ package com.wire.android.media.audiomessage

import androidx.annotation.StringRes
import com.wire.android.R
import com.wire.android.media.audiomessage.ConversationAudioMessagePlayer.MessageIdWrapper
import com.wire.android.util.ui.UIText
import com.wire.kalium.logic.data.id.ConversationId

data class AudioMessagesData(
val statesHistory: Map<MessageIdWrapper, AudioState>,
val playingMessage: PlayingAudioMessage
)

data class AudioState(
val audioMediaPlayingState: AudioMediaPlayingState,
Expand All @@ -40,13 +48,43 @@ data class AudioState(
return totalTimeInMs
}

fun isPlaying() = audioMediaPlayingState is AudioMediaPlayingState.Playing
fun isPlayingOrPaused() = audioMediaPlayingState is AudioMediaPlayingState.Playing
|| audioMediaPlayingState is AudioMediaPlayingState.Paused

fun isPlayingOrPausedOrFetching() = audioMediaPlayingState is AudioMediaPlayingState.Playing
|| audioMediaPlayingState is AudioMediaPlayingState.Paused
|| audioMediaPlayingState is AudioMediaPlayingState.Fetching
|| audioMediaPlayingState is AudioMediaPlayingState.SuccessfulFetching

sealed class TotalTimeInMs {
object NotKnown : TotalTimeInMs()

data class Known(val value: Int) : TotalTimeInMs()
}
}

sealed class PlayingAudioMessage {
data object None : PlayingAudioMessage()
data class Some(
val conversationId: ConversationId,
val messageId: String,
val authorName: UIText,
val state: AudioState
) : PlayingAudioMessage()

fun isSameAs(that: PlayingAudioMessage): Boolean {
val isTypeSame = (this is Some && that is Some)
|| (this is None && that is None)

val isMessageSame = this is Some && that is Some
&& this.messageId == that.messageId
&& this.state.isPlaying() == that.state.isPlaying()

return isTypeSame && isMessageSame
}
}

@Suppress("MagicNumber")
enum class AudioSpeed(val value: Float, @StringRes val titleRes: Int) {
NORMAL(1f, R.string.audio_speed_1),
Expand Down Expand Up @@ -84,27 +122,32 @@ sealed class AudioMediaPlayingState {
}

sealed class AudioMediaPlayerStateUpdate(
open val conversationId: ConversationId,
open val messageId: String
) {
data class AudioMediaPlayingStateUpdate(
override val conversationId: ConversationId,
override val messageId: String,
val audioMediaPlayingState: AudioMediaPlayingState
) : AudioMediaPlayerStateUpdate(messageId)
) : AudioMediaPlayerStateUpdate(conversationId, messageId)

data class PositionChangeUpdate(
override val conversationId: ConversationId,
override val messageId: String,
val position: Int
) : AudioMediaPlayerStateUpdate(messageId)
) : AudioMediaPlayerStateUpdate(conversationId, messageId)

data class TotalTimeUpdate(
override val conversationId: ConversationId,
override val messageId: String,
val totalTimeInMs: Int
) : AudioMediaPlayerStateUpdate(messageId)
) : AudioMediaPlayerStateUpdate(conversationId, messageId)

data class WaveMaskUpdate(
override val conversationId: ConversationId,
override val messageId: String,
val waveMask: List<Int>
) : AudioMediaPlayerStateUpdate(messageId)
) : AudioMediaPlayerStateUpdate(conversationId, messageId)
}

sealed class RecordAudioMediaPlayerStateUpdate {
Expand Down
Loading
Loading