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

Commit b9a806d

Browse files
author
Elad Gil
committed
Changed documents dir on Android to getExternalFilesDir if supported
Added conditional support for AndroidX
1 parent 69e172f commit b9a806d

File tree

7 files changed

+114
-100
lines changed

7 files changed

+114
-100
lines changed

android/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ android {
1818
dependencies {
1919
//noinspection GradleDynamicVersion
2020
implementation 'com.facebook.react:react-native:+'
21-
implementation "androidx.tonyodev.fetch2:xfetch2:3.1.4"
21+
if (project.properties['android.useAndroidX'] == true) {
22+
implementation "androidx.tonyodev.fetch2:xfetch2:3.1.4"
23+
} else {
24+
implementation "com.tonyodev.fetch2:fetch2:3.0.10"
25+
}
2226
}

android/src/main/java/com/eko/RNBackgroundDownloaderModule.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@ public boolean hasConstants() {
109109
@Override
110110
public Map<String, Object> getConstants() {
111111
Map<String, Object> constants = new HashMap<>();
112-
constants.put("documents", this.getReactApplicationContext().getFilesDir().getAbsolutePath());
112+
File externalDirectory = this.getReactApplicationContext().getExternalFilesDir(null);
113+
if (externalDirectory != null) {
114+
constants.put("documents", externalDirectory.getAbsolutePath());
115+
} else {
116+
constants.put("documents", this.getReactApplicationContext().getFilesDir().getAbsolutePath());
117+
}
118+
113119
constants.put("TaskRunning", TASK_RUNNING);
114120
constants.put("TaskSuspended", TASK_SUSPENDED);
115121
constants.put("TaskCanceling", TASK_CANCELING);

lib/downloadTask.js

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,93 @@ import { NativeModules } from 'react-native';
22
const { RNBackgroundDownloader } = NativeModules;
33

44
function validateHandler(handler) {
5-
if (!(typeof handler === 'function')) {
6-
throw new TypeError(`[RNBackgroundDownloader] expected argument to be a function, got: ${typeof handler}`);
7-
}
5+
if (!(typeof handler === 'function')) {
6+
throw new TypeError(`[RNBackgroundDownloader] expected argument to be a function, got: ${typeof handler}`);
7+
}
88
}
99
export default class DownloadTask {
10-
state = 'PENDING'
11-
percent = 0
12-
bytesWritten = 0
13-
totalBytes = 0
10+
state = 'PENDING'
11+
percent = 0
12+
bytesWritten = 0
13+
totalBytes = 0
1414

15-
constructor(taskInfo) {
16-
if (typeof taskInfo === 'string') {
17-
this.id = taskInfo;
18-
} else {
19-
this.id = taskInfo.id;
20-
this.percent = taskInfo.percent;
21-
this.bytesWritten = taskInfo.bytesWritten;
22-
this.totalBytes = taskInfo.totalBytes;
15+
constructor(taskInfo) {
16+
if (typeof taskInfo === 'string') {
17+
this.id = taskInfo;
18+
} else {
19+
this.id = taskInfo.id;
20+
this.percent = taskInfo.percent;
21+
this.bytesWritten = taskInfo.bytesWritten;
22+
this.totalBytes = taskInfo.totalBytes;
23+
}
2324
}
24-
}
2525

26-
begin(handler) {
27-
validateHandler(handler);
28-
this._beginHandler = handler;
29-
return this;
30-
}
26+
begin(handler) {
27+
validateHandler(handler);
28+
this._beginHandler = handler;
29+
return this;
30+
}
3131

32-
progress(handler) {
33-
validateHandler(handler);
34-
this._progressHandler = handler;
35-
return this;
36-
}
32+
progress(handler) {
33+
validateHandler(handler);
34+
this._progressHandler = handler;
35+
return this;
36+
}
3737

38-
done(handler) {
39-
validateHandler(handler);
40-
this._doneHandler = handler;
41-
return this;
42-
}
38+
done(handler) {
39+
validateHandler(handler);
40+
this._doneHandler = handler;
41+
return this;
42+
}
4343

44-
error(handler) {
45-
validateHandler(handler);
46-
this._errorHandler = handler;
47-
return this;
48-
}
44+
error(handler) {
45+
validateHandler(handler);
46+
this._errorHandler = handler;
47+
return this;
48+
}
4949

50-
_onBegin(expectedBytes) {
51-
this.state = 'DOWNLOADING';
52-
if (this._beginHandler) {
53-
this._beginHandler(expectedBytes);
50+
_onBegin(expectedBytes) {
51+
this.state = 'DOWNLOADING';
52+
if (this._beginHandler) {
53+
this._beginHandler(expectedBytes);
54+
}
5455
}
55-
}
5656

57-
_onProgress(percent, bytesWritten, totalBytes) {
58-
this.percent = percent;
59-
this.bytesWritten = bytesWritten;
60-
this.totalBytes = totalBytes;
61-
if (this._progressHandler) {
62-
this._progressHandler(percent, bytesWritten, totalBytes);
57+
_onProgress(percent, bytesWritten, totalBytes) {
58+
this.percent = percent;
59+
this.bytesWritten = bytesWritten;
60+
this.totalBytes = totalBytes;
61+
if (this._progressHandler) {
62+
this._progressHandler(percent, bytesWritten, totalBytes);
63+
}
6364
}
64-
}
6565

66-
_onDone() {
67-
this.state = 'DONE';
68-
if (this._doneHandler) {
69-
this._doneHandler();
66+
_onDone() {
67+
this.state = 'DONE';
68+
if (this._doneHandler) {
69+
this._doneHandler();
70+
}
7071
}
71-
}
7272

73-
_onError(error, errorCode) {
74-
this.state = 'FAILED';
75-
if (this._errorHandler) {
76-
this._errorHandler(error, errorCode);
73+
_onError(error, errorCode) {
74+
this.state = 'FAILED';
75+
if (this._errorHandler) {
76+
this._errorHandler(error, errorCode);
77+
}
7778
}
78-
}
7979

80-
pause() {
81-
this.state = 'PAUSED';
82-
RNBackgroundDownloader.pauseTask(this.id);
83-
}
80+
pause() {
81+
this.state = 'PAUSED';
82+
RNBackgroundDownloader.pauseTask(this.id);
83+
}
8484

85-
resume() {
86-
this.state = 'DOWNLOADING';
87-
RNBackgroundDownloader.resumeTask(this.id);
88-
}
85+
resume() {
86+
this.state = 'DOWNLOADING';
87+
RNBackgroundDownloader.resumeTask(this.id);
88+
}
8989

90-
stop() {
91-
this.state = 'STOPPED';
92-
RNBackgroundDownloader.stopTask(this.id);
93-
}
90+
stop() {
91+
this.state = 'STOPPED';
92+
RNBackgroundDownloader.stopTask(this.id);
93+
}
9494
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"jest": "^24.5.0",
5454
"metro-react-native-babel-preset": "^0.53.1",
5555
"react": "16.8.3",
56-
"react-native": "0.59.2",
56+
"react-native": "0.59.9",
5757
"react-native-fs": "^2.14.1",
5858
"react-native-vector-icons": "^6.6.0",
5959
"react-test-renderer": "16.8.3"

react-native-background-downloader.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Pod::Spec.new do |s|
77
A library for React-Native to help you download large files on iOS and Android both in the foreground and most importantly in the background.
88
DESC
99
s.author = '[email protected]'
10-
s.homepage = 'https://github.com/learnyst/react-native-background-downloader'
10+
s.homepage = 'https://github.com/EkoLabs/react-native-background-downloader'
1111
s.license = 'MIT'
1212
s.platform = :ios, '7.0'
13-
s.source = { git: 'https://github.com/learnyst/react-native-background-downloader.git', tag: 'master' }
13+
s.source = { git: 'https://github.com/EkoLabs/react-native-background-downloader.git', tag: 'master' }
1414
s.source_files = 'ios/**/*.{h,m}'
1515
s.requires_arc = true
1616

testApp/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default class App extends Component {
6666
async loadDownloads() {
6767
const mapStr = await AsyncStorage.getItem('idsToData');
6868
try {
69-
this.idsToData = JSON.parse(mapStr);
69+
this.idsToData = JSON.parse(mapStr) || {};
7070
} catch (e) {
7171
console.error(e);
7272
}

testApp/android/app/src/main/java/com/testapp/MainApplication.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,44 @@
88
import com.facebook.react.ReactPackage;
99
import com.facebook.react.shell.MainReactPackage;
1010
import com.facebook.soloader.SoLoader;
11+
import com.oblador.vectoricons.VectorIconsPackage;
12+
import com.rnfs.RNFSPackage;
1113

1214
import java.util.Arrays;
1315
import java.util.List;
1416

1517
public class MainApplication extends Application implements ReactApplication {
1618

17-
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
18-
@Override
19-
public boolean getUseDeveloperSupport() {
20-
return BuildConfig.DEBUG;
21-
}
19+
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
20+
@Override
21+
public boolean getUseDeveloperSupport() {
22+
return BuildConfig.DEBUG;
23+
}
24+
25+
@Override
26+
protected List<ReactPackage> getPackages() {
27+
return Arrays.<ReactPackage>asList(
28+
new MainReactPackage(),
29+
new RNBackgroundDownloaderPackage(),
30+
new RNFSPackage(),
31+
new VectorIconsPackage()
32+
);
33+
}
34+
35+
@Override
36+
protected String getJSMainModuleName() {
37+
return "testApp/index";
38+
}
39+
};
2240

2341
@Override
24-
protected List<ReactPackage> getPackages() {
25-
return Arrays.<ReactPackage>asList(
26-
new MainReactPackage(),
27-
new RNBackgroundDownloaderPackage()
28-
);
42+
public ReactNativeHost getReactNativeHost() {
43+
return mReactNativeHost;
2944
}
3045

3146
@Override
32-
protected String getJSMainModuleName() {
33-
return "testApp/index";
47+
public void onCreate() {
48+
super.onCreate();
49+
SoLoader.init(this, /* native exopackage */ false);
3450
}
35-
};
36-
37-
@Override
38-
public ReactNativeHost getReactNativeHost() {
39-
return mReactNativeHost;
40-
}
41-
42-
@Override
43-
public void onCreate() {
44-
super.onCreate();
45-
SoLoader.init(this, /* native exopackage */ false);
46-
}
4751
}

0 commit comments

Comments
 (0)