@kesha-antonov/react-native-background-downloader 4.5.9 → 4.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -3
- package/android/build.gradle +17 -0
- package/android/src/main/AndroidManifest.xml +24 -0
- package/android/src/main/java/com/eko/DownloadConstants.kt +14 -2
- package/android/src/main/java/com/eko/Downloader.kt +77 -12
- package/android/src/main/java/com/eko/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/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
|
@@ -16,6 +16,9 @@ data class JobState(
|
|
|
16
16
|
var notificationId: Int,
|
|
17
17
|
val groupId: String = "",
|
|
18
18
|
val groupName: String = "",
|
|
19
|
+
// Optional per-download notification title (read from metadata.notificationTitle).
|
|
20
|
+
// When non-empty, overrides both groupName and the config's default downloadTitle.
|
|
21
|
+
val customTitle: String = "",
|
|
19
22
|
var lastNotifiedProgress: Int = -1,
|
|
20
23
|
var lastNotificationUpdateTime: Long = 0,
|
|
21
24
|
// Track download progress for summary notification
|
|
@@ -37,6 +40,12 @@ enum class NotificationGroupingMode {
|
|
|
37
40
|
data class NotificationConfig(
|
|
38
41
|
var groupingEnabled: Boolean = false,
|
|
39
42
|
var showNotificationsEnabled: Boolean = false,
|
|
43
|
+
// Post a "download complete" notification when a download finishes.
|
|
44
|
+
// Opt-in: it alerts on its own channel, unlike the silent progress one.
|
|
45
|
+
var showCompletionNotification: Boolean = false,
|
|
46
|
+
// Add a Cancel button to the progress notification. Opt-in: cancelling
|
|
47
|
+
// from the shade surfaces in JS as a downloadFailed the app has to handle.
|
|
48
|
+
var showCancelAction: Boolean = false,
|
|
40
49
|
var mode: NotificationGroupingMode = NotificationGroupingMode.INDIVIDUAL,
|
|
41
50
|
var updateInterval: Long = 500L,
|
|
42
51
|
val texts: MutableMap<String, String> = mutableMapOf(
|
|
@@ -45,6 +54,7 @@ data class NotificationConfig(
|
|
|
45
54
|
"downloadProgress" to "Downloading... {progress}%",
|
|
46
55
|
"downloadPaused" to "Paused",
|
|
47
56
|
"downloadFinished" to "Download complete",
|
|
57
|
+
"downloadCancel" to "Cancel",
|
|
48
58
|
"groupTitle" to "Downloads",
|
|
49
59
|
"groupText" to "{count} download(s) in progress"
|
|
50
60
|
)
|
|
@@ -83,7 +93,8 @@ data class GroupProgress(
|
|
|
83
93
|
object UIDTConstants {
|
|
84
94
|
const val TAG = "UIDTDownloadJobService"
|
|
85
95
|
|
|
86
|
-
//
|
|
96
|
+
// Start of the job-ID range owned by the library. IDs inside it are handed
|
|
97
|
+
// out per download by UIDTJobIds.
|
|
87
98
|
const val JOB_ID_BASE = 10000
|
|
88
99
|
|
|
89
100
|
// PersistableBundle keys
|
|
@@ -104,6 +115,11 @@ object UIDTConstants {
|
|
|
104
115
|
// Notification channel for UIDT jobs (ultra-silent for summaryOnly mode)
|
|
105
116
|
const val NOTIFICATION_CHANNEL_ULTRA_SILENT_ID = "uidt_download_channel_ultra_silent"
|
|
106
117
|
|
|
118
|
+
// Notification channel for the one-shot "download complete" notification.
|
|
119
|
+
// Uses IMPORTANCE_DEFAULT so completion is actually surfaced to the user
|
|
120
|
+
// (the progress channel is IMPORTANCE_LOW and never alerts).
|
|
121
|
+
const val NOTIFICATION_CHANNEL_FINISHED_ID = "uidt_download_channel_finished"
|
|
122
|
+
|
|
107
123
|
// Notification group for grouping all download notifications together
|
|
108
124
|
const val NOTIFICATION_GROUP_KEY = "com.eko.DOWNLOAD_GROUP"
|
|
109
125
|
|
|
@@ -112,6 +128,12 @@ object UIDTConstants {
|
|
|
112
128
|
|
|
113
129
|
// Base for individual notification IDs
|
|
114
130
|
const val NOTIFICATION_ID_BASE = 20000
|
|
131
|
+
|
|
132
|
+
// Base for one-shot "download complete" notification IDs. Kept in a
|
|
133
|
+
// disjoint range from NOTIFICATION_ID_BASE (which spans 20000..119999 via
|
|
134
|
+
// hash % 100000) so a finished notification can never collide with another
|
|
135
|
+
// download's in-progress notification.
|
|
136
|
+
const val FINISHED_NOTIFICATION_ID_BASE = 200000
|
|
115
137
|
}
|
|
116
138
|
|
|
117
139
|
/**
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
package com.eko.uidt
|
|
2
|
+
|
|
3
|
+
import android.os.Build
|
|
4
|
+
import androidx.annotation.RequiresApi
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Assigns the notification IDs used for download notifications.
|
|
8
|
+
*
|
|
9
|
+
* IDs used to be derived as `NOTIFICATION_ID_BASE + hash(configId) % RANGE`, so
|
|
10
|
+
* two downloads whose config IDs hash to the same slot shared one notification:
|
|
11
|
+
* their progress updates overwrote each other, and whichever ended first took
|
|
12
|
+
* the notification down for the other - on the UIDT path for good, since the
|
|
13
|
+
* job's end policy removes it. Across a batch of 150 downloads that is about a
|
|
14
|
+
* one-in-ten chance.
|
|
15
|
+
*
|
|
16
|
+
* The hash is still where the search starts, so a download keeps the ID it has
|
|
17
|
+
* always had unless another live download already holds it. Only the collided
|
|
18
|
+
* one moves, and only for as long as it is running.
|
|
19
|
+
*/
|
|
20
|
+
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
|
|
21
|
+
internal object UIDTNotificationIds {
|
|
22
|
+
|
|
23
|
+
/** Size of each notification-ID range (progress, and the finished one). */
|
|
24
|
+
private const val RANGE = 100000
|
|
25
|
+
|
|
26
|
+
private val lock = Any()
|
|
27
|
+
|
|
28
|
+
/** configId -> offset into the range, held while the download has notifications. */
|
|
29
|
+
private val assigned = mutableMapOf<String, Int>()
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The in-progress notification ID for a download - stable for as long as the
|
|
33
|
+
* download is live, and only ever held by one download at a time.
|
|
34
|
+
*/
|
|
35
|
+
fun progressIdFor(configId: String): Int =
|
|
36
|
+
UIDTConstants.NOTIFICATION_ID_BASE + offsetFor(configId)
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The ID for the one-shot "download complete" notification. Taken from a
|
|
40
|
+
* disjoint range at the same offset, so it inherits the same uniqueness and
|
|
41
|
+
* can never collide with an in-progress notification.
|
|
42
|
+
*/
|
|
43
|
+
fun finishedIdFor(configId: String): Int =
|
|
44
|
+
UIDTConstants.FINISHED_NOTIFICATION_ID_BASE + offsetFor(configId)
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Give the offset back once the download's notifications are gone. Called on
|
|
48
|
+
* the paths that remove them - completion, failure and cancellation.
|
|
49
|
+
*/
|
|
50
|
+
fun release(configId: String) {
|
|
51
|
+
synchronized(lock) { assigned.remove(configId) }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private fun offsetFor(configId: String): Int {
|
|
55
|
+
synchronized(lock) {
|
|
56
|
+
assigned[configId]?.let { return it }
|
|
57
|
+
|
|
58
|
+
val taken = assigned.values.toSet()
|
|
59
|
+
val start = (configId.hashCode() and 0x7FFFFFFF) % RANGE
|
|
60
|
+
for (probe in 0 until RANGE) {
|
|
61
|
+
val candidate = (start + probe) % RANGE
|
|
62
|
+
if (candidate in taken) continue
|
|
63
|
+
assigned[configId] = candidate
|
|
64
|
+
return candidate
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Every offset in use at once is not a real state (it would mean
|
|
68
|
+
// 100000 live downloads), but never return a colliding ID silently
|
|
69
|
+
assigned[configId] = start
|
|
70
|
+
return start
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -3,14 +3,21 @@ package com.eko.uidt
|
|
|
3
3
|
import android.app.Notification
|
|
4
4
|
import android.app.NotificationChannel
|
|
5
5
|
import android.app.NotificationManager
|
|
6
|
+
import android.app.PendingIntent
|
|
6
7
|
import android.app.job.JobService
|
|
7
8
|
import android.content.Context
|
|
9
|
+
import android.content.Intent
|
|
10
|
+
import android.content.pm.PackageManager
|
|
11
|
+
import android.net.Uri
|
|
8
12
|
import android.os.Build
|
|
9
13
|
import androidx.annotation.RequiresApi
|
|
10
14
|
import androidx.core.app.NotificationCompat
|
|
15
|
+
import androidx.core.content.FileProvider
|
|
11
16
|
import com.eko.RNBackgroundDownloaderModuleImpl
|
|
12
17
|
import com.eko.UIDTDownloadJobService
|
|
13
18
|
import com.eko.utils.ProgressUtils
|
|
19
|
+
import java.io.File
|
|
20
|
+
import java.net.URLConnection
|
|
14
21
|
|
|
15
22
|
/**
|
|
16
23
|
* Manages all notification-related operations for UIDT downloads.
|
|
@@ -23,13 +30,19 @@ object UIDTNotificationManager {
|
|
|
23
30
|
get() = UIDTJobRegistry.notificationConfig
|
|
24
31
|
|
|
25
32
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* even across app restarts.
|
|
33
|
+
* The notification ID for a download. Stable for as long as the download is
|
|
34
|
+
* live, and never shared with another live download - see [UIDTNotificationIds].
|
|
29
35
|
*/
|
|
30
|
-
fun getNotificationIdForConfig(configId: String): Int
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
fun getNotificationIdForConfig(configId: String): Int =
|
|
37
|
+
UIDTNotificationIds.progressIdFor(configId)
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Notification ID for the one-shot "download complete" notification.
|
|
41
|
+
* Lives in a disjoint range from [getNotificationIdForConfig] so it can
|
|
42
|
+
* never collide with another download's in-progress notification.
|
|
43
|
+
*/
|
|
44
|
+
fun getFinishedNotificationIdForConfig(configId: String): Int =
|
|
45
|
+
UIDTNotificationIds.finishedIdFor(configId)
|
|
33
46
|
|
|
34
47
|
/**
|
|
35
48
|
* Create notification channels for UIDT jobs.
|
|
@@ -74,7 +87,55 @@ object UIDTNotificationManager {
|
|
|
74
87
|
setSound(null, null)
|
|
75
88
|
}
|
|
76
89
|
notificationManager.createNotificationChannel(ultraSilentChannel)
|
|
90
|
+
|
|
91
|
+
// Channel for the one-shot "download complete" notification
|
|
92
|
+
// (IMPORTANCE_DEFAULT so it actually alerts, unlike the progress channel)
|
|
93
|
+
val finishedChannel = NotificationChannel(
|
|
94
|
+
UIDTConstants.NOTIFICATION_CHANNEL_FINISHED_ID,
|
|
95
|
+
"Download Complete",
|
|
96
|
+
NotificationManager.IMPORTANCE_DEFAULT
|
|
97
|
+
).apply {
|
|
98
|
+
description = "Notifies when a background download has finished"
|
|
99
|
+
}
|
|
100
|
+
notificationManager.createNotificationChannel(finishedChannel)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Build a PendingIntent that fires the in-notification "Cancel" action.
|
|
106
|
+
* The receiver lives in [CancelDownloadReceiver] and calls
|
|
107
|
+
* [UIDTJobManager.cancelJob] when triggered.
|
|
108
|
+
*/
|
|
109
|
+
/**
|
|
110
|
+
* Add the "Cancel" action to [builder] when the app opted into it via
|
|
111
|
+
* `showCancelAction`. The label goes through the notification texts so it
|
|
112
|
+
* stays localizable like every other string this library renders.
|
|
113
|
+
*/
|
|
114
|
+
private fun applyCancelAction(builder: NotificationCompat.Builder, context: Context, configId: String) {
|
|
115
|
+
if (!config.showCancelAction) return
|
|
116
|
+
|
|
117
|
+
builder.addAction(
|
|
118
|
+
android.R.drawable.ic_menu_close_clear_cancel,
|
|
119
|
+
config.getText("downloadCancel"),
|
|
120
|
+
buildCancelPendingIntent(context, configId),
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private fun buildCancelPendingIntent(context: Context, configId: String): PendingIntent {
|
|
125
|
+
val intent = Intent(context, CancelDownloadReceiver::class.java).apply {
|
|
126
|
+
action = CancelDownloadReceiver.ACTION_CANCEL_DOWNLOAD
|
|
127
|
+
setPackage(context.packageName)
|
|
128
|
+
putExtra(CancelDownloadReceiver.EXTRA_CONFIG_ID, configId)
|
|
77
129
|
}
|
|
130
|
+
// Use stable notification id as request code so subsequent updates
|
|
131
|
+
// reuse the same PendingIntent slot.
|
|
132
|
+
val requestCode = getNotificationIdForConfig(configId)
|
|
133
|
+
return PendingIntent.getBroadcast(
|
|
134
|
+
context,
|
|
135
|
+
requestCode,
|
|
136
|
+
intent,
|
|
137
|
+
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
|
138
|
+
)
|
|
78
139
|
}
|
|
79
140
|
|
|
80
141
|
/**
|
|
@@ -84,7 +145,8 @@ object UIDTNotificationManager {
|
|
|
84
145
|
context: Context,
|
|
85
146
|
configId: String,
|
|
86
147
|
groupId: String = "",
|
|
87
|
-
groupName: String = ""
|
|
148
|
+
groupName: String = "",
|
|
149
|
+
customTitle: String = ""
|
|
88
150
|
): Notification {
|
|
89
151
|
val isSummaryOnlyMode = config.mode == NotificationGroupingMode.SUMMARY_ONLY
|
|
90
152
|
|
|
@@ -123,10 +185,10 @@ object UIDTNotificationManager {
|
|
|
123
185
|
.build()
|
|
124
186
|
}
|
|
125
187
|
|
|
126
|
-
val title =
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
config.getText("downloadTitle")
|
|
188
|
+
val title = when {
|
|
189
|
+
customTitle.isNotEmpty() -> customTitle
|
|
190
|
+
config.groupingEnabled && groupName.isNotEmpty() -> groupName
|
|
191
|
+
else -> config.getText("downloadTitle")
|
|
130
192
|
}
|
|
131
193
|
val startingText = config.getText("downloadStarting")
|
|
132
194
|
|
|
@@ -140,6 +202,8 @@ object UIDTNotificationManager {
|
|
|
140
202
|
.setShowWhen(false)
|
|
141
203
|
.setProgress(0, 0, true)
|
|
142
204
|
|
|
205
|
+
applyCancelAction(builder, context, configId)
|
|
206
|
+
|
|
143
207
|
// Apply grouping when enabled and groupId is provided
|
|
144
208
|
if (config.groupingEnabled && groupId.isNotEmpty()) {
|
|
145
209
|
val groupKey = "${UIDTConstants.NOTIFICATION_GROUP_KEY}_$groupId"
|
|
@@ -154,6 +218,7 @@ object UIDTNotificationManager {
|
|
|
154
218
|
*/
|
|
155
219
|
fun updateProgressNotification(
|
|
156
220
|
context: Context,
|
|
221
|
+
configId: String,
|
|
157
222
|
jobState: JobState,
|
|
158
223
|
bytesDownloaded: Long,
|
|
159
224
|
bytesTotal: Long
|
|
@@ -173,10 +238,10 @@ object UIDTNotificationManager {
|
|
|
173
238
|
|
|
174
239
|
val progress = ProgressUtils.calculateProgress(bytesDownloaded, bytesTotal)
|
|
175
240
|
|
|
176
|
-
val title =
|
|
177
|
-
jobState.
|
|
178
|
-
|
|
179
|
-
config.getText("downloadTitle")
|
|
241
|
+
val title = when {
|
|
242
|
+
jobState.customTitle.isNotEmpty() -> jobState.customTitle
|
|
243
|
+
config.groupingEnabled && jobState.groupName.isNotEmpty() -> jobState.groupName
|
|
244
|
+
else -> config.getText("downloadTitle")
|
|
180
245
|
}
|
|
181
246
|
|
|
182
247
|
val progressText = config.getText("downloadProgress", "progress" to progress)
|
|
@@ -191,6 +256,8 @@ object UIDTNotificationManager {
|
|
|
191
256
|
.setShowWhen(false)
|
|
192
257
|
.setProgress(100, progress, bytesTotal <= 0)
|
|
193
258
|
|
|
259
|
+
applyCancelAction(builder, context, configId)
|
|
260
|
+
|
|
194
261
|
// Apply grouping when enabled
|
|
195
262
|
if (config.groupingEnabled && jobState.groupId.isNotEmpty()) {
|
|
196
263
|
val groupKey = "${UIDTConstants.NOTIFICATION_GROUP_KEY}_${jobState.groupId}"
|
|
@@ -249,10 +316,10 @@ object UIDTNotificationManager {
|
|
|
249
316
|
// Update tracking state so next onProgress knows current state
|
|
250
317
|
jobState.lastNotifiedProgress = progress
|
|
251
318
|
|
|
252
|
-
val title =
|
|
253
|
-
jobState.
|
|
254
|
-
|
|
255
|
-
config.getText("downloadTitle")
|
|
319
|
+
val title = when {
|
|
320
|
+
jobState.customTitle.isNotEmpty() -> jobState.customTitle
|
|
321
|
+
config.groupingEnabled && jobState.groupName.isNotEmpty() -> jobState.groupName
|
|
322
|
+
else -> config.getText("downloadTitle")
|
|
256
323
|
}
|
|
257
324
|
|
|
258
325
|
val builder = NotificationCompat.Builder(context, UIDTConstants.NOTIFICATION_CHANNEL_ID)
|
|
@@ -451,6 +518,7 @@ object UIDTNotificationManager {
|
|
|
451
518
|
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
452
519
|
val notificationId = getNotificationIdForConfig(configId)
|
|
453
520
|
notificationManager.cancel(notificationId)
|
|
521
|
+
UIDTNotificationIds.release(configId)
|
|
454
522
|
RNBackgroundDownloaderModuleImpl.logD(UIDTConstants.TAG, "Cancelled notification $notificationId for $configId")
|
|
455
523
|
}
|
|
456
524
|
|
|
@@ -514,4 +582,163 @@ object UIDTNotificationManager {
|
|
|
514
582
|
.setSmallIcon(android.R.drawable.stat_sys_download)
|
|
515
583
|
.build()
|
|
516
584
|
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Show a one-shot "download complete" notification that persists after the
|
|
588
|
+
* UIDT job is removed by the system. The title is [customTitle] when the
|
|
589
|
+
* download carries one, otherwise the configured "downloadFinished" text;
|
|
590
|
+
* the body is the supplied [fileName]. Posted via NotificationManager (not
|
|
591
|
+
* tied to the JobService) so it survives the service shutdown.
|
|
592
|
+
*
|
|
593
|
+
* Requires the app to opt in with `showCompletionNotification`, since this
|
|
594
|
+
* one alerts on its own IMPORTANCE_DEFAULT channel.
|
|
595
|
+
*
|
|
596
|
+
* Nothing is posted in summaryOnly mode - that mode exists so a batch of
|
|
597
|
+
* downloads produces a single notification, and one completion notification
|
|
598
|
+
* per finished file would defeat it.
|
|
599
|
+
*
|
|
600
|
+
* Tapping the notification fires ACTION_VIEW with a FileProvider content
|
|
601
|
+
* URI for [destination] so the system file viewer / Files app can open it.
|
|
602
|
+
*/
|
|
603
|
+
fun showFinishedNotification(
|
|
604
|
+
context: Context,
|
|
605
|
+
configId: String,
|
|
606
|
+
destination: String,
|
|
607
|
+
fileName: String,
|
|
608
|
+
groupId: String = "",
|
|
609
|
+
customTitle: String = "",
|
|
610
|
+
) {
|
|
611
|
+
if (!config.showNotificationsEnabled || !config.showCompletionNotification) return
|
|
612
|
+
|
|
613
|
+
val isSummaryOnlyMode = config.mode == NotificationGroupingMode.SUMMARY_ONLY
|
|
614
|
+
if (isSummaryOnlyMode && config.groupingEnabled && groupId.isNotEmpty()) {
|
|
615
|
+
RNBackgroundDownloaderModuleImpl.logD(
|
|
616
|
+
UIDTConstants.TAG,
|
|
617
|
+
"Skipping finished notification for $configId (summaryOnly mode)",
|
|
618
|
+
)
|
|
619
|
+
return
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
createNotificationChannels(context)
|
|
623
|
+
|
|
624
|
+
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
625
|
+
// Use an id from a disjoint range so it is neither removed by the REMOVE
|
|
626
|
+
// policy applied to the ongoing notification nor collides with another
|
|
627
|
+
// download's in-progress notification.
|
|
628
|
+
val notificationId = getFinishedNotificationIdForConfig(configId)
|
|
629
|
+
|
|
630
|
+
// Same precedence as the progress notification: a per-download title
|
|
631
|
+
// wins over the configured default text.
|
|
632
|
+
val title = customTitle.ifEmpty { config.getText("downloadFinished") }
|
|
633
|
+
|
|
634
|
+
// Build a PendingIntent that lets the user open the saved file with the
|
|
635
|
+
// system viewer (Files app / Drive / Extract...). We resolve the file
|
|
636
|
+
// through the library-owned FileProvider first (declared in this
|
|
637
|
+
// library's manifest, so it works with no host-app configuration) and
|
|
638
|
+
// only fall back to auto-detecting a host-app provider if that fails.
|
|
639
|
+
// If neither produces a URI the tap action is simply omitted and the
|
|
640
|
+
// notification is still posted.
|
|
641
|
+
val pendingIntent: PendingIntent? = try {
|
|
642
|
+
val file = File(destination)
|
|
643
|
+
val uri = resolveContentUri(context, file)
|
|
644
|
+
val mime = URLConnection.guessContentTypeFromName(fileName) ?: "*/*"
|
|
645
|
+
val viewIntent = Intent(Intent.ACTION_VIEW).apply {
|
|
646
|
+
setDataAndType(uri, mime)
|
|
647
|
+
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
648
|
+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
649
|
+
}
|
|
650
|
+
val chooser = Intent.createChooser(viewIntent, title).apply {
|
|
651
|
+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
652
|
+
}
|
|
653
|
+
PendingIntent.getActivity(
|
|
654
|
+
context,
|
|
655
|
+
notificationId,
|
|
656
|
+
chooser,
|
|
657
|
+
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
|
658
|
+
)
|
|
659
|
+
} catch (e: Exception) {
|
|
660
|
+
RNBackgroundDownloaderModuleImpl.logW(UIDTConstants.TAG, "Failed to build pendingIntent for finished noti: ${e.message}")
|
|
661
|
+
null
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
val builder = NotificationCompat.Builder(context, UIDTConstants.NOTIFICATION_CHANNEL_FINISHED_ID)
|
|
665
|
+
.setContentTitle(title)
|
|
666
|
+
.setContentText(fileName)
|
|
667
|
+
.setSmallIcon(android.R.drawable.stat_sys_download_done)
|
|
668
|
+
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
|
669
|
+
.setAutoCancel(true)
|
|
670
|
+
if (pendingIntent != null) builder.setContentIntent(pendingIntent)
|
|
671
|
+
|
|
672
|
+
// Deliberately NOT part of the download's notification group: when the
|
|
673
|
+
// last job of a group finishes, cancelSummaryNotification tears down
|
|
674
|
+
// every notification carrying that group key, which would wipe these
|
|
675
|
+
// completion notifications the moment they are posted. The shade still
|
|
676
|
+
// bundles them on its own once there are enough of them.
|
|
677
|
+
notificationManager.notify(notificationId, builder.build())
|
|
678
|
+
RNBackgroundDownloaderModuleImpl.logD(UIDTConstants.TAG, "Posted finished notification $notificationId for $configId")
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Resolve a content:// URI for [file], preferring this library's own
|
|
683
|
+
* FileProvider (authority "${packageName}.rnbackgrounddownloader.fileprovider",
|
|
684
|
+
* declared in the library manifest with [RNBGDFileProvider] +
|
|
685
|
+
* rnbgd_file_paths.xml) so tap-to-open works with no host-app setup.
|
|
686
|
+
*
|
|
687
|
+
* If the file falls outside the library provider's configured roots — e.g.
|
|
688
|
+
* a consumer downloads to a directory not covered by rnbgd_file_paths.xml —
|
|
689
|
+
* fall back to auto-detecting a host-app FileProvider authority. Throws if
|
|
690
|
+
* neither can serve the file; the caller catches and omits the tap action.
|
|
691
|
+
*/
|
|
692
|
+
private fun resolveContentUri(context: Context, file: File): Uri {
|
|
693
|
+
val libraryAuthority = "${context.packageName}.rnbackgrounddownloader.fileprovider"
|
|
694
|
+
return try {
|
|
695
|
+
FileProvider.getUriForFile(context, libraryAuthority, file)
|
|
696
|
+
} catch (e: IllegalArgumentException) {
|
|
697
|
+
// File not under any root declared in rnbgd_file_paths.xml; try a
|
|
698
|
+
// host-app provider as a secondary path.
|
|
699
|
+
RNBackgroundDownloaderModuleImpl.logW(
|
|
700
|
+
UIDTConstants.TAG,
|
|
701
|
+
"Library FileProvider can't serve $file (${e.message}); trying host-app provider",
|
|
702
|
+
)
|
|
703
|
+
val fallbackAuthority = resolveFileProviderAuthority(context)
|
|
704
|
+
FileProvider.getUriForFile(context, fallbackAuthority, file)
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Resolve the host app's FileProvider authority at runtime by scanning the
|
|
710
|
+
* providers registered in its manifest, rather than assuming a fixed
|
|
711
|
+
* "${packageName}.provider". Used only as a secondary fallback when the
|
|
712
|
+
* library-owned provider cannot serve a file (see [resolveContentUri]).
|
|
713
|
+
*
|
|
714
|
+
* Preference order: an authority equal to "${packageName}.provider", then
|
|
715
|
+
* any authority owned by this package, then the conventional default as a
|
|
716
|
+
* last resort (which may still throw in getUriForFile if unregistered —
|
|
717
|
+
* that is caught by the caller).
|
|
718
|
+
*/
|
|
719
|
+
private fun resolveFileProviderAuthority(context: Context): String {
|
|
720
|
+
val packageName = context.packageName
|
|
721
|
+
val default = "$packageName.provider"
|
|
722
|
+
// Skip our own provider — resolveContentUri already tried it.
|
|
723
|
+
val libraryAuthority = "$packageName.rnbackgrounddownloader.fileprovider"
|
|
724
|
+
return try {
|
|
725
|
+
val flags = PackageManager.GET_PROVIDERS
|
|
726
|
+
val providers = context.packageManager
|
|
727
|
+
.getPackageInfo(packageName, flags)
|
|
728
|
+
.providers
|
|
729
|
+
?.filter { it.name == "androidx.core.content.FileProvider" || it.name.endsWith("FileProvider") }
|
|
730
|
+
?.mapNotNull { it.authority }
|
|
731
|
+
?.flatMap { it.split(';') }
|
|
732
|
+
?.filter { it != libraryAuthority }
|
|
733
|
+
?: emptyList()
|
|
734
|
+
|
|
735
|
+
providers.firstOrNull { it == default }
|
|
736
|
+
?: providers.firstOrNull { it.startsWith(packageName) }
|
|
737
|
+
?: providers.firstOrNull()
|
|
738
|
+
?: default
|
|
739
|
+
} catch (e: Exception) {
|
|
740
|
+
RNBackgroundDownloaderModuleImpl.logW(UIDTConstants.TAG, "Failed to resolve FileProvider authority: ${e.message}")
|
|
741
|
+
default
|
|
742
|
+
}
|
|
743
|
+
}
|
|
517
744
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<!--
|
|
3
|
+
Paths the library-owned RNBGDFileProvider can expose via getUriForFile.
|
|
4
|
+
|
|
5
|
+
Downloads default to the app's internal files dir (documents == filesDir),
|
|
6
|
+
but consumers may pass any `destination` under the app's private storage, so
|
|
7
|
+
every app-scoped root is covered. Names are arbitrary URI path segments and
|
|
8
|
+
do not need to match the host app's own file_paths.xml.
|
|
9
|
+
|
|
10
|
+
Only app-scoped roots are declared — the directories this library writes to.
|
|
11
|
+
Shared/public storage is deliberately not exposed; a download saved outside
|
|
12
|
+
these roots falls back to a host-app FileProvider (see resolveContentUri).
|
|
13
|
+
-->
|
|
14
|
+
<paths>
|
|
15
|
+
<!-- context.getFilesDir() — default download location (documents) -->
|
|
16
|
+
<files-path name="rnbgd_files" path="." />
|
|
17
|
+
<!-- context.getCacheDir() -->
|
|
18
|
+
<cache-path name="rnbgd_cache" path="." />
|
|
19
|
+
<!-- context.getExternalFilesDir(null) -->
|
|
20
|
+
<external-files-path name="rnbgd_external_files" path="." />
|
|
21
|
+
<!-- context.getExternalCacheDir() -->
|
|
22
|
+
<external-cache-path name="rnbgd_external_cache" path="." />
|
|
23
|
+
</paths>
|