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

import android.webkit.MimeTypeMap;
import org.apache.commons.io.FilenameUtils;

/** Utility class for handling MIME-Types of enclosures */
public class SyndTypeUtils {

	private static final String VALID_MEDIA_MIMETYPE = "audio/.*" + "|" + "video/.*"
			+ "|" + "application/ogg";
	private static final String VALID_IMAGE_MIMETYPE = "image/.*";


	private SyndTypeUtils() {

	}

	public static boolean enclosureTypeValid(String type) {
		if (type == null) {
			return false;
		} else {
			return type.matches(VALID_MEDIA_MIMETYPE);
		}
	}
	public static boolean imageTypeValid(String type) {
		if (type == null) {
			return false;
		} else {
			return type.matches(VALID_IMAGE_MIMETYPE);
		}
	}


	/**
	 * Should be used if mime-type of enclosure tag is not supported. This
	 * method will check if the mime-type of the file extension is supported. If
	 * the type is not supported, this method will return null.
	 */
	public static String getValidMimeTypeFromUrl(String url) {
		if (url != null) {
			String extension = FilenameUtils.getExtension(url);
			if (extension != null) {
				String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
				if (type != null && enclosureTypeValid(type)) {
					return type;
				}
			}
		}
		return null;
	}

	/**
	 * Should be used if mime-type of enclosure tag is not supported. This
	 * method will return the mime-type of the file extension.
	 */
	public static String getMimeTypeFromUrl(String url) {
		if (url != null) {
			String extension = FilenameUtils.getExtension(url);
			if (extension != null) {
				return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
			}
		}
		return null;
	}

}

@@ -1030,216 +1030,53 @@
 %7B%0A%09%09
-if (url != null) %7B%0A%09%09%09String extension = FilenameUtils.getExtension(url);%0A%09%09%09if (extension != null) %7B%0A%09%09%09%09String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension
+String type = getMimeTypeFromUrl(url
 );%0A%09%09
-%09
-%09
 if (
-type != null && 
 encl
@@ -1102,18 +1102,16 @@
 )) %7B%0A%09%09%09
-%09%09
 return t
@@ -1121,21 +1121,18 @@
 ;%0A%09%09
-%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A
+%7D else %7B%0A%09
 %09%09re
@@ -1138,24 +1138,28 @@
 eturn null;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09/**%0A%09 *
@@ -1357,17 +1357,17 @@
 if (url 
-!
+=
 = null) 
@@ -1369,16 +1369,35 @@
 ull) %7B%0A%09
+%09%09return null;%0A%09%09%7D%0A
 %09%09String
@@ -1444,17 +1444,16 @@
 url);%0A%09%09
-%09
 if (exte
@@ -1462,9 +1462,9 @@
 ion 
-!
+=
 = nu
@@ -1471,16 +1471,35 @@
 ll) %7B%0A%09%09
+%09return null;%0A%09%09%7D%0A%0A
 %09%09return
@@ -1568,33 +1568,8 @@
 );%0A%09
-%09%09%7D%0A%09%09%7D%0A%09%09return null;%0A%09%7D%0A
+%7D
 %0A%7D%0A

Improve readablilty