summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/feed/FeedMedia.java
blob: 4096c6ccad26b2fcb815e0fc85edcc211732b693 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package de.danoeh.antennapod.feed;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Parcel;
import android.os.Parcelable;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.preferences.PlaybackPreferences;
import de.danoeh.antennapod.util.ChapterUtils;
import de.danoeh.antennapod.util.playback.Playable;

public class FeedMedia extends FeedFile implements Playable {

	public static final int FEEDFILETYPE_FEEDMEDIA = 2;
	public static final int PLAYABLE_TYPE_FEEDMEDIA = 1;

	public static final String PREF_MEDIA_ID = "FeedMedia.PrefMediaId";
	public static final String PREF_FEED_ID = "FeedMedia.PrefFeedId";

	private int duration;
	private int position; // Current position in file
	private long size; // File size in Byte
	private String mime_type;
	private FeedItem item;
	private Date playbackCompletionDate;

	public FeedMedia(FeedItem i, String download_url, long size,
			String mime_type) {
		super(null, download_url, false);
		this.item = i;
		this.size = size;
		this.mime_type = mime_type;
	}

	public FeedMedia(long id, FeedItem item, int duration, int position,
			long size, String mime_type, String file_url, String download_url,
			boolean downloaded, Date playbackCompletionDate) {
		super(file_url, download_url, downloaded);
		this.id = id;
		this.item = item;
		this.duration = duration;
		this.position = position;
		this.size = size;
		this.mime_type = mime_type;
		this.playbackCompletionDate = playbackCompletionDate;
	}

	public FeedMedia(long id, FeedItem item) {
		super();
		this.id = id;
		this.item = item;
	}

	@Override
	public String getHumanReadableIdentifier() {
		if (item != null && item.getTitle() != null) {
			return item.getTitle();
		} else {
			return download_url;
		}
	}

	/** Uses mimetype to determine the type of media. */
	public MediaType getMediaType() {
		if (mime_type == null || mime_type.isEmpty()) {
			return MediaType.UNKNOWN;
		} else {
			if (mime_type.startsWith("audio")) {
				return MediaType.AUDIO;
			} else if (mime_type.startsWith("video")) {
				return MediaType.VIDEO;
			} else if (mime_type.equals("application/ogg")) {
				return MediaType.AUDIO;
			}
		}
		return MediaType.UNKNOWN;
	}

	public void updateFromOther(FeedMedia other) {
		super.updateFromOther(other);
		if (other.size > 0) {
			size = other.size;
		}
		if (other.mime_type != null) {
			mime_type = other.mime_type;
		}
	}

	public boolean compareWithOther(FeedMedia other) {
		if (super.compareWithOther(other)) {
			return true;
		}
		if (other.mime_type != null) {
			if (mime_type == null || !mime_type.equals(other.mime_type)) {
				return true;
			}
		}
		if (other.size > 0 && other.size != size) {
			return true;
		}
		return false;
	}

	/**
	 * Reads playback preferences to determine whether this FeedMedia object is
	 * currently being played.
	 */
	public boolean isPlaying() {
		return PlaybackPreferences.getCurrentlyPlayingMedia() == FeedMedia.PLAYABLE_TYPE_FEEDMEDIA
				&& PlaybackPreferences.getCurrentlyPlayingFeedMediaId() == id;
	}

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

	public int getDuration() {
		return duration;
	}

