diff --git a/android/build.gradle b/android/build.gradle index 3018efe8..cac52e0a 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -23,7 +23,9 @@ dependencies { implementation 'com.facebook.react:react-native:+' if (project.properties['android.useAndroidX'] == 'true' || project.properties['android.useAndroidX'] == true) { api "androidx.tonyodev.fetch2:xfetch2:3.1.4" + implementation "androidx.tonyodev.fetch2okhttp:xfetch2okhttp:3.1.4" } else { api "com.tonyodev.fetch2:fetch2:3.0.10" + implementation "com.tonyodev.fetch2okhttp:fetch2okhttp:3.0.10" } } diff --git a/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java b/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java index 0c976a15..f932472d 100644 --- a/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java +++ b/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java @@ -14,6 +14,8 @@ import com.facebook.react.bridge.WritableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; import com.tonyodev.fetch2.Download; +import com.tonyodev.fetch2core.Downloader; +import com.tonyodev.fetch2okhttp.OkHttpDownloader; import com.tonyodev.fetch2.Error; import com.tonyodev.fetch2.Fetch; import com.tonyodev.fetch2.FetchConfiguration; @@ -40,6 +42,8 @@ import javax.annotation.Nullable; +import okhttp3.OkHttpClient; + public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule implements FetchListener { private static final int TASK_RUNNING = 0; @@ -72,15 +76,17 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp private DeviceEventManagerModule.RCTDeviceEventEmitter ee; private Date lastProgressReport = new Date(); private HashMap progressReports = new HashMap<>(); - private static Object sharedLock = new Object(); + private static Object sharedLock = new Object(); public RNBackgroundDownloaderModule(ReactApplicationContext reactContext) { super(reactContext); - + OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); + final Downloader okHttpDownloader = new OkHttpDownloader(okHttpClient, + Downloader.FileDownloaderType.PARALLEL); loadConfigMap(); FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this.getReactApplicationContext()) .setDownloadConcurrentLimit(4) - .setNamespace("RNBackgroundDownloader") + .setHttpDownloader(okHttpDownloader) .build(); fetch = Fetch.Impl.getInstance(fetchConfiguration); fetch.addListener(this); @@ -164,7 +170,7 @@ private void loadConfigMap() { e.printStackTrace(); } } - + private int convertErrorCode(Error error) { if ((error == Error.FILE_NOT_CREATED) || (error == Error.WRITE_PERMISSION_DENIED)) { @@ -196,6 +202,7 @@ public void download(ReadableMap options) { RNBGDTaskConfig config = new RNBGDTaskConfig(id); final Request request = new Request(url, destination); + request.setAutoRetryMaxAttempts(15); if (headers != null) { ReadableMapKeySetIterator it = headers.keySetIterator(); while (it.hasNextKey()) { @@ -205,7 +212,7 @@ public void download(ReadableMap options) { } request.setPriority(options.hasKey("priority") ? Priority.valueOf(options.getInt("priority")) : Priority.NORMAL); request.setNetworkType(options.hasKey("network") ? NetworkType.valueOf(options.getInt("network")) : NetworkType.ALL); - + fetch.enqueue(request, new Func() { @Override public void call(Request download) { @@ -214,7 +221,7 @@ public void call(Request download) { @Override public void call(Error error) { //An error occurred when enqueuing a request. - + WritableMap params = Arguments.createMap(); params.putString("id", id); params.putString("error", error.toString());