@kesha-antonov/react-native-background-downloader 4.6.3 → 4.7.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 +29 -7
- package/android/src/main/java/com/eko/DownloadConstants.kt +13 -2
- package/android/src/main/java/com/eko/DownloadResult.kt +3 -1
- package/android/src/main/java/com/eko/Downloader.kt +25 -4
- package/android/src/main/java/com/eko/ProgressReporter.kt +14 -35
- package/android/src/main/java/com/eko/RNBackgroundDownloaderModuleImpl.kt +162 -76
- package/android/src/main/java/com/eko/ResumableDownloadService.kt +2 -1
- package/android/src/main/java/com/eko/ResumableDownloader.kt +75 -3
- package/android/src/main/java/com/eko/UIDTDownloadJobService.kt +34 -8
- package/android/src/main/java/com/eko/Uploader.kt +15 -11
- package/android/src/main/java/com/eko/handlers/OnBegin.kt +6 -1
- package/android/src/main/java/com/eko/uidt/CancelDownloadReceiver.kt +3 -1
- package/android/src/main/java/com/eko/uidt/UIDTJobManager.kt +18 -2
- package/android/src/main/java/com/eko/utils/RedirectResolver.kt +5 -13
- package/android/src/main/java/com/eko/utils/StorageManager.kt +5 -0
- package/android/src/test/java/com/eko/DownloaderRecoveryTest.kt +78 -0
- package/android/src/test/java/com/eko/DownloaderResumeTest.kt +97 -0
- package/android/src/test/java/com/eko/ResumableDownloaderStallTest.kt +145 -0
- package/android/src/test/java/com/eko/UploaderMultipartTest.kt +84 -0
- package/android/src/test/java/com/eko/handlers/OnBeginTest.kt +51 -0
- package/android/src/test/java/com/eko/uidt/CancelDownloadReceiverTest.kt +84 -0
- package/android/src/test/java/com/eko/utils/RedirectResolverTest.kt +67 -0
- package/ios/RNBGDTaskConfig.h +2 -0
- package/ios/RNBGDTaskConfig.mm +6 -0
- package/ios/RNBackgroundDownloader.mm +640 -90
- package/lib/DownloadTask.d.ts +2 -0
- package/lib/DownloadTask.js +21 -1
- package/lib/UploadTask.d.ts +2 -0
- package/lib/UploadTask.js +25 -3
- package/lib/index.d.ts +1 -1
- package/lib/index.js +83 -70
- package/lib/tasks.d.ts +4 -0
- package/lib/tasks.js +7 -0
- package/package.json +14 -11
- package/plugin/build/index.d.ts +1 -0
- package/plugin/build/index.js +18 -10
- package/react-native-background-downloader.podspec +8 -0
- package/src/DownloadTask.ts +22 -1
- package/src/UploadTask.ts +25 -3
- package/src/index.ts +74 -43
- package/src/tasks.ts +7 -0
|
@@ -9,10 +9,10 @@ import android.media.MediaScannerConnection
|
|
|
9
9
|
import android.net.Uri
|
|
10
10
|
import android.os.Build
|
|
11
11
|
import android.util.Log
|
|
12
|
-
import android.webkit.MimeTypeMap
|
|
13
12
|
import com.eko.handlers.OnBegin
|
|
14
13
|
import com.eko.handlers.OnProgress
|
|
15
14
|
import com.eko.handlers.OnProgressState
|
|
15
|
+
import com.eko.interfaces.BeginCallback
|
|
16
16
|
import com.eko.utils.HeaderUtils
|
|
17
17
|
import com.eko.utils.ReadableConverters
|
|
18
18
|
import com.eko.utils.RedirectResolver
|
|
@@ -154,6 +154,11 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
154
154
|
return configIdToIsAllowedOverMetered[configId] ?: persistedValue ?: true
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
+
// DownloadManager ids whose end was already reported. A failure is broadcast twice (by the
|
|
158
|
+
// progress loop and by DownloadManager), and the task is only cleaned up after the media
|
|
159
|
+
// scanner, so the second broadcast would report it again.
|
|
160
|
+
private val endedDownloadIds = mutableSetOf<Long>()
|
|
161
|
+
|
|
157
162
|
// Last time (ms) a recovery snapshot was persisted for a resumable download,
|
|
158
163
|
// used to throttle snapshot writes from the frequent progress callbacks.
|
|
159
164
|
private val configIdToLastSnapshotMs = mutableMapOf<String, Long>()
|
|
@@ -216,6 +221,11 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
216
221
|
headersMap.putString(key, value)
|
|
217
222
|
}
|
|
218
223
|
onBeginDownload(id, headersMap, expectedBytes)
|
|
224
|
+
// Save the size now: the first progress snapshot comes only after the throttle
|
|
225
|
+
// interval, and a download recovered after a force-stop before it had no total
|
|
226
|
+
if (expectedBytes > 0) {
|
|
227
|
+
maybeSnapshotActiveResumable(id, 0, expectedBytes, force = true)
|
|
228
|
+
}
|
|
219
229
|
}
|
|
220
230
|
|
|
221
231
|
override fun onProgress(id: String, bytesDownloaded: Long, bytesTotal: Long) {
|
|
@@ -399,6 +409,10 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
399
409
|
val config = downloadIdToConfig[downloadId]
|
|
400
410
|
|
|
401
411
|
if (config != null) {
|
|
412
|
+
if (!synchronized(sharedLock) { endedDownloadIds.add(downloadId) }) {
|
|
413
|
+
logD(NAME, "Ignoring repeated broadcast for download: $downloadId")
|
|
414
|
+
return
|
|
415
|
+
}
|
|
402
416
|
val downloadStatus = downloader.checkDownloadStatus(downloadId)
|
|
403
417
|
val status = downloadStatus.getInt("status")
|
|
404
418
|
val localUri = downloadStatus.getString("localUri")
|
|
@@ -423,11 +437,19 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
423
437
|
// Download successful, clean task after media scanning.
|
|
424
438
|
val paths = arrayOf(localUri)
|
|
425
439
|
MediaScannerConnection.scanFile(context, paths, null) { _, _ ->
|
|
426
|
-
|
|
440
|
+
if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
|
441
|
+
// Not stopTask: DownloadManager.remove() deletes the downloaded file too
|
|
442
|
+
releaseCompletedTask(config.id)
|
|
443
|
+
} else {
|
|
444
|
+
stopTask(config.id)
|
|
445
|
+
}
|
|
446
|
+
// Cleaned up: a later broadcast finds no config
|
|
447
|
+
synchronized(sharedLock) { endedDownloadIds.remove(downloadId) }
|
|
427
448
|
}
|
|
428
449
|
} else {
|
|
429
450
|
// Download failed, clean task.
|
|
430
451
|
stopTask(config.id)
|
|
452
|
+
endedDownloadIds.remove(downloadId)
|
|
431
453
|
}
|
|
432
454
|
}
|
|
433
455
|
}
|
|
@@ -469,6 +491,10 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
469
491
|
}
|
|
470
492
|
}
|
|
471
493
|
|
|
494
|
+
// False once the download was paused, stopped or finished: all of them remove it
|
|
495
|
+
private fun isTracked(downloadId: Long, config: RNBGDTaskConfig): Boolean =
|
|
496
|
+
synchronized(sharedLock) { downloadIdToConfig[downloadId] === config }
|
|
497
|
+
|
|
472
498
|
private fun resumeTasks(downloadId: Long, config: RNBGDTaskConfig) {
|
|
473
499
|
Thread {
|
|
474
500
|
try {
|
|
@@ -476,14 +502,28 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
476
502
|
var bytesTotal: Long = 0
|
|
477
503
|
|
|
478
504
|
if (!config.reportedBegin) {
|
|
479
|
-
val
|
|
480
|
-
|
|
481
|
-
val
|
|
482
|
-
|
|
505
|
+
val requestHeaders = synchronized(sharedLock) { configIdToHeaders[config.id] } ?: emptyMap()
|
|
506
|
+
// The HEAD request can take a while; a download that ended meanwhile gets no begin
|
|
507
|
+
val onBegin = BeginCallback { configId, headers, expectedBytes ->
|
|
508
|
+
if (isTracked(downloadId, config)) onBeginDownload(configId, headers, expectedBytes)
|
|
509
|
+
}
|
|
510
|
+
val onBeginCallable = OnBegin(config, onBegin, requestHeaders)
|
|
511
|
+
try {
|
|
512
|
+
bytesTotal = cachedExecutorPool.submit(onBeginCallable).get().expectedBytes
|
|
513
|
+
} catch (e: Exception) {
|
|
514
|
+
// The HEAD request is only for the size; some servers reject it (e.g. presigned
|
|
515
|
+
// GET URLs). Report begin with an unknown size and still track progress.
|
|
516
|
+
logW(NAME, "resumeTasks: HEAD request failed for ${config.id}: ${e.message}")
|
|
517
|
+
bytesTotal = -1
|
|
518
|
+
onBegin.onBegin(config.id, Arguments.createMap(), -1)
|
|
519
|
+
}
|
|
483
520
|
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
521
|
+
synchronized(sharedLock) {
|
|
522
|
+
// Writing it back would list a stopped or finished download again on every launch
|
|
523
|
+
if (downloadIdToConfig[downloadId] !== config) return@Thread
|
|
524
|
+
config.reportedBegin = true
|
|
525
|
+
saveDownloadIdToConfigMap()
|
|
526
|
+
}
|
|
487
527
|
}
|
|
488
528
|
|
|
489
529
|
val onProgressCallable = OnProgress(
|
|
@@ -494,8 +534,10 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
494
534
|
bytesTotal,
|
|
495
535
|
this::onProgressDownload
|
|
496
536
|
)
|
|
497
|
-
|
|
498
|
-
|
|
537
|
+
synchronized(sharedLock) {
|
|
538
|
+
if (downloadIdToConfig[downloadId] !== config) return@Thread
|
|
539
|
+
configIdToProgressFuture[config.id] = cachedExecutorPool.submit(onProgressCallable)
|
|
540
|
+
}
|
|
499
541
|
} catch (e: Exception) {
|
|
500
542
|
logE(NAME, "resumeTasks: ${Log.getStackTraceString(e)}")
|
|
501
543
|
}
|
|
@@ -554,45 +596,49 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
554
596
|
* If the app is force-stopped, restoreRecoverableDownloads() uses this snapshot to
|
|
555
597
|
* surface the download again via getExistingDownloadTasks.
|
|
556
598
|
*/
|
|
557
|
-
private fun maybeSnapshotActiveResumable(configId: String, bytesDownloaded: Long, bytesTotal: Long) {
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
599
|
+
private fun maybeSnapshotActiveResumable(configId: String, bytesDownloaded: Long, bytesTotal: Long, force: Boolean = false) {
|
|
600
|
+
// Runs on download threads: the maps and the load-modify-save of the snapshot
|
|
601
|
+
// store must not interleave with another download's save or a cleanup
|
|
602
|
+
synchronized(sharedLock) {
|
|
603
|
+
val now = System.currentTimeMillis()
|
|
604
|
+
val last = configIdToLastSnapshotMs[configId] ?: 0L
|
|
605
|
+
if (!force && now - last < DownloadConstants.RECOVERY_SNAPSHOT_INTERVAL_MS) {
|
|
606
|
+
return
|
|
607
|
+
}
|
|
608
|
+
configIdToLastSnapshotMs[configId] = now
|
|
609
|
+
|
|
610
|
+
// The active state lives in the foreground-service ResumableDownloader (Android < 14)
|
|
611
|
+
// or, on Android 14+, in the UIDT job. Check both so force-stop recovery works on
|
|
612
|
+
// every supported version.
|
|
613
|
+
val state = downloader.getActiveResumableDownloads()[configId]
|
|
614
|
+
?: (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
|
|
615
|
+
UIDTDownloadJobService.getJobDownloadState(configId)
|
|
616
|
+
else null)
|
|
617
|
+
?: return
|
|
618
|
+
val headers = configIdToHeaders[configId] ?: state.headers
|
|
619
|
+
val metadata = configIdToMetadata[configId] ?: "{}"
|
|
620
|
+
// Persisted fallback: the UIDT job extras survive process death for jobs the
|
|
621
|
+
// JobScheduler restarted in a fresh process; the download state carries the
|
|
622
|
+
// flag on the foreground-service path.
|
|
623
|
+
val persistedIsAllowedOverMetered =
|
|
624
|
+
(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
|
|
625
|
+
UIDTDownloadJobService.getJobIsAllowedOverMetered(configId)
|
|
626
|
+
else null)
|
|
627
|
+
?: state.isAllowedOverMetered
|
|
628
|
+
|
|
629
|
+
downloader.saveActiveDownloadSnapshot(
|
|
630
|
+
Downloader.PausedDownloadInfo(
|
|
631
|
+
configId = configId,
|
|
632
|
+
url = state.url,
|
|
633
|
+
destination = state.destination,
|
|
634
|
+
headers = headers,
|
|
635
|
+
bytesDownloaded = bytesDownloaded,
|
|
636
|
+
bytesTotal = bytesTotal,
|
|
637
|
+
metadata = metadata,
|
|
638
|
+
isAllowedOverMetered = resolveIsAllowedOverMetered(configId, persistedIsAllowedOverMetered)
|
|
639
|
+
)
|
|
594
640
|
)
|
|
595
|
-
|
|
641
|
+
}
|
|
596
642
|
}
|
|
597
643
|
|
|
598
644
|
fun download(options: ReadableMap) {
|
|
@@ -629,10 +675,11 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
629
675
|
} else null
|
|
630
676
|
|
|
631
677
|
val progressIntervalScope = options.getInt("progressInterval")
|
|
632
|
-
|
|
633
|
-
|
|
678
|
+
// 0 is a valid value (percentage only), so it has to be applied too
|
|
679
|
+
val progressMinBytesScope = if (options.hasKey("progressMinBytes")) options.getDouble("progressMinBytes").toLong() else -1L
|
|
680
|
+
if (progressIntervalScope > 0 || progressMinBytesScope >= 0) {
|
|
634
681
|
val newInterval = if (progressIntervalScope > 0) progressIntervalScope.toLong() else progressReporter.getProgressInterval()
|
|
635
|
-
val newMinBytes = if (progressMinBytesScope
|
|
682
|
+
val newMinBytes = if (progressMinBytesScope >= 0) progressMinBytesScope else progressReporter.getProgressMinBytes()
|
|
636
683
|
progressReporter.configure(newInterval, newMinBytes)
|
|
637
684
|
// Sync notification update interval with progress interval for Android 14+
|
|
638
685
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
|
@@ -698,10 +745,6 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
698
745
|
// DownloadManager requires external storage for downloads
|
|
699
746
|
// Use app's private external files directory: /storage/emulated/0/Android/data/<package>/files/
|
|
700
747
|
// This keeps files private to the app while being compatible with DownloadManager
|
|
701
|
-
val uuid = (System.currentTimeMillis() and 0xfffffff).toInt()
|
|
702
|
-
val extension = MimeTypeMap.getFileExtensionFromUrl(destination)
|
|
703
|
-
val filename = "$uuid.$extension"
|
|
704
|
-
|
|
705
748
|
// Get external files directory and validate it's a proper external storage path
|
|
706
749
|
val externalFilesDir = reactContext.getExternalFilesDir(null)
|
|
707
750
|
val isValidExternalPath = externalFilesDir != null &&
|
|
@@ -818,22 +861,13 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
818
861
|
startWithResumableDownloader()
|
|
819
862
|
}
|
|
820
863
|
} else {
|
|
821
|
-
// External files directory path is invalid or null
|
|
822
|
-
//
|
|
864
|
+
// External files directory path is invalid or null (e.g. OnePlus devices returning
|
|
865
|
+
// /data/local/tmp/external/). DownloadManager could only save under its own name in
|
|
866
|
+
// the external files dir, not at the destination, so the completed file wouldn't be
|
|
867
|
+
// where the task expects it. ResumableDownloader writes to the destination directly.
|
|
823
868
|
logW(NAME, "External files directory path may be invalid for DownloadManager: ${externalFilesDir?.absolutePath}")
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
// Try standard setDestinationInExternalFilesDir - may work on some devices
|
|
827
|
-
request.setDestinationInExternalFilesDir(reactContext, null, filename)
|
|
828
|
-
startDownloadManagerDownload(downloader.download(request))
|
|
829
|
-
logD(NAME, "Using setDestinationInExternalFilesDir for download: $id")
|
|
830
|
-
} catch (e: Exception) {
|
|
831
|
-
// DownloadManager failed - fall back to ResumableDownloader
|
|
832
|
-
// This handles OnePlus and other devices that return paths like /data/local/tmp/external/
|
|
833
|
-
logW(NAME, "DownloadManager failed with: ${e.message}")
|
|
834
|
-
logD(NAME, "Using ResumableDownloader as fallback for download: $id")
|
|
835
|
-
startWithResumableDownloader()
|
|
836
|
-
}
|
|
869
|
+
logD(NAME, "Using ResumableDownloader for download: $id")
|
|
870
|
+
startWithResumableDownloader()
|
|
837
871
|
}
|
|
838
872
|
}
|
|
839
873
|
|
|
@@ -841,6 +875,8 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
841
875
|
// If it's already using ResumableDownloader, it pauses the HTTP connection.
|
|
842
876
|
fun pauseTask(configId: String) {
|
|
843
877
|
synchronized(sharedLock) {
|
|
878
|
+
// Progress buffered before the pause would otherwise go out with another task's batch
|
|
879
|
+
progressReporter.clearPendingReport(configId)
|
|
844
880
|
// First check if it's an active resumable download
|
|
845
881
|
if (downloader.isResumableDownload(configId)) {
|
|
846
882
|
// Get state before pausing - check both UIDT jobs and regular service
|
|
@@ -999,6 +1035,16 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
999
1035
|
}
|
|
1000
1036
|
}
|
|
1001
1037
|
|
|
1038
|
+
/**
|
|
1039
|
+
* Drop the bookkeeping for a download that finished, keeping its file.
|
|
1040
|
+
*/
|
|
1041
|
+
private fun releaseCompletedTask(configId: String) {
|
|
1042
|
+
synchronized(sharedLock) {
|
|
1043
|
+
stopTaskProgress(configId)
|
|
1044
|
+
cleanupDownloadState(configId, configIdToDownloadId[configId], removePausedState = false)
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1002
1048
|
fun completeHandler(configId: String) {
|
|
1003
1049
|
// Firebase Performance compatibility: Add defensive programming to prevent crashes
|
|
1004
1050
|
// when Firebase Performance SDK is installed and uses bytecode instrumentation
|
|
@@ -1096,6 +1142,13 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
1096
1142
|
processedIds.add(config.id)
|
|
1097
1143
|
configIdToDownloadId[config.id] = downloadId
|
|
1098
1144
|
progressReporter.setPercent(config.id, percent)
|
|
1145
|
+
|
|
1146
|
+
if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
|
1147
|
+
// Finished while the app wasn't running, so no event was sent; it's
|
|
1148
|
+
// reported here as done once, then forgotten so it isn't listed again
|
|
1149
|
+
// on every launch (and a later stop can't remove its file)
|
|
1150
|
+
releaseCompletedTask(config.id)
|
|
1151
|
+
}
|
|
1099
1152
|
}
|
|
1100
1153
|
} else if (downloadId != null) {
|
|
1101
1154
|
downloader.cancel(downloadId)
|
|
@@ -1165,6 +1218,25 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
1165
1218
|
processedIds.add(configId)
|
|
1166
1219
|
}
|
|
1167
1220
|
|
|
1221
|
+
// Phase 3b: Downloads that finished while the app wasn't running (Android 14+ jobs
|
|
1222
|
+
// restarted in a process without the app). Reported once, as completed
|
|
1223
|
+
for ((configId, info) in downloader.takeFinishedDownloads()) {
|
|
1224
|
+
if (processedIds.contains(configId)) {
|
|
1225
|
+
continue
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
val params = Arguments.createMap()
|
|
1229
|
+
params.putString("id", configId)
|
|
1230
|
+
params.putString("metadata", info.metadata)
|
|
1231
|
+
params.putInt("state", DownloadConstants.TASK_COMPLETED)
|
|
1232
|
+
params.putDouble("bytesDownloaded", info.bytesDownloaded.toDouble())
|
|
1233
|
+
params.putDouble("bytesTotal", info.bytesDownloaded.toDouble())
|
|
1234
|
+
params.putString("destination", info.destination)
|
|
1235
|
+
|
|
1236
|
+
foundTasks.pushMap(params)
|
|
1237
|
+
processedIds.add(configId)
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1168
1240
|
// Phase 4: Add active resumable downloads (in-progress via ResumableDownloader).
|
|
1169
1241
|
// These run in the foreground service (Android < 14) and are invisible to
|
|
1170
1242
|
// DownloadManager, so without this phase getExistingDownloadTasks misses them
|
|
@@ -1352,6 +1424,8 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
1352
1424
|
synchronized(sharedLock) {
|
|
1353
1425
|
uploadConfigs[id]?.state = DownloadConstants.TASK_COMPLETED
|
|
1354
1426
|
uploadConfigs.remove(id)
|
|
1427
|
+
// Progress still buffered must not arrive after uploadComplete
|
|
1428
|
+
uploadProgressReporter.clearDownloadState(id)
|
|
1355
1429
|
// Remove from persistent storage on completion
|
|
1356
1430
|
storageManager.saveUploadConfigs(uploadConfigs)
|
|
1357
1431
|
}
|
|
@@ -1362,6 +1436,7 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
1362
1436
|
synchronized(sharedLock) {
|
|
1363
1437
|
uploadConfigs[id]?.state = DownloadConstants.TASK_CANCELING
|
|
1364
1438
|
uploadConfigs.remove(id)
|
|
1439
|
+
uploadProgressReporter.clearDownloadState(id)
|
|
1365
1440
|
// Remove from persistent storage on error
|
|
1366
1441
|
storageManager.saveUploadConfigs(uploadConfigs)
|
|
1367
1442
|
}
|
|
@@ -1422,10 +1497,11 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
1422
1497
|
|
|
1423
1498
|
// Progress settings
|
|
1424
1499
|
val progressIntervalScope = options.getInt("progressInterval")
|
|
1425
|
-
|
|
1426
|
-
|
|
1500
|
+
// 0 is a valid value (percentage only), so it has to be applied too
|
|
1501
|
+
val progressMinBytesScope = if (options.hasKey("progressMinBytes")) options.getDouble("progressMinBytes").toLong() else -1L
|
|
1502
|
+
if (progressIntervalScope > 0 || progressMinBytesScope >= 0) {
|
|
1427
1503
|
val newInterval = if (progressIntervalScope > 0) progressIntervalScope.toLong() else uploadProgressReporter.getProgressInterval()
|
|
1428
|
-
val newMinBytes = if (progressMinBytesScope
|
|
1504
|
+
val newMinBytes = if (progressMinBytesScope >= 0) progressMinBytesScope else uploadProgressReporter.getProgressMinBytes()
|
|
1429
1505
|
uploadProgressReporter.configure(newInterval, newMinBytes)
|
|
1430
1506
|
}
|
|
1431
1507
|
|
|
@@ -1459,6 +1535,7 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
1459
1535
|
fun pauseUploadTask(configId: String) {
|
|
1460
1536
|
synchronized(sharedLock) {
|
|
1461
1537
|
uploadConfigs[configId]?.state = DownloadConstants.TASK_SUSPENDED
|
|
1538
|
+
uploadProgressReporter.clearPendingReport(configId)
|
|
1462
1539
|
// Persist state change
|
|
1463
1540
|
storageManager.saveUploadConfigs(uploadConfigs)
|
|
1464
1541
|
}
|
|
@@ -1467,12 +1544,21 @@ class RNBackgroundDownloaderModuleImpl(private val reactContext: ReactApplicatio
|
|
|
1467
1544
|
}
|
|
1468
1545
|
|
|
1469
1546
|
fun resumeUploadTask(configId: String) {
|
|
1470
|
-
synchronized(sharedLock) {
|
|
1547
|
+
val config = synchronized(sharedLock) {
|
|
1471
1548
|
uploadConfigs[configId]?.state = DownloadConstants.TASK_RUNNING
|
|
1472
1549
|
// Persist state change
|
|
1473
1550
|
storageManager.saveUploadConfigs(uploadConfigs)
|
|
1551
|
+
// A resumed upload starts over, so progress thresholds start from 0 too
|
|
1552
|
+
uploadProgressReporter.clearDownloadState(configId)
|
|
1553
|
+
uploadProgressReporter.initializeDownload(configId)
|
|
1554
|
+
uploadConfigs[configId]
|
|
1555
|
+
}
|
|
1556
|
+
if (!uploader.resume(configId, uploadListener) && config != null && uploader.getState(configId) == null) {
|
|
1557
|
+
// Restored after a restart: nothing is in memory to resume, so upload it again
|
|
1558
|
+
uploader.startUpload(config, uploadListener)
|
|
1559
|
+
logD(NAME, "Restarted restored upload: $configId")
|
|
1560
|
+
return
|
|
1474
1561
|
}
|
|
1475
|
-
uploader.resume(configId, uploadListener)
|
|
1476
1562
|
logD(NAME, "Resumed upload: $configId")
|
|
1477
1563
|
}
|
|
1478
1564
|
|
|
@@ -504,7 +504,8 @@ class ResumableDownloadService : Service() {
|
|
|
504
504
|
}
|
|
505
505
|
|
|
506
506
|
private fun acquireWakeLock() {
|
|
507
|
-
|
|
507
|
+
// Not just a null check: the lock times out, and an expired one must be taken again
|
|
508
|
+
if (wakeLock?.isHeld != true) {
|
|
508
509
|
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
|
|
509
510
|
wakeLock = powerManager.newWakeLock(
|
|
510
511
|
PowerManager.PARTIAL_WAKE_LOCK,
|
|
@@ -17,6 +17,11 @@ import java.util.concurrent.atomic.AtomicLong
|
|
|
17
17
|
*/
|
|
18
18
|
class ResumableDownloader {
|
|
19
19
|
|
|
20
|
+
// How long a transfer may stall before it's retried, and the waits before each retry;
|
|
21
|
+
// shorter in tests
|
|
22
|
+
internal var readTimeoutMs = DownloadConstants.READ_TIMEOUT_MS
|
|
23
|
+
internal var networkRetryDelaysMs = DownloadConstants.NETWORK_RETRY_DELAYS_MS
|
|
24
|
+
|
|
20
25
|
companion object {
|
|
21
26
|
private const val TAG = "ResumableDownloader"
|
|
22
27
|
|
|
@@ -390,7 +395,31 @@ class ResumableDownloader {
|
|
|
390
395
|
fun getBytesTotal(id: String): Long = activeDownloads[id]?.bytesTotal ?: -1
|
|
391
396
|
|
|
392
397
|
private fun downloadWithResume(state: DownloadState, listener: DownloadListener, expectedSessionId: Long) {
|
|
393
|
-
|
|
398
|
+
var result = executeDownload(state, listener, expectedSessionId)
|
|
399
|
+
|
|
400
|
+
// A transfer the network cut off continues from the partial file after a short wait,
|
|
401
|
+
// as DownloadManager and iOS do, instead of failing the download
|
|
402
|
+
var retries = 0
|
|
403
|
+
while (result is DownloadResult.Error && result.isNetworkFailure) {
|
|
404
|
+
// A redirect replaces the state
|
|
405
|
+
val current = activeDownloads[state.id] ?: state
|
|
406
|
+
val bytesBefore = current.bytesDownloaded.get()
|
|
407
|
+
if (retries >= networkRetryDelaysMs.size) {
|
|
408
|
+
RNBackgroundDownloaderModuleImpl.logE(TAG, "Download error after $retries retries: ${result.message}")
|
|
409
|
+
listener.onError(result.id, result.message, result.errorCode)
|
|
410
|
+
return
|
|
411
|
+
}
|
|
412
|
+
RNBackgroundDownloaderModuleImpl.logW(TAG, "Network failure for ${state.id} (${result.message}), retry ${retries + 1} at $bytesBefore bytes")
|
|
413
|
+
if (!waitBeforeRetry(current, expectedSessionId, networkRetryDelaysMs[retries])) {
|
|
414
|
+
RNBackgroundDownloaderModuleImpl.logD(TAG, "Retry abandoned, download paused or cancelled: ${state.id}")
|
|
415
|
+
return
|
|
416
|
+
}
|
|
417
|
+
// The file holds exactly what was received; continue from there
|
|
418
|
+
current.bytesDownloaded.set(File(current.destination).let { if (it.exists()) it.length() else 0L })
|
|
419
|
+
result = executeDownload(current, listener, expectedSessionId)
|
|
420
|
+
val progressed = (activeDownloads[state.id] ?: current).bytesDownloaded.get() > bytesBefore
|
|
421
|
+
retries = if (progressed) 0 else retries + 1
|
|
422
|
+
}
|
|
394
423
|
|
|
395
424
|
// Handle the result by notifying the listener
|
|
396
425
|
when (result) {
|
|
@@ -415,6 +444,30 @@ class ResumableDownloader {
|
|
|
415
444
|
}
|
|
416
445
|
}
|
|
417
446
|
|
|
447
|
+
// False if the download was paused, cancelled or restarted meanwhile
|
|
448
|
+
private fun waitBeforeRetry(state: DownloadState, expectedSessionId: Long, delayMs: Long): Boolean {
|
|
449
|
+
val deadline = System.currentTimeMillis() + delayMs
|
|
450
|
+
while (true) {
|
|
451
|
+
if (state.sessionId.get() != expectedSessionId || state.isCancelled.get() || state.isPaused.get()) {
|
|
452
|
+
return false
|
|
453
|
+
}
|
|
454
|
+
val remaining = deadline - System.currentTimeMillis()
|
|
455
|
+
if (remaining <= 0) return true
|
|
456
|
+
try {
|
|
457
|
+
Thread.sleep(minOf(remaining, 200L))
|
|
458
|
+
} catch (e: InterruptedException) {
|
|
459
|
+
return false
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
private fun isNetworkFailure(e: Exception): Boolean =
|
|
465
|
+
e is java.net.SocketException ||
|
|
466
|
+
e is java.net.UnknownHostException ||
|
|
467
|
+
e is java.io.EOFException ||
|
|
468
|
+
// "unexpected end of stream" when the connection drops mid-body
|
|
469
|
+
e is java.net.ProtocolException
|
|
470
|
+
|
|
418
471
|
/**
|
|
419
472
|
* Execute the download and return a DownloadResult.
|
|
420
473
|
* This method handles all download logic and returns a type-safe result.
|
|
@@ -443,7 +496,7 @@ class ResumableDownloader {
|
|
|
443
496
|
// Store connection reference so it can be disconnected on cancel
|
|
444
497
|
state.connection = connection
|
|
445
498
|
connection.connectTimeout = DownloadConstants.CONNECT_TIMEOUT_MS
|
|
446
|
-
connection.readTimeout =
|
|
499
|
+
connection.readTimeout = readTimeoutMs
|
|
447
500
|
connection.requestMethod = "GET"
|
|
448
501
|
|
|
449
502
|
// Check again after connection setup
|
|
@@ -640,6 +693,16 @@ class ResumableDownloader {
|
|
|
640
693
|
val bytesDownloaded = state.bytesDownloaded.get()
|
|
641
694
|
val bytesTotal = state.bytesTotal
|
|
642
695
|
|
|
696
|
+
// The connection closed before the whole body arrived (some servers and proxies end
|
|
697
|
+
// it without an error): not done, the rest has to be fetched
|
|
698
|
+
if (bytesTotal > 0 && bytesDownloaded < bytesTotal) {
|
|
699
|
+
return DownloadResult.Error(
|
|
700
|
+
state.id,
|
|
701
|
+
"Connection closed at $bytesDownloaded of $bytesTotal bytes",
|
|
702
|
+
isNetworkFailure = true
|
|
703
|
+
)
|
|
704
|
+
}
|
|
705
|
+
|
|
643
706
|
activeDownloads.remove(state.id)
|
|
644
707
|
listener.onComplete(state.id, state.destination, bytesDownloaded, bytesTotal)
|
|
645
708
|
return DownloadResult.Success(state.id, state.destination, bytesDownloaded, bytesTotal)
|
|
@@ -660,7 +723,12 @@ class ResumableDownloader {
|
|
|
660
723
|
state.sessionId.get() != expectedSessionId -> DownloadResult.SessionInvalidated(state.id)
|
|
661
724
|
state.isCancelled.get() -> DownloadResult.Cancelled(state.id)
|
|
662
725
|
state.isPaused.get() -> DownloadResult.Paused(state.id, state.bytesDownloaded.get(), state.bytesTotal)
|
|
663
|
-
else ->
|
|
726
|
+
else -> {
|
|
727
|
+
// Not a pause or cancel, so a read or connect timeout (SocketTimeoutException is an
|
|
728
|
+
// InterruptedIOException). downloadWithResume retries it, then reports it
|
|
729
|
+
val errorCode = if (e is java.net.SocketTimeoutException) DownloadConstants.ERR_TIMED_OUT else -1
|
|
730
|
+
DownloadResult.fromException(state.id, e, errorCode).copy(isNetworkFailure = true)
|
|
731
|
+
}
|
|
664
732
|
}
|
|
665
733
|
} catch (e: Exception) {
|
|
666
734
|
// Determine result based on state - expected exceptions vs real errors
|
|
@@ -677,6 +745,10 @@ class ResumableDownloader {
|
|
|
677
745
|
RNBackgroundDownloaderModuleImpl.logD(TAG, "Download stopped (paused): ${state.id} - ${e.message}")
|
|
678
746
|
DownloadResult.Paused(state.id, state.bytesDownloaded.get(), state.bytesTotal)
|
|
679
747
|
}
|
|
748
|
+
isNetworkFailure(e) -> {
|
|
749
|
+
// downloadWithResume retries it, then reports it
|
|
750
|
+
DownloadResult.fromException(state.id, e).copy(isNetworkFailure = true)
|
|
751
|
+
}
|
|
680
752
|
else -> {
|
|
681
753
|
// Unexpected error - log with stack trace and report to listener
|
|
682
754
|
RNBackgroundDownloaderModuleImpl.logE(TAG, "Download error: ${e.message}")
|
|
@@ -15,7 +15,9 @@ import com.eko.uidt.UIDTJobRegistry
|
|
|
15
15
|
import com.eko.uidt.UIDTNotificationIds
|
|
16
16
|
import com.eko.uidt.UIDTNotificationManager
|
|
17
17
|
import com.eko.utils.ProgressUtils
|
|
18
|
+
import com.eko.utils.StorageManager
|
|
18
19
|
import org.json.JSONObject
|
|
20
|
+
import java.io.File
|
|
19
21
|
|
|
20
22
|
/**
|
|
21
23
|
* A JobService that uses User-Initiated Data Transfer (UIDT) for background downloads.
|
|
@@ -159,6 +161,15 @@ class UIDTDownloadJobService : JobService() {
|
|
|
159
161
|
var downloadListener: ResumableDownloader.DownloadListener?
|
|
160
162
|
get() = UIDTJobRegistry.downloadListener
|
|
161
163
|
set(value) { UIDTJobRegistry.downloadListener = value }
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The byte a job continues from. When resuming, the partial file is the source of
|
|
167
|
+
* truth: a saved offset can lag the bytes written (it's recorded when the job stops,
|
|
168
|
+
* or not at all if the process died), and appending from a lower offset duplicates
|
|
169
|
+
* data. No file means start over.
|
|
170
|
+
*/
|
|
171
|
+
internal fun resumeStartByte(savedStartByte: Long, destination: String): Long =
|
|
172
|
+
if (savedStartByte > 0) File(destination).let { if (it.exists()) it.length() else 0L } else 0L
|
|
162
173
|
}
|
|
163
174
|
|
|
164
175
|
private var wakeLock: PowerManager.WakeLock? = null
|
|
@@ -211,31 +222,32 @@ class UIDTDownloadJobService : JobService() {
|
|
|
211
222
|
// In-memory pendingHeaders is populated in the same process (scheduleDownload / onStopJob).
|
|
212
223
|
// The disk-persisted resume state is the fallback for a fresh process after a restart.
|
|
213
224
|
val inMemoryHeaders = UIDTJobRegistry.pendingHeaders.remove(configId)
|
|
225
|
+
// Kept on disk until the download completes, fails or is cancelled (those clear it):
|
|
226
|
+
// if the process dies mid-transfer, the restarted job still needs the headers
|
|
214
227
|
val diskResumeState = UIDTJobRegistry.loadResumeState(this, configId)
|
|
215
|
-
// Clear disk state now that we've consumed it.
|
|
216
|
-
UIDTJobRegistry.clearResumeState(this, configId)
|
|
217
228
|
|
|
218
229
|
val headers: Map<String, String>
|
|
219
|
-
val
|
|
230
|
+
val savedStartByte: Long
|
|
220
231
|
when {
|
|
221
232
|
inMemoryHeaders != null -> {
|
|
222
233
|
// Same-process restart: use in-memory headers.
|
|
223
234
|
// Prefer the disk byte position if it is more advanced than the extras
|
|
224
235
|
// (written by onStopJob with the actual download offset).
|
|
225
236
|
headers = inMemoryHeaders
|
|
226
|
-
|
|
237
|
+
savedStartByte = if (diskResumeState != null && diskResumeState.second > startByteFromExtras)
|
|
227
238
|
diskResumeState.second else startByteFromExtras
|
|
228
239
|
}
|
|
229
240
|
diskResumeState != null -> {
|
|
230
241
|
// Cross-process restart: use the persisted state.
|
|
231
242
|
headers = diskResumeState.first
|
|
232
|
-
|
|
243
|
+
savedStartByte = diskResumeState.second
|
|
233
244
|
}
|
|
234
245
|
else -> {
|
|
235
246
|
headers = emptyMap()
|
|
236
|
-
|
|
247
|
+
savedStartByte = startByteFromExtras
|
|
237
248
|
}
|
|
238
249
|
}
|
|
250
|
+
val startByte = resumeStartByte(savedStartByte, destination)
|
|
239
251
|
RNBackgroundDownloaderModuleImpl.logD(UIDTConstants.TAG, "onStartJob: configId=$configId, startByte=$startByte, headers=${headers.size}")
|
|
240
252
|
|
|
241
253
|
// Create notification for UIDT job (required)
|
|
@@ -475,8 +487,14 @@ class UIDTDownloadJobService : JobService() {
|
|
|
475
487
|
// took its ID from this offset, so it can go back to the pool
|
|
476
488
|
UIDTNotificationIds.release(id)
|
|
477
489
|
|
|
478
|
-
// Notify external listener
|
|
479
|
-
|
|
490
|
+
// Notify external listener. With none (the job ran in a process without the
|
|
491
|
+
// app), record it so the next launch reports it as done, not as paused
|
|
492
|
+
val listener = UIDTJobRegistry.downloadListener
|
|
493
|
+
if (listener != null) {
|
|
494
|
+
listener.onComplete(id, location, bytesDownloaded, bytesTotal)
|
|
495
|
+
} else {
|
|
496
|
+
recordFinishedWithoutApp(id, bytesDownloaded)
|
|
497
|
+
}
|
|
480
498
|
}
|
|
481
499
|
|
|
482
500
|
override fun onError(id: String, error: String, errorCode: Int) {
|
|
@@ -523,6 +541,14 @@ class UIDTDownloadJobService : JobService() {
|
|
|
523
541
|
}
|
|
524
542
|
}
|
|
525
543
|
|
|
544
|
+
// The download's recovery snapshot (saved while the app was running) gets its final size,
|
|
545
|
+
// which recovery on the next launch takes as a finished download
|
|
546
|
+
private fun recordFinishedWithoutApp(configId: String, bytesDownloaded: Long) {
|
|
547
|
+
val storage = StorageManager(this, RNBackgroundDownloaderModuleImpl.NAME)
|
|
548
|
+
val snapshot = storage.loadActiveDownloads()[configId] ?: return
|
|
549
|
+
storage.saveActiveDownload(snapshot.copy(bytesDownloaded = bytesDownloaded, bytesTotal = bytesDownloaded))
|
|
550
|
+
}
|
|
551
|
+
|
|
526
552
|
private fun acquireWakeLock() {
|
|
527
553
|
if (wakeLock == null) {
|
|
528
554
|
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
|