Move mime type guessing out of MediaType enum
This commit is contained in:
parent
c376be44dd
commit
d4d506345b
|
@ -34,6 +34,7 @@ import de.danoeh.antennapod.model.feed.FeedItem;
|
|||
import de.danoeh.antennapod.model.feed.FeedMedia;
|
||||
import de.danoeh.antennapod.model.feed.FeedPreferences;
|
||||
import de.danoeh.antennapod.model.playback.MediaType;
|
||||
import de.danoeh.antennapod.parser.feed.util.MimeTypeUtils;
|
||||
|
||||
public class LocalFeedUpdater {
|
||||
|
||||
|
@ -74,21 +75,8 @@ public class LocalFeedUpdater {
|
|||
List<DocumentFile> mediaFiles = new ArrayList<>();
|
||||
Set<String> mediaFileNames = new HashSet<>();
|
||||
for (DocumentFile file : documentFolder.listFiles()) {
|
||||
String mime = file.getType();
|
||||
if (mime == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
MediaType mediaType = MediaType.fromMimeType(mime);
|
||||
if (mediaType == MediaType.UNKNOWN) {
|
||||
String path = file.getUri().toString();
|
||||
int fileExtensionPosition = path.lastIndexOf('.');
|
||||
if (fileExtensionPosition >= 0) {
|
||||
String extensionWithoutDot = path.substring(fileExtensionPosition + 1);
|
||||
mediaType = MediaType.fromFileExtension(extensionWithoutDot);
|
||||
}
|
||||
}
|
||||
|
||||
String mimeType = MimeTypeUtils.getMimeType(file.getType(), file.getUri().toString());
|
||||
MediaType mediaType = MediaType.fromMimeType(mimeType);
|
||||
if (mediaType == MediaType.AUDIO || mediaType == MediaType.VIDEO) {
|
||||
mediaFiles.add(file);
|
||||
mediaFileNames.add(file.getName());
|
||||
|
|
|
@ -15,16 +15,6 @@ public enum MediaType {
|
|||
"application/x-flac"
|
||||
));
|
||||
|
||||
// based on https://developer.android.com/guide/topics/media/media-formats
|
||||
static final Set<String> AUDIO_FILE_EXTENSIONS = new HashSet<>(Arrays.asList(
|
||||
"3gp", "aac", "amr", "flac", "imy", "m4a", "mid", "mkv", "mp3", "mp4", "mxmf", "oga",
|
||||
"ogg", "ogx", "opus", "ota", "rtttl", "rtx", "wav", "xmf"
|
||||
));
|
||||
|
||||
static final Set<String> VIDEO_FILE_EXTENSIONS = new HashSet<>(Arrays.asList(
|
||||
"3gp", "mkv", "mp4", "ogg", "ogv", "ogx", "webm"
|
||||
));
|
||||
|
||||
public static MediaType fromMimeType(String mimeType) {
|
||||
if (TextUtils.isEmpty(mimeType)) {
|
||||
return MediaType.UNKNOWN;
|
||||
|
@ -37,20 +27,4 @@ public enum MediaType {
|
|||
}
|
||||
return MediaType.UNKNOWN;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param extensionWithoutDot the file extension (suffix) without the dot
|
||||
* @return the {@link MediaType} that likely corresponds to the extension. However, since the
|
||||
* extension is not always enough to determine whether a file is an audio or video (3gp
|
||||
* can be both, for example), this may not be correct. As a result, where possible,
|
||||
* {@link #fromMimeType(String) fromMimeType} should always be tried first.
|
||||
*/
|
||||
public static MediaType fromFileExtension(String extensionWithoutDot) {
|
||||
if (AUDIO_FILE_EXTENSIONS.contains(extensionWithoutDot)) {
|
||||
return MediaType.AUDIO;
|
||||
} else if (VIDEO_FILE_EXTENSIONS.contains(extensionWithoutDot)) {
|
||||
return MediaType.VIDEO;
|
||||
}
|
||||
return MediaType.UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import org.xml.sax.Attributes;
|
|||
import de.danoeh.antennapod.model.feed.FeedItem;
|
||||
import de.danoeh.antennapod.model.feed.FeedMedia;
|
||||
import de.danoeh.antennapod.parser.feed.element.SyndElement;
|
||||
import de.danoeh.antennapod.parser.feed.util.SyndTypeUtils;
|
||||
import de.danoeh.antennapod.parser.feed.util.MimeTypeUtils;
|
||||
|
||||
public class Atom extends Namespace {
|
||||
private static final String TAG = "NSAtom";
|
||||
|
@ -91,11 +91,10 @@ public class Atom extends Namespace {
|
|||
} catch (NumberFormatException e) {
|
||||
Log.d(TAG, "Length attribute could not be parsed.");
|
||||
}
|
||||
String type = attributes.getValue(LINK_TYPE);
|
||||
String mimeType = SyndTypeUtils.getMimeType(type, href);
|
||||
String mimeType = MimeTypeUtils.getMimeType(attributes.getValue(LINK_TYPE), href);
|
||||
|
||||
FeedItem currItem = state.getCurrentItem();
|
||||
if (SyndTypeUtils.isMediaFile(mimeType) && currItem != null && !currItem.hasMedia()) {
|
||||
if (MimeTypeUtils.isMediaFile(mimeType) && currItem != null && !currItem.hasMedia()) {
|
||||
currItem.setMedia(new FeedMedia(currItem, href, size, mimeType));
|
||||
}
|
||||
} else if (LINK_REL_PAYMENT.equals(rel)) {
|
||||
|
|
|
@ -11,7 +11,7 @@ import java.util.concurrent.TimeUnit;
|
|||
|
||||
import de.danoeh.antennapod.model.feed.FeedMedia;
|
||||
import de.danoeh.antennapod.parser.feed.element.AtomText;
|
||||
import de.danoeh.antennapod.parser.feed.util.SyndTypeUtils;
|
||||
import de.danoeh.antennapod.parser.feed.util.MimeTypeUtils;
|
||||
|
||||
/** Processes tags from the http://search.yahoo.com/mrss/ namespace. */
|
||||
public class Media extends Namespace {
|
||||
|
@ -43,13 +43,12 @@ public class Media extends Namespace {
|
|||
Attributes attributes) {
|
||||
if (CONTENT.equals(localName)) {
|
||||
String url = attributes.getValue(DOWNLOAD_URL);
|
||||
String type = attributes.getValue(MIME_TYPE);
|
||||
String defaultStr = attributes.getValue(DEFAULT);
|
||||
String medium = attributes.getValue(MEDIUM);
|
||||
boolean validTypeMedia = false;
|
||||
boolean validTypeImage = false;
|
||||
boolean isDefault = "true".equals(defaultStr);
|
||||
String mimeType = SyndTypeUtils.getMimeType(type, url);
|
||||
String mimeType = MimeTypeUtils.getMimeType(attributes.getValue(MIME_TYPE), url);
|
||||
|
||||
if (MEDIUM_AUDIO.equals(medium)) {
|
||||
validTypeMedia = true;
|
||||
|
@ -63,9 +62,9 @@ public class Media extends Namespace {
|
|||
validTypeImage = true;
|
||||
mimeType = "image/*";
|
||||
} else {
|
||||
if (SyndTypeUtils.isMediaFile(mimeType)) {
|
||||
if (MimeTypeUtils.isMediaFile(mimeType)) {
|
||||
validTypeMedia = true;
|
||||
} else if (SyndTypeUtils.isImageFile(mimeType)) {
|
||||
} else if (MimeTypeUtils.isImageFile(mimeType)) {
|
||||
validTypeImage = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ import org.xml.sax.Attributes;
|
|||
|
||||
import de.danoeh.antennapod.model.feed.FeedItem;
|
||||
import de.danoeh.antennapod.model.feed.FeedMedia;
|
||||
import de.danoeh.antennapod.parser.feed.util.SyndTypeUtils;
|
||||
import de.danoeh.antennapod.parser.feed.util.MimeTypeUtils;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
|
@ -46,13 +46,12 @@ public class Rss20 extends Namespace {
|
|||
state.getItems().add(state.getCurrentItem());
|
||||
state.getCurrentItem().setFeed(state.getFeed());
|
||||
} else if (ENCLOSURE.equals(localName) && ITEM.equals(state.getTagstack().peek().getName())) {
|
||||
String type = attributes.getValue(ENC_TYPE);
|
||||
String url = attributes.getValue(ENC_URL);
|
||||
String mimeType = SyndTypeUtils.getMimeType(type, url);
|
||||
String mimeType = MimeTypeUtils.getMimeType(attributes.getValue(ENC_TYPE), url);
|
||||
|
||||
boolean validUrl = !TextUtils.isEmpty(url);
|
||||
if (state.getCurrentItem() != null && state.getCurrentItem().getMedia() == null
|
||||
&& SyndTypeUtils.isMediaFile(mimeType) && validUrl) {
|
||||
&& MimeTypeUtils.isMediaFile(mimeType) && validUrl) {
|
||||
long size = 0;
|
||||
try {
|
||||
size = Long.parseLong(attributes.getValue(ENC_LEN));
|
||||
|
|
|
@ -4,13 +4,27 @@ import android.webkit.MimeTypeMap;
|
|||
import androidx.annotation.Nullable;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Utility class for handling MIME-Types of enclosures.
|
||||
* */
|
||||
public class SyndTypeUtils {
|
||||
public class MimeTypeUtils {
|
||||
public static final String OCTET_STREAM = "application/octet-stream";
|
||||
|
||||
private SyndTypeUtils() {
|
||||
// based on https://developer.android.com/guide/topics/media/media-formats
|
||||
static final Set<String> AUDIO_FILE_EXTENSIONS = new HashSet<>(Arrays.asList(
|
||||
"3gp", "aac", "amr", "flac", "imy", "m4a", "mid", "mkv", "mp3", "mp4", "mxmf", "oga",
|
||||
"ogg", "ogx", "opus", "ota", "rtttl", "rtx", "wav", "xmf"
|
||||
));
|
||||
|
||||
static final Set<String> VIDEO_FILE_EXTENSIONS = new HashSet<>(Arrays.asList(
|
||||
"3gp", "mkv", "mp4", "ogg", "ogv", "ogx", "webm"
|
||||
));
|
||||
|
||||
private MimeTypeUtils() {
|
||||
|
||||
}
|
||||
|
||||
|
@ -19,7 +33,7 @@ public class SyndTypeUtils {
|
|||
if (isMediaFile(type) && !OCTET_STREAM.equals(type)) {
|
||||
return type;
|
||||
}
|
||||
String filenameType = SyndTypeUtils.getMimeTypeFromUrl(filename);
|
||||
String filenameType = MimeTypeUtils.getMimeTypeFromUrl(filename);
|
||||
if (isMediaFile(filenameType)) {
|
||||
return filenameType;
|
||||
}
|
||||
|
@ -54,6 +68,16 @@ public class SyndTypeUtils {
|
|||
return null;
|
||||
}
|
||||
String extension = FilenameUtils.getExtension(url);
|
||||
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
||||
String mapResult = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
||||
if (mapResult != null) {
|
||||
return mapResult;
|
||||
}
|
||||
|
||||
if (AUDIO_FILE_EXTENSIONS.contains(extension)) {
|
||||
return "audio/*";
|
||||
} else if (VIDEO_FILE_EXTENSIONS.contains(extension)) {
|
||||
return "video/*";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue