summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/feed/Feed.java
blob: 7640ef7d1fbc8db18604bf1473c670dbf2cecdb0 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package de.danoeh.antennapod.feed;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import android.preference.PreferenceManager;
import de.danoeh.antennapod.PodcastApp;

/**
 * Data Object for a whole feed
 * 
 * @author daniel
 * 
 */
public class Feed extends FeedFile {
	public static final int FEEDFILETYPE_FEED = 0;
	public static final String TYPE_RSS2 = "rss";
	public static final String TYPE_RSS091 = "rss";
	public static final String TYPE_ATOM1 = "atom";

	private String title;
	/** Contains 'id'-element in Atom feed. */
	private String feedIdentifier;
	/** Link to the website. */
	private String link;
	private String description;
	private String language;
	/** Name of the author */
	private String author;
	private FeedImage image;
	private FeedCategory category;
	private List<FeedItem> items;
	/** Date of last refresh. */
	private Date lastUpdate;
	private String paymentLink;
	/** Feed type, for example RSS 2 or Atom */
	private String type;

	public Feed(Date lastUpdate) {
		super();
		items = new CopyOnWriteArrayList<FeedItem>();
		this.lastUpdate = lastUpdate;
	}

	/**
	 * This constructor is used for requesting a feed download. It should NOT be
	 * used if the title of the feed is already known.
	 * */
	public Feed(String url, Date lastUpdate) {
		this(lastUpdate);
		this.download_url = url;
	}

	/**
	 * This constructor is used for requesting a feed download. It should be
	 * used if the title of the feed is already known.
	 * */
	public Feed(String url, Date lastUpdate, String title) {
		this(url, lastUpdate);
		this.title = title;
	}

	/**
	 * Returns the number of FeedItems where 'read' is false. If the 'display
	 * only episodes' - preference is set to true, this method will only count
	 * items with episodes.
	 * */
	public int getNumOfNewItems() {
		int count = 0;
		boolean displayOnlyEpisodes = PreferenceManager
				.getDefaultSharedPreferences(PodcastApp.getInstance())
				.getBoolean(PodcastApp.PREF_DISPLAY_ONLY_EPISODES, false);

		for (FeedItem item : items) {
			if (item.getState() == FeedItem.State.NEW) {
				if (!displayOnlyEpisodes || item.getMedia() != null) {
					count++;
				}
			}
		}
		return count;
	}

	/**
	 * Returns the number of FeedItems where the media started to play but
	 * wasn't finished yet.
	 * */
	public int getNumOfStartedItems() {
		int count = 0;

		for (FeedItem item : items) {
			FeedItem.State state = item.getState();
			if (state == FeedItem.State.IN_PROGRESS
					|| state == FeedItem.State.PLAYING) {
				count++;
			}
		}
		return count;
	}

	/**
	 * Returns true if at least one item in the itemlist is unread.If the
	 * 'display only episodes' - preference is set to true, this method will
	 * only count items with episodes.
	 */
	public boolean hasNewItems() {
		boolean displayOnlyEpisodes = PreferenceManager
				.getDefaultSharedPreferences(PodcastApp.getInstance())
				.getBoolean(PodcastApp.PREF_DISPLAY_ONLY_EPISODES, false);
		for (FeedItem item : items) {
			if (item.getState() == FeedItem.State.NEW) {
				if (!displayOnlyEpisodes || item.getMedia() != null) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * Returns the number of FeedItems. If the 'display only episodes' -
	 * preference is set to true, this method will only count items with
	 * episodes.
	 * */
	public int getNumOfItems() {
		int count = 0;
		boolean displayOnlyEpisodes = PreferenceManager
				.getDefaultSharedPreferences(PodcastApp.getInstance())
				.getBoolean(PodcastApp.PREF_DISPLAY_ONLY_EPISODES, false);
		for (FeedItem item : items) {
			if (!displayOnlyEpisodes || item.getMedia() != null) {
				count++;
			}
		}
		return count;
	}

	/**
	 * Returns the value that uniquely identifies this Feed. If the
	 * feedIdentifier attribute is not null, it will be returned. Else it will
	 * try to return the title. If the title is not given, it will use the link
	 * of the feed.
	 * */
	public String getIdentifyingValue() {
		if (feedIdentifier != null && !feedIdentifier.isEmpty()) {
			return feedIdentifier;
		} else if (title != null && !title.isEmpty()) {
			return title;
		} else {
			return link;
		}
	}

	@Override
	public String getHumanReadableIdentifier() {
		if (title != null) {
			return title;
		} else {
			return download_url;
		}
	}

	/** Calls cacheDescriptions on all items. */
	protected void cacheDescriptionsOfItems() {
		if (items != null) {
			for (FeedItem item : items) {
				item.cacheDescriptions();
			}
		}
	}

	@Override
	public int getTypeAsInt() {
		return FEEDFILETYPE_FEED;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public FeedImage getImage() {
		return image;
	}

	public void setImage(FeedImage image) {
		this.image = image;
	}

	public FeedCategory getCategory() {
		return category;
	}

	public void setCategory(FeedCategory category) {
		this.category = category;
	}

	public List<FeedItem> getItems() {
		return items;
	}

	public void setItems(ArrayList<FeedItem> items) {
		this.items = items;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public String getFeedIdentifier() {
		return feedIdentifier;
	}

	public void setFeedIdentifier(String feedIdentifier) {
		this.feedIdentifier = feedIdentifier;
	}

	public String getPaymentLink() {
		return paymentLink;
	}

	public void setPaymentLink(String paymentLink) {
		this.paymentLink = paymentLink;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}