diff options
author | ByteHamster <info@bytehamster.com> | 2022-11-05 13:40:44 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2022-11-05 20:44:53 +0100 |
commit | 11292b598cc6867e32f4f313dfd6eff1197efdd3 (patch) | |
tree | 7eaefa01a6295c0eb1cacc036efc96f5572676b2 /core/src/main | |
parent | ae3971a58ff00755148b036794b7e5dc1c57017a (diff) | |
download | AntennaPod-11292b598cc6867e32f4f313dfd6eff1197efdd3.zip |
Remove dependency from other classes to DownloadService
Diffstat (limited to 'core/src/main')
10 files changed, 108 insertions, 68 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/ClientConfigurator.java b/core/src/main/java/de/danoeh/antennapod/core/ClientConfigurator.java index cdd516f16..b70a3a61a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/ClientConfigurator.java +++ b/core/src/main/java/de/danoeh/antennapod/core/ClientConfigurator.java @@ -6,6 +6,8 @@ import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; import de.danoeh.antennapod.core.preferences.UsageStatistics; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.download.AntennapodHttpClient; +import de.danoeh.antennapod.core.service.download.DownloadServiceInterface; +import de.danoeh.antennapod.core.service.download.DownloadServiceInterfaceImpl; import de.danoeh.antennapod.core.sync.SyncService; import de.danoeh.antennapod.core.sync.queue.SynchronizationQueueSink; import de.danoeh.antennapod.core.util.NetworkUtils; @@ -30,6 +32,7 @@ public class ClientConfigurator { SslProviderInstaller.install(context); NetworkUtils.init(context); NetworkConnectionChangeHandler.init(context); + DownloadServiceInterface.setImpl(new DownloadServiceInterfaceImpl()); SynchronizationQueueSink.setServiceStarterImpl(() -> SyncService.sync(context)); AntennapodHttpClient.setCacheDirectory(new File(context.getCacheDir(), "okhttp")); AntennapodHttpClient.setProxyConfig(UserPreferences.getProxyConfig()); diff --git a/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java b/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java index 37849c296..d026e1a7b 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java +++ b/core/src/main/java/de/danoeh/antennapod/core/backup/OpmlBackupAgent.java @@ -9,8 +9,8 @@ import android.os.ParcelFileDescriptor; import android.util.Log; import de.danoeh.antennapod.core.service.download.DownloadRequest; -import de.danoeh.antennapod.core.service.download.DownloadService; import de.danoeh.antennapod.core.service.download.DownloadRequestCreator; +import de.danoeh.antennapod.core.service.download.DownloadServiceInterface; import org.apache.commons.io.IOUtils; import org.xmlpull.v1.XmlPullParserException; @@ -145,7 +145,7 @@ public class OpmlBackupAgent extends BackupAgentHelper { for (OpmlElement opmlElem : opmlElements) { Feed feed = new Feed(opmlElem.getXmlUrl(), null, opmlElem.getText()); DownloadRequest request = DownloadRequestCreator.create(feed).build(); - DownloadService.download(mContext, false, request); + DownloadServiceInterface.get().download(mContext, false, request); } } catch (XmlPullParserException e) { Log.e(TAG, "Error while parsing the OPML file", e); 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 8c26a6d74..12bbf83d0 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 @@ -17,8 +17,6 @@ import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; import androidx.core.app.ServiceCompat; -import androidx.core.content.ContextCompat; -import de.danoeh.antennapod.core.BuildConfig; import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.core.feed.LocalFeedUpdater; import de.danoeh.antennapod.core.storage.EpisodeCleanupAlgorithmFactory; @@ -141,58 +139,6 @@ public class DownloadService extends Service { } } - public static void download(Context context, boolean cleanupMedia, DownloadRequest... requests) { - ArrayList<DownloadRequest> requestsToSend = new ArrayList<>(); - for (DownloadRequest request : requests) { - if (!isDownloadingFile(request.getSource())) { - requestsToSend.add(request); - } - } - if (requestsToSend.isEmpty()) { - return; - } else if (requestsToSend.size() > 100) { - if (BuildConfig.DEBUG) { - throw new IllegalArgumentException("Android silently drops intent payloads that are too large"); - } else { - Log.d(TAG, "Too many download requests. Dropping some to avoid Android dropping all."); - requestsToSend = new ArrayList<>(requestsToSend.subList(0, 100)); - } - } - - Intent launchIntent = new Intent(context, DownloadService.class); - launchIntent.putParcelableArrayListExtra(DownloadService.EXTRA_REQUESTS, requestsToSend); - if (cleanupMedia) { - launchIntent.putExtra(DownloadService.EXTRA_CLEANUP_MEDIA, true); - } - ContextCompat.startForegroundService(context, launchIntent); - } - - public static void refreshAllFeeds(Context context, boolean initiatedByUser) { - Intent launchIntent = new Intent(context, DownloadService.class); - launchIntent.putExtra(DownloadService.EXTRA_REFRESH_ALL, true); - launchIntent.putExtra(DownloadService.EXTRA_INITIATED_BY_USER, initiatedByUser); - ContextCompat.startForegroundService(context, launchIntent); - } - - public static void cancel(Context context, String url) { - if (!isRunning) { - return; - } - Intent cancelIntent = new Intent(DownloadService.ACTION_CANCEL_DOWNLOAD); - cancelIntent.putExtra(DownloadService.EXTRA_DOWNLOAD_URL, url); - cancelIntent.setPackage(context.getPackageName()); - context.sendBroadcast(cancelIntent); - } - - public static void cancelAll(Context context) { - if (!isRunning) { - return; - } - Intent cancelIntent = new Intent(DownloadService.ACTION_CANCEL_ALL_DOWNLOADS); - cancelIntent.setPackage(context.getPackageName()); - context.sendBroadcast(cancelIntent); - } - public static boolean isDownloadingFeeds() { if (!isRunning) { return false; @@ -373,7 +319,7 @@ public class DownloadService extends Service { Log.d(TAG, "Requested invalid range, restarting download from the beginning"); FileUtils.deleteQuietly(new File(downloader.getDownloadRequest().getDestination())); - download(this, false, downloader.getDownloadRequest()); + DownloadServiceInterface.get().download(this, false, downloader.getDownloadRequest()); } else { Log.e(TAG, "Download failed"); saveDownloadStatus(status); diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceInterface.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceInterface.java new file mode 100644 index 000000000..aa4b16490 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceInterface.java @@ -0,0 +1,23 @@ +package de.danoeh.antennapod.core.service.download; + +import android.content.Context; + +public abstract class DownloadServiceInterface { + private static DownloadServiceInterface impl; + + public static DownloadServiceInterface get() { + return impl; + } + + public static void setImpl(DownloadServiceInterface impl) { + DownloadServiceInterface.impl = impl; + } + + public abstract void download(Context context, boolean cleanupMedia, DownloadRequest... requests); + + public abstract void refreshAllFeeds(Context context, boolean initiatedByUser); + + public abstract void cancel(Context context, String url); + + public abstract void cancelAll(Context context); +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceInterfaceImpl.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceInterfaceImpl.java new file mode 100644 index 000000000..f8d34174e --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceInterfaceImpl.java @@ -0,0 +1,67 @@ +package de.danoeh.antennapod.core.service.download; + +import android.content.Context; +import android.content.Intent; +import androidx.core.content.ContextCompat; +import com.google.android.exoplayer2.util.Log; +import de.danoeh.antennapod.core.BuildConfig; + +import java.util.ArrayList; + +import static de.danoeh.antennapod.core.service.download.DownloadService.isDownloadingFile; + +public class DownloadServiceInterfaceImpl extends DownloadServiceInterface { + private static final String TAG = "DownloadServiceInterface"; + + public void download(Context context, boolean cleanupMedia, DownloadRequest... requests) { + ArrayList<DownloadRequest> requestsToSend = new ArrayList<>(); + for (DownloadRequest request : requests) { + if (!isDownloadingFile(request.getSource())) { + requestsToSend.add(request); + } + } + if (requestsToSend.isEmpty()) { + return; + } else if (requestsToSend.size() > 100) { + if (BuildConfig.DEBUG) { + throw new IllegalArgumentException("Android silently drops intent payloads that are too large"); + } else { + Log.d(TAG, "Too many download requests. Dropping some to avoid Android dropping all."); + requestsToSend = new ArrayList<>(requestsToSend.subList(0, 100)); + } + } + + Intent launchIntent = new Intent(context, DownloadService.class); + launchIntent.putParcelableArrayListExtra(DownloadService.EXTRA_REQUESTS, requestsToSend); + if (cleanupMedia) { + launchIntent.putExtra(DownloadService.EXTRA_CLEANUP_MEDIA, true); + } + ContextCompat.startForegroundService(context, launchIntent); + } + + public void refreshAllFeeds(Context context, boolean initiatedByUser) { + Intent launchIntent = new Intent(context, DownloadService.class); + launchIntent.putExtra(DownloadService.EXTRA_REFRESH_ALL, true); + launchIntent.putExtra(DownloadService.EXTRA_INITIATED_BY_USER, initiatedByUser); + ContextCompat.startForegroundService(context, launchIntent); + } + + public void cancel(Context context, String url) { + if (!DownloadService.isRunning) { + return; + } + Intent cancelIntent = new Intent(DownloadService.ACTION_CANCEL_DOWNLOAD); + cancelIntent.putExtra(DownloadService.EXTRA_DOWNLOAD_URL, url); + cancelIntent.setPackage(context.getPackageName()); + context.sendBroadcast(cancelIntent); + } + + public void cancelAll(Context context) { + if (!DownloadService.isRunning) { + return; + } + Intent cancelIntent = new Intent(DownloadService.ACTION_CANCEL_ALL_DOWNLOADS); + cancelIntent.setPackage(context.getPackageName()); + context.sendBroadcast(cancelIntent); + } +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java index 5e1e1127a..4d8a0c25e 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java @@ -9,7 +9,7 @@ import java.util.List; import de.danoeh.antennapod.core.service.download.DownloadRequest; import de.danoeh.antennapod.core.service.download.DownloadRequestCreator; -import de.danoeh.antennapod.core.service.download.DownloadService; +import de.danoeh.antennapod.core.service.download.DownloadServiceInterface; import de.danoeh.antennapod.core.util.PlaybackStatus; import de.danoeh.antennapod.model.feed.FeedItem; import de.danoeh.antennapod.model.feed.FeedPreferences; @@ -100,7 +100,7 @@ public class AutomaticDownloadAlgorithm { request.withInitiatedByUser(false); requests.add(request.build()); } - DownloadService.download(context, false, requests.toArray(new DownloadRequest[0])); + DownloadServiceInterface.get().download(context, false, requests.toArray(new DownloadRequest[0])); } } }; diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java index 3cc607433..13a18b0d7 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java @@ -12,7 +12,7 @@ import androidx.annotation.VisibleForTesting; import de.danoeh.antennapod.core.service.download.DownloadRequest; import de.danoeh.antennapod.core.service.download.DownloadRequestCreator; -import de.danoeh.antennapod.core.service.download.DownloadService; +import de.danoeh.antennapod.core.service.download.DownloadServiceInterface; import de.danoeh.antennapod.storage.database.PodDBAdapter; import de.danoeh.antennapod.storage.database.mapper.FeedCursorMapper; import org.greenrobot.eventbus.EventBus; @@ -113,7 +113,7 @@ public final class DBTasks { * @param initiatedByUser a boolean indicating if the refresh was triggered by user action. */ public static void refreshAllFeeds(final Context context, boolean initiatedByUser) { - DownloadService.refreshAllFeeds(context, initiatedByUser); + DownloadServiceInterface.get().refreshAllFeeds(context, initiatedByUser); SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, MODE_PRIVATE); prefs.edit().putLong(PREF_LAST_REFRESH, System.currentTimeMillis()).apply(); @@ -145,7 +145,7 @@ public final class DBTasks { DownloadRequest.Builder builder = DownloadRequestCreator.create(nextFeed); builder.loadAllPages(loadAllPages); - DownloadService.download(context, false, builder.build()); + DownloadServiceInterface.get().download(context, false, builder.build()); } else { Log.e(TAG, "loadNextPageOfFeed: Feed was either not paged or contained no nextPageLink"); } @@ -164,7 +164,7 @@ public final class DBTasks { builder.withInitiatedByUser(initiatedByUser); builder.setForce(true); builder.loadAllPages(loadAllPages); - DownloadService.download(context, false, builder.build()); + DownloadServiceInterface.get().download(context, false, builder.build()); } /** diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java index 083916ff9..fbcb86586 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java @@ -8,7 +8,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationManagerCompat; -import de.danoeh.antennapod.core.service.download.DownloadService; +import de.danoeh.antennapod.core.service.download.DownloadServiceInterface; import de.danoeh.antennapod.core.service.playback.PlaybackServiceInterface; import de.danoeh.antennapod.storage.database.PodDBAdapter; import org.greenrobot.eventbus.EventBus; @@ -206,7 +206,7 @@ public class DBWriter { if (item.getMedia().isDownloaded()) { deleteFeedMediaSynchronous(context, item.getMedia()); } - DownloadService.cancel(context, item.getMedia().getDownload_url()); + DownloadServiceInterface.get().cancel(context, item.getMedia().getDownload_url()); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/sync/SyncService.java b/core/src/main/java/de/danoeh/antennapod/core/sync/SyncService.java index 0640f7577..c304d67d2 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/sync/SyncService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/sync/SyncService.java @@ -23,6 +23,7 @@ import androidx.work.WorkerParameters; import de.danoeh.antennapod.core.service.download.DownloadRequest; import de.danoeh.antennapod.core.service.download.DownloadService; import de.danoeh.antennapod.core.service.download.DownloadRequestCreator; +import de.danoeh.antennapod.core.service.download.DownloadServiceInterface; import org.apache.commons.lang3.StringUtils; import org.greenrobot.eventbus.EventBus; @@ -152,7 +153,7 @@ public class SyncService extends Worker { if (!URLChecker.containsUrl(localSubscriptions, downloadUrl) && !queuedRemovedFeeds.contains(downloadUrl)) { Feed feed = new Feed(downloadUrl, null); DownloadRequest.Builder builder = DownloadRequestCreator.create(feed); - DownloadService.download(getApplicationContext(), false, builder.build()); + DownloadServiceInterface.get().download(getApplicationContext(), false, builder.build()); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/download/NetworkConnectionChangeHandler.java b/core/src/main/java/de/danoeh/antennapod/core/util/download/NetworkConnectionChangeHandler.java index 38acf7c7c..0eca498c9 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/download/NetworkConnectionChangeHandler.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/download/NetworkConnectionChangeHandler.java @@ -2,7 +2,7 @@ package de.danoeh.antennapod.core.util.download; import android.content.Context; import com.google.android.exoplayer2.util.Log; -import de.danoeh.antennapod.core.service.download.DownloadService; +import de.danoeh.antennapod.core.service.download.DownloadServiceInterface; import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.util.NetworkUtils; @@ -22,7 +22,7 @@ public abstract class NetworkConnectionChangeHandler { // otherwise cancel all downloads if (NetworkUtils.isNetworkRestricted()) { Log.i(TAG, "Device is no longer connected to Wi-Fi. Cancelling ongoing downloads"); - DownloadService.cancelAll(context); + DownloadServiceInterface.get().cancelAll(context); } } } |