Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 8 additions & 3 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.work.Data
import androidx.work.OneTimeWorkRequest
import com.nextcloud.talk.dagger.modules.assistedViewModels
import com.nextcloud.talk.utils.setExpeditedIfSupported
import androidx.work.WorkInfo
import androidx.work.WorkManager
import autodagger.AutoInjector
Expand Down Expand Up @@ -125,6 +123,7 @@ import com.nextcloud.talk.chat.viewmodels.MessageInputViewModel
import com.nextcloud.talk.conversationinfo.ConversationInfoActivity
import com.nextcloud.talk.conversationinfo.viewmodel.ConversationInfoViewModel
import com.nextcloud.talk.conversationlist.ConversationsListActivity
import com.nextcloud.talk.dagger.modules.assistedViewModels
import com.nextcloud.talk.data.database.model.SendStatus
import com.nextcloud.talk.data.network.NetworkMonitor
import com.nextcloud.talk.data.user.model.User
Expand Down Expand Up @@ -214,6 +213,7 @@ import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_THREAD_ID
import com.nextcloud.talk.utils.keepContentOutOfDisplayCutout
import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil
import com.nextcloud.talk.utils.rx.DisposableSet
import com.nextcloud.talk.utils.setExpeditedIfSupported
import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder
import com.nextcloud.talk.webrtc.Globals
import com.nextcloud.talk.webrtc.WebSocketConnectionHelper
Expand All @@ -231,7 +231,6 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -310,6 +309,10 @@ class ChatActivity :
)
}

private val voiceRecordingInterrupter = VoiceRecordingInterrupter(this) {
chatViewModel.interruptAudioRecording()
}

lateinit var conversationInfoViewModel: ConversationInfoViewModel
val messageInputViewModel: MessageInputViewModel by viewModels()

