Search in sources :

Example 1 with SearchResultValueComparator

use of de.danoeh.antennapod.core.util.comparator.SearchResultValueComparator in project AntennaPod by AntennaPod.

the class FeedSearcher method performSearch.

/**
     * Search through a feed, or all feeds, for episodes that match the query in either the title,
     * chapter, or show notes. The search is first performed on titles, then chapters, and finally
     * show notes. The list of resulting episodes also describes where the first match occurred
     * (title, chapters, or show notes).
     *
     * @param context
     * @param query search query
     * @param selectedFeed feed to search, 0 to search through all feeds
     * @return list of episodes containing the query
     */
public static List<SearchResult> performSearch(final Context context, final String query, final long selectedFeed) {
    final int[] values = { 2, 1, 0, 0 };
    final String[] subtitles = { context.getString(R.string.found_in_title_label), context.getString(R.string.found_in_chapters_label), context.getString(R.string.found_in_shownotes_label), context.getString(R.string.found_in_shownotes_label) };
    List<SearchResult> result = new ArrayList<>();
    List<FutureTask<List<FeedItem>>> tasks = new ArrayList<>();
    tasks.add(DBTasks.searchFeedItemTitle(context, selectedFeed, query));
    tasks.add(DBTasks.searchFeedItemChapters(context, selectedFeed, query));
    tasks.add(DBTasks.searchFeedItemDescription(context, selectedFeed, query));
    tasks.add(DBTasks.searchFeedItemContentEncoded(context, selectedFeed, query));
    for (FutureTask<List<FeedItem>> task : tasks) {
        task.run();
    }
    try {
        for (int i = 0; i < tasks.size(); i++) {
            FutureTask<List<FeedItem>> task = tasks.get(i);
            List<FeedItem> items = task.get();
            for (FeedItem item : items) {
                if (result.isEmpty() || !isDuplicate(result, item)) {
                    result.add(new SearchResult(item, values[i], subtitles[i]));
                }
            }
        }
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    Collections.sort(result, new SearchResultValueComparator());
    return result;
}
Also used : SearchResultValueComparator(de.danoeh.antennapod.core.util.comparator.SearchResultValueComparator) ArrayList(java.util.ArrayList) SearchResult(de.danoeh.antennapod.core.feed.SearchResult) FutureTask(java.util.concurrent.FutureTask) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) List(java.util.List) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

FeedItem (de.danoeh.antennapod.core.feed.FeedItem)1 SearchResult (de.danoeh.antennapod.core.feed.SearchResult)1 SearchResultValueComparator (de.danoeh.antennapod.core.util.comparator.SearchResultValueComparator)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 FutureTask (java.util.concurrent.FutureTask)1