Skip to content

Commit

Permalink
Prevent crash on attachment delete while voice note system tone is pl…
Browse files Browse the repository at this point in the history
…aying.
  • Loading branch information
nicholas-signal committed Apr 23, 2024
1 parent a9ea385 commit 1fa53cf
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.signal.core.util.concurrent.SignalExecutors;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.attachments.AttachmentId;
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
import org.thoughtcrime.securesms.database.DatabaseObserver;
import org.thoughtcrime.securesms.database.MessageTable;
Expand Down Expand Up @@ -207,19 +208,31 @@ private void onAttachmentDeleted() {
Log.d(TAG, "Current item is null or playback properties are null.");
return;
}
final Uri currentUi = currentItem.playbackProperties.uri;
final DatabaseAttachment attachment = SignalDatabase.attachments().getAttachment(new PartUriParser(currentUi).getPartId());
if (attachment == null) {
player.stop();
int playingIndex = player.getCurrentMediaItemIndex();
player.removeMediaItem(playingIndex);
Log.d(TAG, "Currently playing item removed.");
} else {
Log.d(TAG, "Attachment was not null, therefore not deleted, therefore no action taken.");

final Uri currentlyPlayingUri = currentItem.playbackProperties.uri;

if (currentlyPlayingUri == VoiceNoteMediaItemFactory.NEXT_URI || currentlyPlayingUri == VoiceNoteMediaItemFactory.END_URI) {
Log.v(TAG, "Attachment deleted while voice note service was playing a system tone.");
}

try {
final AttachmentId partId = new PartUriParser(currentlyPlayingUri).getPartId();
final 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.");
} else {
Log.d(TAG, "Attachment was not null, therefore not deleted, therefore no action taken.");
}
} catch (NumberFormatException ex) {
Log.w(TAG, "Could not parse currently playing URI into an attachmentId.", ex);
}
}
});
}

/**
* Some devices, such as the ASUS Zenfone 8, erroneously report multiple broadcast receivers for {@value Intent#ACTION_MEDIA_BUTTON} in the package manager.
* This triggers a failure within the {@link MediaSession} initialization and throws an {@link IllegalStateException}.
Expand Down

0 comments on commit 1fa53cf

Please sign in to comment.