Skip to content

Commit 8bf84c9

Browse files
authored
Allow custom LCP device identifiers (#720)
1 parent 425947d commit 8bf84c9

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ captures/
4949
.idea/jarRepositories.xml
5050
.idea/misc.xml
5151
.idea/migrations.xml
52+
.idea/markdown.xml
5253
# Android Studio 3 in .gitignore file.
5354
.idea/caches
5455
.idea/modules.xml

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ All notable changes to this project will be documented in this file. Take a look
1313
* New experimental positioning of EPUB decorations that places highlights behind text to improve legibility with opaque decorations (contributed by [@ddfreiling](https://github.com/readium/kotlin-toolkit/pull/721)).
1414
* To opt-in, initialize the `EpubNavigatorFragment.Configuration` object with `decorationTemplates = HtmlDecorationTemplates.defaultTemplates(alpha = 1.0, experimentalPositioning = true)`.
1515

16+
#### LCP
17+
18+
* Added an initializer parameter for providing a custom device identifier (contributed by [@dewantawsif](https://github.com/readium/kotlin-toolkit/pull/720)).
19+
* You must ensure the identifier is unique and stable for the device (persist and reuse across app launches).
20+
* Recommended: generate an app-scoped UUID and store it securely. Avoid hardware or advertising identifiers.
21+
1622

1723
## [3.1.2]
1824

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,15 @@ public interface LcpService {
135135
* @param deviceName Device name used when registering a license to an LSD server.
136136
* If not provided, the device name will be generated from the device's manufacturer and
137137
* model.
138+
* @param deviceId Device ID used when registering a license to an LSD server.
139+
* If not provided, the device ID will be generated as a random UUID and persisted for
140+
* future sessions.
138141
*/
139142
public operator fun invoke(
140143
context: Context,
141144
assetRetriever: AssetRetriever,
142145
deviceName: String? = null,
146+
deviceId: String? = null,
143147
): LcpService? {
144148
if (!LcpClient.isAvailable()) {
145149
return null
@@ -152,6 +156,7 @@ public interface LcpService {
152156
val network = NetworkService()
153157
val device = DeviceService(
154158
deviceName = deviceName,
159+
deviceId = deviceId,
155160
repository = deviceRepository,
156161
network = network,
157162
context = context

readium/lcp/src/main/java/org/readium/r2/lcp/service/DeviceService.kt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,34 @@
1010
package org.readium.r2.lcp.service
1111

1212
import android.content.Context
13-
import android.content.SharedPreferences
1413
import android.os.Build
15-
import java.io.Serializable
14+
import androidx.core.content.edit
1615
import java.util.UUID
1716
import org.readium.r2.lcp.license.model.LicenseDocument
1817
import org.readium.r2.lcp.license.model.components.Link
1918

2019
internal class DeviceService(
2120
deviceName: String?,
21+
deviceId: String?,
2222
private val repository: DeviceRepository,
2323
private val network: NetworkService,
2424
val context: Context,
25-
) : Serializable {
26-
27-
private val preferences: SharedPreferences = context.getSharedPreferences(
28-
"org.readium.r2.settings",
29-
Context.MODE_PRIVATE
30-
)
31-
32-
val id: String
33-
get() {
34-
val deviceId = preferences.getString("lcp_device_id", UUID.randomUUID().toString())!!
35-
preferences.edit().putString("lcp_device_id", deviceId).apply()
36-
return deviceId
37-
}
25+
) {
26+
27+
val id: String = deviceId ?: generatedId
28+
29+
private val generatedId: String get() {
30+
val key = "lcp_device_id"
31+
32+
val preferences = context.getSharedPreferences("org.readium.r2.settings", Context.MODE_PRIVATE)
33+
34+
preferences.getString(key, null)
35+
?.let { return it }
36+
37+
val id = UUID.randomUUID().toString()
38+
preferences.edit { putString(key, id) }
39+
return id
40+
}
3841

3942
val name: String =
4043
deviceName ?: "${Build.MANUFACTURER} ${Build.MODEL}"

0 commit comments

Comments
 (0)