From 2197e9b7418baa3b7833f932a4cb652be1e43ba4 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Sat, 7 Nov 2015 17:09:36 +0100 Subject: [PATCH] If removing currently playing episode, (pause and) skip the same --- .../activity/AudioplayerActivity.java | 16 +++++++++++++ .../antennapod/activity/MainActivity.java | 23 +++++++++++++++++++ .../antennapod/fragment/QueueFragment.java | 1 + .../core/asynctask/FeedRemover.java | 7 ++++++ .../service/playback/PlaybackService.java | 2 +- .../antennapod/core/storage/DBTasks.java | 4 +--- 6 files changed, 49 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java index 417d49e83..b87870c69 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java @@ -45,11 +45,13 @@ import de.danoeh.antennapod.core.dialog.ConfirmationDialog; import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.feed.SimpleChapter; import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.playback.PlaybackService; +import de.danoeh.antennapod.core.service.playback.PlayerStatus; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.util.playback.ExternalMedia; @@ -742,6 +744,20 @@ public class AudioplayerActivity extends MediaplayerActivity implements ItemDesc public void onConfirmButtonPressed( DialogInterface dialog) { dialog.dismiss(); + if (controller != null) { + Playable playable = controller.getMedia(); + if (playable != null && playable instanceof FeedMedia) { + FeedMedia media = (FeedMedia) playable; + if (media.getItem().getFeed().getId() == feed.getId()) { + Log.d(TAG, "Currently playing episode is about to be deleted, skipping"); + remover.skipOnCompletion = true; + if(controller.getStatus() == PlayerStatus.PLAYING) { + sendBroadcast(new Intent( + PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE)); + } + } + } + } remover.executeAsync(); } }; diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java index bcbabffcf..d860f2b01 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -44,10 +44,16 @@ import de.danoeh.antennapod.core.event.ProgressEvent; import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.event.QueueEvent; +import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.UserPreferences; +import de.danoeh.antennapod.core.service.playback.PlaybackService; +import de.danoeh.antennapod.core.service.playback.PlayerStatus; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.util.StorageUtils; +import de.danoeh.antennapod.core.util.playback.Playable; +import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.fragment.AddFeedFragment; import de.danoeh.antennapod.fragment.DownloadsFragment; import de.danoeh.antennapod.fragment.EpisodesFragment; @@ -558,6 +564,23 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity public void onConfirmButtonPressed( DialogInterface dialog) { dialog.dismiss(); + if (externalPlayerFragment != null) { + PlaybackController controller = externalPlayerFragment.getPlaybackControllerTestingOnly(); + if (controller != null) { + Playable playable = controller.getMedia(); + if (playable != null && playable instanceof FeedMedia) { + FeedMedia media = (FeedMedia) playable; + if (media.getItem().getFeed().getId() == feed.getId()) { + Log.d(TAG, "Currently playing episode is about to be deleted, skipping"); + remover.skipOnCompletion = true; + if(controller.getStatus() == PlayerStatus.PLAYING) { + sendBroadcast(new Intent( + PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE)); + } + } + } + } + } remover.executeAsync(); } }; diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java index 709d7038a..79879ffeb 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java @@ -149,6 +149,7 @@ public class QueueFragment extends Fragment { recyclerAdapter.notifyDataSetChanged(); break; case REMOVED: + case IRREVERSIBLE_REMOVED: int position = FeedItemUtil.indexOfItemWithId(queue, event.item.getId()); queue.remove(position); recyclerAdapter.notifyItemRemoved(position); diff --git a/core/src/main/java/de/danoeh/antennapod/core/asynctask/FeedRemover.java b/core/src/main/java/de/danoeh/antennapod/core/asynctask/FeedRemover.java index bea8d2afd..7ff622f34 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/asynctask/FeedRemover.java +++ b/core/src/main/java/de/danoeh/antennapod/core/asynctask/FeedRemover.java @@ -3,12 +3,14 @@ package de.danoeh.antennapod.core.asynctask; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.content.Context; +import android.content.Intent; import android.os.AsyncTask; import java.util.concurrent.ExecutionException; import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.storage.DBWriter; /** Removes a feed in the background. */ @@ -16,6 +18,7 @@ public class FeedRemover extends AsyncTask { Context context; ProgressDialog dialog; Feed feed; + public boolean skipOnCompletion = false; public FeedRemover(Context context, Feed feed) { super(); @@ -38,6 +41,10 @@ public class FeedRemover extends AsyncTask { @Override protected void onPostExecute(Void result) { dialog.dismiss(); + if(skipOnCompletion) { + context.sendBroadcast(new Intent( + PlaybackService.ACTION_SKIP_CURRENT_EPISODE)); + } } @Override 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 ab78387a2..2ce91645b 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 @@ -571,7 +571,7 @@ public class PlaybackService extends Service { try { final List queue = taskManager.getQueue(); isInQueue = QueueAccess.ItemListAccess(queue).contains(item.getId()); - nextItem = DBTasks.getQueueSuccessorOfItem(this, item.getId(), queue); + nextItem = DBTasks.getQueueSuccessorOfItem(item.getId(), queue); } catch (InterruptedException e) { e.printStackTrace(); // isInQueue remains false diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java index f54e13471..a8aee1961 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java @@ -394,14 +394,12 @@ public final class DBTasks { /** * Returns the successor of a FeedItem in the queue. * - * @param context Used for accessing the DB. * @param itemId ID of the FeedItem * @param queue Used for determining the successor of the item. If this parameter is null, the method will load * the queue from the database in the same thread. * @return Successor of the FeedItem or null if the FeedItem is not in the queue or has no successor. */ - public static FeedItem getQueueSuccessorOfItem(Context context, - final long itemId, List queue) { + public static FeedItem getQueueSuccessorOfItem(final long itemId, List queue) { FeedItem result = null; if (queue == null) { queue = DBReader.getQueue();