Stop voice memo playback if the current item is deleted.

Fixes #13502.
This commit is contained in:
Nicholas Tinsley
2024-04-16 14:50:37 -04:00
committed by Greyson Parrelli
parent 5e4dfcc65f
commit 7a2d408ca2

View File

@@ -34,12 +34,15 @@ 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.DatabaseAttachment;
import org.thoughtcrime.securesms.database.DatabaseObserver;
import org.thoughtcrime.securesms.database.MessageTable;
import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.database.model.MessageId;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobs.MultiDeviceViewedUpdateJob;
import org.thoughtcrime.securesms.jobs.SendViewedReceiptJob;
import org.thoughtcrime.securesms.mms.PartUriParser;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.service.KeyCachingService;
@@ -64,6 +67,8 @@ public class VoiceNotePlaybackService extends MediaSessionService {
private KeyClearedReceiver keyClearedReceiver;
private VoiceNotePlayerCallback voiceNotePlayerCallback;
private final DatabaseObserver.Observer attachmentDeletionObserver = this::onAttachmentDeleted;
@Override
public void onCreate() {
super.onCreate();
@@ -83,6 +88,7 @@ public class VoiceNotePlaybackService extends MediaSessionService {
setMediaNotificationProvider(new VoiceNoteMediaNotificationProvider(this));
setListener(new MediaSessionServiceListener());
ApplicationDependencies.getDatabaseObserver().registerAttachmentObserver(attachmentDeletionObserver);
}
@Override
@@ -95,6 +101,7 @@ public class VoiceNotePlaybackService extends MediaSessionService {
@Override
public void onDestroy() {
ApplicationDependencies.getDatabaseObserver().unregisterObserver(attachmentDeletionObserver);
player.release();
mediaSession.release();
mediaSession = null;
@@ -191,6 +198,28 @@ 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.playbackProperties == null) {
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.");
}
}
});
}
/**
* 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}.