Do not play a media item if it was deleted.

This commit is contained in:
Sagar
2025-04-25 20:43:22 +05:30
committed by Cody Henthorne
parent 43b5cb0641
commit 65e1ffaed4

View File

@@ -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);
}
}
});