Search in sources :

Example 6 with FeedMedia

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

the class NSAtom method handleElementStart.

@Override
public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) {
    if (ENTRY.equals(localName)) {
        state.setCurrentItem(new FeedItem());
        state.getItems().add(state.getCurrentItem());
        state.getCurrentItem().setFeed(state.getFeed());
    } else if (localName.matches(isText)) {
        String type = attributes.getValue(TEXT_TYPE);
        return new AtomText(localName, this, type);
    } else if (LINK.equals(localName)) {
        String href = attributes.getValue(LINK_HREF);
        String rel = attributes.getValue(LINK_REL);
        SyndElement parent = state.getTagstack().peek();
        if (parent.getName().matches(isFeedItem)) {
            if (LINK_REL_ALTERNATE.equals(rel)) {
                state.getCurrentItem().setLink(href);
            } else if (LINK_REL_ENCLOSURE.equals(rel)) {
                String strSize = attributes.getValue(LINK_LENGTH);
                long size = 0;
                try {
                    if (strSize != null) {
                        size = Long.parseLong(strSize);
                    }
                } catch (NumberFormatException e) {
                    Log.d(TAG, "Length attribute could not be parsed.");
                }
                String type = attributes.getValue(LINK_TYPE);
                if (type == null) {
                    type = SyndTypeUtils.getMimeTypeFromUrl(href);
                }
                if (SyndTypeUtils.enclosureTypeValid(type)) {
                    FeedItem currItem = state.getCurrentItem();
                    if (currItem != null && !currItem.hasMedia()) {
                        currItem.setMedia(new FeedMedia(currItem, href, size, type));
                    }
                }
            } else if (LINK_REL_PAYMENT.equals(rel)) {
                state.getCurrentItem().setPaymentLink(href);
            }
        } else if (parent.getName().matches(isFeed)) {
            if (LINK_REL_ALTERNATE.equals(rel)) {
                String type = attributes.getValue(LINK_TYPE);
                /*
                     * Use as link if a) no type-attribute is given and
					 * feed-object has no link yet b) type of link is
					 * LINK_TYPE_HTML or LINK_TYPE_XHTML
					 */
                if (state.getFeed() != null && ((type == null && state.getFeed().getLink() == null) || (LINK_TYPE_HTML.equals(type) || LINK_TYPE_XHTML.equals(type)))) {
                    state.getFeed().setLink(href);
                } else if (LINK_TYPE_ATOM.equals(type) || LINK_TYPE_RSS.equals(type)) {
                    // treat as podlove alternate feed
                    String title = attributes.getValue(LINK_TITLE);
                    if (TextUtils.isEmpty(title)) {
                        title = href;
                    }
                    state.addAlternateFeedUrl(title, href);
                }
            } else if (LINK_REL_ARCHIVES.equals(rel) && state.getFeed() != null) {
                String type = attributes.getValue(LINK_TYPE);
                if (LINK_TYPE_ATOM.equals(type) || LINK_TYPE_RSS.equals(type)) {
                    String title = attributes.getValue(LINK_TITLE);
                    if (TextUtils.isEmpty(title)) {
                        title = href;
                    }
                    state.addAlternateFeedUrl(title, href);
                } else if (LINK_TYPE_HTML.equals(type) || LINK_TYPE_XHTML.equals(type)) {
                //A Link such as to a directory such as iTunes
                }
            } else if (LINK_REL_PAYMENT.equals(rel) && state.getFeed() != null) {
                state.getFeed().setPaymentLink(href);
            } else if (LINK_REL_NEXT.equals(rel) && state.getFeed() != null) {
                state.getFeed().setPaged(true);
                state.getFeed().setNextPageLink(href);
            }
        }
    }
    return new SyndElement(localName, this);
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) SyndElement(de.danoeh.antennapod.core.syndication.namespace.SyndElement) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia)

Example 7 with FeedMedia

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

the class DBWriter method deleteFeedMediaOfItem.

/**
     * Deletes a downloaded FeedMedia file from the storage device.
     *
     * @param context A context that is used for opening a database connection.
     * @param mediaId ID of the FeedMedia object whose downloaded file should be deleted.
     */