	public void setDuration(int duration) {
		this.duration = duration;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public String getMime_type() {
		return mime_type;
	}

	public void setMime_type(String mime_type) {
		this.mime_type = mime_type;
	}

	public FeedItem getItem() {
		return item;
	}

	public void setItem(FeedItem item) {
		this.item = item;
	}

	public Date getPlaybackCompletionDate() {
		return playbackCompletionDate;
	}

	public void setPlaybackCompletionDate(Date playbackCompletionDate) {
		this.playbackCompletionDate = playbackCompletionDate;
	}

	public boolean isInProgress() {
		return (this.position > 0);
	}

	public FeedImage getImage() {
		if (item != null && item.getFeed() != null) {
			return item.getFeed().getImage();
		}
		return null;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeLong(item.getFeed().getId());
		dest.writeLong(item.getId());
	}

	@Override
	public void writeToPreferences(Editor prefEditor) {
		prefEditor.putLong(PREF_FEED_ID, item.getFeed().getId());
		prefEditor.putLong(PREF_MEDIA_ID, id);
	}

	@Override
	public void loadMetadata() throws PlayableException {
		if (getChapters() == null) {
			ChapterUtils.loadChaptersFromStreamUrl(this);
		}
	}

	@Override
	public String getEpisodeTitle() {
		if (getItem().getTitle() != null) {
			return getItem().getTitle();
		} else {
			return getItem().getIdentifyingValue();
		}
	}

	@Override
	public List<Chapter> getChapters() {
		return getItem().getChapters();
	}

	@Override
	public String getWebsiteLink() {
		return getItem().getLink();
	}

	@Override
	public String getFeedTitle() {
		return getItem().getFeed().getTitle();
	}

	@Override
	public Object getIdentifier() {
		return id;
	}

	@Override
	public String getLocalMediaUrl() {
		return file_url;
	}

	@Override
	public String getStreamUrl() {
		return download_url;
	}

	@Override
	public boolean localFileAvailable() {
		return isDownloaded() && file_url != null;
	}

	@Override
	public boolean streamAvailable() {
		return download_url != null;
	}

	@Override
	public void saveCurrentPosition(SharedPreferences pref, int newPosition) {
		position = newPosition;
		FeedManager.getInstance().setFeedMedia(PodcastApp.getInstance(), this);
	}

	@Override
	public void onPlaybackStart() {
	}

	@Override
	public void onPlaybackCompleted() {

	}

	@Override
	public int getPlayableType() {
		return PLAYABLE_TYPE_FEEDMEDIA;
	}

	@Override
	public void setChapters(List<Chapter> chapters) {
		getItem().setChapters(chapters);
	}

	@Override
	public String getPaymentLink() {
		return getItem().getPaymentLink();
	}

	@Override
	public void loadShownotes(final ShownoteLoaderCallback callback) {
		String contentEncoded = item.getContentEncoded();
		if (item.getDescription() == null || contentEncoded == null) {
			FeedManager.getInstance().loadExtraInformationOfItem(
					PodcastApp.getInstance(), item,
					new FeedManager.TaskCallback<String[]>() {
						@Override
						public void onCompletion(String[] result) {
							if (result[1] != null) {
								callback.onShownotesLoaded(result[1]);
							} else {
								callback.onShownotesLoaded(result[0]);

							}

						}
					});
		} else {
			callback.onShownotesLoaded(contentEncoded);
		}
	}

	public static final Parcelable.Creator<FeedMedia> CREATOR = new Parcelable.Creator<FeedMedia>() {
		public FeedMedia createFromParcel(Parcel in) {
			long feedId = in.readLong();
			long itemId = in.readLong();
			FeedItem item = FeedManager.getInstance().getFeedItem(itemId,
					feedId);
			if (item != null) {
				return item.getMedia();
			} else {
				return null;
			}
		}

		public FeedMedia[] newArray(int size) {
			return new FeedMedia[size];
		}
	};

	@Override
	public InputStream openImageInputStream() {
		InputStream out = new Playable.DefaultPlayableImageLoader(this)
				.openImageInputStream();
		if (out == null) {
			if (item.getFeed().getImage() != null) {
				return item.getFeed().getImage().openImageInputStream();
			}
		}
		return out;
	}

	@Override
	public String getImageLoaderCacheKey() {
		String out = new Playable.DefaultPlayableImageLoader(this).getImageLoaderCacheKey();
		if (out == null) {
			if (item.getFeed().getImage() != null) {
				return item.getFeed().getImage().getImageLoaderCacheKey();
			}
		}
		return out;
	}

	@Override
	public InputStream reopenImageInputStream(InputStream input) {
		if (input instanceof FileInputStream) {
			return item.getFeed().getImage().reopenImageInputStream(input);
		} else {
			return new Playable.DefaultPlayableImageLoader(this)
					.reopenImageInputStream(input);
		}
	}
}