diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/voice/VoiceNotePlaybackService.java b/app/src/main/java/org/thoughtcrime/securesms/components/voice/VoiceNotePlaybackService.java index d1346b6744..f73be63ec1 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/voice/VoiceNotePlaybackService.java +++ b/app/src/main/java/org/thoughtcrime/securesms/components/voice/VoiceNotePlaybackService.java @@ -220,32 +220,54 @@ public class VoiceNotePlaybackService extends MediaSessionService { private void onAttachmentDeleted() { Log.d(TAG, "Database attachment observer invoked."); ContextCompat.getMainExecutor(getApplicationContext()).execute(() -> { - if (player != null) { - final MediaItem currentItem = player.getCurrentMediaItem(); - if (currentItem == null || currentItem.localConfiguration == null) { - Log.d(TAG, "Current item is null or playback properties are null."); - return; + if (player == null) return; + + for (int i = player.getMediaItemCount() - 1; i >= 0; i--) { + MediaItem item = player.getMediaItemAt(i); + MediaItem.LocalConfiguration config = item.localConfiguration; + + if (config == null) { + Log.d(TAG, "Media item at index " + i + " has null configuration. Skipping."); + continue; } - final Uri currentlyPlayingUri = currentItem.localConfiguration.uri; + Uri uri = config.uri; - if (currentlyPlayingUri == VoiceNoteMediaItemFactory.NEXT_URI || currentlyPlayingUri == VoiceNoteMediaItemFactory.END_URI) { - Log.v(TAG, "Attachment deleted while voice note service was playing a system tone."); + if (VoiceNoteMediaItemFactory.NEXT_URI.equals(uri) || VoiceNoteMediaItemFactory.END_URI.equals(uri)) { + Log.v(TAG, "Skipping system tone media item at index " + i); + continue; } try { - final AttachmentId partId = new PartUriParser(currentlyPlayingUri).getPartId(); - final DatabaseAttachment attachment = SignalDatabase.attachments().getAttachment(partId); + AttachmentId partId = new PartUriParser(uri).getPartId(); + DatabaseAttachment attachment = SignalDatabase.attachments().getAttachment(partId); + if (attachment == null) { - player.stop(); - int playingIndex = player.getCurrentMediaItemIndex(); - player.removeMediaItem(playingIndex); - Log.d(TAG, "Currently playing item removed."); + Log.d(TAG, "Removing media item at index " + i + " due to missing attachment."); + boolean isCurrentlyPlaying = (i == player.getCurrentMediaItemIndex()); + + if (isCurrentlyPlaying) { + player.stop(); + } + + player.removeMediaItem(i); + + // Check and remove previous item if it's a special tone + int prevIndex = i - 1; + if (prevIndex >= 0) { + MediaItem prevItem = player.getMediaItemAt(prevIndex); + Uri prevUri = prevItem.localConfiguration != null ? prevItem.localConfiguration.uri : null; + + if (VoiceNoteMediaItemFactory.NEXT_URI.equals(prevUri) || VoiceNoteMediaItemFactory.END_URI.equals(prevUri)) { + Log.d(TAG, "Removing previous special tone media item at index " + prevIndex); + player.removeMediaItem(prevIndex); + } + } } else { - Log.d(TAG, "Attachment was not null, therefore not deleted, therefore no action taken."); + Log.d(TAG, "Attachment found for index " + i + ", not removing."); } } catch (NumberFormatException ex) { - Log.w(TAG, "Could not parse currently playing URI into an attachmentId.", ex); + Log.w(TAG, "Failed to parse attachment ID from URI at index " + i, ex); } } });