Search in sources :

Example 16 with FeedImage

use of de.danoeh.antennapod.core.feed.FeedImage 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)

Example 17 with FeedImage

use of de.danoeh.antennapod.core.feed.FeedImage in project AntennaPod by AntennaPod.

the class NSAtom method handleElementEnd.

@Override
public void handleElementEnd(String localName, HandlerState state) {
    if (ENTRY.equals(localName)) {
        if (state.getCurrentItem() != null && state.getTempObjects().containsKey(NSITunes.DURATION)) {
            FeedItem currentItem = state.getCurrentItem();
            if (currentItem.hasMedia()) {
                Integer duration = (Integer) state.getTempObjects().get(NSITunes.DURATION);
                currentItem.getMedia().setDuration(duration);
            }
            state.getTempObjects().remove(NSITunes.DURATION);
        }
        state.setCurrentItem(null);
    }
    if (state.getTagstack().size() >= 2) {
        AtomText textElement = null;
        String content;
        if (state.getContentBuf() != null) {
            content = state.getContentBuf().toString();
        } else {
            content = "";
        }
        SyndElement topElement = state.getTagstack().peek();
        String top = topElement.getName();
        SyndElement secondElement = state.getSecondTag();
        String second = secondElement.getName();
        if (top.matches(isText)) {
            textElement = (AtomText) topElement;
            textElement.setContent(content);
        }
        if (ID.equals(top)) {
            if (FEED.equals(second) && state.getFeed() != null) {
                state.getFeed().setFeedIdentifier(content);
            } else if (ENTRY.equals(second) && state.getCurrentItem() != null) {
                state.getCurrentItem().setItemIdentifier(content);
            }
        } else if (TITLE.equals(top) && textElement != null) {
            if (FEED.equals(second) && state.getFeed() != null) {
                state.getFeed().setTitle(textElement.getProcessedContent());
            } else if (ENTRY.equals(second) && state.getCurrentItem() != null) {
                state.getCurrentItem().setTitle(textElement.getProcessedContent());
            }
        } else if (SUBTITLE.equals(top) && FEED.equals(second) && textElement != null && state.getFeed() != null) {
            state.getFeed().setDescription(textElement.getProcessedContent());
        } else if (CONTENT.equals(top) && ENTRY.equals(second) && textElement != null && state.getCurrentItem() != null) {
            state.getCurrentItem().setDescription(textElement.getProcessedContent());
        } else if (SUMMARY.equals(top) && ENTRY.equals(second) && textElement != null && state.getCurrentItem() != null && state.getCurrentItem().getDescription() == null) {
            state.getCurrentItem().setDescription(textElement.getProcessedContent());
        } else if (UPDATED.equals(top) && ENTRY.equals(second) && state.getCurrentItem() != null && state.getCurrentItem().getPubDate() == null) {
            state.getCurrentItem().setPubDate(DateUtils.parse(content));
        } else if (PUBLISHED.equals(top) && ENTRY.equals(second) && state.getCurrentItem() != null) {
            state.getCurrentItem().setPubDate(DateUtils.parse(content));
        } else if (IMAGE_LOGO.equals(top) && state.getFeed() != null && state.getFeed().getImage() == null) {
            state.getFeed().setImage(new FeedImage(state.getFeed(), content, null));
        } else if (IMAGE_ICON.equals(top) && state.getFeed() != null) {
            state.getFeed().setImage(new FeedImage(state.getFeed(), content, null));
        } else if (AUTHOR_NAME.equals(top) && AUTHOR.equals(second) && state.getFeed() != null && state.getCurrentItem() == null) {
            String currentName = state.getFeed().getAuthor();
            if (currentName == null) {
                state.getFeed().setAuthor(content);
            } else {
                state.getFeed().setAuthor(currentName + ", " + content);
            }
        }
    }
}
Also used : FeedImage(de.danoeh.antennapod.core.feed.FeedImage) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) SyndElement(de.danoeh.antennapod.core.syndication.namespace.SyndElement)

Example 18 with FeedImage

use of de.danoeh.antennapod.core.feed.FeedImage in project AntennaPod by AntennaPod.

the class DBWriter method deleteFeed.

/**
     * Deletes a Feed and all downloaded files of its components like images and downloaded episodes.
     *
     * @param context A context that is used for opening a database connection.
     * @param feedId  ID of the Feed that should be deleted.
     */
