summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/syndication/namespace/NSMedia.java
blob: 053a812708b70cda24cf15afcc05d41219c81b49 (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
package de.danoeh.antennapod.syndication.namespace;

import java.util.concurrent.TimeUnit;

import org.xml.sax.Attributes;

import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.syndication.handler.HandlerState;
import de.danoeh.antennapod.syndication.util.SyndTypeUtils;

/** Processes tags from the http://search.yahoo.com/mrss/ namespace. */
public class NSMedia extends Namespace {
	private static final String TAG = "NSMedia";

	public static final String NSTAG = "media";
	public static final String NSURI = "http://search.yahoo.com/mrss/";

	private static final String CONTENT = "content";
	private static final String DOWNLOAD_URL = "url";
	private static final String SIZE = "fileSize";
	private static final String MIME_TYPE = "type";
	private static final String DURATION = "duration";

	@Override
	public SyndElement handleElementStart(String localName, HandlerState state,
			Attributes attributes) {
		if (localName.equals(CONTENT)) {
			String url = attributes.getValue(DOWNLOAD_URL);
			String type = attributes.getValue(MIME_TYPE);
			if (state.getCurrentItem().getMedia() == null
					&& url != null
					&& (SyndTypeUtils.enclosureTypeValid(type) || ((type = SyndTypeUtils
							.getValidMimeTypeFromUrl(url)) != null))) {

				long size = 0;
				try {
					size = Long.parseLong(attributes.getValue(SIZE));
				} catch (NumberFormatException e) {
					if (AppConfig.DEBUG)
						Log.d(TAG, "Length attribute could not be parsed.");
				}
				
				int duration = 0;
				try {
					String durationStr = attributes.getValue(DURATION);
					if (durationStr != null) {
						duration = (int) TimeUnit.MILLISECONDS.convert(
								Long.parseLong(durationStr), TimeUnit.SECONDS);
					}
				} catch (NumberFormatException e) {
					if (AppConfig.DEBUG)
						Log.d(TAG, "Duration attribute could not be parsed");
				}
				
				state.getCurrentItem().setMedia(
						new FeedMedia(state.getCurrentItem(), url, size, type));
			}
		}
		return new SyndElement(localName, this);
	}

	@Override
	public void handleElementEnd(String localName, HandlerState state) {

	}

}