@@ -21,43 +21,143 @@ import org.readium.r2.shared.util.mediatype.MediaType
21
21
import org.readium.r2.shared.util.mediatype.MediaTypeRetriever
22
22
23
23
/* *
24
- * Util to acquire a protected publication from standalone LCPL's bytes .
24
+ * Utility to acquire a protected publication from an LCP License Document .
25
25
*/
26
26
public class LcpPublicationRetriever (
27
27
context : Context ,
28
28
private val downloadManager : DownloadManager ,
29
29
private val mediaTypeRetriever : MediaTypeRetriever
30
30
) {
31
31
32
- private val coroutineScope: CoroutineScope =
33
- MainScope ()
34
-
35
32
@JvmInline
36
33
public value class RequestId (public val value : String )
37
34
38
35
public interface Listener {
39
36
37
+ /* *
38
+ * Called when the publication has been successfully acquired.
39
+ */
40
40
public fun onAcquisitionCompleted (
41
41
requestId : RequestId ,
42
42
acquiredPublication : LcpService .AcquiredPublication
43
43
)
44
44
45
+ /* *
46
+ * The acquisition with ID [requestId] has downloaded [downloaded] out of [expected] bytes.
47
+ */
45
48
public fun onAcquisitionProgressed (
46
49
requestId : RequestId ,
47
50
downloaded : Long ,
48
51
expected : Long?
49
52
)
50
53
54
+ /* *
55
+ * The acquisition with ID [requestId] has failed with the given [error].
56
+ */
51
57
public fun onAcquisitionFailed (
52
58
requestId : RequestId ,
53
59
error : LcpException
54
60
)
55
61
62
+ /* *
63
+ * The acquisition with ID [requestId] has been cancelled.
64
+ */
56
65
public fun onAcquisitionCancelled (
57
66
requestId : RequestId
58
67
)
59
68
}
60
69
70
+ /* *
71
+ * Submits a new request to acquire the publication protected with the given [license].
72
+ *
73
+ * The given [listener] will automatically be registered.
74
+ *
75
+ * Returns the ID of the acquisition request, which can be used to cancel it.
76
+ */
77
+ public fun retrieve (
78
+ license : LicenseDocument ,
79
+ downloadTitle : String ,
80
+ downloadDescription : String? = null,
81
+ listener : Listener
82
+ ): RequestId {
83
+ val requestId = fetchPublication(
84
+ license,
85
+ downloadTitle,
86
+ downloadDescription
87
+ )
88
+ register(requestId, listener)
89
+ return requestId
90
+ }
91
+
92
+ /* *
93
+ * Registers a listener for the acquisition with the given [requestId].
94
+ *
95
+ * If the [downloadManager] provided during construction supports background downloading, this
96
+ * should typically be used when you get create a new instance after the app restarted.
97
+ */
98
+ public fun register (
99
+ requestId : RequestId ,
100
+ listener : Listener
101
+ ) {
102
+ listeners.getOrPut(requestId) {
103
+ downloadManager.register(DownloadManager .RequestId (requestId.value), downloadListener)
104
+ mutableListOf ()
105
+ }.add(listener)
106
+ }
107
+
108
+ /* *
109
+ * Cancels the acquisition with the given [requestId].
110
+ */
111
+ public fun cancel (requestId : RequestId ) {
112
+ downloadManager.cancel(DownloadManager .RequestId (requestId.value))
113
+ downloadsRepository.removeDownload(requestId.value)
114
+ }
115
+
116
+ /* *
117
+ * Releases any in-memory resource associated with this [LcpPublicationRetriever].
118
+ *
119
+ * If the pending acquisitions cannot continue in the background, they will be cancelled.
120
+ */
121
+ public fun close () {
122
+ downloadManager.close()
123
+ }
124
+
125
+ private val coroutineScope: CoroutineScope =
126
+ MainScope ()
127
+
128
+ private val formatRegistry: FormatRegistry =
129
+ FormatRegistry ()
130
+
131
+ private val downloadsRepository: LcpDownloadsRepository =
132
+ LcpDownloadsRepository (context)
133
+
134
+ private val downloadListener: DownloadManager .Listener =
135
+ DownloadListener ()
136
+
137
+ private val listeners: MutableMap <RequestId , MutableList <Listener >> =
138
+ mutableMapOf ()
139
+
140
+ private fun fetchPublication (
141
+ license : LicenseDocument ,
142
+ downloadTitle : String ,
143
+ downloadDescription : String?
144
+ ): RequestId {
145
+ val url = Url (license.publicationLink.url)
146
+
147
+ val requestId = downloadManager.submit(
148
+ request = DownloadManager .Request (
149
+ url = url,
150
+ title = downloadTitle,
151
+ description = downloadDescription,
152
+ headers = emptyMap()
153
+ ),
154
+ listener = downloadListener
155
+ )
156
+
157
+ downloadsRepository.addDownload(requestId.value, license.json)
158
+ return RequestId (requestId.value)
159
+ }
160
+
61
161
private inner class DownloadListener : DownloadManager .Listener {
62
162
63
163
override fun onDownloadCompleted (
@@ -159,67 +259,4 @@ public class LcpPublicationRetriever(
159
259
listeners.remove(lcpRequestId)
160
260
}
161
261
}
162
-
163
- private val formatRegistry: FormatRegistry =
164
- FormatRegistry ()
165
-
166
- private val downloadsRepository: LcpDownloadsRepository =
167
- LcpDownloadsRepository (context)
168
-
169
- private val downloadListener: DownloadManager .Listener =
170
- DownloadListener ()
171
-
172
- private val listeners: MutableMap <RequestId , MutableList <Listener >> =
173
- mutableMapOf ()
174
-
175
- public fun register (
176
- requestId : RequestId ,
177
- listener : Listener
178
- ) {
179
- listeners.getOrPut(requestId) {
180
- downloadManager.register(DownloadManager .RequestId (requestId.value), downloadListener)
181
- mutableListOf ()
182
- }.add(listener)
183
- }
184
-
185
- public fun retrieve (
186
- license : LicenseDocument ,
187
- downloadTitle : String ,
188
- downloadDescription : String? = null,
189
- listener : Listener
190
- ): RequestId {
191
- val requestId = fetchPublication(
192
- license,
193
- downloadTitle,
194
- downloadDescription
195
- )
196
- register(requestId, listener)
197
- return requestId
198
- }
199
-
200
- public fun cancel (requestId : RequestId ) {
201
- downloadManager.cancel(DownloadManager .RequestId (requestId.value))
202
- downloadsRepository.removeDownload(requestId.value)
203
- }
204
-
205
- private fun fetchPublication (
206
- license : LicenseDocument ,
207
- downloadTitle : String ,
208
- downloadDescription : String?
209
- ): RequestId {
210
- val url = Url (license.publicationLink.url)
211
-
212
- val requestId = downloadManager.submit(
213
- request = DownloadManager .Request (
214
- url = url,
215
- title = downloadTitle,
216
- description = downloadDescription,
217
- headers = emptyMap()
218
- ),
219
- listener = downloadListener
220
- )
221
-
222
- downloadsRepository.addDownload(requestId.value, license.json)
223
- return RequestId (requestId.value)
224
- }
225
262
}
0 commit comments