public static Future<?> deleteFeed(final Context context, final long feedId) {
    return dbExec.submit(() -> {
        DownloadRequester requester = DownloadRequester.getInstance();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        final Feed feed = DBReader.getFeed(feedId);
        if (feed != null) {
            if (PlaybackPreferences.getCurrentlyPlayingMedia() == FeedMedia.PLAYABLE_TYPE_FEEDMEDIA && PlaybackPreferences.getLastPlayedFeedId() == feed.getId()) {
                context.sendBroadcast(new Intent(PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
                SharedPreferences.Editor editor = prefs.edit();
                editor.putLong(PlaybackPreferences.PREF_CURRENTLY_PLAYING_FEED_ID, -1);
                editor.commit();
            }
            // delete image file
            if (feed.getImage() != null) {
                if (feed.getImage().isDownloaded() && feed.getImage().getFile_url() != null) {
                    File imageFile = new File(feed.getImage().getFile_url());
                    imageFile.delete();
                } else if (requester.isDownloadingFile(feed.getImage())) {
                    requester.cancelDownload(context, feed.getImage());
                }
            }
            // delete stored media files and mark them as read
            List<FeedItem> queue = DBReader.getQueue();
            List<FeedItem> removed = new ArrayList<>();
            if (feed.getItems() == null) {
                DBReader.getFeedItemList(feed);
            }
            for (FeedItem item : feed.getItems()) {
                if (queue.remove(item)) {
                    removed.add(item);
                }
                if (item.getMedia() != null && item.getMedia().isDownloaded()) {
                    File mediaFile = new File(item.getMedia().getFile_url());
                    mediaFile.delete();
                } else if (item.getMedia() != null && requester.isDownloadingFile(item.getMedia())) {
                    requester.cancelDownload(context, item.getMedia());
                }
                if (item.hasItemImage()) {
                    FeedImage image = item.getImage();
                    if (image.isDownloaded() && image.getFile_url() != null) {
                        File imgFile = new File(image.getFile_url());
                        imgFile.delete();
                    } else if (requester.isDownloadingFile(image)) {
                        requester.cancelDownload(context, item.getImage());
                    }
                }
            }
            PodDBAdapter adapter = PodDBAdapter.getInstance();
            adapter.open();
            if (removed.size() > 0) {
                adapter.setQueue(queue);
                for (FeedItem item : removed) {
                    EventBus.getDefault().post(QueueEvent.irreversibleRemoved(item));
                }
            }
            adapter.removeFeed(feed);
            adapter.close();
            if (ClientConfig.gpodnetCallbacks.gpodnetEnabled()) {
                GpodnetPreferences.addRemovedFeed(feed.getDownload_url());
            }
            EventDistributor.getInstance().sendFeedUpdateBroadcast();
            // we assume we also removed download log entries for the feed or its media files.
            // especially important if download or refresh failed, as the user should not be able
            // to retry these
            EventDistributor.getInstance().sendDownloadLogUpdateBroadcast();
            BackupManager backupManager = new BackupManager(context);
            backupManager.dataChanged();
        }
    });
}
Also used : FeedImage(de.danoeh.antennapod.core.feed.FeedImage) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) Intent(android.content.Intent) File(java.io.File) BackupManager(android.app.backup.BackupManager) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 19 with FeedImage

use of de.danoeh.antennapod.core.feed.FeedImage in project AntennaPod by AntennaPod.

the class DBWriterTest method testDeleteFeedWithItemImages.

public void testDeleteFeedWithItemImages() throws InterruptedException, ExecutionException, TimeoutException, IOException {
    File destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER);
    assertNotNull(destFolder);
    Feed feed = new Feed("url", null, "title");
    feed.setItems(new ArrayList<>());
    // create Feed image
    File imgFile = new File(destFolder, "image");
    assertTrue(imgFile.createNewFile());
    FeedImage image = new FeedImage(0, "image", imgFile.getAbsolutePath(), "url", true);
    image.setOwner(feed);
    feed.setImage(image);
    // create items with images
    for (int i = 0; i < 10; i++) {
        FeedItem item = new FeedItem(0, "Item " + i, "Item" + i, "url", new Date(), FeedItem.PLAYED, feed);
        feed.getItems().add(item);
        File itemImageFile = new File(destFolder, "item-image-" + i);
        FeedImage itemImage = new FeedImage(0, "item-image" + i, itemImageFile.getAbsolutePath(), "url", true);
        item.setImage(itemImage);
    }
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    adapter.setCompleteFeed(feed);
    adapter.close();
    assertTrue(feed.getId() != 0);
    assertTrue(feed.getImage().getId() != 0);
    for (FeedItem item : feed.getItems()) {
        assertTrue(item.getId() != 0);
        assertTrue(item.getImage().getId() != 0);
    }
    DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(TIMEOUT, TimeUnit.SECONDS);
    // check if files still exist
    assertFalse(imgFile.exists());
    adapter = PodDBAdapter.getInstance();
    adapter.open();
    Cursor c = adapter.getFeedCursor(feed.getId());
    assertTrue(c.getCount() == 0);
    c.close();
    c = adapter.getImageCursor(String.valueOf(image.getId()));
    assertTrue(c.getCount() == 0);
    c.close();
    for (FeedItem item : feed.getItems()) {
        c = adapter.getFeedItemCursor(String.valueOf(item.getId()));
        assertTrue(c.getCount() == 0);
        c.close();
        c = adapter.getImageCursor(String.valueOf(item.getImage().getId()));
        assertEquals(0, c.getCount());
        c.close();
    }
    adapter.close();
}
Also used : PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedImage(de.danoeh.antennapod.core.feed.FeedImage) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Cursor(android.database.Cursor) File(java.io.File) Date(java.util.Date) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 20 with FeedImage

