14
14
import com .facebook .react .bridge .WritableMap ;
15
15
import com .facebook .react .modules .core .DeviceEventManagerModule ;
16
16
import com .tonyodev .fetch2 .Download ;
17
+ import com .tonyodev .fetch2core .Downloader ;
18
+ import com .tonyodev .fetch2okhttp .OkHttpDownloader ;
17
19
import com .tonyodev .fetch2 .Error ;
18
20
import com .tonyodev .fetch2 .Fetch ;
19
21
import com .tonyodev .fetch2 .FetchConfiguration ;
40
42
41
43
import javax .annotation .Nullable ;
42
44
45
+ import okhttp3 .OkHttpClient ;
46
+
43
47
public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule implements FetchListener {
44
48
45
49
private static final int TASK_RUNNING = 0 ;
@@ -72,14 +76,17 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp
72
76
private DeviceEventManagerModule .RCTDeviceEventEmitter ee ;
73
77
private Date lastProgressReport = new Date ();
74
78
private HashMap <String , WritableMap > progressReports = new HashMap <>();
75
- private static Object sharedLock = new Object ();
79
+ private static Object sharedLock = new Object ();
76
80
77
81
public RNBackgroundDownloaderModule (ReactApplicationContext reactContext ) {
78
82
super (reactContext );
79
-
83
+ OkHttpClient okHttpClient = new OkHttpClient .Builder ().build ();
84
+ final Downloader okHttpDownloader = new OkHttpDownloader (okHttpClient ,
85
+ Downloader .FileDownloaderType .PARALLEL );
80
86
loadConfigMap ();
81
87
FetchConfiguration fetchConfiguration = new FetchConfiguration .Builder (this .getReactApplicationContext ())
82
88
.setDownloadConcurrentLimit (4 )
89
+ .setHttpDownloader (okHttpDownloader )
83
90
.build ();
84
91
fetch = Fetch .Impl .getInstance (fetchConfiguration );
85
92
fetch .addListener (this );
@@ -163,7 +170,7 @@ private void loadConfigMap() {
163
170
e .printStackTrace ();
164
171
}
165
172
}
166
-
173
+
167
174
private int convertErrorCode (Error error ) {
168
175
if ((error == Error .FILE_NOT_CREATED )
169
176
|| (error == Error .WRITE_PERMISSION_DENIED )) {
@@ -204,7 +211,7 @@ public void download(ReadableMap options) {
204
211
}
205
212
request .setPriority (options .hasKey ("priority" ) ? Priority .valueOf (options .getInt ("priority" )) : Priority .NORMAL );
206
213
request .setNetworkType (options .hasKey ("network" ) ? NetworkType .valueOf (options .getInt ("network" )) : NetworkType .ALL );
207
-
214
+
208
215
fetch .enqueue (request , new Func <Request >() {
209
216
@ Override
210
217
public void call (Request download ) {
@@ -213,7 +220,7 @@ public void call(Request download) {
213
220
@ Override
214
221
public void call (Error error ) {
215
222
//An error occurred when enqueuing a request.
216
-
223
+
217
224
WritableMap params = Arguments .createMap ();
218
225
params .putString ("id" , id );
219
226
params .putString ("error" , error .toString ());
0 commit comments