@kesha-antonov/react-native-background-downloader 4.7.0 → 5.0.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 +85 -37
- package/android/build.gradle +5 -1
- package/android/consumer-rules.pro +14 -0
- package/android/src/main/AndroidManifest.xml +18 -1
- package/android/src/main/java/com/eko/UIDTDownloadJobService.kt +5 -567
- package/android/src/main/java/com/eko/uidt/CancelDownloadReceiver.kt +4 -58
- package/android/src/main/java/com/{eko → kesha_antonov}/DownloadConstants.kt +6 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/DownloadEventEmitter.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/DownloadResult.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/Downloader.kt +14 -7
- package/android/src/main/java/com/{eko → kesha_antonov}/ProgressReporter.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/PromiseExtensions.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/RNBGDTaskConfig.kt +1 -1
- package/android/src/main/java/com/kesha_antonov/RNBGDUploadTaskConfig.kt +57 -0
- package/android/src/main/java/com/{eko → kesha_antonov}/RNBackgroundDownloaderModuleImpl.kt +103 -63
- package/android/src/main/java/com/{eko → kesha_antonov}/RNBackgroundDownloaderPackage.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/ResumableDownloadService.kt +11 -9
- package/android/src/main/java/com/{eko → kesha_antonov}/ResumableDownloader.kt +36 -15
- package/android/src/main/java/com/kesha_antonov/UIDTDownloadJobService.kt +590 -0
- package/android/src/main/java/com/{eko → kesha_antonov}/UnmeteredNetworkGate.kt +136 -19
- package/android/src/main/java/com/{eko → kesha_antonov}/UploadEventEmitter.kt +27 -2
- package/android/src/main/java/com/{eko → kesha_antonov}/Uploader.kt +189 -19
- package/android/src/main/java/com/{eko → kesha_antonov}/handlers/OnBegin.kt +4 -4
- package/android/src/main/java/com/{eko → kesha_antonov}/handlers/OnBeginState.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/handlers/OnProgress.kt +5 -5
- package/android/src/main/java/com/{eko → kesha_antonov}/handlers/OnProgressState.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/interfaces/BeginCallback.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/interfaces/ProgressCallback.kt +1 -1
- package/android/src/main/java/com/kesha_antonov/uidt/CancelDownloadReceiver.kt +68 -0
- package/android/src/main/java/com/{eko → kesha_antonov}/uidt/RNBGDFileProvider.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/uidt/UIDTJobIds.kt +9 -4
- package/android/src/main/java/com/{eko → kesha_antonov}/uidt/UIDTJobManager.kt +6 -6
- package/android/src/main/java/com/{eko → kesha_antonov}/uidt/UIDTJobState.kt +7 -5
- package/android/src/main/java/com/{eko → kesha_antonov}/uidt/UIDTNotificationIds.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/uidt/UIDTNotificationManager.kt +18 -10
- package/android/src/main/java/com/{eko → kesha_antonov}/utils/FileUtils.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/utils/HeaderUtils.kt +11 -2
- package/android/src/main/java/com/{eko → kesha_antonov}/utils/NetworkRequestUtils.kt +1 -1
- package/android/src/main/java/com/kesha_antonov/utils/NotificationUtils.kt +24 -0
- package/android/src/main/java/com/{eko → kesha_antonov}/utils/ProgressUtils.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/utils/ReadableConverters.kt +1 -1
- package/android/src/main/java/com/{eko → kesha_antonov}/utils/RedirectResolver.kt +32 -4
- package/android/src/main/java/com/{eko → kesha_antonov}/utils/StorageManager.kt +5 -5
- package/android/src/main/java/com/{eko → kesha_antonov}/utils/TempFileUtils.kt +2 -2
- package/android/src/newarch/java/com/{eko → kesha_antonov}/RNBackgroundDownloaderModule.kt +1 -1
- package/android/src/oldarch/java/com/{eko → kesha_antonov}/RNBackgroundDownloaderModule.kt +1 -1
- package/android/src/test/java/com/kesha_antonov/DownloadCompleteReceiverTest.kt +57 -0
- package/android/src/test/java/com/{eko → kesha_antonov}/DownloaderRecoveryTest.kt +2 -2
- package/android/src/test/java/com/{eko → kesha_antonov}/DownloaderResumeTest.kt +3 -3
- package/android/src/test/java/com/{eko → kesha_antonov}/ResumableDownloaderConcurrencyTest.kt +1 -1
- package/android/src/test/java/com/kesha_antonov/ResumableDownloaderRedirectTest.kt +77 -0
- package/android/src/test/java/com/{eko → kesha_antonov}/ResumableDownloaderStallTest.kt +1 -1
- package/android/src/test/java/com/kesha_antonov/UnmeteredNetworkGateTest.kt +248 -0
- package/android/src/test/java/com/kesha_antonov/UploadPartRangesTest.kt +33 -0
- package/android/src/test/java/com/{eko → kesha_antonov}/UploaderMultipartTest.kt +11 -2
- package/android/src/test/java/com/kesha_antonov/UploaderPartsTest.kt +222 -0
- package/android/src/test/java/com/kesha_antonov/UploaderResponseTest.kt +26 -0
- package/android/src/test/java/com/{eko → kesha_antonov}/handlers/OnBeginTest.kt +2 -2
- package/android/src/test/java/com/{eko → kesha_antonov}/uidt/CancelDownloadReceiverTest.kt +25 -2
- package/android/src/test/java/com/{eko → kesha_antonov}/uidt/UIDTJobIdsTest.kt +13 -2
- package/android/src/test/java/com/{eko → kesha_antonov}/uidt/UIDTJobManagerTest.kt +1 -1
- package/android/src/test/java/com/{eko → kesha_antonov}/uidt/UIDTNotificationIdsTest.kt +1 -1
- package/android/src/test/java/com/kesha_antonov/utils/RedirectResolverTest.kt +133 -0
- package/bin/react-native-background-downloader.js +197 -0
- package/ios/RNBGDUploadTaskConfig.h +26 -0
- package/ios/RNBGDUploadTaskConfig.mm +69 -3
- package/ios/RNBackgroundDownloader.mm +587 -47
- package/lib/NativeRNBackgroundDownloader.d.ts +15 -0
- package/lib/UploadTask.d.ts +5 -1
- package/lib/UploadTask.js +22 -4
- package/lib/index.js +57 -27
- package/lib/types.d.ts +33 -2
- package/package.json +5 -2
- package/src/NativeRNBackgroundDownloader.ts +23 -0
- package/src/UploadTask.ts +23 -1
- package/src/index.ts +60 -27
- package/src/types.ts +42 -2
- package/android/src/main/java/com/eko/RNBGDUploadTaskConfig.kt +0 -22
- package/android/src/test/java/com/eko/utils/RedirectResolverTest.kt +0 -67
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
package com.kesha_antonov.uidt
|
|
2
|
+
|
|
3
|
+
import android.content.BroadcastReceiver
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.content.Intent
|
|
6
|
+
import android.os.Build
|
|
7
|
+
import androidx.annotation.RequiresApi
|
|
8
|
+
import com.kesha_antonov.RNBackgroundDownloaderModuleImpl
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Receives the cancel action fired from the download progress notification.
|
|
12
|
+
* The action carries the download's [EXTRA_CONFIG_ID] as a string extra; on
|
|
13
|
+
* receive we stop the download and dispatch a downloadFailed event so JS can
|
|
14
|
+
* clean up.
|
|
15
|
+
*
|
|
16
|
+
* Cancelling from the shade must leave exactly the same state behind as
|
|
17
|
+
* `task.stop()` from JS, so the work is delegated to the module's stopTask
|
|
18
|
+
* (progress tracking, resumable + DownloadManager cancellation, persisted
|
|
19
|
+
* maps and paused records). Only when the module is not up - a cold start
|
|
20
|
+
* triggered by this very broadcast - do we fall back to cancelling the UIDT
|
|
21
|
+
* job on its own.
|
|
22
|
+
*/
|
|
23
|
+
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
|
|
24
|
+
open class CancelDownloadReceiver : BroadcastReceiver() {
|
|
25
|
+
|
|
26
|
+
override fun onReceive(context: Context, intent: Intent) {
|
|
27
|
+
if (intent.action != ACTION_CANCEL_DOWNLOAD && intent.action != LEGACY_ACTION_CANCEL_DOWNLOAD) return
|
|
28
|
+
val configId = intent.getStringExtra(EXTRA_CONFIG_ID) ?: return
|
|
29
|
+
|
|
30
|
+
RNBackgroundDownloaderModuleImpl.logD(
|
|
31
|
+
UIDTConstants.TAG,
|
|
32
|
+
"CancelDownloadReceiver: cancelling $configId",
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
// Snapshot liveness first: stopTask tears the job down, so afterwards we
|
|
36
|
+
// can no longer tell an active download from an already-finished one.
|
|
37
|
+
// A job the system stopped (e.g. waiting for Wi-Fi) is no longer active but still
|
|
38
|
+
// scheduled, and its notification with this action stays up - that counts too.
|
|
39
|
+
val wasActive = UIDTJobRegistry.isActiveJob(configId) || UIDTJobManager.isScheduledJob(context, configId)
|
|
40
|
+
|
|
41
|
+
if (!RNBackgroundDownloaderModuleImpl.stopTaskFromNative(configId)) {
|
|
42
|
+
RNBackgroundDownloaderModuleImpl.logD(
|
|
43
|
+
UIDTConstants.TAG,
|
|
44
|
+
"CancelDownloadReceiver: module not initialized, cancelling job directly",
|
|
45
|
+
)
|
|
46
|
+
UIDTJobManager.cancelJob(context, configId)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Only notify JS when a live job was actually cancelled. If the download
|
|
50
|
+
// already finished there is nothing to clean up, and dispatching here
|
|
51
|
+
// would emit a spurious downloadFailed for an already-completed task.
|
|
52
|
+
if (wasActive) {
|
|
53
|
+
// Notify JS so the in-memory task map can be cleaned and the
|
|
54
|
+
// .error() handler runs (lib uses CANCELLED errorCode = -1).
|
|
55
|
+
UIDTJobRegistry.downloadListener?.onError(configId, CANCELLED_MESSAGE, CANCELLED_ERROR_CODE)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
companion object {
|
|
60
|
+
const val ACTION_CANCEL_DOWNLOAD = "com.kesha_antonov.uidt.ACTION_CANCEL_DOWNLOAD"
|
|
61
|
+
// The action of notifications posted before v5.0.0
|
|
62
|
+
const val LEGACY_ACTION_CANCEL_DOWNLOAD = "com.eko.uidt.ACTION_CANCEL_DOWNLOAD"
|
|
63
|
+
const val EXTRA_CONFIG_ID = "config_id"
|
|
64
|
+
|
|
65
|
+
private const val CANCELLED_MESSAGE = "Download cancelled by user"
|
|
66
|
+
private const val CANCELLED_ERROR_CODE = -1
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
package com.
|
|
1
|
+
package com.kesha_antonov.uidt
|
|
2
2
|
|
|
3
3
|
import android.app.job.JobInfo
|
|
4
4
|
import android.os.Build
|
|
5
5
|
import androidx.annotation.RequiresApi
|
|
6
|
-
import com.
|
|
6
|
+
import com.kesha_antonov.UIDTDownloadJobService
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Assigns the JobScheduler job IDs used for UIDT downloads.
|
|
@@ -32,7 +32,12 @@ internal object UIDTJobIds {
|
|
|
32
32
|
/** configId -> ID picked but not yet handed to the JobScheduler. */
|
|
33
33
|
private val reservations = mutableMapOf<String, Int>()
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
// Jobs scheduled before v5.0.0 name the service by its old class, which
|
|
36
|
+
// still runs them (com.eko.UIDTDownloadJobService)
|
|
37
|
+
private val serviceNames = setOf(
|
|
38
|
+
UIDTDownloadJobService::class.java.name,
|
|
39
|
+
"com.eko.UIDTDownloadJobService",
|
|
40
|
+
)
|
|
36
41
|
|
|
37
42
|
/**
|
|
38
43
|
* The ID a download would have had before IDs were assigned. Still the
|
|
@@ -45,7 +50,7 @@ internal object UIDTJobIds {
|
|
|
45
50
|
|
|
46
51
|
/** The download a scheduled job belongs to, or null if it isn't one of ours. */
|
|
47
52
|
fun configIdOf(job: JobInfo): String? {
|
|
48
|
-
if (job.service.className
|
|
53
|
+
if (job.service.className !in serviceNames) return null
|
|
49
54
|
return job.extras.getString(UIDTConstants.KEY_DOWNLOAD_ID)
|
|
50
55
|
}
|
|
51
56
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package com.
|
|
1
|
+
package com.kesha_antonov.uidt
|
|
2
2
|
|
|
3
3
|
import android.app.DownloadManager
|
|
4
4
|
import android.app.NotificationManager
|
|
@@ -7,14 +7,14 @@ import android.app.job.JobScheduler
|
|
|
7
7
|
import android.app.job.JobService
|
|
8
8
|
import android.content.ComponentName
|
|
9
9
|
import android.content.Context
|
|
10
|
-
import com.
|
|
10
|
+
import com.kesha_antonov.utils.NetworkRequestUtils
|
|
11
11
|
import android.os.Build
|
|
12
12
|
import android.os.PersistableBundle
|
|
13
13
|
import androidx.annotation.RequiresApi
|
|
14
|
-
import com.
|
|
15
|
-
import com.
|
|
16
|
-
import com.
|
|
17
|
-
import com.
|
|
14
|
+
import com.kesha_antonov.RNBackgroundDownloaderModuleImpl
|
|
15
|
+
import com.kesha_antonov.ResumableDownloader
|
|
16
|
+
import com.kesha_antonov.UIDTDownloadJobService
|
|
17
|
+
import com.kesha_antonov.utils.ProgressUtils
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Manages UIDT job scheduling, cancellation, pause and resume operations.
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
package com.
|
|
1
|
+
package com.kesha_antonov.uidt
|
|
2
2
|
|
|
3
3
|
import android.app.job.JobParameters
|
|
4
4
|
import android.content.Context
|
|
5
|
-
import com.
|
|
6
|
-
import com.
|
|
5
|
+
import com.kesha_antonov.RNBackgroundDownloaderModuleImpl
|
|
6
|
+
import com.kesha_antonov.ResumableDownloader
|
|
7
7
|
import org.json.JSONObject
|
|
8
8
|
import java.util.concurrent.ConcurrentHashMap
|
|
9
9
|
|
|
@@ -21,6 +21,8 @@ data class JobState(
|
|
|
21
21
|
val customTitle: String = "",
|
|
22
22
|
var lastNotifiedProgress: Int = -1,
|
|
23
23
|
var lastNotificationUpdateTime: Long = 0,
|
|
24
|
+
// When the byte position was last written to disk (see UIDTDownloadJobService.RESUME_SAVE_INTERVAL_MS)
|
|
25
|
+
var lastResumeSaveTime: Long = 0,
|
|
24
26
|
// Track download progress for summary notification
|
|
25
27
|
@Volatile var bytesDownloaded: Long = 0L,
|
|
26
28
|
@Volatile var bytesTotal: Long = -1L
|
|
@@ -121,7 +123,7 @@ object UIDTConstants {
|
|
|
121
123
|
const val NOTIFICATION_CHANNEL_FINISHED_ID = "uidt_download_channel_finished"
|
|
122
124
|
|
|
123
125
|
// Notification group for grouping all download notifications together
|
|
124
|
-
const val NOTIFICATION_GROUP_KEY = "com.
|
|
126
|
+
const val NOTIFICATION_GROUP_KEY = "com.kesha_antonov.DOWNLOAD_GROUP"
|
|
125
127
|
|
|
126
128
|
// Summary notification ID (used to group all download notifications)
|
|
127
129
|
const val SUMMARY_NOTIFICATION_ID = 19999
|
|
@@ -224,7 +226,7 @@ object UIDTJobRegistry {
|
|
|
224
226
|
|
|
225
227
|
// Store service instance reference for notification updates
|
|
226
228
|
@Volatile
|
|
227
|
-
var serviceInstance: com.
|
|
229
|
+
var serviceInstance: com.kesha_antonov.UIDTDownloadJobService? = null
|
|
228
230
|
|
|
229
231
|
// Notification configuration
|
|
230
232
|
val notificationConfig = NotificationConfig()
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package com.
|
|
1
|
+
package com.kesha_antonov.uidt
|
|
2
2
|
|
|
3
3
|
import android.app.Notification
|
|
4
4
|
import android.app.NotificationChannel
|
|
@@ -13,9 +13,10 @@ import android.os.Build
|
|
|
13
13
|
import androidx.annotation.RequiresApi
|
|
14
14
|
import androidx.core.app.NotificationCompat
|
|
15
15
|
import androidx.core.content.FileProvider
|
|
16
|
-
import com.
|
|
17
|
-
import com.
|
|
18
|
-
import com.
|
|
16
|
+
import com.kesha_antonov.RNBackgroundDownloaderModuleImpl
|
|
17
|
+
import com.kesha_antonov.UIDTDownloadJobService
|
|
18
|
+
import com.kesha_antonov.utils.NotificationUtils.applyOpenAppIntent
|
|
19
|
+
import com.kesha_antonov.utils.ProgressUtils
|
|
19
20
|
import java.io.File
|
|
20
21
|
import java.net.URLConnection
|
|
21
22
|
|
|
@@ -203,6 +204,7 @@ object UIDTNotificationManager {
|
|
|
203
204
|
.setProgress(0, 0, true)
|
|
204
205
|
|
|
205
206
|
applyCancelAction(builder, context, configId)
|
|
207
|
+
applyOpenAppIntent(builder, context)
|
|
206
208
|
|
|
207
209
|
// Apply grouping when enabled and groupId is provided
|
|
208
210
|
if (config.groupingEnabled && groupId.isNotEmpty()) {
|
|
@@ -257,6 +259,7 @@ object UIDTNotificationManager {
|
|
|
257
259
|
.setProgress(100, progress, bytesTotal <= 0)
|
|
258
260
|
|
|
259
261
|
applyCancelAction(builder, context, configId)
|
|
262
|
+
applyOpenAppIntent(builder, context)
|
|
260
263
|
|
|
261
264
|
// Apply grouping when enabled
|
|
262
265
|
if (config.groupingEnabled && jobState.groupId.isNotEmpty()) {
|
|
@@ -285,7 +288,7 @@ object UIDTNotificationManager {
|
|
|
285
288
|
config.getText("downloadTitle")
|
|
286
289
|
}
|
|
287
290
|
|
|
288
|
-
val
|
|
291
|
+
val pausedBuilder = NotificationCompat.Builder(context, UIDTConstants.NOTIFICATION_CHANNEL_ID)
|
|
289
292
|
.setContentTitle(title)
|
|
290
293
|
.setContentText(config.getText("downloadPaused"))
|
|
291
294
|
.setSmallIcon(android.R.drawable.stat_sys_download_done)
|
|
@@ -294,7 +297,8 @@ object UIDTNotificationManager {
|
|
|
294
297
|
.setOnlyAlertOnce(true)
|
|
295
298
|
.setShowWhen(false)
|
|
296
299
|
.setProgress(100, progress, false)
|
|
297
|
-
|
|
300
|
+
applyOpenAppIntent(pausedBuilder, context)
|
|
301
|
+
val notification = pausedBuilder.build()
|
|
298
302
|
|
|
299
303
|
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
300
304
|
notificationManager.notify(notificationId, notification)
|
|
@@ -332,6 +336,8 @@ object UIDTNotificationManager {
|
|
|
332
336
|
.setShowWhen(false)
|
|
333
337
|
.setProgress(100, progress, bytesTotal <= 0)
|
|
334
338
|
|
|
339
|
+
applyCancelAction(builder, context, configId)
|
|
340
|
+
applyOpenAppIntent(builder, context)
|
|
335
341
|
val notification = builder.build()
|
|
336
342
|
|
|
337
343
|
val service = UIDTJobRegistry.serviceInstance
|
|
@@ -398,7 +404,7 @@ object UIDTNotificationManager {
|
|
|
398
404
|
val title = groupName.ifEmpty { config.getText("groupTitle") }
|
|
399
405
|
val text = config.getText("groupText", "count" to groupDownloads)
|
|
400
406
|
|
|
401
|
-
val
|
|
407
|
+
val summaryBuilder = NotificationCompat.Builder(context, UIDTConstants.NOTIFICATION_CHANNEL_ID)
|
|
402
408
|
.setContentTitle(title)
|
|
403
409
|
.setContentText(text)
|
|
404
410
|
.setSmallIcon(android.R.drawable.stat_sys_download)
|
|
@@ -408,7 +414,8 @@ object UIDTNotificationManager {
|
|
|
408
414
|
.setOngoing(true)
|
|
409
415
|
.setOnlyAlertOnce(true)
|
|
410
416
|
.setShowWhen(false)
|
|
411
|
-
|
|
417
|
+
applyOpenAppIntent(summaryBuilder, context)
|
|
418
|
+
val summaryNotification = summaryBuilder.build()
|
|
412
419
|
|
|
413
420
|
notificationManager.notify(summaryNotificationId, summaryNotification)
|
|
414
421
|
}
|
|
@@ -478,7 +485,7 @@ object UIDTNotificationManager {
|
|
|
478
485
|
|
|
479
486
|
// Create a standalone progress notification (NOT part of the group)
|
|
480
487
|
// This ensures progress bar is visible in collapsed view
|
|
481
|
-
val
|
|
488
|
+
val progressBuilder = NotificationCompat.Builder(context, UIDTConstants.NOTIFICATION_CHANNEL_ID)
|
|
482
489
|
.setContentTitle(title)
|
|
483
490
|
.setContentText(text)
|
|
484
491
|
.setSmallIcon(android.R.drawable.stat_sys_download)
|
|
@@ -488,7 +495,8 @@ object UIDTNotificationManager {
|
|
|
488
495
|
.setOnlyAlertOnce(true)
|
|
489
496
|
.setShowWhen(false)
|
|
490
497
|
.setProgress(100, progress, indeterminate)
|
|
491
|
-
|
|
498
|
+
applyOpenAppIntent(progressBuilder, context)
|
|
499
|
+
val progressNotification = progressBuilder.build()
|
|
492
500
|
|
|
493
501
|
notificationManager.notify(summaryNotificationId, progressNotification)
|
|
494
502
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
package com.
|
|
1
|
+
package com.kesha_antonov.utils
|
|
2
2
|
|
|
3
3
|
import android.app.DownloadManager
|
|
4
|
-
import com.
|
|
4
|
+
import com.kesha_antonov.DownloadConstants
|
|
5
5
|
import com.facebook.react.bridge.ReadableMap
|
|
6
6
|
import java.net.HttpURLConnection
|
|
7
7
|
|
|
@@ -10,6 +10,15 @@ import java.net.HttpURLConnection
|
|
|
10
10
|
*/
|
|
11
11
|
object HeaderUtils {
|
|
12
12
|
|
|
13
|
+
// Headers that carry credentials and must not be sent to another host
|
|
14
|
+
private val CREDENTIAL_HEADERS = setOf("authorization", "cookie", "proxy-authorization")
|
|
15
|
+
|
|
16
|
+
fun isCredentialHeader(name: String): Boolean = name.lowercase() in CREDENTIAL_HEADERS
|
|
17
|
+
|
|
18
|
+
/** The headers without Authorization, Cookie and Proxy-Authorization (case-insensitive). */
|
|
19
|
+
fun withoutCredentials(headers: Map<String, String>): Map<String, String> =
|
|
20
|
+
headers.filterKeys { !isCredentialHeader(it) }
|
|
21
|
+
|
|
13
22
|
/**
|
|
14
23
|
* Check if the provided headers already contain a User-Agent header (case-insensitive).
|
|
15
24
|
*/
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
package com.kesha_antonov.utils
|
|
2
|
+
|
|
3
|
+
import android.app.PendingIntent
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import androidx.core.app.NotificationCompat
|
|
6
|
+
|
|
7
|
+
object NotificationUtils {
|
|
8
|
+
/**
|
|
9
|
+
* Tapping the notification brings the host app to the front (the launcher
|
|
10
|
+
* intent reuses the running task instead of starting a second one).
|
|
11
|
+
* Does nothing when the app has no launcher activity.
|
|
12
|
+
*/
|
|
13
|
+
fun applyOpenAppIntent(builder: NotificationCompat.Builder, context: Context) {
|
|
14
|
+
val launchIntent = context.packageManager.getLaunchIntentForPackage(context.packageName) ?: return
|
|
15
|
+
builder.setContentIntent(
|
|
16
|
+
PendingIntent.getActivity(
|
|
17
|
+
context,
|
|
18
|
+
0,
|
|
19
|
+
launchIntent,
|
|
20
|
+
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
|
21
|
+
)
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
package com.
|
|
1
|
+
package com.kesha_antonov.utils
|
|
2
2
|
|
|
3
|
-
import com.
|
|
4
|
-
import com.
|
|
3
|
+
import com.kesha_antonov.DownloadConstants
|
|
4
|
+
import com.kesha_antonov.RNBackgroundDownloaderModuleImpl
|
|
5
5
|
import com.facebook.react.bridge.ReadableMap
|
|
6
6
|
import java.net.HttpURLConnection
|
|
7
7
|
import java.net.URL
|
|
@@ -58,7 +58,15 @@ object RedirectResolver {
|
|
|
58
58
|
// Resolve relative locations against the current URL, keeping its port,
|
|
59
59
|
// path and scheme (for "//host/path")
|
|
60
60
|
val previousUrl = currentUrl
|
|
61
|
-
|
|
61
|
+
val nextUrl = URL(URL(currentUrl), location)
|
|
62
|
+
if (!isSafeHop(URL(currentUrl), nextUrl, headers)) {
|
|
63
|
+
// Leave this hop to the downloader: the platform's own redirect
|
|
64
|
+
// handling drops credentials across hosts and refuses downgrades
|
|
65
|
+
RNBackgroundDownloaderModuleImpl.logW(TAG, "Not following redirect from $previousUrl to $nextUrl")
|
|
66
|
+
connection.disconnect()
|
|
67
|
+
break
|
|
68
|
+
}
|
|
69
|
+
currentUrl = nextUrl.toString()
|
|
62
70
|
|
|
63
71
|
RNBackgroundDownloaderModuleImpl.logD(TAG, "Redirect ${redirectCount + 1}/$maxRedirects: $previousUrl -> $currentUrl")
|
|
64
72
|
redirectCount++
|
|
@@ -86,4 +94,24 @@ object RedirectResolver {
|
|
|
86
94
|
return originalUrl
|
|
87
95
|
}
|
|
88
96
|
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Whether the resolver may probe [next] with the request's headers. Only http(s),
|
|
100
|
+
* never https -> http, and no other origin while credentials are attached -
|
|
101
|
+
* every probe resends all headers, and the resolved URL is downloaded with them.
|
|
102
|
+
*/
|
|
103
|
+
internal fun isSafeHop(current: URL, next: URL, headers: Map<String, String>): Boolean {
|
|
104
|
+
val scheme = next.protocol.lowercase()
|
|
105
|
+
if (scheme != "http" && scheme != "https") return false
|
|
106
|
+
if (current.protocol.equals("https", ignoreCase = true) && scheme == "http") return false
|
|
107
|
+
val sameOrigin = current.protocol.equals(next.protocol, ignoreCase = true) &&
|
|
108
|
+
current.host.equals(next.host, ignoreCase = true) &&
|
|
109
|
+
effectivePort(current) == effectivePort(next)
|
|
110
|
+
return sameOrigin || headers.keys.none { HeaderUtils.isCredentialHeader(it) }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private fun isSafeHop(current: URL, next: URL, headers: ReadableMap?): Boolean =
|
|
114
|
+
isSafeHop(current, next, HeaderUtils.toMap(headers))
|
|
115
|
+
|
|
116
|
+
private fun effectivePort(url: URL): Int = if (url.port != -1) url.port else url.defaultPort
|
|
89
117
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
package com.
|
|
1
|
+
package com.kesha_antonov.utils
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
4
|
import android.content.SharedPreferences
|
|
5
|
-
import com.
|
|
6
|
-
import com.
|
|
7
|
-
import com.
|
|
8
|
-
import com.
|
|
5
|
+
import com.kesha_antonov.Downloader
|
|
6
|
+
import com.kesha_antonov.RNBackgroundDownloaderModuleImpl
|
|
7
|
+
import com.kesha_antonov.RNBGDTaskConfig
|
|
8
|
+
import com.kesha_antonov.RNBGDUploadTaskConfig
|
|
9
9
|
import com.google.gson.Gson
|
|
10
10
|
import com.google.gson.reflect.TypeToken
|
|
11
11
|
import com.tencent.mmkv.MMKV
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
package com.kesha_antonov
|
|
2
|
+
|
|
3
|
+
import android.app.Application
|
|
4
|
+
import android.app.DownloadManager
|
|
5
|
+
import androidx.test.core.app.ApplicationProvider
|
|
6
|
+
import com.facebook.react.bridge.BridgeReactContext
|
|
7
|
+
import org.junit.Assert.assertEquals
|
|
8
|
+
import org.junit.Assert.assertTrue
|
|
9
|
+
import org.junit.Test
|
|
10
|
+
import org.junit.runner.RunWith
|
|
11
|
+
import org.robolectric.RobolectricTestRunner
|
|
12
|
+
import org.robolectric.Shadows.shadowOf
|
|
13
|
+
import org.robolectric.annotation.Config
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The DOWNLOAD_COMPLETE receiver has to be exported for DownloadManager to reach it,
|
|
17
|
+
* and the action isn't protected on every Android version. Without a sender
|
|
18
|
+
* permission any app could send one and end (and remove) a running download.
|
|
19
|
+
*/
|
|
20
|
+
@RunWith(RobolectricTestRunner::class)
|
|
21
|
+
class DownloadCompleteReceiverTest {
|
|
22
|
+
|
|
23
|
+
private val app: Application get() = ApplicationProvider.getApplicationContext()
|
|
24
|
+
|
|
25
|
+
private fun registeredDownloadReceivers() = shadowOf(app).registeredReceivers.filter {
|
|
26
|
+
it.intentFilter.hasAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private fun registerReceiver() {
|
|
30
|
+
val module = RNBackgroundDownloaderModuleImpl(BridgeReactContext(app))
|
|
31
|
+
RNBackgroundDownloaderModuleImpl::class.java.getDeclaredMethod("registerDownloadReceiver").apply {
|
|
32
|
+
isAccessible = true
|
|
33
|
+
invoke(module)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@Test
|
|
38
|
+
@Config(sdk = [33])
|
|
39
|
+
fun `only DownloadProvider may send it before Android 14`() {
|
|
40
|
+
registerReceiver()
|
|
41
|
+
|
|
42
|
+
val receivers = registeredDownloadReceivers()
|
|
43
|
+
assertEquals(1, receivers.size)
|
|
44
|
+
assertEquals("android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS", receivers[0].broadcastPermission)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Test
|
|
48
|
+
@Config(sdk = [34])
|
|
49
|
+
fun `only DownloadProvider may send it on Android 14+`() {
|
|
50
|
+
registerReceiver()
|
|
51
|
+
|
|
52
|
+
val receivers = registeredDownloadReceivers()
|
|
53
|
+
assertEquals(1, receivers.size)
|
|
54
|
+
assertEquals("android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS", receivers[0].broadcastPermission)
|
|
55
|
+
assertTrue(receivers[0].flags and android.content.Context.RECEIVER_EXPORTED != 0)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
package com.
|
|
1
|
+
package com.kesha_antonov
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
4
|
import androidx.test.core.app.ApplicationProvider
|
|
5
|
-
import com.
|
|
5
|
+
import com.kesha_antonov.utils.StorageManager
|
|
6
6
|
import org.junit.Assert.assertEquals
|
|
7
7
|
import org.junit.Assert.assertTrue
|
|
8
8
|
import org.junit.Rule
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
package com.
|
|
1
|
+
package com.kesha_antonov
|
|
2
2
|
|
|
3
3
|
import android.app.job.JobScheduler
|
|
4
4
|
import android.content.Context
|
|
5
5
|
import androidx.test.core.app.ApplicationProvider
|
|
6
|
-
import com.
|
|
7
|
-
import com.
|
|
6
|
+
import com.kesha_antonov.uidt.UIDTConstants
|
|
7
|
+
import com.kesha_antonov.uidt.UIDTJobRegistry
|
|
8
8
|
import org.junit.Assert.assertEquals
|
|
9
9
|
import org.junit.Assert.assertTrue
|
|
10
10
|
import org.junit.Before
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
package com.kesha_antonov
|
|
2
|
+
|
|
3
|
+
import okhttp3.mockwebserver.MockResponse
|
|
4
|
+
import okhttp3.mockwebserver.MockWebServer
|
|
5
|
+
import org.junit.After
|
|
6
|
+
import org.junit.Assert.assertEquals
|
|
7
|
+
import org.junit.Assert.assertTrue
|
|
8
|
+
import org.junit.Before
|
|
9
|
+
import org.junit.Rule
|
|
10
|
+
import org.junit.Test
|
|
11
|
+
import org.junit.rules.TemporaryFolder
|
|
12
|
+
import org.junit.runner.RunWith
|
|
13
|
+
import org.robolectric.RobolectricTestRunner
|
|
14
|
+
import org.robolectric.annotation.Config
|
|
15
|
+
import java.io.File
|
|
16
|
+
import java.util.concurrent.CountDownLatch
|
|
17
|
+
import java.util.concurrent.TimeUnit
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* HttpURLConnection follows same-scheme redirects itself and refuses the others on
|
|
21
|
+
* purpose. The downloader may only add http -> https upgrades on top of that, so a
|
|
22
|
+
* redirect elsewhere is a plain HTTP error.
|
|
23
|
+
*/
|
|
24
|
+
@RunWith(RobolectricTestRunner::class)
|
|
25
|
+
@Config(sdk = [34])
|
|
26
|
+
class ResumableDownloaderRedirectTest {
|
|
27
|
+
|
|
28
|
+
@get:Rule
|
|
29
|
+
val tempFolder = TemporaryFolder()
|
|
30
|
+
|
|
31
|
+
private lateinit var server: MockWebServer
|
|
32
|
+
private val finished = CountDownLatch(1)
|
|
33
|
+
@Volatile private var errorCode: Int? = null
|
|
34
|
+
|
|
35
|
+
private val listener = object : ResumableDownloader.DownloadListener {
|
|
36
|
+
override fun onBegin(id: String, expectedBytes: Long, headers: Map<String, String>) = Unit
|
|
37
|
+
override fun onProgress(id: String, bytesDownloaded: Long, bytesTotal: Long) = Unit
|
|
38
|
+
override fun onComplete(id: String, location: String, bytesDownloaded: Long, bytesTotal: Long) {
|
|
39
|
+
finished.countDown()
|
|
40
|
+
}
|
|
41
|
+
override fun onError(id: String, error: String, errorCode: Int) {
|
|
42
|
+
this@ResumableDownloaderRedirectTest.errorCode = errorCode
|
|
43
|
+
finished.countDown()
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Before
|
|
48
|
+
fun startServer() {
|
|
49
|
+
server = MockWebServer()
|
|
50
|
+
server.start()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@After
|
|
54
|
+
fun stopServer() {
|
|
55
|
+
server.shutdown()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@Test
|
|
59
|
+
fun `a redirect to a non-http scheme fails with the redirect status`() {
|
|
60
|
+
server.enqueue(MockResponse().setResponseCode(302).setHeader("Location", "ftp://example.com/file"))
|
|
61
|
+
|
|
62
|
+
ResumableDownloader().apply {
|
|
63
|
+
networkRetryDelaysMs = longArrayOf(10)
|
|
64
|
+
}.startDownload(
|
|
65
|
+
id = "ftp",
|
|
66
|
+
url = server.url("/file").toString(),
|
|
67
|
+
destination = File(tempFolder.root, "ftp").absolutePath,
|
|
68
|
+
headers = mapOf("Authorization" to "Bearer secret"),
|
|
69
|
+
listener = listener,
|
|
70
|
+
isAllowedOverMetered = true
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
assertTrue("the download didn't finish", finished.await(10, TimeUnit.SECONDS))
|
|
74
|
+
assertEquals(302, errorCode)
|
|
75
|
+
assertEquals(1, server.requestCount)
|
|
76
|
+
}
|
|
77
|
+
}
|