Skip to content

Commit 0be4763

Browse files
authored
Add ContentProtection.Scheme to identify DRM technologies (#168)
1 parent 620f11b commit 0be4763

File tree

5 files changed

+57
-12
lines changed

5 files changed

+57
-12
lines changed

readium/shared/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
99
### Added
1010

1111
* (*alpha*) A new Publication `SearchService` to search through the resources' content, with a default implementation `StringSearchService`.
12+
* `ContentProtection.Scheme` can be used to identify protection technologies using unique URI identifiers.
1213

1314
### Changed
1415

readium/shared/r2-shared/src/main/java/org/readium/r2/shared/UserException.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ open class UserException protected constructor(
2222
cause: Throwable?
2323
) : Exception(cause) {
2424

25-
constructor(@StringRes userMessageId: Int, vararg args: Any, cause: Throwable? = null)
25+
constructor(@StringRes userMessageId: Int, vararg args: Any?, cause: Throwable? = null)
2626
: this(Content(userMessageId, *args), cause)
2727

28-
constructor(@PluralsRes userMessageId: Int, quantity: Int?, vararg args: Any, cause: Throwable? = null)
28+
constructor(@PluralsRes userMessageId: Int, quantity: Int?, vararg args: Any?, cause: Throwable? = null)
2929
: this(Content(userMessageId, quantity, *args), cause)
3030

3131
constructor(message: String, cause: Throwable? = null)
@@ -64,7 +64,7 @@ open class UserException protected constructor(
6464
* @param args Optional arguments to expand in the message.
6565
* @param quantity Quantity to use if the user message is a quantity strings.
6666
*/
67-
class LocalizedString(private val userMessageId: Int, private val args: Array<out Any>, private val quantity: Int?) : Content() {
67+
class LocalizedString(private val userMessageId: Int, private val args: Array<out Any?>, private val quantity: Int?) : Content() {
6868
override fun getUserMessage(context: Context, cause: Throwable?, includesCauses: Boolean): String {
6969
// Convert complex objects to strings, such as Date, to be interpolated.
7070
val args = args.map { arg ->
@@ -98,9 +98,9 @@ open class UserException protected constructor(
9898
}
9999

100100
companion object {
101-
operator fun invoke(@StringRes userMessageId: Int, vararg args: Any) =
101+
operator fun invoke(@StringRes userMessageId: Int, vararg args: Any?) =
102102
LocalizedString(userMessageId, args, null)
103-
operator fun invoke(@PluralsRes userMessageId: Int, quantity: Int?, vararg args: Any) =
103+
operator fun invoke(@PluralsRes userMessageId: Int, quantity: Int?, vararg args: Any?) =
104104
LocalizedString(userMessageId, args, quantity)
105105
operator fun invoke(cause: UserException) =
106106
Exception(cause)

readium/shared/r2-shared/src/main/java/org/readium/r2/shared/publication/ContentProtection.kt

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

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

12+
import androidx.annotation.StringRes
13+
import org.readium.r2.shared.R
14+
import org.readium.r2.shared.UserException
1215
import org.readium.r2.shared.fetcher.Fetcher
1316
import org.readium.r2.shared.publication.asset.PublicationAsset
1417
import org.readium.r2.shared.publication.services.ContentProtectionService
@@ -76,4 +79,35 @@ interface ContentProtection {
7679
val onCreatePublication: Publication.Builder.() -> Unit = {}
7780
)
7881

82+
/**
83+
* Represents a specific Content Protection technology, uniquely identified with an [uri].
84+
*/
85+
class Scheme(
86+
val uri: String,
87+
val name: LocalizedString?
88+
) {
89+
override fun hashCode(): Int = uri.hashCode()
90+
override fun equals(other: Any?): Boolean = (other as? Scheme)?.uri == uri
91+
92+
companion object {
93+
/** Readium LCP DRM scheme. */
94+
val Lcp = Scheme(uri = "http://readium.org/2014/01/lcp", name = LocalizedString("Readium LCP"))
95+
/** Adobe ADEPT DRM scheme. */
96+
val Adept = Scheme(uri = "http://ns.adobe.com/adept", name = LocalizedString("Adobe ADEPT"))
97+
}
98+
}
99+
100+
sealed class Exception(userMessageId: Int, vararg args: Any?, quantity: Int? = null, cause: Throwable? = null) : UserException(userMessageId, quantity, *args, cause = cause) {
101+
constructor(@StringRes userMessageId: Int, vararg args: Any?, cause: Throwable? = null) : this(userMessageId, *args, quantity = null, cause = cause)
102+
103+
/**
104+
* Exception returned when the given Content Protection [scheme] is not supported by the
105+
* app.
106+
*/
107+
class SchemeNotSupported(val scheme: Scheme? = null) : Exception(
108+
if (scheme?.name == null) R.string.r2_shared_publication_content_protection_exception_not_supported_unknown
109+
else R.string.r2_shared_publication_content_protection_exception_not_supported,
110+
scheme?.name?.string
111+
)
112+
}
79113
}

readium/shared/r2-shared/src/main/java/org/readium/r2/shared/publication/services/ContentProtectionService.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ import org.readium.r2.shared.extensions.queryParameters
1616
import org.readium.r2.shared.fetcher.FailureResource
1717
import org.readium.r2.shared.fetcher.Resource
1818
import org.readium.r2.shared.fetcher.StringResource
19-
import org.readium.r2.shared.publication.Link
20-
import org.readium.r2.shared.publication.LocalizedString
21-
import org.readium.r2.shared.publication.Publication
22-
import org.readium.r2.shared.publication.ServiceFactory
23-
import java.lang.IllegalArgumentException
24-
import java.util.Locale
19+
import org.readium.r2.shared.publication.*
20+
import java.util.*
2521

2622
/**
2723
* Provides information about a publication's content protection and manages user rights.
@@ -49,11 +45,16 @@ interface ContentProtectionService: Publication.Service {
4945
*/
5046
val rights: UserRights
5147

48+
/**
49+
* Known technology for this type of Content Protection.
50+
*/
51+
val scheme: ContentProtection.Scheme? get() = null
52+
5253
/**
5354
* User-facing name for this Content Protection, e.g. "Readium LCP".
5455
* It could be used in a sentence such as "Protected by {name}"
5556
*/
56-
val name: LocalizedString?
57+
val name: LocalizedString? get() = scheme?.name
5758

5859
override val links: List<Link>
5960
get() = RouteHandler.links
@@ -205,6 +206,12 @@ val Publication.rights: ContentProtectionService.UserRights
205206
get() = protectionService?.rights
206207
?: ContentProtectionService.UserRights.Unrestricted
207208

209+
/**
210+
* Known technology for this type of Content Protection.
211+
*/
212+
val Publication.protectionScheme: ContentProtection.Scheme?
213+
get() = protectionService?.scheme
214+
208215
/**
209216
* User-facing localized name for this Content Protection, e.g. "Readium LCP".
210217
* It could be used in a sentence such as "Protected by {name}".

readium/shared/r2-shared/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<string name="r2.shared.publication.opening_exception.unavailable">Not available, please try again later</string>
1414
<string name="r2.shared.publication.opening_exception.incorrect_credentials">Provided credentials were incorrect</string>
1515

16+
<string name="r2.shared.publication.content_protection.exception.not_supported">This publication cannot be opened because it is protected with %1$s</string>
17+
<string name="r2.shared.publication.content_protection.exception.not_supported.unknown">This publication cannot be opened because it is protected with an unknown DRM</string>
18+
1619
<string name="r2.shared.resource.exception.bad_request">Invalid request which can\'t be processed</string>
1720
<string name="r2.shared.resource.exception.not_found">Resource not found</string>
1821
<string name="r2.shared.resource.exception.forbidden">You are not allowed to access the resource</string>

0 commit comments

Comments
 (0)