@kesha-antonov/react-native-background-downloader 4.5.9 → 4.6.1
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 +69 -4
- 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/RNBackgroundDownloaderModuleImpl.kt +81 -11
- 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/res/xml/rnbgd_file_paths.xml +23 -0
- 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/lib/NativeRNBackgroundDownloader.d.ts +2 -0
- package/lib/config.d.ts +2 -0
- package/lib/config.js +3 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +9 -2
- package/lib/notifications.js +4 -3
- package/lib/types.d.ts +19 -0
- package/package.json +3 -2
- package/react-native-background-downloader.podspec +14 -1
- package/src/NativeRNBackgroundDownloader.ts +8 -1
- package/src/config.ts +5 -0
- package/src/index.ts +19 -1
- package/src/notifications.ts +1 -0
- package/src/types.ts +19 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
package com.eko.uidt
|
|
2
|
+
|
|
3
|
+
import android.app.job.JobInfo
|
|
4
|
+
import android.content.ComponentName
|
|
5
|
+
import android.content.Context
|
|
6
|
+
import android.os.PersistableBundle
|
|
7
|
+
import androidx.test.core.app.ApplicationProvider
|
|
8
|
+
import com.eko.UIDTDownloadJobService
|
|
9
|
+
import org.junit.Assert.assertEquals
|
|
10
|
+
import org.junit.Assert.assertNotEquals
|
|
11
|
+
import org.junit.Assert.assertNull
|
|
12
|
+
import org.junit.Before
|
|
13
|
+
import org.junit.Test
|
|
14
|
+
import org.junit.runner.RunWith
|
|
15
|
+
import org.robolectric.RobolectricTestRunner
|
|
16
|
+
import org.robolectric.annotation.Config
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Job IDs used to be `JOB_ID_BASE + hash(configId) % 10000`, and scheduling an ID
|
|
20
|
+
* that already exists replaces the job holding it - so two downloads hashing
|
|
21
|
+
* together silently killed one of each other. These pin the lookup that replaced
|
|
22
|
+
* it: a job is found by the config ID it already carries in its extras.
|
|
23
|
+
*/
|
|
24
|
+
@RunWith(RobolectricTestRunner::class)
|
|
25
|
+
@Config(sdk = [34])
|
|
26
|
+
class UIDTJobIdsTest {
|
|
27
|
+
|
|
28
|
+
private val context: Context get() = ApplicationProvider.getApplicationContext()
|
|
29
|
+
|
|
30
|
+
/** Two distinct strings with the same hash code - the collision this fixes. */
|
|
31
|
+
private val colliding = Pair("Aa", "BB")
|
|
32
|
+
|
|
33
|
+
@Before
|
|
34
|
+
fun clearReservations() {
|
|
35
|
+
for (id in listOf(colliding.first, colliding.second, "a", "b")) {
|
|
36
|
+
UIDTJobIds.endReservation(id)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private fun downloadJob(jobId: Int, configId: String): JobInfo =
|
|
41
|
+
JobInfo.Builder(jobId, ComponentName(context, UIDTDownloadJobService::class.java))
|
|
42
|
+
.setExtras(PersistableBundle().apply { putString(UIDTConstants.KEY_DOWNLOAD_ID, configId) })
|
|
43
|
+
.setMinimumLatency(1000)
|
|
44
|
+
.build()
|
|
45
|
+
|
|
46
|
+
/** A job belonging to something else in the app - WorkManager, say. */
|
|
47
|
+
private fun foreignJob(jobId: Int): JobInfo =
|
|
48
|
+
JobInfo.Builder(jobId, ComponentName(context, "com.example.SomeOtherService"))
|
|
49
|
+
.setMinimumLatency(1000)
|
|
50
|
+
.build()
|
|
51
|
+
|
|
52
|
+
@Test
|
|
53
|
+
fun `a download's job is found by the config ID in its extras`() {
|
|
54
|
+
val jobs = listOf(downloadJob(12345, "other"), downloadJob(23456, "a"))
|
|
55
|
+
|
|
56
|
+
assertEquals(23456, UIDTJobIds.jobIdFor("a", jobs))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@Test
|
|
60
|
+
fun `a download with no job has no ID`() {
|
|
61
|
+
assertNull(UIDTJobIds.jobIdFor("a", listOf(downloadJob(12345, "other"))))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
@Test
|
|
65
|
+
fun `another component's job is never mistaken for a download`() {
|
|
66
|
+
val jobs = listOf(foreignJob(10001))
|
|
67
|
+
|
|
68
|
+
assertNull(UIDTJobIds.jobIdFor("a", jobs))
|
|
69
|
+
assertNull(UIDTJobIds.configIdOf(jobs.first()))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@Test
|
|
73
|
+
fun `colliding downloads reserve different job IDs`() {
|
|
74
|
+
val first = UIDTJobIds.reserveJobId(colliding.first, emptyList())
|
|
75
|
+
val second = UIDTJobIds.reserveJobId(colliding.second, emptyList())
|
|
76
|
+
|
|
77
|
+
assertNotEquals(first, second)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@Test
|
|
81
|
+
fun `a reservation is not handed out twice`() {
|
|
82
|
+
val reserved = UIDTJobIds.reserveJobId(colliding.first, emptyList())
|
|
83
|
+
|
|
84
|
+
// The colliding download must not be offered the same ID while the first
|
|
85
|
+
// one is still on its way to the JobScheduler
|
|
86
|
+
assertNotEquals(reserved, UIDTJobIds.reserveJobId(colliding.second, emptyList()))
|
|
87
|
+
|
|
88
|
+
UIDTJobIds.endReservation(colliding.first)
|
|
89
|
+
|
|
90
|
+
// Once the first is scheduled (or was rejected), the ID is free again
|
|
91
|
+
UIDTJobIds.endReservation(colliding.second)
|
|
92
|
+
assertEquals(reserved, UIDTJobIds.reserveJobId(colliding.second, emptyList()))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@Test
|
|
96
|
+
fun `a download that already has a job keeps that job's ID`() {
|
|
97
|
+
val jobs = listOf(downloadJob(19999, "a"))
|
|
98
|
+
|
|
99
|
+
// Re-scheduling replaces the existing job instead of adding a second one
|
|
100
|
+
assertEquals(19999, UIDTJobIds.reserveJobId("a", jobs))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
@Test
|
|
104
|
+
fun `an ID held by a live job is never reserved`() {
|
|
105
|
+
val legacy = UIDTConstants.JOB_ID_BASE + (colliding.first.hashCode() and 0x7FFFFFFF) % 10000
|
|
106
|
+
val jobs = listOf(downloadJob(legacy, "somebody-else"))
|
|
107
|
+
|
|
108
|
+
assertNotEquals(legacy, UIDTJobIds.reserveJobId(colliding.first, jobs))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
@Test
|
|
112
|
+
fun `an ID held by another component's job is never reserved`() {
|
|
113
|
+
val legacy = UIDTConstants.JOB_ID_BASE + ("a".hashCode() and 0x7FFFFFFF) % 10000
|
|
114
|
+
val jobs = listOf(foreignJob(legacy))
|
|
115
|
+
|
|
116
|
+
// The old hash scheme would have stomped the host app's job here
|
|
117
|
+
assertNotEquals(legacy, UIDTJobIds.reserveJobId("a", jobs))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@Test
|
|
121
|
+
fun `a download with no live job reserves its legacy ID`() {
|
|
122
|
+
val legacy = UIDTConstants.JOB_ID_BASE + ("a".hashCode() and 0x7FFFFFFF) % 10000
|
|
123
|
+
|
|
124
|
+
// Nothing else holds it, so the download keeps the ID it always had -
|
|
125
|
+
// including a job scheduled by a version that derived IDs from the hash
|
|
126
|
+
assertEquals(legacy, UIDTJobIds.reserveJobId("a", emptyList()))
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
@Test
|
|
130
|
+
fun `cancelling falls back to the legacy ID when the pending jobs are unreadable`() {
|
|
131
|
+
val legacy = UIDTConstants.JOB_ID_BASE + ("a".hashCode() and 0x7FFFFFFF) % 10000
|
|
132
|
+
|
|
133
|
+
// Cancelling an ID that holds no job is a no-op, so the legacy guess is
|
|
134
|
+
// strictly better than not cancelling at all
|
|
135
|
+
assertEquals(legacy, UIDTJobIds.jobIdToCancel("a", null))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
@Test
|
|
139
|
+
fun `cancelling uses the live job's ID when it is known`() {
|
|
140
|
+
val jobs = listOf(downloadJob(17777, "a"))
|
|
141
|
+
|
|
142
|
+
assertEquals(17777, UIDTJobIds.jobIdToCancel("a", jobs))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
@Test
|
|
146
|
+
fun `ourJobs keeps only the library's download jobs`() {
|
|
147
|
+
val jobs = listOf(downloadJob(11111, "a"), foreignJob(11112), downloadJob(11113, "b"))
|
|
148
|
+
|
|
149
|
+
assertEquals(listOf(11111, 11113), UIDTJobIds.ourJobs(jobs).map { it.id })
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
@Test
|
|
153
|
+
fun `IDs stay unique across a full batch of downloads`() {
|
|
154
|
+
val ids = (1..150).map { UIDTJobIds.reserveJobId("download-$it", emptyList()) }
|
|
155
|
+
|
|
156
|
+
assertEquals(150, ids.toSet().size)
|
|
157
|
+
for (i in 1..150) UIDTJobIds.endReservation("download-$i")
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
package com.eko.uidt
|
|
2
|
+
|
|
3
|
+
import android.app.DownloadManager
|
|
4
|
+
import android.app.job.JobInfo
|
|
5
|
+
import android.app.job.JobScheduler
|
|
6
|
+
import android.content.ComponentName
|
|
7
|
+
import android.content.Context
|
|
8
|
+
import androidx.test.core.app.ApplicationProvider
|
|
9
|
+
import org.junit.Assert.assertEquals
|
|
10
|
+
import org.junit.Assert.assertFalse
|
|
11
|
+
import org.junit.Assert.assertNotEquals
|
|
12
|
+
import org.junit.Assert.assertNull
|
|
13
|
+
import org.junit.Assert.assertTrue
|
|
14
|
+
import org.junit.Before
|
|
15
|
+
import org.junit.Test
|
|
16
|
+
import org.junit.runner.RunWith
|
|
17
|
+
import org.robolectric.RobolectricTestRunner
|
|
18
|
+
import org.robolectric.annotation.Config
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The scheduling contract of the UIDT path: every download gets its own job, a
|
|
22
|
+
* batch big enough to exhaust the app's job quota degrades instead of crashing,
|
|
23
|
+
* and a job that hasn't started yet is still visible.
|
|
24
|
+
*/
|
|
25
|
+
@RunWith(RobolectricTestRunner::class)
|
|
26
|
+
@Config(sdk = [34])
|
|
27
|
+
class UIDTJobManagerTest {
|
|
28
|
+
|
|
29
|
+
private val context: Context get() = ApplicationProvider.getApplicationContext()
|
|
30
|
+
private val jobScheduler: JobScheduler
|
|
31
|
+
get() = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
|
|
32
|
+
|
|
33
|
+
/** Two distinct strings with the same hash code. */
|
|
34
|
+
private val colliding = Pair("Aa", "BB")
|
|
35
|
+
|
|
36
|
+
private var foreignJobId = 500000
|
|
37
|
+
|
|
38
|
+
@Before
|
|
39
|
+
fun clearScheduler() {
|
|
40
|
+
foreignJobId = 500000
|
|
41
|
+
jobScheduler.cancelAll()
|
|
42
|
+
UIDTJobRegistry.activeJobs.clear()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private fun schedule(configId: String, startByte: Long = 0, totalBytes: Long = -1): Boolean =
|
|
46
|
+
UIDTJobManager.scheduleDownload(
|
|
47
|
+
context = context,
|
|
48
|
+
configId = configId,
|
|
49
|
+
url = "https://example.com/$configId",
|
|
50
|
+
destination = "/tmp/$configId",
|
|
51
|
+
headers = mapOf("Authorization" to "Bearer $configId"),
|
|
52
|
+
startByte = startByte,
|
|
53
|
+
totalBytes = totalBytes,
|
|
54
|
+
metadata = """{"groupId":"group-1"}""",
|
|
55
|
+
isAllowedOverMetered = true
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
/** Fill the scheduler with jobs belonging to the rest of the app. */
|
|
59
|
+
private fun fillSchedulerWith(count: Int) {
|
|
60
|
+
repeat(count) {
|
|
61
|
+
jobScheduler.schedule(
|
|
62
|
+
JobInfo.Builder(foreignJobId++, ComponentName(context, "com.example.SomeOtherService"))
|
|
63
|
+
.setMinimumLatency(1000)
|
|
64
|
+
.build()
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private fun scheduledConfigIds(): List<String> =
|
|
70
|
+
jobScheduler.allPendingJobs.mapNotNull { UIDTJobIds.configIdOf(it) }
|
|
71
|
+
|
|
72
|
+
@Test
|
|
73
|
+
fun `scheduling a download creates a job carrying its config ID`() {
|
|
74
|
+
assertTrue(schedule("a"))
|
|
75
|
+
|
|
76
|
+
assertEquals(listOf("a"), scheduledConfigIds())
|
|
77
|
+
val job = jobScheduler.allPendingJobs.single()
|
|
78
|
+
assertEquals("https://example.com/a", job.extras.getString(UIDTConstants.KEY_URL))
|
|
79
|
+
assertTrue(job.isUserInitiated)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
@Test
|
|
83
|
+
fun `two downloads whose IDs collide each get their own job`() {
|
|
84
|
+
assertTrue(schedule(colliding.first))
|
|
85
|
+
assertTrue(schedule(colliding.second))
|
|
86
|
+
|
|
87
|
+
// The old hash-derived ID would have made the second replace the first
|
|
88
|
+
assertEquals(2, jobScheduler.allPendingJobs.size)
|
|
89
|
+
assertEquals(setOf(colliding.first, colliding.second), scheduledConfigIds().toSet())
|
|
90
|
+
assertNotEquals(
|
|
91
|
+
UIDTJobIds.jobIdFor(colliding.first, jobScheduler.allPendingJobs),
|
|
92
|
+
UIDTJobIds.jobIdFor(colliding.second, jobScheduler.allPendingJobs)
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Test
|
|
97
|
+
fun `re-scheduling a download replaces its own job instead of adding one`() {
|
|
98
|
+
assertTrue(schedule("a"))
|
|
99
|
+
val firstJobId = UIDTJobIds.jobIdFor("a", jobScheduler.allPendingJobs)
|
|
100
|
+
|
|
101
|
+
assertTrue(schedule("a", startByte = 4096))
|
|
102
|
+
|
|
103
|
+
assertEquals(1, jobScheduler.allPendingJobs.size)
|
|
104
|
+
assertEquals(firstJobId, UIDTJobIds.jobIdFor("a", jobScheduler.allPendingJobs))
|
|
105
|
+
assertEquals(4096L, jobScheduler.allPendingJobs.single().extras.getLong(UIDTConstants.KEY_START_BYTE))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
@Test
|
|
109
|
+
fun `a batch of downloads gets a job each`() {
|
|
110
|
+
val ids = (1..40).map { "download-$it" }
|
|
111
|
+
ids.forEach { assertTrue(schedule(it)) }
|
|
112
|
+
|
|
113
|
+
assertEquals(ids.toSet(), scheduledConfigIds().toSet())
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
@Test
|
|
117
|
+
fun `scheduling is refused once the app is out of job slots`() {
|
|
118
|
+
// The quota is app-wide, so somebody else's jobs can exhaust it
|
|
119
|
+
fillSchedulerWith(130)
|
|
120
|
+
|
|
121
|
+
assertFalse("the caller must fall back instead of crashing", schedule("a"))
|
|
122
|
+
assertTrue("no job may be left behind", scheduledConfigIds().isEmpty())
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
@Test
|
|
126
|
+
fun `headroom is kept for the rest of the app`() {
|
|
127
|
+
fillSchedulerWith(100)
|
|
128
|
+
|
|
129
|
+
// Well under the 150-job limit, but past the point where the library
|
|
130
|
+
// stops claiming slots
|
|
131
|
+
assertTrue(schedule("a"))
|
|
132
|
+
fillSchedulerWith(25)
|
|
133
|
+
assertFalse(schedule("b"))
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
@Test
|
|
137
|
+
fun `a download that already has a job can still be rescheduled at the quota`() {
|
|
138
|
+
assertTrue(schedule("a"))
|
|
139
|
+
fillSchedulerWith(130)
|
|
140
|
+
|
|
141
|
+
// Replacing its own job needs no free slot - this is how a resume works
|
|
142
|
+
assertTrue(schedule("a", startByte = 128))
|
|
143
|
+
assertEquals(128L, jobScheduler.allPendingJobs.single { UIDTJobIds.configIdOf(it) == "a" }
|
|
144
|
+
.extras.getLong(UIDTConstants.KEY_START_BYTE))
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
@Test
|
|
148
|
+
fun `a scheduled download is reported as pending with its byte counts`() {
|
|
149
|
+
assertTrue(schedule("a", startByte = 512, totalBytes = 2048))
|
|
150
|
+
|
|
151
|
+
val reported = UIDTJobManager.getScheduledJobs(context).single()
|
|
152
|
+
assertEquals("a", reported.id)
|
|
153
|
+
assertEquals(DownloadManager.STATUS_PENDING, reported.status)
|
|
154
|
+
assertEquals(512L, reported.bytesDownloaded)
|
|
155
|
+
assertEquals(2048L, reported.bytesTotal)
|
|
156
|
+
assertEquals("https://example.com/a", reported.url)
|
|
157
|
+
assertTrue(UIDTJobManager.isScheduledJob(context, "a"))
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
@Test
|
|
161
|
+
fun `only the library's own jobs are reported`() {
|
|
162
|
+
fillSchedulerWith(3)
|
|
163
|
+
assertTrue(schedule("a"))
|
|
164
|
+
|
|
165
|
+
assertEquals(listOf("a"), UIDTJobManager.getScheduledJobs(context).map { it.id })
|
|
166
|
+
assertFalse(UIDTJobManager.isScheduledJob(context, "not-a-download"))
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
@Test
|
|
170
|
+
fun `cancelling a download removes its job and leaves the others alone`() {
|
|
171
|
+
assertTrue(schedule(colliding.first))
|
|
172
|
+
assertTrue(schedule(colliding.second))
|
|
173
|
+
|
|
174
|
+
UIDTJobManager.cancelJob(context, colliding.first)
|
|
175
|
+
|
|
176
|
+
assertEquals(listOf(colliding.second), scheduledConfigIds())
|
|
177
|
+
assertNull(UIDTJobIds.jobIdFor(colliding.first, jobScheduler.allPendingJobs))
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@Test
|
|
181
|
+
fun `pausing a scheduled download returns what it needs to resume`() {
|
|
182
|
+
assertTrue(schedule("a", startByte = 1024, totalBytes = 8192))
|
|
183
|
+
|
|
184
|
+
val paused = UIDTJobManager.cancelPendingJob(context, "a")
|
|
185
|
+
|
|
186
|
+
assertEquals("https://example.com/a", paused?.url)
|
|
187
|
+
assertEquals(1024L, paused?.startByte)
|
|
188
|
+
assertEquals(8192L, paused?.totalBytes)
|
|
189
|
+
assertEquals("Bearer a", paused?.headers?.get("Authorization"))
|
|
190
|
+
assertTrue("the job must be gone", scheduledConfigIds().isEmpty())
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
package com.eko.uidt
|
|
2
|
+
|
|
3
|
+
import org.junit.Assert.assertEquals
|
|
4
|
+
import org.junit.Assert.assertNotEquals
|
|
5
|
+
import org.junit.Assert.assertTrue
|
|
6
|
+
import org.junit.Before
|
|
7
|
+
import org.junit.Test
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Notification IDs used to be `BASE + hash(configId) % 100000`, so two downloads
|
|
11
|
+
* whose config IDs hashed together shared one notification. These pin the
|
|
12
|
+
* behaviour that replaced it.
|
|
13
|
+
*/
|
|
14
|
+
class UIDTNotificationIdsTest {
|
|
15
|
+
|
|
16
|
+
/** Two distinct strings with the same hash code - the collision this fixes. */
|
|
17
|
+
private val colliding = Pair("Aa", "BB")
|
|
18
|
+
|
|
19
|
+
@Before
|
|
20
|
+
fun releaseEverything() {
|
|
21
|
+
for (id in listOf(colliding.first, colliding.second, "a", "b", "c")) {
|
|
22
|
+
UIDTNotificationIds.release(id)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@Test
|
|
27
|
+
fun `the two colliding config IDs really do collide`() {
|
|
28
|
+
assertEquals(colliding.first.hashCode(), colliding.second.hashCode())
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@Test
|
|
32
|
+
fun `colliding downloads get different notification IDs`() {
|
|
33
|
+
val first = UIDTNotificationIds.progressIdFor(colliding.first)
|
|
34
|
+
val second = UIDTNotificationIds.progressIdFor(colliding.second)
|
|
35
|
+
|
|
36
|
+
assertNotEquals(first, second)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@Test
|
|
40
|
+
fun `a download keeps the same ID while it is live`() {
|
|
41
|
+
val first = UIDTNotificationIds.progressIdFor("a")
|
|
42
|
+
|
|
43
|
+
assertEquals(first, UIDTNotificationIds.progressIdFor("a"))
|
|
44
|
+
assertEquals(first, UIDTNotificationIds.progressIdFor("a"))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Test
|
|
48
|
+
fun `the first download of a colliding pair keeps the legacy ID`() {
|
|
49
|
+
val legacy = UIDTConstants.NOTIFICATION_ID_BASE + (colliding.first.hashCode() and 0x7FFFFFFF) % 100000
|
|
50
|
+
|
|
51
|
+
assertEquals(legacy, UIDTNotificationIds.progressIdFor(colliding.first))
|
|
52
|
+
// Only the download that arrived second has to move
|
|
53
|
+
assertNotEquals(legacy, UIDTNotificationIds.progressIdFor(colliding.second))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
@Test
|
|
57
|
+
fun `a released ID goes back to the pool`() {
|
|
58
|
+
val legacy = UIDTNotificationIds.progressIdFor(colliding.first)
|
|
59
|
+
val moved = UIDTNotificationIds.progressIdFor(colliding.second)
|
|
60
|
+
assertNotEquals(legacy, moved)
|
|
61
|
+
|
|
62
|
+
// Both downloads end, so both slots are free again
|
|
63
|
+
UIDTNotificationIds.release(colliding.first)
|
|
64
|
+
UIDTNotificationIds.release(colliding.second)
|
|
65
|
+
|
|
66
|
+
// The one that had to move gets the slot its hash points at this time
|
|
67
|
+
assertEquals(legacy, UIDTNotificationIds.progressIdFor(colliding.second))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@Test
|
|
71
|
+
fun `the finished notification ID is in its own range at the same offset`() {
|
|
72
|
+
val progress = UIDTNotificationIds.progressIdFor(colliding.second)
|
|
73
|
+
val finished = UIDTNotificationIds.finishedIdFor(colliding.second)
|
|
74
|
+
|
|
75
|
+
assertEquals(
|
|
76
|
+
progress - UIDTConstants.NOTIFICATION_ID_BASE,
|
|
77
|
+
finished - UIDTConstants.FINISHED_NOTIFICATION_ID_BASE
|
|
78
|
+
)
|
|
79
|
+
// The ranges are disjoint, so a completion notification can never land on
|
|
80
|
+
// another download's in-progress one
|
|
81
|
+
assertTrue(finished > UIDTConstants.NOTIFICATION_ID_BASE + 100000)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@Test
|
|
85
|
+
fun `colliding downloads get different finished IDs too`() {
|
|
86
|
+
assertNotEquals(
|
|
87
|
+
UIDTNotificationIds.finishedIdFor(colliding.first),
|
|
88
|
+
UIDTNotificationIds.finishedIdFor(colliding.second)
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@Test
|
|
93
|
+
fun `IDs stay unique across many downloads`() {
|
|
94
|
+
val ids = (1..500).map { UIDTNotificationIds.progressIdFor("download-$it") }
|
|
95
|
+
|
|
96
|
+
assertEquals(500, ids.toSet().size)
|
|
97
|
+
for (i in 1..500) UIDTNotificationIds.release("download-$i")
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -76,6 +76,8 @@ export interface Spec extends TurboModule {
|
|
|
76
76
|
setNotificationGroupingConfig?(config: {
|
|
77
77
|
enabled: boolean;
|
|
78
78
|
showNotificationsEnabled: boolean;
|
|
79
|
+
showCompletionNotification: boolean;
|
|
80
|
+
showCancelAction: boolean;
|
|
79
81
|
mode: string;
|
|
80
82
|
texts: UnsafeObject;
|
|
81
83
|
}): void;
|
package/lib/config.d.ts
CHANGED
|
@@ -15,6 +15,8 @@ interface ConfigState {
|
|
|
15
15
|
maxParallelDownloads: number;
|
|
16
16
|
allowsCellularAccess: boolean;
|
|
17
17
|
showNotificationsEnabled: boolean;
|
|
18
|
+
showCompletionNotification: boolean;
|
|
19
|
+
showCancelAction: boolean;
|
|
18
20
|
notificationsGrouping: NotificationsGroupingConfig & {
|
|
19
21
|
mode: NotificationGroupingMode;
|
|
20
22
|
};
|
package/lib/config.js
CHANGED
|
@@ -15,6 +15,7 @@ exports.DEFAULT_NOTIFICATION_TEXTS = {
|
|
|
15
15
|
downloadProgress: 'Downloading... {progress}%',
|
|
16
16
|
downloadPaused: 'Paused',
|
|
17
17
|
downloadFinished: 'Download complete',
|
|
18
|
+
downloadCancel: 'Cancel',
|
|
18
19
|
groupTitle: 'Downloads',
|
|
19
20
|
groupText: (count) => `${count} download${count !== 1 ? 's' : ''} in progress`,
|
|
20
21
|
};
|
|
@@ -27,6 +28,8 @@ exports.config = {
|
|
|
27
28
|
maxParallelDownloads: exports.DEFAULT_MAX_PARALLEL_DOWNLOADS,
|
|
28
29
|
allowsCellularAccess: exports.DEFAULT_ALLOWS_CELLULAR_ACCESS,
|
|
29
30
|
showNotificationsEnabled: false,
|
|
31
|
+
showCompletionNotification: false,
|
|
32
|
+
showCancelAction: false,
|
|
30
33
|
iosDataProtection: exports.DEFAULT_IOS_DATA_PROTECTION,
|
|
31
34
|
notificationsGrouping: {
|
|
32
35
|
enabled: false,
|
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
|
@@ -262,8 +262,8 @@ function initializeEventListeners() {
|
|
|
262
262
|
}
|
|
263
263
|
// Event listeners are now initialized lazily when ensureNativeModuleInitialized() is called
|
|
264
264
|
// This ensures the bridge is ready before any native module access
|
|
265
|
-
function setConfig({ headers = {}, progressInterval = config_1.DEFAULT_PROGRESS_INTERVAL, progressMinBytes = config_1.DEFAULT_PROGRESS_MIN_BYTES, isLogsEnabled = false, logCallback, maxParallelDownloads, allowsCellularAccess, showNotificationsEnabled, notificationsGrouping, iosDataProtection, }) {
|
|
266
|
-
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;
|
|
267
267
|
config_1.config.headers = headers;
|
|
268
268
|
if (iosDataProtection !== undefined)
|
|
269
269
|
config_1.config.iosDataProtection = iosDataProtection;
|
|
@@ -285,6 +285,11 @@ function setConfig({ headers = {}, progressInterval = config_1.DEFAULT_PROGRESS_
|
|
|
285
285
|
// Update showNotificationsEnabled
|
|
286
286
|
if (showNotificationsEnabled !== undefined)
|
|
287
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;
|
|
288
293
|
// Update notification grouping config
|
|
289
294
|
if (notificationsGrouping !== undefined)
|
|
290
295
|
config_1.config.notificationsGrouping = {
|
|
@@ -312,6 +317,8 @@ function setConfig({ headers = {}, progressInterval = config_1.DEFAULT_PROGRESS_
|
|
|
312
317
|
nativeModule.setNotificationGroupingConfig({
|
|
313
318
|
enabled: config_1.config.notificationsGrouping.enabled,
|
|
314
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,
|
|
315
322
|
mode: config_1.config.notificationsGrouping.mode,
|
|
316
323
|
texts: (0, notifications_1.getNotificationTextsForNative)(),
|
|
317
324
|
});
|
package/lib/notifications.js
CHANGED
|
@@ -17,7 +17,7 @@ function getGroupText(count) {
|
|
|
17
17
|
* Get notification texts config for native side (serializable).
|
|
18
18
|
*/
|
|
19
19
|
function getNotificationTextsForNative() {
|
|
20
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
20
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
21
21
|
const texts = (_a = config_1.config.notificationsGrouping.texts) !== null && _a !== void 0 ? _a : config_1.DEFAULT_NOTIFICATION_TEXTS;
|
|
22
22
|
return {
|
|
23
23
|
downloadTitle: (_b = texts.downloadTitle) !== null && _b !== void 0 ? _b : config_1.DEFAULT_NOTIFICATION_TEXTS.downloadTitle,
|
|
@@ -25,10 +25,11 @@ function getNotificationTextsForNative() {
|
|
|
25
25
|
downloadProgress: (_d = texts.downloadProgress) !== null && _d !== void 0 ? _d : config_1.DEFAULT_NOTIFICATION_TEXTS.downloadProgress,
|
|
26
26
|
downloadPaused: (_e = texts.downloadPaused) !== null && _e !== void 0 ? _e : config_1.DEFAULT_NOTIFICATION_TEXTS.downloadPaused,
|
|
27
27
|
downloadFinished: (_f = texts.downloadFinished) !== null && _f !== void 0 ? _f : config_1.DEFAULT_NOTIFICATION_TEXTS.downloadFinished,
|
|
28
|
-
|
|
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,
|
|
29
30
|
// For native side, we send a pattern with {count} placeholder
|
|
30
31
|
groupText: typeof texts.groupText === 'function'
|
|
31
32
|
? '{count} download(s) in progress' // Default pattern if function provided
|
|
32
|
-
: ((
|
|
33
|
+
: ((_j = texts.groupText) !== null && _j !== void 0 ? _j : '{count} download(s) in progress'),
|
|
33
34
|
};
|
|
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.1",
|
|
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"
|
|
@@ -27,7 +27,20 @@ Pod::Spec.new do |s|
|
|
|
27
27
|
# resolve a newer MMKV when another pod (e.g. react-native-mmkv, which pins an
|
|
28
28
|
# exact MMKVCore) requires one - we just don't impose our own higher floor.
|
|
29
29
|
# See https://github.com/kesha-antonov/react-native-background-downloader/issues/162
|
|
30
|
-
|
|
30
|
+
#
|
|
31
|
+
# 2.4.1 is excluded: MMKVCore 2.4.1 fails to compile on Apple platforms with
|
|
32
|
+
# "use of undeclared identifier 'memset_s'" in Core/aes/AESCrypt.cpp. It defines
|
|
33
|
+
# __STDC_WANT_LIB_EXT1__ at the top of the .cpp, but CocoaPods force-includes the
|
|
34
|
+
# generated prefix header (-include ...-prefix.pch) first, so <string.h> is already
|
|
35
|
+
# parsed by then and Darwin never declares memset_s. Only that single release is
|
|
36
|
+
# blocked, so a fixed 2.4.2+ will be picked up without another release here.
|
|
37
|
+
# See https://github.com/Tencent/MMKV/issues/1675
|
|
38
|
+
# and https://github.com/kesha-antonov/react-native-background-downloader/issues/175
|
|
39
|
+
s.dependency 'MMKV', '>= 1.2.0', '!= 2.4.1'
|
|
40
|
+
# MMKVCore must be excluded explicitly as well: the broken source file lives in
|
|
41
|
+
# MMKVCore, and MMKV 2.4.0 depends on 'MMKVCore (~> 2.4.0)', which would still
|
|
42
|
+
# resolve to the broken 2.4.1 on its own.
|
|
43
|
+
s.dependency 'MMKVCore', '!= 2.4.1'
|
|
31
44
|
|
|
32
45
|
# Enable codegen for new architecture
|
|
33
46
|
if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
|
|
@@ -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
|