Merge pull request #3352 from ByteHamster/delete-current-feed

Fixed deleting currently playing feed
This commit is contained in:
H. Lehmann 2019-08-31 14:03:56 +02:00 committed by GitHub
commit a0ee6a8ca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 43 deletions

View File

@ -487,9 +487,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0); sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0);
//If the user asks to play External Media, the casting session, if on, should end. //If the user asks to play External Media, the casting session, if on, should end.
flavorHelper.castDisconnect(playable instanceof ExternalMedia); flavorHelper.castDisconnect(playable instanceof ExternalMedia);
if (playable instanceof FeedMedia) {
playable = DBReader.getFeedMedia(((FeedMedia) playable).getId());
}
if (allowStreamAlways) { if (allowStreamAlways) {
UserPreferences.setAllowMobileStreaming(true); UserPreferences.setAllowMobileStreaming(true);
} }
@ -499,6 +496,14 @@ public class PlaybackService extends MediaBrowserServiceCompat {
stateManager.stopService(); stateManager.stopService();
return Service.START_NOT_STICKY; 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); mediaPlayer.playMediaObject(playable, stream, startWhenPrepared, prepareImmediately);
} else { } else {
Log.d(TAG, "Did not handle intent to PlaybackService: " + intent); Log.d(TAG, "Did not handle intent to PlaybackService: " + intent);

View File

@ -831,15 +831,14 @@ public final class DBReader {
* Searches the DB for a FeedMedia of the given id. * Searches the DB for a FeedMedia of the given id.
* *
* @param mediaId The id of the object * @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) { public static FeedMedia getFeedMedia(final long mediaId) {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor mediaCursor = null;
try { try (Cursor mediaCursor = adapter.getSingleFeedMediaCursor(mediaId)) {
mediaCursor = adapter.getSingleFeedMediaCursor(mediaId);
if (!mediaCursor.moveToFirst()) { if (!mediaCursor.moveToFirst()) {
return null; return null;
} }
@ -847,19 +846,13 @@ public final class DBReader {
int indexFeedItem = mediaCursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM); int indexFeedItem = mediaCursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM);
long itemId = mediaCursor.getLong(indexFeedItem); long itemId = mediaCursor.getLong(indexFeedItem);
FeedMedia media = FeedMedia.fromCursor(mediaCursor); FeedMedia media = FeedMedia.fromCursor(mediaCursor);
if (media != null) { FeedItem item = getFeedItem(itemId);
FeedItem item = getFeedItem(itemId); if (item != null) {
if (item != null) { media.setItem(item);
media.setItem(item); item.setMedia(media);
item.setMedia(media);
}
} }
return media; return media;
} finally { } finally {
if (mediaCursor != null) {
mediaCursor.close();
}
adapter.close(); adapter.close();
} }
} }

View File

@ -138,23 +138,9 @@ public class DBWriter {
public static Future<?> deleteFeed(final Context context, final long feedId) { public static Future<?> deleteFeed(final Context context, final long feedId) {
return dbExec.submit(() -> { return dbExec.submit(() -> {
DownloadRequester requester = DownloadRequester.getInstance(); DownloadRequester requester = DownloadRequester.getInstance();
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context
.getApplicationContext());
final Feed feed = DBReader.getFeed(feedId); final Feed feed = DBReader.getFeed(feedId);
if (feed != null) { 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 // delete stored media files and mark them as read
List<FeedItem> queue = DBReader.getQueue(); List<FeedItem> queue = DBReader.getQueue();
List<FeedItem> removed = new ArrayList<>(); List<FeedItem> removed = new ArrayList<>();
@ -163,19 +149,12 @@ public class DBWriter {
} }
for (FeedItem item : feed.getItems()) { for (FeedItem item : feed.getItems()) {
if(queue.remove(item)) { if (queue.remove(item)) {
removed.add(item); removed.add(item);
} }
if (item.getState() == FeedItem.State.PLAYING && PlaybackService.isRunning) { if (item.getMedia() != null && item.getMedia().isDownloaded()) {
context.stopService(new Intent(context, PlaybackService.class)); deleteFeedMediaSynchronous(context, item.getMedia());
} } else if (item.getMedia() != null && requester.isDownloadingFile(item.getMedia())) {
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())) {
requester.cancelDownload(context, item.getMedia()); requester.cancelDownload(context, item.getMedia());
} }
} }