Prevent crash on attachment delete while voice note system tone is playing.

This commit is contained in:
Nicholas Tinsley
2024-04-23 10:22:01 -04:00
parent a9ea3854d2
commit 1fa53cfcb8

View File

@@ -34,6 +34,7 @@ import com.google.common.util.concurrent.ListenableFuture;
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;
@@ -207,19 +208,31 @@ public class VoiceNotePlaybackService extends MediaSessionService {
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}.