public static Future<?> deleteFeedMediaOfItem(final Context context, final long mediaId) {
    return dbExec.submit(() -> {
        final FeedMedia media = DBReader.getFeedMedia(mediaId);
        if (media != null) {
            Log.i(TAG, String.format("Requested to delete FeedMedia [id=%d, title=%s, downloaded=%s", media.getId(), media.getEpisodeTitle(), String.valueOf(media.isDownloaded())));
            boolean result = false;
            if (media.isDownloaded()) {
                // delete downloaded media file
                File mediaFile = new File(media.getFile_url());
                if (mediaFile.exists()) {
                    result = mediaFile.delete();
                }
                media.setDownloaded(false);
                media.setFile_url(null);
                media.setHasEmbeddedPicture(false);
                PodDBAdapter adapter = PodDBAdapter.getInstance();
                adapter.open();
                adapter.setMedia(media);
                adapter.close();
                // If media is currently being played, change playback
                // type to 'stream' and shutdown playback service
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                if (PlaybackPreferences.getCurrentlyPlayingMedia() == FeedMedia.PLAYABLE_TYPE_FEEDMEDIA) {
                    if (media.getId() == PlaybackPreferences.getCurrentlyPlayingFeedMediaId()) {
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.putBoolean(PlaybackPreferences.PREF_CURRENT_EPISODE_IS_STREAM, true);
                        editor.commit();
                    }
                    if (PlaybackPreferences.getCurrentlyPlayingFeedMediaId() == media.getId()) {
                        context.sendBroadcast(new Intent(PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
                    }
                }
                // Gpodder: queue delete action for synchronization
                if (GpodnetPreferences.loggedIn()) {
                    FeedItem item = media.getItem();
                    GpodnetEpisodeAction action = new GpodnetEpisodeAction.Builder(item, GpodnetEpisodeAction.Action.DELETE).currentDeviceId().currentTimestamp().build();
                    GpodnetPreferences.enqueueEpisodeAction(action);
                }
            }
            Log.d(TAG, "Deleting File. Result: " + result);
            EventBus.getDefault().post(FeedItemEvent.deletedMedia(Collections.singletonList(media.getItem())));
            EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast();
        }
    });
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) GpodnetEpisodeAction(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) File(java.io.File)

Example 8 with FeedMedia

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

the class DBReader method extractItemlistFromCursor.

private static List<FeedItem> extractItemlistFromCursor(PodDBAdapter adapter, Cursor cursor) {
    List<FeedItem> result = new ArrayList<>(cursor.getCount());
    LongList imageIds = new LongList(cursor.getCount());
    LongList itemIds = new LongList(cursor.getCount());
    if (cursor.moveToFirst()) {
        do {
            int indexImage = cursor.getColumnIndex(PodDBAdapter.KEY_IMAGE);
            long imageId = cursor.getLong(indexImage);
            imageIds.add(imageId);
            FeedItem item = FeedItem.fromCursor(cursor);
            result.add(item);
            itemIds.add(item.getId());
        } while (cursor.moveToNext());
        Map<Long, FeedImage> images = getFeedImages(adapter, imageIds.toArray());
        Map<Long, FeedMedia> medias = getFeedMedia(adapter, itemIds.toArray());
        for (int i = 0; i < result.size(); i++) {
            FeedItem item = result.get(i);
            long imageId = imageIds.get(i);
            FeedImage image = images.get(imageId);
            item.setImage(image);
            FeedMedia media = medias.get(item.getId());
            item.setMedia(media);
            if (media != null) {
                media.setItem(item);
            }
        }
    }
    return result;
}
Also used : FeedImage(de.danoeh.antennapod.core.feed.FeedImage) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) ArrayList(java.util.ArrayList) LongList(de.danoeh.antennapod.core.util.LongList)

Example 9 with FeedMedia

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

the class PlayerWidgetService method onDestroy.

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "Service is about to be destroyed");
    if (playbackService != null) {
        Playable playable = playbackService.getPlayable();
        if (playable != null && playable instanceof FeedMedia) {
            FeedMedia media = (FeedMedia) playable;
            if (media.hasAlmostEnded()) {
                Log.d(TAG, "smart mark as read");
                FeedItem item = media.getItem();
                DBWriter.markItemPlayed(item, FeedItem.PLAYED, false);
                DBWriter.removeQueueItem(this, item, false);
                DBWriter.addItemToPlaybackHistory(media);
                if (item.getFeed().getPreferences().getCurrentAutoDelete()) {
                    Log.d(TAG, "Delete " + media.toString());
                    DBWriter.deleteFeedMediaOfItem(this, media.getId());
                }
            }
        }
    }
    try {
        unbindService(mConnection);
    } catch (IllegalArgumentException e) {
        Log.w(TAG, "IllegalArgumentException when trying to unbind service");
    }
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Playable(de.danoeh.antennapod.core.util.playback.Playable) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia)

Example 10 with FeedMedia

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

the class MediaplayerActivity method checkFavorite.

private void checkFavorite() {
    Playable playable = controller.getMedia();
    if (playable != null && playable instanceof FeedMedia) {
        FeedItem feedItem = ((FeedMedia) playable).getItem();
        if (feedItem != null) {
            Observable.fromCallable(() -> DBReader.getFeedItem(feedItem.getId())).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(item -> {
                boolean isFav = item.isTagged(FeedItem.TAG_FAVORITE);
                if (isFavorite != isFav) {
                    isFavorite = isFav;
                    invalidateOptionsMenu();
                }
            }, error -> Log.e(TAG, Log.getStackTraceString(error)));
        }
    }
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Playable(de.danoeh.antennapod.core.util.playback.Playable) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia)

Aggregations

FeedMedia (de.danoeh.antennapod.core.feed.FeedMedia)53 FeedItem (de.danoeh.antennapod.core.feed.FeedItem)39 Feed (de.danoeh.antennapod.core.feed.Feed)20 Date (java.util.Date)19 PodDBAdapter (de.danoeh.antennapod.core.storage.PodDBAdapter)16 ArrayList (java.util.ArrayList)13 FeedImage (de.danoeh.antennapod.core.feed.FeedImage)10 File (java.io.File)9 Intent (android.content.Intent)7 Playable (de.danoeh.antennapod.core.util.playback.Playable)7 Cursor (android.database.Cursor)6 SharedPreferences (android.content.SharedPreferences)4 TypedArray (android.content.res.TypedArray)3 Uri (android.net.Uri)3 View (android.view.View)3 Chapter (de.danoeh.antennapod.core.feed.Chapter)3 GpodnetEpisodeAction (de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction)3 Context (android.content.Context)2 ArrayMap (android.support.v4.util.ArrayMap)2 FlakyTest (android.test.FlakyTest)2