@kesha-antonov/react-native-background-downloader 3.1.2 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +2 -2
- package/android/src/main/java/com/eko/Downloader.java +76 -104
- package/android/src/main/java/com/eko/RNBackgroundDownloaderModule.java +332 -348
- package/android/src/main/java/com/eko/RNBackgroundDownloaderPackage.java +3 -3
- package/android/src/main/java/com/eko/handlers/OnBegin.java +88 -0
- package/android/src/main/java/com/eko/handlers/OnBeginState.java +16 -0
- package/android/src/main/java/com/eko/handlers/OnProgress.java +121 -0
- package/android/src/main/java/com/eko/handlers/OnProgressState.java +14 -0
- package/android/src/main/java/com/eko/interfaces/BeginCallback.java +7 -0
- package/android/src/main/java/com/eko/{ProgressCallback.java → interfaces/ProgressCallback.java} +1 -1
- package/android/src/main/java/com/eko/utils/FileUtils.java +67 -0
- package/index.d.ts +5 -2
- package/ios/RNBGDTaskConfig.h +10 -19
- package/ios/RNBackgroundDownloader.m +300 -178
- package/lib/DownloadTask.ts +5 -0
- package/package.json +3 -3
- package/react-native-background-downloader.podspec +1 -1
- package/android/src/main/java/com/eko/OnBegin.java +0 -51
- package/android/src/main/java/com/eko/OnProgress.java +0 -104
package/android/build.gradle
CHANGED
|
@@ -24,6 +24,6 @@ android {
|
|
|
24
24
|
dependencies {
|
|
25
25
|
//noinspection GradleDynamicVersion
|
|
26
26
|
implementation 'com.facebook.react:react-native:+'
|
|
27
|
-
implementation 'com.tencent:mmkv
|
|
28
|
-
implementation 'com.google.code.gson:gson
|
|
27
|
+
implementation 'com.tencent:mmkv:1.3.4'
|
|
28
|
+
implementation 'com.google.code.gson:gson:2.10.1'
|
|
29
29
|
}
|
|
@@ -4,173 +4,145 @@ import android.app.DownloadManager;
|
|
|
4
4
|
import android.content.Context;
|
|
5
5
|
import android.content.Intent;
|
|
6
6
|
import android.database.Cursor;
|
|
7
|
-
import android.net.Uri;
|
|
8
|
-
import android.os.Environment;
|
|
9
7
|
|
|
10
8
|
import com.facebook.react.bridge.Arguments;
|
|
11
|
-
import com.facebook.react.bridge.ReadableMap;
|
|
12
|
-
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
|
13
9
|
import com.facebook.react.bridge.WritableMap;
|
|
14
|
-
import com.facebook.react.bridge.WritableNativeMap;
|
|
15
|
-
|
|
16
|
-
import java.util.HashMap;
|
|
17
|
-
import java.util.HashSet;
|
|
18
|
-
import java.util.Set;
|
|
19
10
|
|
|
20
11
|
import android.util.Log;
|
|
21
12
|
|
|
22
13
|
import static android.content.Context.DOWNLOAD_SERVICE;
|
|
23
14
|
|
|
24
15
|
public class Downloader {
|
|
25
|
-
|
|
16
|
+
private final Context context;
|
|
26
17
|
public DownloadManager downloadManager;
|
|
27
|
-
private Context context;
|
|
28
18
|
|
|
29
|
-
private HashSet<String> alreadySentIntentDownloadIds = new HashSet<>();
|
|
30
19
|
|
|
31
|
-
public Downloader(Context
|
|
32
|
-
context =
|
|
33
|
-
downloadManager = (DownloadManager)
|
|
20
|
+
public Downloader(Context context) {
|
|
21
|
+
this.context = context;
|
|
22
|
+
this.downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
|
|
34
23
|
}
|
|
35
24
|
|
|
36
|
-
public long
|
|
25
|
+
public long download(DownloadManager.Request request) {
|
|
37
26
|
return downloadManager.enqueue(request);
|
|
38
27
|
}
|
|
39
28
|
|
|
40
|
-
public
|
|
41
|
-
DownloadManager.Query downloadQuery = new DownloadManager.Query();
|
|
42
|
-
downloadQuery.setFilterById(downloadId);
|
|
43
|
-
Cursor cursor = downloadManager.query(downloadQuery);
|
|
44
|
-
|
|
45
|
-
WritableMap result = Arguments.createMap();
|
|
46
|
-
|
|
47
|
-
if (cursor.moveToFirst()) {
|
|
48
|
-
result = getDownloadStatus(cursor);
|
|
49
|
-
} else {
|
|
50
|
-
result.putString("downloadId", String.valueOf(downloadId));
|
|
51
|
-
result.putInt("status", DownloadManager.STATUS_FAILED);
|
|
52
|
-
result.putInt("reason", -1);
|
|
53
|
-
result.putString("reasonText", "COULD_NOT_FIND");
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return result;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public int cancelDownload(long downloadId) {
|
|
29
|
+
public int cancel(long downloadId) {
|
|
60
30
|
return downloadManager.remove(downloadId);
|
|
61
31
|
}
|
|
62
32
|
|
|
63
33
|
// WAITING FOR THE FIX TO BE MERGED
|
|
64
34
|
// https://android-review.googlesource.com/c/platform/packages/providers/DownloadProvider/+/2089866
|
|
65
|
-
public void
|
|
35
|
+
public void pause(long downloadId) {
|
|
66
36
|
// ContentValues values = new ContentValues();
|
|
67
|
-
|
|
68
37
|
// values.put(Downloads.Impl.COLUMN_CONTROL, Downloads.Impl.CONTROL_PAUSED);
|
|
69
38
|
// values.put(Downloads.Impl.COLUMN_STATUS,
|
|
70
39
|
// Downloads.Impl.STATUS_PAUSED_BY_APP);
|
|
71
|
-
|
|
72
40
|
// downloadManager.mResolver.update(ContentUris.withAppendedId(mBaseUri,
|
|
73
41
|
// ids[0]), values, null, null)
|
|
74
42
|
}
|
|
75
43
|
|
|
76
|
-
public void
|
|
44
|
+
public void resume(long downloadId) {
|
|
77
45
|
// ContentValues values = new ContentValues();
|
|
78
|
-
|
|
79
46
|
// values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING);
|
|
80
47
|
// values.put(Downloads.Impl.COLUMN_CONTROL, Downloads.Impl.CONTROL_RUN);
|
|
81
48
|
}
|
|
82
49
|
|
|
50
|
+
// Manually trigger the receiver from anywhere.
|
|
51
|
+
public void broadcast(long downloadId) {
|
|
52
|
+
Intent intent = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
|
|
53
|
+
intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, downloadId);
|
|
54
|
+
context.sendBroadcast(intent);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public WritableMap checkDownloadStatus(long downloadId) {
|
|
58
|
+
DownloadManager.Query query = new DownloadManager.Query();
|
|
59
|
+
query.setFilterById(downloadId);
|
|
60
|
+
|
|
61
|
+
WritableMap result = Arguments.createMap();
|
|
62
|
+
|
|
63
|
+
try (Cursor cursor = downloadManager.query(query);) {
|
|
64
|
+
if (cursor.moveToFirst()) {
|
|
65
|
+
result = getDownloadStatus(cursor);
|
|
66
|
+
} else {
|
|
67
|
+
result.putString("downloadId", String.valueOf(downloadId));
|
|
68
|
+
result.putInt("status", DownloadManager.STATUS_FAILED);
|
|
69
|
+
result.putInt("reason", -1);
|
|
70
|
+
result.putString("reasonText", "COULD_NOT_FIND");
|
|
71
|
+
}
|
|
72
|
+
} catch (Exception e) {
|
|
73
|
+
Log.e("RNBackgroundDownloader", "Downloader: " + Log.getStackTraceString(e));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
83
79
|
public WritableMap getDownloadStatus(Cursor cursor) {
|
|
84
|
-
String downloadId = cursor.getString(cursor.
|
|
85
|
-
String localUri = cursor.getString(cursor.
|
|
80
|
+
String downloadId = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
|
|
81
|
+
String localUri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI));
|
|
82
|
+
String bytesDownloadedSoFar = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
|
|
83
|
+
String totalSizeBytes = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
|
|
84
|
+
int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
|
|
85
|
+
int reason = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON));
|
|
86
|
+
|
|
86
87
|
if (localUri != null) {
|
|
87
88
|
localUri = localUri.replace("file://", "");
|
|
88
89
|
}
|
|
89
|
-
String bytesDownloadedSoFar = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
|
|
90
|
-
String totalSizeBytes = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
|
|
91
|
-
|
|
92
|
-
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
|
93
|
-
int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
|
|
94
90
|
|
|
95
91
|
String reasonText = "";
|
|
92
|
+
if (status == DownloadManager.STATUS_PAUSED || status == DownloadManager.STATUS_FAILED) {
|
|
93
|
+
reasonText = getReasonText(status, reason);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
WritableMap result = Arguments.createMap();
|
|
97
|
+
result.putString("downloadId", downloadId);
|
|
98
|
+
result.putInt("status", status);
|
|
99
|
+
result.putInt("reason", reason);
|
|
100
|
+
result.putString("reasonText", reasonText);
|
|
101
|
+
result.putDouble("bytesDownloaded", Long.parseLong(bytesDownloadedSoFar));
|
|
102
|
+
result.putDouble("bytesTotal", Long.parseLong(totalSizeBytes));
|
|
103
|
+
result.putString("localUri", localUri);
|
|
96
104
|
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public String getReasonText(int status, int reason) {
|
|
97
109
|
switch (status) {
|
|
98
110
|
case DownloadManager.STATUS_FAILED:
|
|
99
111
|
switch (reason) {
|
|
100
112
|
case DownloadManager.ERROR_CANNOT_RESUME:
|
|
101
|
-
|
|
102
|
-
break;
|
|
113
|
+
return "ERROR_CANNOT_RESUME";
|
|
103
114
|
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
|
|
104
|
-
|
|
105
|
-
break;
|
|
115
|
+
return "ERROR_DEVICE_NOT_FOUND";
|
|
106
116
|
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
|
|
107
|
-
|
|
108
|
-
break;
|
|
117
|
+
return "ERROR_FILE_ALREADY_EXISTS";
|
|
109
118
|
case DownloadManager.ERROR_FILE_ERROR:
|
|
110
|
-
|
|
111
|
-
break;
|
|
119
|
+
return "ERROR_FILE_ERROR";
|
|
112
120
|
case DownloadManager.ERROR_HTTP_DATA_ERROR:
|
|
113
|
-
|
|
114
|
-
break;
|
|
121
|
+
return "ERROR_HTTP_DATA_ERROR";
|
|
115
122
|
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
|
|
116
|
-
|
|
117
|
-
break;
|
|
123
|
+
return "ERROR_INSUFFICIENT_SPACE";
|
|
118
124
|
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
|
|
119
|
-
|
|
120
|
-
break;
|
|
125
|
+
return "ERROR_TOO_MANY_REDIRECTS";
|
|
121
126
|
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
|
|
122
|
-
|
|
123
|
-
break;
|
|
127
|
+
return "ERROR_UNHANDLED_HTTP_CODE";
|
|
124
128
|
default:
|
|
125
|
-
|
|
126
|
-
break;
|
|
129
|
+
return "ERROR_UNKNOWN";
|
|
127
130
|
}
|
|
128
|
-
break;
|
|
129
131
|
case DownloadManager.STATUS_PAUSED:
|
|
130
132
|
switch (reason) {
|
|
131
133
|
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
|
|
132
|
-
|
|
133
|
-
break;
|
|
134
|
+
return "PAUSED_QUEUED_FOR_WIFI";
|
|
134
135
|
case DownloadManager.PAUSED_UNKNOWN:
|
|
135
|
-
|
|
136
|
-
break;
|
|
136
|
+
return "PAUSED_UNKNOWN";
|
|
137
137
|
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
|
|
138
|
-
|
|
139
|
-
break;
|
|
138
|
+
return "PAUSED_WAITING_FOR_NETWORK";
|
|
140
139
|
case DownloadManager.PAUSED_WAITING_TO_RETRY:
|
|
141
|
-
|
|
142
|
-
break;
|
|
140
|
+
return "PAUSED_WAITING_TO_RETRY";
|
|
143
141
|
default:
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
break;
|
|
147
|
-
|
|
148
|
-
case DownloadManager.STATUS_SUCCESSFUL:
|
|
149
|
-
if(!alreadySentIntentDownloadIds.contains(downloadId)) {
|
|
150
|
-
alreadySentIntentDownloadIds.add(downloadId);
|
|
151
|
-
|
|
152
|
-
// broadcast the download complete to handle the case where the app was closed when the download was done
|
|
153
|
-
Intent intent = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
|
|
154
|
-
|
|
155
|
-
// You can add extras to the intent if needed
|
|
156
|
-
intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, Long.parseLong(downloadId));
|
|
157
|
-
context.sendBroadcast(intent);
|
|
142
|
+
return "UNKNOWN";
|
|
158
143
|
}
|
|
159
|
-
|
|
160
|
-
|
|
144
|
+
default:
|
|
145
|
+
return "UNKNOWN";
|
|
161
146
|
}
|
|
162
|
-
|
|
163
|
-
WritableMap result = Arguments.createMap();
|
|
164
|
-
result.putString("downloadId", downloadId);
|
|
165
|
-
|
|
166
|
-
result.putInt("status", status);
|
|
167
|
-
result.putInt("reason", reason);
|
|
168
|
-
result.putString("reasonText", reasonText);
|
|
169
|
-
|
|
170
|
-
result.putDouble("bytesDownloaded", Long.parseLong(bytesDownloadedSoFar));
|
|
171
|
-
result.putDouble("bytesTotal", Long.parseLong(totalSizeBytes));
|
|
172
|
-
result.putString("localUri", localUri);
|
|
173
|
-
|
|
174
|
-
return result;
|
|
175
147
|
}
|
|
176
148
|
}
|