Expand Down Expand Up @@ -636,6 +639,7 @@ class ChatActivity :
if (isSharedTextTaken) sharedText = ""
if (areSharedFilesTaken) sharedFilePaths = emptyList()
chatViewModel.initData()
voiceRecordingInterrupter.register()
if (sharedText.isNotEmpty()) {
chatViewModel.messageDraft.messageText = sharedText
chatViewModel.saveMessageDraft()
Expand Down Expand Up @@ -2861,6 +2865,7 @@ class ChatActivity :

override fun onDestroy() {
super.onDestroy()
voiceRecordingInterrupter.unregister()

if (::initialUser.isInitialized) {
logConversationInfos("onDestroy")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ class MessageInputFragment : Fragment() {
}
}

// a held recording can also end without a touch, e.g. when the app goes to the background
chatActivity.chatViewModel.getVoiceRecordingInProgress.observe(viewLifecycleOwner) { inProgress ->
if (!inProgress && binding.fragmentMessageInputView.microphoneEnabledInfo.isVisible) {
showRecordAudioUi(false)
resetSlider()
}
}

chatActivity.chatViewModel.scheduledMessagesCount.observe(viewLifecycleOwner) { count ->
if (chatActivity.conversationThreadId != null && chatActivity.conversationThreadId!! > 0) {
val threadId = chatActivity.conversationThreadId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import autodagger.AutoInjector
import com.google.android.material.snackbar.Snackbar
import com.nextcloud.android.common.ui.theme.utils.ColorRole
import com.nextcloud.talk.R
import com.nextcloud.talk.application.NextcloudTalkApplication
Expand Down Expand Up @@ -51,6 +53,7 @@ class MessageInputVoiceRecordingFragment : Fragment() {
lateinit var binding: FragmentMessageInputVoiceRecordingBinding
private lateinit var chatActivity: ChatActivity
private var pause = false
private var micInputPausedByStop = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -74,8 +77,39 @@ class MessageInputVoiceRecordingFragment : Fragment() {
this.lifecycle.removeObserver(messageInputViewModel)
}

override fun onStart() {
super.onStart()
// MessageInputViewModel stops the mic input when the fragment stops, e.g. under the media viewer
if (micInputPausedByStop && isRecordingShown()) {
messageInputViewModel.resumeMicInput(requireContext())
}
micInputPausedByStop = false
}

override fun onResume() {
super.onResume()
showInterruptionIfPending()
}

override fun onStop() {
super.onStop()
micInputPausedByStop = isRecordingShown()
}

private fun isRecordingShown() =
chatActivity.chatViewModel.getVoiceRecordingInProgress.value == true &&
binding.voicePreviewContainer.visibility != View.VISIBLE

private fun initObservers() {
messageInputViewModel.startMicInput(requireContext())

// an interruption can come while another screen already covers the chat: it is shown once the chat is back
chatActivity.chatViewModel.voiceRecordingInterrupted.observe(viewLifecycleOwner) {
if (lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
showInterruptionIfPending()
}
}

messageInputViewModel.micInputAudioObserver.observe(viewLifecycleOwner) {
binding.micInputCloud.setRotationSpeed(it.first, it.second)
}
Expand Down Expand Up @@ -164,9 +198,38 @@ class MessageInputVoiceRecordingFragment : Fragment() {
binding.audioRecordDuration.clearAnimation()
}

/**
* The recording was stopped without the user, e.g. by leaving the app: shows what was recorded like after
* stopping it by hand, so it can be played, sent or deleted.
*/
private fun showInterruptionIfPending() {
if (chatActivity.chatViewModel.voiceRecordingInterrupted.value != true) {
return
}
chatActivity.chatViewModel.onVoiceRecordingInterruptionShown()
showPreview()
Snackbar.make(
binding.root,
R.string.nc_voice_message_recording_interrupted,
Snackbar.LENGTH_LONG
).show()
}

/** Shows the preview of a stopped recording instead of the running one. */
private fun showPreview() {
pause = false
binding.seekbar.progress = 0
messageInputViewModel.stopMicInput()
binding.audioRecordDuration.visibility = View.GONE
binding.audioRecordDuration.stop()
binding.voicePreviewContainer.visibility = View.VISIBLE
}

private fun togglePreviewVisibility() {
val visibility = binding.voicePreviewContainer.visibility
binding.voicePreviewContainer.visibility = if (visibility == View.VISIBLE) {
if (binding.voicePreviewContainer.visibility != View.VISIBLE) {
chatActivity.chatViewModel.stopAudioRecording()
showPreview()
} else {
messageInputViewModel.stopMediaPlayer()
binding.playPauseBtn.icon = ContextCompat.getDrawable(
requireContext(),
Expand All @@ -178,15 +241,7 @@ class MessageInputVoiceRecordingFragment : Fragment() {
binding.audioRecordDuration.visibility = View.VISIBLE
binding.audioRecordDuration.base = SystemClock.elapsedRealtime()
binding.audioRecordDuration.start()
View.GONE
} else {
pause = false
binding.seekbar.progress = 0
messageInputViewModel.stopMicInput()
chatActivity.chatViewModel.stopAudioRecording()
binding.audioRecordDuration.visibility = View.GONE
binding.audioRecordDuration.stop()
View.VISIBLE
binding.voicePreviewContainer.visibility = View.GONE
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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

import android.app.Activity
import android.app.Application
import android.os.Bundle
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import com.nextcloud.talk.activities.CallBaseActivity

/**
* Tells when a voice recording of [chatActivity] has to end although the chat was not left: Talk went to the
* background, where Android silences the microphone, or a Talk screen started that uses the microphone itself (a
* call or another chat). Android does not silence a second capture from the same app, so these screens are checked on
* every version; captures by other apps are reported by MediaRecorderManager from Android 10 on. Other Talk screens
* on top of the chat, like the media viewer, keep the recording running.
*/
class VoiceRecordingInterrupter(private val chatActivity: Activity, private val onInterrupt: () -> Unit) :
DefaultLifecycleObserver,
Application.ActivityLifecycleCallbacks {

fun register() {
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
chatActivity.application.registerActivityLifecycleCallbacks(this)
}

fun unregister() {
ProcessLifecycleOwner.get().lifecycle.removeObserver(this)
chatActivity.application.unregisterActivityLifecycleCallbacks(this)
}

/** The whole app went to the background. */
override fun onStop(owner: LifecycleOwner) = onInterrupt()

override fun onActivityStarted(activity: Activity) {
if (activity !== chatActivity && usesMicrophone(activity)) {
onInterrupt()
}
}

private fun usesMicrophone(activity: Activity) = activity is CallBaseActivity || activity is ChatActivity

override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit

override fun onActivityResumed(activity: Activity) = Unit

override fun onActivityPaused(activity: Activity) = Unit

override fun onActivityStopped(activity: Activity) = Unit

override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit

override fun onActivityDestroyed(activity: Activity) = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ package com.nextcloud.talk.chat.data.io

import android.annotation.SuppressLint
import android.content.Context
import android.media.AudioManager
import android.media.AudioRecordingConfiguration
import android.media.MediaRecorder
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import com.nextcloud.talk.R
import com.nextcloud.talk.models.domain.ConversationModel
import java.io.IOException
Expand Down Expand Up @@ -49,11 +54,13 @@ class MediaRecorderManager : LifecycleAwareManager {
val mediaRecorderState: MediaRecorderState
get() = _mediaRecorderState
private var recorder: MediaRecorder? = null
private var silenceCallback: AudioManager.AudioRecordingCallback? = null

/**
* Initializes and starts the MediaRecorder
* Initializes and starts the MediaRecorder. [onSilenced] is called when Android silences the recording, e.g.
* because a call takes the microphone or the app went to the background (Android 10 and later).
*/
fun start(context: Context, currentConversation: ConversationModel) {
fun start(context: Context, currentConversation: ConversationModel, onSilenced: () -> Unit = {}) {
if (_mediaRecorderState == MediaRecorderState.ERROR ||
_mediaRecorderState == MediaRecorderState.RELEASED
) {
Expand All @@ -62,7 +69,7 @@ class MediaRecorderManager : LifecycleAwareManager {

if (_mediaRecorderState == MediaRecorderState.INITIAL) {
setVoiceRecordFileName(context, currentConversation)
initAndStartRecorder()
initAndStartRecorder(context, onSilenced)
} else {
Log.e(TAG, "Started MediaRecorder with invalid state ${_mediaRecorderState.name}")
}
Expand All @@ -79,7 +86,7 @@ class MediaRecorderManager : LifecycleAwareManager {
}
}

private fun initAndStartRecorder() {
private fun initAndStartRecorder(context: Context, onSilenced: () -> Unit) {
recorder = MediaRecorder().apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
_mediaRecorderState = MediaRecorderState.INITIALIZED
Expand All @@ -101,6 +108,10 @@ class MediaRecorderManager : LifecycleAwareManager {
Log.e(TAG, "prepare for audio recording failed")
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
watchForSilencing(this, context, onSilenced)
}

try {
start()
_mediaRecorderState = MediaRecorderState.RECORDING
Expand All @@ -112,8 +123,27 @@ class MediaRecorderManager : LifecycleAwareManager {
}
}

/** Registered before the recording starts, so a capture that is silenced right away is reported, too. */
@RequiresApi(Build.VERSION_CODES.Q)
private fun watchForSilencing(mediaRecorder: MediaRecorder, context: Context, onSilenced: () -> Unit) {
val callback = object : AudioManager.AudioRecordingCallback() {
override fun onRecordingConfigChanged(configs: List<AudioRecordingConfiguration>) {
if (mediaRecorder.activeRecordingConfiguration?.isClientSilenced == true) {
Log.d(TAG, "recording silenced")
onSilenced()
}
}
}
mediaRecorder.registerAudioRecordingCallback(ContextCompat.getMainExecutor(context), callback)
silenceCallback = callback
}

@Suppress("TooGenericExceptionCaught")
private fun stopAndDestroyRecorder() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
silenceCallback?.let { recorder?.unregisterAudioRecordingCallback(it) }
}
silenceCallback = null
recorder?.apply {
try {
if (_mediaRecorderState == MediaRecorderState.RECORDING) {
Expand Down
Loading
Loading