@kesha-antonov/react-native-background-downloader 4.5.8 → 4.6.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/README.md +30 -3
- package/android/build.gradle +17 -0
- package/android/src/main/AndroidManifest.xml +24 -0
- package/android/src/main/java/com/eko/DownloadConstants.kt +14 -2
- package/android/src/main/java/com/eko/Downloader.kt +77 -12
- package/android/src/main/java/com/eko/PromiseExtensions.kt +36 -0
- package/android/src/main/java/com/eko/RNBackgroundDownloaderModuleImpl.kt +82 -134
- package/android/src/main/java/com/eko/ResumableDownloadService.kt +0 -4
- package/android/src/main/java/com/eko/ResumableDownloader.kt +121 -12
- package/android/src/main/java/com/eko/UIDTDownloadJobService.kt +65 -9
- package/android/src/main/java/com/eko/uidt/CancelDownloadReceiver.kt +64 -0
- package/android/src/main/java/com/eko/uidt/RNBGDFileProvider.kt +17 -0
- package/android/src/main/java/com/eko/uidt/UIDTJobIds.kt +112 -0
- package/android/src/main/java/com/eko/uidt/UIDTJobManager.kt +178 -17
- package/android/src/main/java/com/eko/uidt/UIDTJobState.kt +23 -1
- package/android/src/main/java/com/eko/uidt/UIDTNotificationIds.kt +73 -0
- package/android/src/main/java/com/eko/uidt/UIDTNotificationManager.kt +246 -19
- package/android/src/main/java/com/eko/utils/ReadableConverters.kt +55 -0
- package/android/src/main/java/com/eko/utils/RedirectResolver.kt +97 -0
- package/android/src/main/java/com/eko/utils/StorageManager.kt +13 -0
- package/android/src/main/res/xml/rnbgd_file_paths.xml +23 -0
- package/android/src/newarch/java/com/eko/RNBackgroundDownloaderModule.kt +18 -70
- package/android/src/oldarch/java/com/eko/RNBackgroundDownloaderModule.kt +18 -70
- package/android/src/test/java/com/eko/ResumableDownloaderConcurrencyTest.kt +199 -0
- package/android/src/test/java/com/eko/uidt/UIDTJobIdsTest.kt +159 -0
- package/android/src/test/java/com/eko/uidt/UIDTJobManagerTest.kt +192 -0
- package/android/src/test/java/com/eko/uidt/UIDTNotificationIdsTest.kt +99 -0
- package/ios/RNBackgroundDownloader.mm +187 -91
- package/lib/DownloadTask.js +8 -7
- package/lib/NativeRNBackgroundDownloader.d.ts +2 -0
- package/lib/UploadTask.js +11 -10
- package/lib/config.d.ts +3 -10
- package/lib/config.js +4 -44
- package/lib/index.d.ts +1 -1
- package/lib/index.js +43 -34
- package/lib/logger.d.ts +2 -0
- package/lib/logger.js +16 -0
- package/lib/notifications.d.ts +8 -0
- package/lib/notifications.js +35 -0
- package/lib/types.d.ts +19 -0
- package/package.json +8 -3
- package/src/DownloadTask.ts +2 -1
- package/src/NativeRNBackgroundDownloader.ts +8 -1
- package/src/UploadTask.ts +2 -1
- package/src/config.ts +9 -45
- package/src/index.ts +22 -2
- package/src/logger.ts +16 -0
- package/src/notifications.ts +32 -0
- package/src/types.ts +19 -0
package/lib/config.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.getGroupText = getGroupText;
|
|
5
|
-
exports.getNotificationTextsForNative = getNotificationTextsForNative;
|
|
3
|
+
exports.config = exports.DEFAULT_NOTIFICATION_TEXTS = exports.DEFAULT_IOS_DATA_PROTECTION = exports.DEFAULT_ALLOWS_CELLULAR_ACCESS = exports.DEFAULT_MAX_PARALLEL_DOWNLOADS = exports.DEFAULT_PROGRESS_MIN_BYTES = exports.DEFAULT_PROGRESS_INTERVAL = void 0;
|
|
6
4
|
exports.DEFAULT_PROGRESS_INTERVAL = 1000;
|
|
7
5
|
exports.DEFAULT_PROGRESS_MIN_BYTES = 1024 * 1024; // 1MB
|
|
8
6
|
exports.DEFAULT_MAX_PARALLEL_DOWNLOADS = 4;
|
|
@@ -17,6 +15,7 @@ exports.DEFAULT_NOTIFICATION_TEXTS = {
|
|
|
17
15
|
downloadProgress: 'Downloading... {progress}%',
|
|
18
16
|
downloadPaused: 'Paused',
|
|
19
17
|
downloadFinished: 'Download complete',
|
|
18
|
+
downloadCancel: 'Cancel',
|
|
20
19
|
groupTitle: 'Downloads',
|
|
21
20
|
groupText: (count) => `${count} download${count !== 1 ? 's' : ''} in progress`,
|
|
22
21
|
};
|
|
@@ -29,6 +28,8 @@ exports.config = {
|
|
|
29
28
|
maxParallelDownloads: exports.DEFAULT_MAX_PARALLEL_DOWNLOADS,
|
|
30
29
|
allowsCellularAccess: exports.DEFAULT_ALLOWS_CELLULAR_ACCESS,
|
|
31
30
|
showNotificationsEnabled: false,
|
|
31
|
+
showCompletionNotification: false,
|
|
32
|
+
showCancelAction: false,
|
|
32
33
|
iosDataProtection: exports.DEFAULT_IOS_DATA_PROTECTION,
|
|
33
34
|
notificationsGrouping: {
|
|
34
35
|
enabled: false,
|
|
@@ -36,44 +37,3 @@ exports.config = {
|
|
|
36
37
|
texts: exports.DEFAULT_NOTIFICATION_TEXTS,
|
|
37
38
|
},
|
|
38
39
|
};
|
|
39
|
-
/**
|
|
40
|
-
* Get notification text with proper pluralization.
|
|
41
|
-
*/
|
|
42
|
-
function getGroupText(count) {
|
|
43
|
-
var _a, _b;
|
|
44
|
-
const groupText = (_b = (_a = exports.config.notificationsGrouping.texts) === null || _a === void 0 ? void 0 : _a.groupText) !== null && _b !== void 0 ? _b : exports.DEFAULT_NOTIFICATION_TEXTS.groupText;
|
|
45
|
-
if (typeof groupText === 'function')
|
|
46
|
-
return groupText(count);
|
|
47
|
-
return groupText.replace('{count}', String(count));
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Get notification texts config for native side (serializable).
|
|
51
|
-
*/
|
|
52
|
-
function getNotificationTextsForNative() {
|
|
53
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
54
|
-
const texts = (_a = exports.config.notificationsGrouping.texts) !== null && _a !== void 0 ? _a : exports.DEFAULT_NOTIFICATION_TEXTS;
|
|
55
|
-
return {
|
|
56
|
-
downloadTitle: (_b = texts.downloadTitle) !== null && _b !== void 0 ? _b : exports.DEFAULT_NOTIFICATION_TEXTS.downloadTitle,
|
|
57
|
-
downloadStarting: (_c = texts.downloadStarting) !== null && _c !== void 0 ? _c : exports.DEFAULT_NOTIFICATION_TEXTS.downloadStarting,
|
|
58
|
-
downloadProgress: (_d = texts.downloadProgress) !== null && _d !== void 0 ? _d : exports.DEFAULT_NOTIFICATION_TEXTS.downloadProgress,
|
|
59
|
-
downloadPaused: (_e = texts.downloadPaused) !== null && _e !== void 0 ? _e : exports.DEFAULT_NOTIFICATION_TEXTS.downloadPaused,
|
|
60
|
-
downloadFinished: (_f = texts.downloadFinished) !== null && _f !== void 0 ? _f : exports.DEFAULT_NOTIFICATION_TEXTS.downloadFinished,
|
|
61
|
-
groupTitle: (_g = texts.groupTitle) !== null && _g !== void 0 ? _g : exports.DEFAULT_NOTIFICATION_TEXTS.groupTitle,
|
|
62
|
-
// For native side, we send a pattern with {count} placeholder
|
|
63
|
-
groupText: typeof texts.groupText === 'function'
|
|
64
|
-
? '{count} download(s) in progress' // Default pattern if function provided
|
|
65
|
-
: ((_h = texts.groupText) !== null && _h !== void 0 ? _h : '{count} download(s) in progress'),
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
const log = (...args) => {
|
|
69
|
-
if (exports.config.isLogsEnabled) {
|
|
70
|
-
console.log('[RNBackgroundDownloader]', ...args);
|
|
71
|
-
// Call external log callback if provided
|
|
72
|
-
if (exports.config.logCallback) {
|
|
73
|
-
const message = args.length > 0 && typeof args[0] === 'string' ? args[0] : 'log';
|
|
74
|
-
const restArgs = args.length > 1 ? args.slice(1) : [];
|
|
75
|
-
exports.config.logCallback('RNBD', message, ...restArgs);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
exports.log = log;
|
package/lib/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { Spec } from './NativeRNBackgroundDownloader';
|
|
|
7
7
|
* This prevents memory leaks from accumulated event listeners.
|
|
8
8
|
*/
|
|
9
9
|
export declare function cleanup(): void;
|
|
10
|
-
export declare function setConfig({ headers, progressInterval, progressMinBytes, isLogsEnabled, logCallback, maxParallelDownloads, allowsCellularAccess, showNotificationsEnabled, notificationsGrouping, iosDataProtection, }: Config): void;
|
|
10
|
+
export declare function setConfig({ headers, progressInterval, progressMinBytes, isLogsEnabled, logCallback, maxParallelDownloads, allowsCellularAccess, showNotificationsEnabled, showCompletionNotification, showCancelAction, notificationsGrouping, iosDataProtection, }: Config): void;
|
|
11
11
|
export declare const getExistingDownloadTasks: () => Promise<DownloadTask[]>;
|
|
12
12
|
export declare const completeHandler: (jobId: string) => Promise<void> | undefined;
|
|
13
13
|
export declare function createDownloadTask({ isAllowedOverRoaming, isAllowedOverMetered, metadata, ...rest }: TaskInfo & DownloadParams): DownloadTask;
|
package/lib/index.js
CHANGED
|
@@ -10,6 +10,8 @@ const react_native_1 = require("react-native");
|
|
|
10
10
|
const DownloadTask_1 = require("./DownloadTask");
|
|
11
11
|
const UploadTask_1 = require("./UploadTask");
|
|
12
12
|
const config_1 = require("./config");
|
|
13
|
+
const logger_1 = require("./logger");
|
|
14
|
+
const notifications_1 = require("./notifications");
|
|
13
15
|
// Lazy initialization state
|
|
14
16
|
let RNBackgroundDownloader = null;
|
|
15
17
|
let turboModule = null;
|
|
@@ -87,16 +89,16 @@ function initializeEventListeners() {
|
|
|
87
89
|
// iOS new architecture: use EventEmitter from TurboModule spec
|
|
88
90
|
turboModule.onDownloadBegin((data) => {
|
|
89
91
|
const { id, ...rest } = data;
|
|
90
|
-
(0,
|
|
92
|
+
(0, logger_1.log)('downloadBegin', id, rest);
|
|
91
93
|
const task = tasksMap.get(id);
|
|
92
94
|
if (!task) {
|
|
93
|
-
(0,
|
|
95
|
+
(0, logger_1.log)('downloadBegin: task not found in tasksMap', id);
|
|
94
96
|
return;
|
|
95
97
|
}
|
|
96
98
|
task.onBegin(rest);
|
|
97
99
|
});
|
|
98
100
|
turboModule.onDownloadProgress((events) => {
|
|
99
|
-
(0,
|
|
101
|
+
(0, logger_1.log)('downloadProgress', events);
|
|
100
102
|
for (const event of events) {
|
|
101
103
|
const { id, ...rest } = event;
|
|
102
104
|
const task = tasksMap.get(id);
|
|
@@ -106,20 +108,20 @@ function initializeEventListeners() {
|
|
|
106
108
|
});
|
|
107
109
|
turboModule.onDownloadComplete((data) => {
|
|
108
110
|
const { id, ...rest } = data;
|
|
109
|
-
(0,
|
|
111
|
+
(0, logger_1.log)('downloadComplete', id, rest);
|
|
110
112
|
const task = tasksMap.get(id);
|
|
111
113
|
if (!task)
|
|
112
|
-
(0,
|
|
114
|
+
(0, logger_1.log)('downloadComplete: task not found in tasksMap', id);
|
|
113
115
|
else
|
|
114
116
|
task.onDone(rest);
|
|
115
117
|
tasksMap.delete(id);
|
|
116
118
|
});
|
|
117
119
|
turboModule.onDownloadFailed((data) => {
|
|
118
120
|
const { id, ...rest } = data;
|
|
119
|
-
(0,
|
|
121
|
+
(0, logger_1.log)('downloadFailed', id, rest);
|
|
120
122
|
const task = tasksMap.get(id);
|
|
121
123
|
if (!task)
|
|
122
|
-
(0,
|
|
124
|
+
(0, logger_1.log)('downloadFailed: task not found in tasksMap', id);
|
|
123
125
|
else
|
|
124
126
|
task.onError(rest);
|
|
125
127
|
tasksMap.delete(id);
|
|
@@ -128,16 +130,16 @@ function initializeEventListeners() {
|
|
|
128
130
|
if (typeof turboModule.onUploadBegin === 'function') {
|
|
129
131
|
(_a = turboModule.onUploadBegin) === null || _a === void 0 ? void 0 : _a.call(turboModule, (data) => {
|
|
130
132
|
const { id, ...rest } = data;
|
|
131
|
-
(0,
|
|
133
|
+
(0, logger_1.log)('uploadBegin', id, rest);
|
|
132
134
|
const task = uploadTasksMap.get(id);
|
|
133
135
|
if (!task) {
|
|
134
|
-
(0,
|
|
136
|
+
(0, logger_1.log)('uploadBegin: task not found in uploadTasksMap', id);
|
|
135
137
|
return;
|
|
136
138
|
}
|
|
137
139
|
task.onBegin(rest);
|
|
138
140
|
});
|
|
139
141
|
(_b = turboModule.onUploadProgress) === null || _b === void 0 ? void 0 : _b.call(turboModule, (events) => {
|
|
140
|
-
(0,
|
|
142
|
+
(0, logger_1.log)('uploadProgress', events);
|
|
141
143
|
for (const event of events) {
|
|
142
144
|
const { id, ...rest } = event;
|
|
143
145
|
const task = uploadTasksMap.get(id);
|
|
@@ -147,20 +149,20 @@ function initializeEventListeners() {
|
|
|
147
149
|
});
|
|
148
150
|
(_c = turboModule.onUploadComplete) === null || _c === void 0 ? void 0 : _c.call(turboModule, (data) => {
|
|
149
151
|
const { id, ...rest } = data;
|
|
150
|
-
(0,
|
|
152
|
+
(0, logger_1.log)('uploadComplete', id, rest);
|
|
151
153
|
const task = uploadTasksMap.get(id);
|
|
152
154
|
if (!task)
|
|
153
|
-
(0,
|
|
155
|
+
(0, logger_1.log)('uploadComplete: task not found in uploadTasksMap', id);
|
|
154
156
|
else
|
|
155
157
|
task.onDone(rest);
|
|
156
158
|
uploadTasksMap.delete(id);
|
|
157
159
|
});
|
|
158
160
|
(_d = turboModule.onUploadFailed) === null || _d === void 0 ? void 0 : _d.call(turboModule, (data) => {
|
|
159
161
|
const { id, ...rest } = data;
|
|
160
|
-
(0,
|
|
162
|
+
(0, logger_1.log)('uploadFailed', id, rest);
|
|
161
163
|
const task = uploadTasksMap.get(id);
|
|
162
164
|
if (!task)
|
|
163
|
-
(0,
|
|
165
|
+
(0, logger_1.log)('uploadFailed: task not found in uploadTasksMap', id);
|
|
164
166
|
else
|
|
165
167
|
task.onError(rest);
|
|
166
168
|
uploadTasksMap.delete(id);
|
|
@@ -175,16 +177,16 @@ function initializeEventListeners() {
|
|
|
175
177
|
const eventEmitter = new react_native_1.NativeEventEmitter(RNBackgroundDownloader);
|
|
176
178
|
eventSubscriptions.push(eventEmitter.addListener('downloadBegin', (data) => {
|
|
177
179
|
const { id, ...rest } = data;
|
|
178
|
-
(0,
|
|
180
|
+
(0, logger_1.log)('downloadBegin', id, rest);
|
|
179
181
|
const task = tasksMap.get(id);
|
|
180
182
|
if (!task) {
|
|
181
|
-
(0,
|
|
183
|
+
(0, logger_1.log)('downloadBegin: task not found in tasksMap', id);
|
|
182
184
|
return;
|
|
183
185
|
}
|
|
184
186
|
task.onBegin(rest);
|
|
185
187
|
}));
|
|
186
188
|
eventSubscriptions.push(eventEmitter.addListener('downloadProgress', (events) => {
|
|
187
|
-
(0,
|
|
189
|
+
(0, logger_1.log)('downloadProgress', events);
|
|
188
190
|
for (const event of events) {
|
|
189
191
|
const { id, ...rest } = event;
|
|
190
192
|
const task = tasksMap.get(id);
|
|
@@ -194,20 +196,20 @@ function initializeEventListeners() {
|
|
|
194
196
|
}));
|
|
195
197
|
eventSubscriptions.push(eventEmitter.addListener('downloadComplete', (data) => {
|
|
196
198
|
const { id, ...rest } = data;
|
|
197
|
-
(0,
|
|
199
|
+
(0, logger_1.log)('downloadComplete', id, rest);
|
|
198
200
|
const task = tasksMap.get(id);
|
|
199
201
|
if (!task)
|
|
200
|
-
(0,
|
|
202
|
+
(0, logger_1.log)('downloadComplete: task not found in tasksMap', id);
|
|
201
203
|
else
|
|
202
204
|
task.onDone(rest);
|
|
203
205
|
tasksMap.delete(id);
|
|
204
206
|
}));
|
|
205
207
|
eventSubscriptions.push(eventEmitter.addListener('downloadFailed', (data) => {
|
|
206
208
|
const { id, ...rest } = data;
|
|
207
|
-
(0,
|
|
209
|
+
(0, logger_1.log)('downloadFailed', id, rest);
|
|
208
210
|
const task = tasksMap.get(id);
|
|
209
211
|
if (!task)
|
|
210
|
-
(0,
|
|
212
|
+
(0, logger_1.log)('downloadFailed: task not found in tasksMap', id);
|
|
211
213
|
else
|
|
212
214
|
task.onError(rest);
|
|
213
215
|
tasksMap.delete(id);
|
|
@@ -215,16 +217,16 @@ function initializeEventListeners() {
|
|
|
215
217
|
// Upload events for old architecture
|
|
216
218
|
eventSubscriptions.push(eventEmitter.addListener('uploadBegin', (data) => {
|
|
217
219
|
const { id, ...rest } = data;
|
|
218
|
-
(0,
|
|
220
|
+
(0, logger_1.log)('uploadBegin', id, rest);
|
|
219
221
|
const task = uploadTasksMap.get(id);
|
|
220
222
|
if (!task) {
|
|
221
|
-
(0,
|
|
223
|
+
(0, logger_1.log)('uploadBegin: task not found in uploadTasksMap', id);
|
|
222
224
|
return;
|
|
223
225
|
}
|
|
224
226
|
task.onBegin(rest);
|
|
225
227
|
}));
|
|
226
228
|
eventSubscriptions.push(eventEmitter.addListener('uploadProgress', (events) => {
|
|
227
|
-
(0,
|
|
229
|
+
(0, logger_1.log)('uploadProgress', events);
|
|
228
230
|
for (const event of events) {
|
|
229
231
|
const { id, ...rest } = event;
|
|
230
232
|
const task = uploadTasksMap.get(id);
|
|
@@ -234,34 +236,34 @@ function initializeEventListeners() {
|
|
|
234
236
|
}));
|
|
235
237
|
eventSubscriptions.push(eventEmitter.addListener('uploadComplete', (data) => {
|
|
236
238
|
const { id, ...rest } = data;
|
|
237
|
-
(0,
|
|
239
|
+
(0, logger_1.log)('uploadComplete', id, rest);
|
|
238
240
|
const task = uploadTasksMap.get(id);
|
|
239
241
|
if (!task)
|
|
240
|
-
(0,
|
|
242
|
+
(0, logger_1.log)('uploadComplete: task not found in uploadTasksMap', id);
|
|
241
243
|
else
|
|
242
244
|
task.onDone(rest);
|
|
243
245
|
uploadTasksMap.delete(id);
|
|
244
246
|
}));
|
|
245
247
|
eventSubscriptions.push(eventEmitter.addListener('uploadFailed', (data) => {
|
|
246
248
|
const { id, ...rest } = data;
|
|
247
|
-
(0,
|
|
249
|
+
(0, logger_1.log)('uploadFailed', id, rest);
|
|
248
250
|
const task = uploadTasksMap.get(id);
|
|
249
251
|
if (!task)
|
|
250
|
-
(0,
|
|
252
|
+
(0, logger_1.log)('uploadFailed: task not found in uploadTasksMap', id);
|
|
251
253
|
else
|
|
252
254
|
task.onError(rest);
|
|
253
255
|
uploadTasksMap.delete(id);
|
|
254
256
|
}));
|
|
255
257
|
// Native debug log events - forward native iOS logs to JS logCallback
|
|
256
258
|
eventSubscriptions.push(eventEmitter.addListener('nativeDebugLog', (data) => {
|
|
257
|
-
(0,
|
|
259
|
+
(0, logger_1.log)('[Native]', data.taskId || '', data.message);
|
|
258
260
|
}));
|
|
259
261
|
}
|
|
260
262
|
}
|
|
261
263
|
// Event listeners are now initialized lazily when ensureNativeModuleInitialized() is called
|
|
262
264
|
// This ensures the bridge is ready before any native module access
|
|
263
|
-
function setConfig({ headers = {}, progressInterval = config_1.DEFAULT_PROGRESS_INTERVAL, progressMinBytes = config_1.DEFAULT_PROGRESS_MIN_BYTES, isLogsEnabled = false, logCallback, maxParallelDownloads, allowsCellularAccess, showNotificationsEnabled, notificationsGrouping, iosDataProtection, }) {
|
|
264
|
-
var _a, _b, _c;
|
|
265
|
+
function setConfig({ headers = {}, progressInterval = config_1.DEFAULT_PROGRESS_INTERVAL, progressMinBytes = config_1.DEFAULT_PROGRESS_MIN_BYTES, isLogsEnabled = false, logCallback, maxParallelDownloads, allowsCellularAccess, showNotificationsEnabled, showCompletionNotification, showCancelAction, notificationsGrouping, iosDataProtection, }) {
|
|
266
|
+
var _a, _b, _c, _d, _e;
|
|
265
267
|
config_1.config.headers = headers;
|
|
266
268
|
if (iosDataProtection !== undefined)
|
|
267
269
|
config_1.config.iosDataProtection = iosDataProtection;
|
|
@@ -283,6 +285,11 @@ function setConfig({ headers = {}, progressInterval = config_1.DEFAULT_PROGRESS_
|
|
|
283
285
|
// Update showNotificationsEnabled
|
|
284
286
|
if (showNotificationsEnabled !== undefined)
|
|
285
287
|
config_1.config.showNotificationsEnabled = showNotificationsEnabled;
|
|
288
|
+
// Android 14+ notification extras - both opt-in
|
|
289
|
+
if (showCompletionNotification !== undefined)
|
|
290
|
+
config_1.config.showCompletionNotification = showCompletionNotification;
|
|
291
|
+
if (showCancelAction !== undefined)
|
|
292
|
+
config_1.config.showCancelAction = showCancelAction;
|
|
286
293
|
// Update notification grouping config
|
|
287
294
|
if (notificationsGrouping !== undefined)
|
|
288
295
|
config_1.config.notificationsGrouping = {
|
|
@@ -310,8 +317,10 @@ function setConfig({ headers = {}, progressInterval = config_1.DEFAULT_PROGRESS_
|
|
|
310
317
|
nativeModule.setNotificationGroupingConfig({
|
|
311
318
|
enabled: config_1.config.notificationsGrouping.enabled,
|
|
312
319
|
showNotificationsEnabled: (_c = config_1.config.showNotificationsEnabled) !== null && _c !== void 0 ? _c : false,
|
|
320
|
+
showCompletionNotification: (_d = config_1.config.showCompletionNotification) !== null && _d !== void 0 ? _d : false,
|
|
321
|
+
showCancelAction: (_e = config_1.config.showCancelAction) !== null && _e !== void 0 ? _e : false,
|
|
313
322
|
mode: config_1.config.notificationsGrouping.mode,
|
|
314
|
-
texts: (0,
|
|
323
|
+
texts: (0, notifications_1.getNotificationTextsForNative)(),
|
|
315
324
|
});
|
|
316
325
|
}
|
|
317
326
|
catch {
|
|
@@ -376,7 +385,7 @@ const getExistingDownloadTasks = async () => {
|
|
|
376
385
|
exports.getExistingDownloadTasks = getExistingDownloadTasks;
|
|
377
386
|
const completeHandler = (jobId) => {
|
|
378
387
|
if (jobId == null) {
|
|
379
|
-
(0,
|
|
388
|
+
(0, logger_1.log)('completeHandler: jobId is empty');
|
|
380
389
|
return;
|
|
381
390
|
}
|
|
382
391
|
const nativeModule = ensureNativeModuleInitialized();
|
|
@@ -405,7 +414,7 @@ function createDownloadTask({ isAllowedOverRoaming = true, isAllowedOverMetered
|
|
|
405
414
|
const getExistingUploadTasks = async () => {
|
|
406
415
|
const nativeModule = ensureNativeModuleInitialized();
|
|
407
416
|
if (!nativeModule.getExistingUploadTasks) {
|
|
408
|
-
(0,
|
|
417
|
+
(0, logger_1.log)('getExistingUploadTasks: not supported - native implementation missing');
|
|
409
418
|
return [];
|
|
410
419
|
}
|
|
411
420
|
const uploads = await nativeModule.getExistingUploadTasks();
|
package/lib/logger.d.ts
ADDED
package/lib/logger.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.log = void 0;
|
|
4
|
+
const config_1 = require("./config");
|
|
5
|
+
const log = (...args) => {
|
|
6
|
+
if (config_1.config.isLogsEnabled) {
|
|
7
|
+
console.log('[RNBackgroundDownloader]', ...args);
|
|
8
|
+
// Call external log callback if provided
|
|
9
|
+
if (config_1.config.logCallback) {
|
|
10
|
+
const message = args.length > 0 && typeof args[0] === 'string' ? args[0] : 'log';
|
|
11
|
+
const restArgs = args.length > 1 ? args.slice(1) : [];
|
|
12
|
+
config_1.config.logCallback('RNBD', message, ...restArgs);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
exports.log = log;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get notification text with proper pluralization.
|
|
3
|
+
*/
|
|
4
|
+
export declare function getGroupText(count: number): string;
|
|
5
|
+
/**
|
|
6
|
+
* Get notification texts config for native side (serializable).
|
|
7
|
+
*/
|
|
8
|
+
export declare function getNotificationTextsForNative(): Record<string, string>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getGroupText = getGroupText;
|
|
4
|
+
exports.getNotificationTextsForNative = getNotificationTextsForNative;
|
|
5
|
+
const config_1 = require("./config");
|
|
6
|
+
/**
|
|
7
|
+
* Get notification text with proper pluralization.
|
|
8
|
+
*/
|
|
9
|
+
function getGroupText(count) {
|
|
10
|
+
var _a, _b;
|
|
11
|
+
const groupText = (_b = (_a = config_1.config.notificationsGrouping.texts) === null || _a === void 0 ? void 0 : _a.groupText) !== null && _b !== void 0 ? _b : config_1.DEFAULT_NOTIFICATION_TEXTS.groupText;
|
|
12
|
+
if (typeof groupText === 'function')
|
|
13
|
+
return groupText(count);
|
|
14
|
+
return groupText.replace('{count}', String(count));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Get notification texts config for native side (serializable).
|
|
18
|
+
*/
|
|
19
|
+
function getNotificationTextsForNative() {
|
|
20
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
21
|
+
const texts = (_a = config_1.config.notificationsGrouping.texts) !== null && _a !== void 0 ? _a : config_1.DEFAULT_NOTIFICATION_TEXTS;
|
|
22
|
+
return {
|
|
23
|
+
downloadTitle: (_b = texts.downloadTitle) !== null && _b !== void 0 ? _b : config_1.DEFAULT_NOTIFICATION_TEXTS.downloadTitle,
|
|
24
|
+
downloadStarting: (_c = texts.downloadStarting) !== null && _c !== void 0 ? _c : config_1.DEFAULT_NOTIFICATION_TEXTS.downloadStarting,
|
|
25
|
+
downloadProgress: (_d = texts.downloadProgress) !== null && _d !== void 0 ? _d : config_1.DEFAULT_NOTIFICATION_TEXTS.downloadProgress,
|
|
26
|
+
downloadPaused: (_e = texts.downloadPaused) !== null && _e !== void 0 ? _e : config_1.DEFAULT_NOTIFICATION_TEXTS.downloadPaused,
|
|
27
|
+
downloadFinished: (_f = texts.downloadFinished) !== null && _f !== void 0 ? _f : config_1.DEFAULT_NOTIFICATION_TEXTS.downloadFinished,
|
|
28
|
+
downloadCancel: (_g = texts.downloadCancel) !== null && _g !== void 0 ? _g : config_1.DEFAULT_NOTIFICATION_TEXTS.downloadCancel,
|
|
29
|
+
groupTitle: (_h = texts.groupTitle) !== null && _h !== void 0 ? _h : config_1.DEFAULT_NOTIFICATION_TEXTS.groupTitle,
|
|
30
|
+
// For native side, we send a pattern with {count} placeholder
|
|
31
|
+
groupText: typeof texts.groupText === 'function'
|
|
32
|
+
? '{count} download(s) in progress' // Default pattern if function provided
|
|
33
|
+
: ((_j = texts.groupText) !== null && _j !== void 0 ? _j : '{count} download(s) in progress'),
|
|
34
|
+
};
|
|
35
|
+
}
|
package/lib/types.d.ts
CHANGED
|
@@ -18,6 +18,9 @@ export interface NotificationTexts {
|
|
|
18
18
|
downloadPaused?: string;
|
|
19
19
|
/** Text shown when download is finished (default: "Download complete") */
|
|
20
20
|
downloadFinished?: string;
|
|
21
|
+
/** Label of the Cancel action on the progress notification (default: "Cancel").
|
|
22
|
+
* Android 14+ only, and only when `showCancelAction` is enabled */
|
|
23
|
+
downloadCancel?: string;
|
|
21
24
|
/** Title for group summary notification (default: "Downloads") */
|
|
22
25
|
groupTitle?: string;
|
|
23
26
|
/** Text pattern for group summary. Use {count} for number of downloads.
|
|
@@ -63,6 +66,19 @@ export interface Config {
|
|
|
63
66
|
allowsCellularAccess?: boolean;
|
|
64
67
|
/** Show download notifications (Android only, default: false). When false, creates minimal silent notifications (UIDT requires a notification) */
|
|
65
68
|
showNotificationsEnabled?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Post a "download complete" notification when a download finishes
|
|
71
|
+
* (Android 14+ only, default: false). Tapping it opens the saved file.
|
|
72
|
+
* Requires `showNotificationsEnabled`; ignored in `summaryOnly` grouping mode.
|
|
73
|
+
*/
|
|
74
|
+
showCompletionNotification?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Add a Cancel button to the download notification (Android 14+ only, default: false).
|
|
77
|
+
* Tapping it stops the download and fires the task's `.error()` handler with
|
|
78
|
+
* `errorCode = -1`, so only enable it if your app handles that.
|
|
79
|
+
* Requires `showNotificationsEnabled`.
|
|
80
|
+
*/
|
|
81
|
+
showCancelAction?: boolean;
|
|
66
82
|
/** Configuration for notifications grouping on Android */
|
|
67
83
|
notificationsGrouping?: NotificationsGroupingConfig;
|
|
68
84
|
/**
|
|
@@ -119,6 +135,9 @@ export type DownloadParams = {
|
|
|
119
135
|
groupId?: string;
|
|
120
136
|
/** Group name displayed in notification (only used when grouping is enabled) */
|
|
121
137
|
groupName?: string;
|
|
138
|
+
/** Notification title for this download, overriding `groupName` and the
|
|
139
|
+
* configured `downloadTitle` text (Android 14+ only) */
|
|
140
|
+
notificationTitle?: string;
|
|
122
141
|
/**
|
|
123
142
|
* iOS Data Protection level for this download's file (iOS only).
|
|
124
143
|
* Overrides the global `iosDataProtection` from setConfig.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kesha-antonov/react-native-background-downloader",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
4
4
|
"description": "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.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-native",
|
|
@@ -59,7 +59,8 @@
|
|
|
59
59
|
"prepublishOnly": "npm run build-plugin && npm run build && npm run typecheck && jest && npm run lint",
|
|
60
60
|
"build-plugin": "tsc plugin/src/index.ts --outDir plugin/build --target ES2018 --module commonjs --declaration --esModuleInterop --strict --skipLibCheck",
|
|
61
61
|
"release": "npm publish",
|
|
62
|
-
"test": "jest"
|
|
62
|
+
"test": "jest",
|
|
63
|
+
"test:android": "cd example/android && ./gradlew :kesha-antonov_react-native-background-downloader:testDebugUnitTest"
|
|
63
64
|
},
|
|
64
65
|
"lint-staged": {
|
|
65
66
|
"*.js": "eslint --cache"
|
|
@@ -120,5 +121,9 @@
|
|
|
120
121
|
},
|
|
121
122
|
"publishConfig": {
|
|
122
123
|
"access": "public"
|
|
123
|
-
}
|
|
124
|
+
},
|
|
125
|
+
"resolutions": {
|
|
126
|
+
"uuid": "^11.1.1"
|
|
127
|
+
},
|
|
128
|
+
"packageManager": "yarn@4.17.1"
|
|
124
129
|
}
|
package/src/DownloadTask.ts
CHANGED
|
@@ -16,7 +16,8 @@ import {
|
|
|
16
16
|
UnsafeObject,
|
|
17
17
|
Headers,
|
|
18
18
|
} from './types'
|
|
19
|
-
import { config
|
|
19
|
+
import { config } from './config'
|
|
20
|
+
import { log } from './logger'
|
|
20
21
|
|
|
21
22
|
// Import shared native module getter to avoid duplicating TurboModule lookup
|
|
22
23
|
// This is lazily imported to avoid circular dependency issues at module load time
|
|
@@ -92,7 +92,14 @@ export interface Spec extends TurboModule {
|
|
|
92
92
|
setLogsEnabled(enabled: boolean): void
|
|
93
93
|
setMaxParallelDownloads(max: number): void
|
|
94
94
|
setAllowsCellularAccess(allows: boolean): void
|
|
95
|
-
setNotificationGroupingConfig?(config: {
|
|
95
|
+
setNotificationGroupingConfig?(config: {
|
|
96
|
+
enabled: boolean
|
|
97
|
+
showNotificationsEnabled: boolean
|
|
98
|
+
showCompletionNotification: boolean
|
|
99
|
+
showCancelAction: boolean
|
|
100
|
+
mode: string
|
|
101
|
+
texts: UnsafeObject
|
|
102
|
+
}): void
|
|
96
103
|
|
|
97
104
|
getExistingDownloadTasks(): Promise<Array<{
|
|
98
105
|
id: string
|
package/src/UploadTask.ts
CHANGED
|
@@ -16,7 +16,8 @@ import {
|
|
|
16
16
|
UnsafeObject,
|
|
17
17
|
Headers,
|
|
18
18
|
} from './types'
|
|
19
|
-
import { config
|
|
19
|
+
import { config } from './config'
|
|
20
|
+
import { log } from './logger'
|
|
20
21
|
|
|
21
22
|
// Import shared native module getter to avoid duplicating TurboModule lookup
|
|
22
23
|
// This is lazily imported to avoid circular dependency issues at module load time
|
package/src/config.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { Headers, IosDataProtection, NotificationGroupingMode, NotificationsGroupingConfig, NotificationTexts } from './types'
|
|
2
|
+
// Logging lives in ./logger (log, LogCallback); notification-text formatting
|
|
3
|
+
// lives in ./notifications (getGroupText, getNotificationTextsForNative).
|
|
4
|
+
// This module holds only the shared config state and its defaults.
|
|
5
|
+
import type { LogCallback } from './logger'
|
|
2
6
|
|
|
3
7
|
export const DEFAULT_PROGRESS_INTERVAL = 1000
|
|
4
8
|
export const DEFAULT_PROGRESS_MIN_BYTES = 1024 * 1024 // 1MB
|
|
@@ -15,13 +19,11 @@ export const DEFAULT_NOTIFICATION_TEXTS: Required<NotificationTexts> = {
|
|
|
15
19
|
downloadProgress: 'Downloading... {progress}%',
|
|
16
20
|
downloadPaused: 'Paused',
|
|
17
21
|
downloadFinished: 'Download complete',
|
|
22
|
+
downloadCancel: 'Cancel',
|
|
18
23
|
groupTitle: 'Downloads',
|
|
19
24
|
groupText: (count: number) => `${count} download${count !== 1 ? 's' : ''} in progress`,
|
|
20
25
|
}
|
|
21
26
|
|
|
22
|
-
// External log callback for capturing logs in parent app
|
|
23
|
-
type LogCallback = (tag: string, message: string, ...args: unknown[]) => void
|
|
24
|
-
|
|
25
27
|
interface ConfigState {
|
|
26
28
|
headers: Headers
|
|
27
29
|
progressInterval: number
|
|
@@ -31,6 +33,8 @@ interface ConfigState {
|
|
|
31
33
|
maxParallelDownloads: number
|
|
32
34
|
allowsCellularAccess: boolean
|
|
33
35
|
showNotificationsEnabled: boolean
|
|
36
|
+
showCompletionNotification: boolean
|
|
37
|
+
showCancelAction: boolean
|
|
34
38
|
notificationsGrouping: NotificationsGroupingConfig & { mode: NotificationGroupingMode }
|
|
35
39
|
iosDataProtection: IosDataProtection
|
|
36
40
|
}
|
|
@@ -44,6 +48,8 @@ export const config: ConfigState = {
|
|
|
44
48
|
maxParallelDownloads: DEFAULT_MAX_PARALLEL_DOWNLOADS,
|
|
45
49
|
allowsCellularAccess: DEFAULT_ALLOWS_CELLULAR_ACCESS,
|
|
46
50
|
showNotificationsEnabled: false,
|
|
51
|
+
showCompletionNotification: false,
|
|
52
|
+
showCancelAction: false,
|
|
47
53
|
iosDataProtection: DEFAULT_IOS_DATA_PROTECTION,
|
|
48
54
|
notificationsGrouping: {
|
|
49
55
|
enabled: false,
|
|
@@ -51,45 +57,3 @@ export const config: ConfigState = {
|
|
|
51
57
|
texts: DEFAULT_NOTIFICATION_TEXTS,
|
|
52
58
|
},
|
|
53
59
|
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Get notification text with proper pluralization.
|
|
57
|
-
*/
|
|
58
|
-
export function getGroupText (count: number): string {
|
|
59
|
-
const groupText = config.notificationsGrouping.texts?.groupText ?? DEFAULT_NOTIFICATION_TEXTS.groupText
|
|
60
|
-
if (typeof groupText === 'function')
|
|
61
|
-
return groupText(count)
|
|
62
|
-
|
|
63
|
-
return groupText.replace('{count}', String(count))
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Get notification texts config for native side (serializable).
|
|
68
|
-
*/
|
|
69
|
-
export function getNotificationTextsForNative (): Record<string, string> {
|
|
70
|
-
const texts = config.notificationsGrouping.texts ?? DEFAULT_NOTIFICATION_TEXTS
|
|
71
|
-
return {
|
|
72
|
-
downloadTitle: texts.downloadTitle ?? DEFAULT_NOTIFICATION_TEXTS.downloadTitle,
|
|
73
|
-
downloadStarting: texts.downloadStarting ?? DEFAULT_NOTIFICATION_TEXTS.downloadStarting,
|
|
74
|
-
downloadProgress: texts.downloadProgress ?? DEFAULT_NOTIFICATION_TEXTS.downloadProgress,
|
|
75
|
-
downloadPaused: texts.downloadPaused ?? DEFAULT_NOTIFICATION_TEXTS.downloadPaused,
|
|
76
|
-
downloadFinished: texts.downloadFinished ?? DEFAULT_NOTIFICATION_TEXTS.downloadFinished,
|
|
77
|
-
groupTitle: texts.groupTitle ?? DEFAULT_NOTIFICATION_TEXTS.groupTitle,
|
|
78
|
-
// For native side, we send a pattern with {count} placeholder
|
|
79
|
-
groupText: typeof texts.groupText === 'function'
|
|
80
|
-
? '{count} download(s) in progress' // Default pattern if function provided
|
|
81
|
-
: (texts.groupText ?? '{count} download(s) in progress'),
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export const log = (...args: unknown[]): void => {
|
|
86
|
-
if (config.isLogsEnabled) {
|
|
87
|
-
console.log('[RNBackgroundDownloader]', ...args)
|
|
88
|
-
// Call external log callback if provided
|
|
89
|
-
if (config.logCallback) {
|
|
90
|
-
const message = args.length > 0 && typeof args[0] === 'string' ? args[0] : 'log'
|
|
91
|
-
const restArgs = args.length > 1 ? args.slice(1) : []
|
|
92
|
-
config.logCallback('RNBD', message, ...restArgs)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,9 @@ import { NativeModules, Platform, TurboModuleRegistry, NativeEventEmitter, Nativ
|
|
|
2
2
|
import { DownloadTask } from './DownloadTask'
|
|
3
3
|
import { UploadTask } from './UploadTask'
|
|
4
4
|
import { Config, DownloadParams, Headers, Metadata, TaskInfo, TaskInfoNative, UploadParams, UploadTaskInfo, UploadTaskInfoNative } from './types'
|
|
5
|
-
import { config,
|
|
5
|
+
import { config, DEFAULT_PROGRESS_INTERVAL, DEFAULT_PROGRESS_MIN_BYTES, DEFAULT_NOTIFICATION_TEXTS } from './config'
|
|
6
|
+
import { log } from './logger'
|
|
7
|
+
import { getNotificationTextsForNative } from './notifications'
|
|
6
8
|
import type { Spec } from './NativeRNBackgroundDownloader'
|
|
7
9
|
|
|
8
10
|
type RNBackgroundDownloaderModule = Spec & {
|
|
@@ -371,6 +373,8 @@ export function setConfig ({
|
|
|
371
373
|
maxParallelDownloads,
|
|
372
374
|
allowsCellularAccess,
|
|
373
375
|
showNotificationsEnabled,
|
|
376
|
+
showCompletionNotification,
|
|
377
|
+
showCancelAction,
|
|
374
378
|
notificationsGrouping,
|
|
375
379
|
iosDataProtection,
|
|
376
380
|
}: Config) {
|
|
@@ -402,6 +406,13 @@ export function setConfig ({
|
|
|
402
406
|
if (showNotificationsEnabled !== undefined)
|
|
403
407
|
config.showNotificationsEnabled = showNotificationsEnabled
|
|
404
408
|
|
|
409
|
+
// Android 14+ notification extras - both opt-in
|
|
410
|
+
if (showCompletionNotification !== undefined)
|
|
411
|
+
config.showCompletionNotification = showCompletionNotification
|
|
412
|
+
|
|
413
|
+
if (showCancelAction !== undefined)
|
|
414
|
+
config.showCancelAction = showCancelAction
|
|
415
|
+
|
|
405
416
|
// Update notification grouping config
|
|
406
417
|
if (notificationsGrouping !== undefined)
|
|
407
418
|
config.notificationsGrouping = {
|
|
@@ -422,7 +433,14 @@ export function setConfig ({
|
|
|
422
433
|
setLogsEnabled?: (enabled: boolean) => void
|
|
423
434
|
setMaxParallelDownloads?: (max: number) => void
|
|
424
435
|
setAllowsCellularAccess?: (allows: boolean) => void
|
|
425
|
-
setNotificationGroupingConfig?: (config: {
|
|
436
|
+
setNotificationGroupingConfig?: (config: {
|
|
437
|
+
enabled: boolean
|
|
438
|
+
showNotificationsEnabled: boolean
|
|
439
|
+
showCompletionNotification: boolean
|
|
440
|
+
showCancelAction: boolean
|
|
441
|
+
mode: string
|
|
442
|
+
texts: Record<string, string>
|
|
443
|
+
}) => void
|
|
426
444
|
}
|
|
427
445
|
if (nativeModule.setLogsEnabled)
|
|
428
446
|
nativeModule.setLogsEnabled(isLogsEnabled)
|
|
@@ -436,6 +454,8 @@ export function setConfig ({
|
|
|
436
454
|
nativeModule.setNotificationGroupingConfig({
|
|
437
455
|
enabled: config.notificationsGrouping.enabled,
|
|
438
456
|
showNotificationsEnabled: config.showNotificationsEnabled ?? false,
|
|
457
|
+
showCompletionNotification: config.showCompletionNotification ?? false,
|
|
458
|
+
showCancelAction: config.showCancelAction ?? false,
|
|
439
459
|
mode: config.notificationsGrouping.mode,
|
|
440
460
|
texts: getNotificationTextsForNative(),
|
|
441
461
|
})
|