diff options
Diffstat (limited to 'app/src/main/java/de/danoeh/antennapod')
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/adapter/ActionButtonUtils.java | 40 | ||||
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/adapter/DefaultActionButtonCallback.java | 82 |
2 files changed, 105 insertions, 17 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/ActionButtonUtils.java b/app/src/main/java/de/danoeh/antennapod/adapter/ActionButtonUtils.java index 8d3e73429..8e347a819 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/ActionButtonUtils.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/ActionButtonUtils.java @@ -10,7 +10,9 @@ import org.apache.commons.lang3.Validate; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; +import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DownloadRequester; +import de.danoeh.antennapod.core.util.LongList; /** * Utility methods for the action button that is displayed on the right hand side @@ -26,9 +28,21 @@ public class ActionButtonUtils { Validate.notNull(context); this.context = context; - drawables = context.obtainStyledAttributes(new int[]{ - R.attr.av_play, R.attr.navigation_cancel, R.attr.av_download, R.attr.av_pause, R.attr.navigation_accept}); - labels = new int[]{R.string.play_label, R.string.cancel_download_label, R.string.download_label, R.string.mark_read_label}; + drawables = context.obtainStyledAttributes(new int[] { + R.attr.av_play, + R.attr.navigation_cancel, + R.attr.av_download, + R.attr.av_pause, + R.attr.navigation_accept, + R.attr.content_new + }); + labels = new int[] { + R.string.play_label, + R.string.cancel_download_label, + R.string.download_label, + R.string.mark_read_label, + R.string.add_to_queue_label + }; } /** @@ -50,18 +64,26 @@ public class ActionButtonUtils { butSecondary.setContentDescription(context.getString(labels[1])); } else { // item is not downloaded and not being downloaded - butSecondary.setVisibility(View.VISIBLE); - butSecondary.setImageDrawable(drawables.getDrawable(2)); - butSecondary.setContentDescription(context.getString(labels[2])); + LongList queueIds = DBReader.getQueueIDList(context); + if(DefaultActionButtonCallback.userAllowedMobileDownloads() || + !DefaultActionButtonCallback.userChoseAddToQueue() || queueIds.contains(item.getId())) { + butSecondary.setVisibility(View.VISIBLE); + butSecondary.setImageDrawable(drawables.getDrawable(2)); + butSecondary.setContentDescription(context.getString(labels[2])); + } else { + // mobile download not allowed yet, item is not in queue and user chose add to queue + butSecondary.setVisibility(View.VISIBLE); + butSecondary.setImageDrawable(drawables.getDrawable(5)); + butSecondary.setContentDescription(context.getString(labels[4])); + } } } else { - // item is not being downloaded + // item is downloaded butSecondary.setVisibility(View.VISIBLE); if (media.isCurrentlyPlaying()) { butSecondary.setImageDrawable(drawables.getDrawable(3)); } else { - butSecondary - .setImageDrawable(drawables.getDrawable(0)); + butSecondary.setImageDrawable(drawables.getDrawable(0)); } butSecondary.setContentDescription(context.getString(labels[0])); } diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/DefaultActionButtonCallback.java b/app/src/main/java/de/danoeh/antennapod/adapter/DefaultActionButtonCallback.java index d3843934a..6df18e759 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/DefaultActionButtonCallback.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/DefaultActionButtonCallback.java @@ -1,6 +1,8 @@ package de.danoeh.antennapod.adapter; +import android.app.AlertDialog; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.widget.Toast; @@ -10,41 +12,66 @@ import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; -import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction; import de.danoeh.antennapod.core.preferences.GpodnetPreferences; +import de.danoeh.antennapod.core.service.playback.PlaybackService; +import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.DownloadRequestException; import de.danoeh.antennapod.core.storage.DownloadRequester; +import de.danoeh.antennapod.core.util.LongList; +import de.danoeh.antennapod.core.util.NetworkUtils; /** * Default implementation of an ActionButtonCallback */ public class DefaultActionButtonCallback implements ActionButtonCallback { + private static final String TAG = "DefaultActionButtonCallback"; private final Context context; + private static final int TEN_MINUTES_IN_MILLIS = 60 * 1000 * 10; + + // remember timestamp when user allowed downloading via mobile connection + private static long allowMobileDownloadsTimestamp; + private static long onlyAddToQueueTimeStamp; + public DefaultActionButtonCallback(Context context) { Validate.notNull(context); this.context = context; } + public static boolean userAllowedMobileDownloads() { + return System.currentTimeMillis() - allowMobileDownloadsTimestamp < TEN_MINUTES_IN_MILLIS; + } + + public static boolean userChoseAddToQueue() { + return System.currentTimeMillis() - onlyAddToQueueTimeStamp < TEN_MINUTES_IN_MILLIS; + } + @Override public void onActionButtonPressed(final FeedItem item) { - if (item.hasMedia()) { final FeedMedia media = item.getMedia(); boolean isDownloading = DownloadRequester.getInstance().isDownloadingFile(media); if (!isDownloading && !media.isDownloaded()) { - try { - DBTasks.downloadFeedItems(context, item); - Toast.makeText(context, R.string.status_downloading_label, Toast.LENGTH_SHORT).show(); - } catch (DownloadRequestException e) { - e.printStackTrace(); - DownloadRequestErrorDialogCreator.newRequestErrorDialog(context, e.getMessage()); + LongList queueIds = DBReader.getQueueIDList(context); + if (NetworkUtils.isDownloadAllowed(context) || userAllowedMobileDownloads()) { + try { + DBTasks.downloadFeedItems(context, item); + Toast.makeText(context, R.string.status_downloading_label, Toast.LENGTH_SHORT).show(); + } catch (DownloadRequestException e) { + e.printStackTrace(); + DownloadRequestErrorDialogCreator.newRequestErrorDialog(context, e.getMessage()); + } + } else if(userChoseAddToQueue() && !queueIds.contains(item.getId())) { + DBWriter.addQueueItem(context, item.getId()); + Toast.makeText(context, R.string.added_to_queue_label, Toast.LENGTH_SHORT).show(); + } else { + confirmMobileDownload(context, item); } } else if (isDownloading) { DownloadRequester.getInstance().cancelDownload(context, media); @@ -79,4 +106,43 @@ public class DefaultActionButtonCallback implements ActionButtonCallback { } } } + + private void confirmMobileDownload(final Context context, final FeedItem item) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder + .setTitle(R.string.confirm_mobile_download_dialog_title) + .setMessage(context.getText(R.string.confirm_mobile_download_dialog_message)) + .setPositiveButton(R.string.confirm_mobile_download_dialog_enable_temporarily, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + allowMobileDownloadsTimestamp = System.currentTimeMillis(); + try { + DBTasks.downloadFeedItems(context, item); + Toast.makeText(context, R.string.status_downloading_label, Toast.LENGTH_SHORT).show(); + } catch (DownloadRequestException e) { + e.printStackTrace(); + DownloadRequestErrorDialogCreator.newRequestErrorDialog(context, e.getMessage()); + } + } + }); + LongList queueIds = DBReader.getQueueIDList(context); + if(!queueIds.contains(item.getId())) { + builder.setNeutralButton(R.string.confirm_mobile_download_dialog_only_add_to_queue, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + onlyAddToQueueTimeStamp = System.currentTimeMillis(); + DBWriter.addQueueItem(context, item.getId()); + Toast.makeText(context, R.string.added_to_queue_label, Toast.LENGTH_SHORT).show(); + } + }) + .setMessage(context.getText(R.string.confirm_mobile_download_dialog_message_not_in_queue)); + } else { + builder.setMessage(context.getText(R.string.confirm_mobile_download_dialog_message)); + } + builder.setNegativeButton(R.string.cancel_label, null) + .create() + .show(); + } } |