From e18d8f351476ede5047a08ec1f0dd3a9b45a8b99 Mon Sep 17 00:00:00 2001 From: orionlee Date: Sat, 21 Sep 2019 14:25:50 -0700 Subject: [PATCH] #2929 - refactor common getFeedItem from playable logic in Player --- .../activity/MediaplayerActivity.java | 63 +++++++++---------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java index 5a12664c9..c0af36d84 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -391,29 +391,24 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements return true; } else { if (media != null) { + final FeedItem feedItem = getFeedItem(media); // some options option requires FeedItem switch (item.getItemId()) { case R.id.add_to_favorites_item: - if(media instanceof FeedMedia) { - FeedItem feedItem = ((FeedMedia)media).getItem(); - if(feedItem != null) { - DBWriter.addFavoriteItem(feedItem); - isFavorite = true; - invalidateOptionsMenu(); - Toast.makeText(this, R.string.added_to_favorites, Toast.LENGTH_SHORT) - .show(); - } + if(feedItem != null) { + DBWriter.addFavoriteItem(feedItem); + isFavorite = true; + invalidateOptionsMenu(); + Toast.makeText(this, R.string.added_to_favorites, Toast.LENGTH_SHORT) + .show(); } break; case R.id.remove_from_favorites_item: - if(media instanceof FeedMedia) { - FeedItem feedItem = ((FeedMedia)media).getItem(); - if(feedItem != null) { - DBWriter.removeFavoriteItem(feedItem); - isFavorite = false; - invalidateOptionsMenu(); - Toast.makeText(this, R.string.removed_from_favorites, Toast.LENGTH_SHORT) - .show(); - } + if(feedItem != null) { + DBWriter.removeFavoriteItem(feedItem); + isFavorite = false; + invalidateOptionsMenu(); + Toast.makeText(this, R.string.removed_from_favorites, Toast.LENGTH_SHORT) + .show(); } break; case R.id.disable_sleeptimer_item: @@ -451,12 +446,9 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements dialog.show(getSupportFragmentManager(), "playback_controls"); break; case R.id.open_feed_item: - if(media instanceof FeedMedia) { - FeedItem feedItem = ((FeedMedia)media).getItem(); - if (feedItem != null) { - Intent intent = MainActivity.getIntentToOpenFeed(this, feedItem.getFeedId()); - startActivity(intent); - } + if (feedItem != null) { + Intent intent = MainActivity.getIntentToOpenFeed(this, feedItem.getFeedId()); + startActivity(intent); } break; case R.id.visit_website_item: @@ -465,22 +457,22 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements break; case R.id.share_link_item: if (media instanceof FeedMedia) { - ShareUtils.shareFeedItemLink(this, ((FeedMedia) media).getItem()); + ShareUtils.shareFeedItemLink(this, feedItem); } break; case R.id.share_download_url_item: if (media instanceof FeedMedia) { - ShareUtils.shareFeedItemDownloadLink(this, ((FeedMedia) media).getItem()); + ShareUtils.shareFeedItemDownloadLink(this, feedItem); } break; case R.id.share_link_with_position_item: if (media instanceof FeedMedia) { - ShareUtils.shareFeedItemLink(this, ((FeedMedia) media).getItem(), true); + ShareUtils.shareFeedItemLink(this, feedItem, true); } break; case R.id.share_download_url_with_position_item: if (media instanceof FeedMedia) { - ShareUtils.shareFeedItemDownloadLink(this, ((FeedMedia) media).getItem(), true); + ShareUtils.shareFeedItemDownloadLink(this, feedItem, true); } break; case R.id.share_file: @@ -824,11 +816,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements } private void checkFavorite() { - Playable playable = controller.getMedia(); - if (!(playable instanceof FeedMedia)) { - return; - } - FeedItem feedItem = ((FeedMedia) playable).getItem(); + FeedItem feedItem = getFeedItem(controller.getMedia()); if (feedItem == null) { return; } @@ -883,4 +871,13 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements } } } + + @Nullable + private static FeedItem getFeedItem(@Nullable Playable playable) { + if (playable instanceof FeedMedia) { + return ((FeedMedia)playable).getItem(); + } else { + return null; + } + } }