com.none.tom.simplerssreader.feed.CurrentFeed.java Source code

Java tutorial

Introduction

Here is the source code for com.none.tom.simplerssreader.feed.CurrentFeed.java

Source

// Copyright (c) 2017-2018, Tom Geiselmann
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY,WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.none.tom.simplerssreader.feed;

import android.content.Context;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;

import com.none.tom.simplerssreader.R;
import com.none.tom.simplerssreader.utils.DateUtils;
import com.none.tom.simplerssreader.utils.FileUtils;
import com.none.tom.simplerssreader.utils.HtmlUtils;
import com.none.tom.simplerssreader.utils.SearchResults;
import com.none.tom.simplerssreader.utils.SearchUtils;
import com.rometools.modules.itunes.EntryInformation;
import com.rometools.modules.itunes.FeedInformation;
import com.rometools.modules.itunes.types.Category;
import com.rometools.modules.slash.Slash;
import com.rometools.modules.wfw.WfwModule;
import com.rometools.rome.feed.module.DCModule;
import com.rometools.rome.feed.synd.SyndCategory;
import com.rometools.rome.feed.synd.SyndContent;
import com.rometools.rome.feed.synd.SyndEnclosure;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.parser.Parser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

public class CurrentFeed {
    public static final int ITUNES_OWNER = 0;
    public static final int ITUNES_AUTHOR = 1;
    public static final int ITUNES_EXPLICIT = 2;
    public static final int ITUNES_DURATION = 3;
    public static final int ITUNES_SUBTITLE_SUMMARY = 4;

    private static SyndFeed sCurrentFeed;

    public static void set(final SyndFeed feed) {
        sCurrentFeed = feed;
    }

    public static List<SyndEntry> getEntries() {
        return sCurrentFeed.getEntries();
    }

    public static List<Category> getItunesCategories() {
        final FeedInformation feedInformation = (FeedInformation) sCurrentFeed.getModule(FeedInformation.URI);

        return feedInformation != null ? feedInformation.getCategories() : null;
    }

    public static String getAuthorForEntry(final SyndEntry entry) {
        return Parser.unescapeEntities(entry.getAuthor(), false);
    }

    public static String getTitleForEntry(final SyndEntry entry) {
        return Parser.unescapeEntities(entry.getTitle(), false);
    }

    public static String getLinkForEntry(final SyndEntry entry) {
        return entry.getLink();
    }

    public static String getCommentsForEntry(final SyndEntry entry) {
        return entry.getComments();
    }

    public static CharSequence getDescriptionForEntry(final SyndEntry entry) {
        final SyndContent itemDescription = entry.getDescription();

        if (itemDescription != null) {
            return Jsoup.parse(Parser.unescapeEntities(itemDescription.getValue(), false)).text();
        }

        return null;
    }

    public static CharSequence getContentForEntry(final SyndEntry entry) {
        final List<SyndContent> entryContents = entry.getContents();

        if (!entryContents.isEmpty()) {
            final SyndContent entryContent = entry.getContents().get(0);

            if ((entryContent.getType().equals("html") || entryContent.getType().equals("xhtml"))) {
                return HtmlUtils.fromHtml(Parser.unescapeEntities(entryContent.getValue(), false));
            }

            return Parser.unescapeEntities(entryContent.getValue(), false);
        }

        return null;
    }

    public static List<SyndCategory> getCategoriesForEntry(final SyndEntry entry) {
        return entry.getCategories();
    }

    public static String getPublishedDateForEntry(final Context context, final SyndEntry entry) {
        return DateUtils.parseDate(context, entry.getPublishedDate());
    }

    public static String getDcCreatorForEntry(final SyndEntry entry) {
        final DCModule dcModule = (DCModule) entry.getModule(DCModule.URI);

        return dcModule != null ? dcModule.getCreator() : null;
    }

    public static Integer getSlashCommentsForEntry(final SyndEntry entry) {
        final Slash slash = (Slash) entry.getModule(Slash.URI);

        return slash != null ? slash.getComments() : 0;
    }

    public static List<String> getEnclosureForEntry(final SyndEntry entry) {
        final SyndEnclosure enclosure = entry.getEnclosures().get(0);

        return new ArrayList<>(Arrays.asList(enclosure.getUrl(), FileUtils.parseFileSize(enclosure.getLength()),
                MimeTypeMap.getSingleton().getExtensionFromMimeType(enclosure.getType())));
    }

