handle becoming noisy manually

Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
parneet-guraya 2024-01-05 15:17:54 +05:30
parent 91d5217b1e
commit 86cd617c6a
No known key found for this signature in database
GPG key ID: 26DB680F1EE174D5
2 changed files with 43 additions and 0 deletions

View file

@ -31,10 +31,12 @@ package com.nextcloud.talk.chat
import android.Manifest import android.Manifest
import android.animation.ObjectAnimator import android.animation.ObjectAnimator
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.ClipData import android.content.ClipData
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.AssetFileDescriptor import android.content.res.AssetFileDescriptor
import android.content.res.Resources import android.content.res.Resources
@ -388,6 +390,18 @@ class ChatActivity :
private val audioFocusChangeListener = getAudioFocusChangeListener() private val audioFocusChangeListener = getAudioFocusChangeListener()
private val noisyAudioStreamReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
chatViewModel.isPausedDueToBecomingNoisy = true
if (isVoicePreviewPlaying) {
pausePreviewVoicePlaying()
}
if (currentlyPlayedVoiceMessage != null) {
pausePlayback(currentlyPlayedVoiceMessage!!)
}
}
}
private lateinit var participantPermissions: ParticipantPermissions private lateinit var participantPermissions: ParticipantPermissions
private var videoURI: Uri? = null private var videoURI: Uri? = null
@ -1337,6 +1351,7 @@ class ChatActivity :
audioFocusRequest(true) { audioFocusRequest(true) {
voicePreviewMediaPlayer!!.start() voicePreviewMediaPlayer!!.start()
voicePreviewObjectAnimator!!.start() voicePreviewObjectAnimator!!.start()
handleBecomingNoisyBroadcast(register = true)
} }
} }
@ -1354,6 +1369,7 @@ class ChatActivity :
audioFocusRequest(true) { audioFocusRequest(true) {
voicePreviewMediaPlayer!!.start() voicePreviewMediaPlayer!!.start()
voicePreviewObjectAnimator!!.resume() voicePreviewObjectAnimator!!.resume()
handleBecomingNoisyBroadcast(register = true)
} }
} }
} }
@ -1363,6 +1379,7 @@ class ChatActivity :
audioFocusRequest(false) { audioFocusRequest(false) {
voicePreviewMediaPlayer!!.pause() voicePreviewMediaPlayer!!.pause()
voicePreviewObjectAnimator!!.pause() voicePreviewObjectAnimator!!.pause()
handleBecomingNoisyBroadcast(register = false)
} }
} }
@ -1377,6 +1394,7 @@ class ChatActivity :
voicePreviewMediaPlayer!!.stop() voicePreviewMediaPlayer!!.stop()
voicePreviewMediaPlayer!!.release() voicePreviewMediaPlayer!!.release()
voicePreviewMediaPlayer = null voicePreviewMediaPlayer = null
handleBecomingNoisyBroadcast(register = false)
} }
} }
} }
@ -1835,6 +1853,7 @@ class ChatActivity :
return AudioManager.OnAudioFocusChangeListener { flag -> return AudioManager.OnAudioFocusChangeListener { flag ->
when (flag) { when (flag) {
AudioManager.AUDIOFOCUS_LOSS -> { AudioManager.AUDIOFOCUS_LOSS -> {
chatViewModel.isPausedDueToBecomingNoisy = false
if (isVoicePreviewPlaying) { if (isVoicePreviewPlaying) {
stopPreviewVoicePlaying() stopPreviewVoicePlaying()
} }
@ -1844,6 +1863,7 @@ class ChatActivity :
} }
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> { AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
chatViewModel.isPausedDueToBecomingNoisy = false
if (isVoicePreviewPlaying) { if (isVoicePreviewPlaying) {
pausePreviewVoicePlaying() pausePreviewVoicePlaying()
} }
@ -1856,6 +1876,10 @@ class ChatActivity :
} }
private fun audioFocusRequest(shouldRequestFocus: Boolean, onGranted: () -> Unit) { private fun audioFocusRequest(shouldRequestFocus: Boolean, onGranted: () -> Unit) {
if (chatViewModel.isPausedDueToBecomingNoisy) {
onGranted()
return
}
val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
val duration = AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE val duration = AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE
@ -1881,6 +1905,17 @@ class ChatActivity :
} }
} }
private fun handleBecomingNoisyBroadcast(register: Boolean) {
if (register && !chatViewModel.receiverRegistered) {
registerReceiver(noisyAudioStreamReceiver, IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY))
chatViewModel.receiverRegistered = true
} else if (!chatViewModel.receiverUnregistered) {
unregisterReceiver(noisyAudioStreamReceiver)
chatViewModel.receiverUnregistered = true
chatViewModel.receiverRegistered = false
}
}
private fun startPlayback(message: ChatMessage) { private fun startPlayback(message: ChatMessage) {
if (!active) { if (!active) {
// don't begin to play voice message if screen is not visible anymore. // don't begin to play voice message if screen is not visible anymore.
@ -1896,6 +1931,7 @@ class ChatActivity :
if (!it.isPlaying) { if (!it.isPlaying) {
audioFocusRequest(true) { audioFocusRequest(true) {
it.start() it.start()
handleBecomingNoisyBroadcast(register = true)
} }
} }
@ -1933,6 +1969,7 @@ class ChatActivity :
if (mediaPlayer!!.isPlaying) { if (mediaPlayer!!.isPlaying) {
audioFocusRequest(false) { audioFocusRequest(false) {
mediaPlayer!!.pause() mediaPlayer!!.pause()
handleBecomingNoisyBroadcast(register = false)
} }
} }
@ -1993,6 +2030,7 @@ class ChatActivity :
Log.d(TAG, "media player is stopped") Log.d(TAG, "media player is stopped")
audioFocusRequest(false) { audioFocusRequest(false) {
it.stop() it.stop()
handleBecomingNoisyBroadcast(register = false)
} }
} }
} }

View file

@ -48,6 +48,11 @@ class ChatViewModel @Inject constructor(private val repository: ChatRepository)
open class GetReminderExistState(val reminder: Reminder) : ViewState open class GetReminderExistState(val reminder: Reminder) : ViewState
private val _getReminderExistState: MutableLiveData<ViewState> = MutableLiveData(GetReminderStartState) private val _getReminderExistState: MutableLiveData<ViewState> = MutableLiveData(GetReminderStartState)
var isPausedDueToBecomingNoisy = false
var receiverRegistered = false
var receiverUnregistered = false
val getReminderExistState: LiveData<ViewState> val getReminderExistState: LiveData<ViewState>
get() = _getReminderExistState get() = _getReminderExistState