From 2df8e655ace3cb75a9d7c263bd61e9b4a792a9a5 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Wed, 16 Mar 2022 23:51:51 +0100 Subject: [PATCH] Updating local files takes long. Show progress. --- .../core/feed/LocalFeedUpdater.java | 23 +++++++++++---- .../service/download/DownloadService.java | 6 +++- .../download/DownloadServiceNotification.java | 28 ++++++++----------- .../core/feed/LocalFeedUpdaterTest.java | 2 +- 4 files changed, 34 insertions(+), 25 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 988b7c015..d37bc230d 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 @@ -7,6 +7,7 @@ import android.text.TextUtils; import android.util.Log; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.documentfile.provider.DocumentFile; import java.io.IOException; @@ -47,9 +48,10 @@ 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) { + public static void updateFeed(Feed feed, Context context, + @Nullable UpdaterProgressListener updaterProgressListener) { try { - tryUpdateFeed(feed, context); + tryUpdateFeed(feed, context, updaterProgressListener); if (mustReportDownloadSuccessful(feed)) { reportSuccess(feed); @@ -60,7 +62,8 @@ public class LocalFeedUpdater { } } - private static void tryUpdateFeed(Feed feed, Context context) throws IOException { + private static void tryUpdateFeed(Feed feed, Context context, UpdaterProgressListener updaterProgressListener) + throws IOException { String uriString = feed.getDownload_url().replace(Feed.PREFIX_LOCAL_FOLDER, ""); DocumentFile documentFolder = DocumentFile.fromTreeUri(context, Uri.parse(uriString)); if (documentFolder == null) { @@ -92,14 +95,17 @@ public class LocalFeedUpdater { // add new files to feed and update item data List newItems = feed.getItems(); - for (DocumentFile f : mediaFiles) { - FeedItem oldItem = feedContainsFile(feed, f.getName()); - FeedItem newItem = createFeedItem(feed, f, context); + for (int i = 0; i < mediaFiles.size(); i++) { + FeedItem oldItem = feedContainsFile(feed, mediaFiles.get(i).getName()); + FeedItem newItem = createFeedItem(feed, mediaFiles.get(i), context); if (oldItem == null) { newItems.add(newItem); } else { oldItem.updateFromOther(newItem); } + if (updaterProgressListener != null) { + updaterProgressListener.onLocalFileScanned(i, mediaFiles.size()); + } } // remove feed items without corresponding file @@ -260,4 +266,9 @@ public class LocalFeedUpdater { // (avoid logging success again if the last update was ok) return !lastDownloadStatus.isSuccessful(); } + + @FunctionalInterface + public interface UpdaterProgressListener { + void onLocalFileScanned(int scanned, int totalFiles); + } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java index 1522f42e2..85b539af8 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java @@ -301,7 +301,11 @@ public class DownloadService extends Service { private void performLocalFeedRefresh(Downloader downloader, DownloadRequest request) { try { Feed feed = DBReader.getFeed(request.getFeedfileId()); - LocalFeedUpdater.updateFeed(feed, DownloadService.this); + LocalFeedUpdater.updateFeed(feed, DownloadService.this, (scanned, totalFiles) -> { + request.setSize(totalFiles); + request.setSoFar(scanned); + request.setProgressPercent((int) (100.0 * scanned / totalFiles)); + }); } catch (Exception e) { e.printStackTrace(); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceNotification.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceNotification.java index a33409323..2bdd922f5 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceNotification.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceNotification.java @@ -73,24 +73,18 @@ public class DownloadServiceNotification { } stringBuilder.append("• "); DownloadRequest request = downloader.getDownloadRequest(); - switch (request.getFeedfileType()) { - case Feed.FEEDFILETYPE_FEED: - if (request.getTitle() != null) { - stringBuilder.append(request.getTitle()); - } - break; - case FeedMedia.FEEDFILETYPE_FEEDMEDIA: - if (request.getTitle() != null) { - stringBuilder.append(request.getTitle()) - .append(" (") - .append(request.getProgressPercent()) - .append("%)"); - } - break; - default: - stringBuilder.append("Unknown: ").append(request.getFeedfileType()); + if (request.getTitle() != null) { + stringBuilder.append(request.getTitle()); + } else { + stringBuilder.append(request.getSource()); } - if (i != downloads.size()) { + if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { + stringBuilder.append(" (").append(request.getProgressPercent()).append("%)"); + } else if (request.getSource().startsWith(Feed.PREFIX_LOCAL_FOLDER)) { + stringBuilder.append(" (").append(request.getSoFar()) + .append("/").append(request.getSize()).append(")"); + } + if (i != downloads.size() - 1) { stringBuilder.append("\n"); } } diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/LocalFeedUpdaterTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/LocalFeedUpdaterTest.java index bafee7293..05b0584ed 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/LocalFeedUpdaterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/feed/LocalFeedUpdaterTest.java @@ -276,7 +276,7 @@ public class LocalFeedUpdaterTest { // call method to test Feed feed = new Feed(FEED_URL, null); - LocalFeedUpdater.updateFeed(feed, context); + LocalFeedUpdater.updateFeed(feed, context, null); } }