Skip to content

Commit 6055779

Browse files
committed
Documentation and formatting
1 parent 840455c commit 6055779

File tree

4 files changed

+137
-87
lines changed

4 files changed

+137
-87
lines changed

readium/lcp/src/main/java/org/readium/r2/lcp/LcpPublicationRetriever.kt

Lines changed: 104 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,143 @@ import org.readium.r2.shared.util.mediatype.MediaType
2121
import org.readium.r2.shared.util.mediatype.MediaTypeRetriever
2222

2323
/**
24-
* Util to acquire a protected publication from standalone LCPL's bytes.
24+
* Utility to acquire a protected publication from an LCP License Document.
2525
*/
2626
public class LcpPublicationRetriever(
2727
context: Context,
2828
private val downloadManager: DownloadManager,
2929
private val mediaTypeRetriever: MediaTypeRetriever
3030
) {
3131

32-
private val coroutineScope: CoroutineScope =
33-
MainScope()
34-
3532
@JvmInline
3633
public value class RequestId(public val value: String)
3734

3835
public interface Listener {
3936

37+
/**
38+
* Called when the publication has been successfully acquired.
39+
*/
4040
public fun onAcquisitionCompleted(
4141
requestId: RequestId,
4242
acquiredPublication: LcpService.AcquiredPublication
4343
)
4444

45+
/**
46+
* The acquisition with ID [requestId] has downloaded [downloaded] out of [expected] bytes.
47+
*/
4548
public fun onAcquisitionProgressed(
4649
requestId: RequestId,
4750
downloaded: Long,
4851
expected: Long?
4952
)
5053

54+
/**
55+
* The acquisition with ID [requestId] has failed with the given [error].
56+
*/
5157
public fun onAcquisitionFailed(
5258
requestId: RequestId,
5359
error: LcpException
5460
)
5561

62+
/**
63+
* The acquisition with ID [requestId] has been cancelled.
64+
*/
5665
public fun onAcquisitionCancelled(
5766
requestId: RequestId
5867
)
5968
}
6069

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+
61161
private inner class DownloadListener : DownloadManager.Listener {
62162

63163
override fun onDownloadCompleted(
@@ -159,67 +259,4 @@ public class LcpPublicationRetriever(
159259
listeners.remove(lcpRequestId)
160260
}
161261
}
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-
}
225262
}

