Search in sources :

Example 1 with MediaType

use of de.danoeh.antennapod.model.playback.MediaType in project AntennaPod by AntennaPod.

the class PlaybackSpeedUtils method getCurrentPlaybackSpeed.

/**
 * Returns the currently configured playback speed for the specified media.
 */
public static float getCurrentPlaybackSpeed(Playable media) {
    float playbackSpeed = SPEED_USE_GLOBAL;
    MediaType mediaType = null;
    if (media != null) {
        mediaType = media.getMediaType();
        playbackSpeed = PlaybackPreferences.getCurrentlyPlayingTemporaryPlaybackSpeed();
        if (playbackSpeed == SPEED_USE_GLOBAL && media instanceof FeedMedia) {
            FeedItem item = ((FeedMedia) media).getItem();
            if (item != null) {
                Feed feed = item.getFeed();
                if (feed != null && feed.getPreferences() != null) {
                    playbackSpeed = feed.getPreferences().getFeedPlaybackSpeed();
                } else {
                    Log.d(TAG, "Can not get feed specific playback speed: " + feed);
                }
            }
        }
    }
    if (playbackSpeed == SPEED_USE_GLOBAL) {
        playbackSpeed = UserPreferences.getPlaybackSpeed(mediaType);
    }
    return playbackSpeed;
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) MediaType(de.danoeh.antennapod.model.playback.MediaType) Feed(de.danoeh.antennapod.model.feed.Feed)

Example 2 with MediaType

use of de.danoeh.antennapod.model.playback.MediaType in project AntennaPod by AntennaPod.

the class LocalFeedUpdater method tryUpdateFeed.

private static void tryUpdateFeed(Feed feed, Context context) throws IOException {
    String uriString = feed.getDownload_url().replace(Feed.PREFIX_LOCAL_FOLDER, "");
    DocumentFile documentFolder = DocumentFile.fromTreeUri(context, Uri.parse(uriString));
    if (documentFolder == null) {
        throw new IOException("Unable to retrieve document tree. " + "Try re-connecting the folder on the podcast info page.");
    }
    if (!documentFolder.exists() || !documentFolder.canRead()) {
        throw new IOException("Cannot read local directory. " + "Try re-connecting the folder on the podcast info page.");
    }
    if (feed.getItems() == null) {
        feed.setItems(new ArrayList<>());
    }
    // make sure it is the latest 'version' of this feed from the db (all items etc)
    feed = DBTasks.updateFeed(context, feed, false);
    // list files in feed folder
    List<DocumentFile> mediaFiles = new ArrayList<>();
    Set<String> mediaFileNames = new HashSet<>();
    for (DocumentFile file : documentFolder.listFiles()) {
        String mime = file.getType();
        if (mime == null) {
            continue;
        }
        MediaType mediaType = MediaType.fromMimeType(mime);
        if (mediaType == MediaType.UNKNOWN) {
            String path = file.getUri().toString();
            int fileExtensionPosition = path.lastIndexOf('.');
            if (fileExtensionPosition >= 0) {
                String extensionWithoutDot = path.substring(fileExtensionPosition + 1);
                mediaType = MediaType.fromFileExtension(extensionWithoutDot);
            }
        }
        if (mediaType == MediaType.AUDIO || mediaType == MediaType.VIDEO) {
            mediaFiles.add(file);
            mediaFileNames.add(file.getName());
        }
    }
    // add new files to feed and update item data
    List<FeedItem> newItems = feed.getItems();
    for (DocumentFile f : mediaFiles) {
        FeedItem oldItem = feedContainsFile(feed, f.getName());
        FeedItem newItem = createFeedItem(feed, f, context);
        if (oldItem == null) {
            newItems.add(newItem);
        } else {
            oldItem.updateFromOther(newItem);
        }
    }
    // remove feed items without corresponding file
    Iterator<FeedItem> it = newItems.iterator();
    while (it.hasNext()) {
        FeedItem feedItem = it.next();
        if (!mediaFileNames.contains(feedItem.getLink())) {
            it.remove();
        }
    }
    feed.setImageUrl(getImageUrl(context, documentFolder));
    feed.getPreferences().setAutoDownload(false);
    feed.getPreferences().setAutoDeleteAction(FeedPreferences.AutoDeleteAction.NO);
    feed.setDescription(context.getString(R.string.local_feed_description));
    feed.setAuthor(context.getString(R.string.local_folder));
    // update items, delete items without existing file;
    // only delete items if the folder contains at least one element to avoid accidentally
    // deleting played state or position in case the folder is temporarily unavailable.
    boolean removeUnlistedItems = (newItems.size() >= 1);
    DBTasks.updateFeed(context, feed, removeUnlistedItems);
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FeedItem(de.danoeh.antennapod.model.feed.FeedItem) MediaType(de.danoeh.antennapod.model.playback.MediaType) HashSet(java.util.HashSet)

Aggregations

FeedItem (de.danoeh.antennapod.model.feed.FeedItem)2 MediaType (de.danoeh.antennapod.model.playback.MediaType)2 DocumentFile (androidx.documentfile.provider.DocumentFile)1 Feed (de.danoeh.antennapod.model.feed.Feed)1 FeedMedia (de.danoeh.antennapod.model.feed.FeedMedia)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1