From 020d5859725b5322f85fbf3db6d4332c467b08ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerardo=20V=C3=A1zquez?= Date: Tue, 23 Jun 2020 17:15:41 -0500 Subject: [PATCH] fixes #1215 replacing share menu by dialog --- .../activity/MediaplayerActivity.java | 52 +++-------- .../danoeh/antennapod/dialog/ShareDialog.java | 89 +++++++++++++++++++ .../menuhandler/FeedItemMenuHandler.java | 36 ++------ app/src/main/res/menu/feeditem_options.xml | 22 ----- app/src/main/res/menu/mediaplayer.xml | 22 ----- 5 files changed, 111 insertions(+), 110 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/dialog/ShareDialog.java 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 34c7e3aba..d0f7eb63b 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -18,16 +18,22 @@ import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import android.widget.Toast; + +import com.bumptech.glide.Glide; + +import org.apache.commons.lang3.StringUtils; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + +import java.text.NumberFormat; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; -import androidx.arch.core.util.Function; import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityOptionsCompat; import androidx.core.content.ContextCompat; -import androidx.core.util.Consumer; -import androidx.core.util.Supplier; -import com.bumptech.glide.Glide; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.event.PlaybackPositionEvent; import de.danoeh.antennapod.core.feed.FeedItem; @@ -39,7 +45,6 @@ import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.FeedItemUtil; -import de.danoeh.antennapod.core.util.Flavors; import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.ShareUtils; import de.danoeh.antennapod.core.util.StorageUtils; @@ -51,18 +56,13 @@ import de.danoeh.antennapod.core.util.playback.Playable; import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter; import de.danoeh.antennapod.dialog.PlaybackControlsDialog; +import de.danoeh.antennapod.dialog.ShareDialog; import de.danoeh.antennapod.dialog.SkipPreferenceDialog; import de.danoeh.antennapod.dialog.SleepTimerDialog; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; -import org.apache.commons.lang3.StringUtils; -import org.greenrobot.eventbus.EventBus; -import org.greenrobot.eventbus.Subscribe; -import org.greenrobot.eventbus.ThreadMode; - -import java.text.NumberFormat; /** @@ -313,13 +313,8 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements boolean isItemAndHasLink = isFeedMedia && ShareUtils.hasLinkToShare(((FeedMedia) media).getItem()); - menu.findItem(R.id.share_link_item).setVisible(isItemAndHasLink); - menu.findItem(R.id.share_link_with_position_item).setVisible(isItemAndHasLink); boolean isItemHasDownloadLink = isFeedMedia && ((FeedMedia) media).getDownload_url() != null; - menu.findItem(R.id.share_download_url_item).setVisible(isItemHasDownloadLink); - menu.findItem(R.id.share_download_url_with_position_item).setVisible(isItemHasDownloadLink); - menu.findItem(R.id.share_file).setVisible(isFeedMedia && ((FeedMedia) media).fileExists()); menu.findItem(R.id.share_item).setVisible(hasWebsiteLink || isItemAndHasLink || isItemHasDownloadLink); @@ -393,29 +388,10 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements case R.id.visit_website_item: IntentUtils.openInBrowser(MediaplayerActivity.this, getWebsiteLinkWithFallback(media)); break; - case R.id.share_link_item: + case R.id.share_item: if (feedItem != null) { - ShareUtils.shareFeedItemLink(this, feedItem); - } - break; - case R.id.share_download_url_item: - if (feedItem != null) { - ShareUtils.shareFeedItemDownloadLink(this, feedItem); - } - break; - case R.id.share_link_with_position_item: - if (feedItem != null) { - ShareUtils.shareFeedItemLink(this, feedItem, true); - } - break; - case R.id.share_download_url_with_position_item: - if (feedItem != null) { - ShareUtils.shareFeedItemDownloadLink(this, feedItem, true); - } - break; - case R.id.share_file: - if (media instanceof FeedMedia) { - ShareUtils.shareFeedItemFile(this, ((FeedMedia) media)); + ShareDialog shareDialog = new ShareDialog(getApplicationContext(), feedItem); + shareDialog.createDialog().show(); } break; default: diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/ShareDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/ShareDialog.java new file mode 100644 index 000000000..41c6eedf9 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/dialog/ShareDialog.java @@ -0,0 +1,89 @@ +package de.danoeh.antennapod.dialog; + +import android.content.Context; +import android.content.DialogInterface; + +import java.util.ArrayList; +import java.util.List; + +import androidx.appcompat.app.AlertDialog; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.util.ShareUtils; + +public class ShareDialog { + + private static final String TAG = "ShareDialog"; + private final Context context; + private AlertDialog dialog; + private List shareDialogOptions; + private String[] arrayItems; + private FeedItem item; + + public ShareDialog(Context context, FeedItem item) { + this.context = context; + this.item = item; + shareDialogOptions = new ArrayList<>(); + } + + public AlertDialog createDialog() { + setupOptions(); + + dialog = new AlertDialog.Builder(context) + .setTitle(R.string.share_label) + .setItems(arrayItems, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + // The 'which' argument contains the index position + // of the selected item + if(arrayItems[which].equals(context.getString(R.string.share_link_label))){ + ShareUtils.shareFeedItemLink(context, item); + } else if(arrayItems[which].equals(context.getString(R.string.share_item_url_label))) { + ShareUtils.shareFeedItemDownloadLink(context, item); + } else if(arrayItems[which].equals(context.getString(R.string.share_link_with_position_label))) { + ShareUtils.shareFeedItemLink(context, item, true); + } else if(arrayItems[which].equals(context.getString(R.string.share_item_url_with_position_label))) { + ShareUtils.shareFeedItemDownloadLink(context, item, true); + } else if(arrayItems[which].equals(context.getString(R.string.share_file_label))) { + ShareUtils.shareFeedItemFile(context, item.getMedia()); + } + } + }) + .create(); + + return dialog; + } + + private void setupOptions(){ + boolean hasMedia = item.getMedia() != null; + + shareDialogOptions.add(context.getString(R.string.share_link_label)); + shareDialogOptions.add(context.getString(R.string.share_link_with_position_label)); + shareDialogOptions.add(context.getString(R.string.share_item_url_label)); + shareDialogOptions.add(context.getString(R.string.share_item_url_with_position_label)); + shareDialogOptions.add(context.getString(R.string.share_file_label)); + + if (!ShareUtils.hasLinkToShare(item)) { + shareDialogOptions.remove(context.getString(R.string.share_link_label)); + shareDialogOptions.remove(context.getString(R.string.share_link_with_position_label)); + } + + if (!hasMedia || item.getMedia().getDownload_url() == null) { + shareDialogOptions.remove(context.getString(R.string.share_item_url_label)); + shareDialogOptions.remove(context.getString(R.string.share_item_url_with_position_label)); + } + + if(!hasMedia || item.getMedia().getPosition() <= 0) { + shareDialogOptions.remove(context.getString(R.string.share_item_url_with_position_label)); + shareDialogOptions.remove(context.getString(R.string.share_link_with_position_label)); + } + + boolean fileDownloaded = hasMedia && item.getMedia().fileExists(); + if(!fileDownloaded){ + shareDialogOptions.remove(context.getString(R.string.share_file_label)); + } + + // preparing the resulting shareOptions for dialog + arrayItems = new String[shareDialogOptions.size()]; + shareDialogOptions.toArray(arrayItems); + } +} diff --git a/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedItemMenuHandler.java b/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedItemMenuHandler.java index 1f15f66ec..734124c1e 100644 --- a/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedItemMenuHandler.java +++ b/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedItemMenuHandler.java @@ -2,13 +2,14 @@ package de.danoeh.antennapod.menuhandler; import android.content.Context; import android.os.Handler; -import androidx.annotation.NonNull; -import com.google.android.material.snackbar.Snackbar; -import androidx.fragment.app.Fragment; import android.util.Log; import android.view.Menu; import android.view.MenuItem; +import com.google.android.material.snackbar.Snackbar; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.core.feed.FeedItem; @@ -23,6 +24,7 @@ import de.danoeh.antennapod.core.sync.model.EpisodeAction; import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.ShareUtils; +import de.danoeh.antennapod.dialog.ShareDialog; /** * Handles interactions with the FeedItemMenu. @@ -61,20 +63,9 @@ public class FeedItemMenuHandler { } if (!ShareUtils.hasLinkToShare(selectedItem)) { setItemVisibility(menu, R.id.visit_website_item, false); - setItemVisibility(menu, R.id.share_link_item, false); - setItemVisibility(menu, R.id.share_link_with_position_item, false); - } - if (!hasMedia || selectedItem.getMedia().getDownload_url() == null) { - setItemVisibility(menu, R.id.share_download_url_item, false); - setItemVisibility(menu, R.id.share_download_url_with_position_item, false); - } - if(!hasMedia || selectedItem.getMedia().getPosition() <= 0) { - setItemVisibility(menu, R.id.share_download_url_with_position_item, false); - setItemVisibility(menu, R.id.share_link_with_position_item, false); } boolean fileDownloaded = hasMedia && selectedItem.getMedia().fileExists(); - setItemVisibility(menu, R.id.share_file, fileDownloaded); setItemVisibility(menu, R.id.remove_new_flag_item, selectedItem.isNew()); if (selectedItem.isPlayed()) { @@ -243,20 +234,9 @@ public class FeedItemMenuHandler { case R.id.visit_website_item: IntentUtils.openInBrowser(context, FeedItemUtil.getLinkWithFallback(selectedItem)); break; - case R.id.share_link_item: - ShareUtils.shareFeedItemLink(context, selectedItem); - break; - case R.id.share_download_url_item: - ShareUtils.shareFeedItemDownloadLink(context, selectedItem); - break; - case R.id.share_link_with_position_item: - ShareUtils.shareFeedItemLink(context, selectedItem, true); - break; - case R.id.share_download_url_with_position_item: - ShareUtils.shareFeedItemDownloadLink(context, selectedItem, true); - break; - case R.id.share_file: - ShareUtils.shareFeedItemFile(context, selectedItem.getMedia()); + case R.id.share_item: + ShareDialog shareDialog = new ShareDialog(context, selectedItem); + shareDialog.createDialog().show(); break; default: Log.d(TAG, "Unknown menuItemId: " + menuItemId); diff --git a/app/src/main/res/menu/feeditem_options.xml b/app/src/main/res/menu/feeditem_options.xml index e415ff85a..a53f22d02 100644 --- a/app/src/main/res/menu/feeditem_options.xml +++ b/app/src/main/res/menu/feeditem_options.xml @@ -72,28 +72,6 @@ android:id="@+id/share_item" android:menuCategory="container" android:title="@string/share_label"> - - - - - - - - - - - - - - \ No newline at end of file