android: allow pass downloader type

This commit is contained in:
Kesha Antonov
2023-07-17 12:51:23 +03:00
parent 15bc65bca5
commit aacb21e280
2 changed files with 22 additions and 7 deletions
+13
View File
@@ -333,6 +333,19 @@ An object containing options properties
| ------------- | ------------------------------------------------ | :------: | :-------: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `type` | String | | Android | Downloader type: 'parallel' or 'sequential'. Default: 'sequential'. 'parallel' seems to cause lots of ANRs, so be careful |
**Usage**
```javascript
import { initDownloader } from '@kesha-antonov/react-native-background-downloader'
...
// SOMEWHERE AT APP STARTUP
useEffect(() => {
initDownloader({ type: 'parallel' })
}, [])
```
## Constants
### directories
@@ -8,6 +8,7 @@ import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableArray;
@@ -84,10 +85,11 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp
private HashMap<String, WritableMap> progressReports = new HashMap<>();
private static Object sharedLock = new Object();
public RNBackgroundDownloaderModule(ReactApplicationContext reactContext, ReadableMap options) {
public RNBackgroundDownloaderModule(ReactApplicationContext reactContext) {
super(reactContext);
this.initDownloader();
ReadableMap emptyMap = Arguments.createMap();
this.initDownloader(emptyMap);
}
@Override
@@ -140,30 +142,30 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp
constants.put("PriorityLow", Priority.LOW.getValue());
constants.put("OnlyWifi", NetworkType.WIFI_ONLY.getValue());
constants.put("AllNetworks", NetworkType.ALL.getValue());
return constants;
return constants;
}
@ReactMethod
public void initDownloader(ReadableMap options) {
if (fetch) {
if (fetch != null) {
fetch.close();
fetch = null;
}
Downloader downloader = options.getString("androidDownloaderType") == "parallel"
Downloader.FileDownloaderType downloaderType = options.getString("type") == "parallel"
? Downloader.FileDownloaderType.PARALLEL
: Downloader.FileDownloaderType.SEQUENTIAL;
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
final Downloader okHttpDownloader = new OkHttpDownloader(okHttpClient,
downloader);
downloaderType);
loadConfigMap();
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this.getReactApplicationContext())
.setDownloadConcurrentLimit(4)
.setHttpDownloader(okHttpDownloader)
.enableRetryOnNetworkGain(true)
.setHttpDownloader(new HttpUrlConnectionDownloader(downloader))
.setHttpDownloader(new HttpUrlConnectionDownloader(downloaderType))
.build();
fetch = Fetch.Impl.getInstance(fetchConfiguration);
fetch.addListener(this);