diff options
author | Herbert Reiter <46045854+damoasda@users.noreply.github.com> | 2021-01-05 16:32:15 +0100 |
---|---|---|
committer | Herbert Reiter <46045854+damoasda@users.noreply.github.com> | 2021-01-13 20:51:49 +0100 |
commit | fd3b07cafbbf09bba8e210d0442924d4634da85c (patch) | |
tree | af8e2735a0ad8d8ff13342310ae38ec157d9345a /core/src/main | |
parent | 092e9a9a20a29bf1d434d7717242bdc99b19180e (diff) | |
download | AntennaPod-fd3b07cafbbf09bba8e210d0442924d4634da85c.zip |
Use any image for local folders
Diffstat (limited to 'core/src/main')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java b/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java index 4e59fd750..7eb6618b5 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java @@ -6,15 +6,13 @@ import android.media.MediaMetadataRetriever; import android.net.Uri; import android.text.TextUtils; +import androidx.annotation.NonNull; import androidx.documentfile.provider.DocumentFile; -import org.apache.commons.lang3.StringUtils; - import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashSet; @@ -34,6 +32,8 @@ import de.danoeh.antennapod.core.util.DownloadError; public class LocalFeedUpdater { + static final String[] PREFERRED_FEED_IMAGE_FILENAMES = { "folder.jpg", "Folder.jpg", "folder.png", "Folder.png" }; + public static void updateFeed(Feed feed, Context context) { try { tryUpdateFeed(feed, context); @@ -97,18 +97,7 @@ public class LocalFeedUpdater { } } - List<String> iconLocations = Arrays.asList("folder.jpg", "Folder.jpg", "folder.png", "Folder.png"); - for (String iconLocation : iconLocations) { - DocumentFile image = documentFolder.findFile(iconLocation); - if (image != null) { - feed.setImageUrl(image.getUri().toString()); - break; - } - } - if (StringUtils.isBlank(feed.getImageUrl())) { - // set default feed image - feed.setImageUrl(getDefaultIconUrl(context)); - } + feed.setImageUrl(getImageUrl(context, documentFolder)); feed.getPreferences().setAutoDownload(false); feed.getPreferences().setAutoDeleteAction(FeedPreferences.AutoDeleteAction.NO); @@ -123,6 +112,40 @@ public class LocalFeedUpdater { } /** + * Returns the image URL for the local feed. + */ + @NonNull + static String getImageUrl(@NonNull Context context, @NonNull DocumentFile documentFolder) { + String imageUrl = null; + + // look for special file names + for (String iconLocation : PREFERRED_FEED_IMAGE_FILENAMES) { + DocumentFile image = documentFolder.findFile(iconLocation); + if (image != null) { + imageUrl = image.getUri().toString(); + break; + } + } + + // use the first image in the folder if existing + if (imageUrl == null) { + for (DocumentFile file : documentFolder.listFiles()) { + String mime = file.getType(); + if (mime != null && (mime.startsWith("image/jpeg") || mime.startsWith("image/png"))) { + imageUrl = file.getUri().toString(); + break; + } + } + } + + // use default icon as fallback + if (imageUrl == null) { + imageUrl = getDefaultIconUrl(context); + } + return imageUrl; + } + + /** * Returns the URL of the default icon for a local feed. The URL refers to an app resource file. */ public static String getDefaultIconUrl(Context context) { @@ -161,7 +184,7 @@ public class LocalFeedUpdater { return item; } - private static void loadMetadata(FeedItem item, DocumentFile file, Context context) throws Exception { + private static void loadMetadata(FeedItem item, DocumentFile file, Context context) { MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); mediaMetadataRetriever.setDataSource(context, file.getUri()); |