package de.danoeh.antennapod.core.syndication.namespace;

import android.text.TextUtils;
import android.util.Log;

import org.xml.sax.Attributes;

import java.util.concurrent.TimeUnit;

import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.syndication.handler.HandlerState;
import de.danoeh.antennapod.core.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 (CONTENT.equals(localName)) {
			String url = attributes.getValue(DOWNLOAD_URL);
			String type = attributes.getValue(MIME_TYPE);
            boolean validType;
            if(SyndTypeUtils.enclosureTypeValid(type)) {
                validType = true;
            } else {
                type = SyndTypeUtils.getValidMimeTypeFromUrl(url);
                validType = type != null;
            }
            if (state.getCurrentItem() != null && state.getCurrentItem().getMedia() == null &&
                url != null && validType) {
				long size = 0;
				String sizeStr = attributes.getValue(SIZE);
				try {
					size = Long.parseLong(sizeStr);
				} catch (NumberFormatException e) {
					Log.e(TAG, "Size \"" + sizeStr + "\" could not be parsed.");
				}

				int durationMs = 0;
				String durationStr = attributes.getValue(DURATION);
				if (!TextUtils.isEmpty(durationStr)) {
					try {
                        long duration = Long.parseLong(durationStr);
						durationMs = (int) TimeUnit.MILLISECONDS.convert(duration, TimeUnit.SECONDS);
					} catch (NumberFormatException e) {
						Log.e(TAG, "Duration \"" + durationStr + "\" could not be parsed");
					}
				}
				FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, type);
				if(durationMs > 0) {
					media.setDuration(durationMs);
				}
				state.getCurrentItem().setMedia(media);
			}
		}
		return new SyndElement(localName, this);
	}

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

	}

}

@@ -978,16 +978,25 @@
 ate,%0A%09%09%09
+%09%09%09%09%09%09%09  
 Attribut
@@ -1012,16 +1012,16 @@
 utes) %7B%0A
-
 %09%09if (CO
@@ -1147,28 +1147,19 @@
 _TYPE);%0A
-            
+%09%09%09
 boolean 
@@ -1169,30 +1169,22 @@
 idType;%0A
-            
+%09%09%09
 if
+ 
 (SyndTyp
@@ -1218,32 +1218,20 @@
 ype)) %7B%0A
-                
+%09%09%09%09
 validTyp
@@ -1244,45 +1244,24 @@
 ue;%0A
-            %7D else %7B%0A                
+%09%09%09%7D else %7B%0A%09%09%09%09
 type
@@ -1307,32 +1307,20 @@
 l(url);%0A
-                
+%09%09%09%09
 validTyp
@@ -1341,34 +1341,16 @@
 ll;%0A
-            %7D%0A            
+%09%09%09%7D%0A%09%09%09
 if (
@@ -1428,32 +1428,21 @@
 null &&%0A
-                
+%09%09%09%09%09
 url != n
@@ -1826,32 +1826,14 @@
 y %7B%0A
-                        
+%09%09%09%09%09%09
 long
@@ -2169,16 +2169,17 @@
 ;%0A%09%09%09%09if
+ 
 (duratio
@@ -2420,7 +2420,5 @@
 %0A%09%7D%0A
-%0A%7D%0A
+%7D

Format NSMedia.java