Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Uses okHttp Parallel/Chunk Downloader as described here: #52

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
19 changes: 13 additions & 6 deletions android/src/main/java/com/eko/RNBackgroundDownloaderModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -72,15 +76,17 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp
private DeviceEventManagerModule.RCTDeviceEventEmitter ee;
private Date lastProgressReport = new Date();
private HashMap<String, WritableMap> 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);
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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<Request>() {
@Override
public void call(Request download) {
Expand All @@ -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());
Expand Down