readium/lcp/src/main/java/org/readium/r2/lcp/LcpService.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ public interface LcpService {
5353
* Acquires a protected publication from a standalone LCPL's bytes.
5454
*
5555
* You can cancel the on-going acquisition by cancelling its parent coroutine context.
56-
* @Deprecated(
57-
"Use a LcpPublicationRetriever instead.",
58-
ReplaceWith("publicationRetriever()"),
59-
level = DeprecationLevel.ERROR
60-
)
56+
*
6157
* @param onProgress Callback to follow the acquisition progress from 0.0 to 1.0.
6258
*/
6359
@Deprecated(
@@ -125,9 +121,8 @@ public interface LcpService {
125121
): Try<LcpLicense, LcpException>
126122

127123
/**
128-
* Creates a [LcpPublicationRetriever] instance which can be used to acquire a protected
129-
* publication from standalone LCPL's bytes.
130-
*
124+
* Creates an [LcpPublicationRetriever] instance which can be used to acquire a protected
125+
* publication from an LCP License Document.
131126
*/
132127
public fun publicationRetriever(): LcpPublicationRetriever
133128

@@ -215,8 +210,8 @@ public interface LcpService {
215210
}
216211

217212
@Deprecated(
218-
"Use `acquirePublication()` with coroutines instead",
219-
ReplaceWith("acquirePublication(lcpl)"),
213+
"Use a LcpPublicationRetriever instead.",
214+
ReplaceWith("publicationRetriever()"),
220215
level = DeprecationLevel.ERROR
221216
)
222217
@DelicateCoroutinesApi

readium/shared/src/main/java/org/readium/r2/shared/util/MapCompanion.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
package org.readium.r2.shared.util
1111

12+
import org.readium.r2.shared.InternalReadiumApi
13+
1214
/**
1315
* Encapsulates a [Map] into a more limited query API.
1416
*
@@ -24,6 +26,7 @@ package org.readium.r2.shared.util
2426
* val layout: Layout? = Layout("reflowable")
2527
* ```
2628
*/
29+
@InternalReadiumApi
2730
public open class MapCompanion<K, E>(protected val map: Map<K, E>) {
2831

2932
public constructor(elements: Array<E>, keySelector: (E) -> K) :
@@ -60,6 +63,7 @@ public open class MapCompanion<K, E>(protected val map: Map<K, E>) {
6063
/**
6164
* Extends a [MapCompanion] by adding a [default] value as a fallback.
6265
*/
66+
@InternalReadiumApi
6367
public open class MapWithDefaultCompanion<K, E>(map: Map<K, E>, public val default: E) : MapCompanion<K, E>(
6468
map
6569
) {

readium/shared/src/main/java/org/readium/r2/shared/util/downloads/DownloadManager.kt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ package org.readium.r2.shared.util.downloads
88

99
import java.io.File
1010
import org.readium.r2.shared.util.Url
11+
import org.readium.r2.shared.util.downloads.android.AndroidDownloadManager
12+
import org.readium.r2.shared.util.downloads.foreground.ForegroundDownloadManager
1113

1214
/**
13-
* Retrieves files through Http with various features depending on implementations.
15+
* Manages a set of concurrent files downloaded through HTTP.
16+
*
17+
* Choose the implementation that best fits your needs:
18+
* - [AndroidDownloadManager] for downloading files in the background with the Android system
19+
* service, even if the app is stopped.
20+
* - [ForegroundDownloadManager] for a simpler implementation based on HttpClient which cancels
21+
* the on-going download when the app is closed.
1422
*/
1523
public interface DownloadManager {
1624

@@ -77,46 +85,52 @@ public interface DownloadManager {
7785
public interface Listener {
7886

7987
/**
80-
* Download with id [requestId] successfully completed and is available at [file].
88+
* The download with ID [requestId] has been successfully completed and is now available at
89+
* [file].
8190
*/
8291
public fun onDownloadCompleted(requestId: RequestId, file: File)
8392

8493
/**
85-
* [downloaded] / [expected] bytes have been downloaded for request with id [requestId].
94+
* The request with ID [requestId] has downloaded [downloaded] out of [expected] bytes.
8695
*/
8796
public fun onDownloadProgressed(requestId: RequestId, downloaded: Long, expected: Long?)
8897

8998
/**
90-
* Download with id [requestId] failed with [error].
99+
* The download with ID [requestId] failed due to [error].
91100
*/
92101
public fun onDownloadFailed(requestId: RequestId, error: Error)
93102

94103
/**
95-
* Download with id [requestId] has been cancelled.
104+
* The download with ID [requestId] has been cancelled.
96105
*/
97106
public fun onDownloadCancelled(requestId: RequestId)
98107
}
99108

100109
/**
101-
* Submits a new request to this [DownloadManager]. [listener] will automatically be registered.
110+
* Submits a new request to this [DownloadManager]. The given [listener] will automatically be
111+
* registered.
112+
*
113+
* Returns the ID of the download request, which can be used to cancel it.
102114
*/
103115
public fun submit(request: Request, listener: Listener): RequestId
104116

105117
/**
106-
* Registers a listener for the download with [requestId].
118+
* Registers a listener for the download with the given [requestId].
107119
*
108-
* If your [DownloadManager] supports background downloading, this should typically be used
109-
* when you get back a new instance after the app restarted.
120+
* If your [DownloadManager] supports background downloading, this should typically be used when
121+
* you get create a new instance after the app restarted.
110122
*/
111123
public fun register(requestId: RequestId, listener: Listener)
112124

113125
/**
114-
* Cancels the download with [requestId].
126+
* Cancels the download with the given [requestId].
115127
*/
116128
public fun cancel(requestId: RequestId)
117129

118130
/**
119131
* Releases any in-memory resource associated with this [DownloadManager].
132+
*
133+
* If the pending downloads cannot continue in the background, they will be cancelled.
120134
*/
121135
public fun close()
122136
}

0 commit comments

Comments
 (0)