    public static LinkedHashMap<Integer, String> getItunesMetadataForEntry(final Context context,
            final SyndEntry entry) {
        final FeedInformation itunesFeed = (FeedInformation) sCurrentFeed.getModule(FeedInformation.URI);
        final EntryInformation itunesEntry = (EntryInformation) entry.getModule(EntryInformation.URI);

        final LinkedHashMap<Integer, String> itunesMetaData = new LinkedHashMap<>(5);
        final StringBuilder owner = new StringBuilder();

        if (itunesFeed != null) {
            if (!TextUtils.isEmpty(itunesFeed.getOwnerName())) {
                owner.append(itunesFeed.getOwnerName());
            }

            if (!TextUtils.isEmpty(itunesFeed.getOwnerEmailAddress())) {
                if (owner.length() > 0) {
                    owner.append('\n');
                }
                owner.append(itunesFeed.getOwnerEmailAddress());
            }

            itunesMetaData.put(ITUNES_OWNER, owner.toString());
        }

        if (itunesEntry != null) {
            itunesMetaData.put(ITUNES_AUTHOR,
                    !TextUtils.isEmpty(itunesEntry.getAuthor()) ? itunesEntry.getAuthor()
                            : (!TextUtils.isEmpty(sCurrentFeed.getManagingEditor())
                                    ? sCurrentFeed.getManagingEditor()
                                    : "-"));
            itunesMetaData.put(ITUNES_EXPLICIT,
                    itunesEntry.getExplicit() ? context.getString(R.string.yes) : context.getString(R.string.no));
            itunesMetaData.put(ITUNES_DURATION,
                    itunesEntry.getDuration() != null ? itunesEntry.getDuration().toString() : "-");
            itunesMetaData.put(ITUNES_SUBTITLE_SUMMARY,
                    !TextUtils.isEmpty(itunesEntry.getSummary()) ? itunesEntry.getSummary()
                            : itunesEntry.getSubtitle());
        }

        return itunesMetaData;
    }

    public static String getWfwCommentRssForEntry(final SyndEntry entry) {
        final WfwModule wfwModule = (WfwModule) entry.getModule(WfwModule.URI);

        return wfwModule != null ? wfwModule.getCommentRss() : null;
    }

    public static boolean entryContainsEnclosure(final SyndEntry entry) {
        return entry.getEnclosures() != null && !entry.getEnclosures().isEmpty();
    }

    public static boolean isItunesFeedPresent() {
        return sCurrentFeed.getModule(FeedInformation.URI) != null;
    }

    public static boolean isItunesEntry(final SyndEntry entry) {
        return entry.getModule(EntryInformation.URI) != null;
    }

    public static boolean isRSSFeed() {
        return sCurrentFeed.getFeedType().startsWith("rss");

    }

    public static List<String> getCategories() {
        final List<String> allCategories = new ArrayList<>();

        for (final SyndEntry entry : sCurrentFeed.getEntries()) {
            for (final SyndCategory category : entry.getCategories()) {
                final String name = category.getName();

                if (!TextUtils.isEmpty(name) && !allCategories.contains(name)) {
                    allCategories.add(name);
                }
            }
        }

        Collections.sort(allCategories);

        return allCategories;
    }

    public static List<Integer> getCategoriesPositions(final List<String> categories) {
        final List<Integer> positions = new ArrayList<>();

        int i = 1;

        for (final SyndEntry entry : sCurrentFeed.getEntries()) {
            for (final SyndCategory category : entry.getCategories()) {
                if (categories.contains(category.getName()) && !positions.contains(i)) {
                    positions.add(i);
                }
            }
            i++;
        }

        Collections.sort(positions);

        return positions;
    }

    public static boolean isCategoriesAvailable() {
        if (sCurrentFeed != null) {
            for (final SyndEntry entry : sCurrentFeed.getEntries()) {
                for (final SyndCategory category : entry.getCategories()) {
                    if (!TextUtils.isEmpty(category.getName())) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    public static SearchResults getSearchResultsFor(final String query, final int position) {
        final List<Integer> positions = new ArrayList<>();
        final List<LinkedHashMap<Integer, Integer>> indicesTitle = new ArrayList<>();

        int i = 0;

        for (final SyndEntry entry : sCurrentFeed.getEntries()) {
            final String title = entry.getTitle();

            if (StringUtils.containsIgnoreCase(title, query)) {
                indicesTitle.add(SearchUtils.getIndicesForQuery(title, query));
                positions.add(i);
            }
            i++;
        }

        return new SearchResults(position, positions, indicesTitle);
    }

    public static int size() {
        if (sCurrentFeed != null) {
            return sCurrentFeed.getEntries().size();
        }

        return 0;
    }
}