Search in sources :

Example 1 with AtomText

use of de.danoeh.antennapod.core.syndication.namespace.atom.AtomText in project AntennaPod by AntennaPod.

the class NSMedia method handleElementStart.

@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);
        String defaultStr = attributes.getValue(DEFAULT);
        String medium = attributes.getValue(MEDIUM);
        boolean validTypeMedia = false;
        boolean validTypeImage = false;
        boolean isDefault = "true".equals(defaultStr);
        if (MEDIUM_AUDIO.equals(medium) || MEDIUM_VIDEO.equals(medium)) {
            validTypeMedia = true;
        } else if (MEDIUM_IMAGE.equals(medium)) {
            validTypeImage = true;
        } else {
            if (type == null) {
                type = SyndTypeUtils.getMimeTypeFromUrl(url);
            }
            if (SyndTypeUtils.enclosureTypeValid(type)) {
                validTypeMedia = true;
            } else if (SyndTypeUtils.imageTypeValid(type)) {
                validTypeImage = true;
            }
        }
        if (state.getCurrentItem() != null && (state.getCurrentItem().getMedia() == null || isDefault) && url != null && validTypeMedia) {
            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);
        } else if (state.getCurrentItem() != null && url != null && validTypeImage) {
            FeedImage image = new FeedImage();
            image.setDownload_url(url);
            image.setOwner(state.getCurrentItem());
            state.getCurrentItem().setImage(image);
        }
    } else if (IMAGE.equals(localName)) {
        String url = attributes.getValue(IMAGE_URL);
        if (url != null) {
            FeedImage image = new FeedImage();
            image.setDownload_url(url);
            if (state.getCurrentItem() != null) {
                image.setOwner(state.getCurrentItem());
                state.getCurrentItem().setImage(image);
            } else {
                if (state.getFeed().getImage() == null) {
                    image.setOwner(state.getFeed());
                    state.getFeed().setImage(image);
                }
            }
        }
    } else if (DESCRIPTION.equals(localName)) {
        String type = attributes.getValue(DESCRIPTION_TYPE);
        return new AtomText(localName, this, type);
    }
    return new SyndElement(localName, this);
}
Also used : FeedImage(de.danoeh.antennapod.core.feed.FeedImage) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) AtomText(de.danoeh.antennapod.core.syndication.namespace.atom.AtomText)

Aggregations

FeedImage (de.danoeh.antennapod.core.feed.FeedImage)1 FeedMedia (de.danoeh.antennapod.core.feed.FeedMedia)1 AtomText (de.danoeh.antennapod.core.syndication.namespace.atom.AtomText)1