summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java
blob: 2c7a7f074e0b07b55519fc19b024c2a0d2bb5686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package de.danoeh.antennapod.util.menuhandler;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.service.playback.PlaybackService;
import de.danoeh.antennapod.storage.DBTasks;
import de.danoeh.antennapod.storage.DBWriter;
import de.danoeh.antennapod.storage.DownloadRequestException;
import de.danoeh.antennapod.storage.DownloadRequester;
import de.danoeh.antennapod.util.QueueAccess;
import de.danoeh.antennapod.util.ShareUtils;

/**
 * Handles interactions with the FeedItemMenu.
 */
public class FeedItemMenuHandler {
    private static final String TAG = "FeedItemMenuHandler";

    private FeedItemMenuHandler() {

    }

    /**
     * Used by the MenuHandler to access different types of menus through one
     * interface
     */
    public interface MenuInterface {
        /**
         * Implementations of this method should call findItem(id) on their
         * menu-object and call setVisibility(visibility) on the returned
         * MenuItem object.
         */
        abstract void setItemVisibility(int id, boolean visible);
    }

    /**
     * This method should be called in the prepare-methods of menus. It changes
     * the visibility of the menu items depending on a FeedItem's attributes.
     *
     * @param mi               An instance of MenuInterface that the method uses to change a
     *                         MenuItem's visibility
     * @param selectedItem     The FeedItem for which the menu is supposed to be prepared
     * @param showExtendedMenu True if MenuItems that let the user share information about
     *                         the FeedItem and visit its website should be set visible. This
     *                         parameter should be set to false if the menu space is limited.
     * @param queueAccess      Used for testing if the queue contains the selected item
     * @return Returns true if selectedItem is not null.
     */
    public static boolean onPrepareMenu(MenuInterface mi,
                                        FeedItem selectedItem, boolean showExtendedMenu, QueueAccess queueAccess) {
        if (selectedItem == null) {
            return false;
        }
        DownloadRequester requester = DownloadRequester.getInstance();
        boolean hasMedia = selectedItem.getMedia() != null;
        boolean downloaded = hasMedia && selectedItem.getMedia().isDownloaded();
        boolean downloading = hasMedia
                && requester.isDownloadingFile(selectedItem.getMedia());
        boolean notLoadedAndNotLoading = hasMedia && (!downloaded)
                && (!downloading);
        boolean isPlaying = hasMedia
                && selectedItem.getState() == FeedItem.State.PLAYING;

        FeedItem.State state = selectedItem.getState();

        if (!isPlaying) {
            mi.setItemVisibility(R.id.skip_episode_item, false);
        }
        if (!downloaded || isPlaying) {
            mi.setItemVisibility(R.id.play_item, false);
            mi.setItemVisibility(R.id.remove_item, false);
        }
        if (!notLoadedAndNotLoading) {
            mi.setItemVisibility(R.id.download_item, false);
        }
        if (!(notLoadedAndNotLoading | downloading) | isPlaying) {
            mi.setItemVisibility(R.id.stream_item, false);
        }
        if (!downloading) {
            mi.setItemVisibility(R.id.cancel_download_item, false);
        }

        boolean isInQueue = queueAccess.contains(selectedItem.getId());
        if (!isInQueue || isPlaying) {
            mi.setItemVisibility(R.id.remove_from_queue_item, false);
        }
        if (!(!isInQueue && selectedItem.getMedia() != null)) {
            mi.setItemVisibility(R.id.add_to_queue_item, false);
        }
        if (!showExtendedMenu || selectedItem.getLink() == null) {
            mi.setItemVisibility(R.id.share_link_item, false);
        }

        if (!BuildConfig.DEBUG
                || !(state == FeedItem.State.IN_PROGRESS || state == FeedItem.State.READ)) {
            mi.setItemVisibility(R.id.mark_unread_item, false);
        }
        if (!(state == FeedItem.State.NEW || state == FeedItem.State.IN_PROGRESS)) {
            mi.setItemVisibility(R.id.mark_read_item, false);
        }

        if (!showExtendedMenu || selectedItem.getLink() == null) {
            mi.setItemVisibility(R.id.visit_website_item, false);
        }

        if (selectedItem.getPaymentLink() == null || !selectedItem.getFlattrStatus().flattrable()) {
            mi.setItemVisibility(R.id.support_item, false);
        }
        return true;
    }

    /**
     * The same method as onPrepareMenu(MenuInterface, FeedItem, boolean, QueueAccess), but lets the
     * caller also specify a list of menu items that should not be shown.
     *
     * @param excludeIds Menu item that should be excluded
     * @return true if selectedItem is not null.
     */
    public static boolean onPrepareMenu(MenuInterface mi,
                                        FeedItem selectedItem, boolean showExtendedMenu, QueueAccess queueAccess, int... excludeIds) {
        boolean rc = onPrepareMenu(mi, selectedItem, showExtendedMenu, queueAccess);
        if (rc && excludeIds != null) {
            for (int id : excludeIds) {
                mi.setItemVisibility(id, false);
            }
        }

        return rc;
    }

    public static boolean onMenuItemClicked(Context context, int menuItemId,
                                            FeedItem selectedItem) throws DownloadRequestException {
        DownloadRequester requester = DownloadRequester.getInstance();
        switch (menuItemId) {
            case R.id.skip_episode_item:
                context.sendBroadcast(new Intent(
                        PlaybackService.ACTION_SKIP_CURRENT_EPISODE));
                break;
            case R.id.download_item:
                DBTasks.downloadFeedItems(context, selectedItem);
                break;
            case R.id.play_item:
                DBTasks.playMedia(context, selectedItem.getMedia(), true, true,
                        false);
                break;
            case R.id.remove_item:
                DBWriter.deleteFeedMediaOfItem(context, selectedItem.getMedia().getId());
                break;
            case R.id.cancel_download_item:
                requester.cancelDownload(context, selectedItem.getMedia());
                break;
            case R.id.mark_read_item:
                DBWriter.markItemRead(context, selectedItem, true, true);
                break;
            case R.id.mark_unread_item:
                DBWriter.markItemRead(context, selectedItem, false, true);
                break;
            case R.id.add_to_queue_item:
                DBWriter.addQueueItem(context, selectedItem.getId());
                break;
            case R.id.remove_from_queue_item:
                DBWriter.removeQueueItem(context, selectedItem.getId(), true);
                break;
            case R.id.stream_item:
                DBTasks.playMedia(context, selectedItem.getMedia(), true, true,
                        true);
                break;
            case R.id.visit_website_item:
                Uri uri = Uri.parse(selectedItem.getLink());
                context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
                break;
            case R.id.support_item:
                DBTasks.flattrItemIfLoggedIn(context, selectedItem);
                break;
            case R.id.share_link_item:
                ShareUtils.shareFeedItemLink(context, selectedItem);
                break;
            default:
                return false;
        }
        // Refresh menu state

        return true;
    }

}