From 1377e192a155cdd6f5262ef49c4070414adefea3 Mon Sep 17 00:00:00 2001 From: fanchao Date: Fri, 17 May 2024 14:02:34 +1000 Subject: [PATCH 1/8] Stop playing message if deleted --- .../securesms/audio/AudioSlidePlayer.java | 5 ++++ .../conversation/v2/ConversationActivityV2.kt | 1 + .../conversation/v2/ConversationViewModel.kt | 29 +++++++++++++++---- .../v2/messages/VoiceMessageView.kt | 2 +- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java b/app/src/main/java/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java index 61a92105aa..ef404bb070 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java +++ b/app/src/main/java/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java @@ -80,6 +80,11 @@ public class AudioSlidePlayer implements SensorEventListener { } } + @Nullable + public synchronized static AudioSlidePlayer getInstance() { + return playing.orNull(); + } + private AudioSlidePlayer(@NonNull Context context, @NonNull AudioSlide slide, @NonNull Listener listener) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 84f43e014b..781cc2b4e7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -103,6 +103,7 @@ import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.attachments.ScreenshotObserver import org.thoughtcrime.securesms.audio.AudioRecorder +import org.thoughtcrime.securesms.audio.AudioSlidePlayer import org.thoughtcrime.securesms.components.emoji.RecentEmojiPageModel import org.thoughtcrime.securesms.contacts.SelectContactsActivity.Companion.selectedContactsKey import org.thoughtcrime.securesms.conversation.ConversationActionBarDelegate diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt index 80d6df87fe..1bfd19274c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt @@ -27,10 +27,11 @@ import org.session.libsession.utilities.Address import org.session.libsession.utilities.recipients.Recipient import org.session.libsignal.utilities.IdPrefix import org.session.libsignal.utilities.Log -import org.thoughtcrime.securesms.database.MmsSmsDatabase +import org.thoughtcrime.securesms.audio.AudioSlidePlayer import org.thoughtcrime.securesms.database.Storage import org.thoughtcrime.securesms.database.model.MessageRecord +import org.thoughtcrime.securesms.database.model.MmsMessageRecord import org.thoughtcrime.securesms.repository.ConversationRepository import java.util.UUID @@ -95,14 +96,15 @@ class ConversationViewModel( init { viewModelScope.launch(Dispatchers.IO) { repository.recipientUpdateFlow(threadId) - .collect { recipient -> - if (recipient == null && _uiState.value.conversationExists) { - _uiState.update { it.copy(conversationExists = false) } - } - } } } + override fun onCleared() { + super.onCleared() + + AudioSlidePlayer.stopAll() + } + fun saveDraft(text: String) { GlobalScope.launch(Dispatchers.IO) { repository.saveDraft(threadId, text) @@ -142,10 +144,24 @@ class ConversationViewModel( } fun deleteLocally(message: MessageRecord) { + stopPlayingAudioMessage(message) val recipient = recipient ?: return Log.w("Loki", "Recipient was null for delete locally action") repository.deleteLocally(recipient, message) } + /** + * Stops audio player if its current playing is the one given in the message. + */ + private fun stopPlayingAudioMessage(message: MessageRecord) { + val player = AudioSlidePlayer.getInstance() + val audioSlide = player?.audioSlide + if (audioSlide != null && + message is MmsMessageRecord && + message.slideDeck.audioSlide == audioSlide) { + player.stop() + } + } + fun setRecipientApproved() { val recipient = recipient ?: return Log.w("Loki", "Recipient was null for set approved action") repository.setApproved(recipient, true) @@ -157,6 +173,7 @@ class ConversationViewModel( repository.deleteForEveryone(threadId, recipient, message) .onSuccess { Log.d("Loki", "Deleted message ${message.id} ") + stopPlayingAudioMessage(message) } .onFailure { Log.w("Loki", "FAILED TO delete message ${message.id} ") diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt index 2b829af152..06a5168a99 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt @@ -68,7 +68,7 @@ class VoiceMessageView : RelativeLayout, AudioSlidePlayer.Listener { return } - val player = AudioSlidePlayer.createFor(context, audio, this) + val player = AudioSlidePlayer.createFor(context.applicationContext, audio, this) this.player = player (audio.asAttachment() as? DatabaseAttachment)?.let { attachment -> From 580bf9ebb3ec9be2ad7286bb319bcbc648c99aff Mon Sep 17 00:00:00 2001 From: fanchao Date: Fri, 17 May 2024 14:07:52 +1000 Subject: [PATCH 2/8] Accidental change --- .../securesms/conversation/v2/ConversationViewModel.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt index 1bfd19274c..2f22edeafe 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt @@ -96,6 +96,11 @@ class ConversationViewModel( init { viewModelScope.launch(Dispatchers.IO) { repository.recipientUpdateFlow(threadId) + .collect { recipient -> + if (recipient == null && _uiState.value.conversationExists) { + _uiState.update { it.copy(conversationExists = false) } + } + } } } From 23872afeb44237db402d3ef6e2ce343a8838f87e Mon Sep 17 00:00:00 2001 From: fanchao Date: Fri, 17 May 2024 14:09:10 +1000 Subject: [PATCH 3/8] Accidental change --- .../securesms/conversation/v2/ConversationViewModel.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt index 2f22edeafe..6e4b51cf39 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt @@ -96,11 +96,11 @@ class ConversationViewModel( init { viewModelScope.launch(Dispatchers.IO) { repository.recipientUpdateFlow(threadId) - .collect { recipient -> - if (recipient == null && _uiState.value.conversationExists) { - _uiState.update { it.copy(conversationExists = false) } - } + .collect { recipient -> + if (recipient == null && _uiState.value.conversationExists) { + _uiState.update { it.copy(conversationExists = false) } } + } } } From e49d017b0831da74a69c77a2ec75e44a06e280c6 Mon Sep 17 00:00:00 2001 From: fanchao Date: Fri, 17 May 2024 14:09:40 +1000 Subject: [PATCH 4/8] Comments --- .../securesms/conversation/v2/ConversationViewModel.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt index 6e4b51cf39..b64cb10556 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt @@ -107,6 +107,7 @@ class ConversationViewModel( override fun onCleared() { super.onCleared() + // Stop all voice message when existing the convo page AudioSlidePlayer.stopAll() } From 03893973b7bdcf22b3acd8f7eca73d48a10bebc8 Mon Sep 17 00:00:00 2001 From: fanchao Date: Mon, 20 May 2024 09:34:40 +1000 Subject: [PATCH 5/8] Feedback --- .../conversation/v2/ConversationViewModel.kt | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt index b64cb10556..ce35a85910 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt @@ -107,7 +107,7 @@ class ConversationViewModel( override fun onCleared() { super.onCleared() - // Stop all voice message when existing the convo page + // Stop all voice message when exiting the convo page AudioSlidePlayer.stopAll() } @@ -159,13 +159,9 @@ class ConversationViewModel( * Stops audio player if its current playing is the one given in the message. */ private fun stopPlayingAudioMessage(message: MessageRecord) { - val player = AudioSlidePlayer.getInstance() - val audioSlide = player?.audioSlide - if (audioSlide != null && - message is MmsMessageRecord && - message.slideDeck.audioSlide == audioSlide) { - player.stop() - } + val mmsMessage = message as? MmsMessageRecord ?: return + val audioSlide = mmsMessage.slideDeck.audioSlide ?: return + AudioSlidePlayer.getInstance()?.takeIf { it.audioSlide == audioSlide }?.stop() } fun setRecipientApproved() { From a8cc9e24097945c4a77f71da8022b2e0173ad783 Mon Sep 17 00:00:00 2001 From: fanchao Date: Mon, 20 May 2024 09:50:29 +1000 Subject: [PATCH 6/8] Comments --- .../securesms/conversation/v2/ConversationViewModel.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt index ce35a85910..764e97e10a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt @@ -107,7 +107,7 @@ class ConversationViewModel( override fun onCleared() { super.onCleared() - // Stop all voice message when exiting the convo page + // Stop all voice message when exiting this page AudioSlidePlayer.stopAll() } From 10597f1f3096141394c536233b597021a915935e Mon Sep 17 00:00:00 2001 From: fanchao Date: Mon, 20 May 2024 09:53:44 +1000 Subject: [PATCH 7/8] Import --- .../securesms/conversation/v2/ConversationActivityV2.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 781cc2b4e7..84f43e014b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -103,7 +103,6 @@ import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.attachments.ScreenshotObserver import org.thoughtcrime.securesms.audio.AudioRecorder -import org.thoughtcrime.securesms.audio.AudioSlidePlayer import org.thoughtcrime.securesms.components.emoji.RecentEmojiPageModel import org.thoughtcrime.securesms.contacts.SelectContactsActivity.Companion.selectedContactsKey import org.thoughtcrime.securesms.conversation.ConversationActionBarDelegate From 0d3a33e6c6e571d73089d0ea6286aad28a4b54de Mon Sep 17 00:00:00 2001 From: fanchao Date: Mon, 27 May 2024 13:25:56 +1000 Subject: [PATCH 8/8] Fix delete message for everyone doesn't stop the audio playing --- .../securesms/conversation/v2/ConversationViewModel.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt index 764e97e10a..1a036eee11 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationViewModel.kt @@ -171,6 +171,7 @@ class ConversationViewModel( fun deleteForEveryone(message: MessageRecord) = viewModelScope.launch { val recipient = recipient ?: return@launch Log.w("Loki", "Recipient was null for delete for everyone - aborting delete operation.") + stopPlayingAudioMessage(message) repository.deleteForEveryone(threadId, recipient, message) .onSuccess {