Extracted DownloaderFactory to new class
This commit is contained in:
parent
8f168948ad
commit
ea6b41116f
|
@ -5,6 +5,7 @@ import androidx.annotation.Nullable;
|
|||
import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import de.danoeh.antennapod.core.service.download.DownloaderFactory;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.awaitility.core.ConditionTimeoutException;
|
||||
import org.junit.After;
|
||||
|
@ -47,7 +48,7 @@ public class DownloadServiceTest {
|
|||
private Feed testFeed = null;
|
||||
private FeedMedia testMedia11 = null;
|
||||
|
||||
private DownloadService.DownloaderFactory origFactory = null;
|
||||
private DownloaderFactory origFactory = null;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
@ -106,7 +107,7 @@ public class DownloadServiceTest {
|
|||
});
|
||||
}
|
||||
|
||||
private static class StubDownloaderFactory implements DownloadService.DownloaderFactory {
|
||||
private static class StubDownloaderFactory implements DownloaderFactory {
|
||||
private final long downloadTime;
|
||||
|
||||
@NonNull
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package de.danoeh.antennapod.core.service.download;
|
||||
|
||||
import android.util.Log;
|
||||
import android.webkit.URLUtil;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class DefaultDownloaderFactory implements DownloaderFactory {
|
||||
private static final String TAG = "DefaultDwnldrFactory";
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Downloader create(@NonNull DownloadRequest request) {
|
||||
if (!URLUtil.isHttpUrl(request.getSource()) && !URLUtil.isHttpsUrl(request.getSource())) {
|
||||
Log.e(TAG, "Could not find appropriate downloader for " + request.getSource());
|
||||
return null;
|
||||
}
|
||||
return new HttpDownloader(request);
|
||||
}
|
||||
}
|
|
@ -12,8 +12,6 @@ import android.os.Handler;
|
|||
import android.os.IBinder;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.webkit.URLUtil;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import de.danoeh.antennapod.core.ClientConfig;
|
||||
|
@ -116,6 +114,7 @@ public class DownloadService extends Service {
|
|||
private ScheduledFuture<?> downloadPostFuture;
|
||||
private static final int SCHED_EX_POOL_SIZE = 1;
|
||||
private ScheduledThreadPoolExecutor schedExecutor;
|
||||
private static DownloaderFactory downloaderFactory = new DefaultDownloaderFactory();
|
||||
|
||||
private final IBinder mBinder = new LocalBinder();
|
||||
|
||||
|
@ -397,7 +396,7 @@ public class DownloadService extends Service {
|
|||
|
||||
writeFileUrl(request);
|
||||
|
||||
Downloader downloader = getDownloader(request);
|
||||
Downloader downloader = downloaderFactory.create(request);
|
||||
if (downloader != null) {
|
||||
numberOfDownloads.incrementAndGet();
|
||||
// smaller rss feeds before bigger media files
|
||||
|
@ -414,26 +413,6 @@ public class DownloadService extends Service {
|
|||
queryDownloads();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public interface DownloaderFactory {
|
||||
@Nullable
|
||||
Downloader create(@NonNull DownloadRequest request);
|
||||
}
|
||||
|
||||
private static class DefaultDownloaderFactory implements DownloaderFactory {
|
||||
@Nullable
|
||||
@Override
|
||||
public Downloader create(@NonNull DownloadRequest request) {
|
||||
if (!URLUtil.isHttpUrl(request.getSource()) && !URLUtil.isHttpsUrl(request.getSource())) {
|
||||
Log.e(TAG, "Could not find appropriate downloader for " + request.getSource());
|
||||
return null;
|
||||
}
|
||||
return new HttpDownloader(request);
|
||||
}
|
||||
}
|
||||
|
||||
private static DownloaderFactory downloaderFactory = new DefaultDownloaderFactory();
|
||||
|
||||
@VisibleForTesting
|
||||
public static DownloaderFactory getDownloaderFactory() {
|
||||
return downloaderFactory;
|
||||
|
@ -446,10 +425,6 @@ public class DownloadService extends Service {
|
|||
DownloadService.downloaderFactory = downloaderFactory;
|
||||
}
|
||||
|
||||
private Downloader getDownloader(@NonNull DownloadRequest request) {
|
||||
return downloaderFactory.create(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove download from the DownloadRequester list and from the
|
||||
* DownloadService list.
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package de.danoeh.antennapod.core.service.download;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public interface DownloaderFactory {
|
||||
@Nullable
|
||||
Downloader create(@NonNull DownloadRequest request);
|
||||
}
|
Loading…
Reference in New Issue