use of de.danoeh.antennapod.core.feed.FeedImage in project AntennaPod by AntennaPod.

the class DBWriterTest method testDeleteFeed.

public void testDeleteFeed() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    File destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER);
    assertNotNull(destFolder);
    Feed feed = new Feed("url", null, "title");
    feed.setItems(new ArrayList<>());
    // create Feed image
    File imgFile = new File(destFolder, "image");
    assertTrue(imgFile.createNewFile());
    FeedImage image = new FeedImage(0, "image", imgFile.getAbsolutePath(), "url", true);
    image.setOwner(feed);
    feed.setImage(image);
    List<File> itemFiles = new ArrayList<File>();
    // create items with downloaded media files
    for (int i = 0; i < 10; i++) {
        FeedItem item = new FeedItem(0, "Item " + i, "Item" + i, "url", new Date(), FeedItem.PLAYED, feed, true);
        feed.getItems().add(item);
        File enc = new File(destFolder, "file " + i);
        assertTrue(enc.createNewFile());
        itemFiles.add(enc);
        FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", enc.getAbsolutePath(), "download_url", true, null, 0, 0);
        item.setMedia(media);
        item.setChapters(new ArrayList<Chapter>());
        item.getChapters().add(new SimpleChapter(0, "item " + i, item, "example.com"));
    }
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    adapter.setCompleteFeed(feed);
    adapter.close();
    assertTrue(feed.getId() != 0);
    assertTrue(feed.getImage().getId() != 0);
    for (FeedItem item : feed.getItems()) {
        assertTrue(item.getId() != 0);
        assertTrue(item.getMedia().getId() != 0);
        assertTrue(item.getChapters().get(0).getId() != 0);
    }
    DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(TIMEOUT, TimeUnit.SECONDS);
    // check if files still exist
    assertFalse(imgFile.exists());
    for (File f : itemFiles) {
        assertFalse(f.exists());
    }
    adapter = PodDBAdapter.getInstance();
    adapter.open();
    Cursor c = adapter.getFeedCursor(feed.getId());
    assertEquals(0, c.getCount());
    c.close();
    c = adapter.getImageCursor(String.valueOf(image.getId()));
    assertEquals(0, c.getCount());
    c.close();
    for (FeedItem item : feed.getItems()) {
        c = adapter.getFeedItemCursor(String.valueOf(item.getId()));
        assertEquals(0, c.getCount());
        c.close();
        c = adapter.getSingleFeedMediaCursor(item.getMedia().getId());
        assertEquals(0, c.getCount());
        c.close();
        c = adapter.getSimpleChaptersOfFeedItemCursor(item);
        assertEquals(0, c.getCount());
        c.close();
    }
    adapter.close();
}
Also used : FeedImage(de.danoeh.antennapod.core.feed.FeedImage) ArrayList(java.util.ArrayList) SimpleChapter(de.danoeh.antennapod.core.feed.SimpleChapter) Chapter(de.danoeh.antennapod.core.feed.Chapter) Cursor(android.database.Cursor) Date(java.util.Date) SimpleChapter(de.danoeh.antennapod.core.feed.SimpleChapter) PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) File(java.io.File) Feed(de.danoeh.antennapod.core.feed.Feed)

Aggregations

FeedImage (de.danoeh.antennapod.core.feed.FeedImage)20 FeedItem (de.danoeh.antennapod.core.feed.FeedItem)14 Feed (de.danoeh.antennapod.core.feed.Feed)13 FeedMedia (de.danoeh.antennapod.core.feed.FeedMedia)10 File (java.io.File)8 Date (java.util.Date)8 Cursor (android.database.Cursor)7 PodDBAdapter (de.danoeh.antennapod.core.storage.PodDBAdapter)7 ArrayList (java.util.ArrayList)7 Chapter (de.danoeh.antennapod.core.feed.Chapter)2 BackupManager (android.app.backup.BackupManager)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 ArrayMap (android.support.v4.util.ArrayMap)1 MediaInfo (com.google.android.gms.cast.MediaInfo)1 MediaMetadata (com.google.android.gms.cast.MediaMetadata)1 WebImage (com.google.android.gms.common.images.WebImage)1 FeedPreferences (de.danoeh.antennapod.core.feed.FeedPreferences)1 SimpleChapter (de.danoeh.antennapod.core.feed.SimpleChapter)1 SyndElement (de.danoeh.antennapod.core.syndication.namespace.SyndElement)1