From 4962f0693f21a29a36d82ba79bd787b6386bb58a Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Fri, 30 Aug 2019 22:34:43 +0200 Subject: [PATCH] Fixed deleting currently playing feed --- .../service/playback/PlaybackService.java | 11 +++++-- .../antennapod/core/storage/DBReader.java | 23 +++++---------- .../antennapod/core/storage/DBWriter.java | 29 +++---------------- 3 files changed, 20 insertions(+), 43 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 8d3dda524..1293076b5 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -487,9 +487,6 @@ public class PlaybackService extends MediaBrowserServiceCompat { sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0); //If the user asks to play External Media, the casting session, if on, should end. flavorHelper.castDisconnect(playable instanceof ExternalMedia); - if (playable instanceof FeedMedia) { - playable = DBReader.getFeedMedia(((FeedMedia) playable).getId()); - } if (allowStreamAlways) { UserPreferences.setAllowMobileStreaming(true); } @@ -499,6 +496,14 @@ public class PlaybackService extends MediaBrowserServiceCompat { stateManager.stopService(); return Service.START_NOT_STICKY; } + if (playable instanceof FeedMedia) { + playable = DBReader.getFeedMedia(((FeedMedia) playable).getId()); + } + if (playable == null) { + Log.d(TAG, "Playable was not found. Stopping service."); + stateManager.stopService(); + return Service.START_NOT_STICKY; + } mediaPlayer.playMediaObject(playable, stream, startWhenPrepared, prepareImmediately); } else { Log.d(TAG, "Did not handle intent to PlaybackService: " + intent); diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java index 5ceda03f0..70d3ba9dd 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java @@ -831,15 +831,14 @@ public final class DBReader { * Searches the DB for a FeedMedia of the given id. * * @param mediaId The id of the object - * @return The found object + * @return The found object, or null if it does not exist */ + @Nullable public static FeedMedia getFeedMedia(final long mediaId) { PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor mediaCursor = null; - try { - mediaCursor = adapter.getSingleFeedMediaCursor(mediaId); + + try (Cursor mediaCursor = adapter.getSingleFeedMediaCursor(mediaId)) { if (!mediaCursor.moveToFirst()) { return null; } @@ -847,19 +846,13 @@ public final class DBReader { int indexFeedItem = mediaCursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM); long itemId = mediaCursor.getLong(indexFeedItem); FeedMedia media = FeedMedia.fromCursor(mediaCursor); - if (media != null) { - FeedItem item = getFeedItem(itemId); - if (item != null) { - media.setItem(item); - item.setMedia(media); - } + FeedItem item = getFeedItem(itemId); + if (item != null) { + media.setItem(item); + item.setMedia(media); } return media; - } finally { - if (mediaCursor != null) { - mediaCursor.close(); - } adapter.close(); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java index 6f8498710..4f0ee70ef 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java @@ -138,23 +138,9 @@ public class DBWriter { public static Future deleteFeed(final Context context, final long feedId) { return dbExec.submit(() -> { DownloadRequester requester = DownloadRequester.getInstance(); - SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(context - .getApplicationContext()); final Feed feed = DBReader.getFeed(feedId); if (feed != null) { - if (PlaybackPreferences.getCurrentlyPlayingMedia() == FeedMedia.PLAYABLE_TYPE_FEEDMEDIA - && PlaybackPreferences.getLastPlayedFeedId() == feed - .getId()) { - IntentUtils.sendLocalBroadcast(context, PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE); - SharedPreferences.Editor editor = prefs.edit(); - editor.putLong( - PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEED_ID, - -1); - editor.commit(); - } - // delete stored media files and mark them as read List queue = DBReader.getQueue(); List removed = new ArrayList<>(); @@ -163,19 +149,12 @@ public class DBWriter { } for (FeedItem item : feed.getItems()) { - if(queue.remove(item)) { + if (queue.remove(item)) { removed.add(item); } - if (item.getState() == FeedItem.State.PLAYING && PlaybackService.isRunning) { - context.stopService(new Intent(context, PlaybackService.class)); - } - if (item.getMedia() != null - && item.getMedia().isDownloaded()) { - File mediaFile = new File(item.getMedia() - .getFile_url()); - mediaFile.delete(); - } else if (item.getMedia() != null - && requester.isDownloadingFile(item.getMedia())) { + if (item.getMedia() != null && item.getMedia().isDownloaded()) { + deleteFeedMediaSynchronous(context, item.getMedia()); + } else if (item.getMedia() != null && requester.isDownloadingFile(item.getMedia())) { requester.cancelDownload(context, item.getMedia()); } }