Skip to content

Commit db529f9

Browse files
Merge pull request #56 from readium/fixes/debug
Fixes/debug
2 parents 3e22a04 + 4d9c527 commit db529f9

File tree

8 files changed

+83
-78
lines changed

8 files changed

+83
-78
lines changed

readium/lcp/r2-lcp/src/main/java/org/readium/r2/lcp/license/License.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import nl.komponents.kovenant.Promise
1616
import nl.komponents.kovenant.then
1717
import org.joda.time.DateTime
1818
import org.readium.lcp.sdk.Lcp
19+
import org.readium.r2.lcp.BuildConfig.DEBUG
1920
import org.readium.r2.lcp.license.model.LicenseDocument
2021
import org.readium.r2.lcp.license.model.StatusDocument
2122
import org.readium.r2.lcp.public.*
@@ -56,7 +57,7 @@ class License(private var documents: ValidatedDocuments,
5657
return charactersLeft
5758
}
5859
} catch (error: Error) {
59-
Timber.e(error)
60+
if (DEBUG) Timber.e(error)
6061
}
6162
return null
6263
}
@@ -77,7 +78,7 @@ class License(private var documents: ValidatedDocuments,
7778
charactersLeft = maxOf(0, charactersLeft - result.length)
7879
licenses.setCopiesLeft(charactersLeft, license.id)
7980
} catch (error: Error) {
80-
Timber.e(error)
81+
if (DEBUG) Timber.e(error)
8182
}
8283
return result
8384
}
@@ -90,7 +91,7 @@ class License(private var documents: ValidatedDocuments,
9091
return pagesLeft
9192
}
9293
} catch (error: Error) {
93-
Timber.e(error)
94+
if (DEBUG) Timber.e(error)
9495
}
9596
return null
9697
}
@@ -106,7 +107,7 @@ class License(private var documents: ValidatedDocuments,
106107
pagesLeft = maxOf(0, pagesLeft - pagesCount)
107108
licenses.setPrintsLeft(pagesLeft, license.id)
108109
} catch (error: Error) {
109-
Timber.e(error)
110+
if (DEBUG) Timber.e(error)
110111
}
111112
return true
112113
}
@@ -161,7 +162,6 @@ class License(private var documents: ValidatedDocuments,
161162
if (link.type == "text/html") {
162163
callHTML(url, params) {
163164
validateStatusDocument(it)
164-
165165
}
166166
} else {
167167
callPUT(url, params) {
@@ -210,7 +210,7 @@ class License(private var documents: ValidatedDocuments,
210210

211211
fun moveLicense(archivePath: String, licenseData: ByteArray) {
212212
val pathInZip = "META-INF/license.lcpl"
213-
Timber.i("LCP moveLicense")
213+
if (DEBUG) Timber.i("LCP moveLicense")
214214
val source = File(archivePath)
215215
val tmpZip = File("$archivePath.tmp")
216216
tmpZip.delete()
@@ -228,9 +228,9 @@ class License(private var documents: ValidatedDocuments,
228228
val title = license.link(LicenseDocument.Rel.publication)?.title
229229
val url = license.url(LicenseDocument.Rel.publication)
230230

231-
val properties = Properties();
232-
val inputStream = context.assets.open("configs/config.properties");
233-
properties.load(inputStream);
231+
val properties = Properties()
232+
val inputStream = context.assets.open("configs/config.properties")
233+
properties.load(inputStream)
234234
val useExternalFileDir = properties.getProperty("useExternalFileDir", "false")!!.toBoolean()
235235

236236
val rootDir: String = if (useExternalFileDir) {
@@ -241,13 +241,13 @@ class License(private var documents: ValidatedDocuments,
241241

242242
val fileName = UUID.randomUUID().toString()
243243
return Fuel.download(url.toString()).destination { _, _ ->
244-
Timber.i("LCP destination %s%s", rootDir, fileName)
244+
if (DEBUG) Timber.i("LCP destination %s%s", rootDir, fileName)
245245
File(rootDir, fileName)
246246

247247
}.promise() then {
248248
val (_, response, _) = it
249-
Timber.i("LCP destination %s%s", rootDir, fileName)
250-
Timber.i("LCP then %s", response.url.toString())
249+
if (DEBUG) Timber.i("LCP destination %s%s", rootDir, fileName)
250+
if (DEBUG) Timber.i("LCP then %s", response.url.toString())
251251

252252
rootDir + fileName
253253

readium/lcp/r2-lcp/src/main/java/org/readium/r2/lcp/license/LicenseValidation.kt

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package org.readium.r2.lcp.license
1212
import org.joda.time.DateTime
1313
import org.readium.lcp.sdk.DRMContext
1414
import org.readium.lcp.sdk.Lcp
15+
import org.readium.r2.lcp.BuildConfig.DEBUG
1516
import org.readium.r2.lcp.license.model.LicenseDocument
1617
import org.readium.r2.lcp.license.model.StatusDocument
1718
import org.readium.r2.lcp.license.model.components.Link
@@ -100,12 +101,11 @@ class LicenseValidation(var authentication: LCPAuthenticating?,
100101
}
101102

102103
fun validate(document: Document, completion: Observer) {
103-
val event: Event
104-
when (document) {
105-
is Document.license -> event = Event.retrievedLicenseData(document.data)
106-
is Document.status -> event = Event.retrievedStatusData(document.data)
104+
val event: Event = when (document) {
105+
is Document.license -> Event.retrievedLicenseData(document.data)
106+
is Document.status -> Event.retrievedStatusData(document.data)
107107
}
108-
Timber.d("validate $event ")
108+
// Timber.tag("LicenseValidation").d("validate ${event} ")
109109
observe(event, completion)
110110
}
111111

@@ -124,82 +124,82 @@ class LicenseValidation(var authentication: LCPAuthenticating?,
124124
initialState(State.start)
125125
state<State.start> {
126126
on<Event.retrievedLicenseData> {
127-
Timber.d("State.validateLicense(it.data, null)")
127+
if (DEBUG) Timber.tag("LicenseValidation").d("State.validateLicense(it.data, null)")
128128
transitionTo(State.validateLicense(it.data, null))
129129
}
130130
}
131131
state<State.validateLicense> {
132132
on<Event.validatedLicense> {
133133
status?.let { status ->
134-
Timber.d("State.checkLicenseStatus(it.license, status)")
134+
if (DEBUG) Timber.tag("LicenseValidation").d("State.checkLicenseStatus(it.license, status)")
135135
transitionTo(State.checkLicenseStatus(it.license, status))
136136
} ?: run {
137-
Timber.d("State.fetchStatus(it.license)")
137+
if (DEBUG) Timber.tag("LicenseValidation").d("State.fetchStatus(it.license)")
138138
transitionTo(State.fetchStatus(it.license))
139139
}
140140
}
141141
on<Event.failed> {
142-
Timber.d("State.failure(it.error)")
142+
if (DEBUG) Timber.tag("LicenseValidation").d("State.failure(it.error)")
143143
transitionTo(State.failure(it.error))
144144
}
145145
}
146146
state<State.fetchStatus> {
147147
on<Event.retrievedStatusData> {
148-
Timber.d("State.validateStatus(license, it.data)")
148+
if (DEBUG) Timber.tag("LicenseValidation").d("State.validateStatus(license, it.data)")
149149
transitionTo(State.validateStatus(license, it.data))
150150
}
151151
on<Event.failed> {
152-
Timber.d("State.checkLicenseStatus(license, null)")
152+
if (DEBUG) Timber.tag("LicenseValidation").d("State.checkLicenseStatus(license, null)")
153153
transitionTo(State.checkLicenseStatus(license, null))
154154
}
155155
}
156156
state<State.validateStatus> {
157157
on<Event.validatedStatus> {
158158
if (license.updated < it.status.licenseUpdated) {
159-
Timber.d("State.fetchLicense(license, it.status)")
159+
if (DEBUG) Timber.tag("LicenseValidation").d("State.fetchLicense(license, it.status)")
160160
transitionTo(State.fetchLicense(license, it.status))
161161
} else {
162-
Timber.d("State.checkLicenseStatus(license, it.status)")
162+
if (DEBUG) Timber.tag("LicenseValidation").d("State.checkLicenseStatus(license, it.status)")
163163
transitionTo(State.checkLicenseStatus(license, it.status))
164164
}
165165
}
166166
on<Event.failed> {
167-
Timber.d("State.checkLicenseStatus(license, null)")
167+
if (DEBUG) Timber.tag("LicenseValidation").d("State.checkLicenseStatus(license, null)")
168168
transitionTo(State.checkLicenseStatus(license, null))
169169
}
170170
}
171171
state<State.fetchLicense> {
172172
on<Event.retrievedLicenseData> {
173-
Timber.d("State.validateLicense(it.data, status)")
173+
if (DEBUG) Timber.tag("LicenseValidation").d("State.validateLicense(it.data, status)")
174174
transitionTo(State.validateLicense(it.data, status))
175175
}
176176
on<Event.failed> {
177-
Timber.d("State.checkLicenseStatus(license, status)")
177+
if (DEBUG) Timber.tag("LicenseValidation").d("State.checkLicenseStatus(license, status)")
178178
transitionTo(State.checkLicenseStatus(license, status))
179179
}
180180
}
181181
state<State.checkLicenseStatus> {
182182
on<Event.checkedLicenseStatus> {
183183
it.error?.let{ error ->
184-
Timber.d("State.valid(ValidatedDocuments(license, Either.Right(error), status))")
184+
if (DEBUG) Timber.tag("LicenseValidation").d("State.valid(ValidatedDocuments(license, Either.Right(error), status))")
185185
transitionTo(State.valid(ValidatedDocuments(license, Either.Right(error), status)))
186186
}?: run {
187-
Timber.d("State.requestPassphrase(license, status)")
187+
if (DEBUG) Timber.tag("LicenseValidation").d("State.requestPassphrase(license, status)")
188188
transitionTo(State.requestPassphrase(license, status))
189189
}
190190
}
191191
}
192192
state<State.requestPassphrase> {
193193
on<Event.retrievedPassphrase> {
194-
Timber.d("State.validateIntegrity(license, status, it.passphrase)")
194+
if (DEBUG) Timber.tag("LicenseValidation").d("State.validateIntegrity(license, status, it.passphrase)")
195195
transitionTo(State.validateIntegrity(license, status, it.passphrase))
196196
}
197197
on<Event.failed> {
198-
Timber.d("State.failure(it.error)")
198+
if (DEBUG) Timber.tag("LicenseValidation").d("State.failure(it.error)")
199199
transitionTo(State.failure(it.error))
200200
}
201201
on<Event.cancelled> {
202-
Timber.d("State.start)")
202+
if (DEBUG) Timber.tag("LicenseValidation").d("State.start)")
203203
transitionTo(State.start)
204204
}
205205
}
@@ -208,42 +208,42 @@ class LicenseValidation(var authentication: LCPAuthenticating?,
208208
val documents = ValidatedDocuments(license, Either.Left(it.context), status)
209209
val link = status?.link(StatusDocument.Rel.register)
210210
link?.let {
211-
Timber.d("State.registerDevice(documents, link)")
211+
if (DEBUG) Timber.tag("LicenseValidation").d("State.registerDevice(documents, link)")
212212
transitionTo(State.registerDevice(documents, link))
213213
} ?: run {
214-
Timber.d("State.valid(documents)")
214+
if (DEBUG) Timber.tag("LicenseValidation").d("State.valid(documents)")
215215
transitionTo(State.valid(documents))
216216
}
217217
}
218218
on<Event.failed> {
219-
Timber.d("State.failure(it.error)")
219+
if (DEBUG) Timber.tag("LicenseValidation").d("State.failure(it.error)")
220220
transitionTo(State.failure(it.error))
221221
}
222222
}
223223
state<State.registerDevice> {
224224
on<Event.registeredDevice> {
225225
it.statusData?.let { statusData->
226-
Timber.d("State.validateStatus(documents.license, statusData)")
226+
if (DEBUG) Timber.tag("LicenseValidation").d("State.validateStatus(documents.license, statusData)")
227227
transitionTo(State.validateStatus(documents.license, statusData))
228228
} ?: run {
229-
Timber.d("State.valid(documents)")
229+
if (DEBUG) Timber.tag("LicenseValidation").d("State.valid(documents)")
230230
transitionTo(State.valid(documents))
231231
}
232232
}
233233
on<Event.failed> {
234-
Timber.d("State.valid(documents)")
234+
if (DEBUG) Timber.tag("LicenseValidation").d("State.valid(documents)")
235235
transitionTo(State.valid(documents))
236236
}
237237
}
238238
state<State.valid> {
239239
on<Event.retrievedStatusData> {
240-
Timber.d("State.validateStatus(documents.license, it.data)")
240+
if (DEBUG) Timber.tag("LicenseValidation").d("State.validateStatus(documents.license, it.data)")
241241
transitionTo(State.validateStatus(documents.license, it.data))
242242
}
243243
}
244244
state<State.failure> {
245245
onEnter {
246-
Timber.d("throw error")
246+
if (DEBUG) Timber.tag("LicenseValidation").d("throw error")
247247
// throw error
248248
}
249249
}
@@ -275,7 +275,7 @@ class LicenseValidation(var authentication: LCPAuthenticating?,
275275
is State.failure -> notifyObservers(null, state.error)
276276
}
277277
} catch (error: Exception) {
278-
Timber.e(error)
278+
if (DEBUG) Timber.tag("LicenseValidation").e(error)
279279
raise(Event.failed(error))
280280
}
281281
}
@@ -287,12 +287,12 @@ class LicenseValidation(var authentication: LCPAuthenticating?,
287287

288288
private fun notifyObservers(documents: ValidatedDocuments?, error: Exception?) {
289289
for (observer in observers) {
290-
Timber.d("observers $observers")
290+
// Timber.tag("LicenseValidation").d("observers $observers")
291291
observer.first(documents, error)
292292
}
293-
Timber.d("observers $observers")
293+
// Timber.tag("LicenseValidation").d("observers $observers")
294294
observers = (observers.filter { it.second != ObserverPolicy.once }).toMutableList()
295-
Timber.d("observers $observers")
295+
// Timber.tag("LicenseValidation").d("observers $observers")
296296
}
297297

298298
private fun validateLicense(data: ByteArray) {
@@ -335,25 +335,26 @@ class LicenseValidation(var authentication: LCPAuthenticating?,
335335
val start = license.rights.start ?: now
336336
val end = license.rights.end ?: now
337337
if (start > now || now > end) {
338-
if (status != null) {
338+
error = if (status != null) {
339339
val date = status.statusUpdated
340340
when (status.status) {
341-
StatusDocument.Status.ready, StatusDocument.Status.active, StatusDocument.Status.expired -> error = StatusError.expired(start = start, end = end)
342-
StatusDocument.Status.returned -> error = StatusError.returned(date)
341+
StatusDocument.Status.ready, StatusDocument.Status.active, StatusDocument.Status.expired -> StatusError.expired(start = start, end = end)
342+
StatusDocument.Status.returned -> StatusError.returned(date)
343343
StatusDocument.Status.revoked -> {
344344
val devicesCount = status.events(org.readium.r2.lcp.license.model.components.lsd.Event.EventType.register).size
345-
error = StatusError.revoked(date, devicesCount = devicesCount)
345+
StatusError.revoked(date, devicesCount = devicesCount)
346346
}
347-
StatusDocument.Status.cancelled -> error = StatusError.cancelled(date)
347+
StatusDocument.Status.cancelled -> StatusError.cancelled(date)
348348
}
349349
} else {
350-
error = StatusError.expired(start = start, end = end)
350+
StatusError.expired(start = start, end = end)
351351
}
352352
}
353353
raise(Event.checkedLicenseStatus(error))
354354
}
355355

356356
private fun requestPassphrase(license: LicenseDocument) {
357+
if (DEBUG) Timber.tag("LicenseValidation").d("requestPassphrase")
357358
passphrases.request(license, authentication) { passphrase ->
358359
passphrase?.let {
359360
raise(Event.retrievedPassphrase(passphrase))
@@ -364,6 +365,7 @@ class LicenseValidation(var authentication: LCPAuthenticating?,
364365
}
365366

366367
private fun validateIntegrity(license: LicenseDocument, passphrase: String) {
368+
if (DEBUG) Timber.tag("LicenseValidation").d("validateIntegrity")
367369
val profile = license.encryption.profile
368370
if (!supportedProfiles.contains(profile)) {
369371
throw LCPError.licenseProfileNotSupported
@@ -375,6 +377,7 @@ class LicenseValidation(var authentication: LCPAuthenticating?,
375377
}
376378

377379
private fun registerDevice(license: LicenseDocument, link: Link) {
380+
if (DEBUG) Timber.tag("LicenseValidation").d("registerDevice")
378381
device.registerLicense(license, link) { data ->
379382
raise(Event.registeredDevice(data))
380383
}

readium/lcp/r2-lcp/src/main/java/org/readium/r2/lcp/license/container/LCPLLicenseContainer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package org.readium.r2.lcp.license.container
1010

1111
import android.net.Uri
12+
import org.readium.r2.lcp.BuildConfig.DEBUG
1213
import org.readium.r2.lcp.license.model.LicenseDocument
1314
import org.zeroturnaround.zip.ZipUtil
1415
import timber.log.Timber
@@ -34,7 +35,7 @@ class LCPLLicenseContainer(private val lcpl: String? = null, private val byteArr
3435
override fun write(license: LicenseDocument) {
3536
publication?.let {
3637
val pathInZip = "META-INF/license.lcpl"
37-
Timber.i("LCP moveLicense")
38+
if (DEBUG) Timber.i("LCP moveLicense")
3839
val source = File(publication)
3940
val tmpZip = File("$publication.tmp")
4041
tmpZip.delete()

readium/lcp/r2-lcp/src/main/java/org/readium/r2/lcp/persistence/Database.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ val Context.appContext: Context
2727

2828
class Database(context: Context) {
2929

30-
val shared: LcpDatabaseOpenHelper
30+
val shared: LcpDatabaseOpenHelper = LcpDatabaseOpenHelper(context)
3131
var licenses: Licenses
3232
var transactions: Transactions
3333

3434
init {
35-
shared = LcpDatabaseOpenHelper(context)
3635
licenses = Licenses(shared)
3736
transactions = Transactions(shared)
3837
}

0 commit comments

Comments
 (0)