@@ -12,6 +12,7 @@ package org.readium.r2.lcp.license
1212import org.joda.time.DateTime
1313import org.readium.lcp.sdk.DRMContext
1414import org.readium.lcp.sdk.Lcp
15+ import org.readium.r2.lcp.BuildConfig.DEBUG
1516import org.readium.r2.lcp.license.model.LicenseDocument
1617import org.readium.r2.lcp.license.model.StatusDocument
1718import 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 }
0 commit comments