Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jul 29, 2022. It is now read-only.

Commit fa03fa2

Browse files
committed
Use the new LCP renew API
1 parent 967733d commit fa03fa2

File tree

7 files changed

+63
-118
lines changed

7 files changed

+63
-118
lines changed

r2-testapp/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
android:name=".comic.ComicActivity"
202202
android:label="@string/title_activity_epub" />
203203
<activity
204-
android:name=".DRMManagementActivity"
204+
android:name=".drm.DRMManagementActivity"
205205
android:label="DRM Management"
206206
android:theme="@style/AppTheme" />
207207
<activity

r2-testapp/src/main/java/org/readium/r2/testapp/audiobook/AudiobookActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import org.readium.r2.navigator.audiobook.R2AudiobookActivity
1919
import org.readium.r2.shared.extensions.putPublicationFrom
2020
import org.readium.r2.shared.publication.Locator
2121
import org.readium.r2.shared.publication.services.isProtected
22-
import org.readium.r2.testapp.DRMManagementActivity
2322
import org.readium.r2.testapp.R
2423
import org.readium.r2.testapp.db.Bookmark
2524
import org.readium.r2.testapp.db.BookmarksDatabase
2625
import org.readium.r2.testapp.db.BooksDatabase
26+
import org.readium.r2.testapp.drm.DRMManagementActivity
2727
import org.readium.r2.testapp.library.LibraryActivity
2828
import org.readium.r2.testapp.library.activitiesLaunched
2929
import org.readium.r2.testapp.outline.R2OutlineActivity

r2-testapp/src/main/java/org/readium/r2/testapp/drm/DRMManagementActivity.kt

Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,30 @@
88
* LICENSE file present in the project repository where this source code is maintained.
99
*/
1010

11-
package org.readium.r2.testapp
11+
package org.readium.r2.testapp.drm
1212

1313
import android.app.Activity
1414
import android.content.Intent
15-
import android.graphics.Color
1615
import android.graphics.Typeface
1716
import android.os.Bundle
1817
import android.view.Gravity
19-
import android.view.View
20-
import android.view.ViewGroup
21-
import android.widget.ArrayAdapter
2218
import android.widget.LinearLayout
23-
import android.widget.Spinner
2419
import android.widget.TextView
2520
import androidx.appcompat.app.AlertDialog
2621
import androidx.appcompat.app.AppCompatActivity
2722
import com.mcxiaoke.koi.ext.onClick
2823
import kotlinx.coroutines.CoroutineScope
2924
import kotlinx.coroutines.Dispatchers
3025
import kotlinx.coroutines.launch
26+
import kotlinx.coroutines.runBlocking
3127
import org.jetbrains.anko.*
3228
import org.jetbrains.anko.appcompat.v7.Appcompat
3329
import org.jetbrains.anko.design.coordinatorLayout
3430
import org.jetbrains.anko.design.longSnackbar
3531
import org.joda.time.DateTime
3632
import org.joda.time.format.DateTimeFormat
3733
import org.readium.r2.shared.UserException
38-
import org.readium.r2.testapp.utils.extensions.color
34+
import org.readium.r2.testapp.R
3935
import java.io.File
4036
import kotlin.coroutines.CoroutineContext
4137

@@ -57,37 +53,11 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
5753
val pubPath = intent.getStringExtra("publication")
5854
?: throw Exception("publication required")
5955

60-
drmModel = LCPViewModel(File(pubPath), this)
61-
62-
val daysArray = arrayOf(1, 3, 7, 15)
63-
64-
val daysInput = Spinner(this@DRMManagementActivity)
65-
daysInput.dropDownWidth = wrapContent
66-
67-
val adapter = object : ArrayAdapter<Int>(this@DRMManagementActivity, R.layout.item_spinner_days, daysArray) {
68-
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
69-
val v: View? = super.getDropDownView(position, null, parent)
70-
// Makes the selected font appear in dark
71-
// If this is the selected item position
72-
if (position == daysInput.selectedItemPosition) {
73-
v!!.setBackgroundColor(context.color(R.color.colorPrimaryDark))
74-
v.findViewById<TextView>(R.id.days_spinner).setTextColor(Color.WHITE)
75-
} else {
76-
// for other views
77-
v!!.setBackgroundColor(Color.WHITE)
78-
v.findViewById<TextView>(R.id.days_spinner).setTextColor(Color.BLACK)
79-
}
80-
return v
56+
drmModel = runBlocking { LCPViewModel.from(File(pubPath), this@DRMManagementActivity) }
57+
?: run {
58+
finish()
59+
return
8160
}
82-
}
83-
84-
daysInput.adapter = adapter
85-
val renewDialog = alert(Appcompat, "How many days do you wish to extend your loan ?") {
86-
this.customView = daysInput
87-
daysInput.setSelection(2)
88-
positiveButton("Renew") { }
89-
negativeButton("Cancel") { }
90-
}.build()
9161

9262
coordinatorLayout {
9363
fitsSystemWindows = true
@@ -169,7 +139,7 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
169139
}.lparams(width = wrapContent, height = wrapContent, weight = 1f)
170140
textView {
171141
padding = dip(10)
172-
text = drmModel.issued?.toString(DateTimeFormat.shortDateTime())
142+
text = drmModel.issued?.let { DateTime(it) }?.toString(DateTimeFormat.shortDateTime())
173143
textSize = 18f
174144
gravity = Gravity.END
175145
}.lparams(width = wrapContent, height = wrapContent, weight = 1f)
@@ -185,7 +155,7 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
185155
}.lparams(width = wrapContent, height = wrapContent, weight = 1f)
186156
textView {
187157
padding = dip(10)
188-
text = drmModel.updated?.toString(DateTimeFormat.shortDateTime())
158+
text = drmModel.updated?.let { DateTime(it) }?.toString(DateTimeFormat.shortDateTime())
189159
textSize = 18f
190160
gravity = Gravity.END
191161
}.lparams(width = wrapContent, height = wrapContent, weight = 1f)
@@ -244,7 +214,7 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
244214
}.lparams(width = wrapContent, height = wrapContent, weight = 1f)
245215
textView {
246216
padding = dip(10)
247-
text = drmModel.start?.toString(DateTimeFormat.shortDateTime())
217+
text = drmModel.start?.let { DateTime(it) }?.toString(DateTimeFormat.shortDateTime())
248218
textSize = 18f
249219
gravity = Gravity.END
250220
}.lparams(width = wrapContent, height = wrapContent, weight = 1f)
@@ -261,7 +231,7 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
261231

262232
endTextView = textView {
263233
padding = dip(10)
264-
text = drmModel.end?.toString(DateTimeFormat.shortDateTime())
234+
text = drmModel.end?.let { DateTime(it) }?.toString(DateTimeFormat.shortDateTime())
265235
textSize = 18f
266236
gravity = Gravity.END
267237
}.lparams(width = wrapContent, height = wrapContent, weight = 1f)
@@ -277,32 +247,16 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
277247
button {
278248
text = context.getString(R.string.drm_label_renew)
279249
onClick {
280-
renewDialog.apply {
281-
setCancelable(false)
282-
setCanceledOnTouchOutside(false)
283-
setOnShowListener {
284-
val renewButton = getButton(AlertDialog.BUTTON_POSITIVE)
285-
renewButton.setOnClickListener {
286-
287-
val addDays = daysInput.selectedItem.toString().toInt()
288-
val newEndDate = DateTime(drmModel.end).plusDays(addDays)
289-
290-
launch {
291-
drmModel.renewLoan(newEndDate)
292-
.onSuccess {
293-
dismiss()
294-
endTextView.text =
295-
newEndDate?.toString(DateTimeFormat.shortDateTime())
296-
}.onFailure { exception ->
297-
dismiss()
298-
(exception as? UserException)?.getUserMessage(this@DRMManagementActivity)
299-
?.let { longSnackbar(it) }
300-
}
301-
}
250+
launch {
251+
drmModel.renewLoan()
252+
.onSuccess { newDate ->
253+
endTextView.text = newDate?.let { DateTime(it).toString(DateTimeFormat.shortDateTime()) }
254+
}.onFailure { exception ->
255+
(exception as? UserException)?.getUserMessage(this@DRMManagementActivity)
256+
?.let { longSnackbar(it) }
302257
}
303-
}
304258
}
305-
renewDialog.show()
259+
306260
}
307261
}.lparams(width = matchParent, height = wrapContent, weight = 1f)
308262
}

r2-testapp/src/main/java/org/readium/r2/testapp/drm/DRMViewModel.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
* LICENSE file present in the project repository where this source code is maintained.
99
*/
1010

11-
package org.readium.r2.testapp
11+
package org.readium.r2.testapp.drm
1212

1313
import android.content.Context
14-
import org.joda.time.DateTime
1514
import org.readium.r2.shared.util.Try
1615
import java.io.Serializable
16+
import java.util.*
1717

1818

1919
abstract class DRMViewModel(val context: Context) : Serializable {
@@ -24,13 +24,13 @@ abstract class DRMViewModel(val context: Context) : Serializable {
2424

2525
open val provider: String? = null
2626

27-
open val issued: DateTime? = null
27+
open val issued: Date? = null
2828

29-
open val updated: DateTime? = null
29+
open val updated: Date? = null
3030

31-
open val start: DateTime? = null
31+
open val start: Date? = null
3232

33-
open val end: DateTime? = null
33+
open val end: Date? = null
3434

3535
open val copiesLeft: String = "unlimited"
3636

@@ -39,7 +39,7 @@ abstract class DRMViewModel(val context: Context) : Serializable {
3939
open val canRenewLoan: Boolean
4040
get() = false
4141

42-
open suspend fun renewLoan(end: DateTime?): Try<Unit, Exception> =
42+
open suspend fun renewLoan(): Try<Date?, Exception> =
4343
Try.failure(Exception("Renewing a loan is not supported"))
4444

4545
open val canReturnPublication: Boolean

r2-testapp/src/main/java/org/readium/r2/testapp/drm/LCPViewModel.kt

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,77 +9,68 @@
99
* LICENSE file present in the project repository where this source code is maintained.
1010
*/
1111

12-
package org.readium.r2.testapp
13-
14-
import android.net.Uri
15-
import androidx.activity.ComponentActivity
16-
import androidx.activity.result.contract.ActivityResultContracts
17-
import androidx.browser.customtabs.CustomTabsIntent
18-
import kotlinx.coroutines.runBlocking
19-
import org.joda.time.DateTime
12+
package org.readium.r2.testapp.drm
13+
14+
import androidx.fragment.app.FragmentActivity
2015
import org.readium.r2.lcp.LcpLicense
2116
import org.readium.r2.lcp.LcpService
17+
import org.readium.r2.lcp.MaterialRenewListener
2218
import org.readium.r2.shared.util.Try
2319
import java.io.File
2420
import java.io.Serializable
25-
import java.net.URL
26-
import kotlin.coroutines.resume
27-
import kotlin.coroutines.suspendCoroutine
21+
import java.util.*
22+
23+
class LCPViewModel(val lcpLicense: LcpLicense, val activity: FragmentActivity) : DRMViewModel(activity), Serializable {
2824

29-
class LCPViewModel(val file: File, val activity: ComponentActivity) : DRMViewModel(activity), Serializable {
25+
companion object {
26+
27+
suspend fun from(file: File, activity: FragmentActivity): LCPViewModel? {
28+
val service = LcpService(activity) ?: return null
29+
val license = service.retrieveLicense(file, allowUserInteraction = false)?.getOrNull() ?: return null
30+
return LCPViewModel(license, activity)
31+
}
3032

31-
private val lcpLicense: LcpLicense? = runBlocking {
32-
LcpService(activity)!!
33-
.retrieveLicense(file, allowUserInteraction = false)
34-
?.getOrNull()
3533
}
3634

3735
override val type: String = "LCP"
3836

39-
override val state: String? = lcpLicense?.status?.status?.rawValue
37+
override val state: String? = lcpLicense.status?.status?.rawValue
4038

41-
override val provider: String? = lcpLicense?.license?.provider
39+
override val provider: String? = lcpLicense.license.provider
4240

43-
override val issued: DateTime? = lcpLicense?.license?.issued
41+
override val issued: Date? = lcpLicense.license.issued
4442

45-
override val updated: DateTime? = lcpLicense?.license?.updated
43+
override val updated: Date? = lcpLicense.license.updated
4644

47-
override val start: DateTime? = lcpLicense?.license?.rights?.start
45+
override val start: Date? = lcpLicense.license.rights.start
4846

49-
override val end: DateTime? = lcpLicense?.license?.rights?.end
47+
override val end: Date? = lcpLicense.license.rights.end
5048

5149
override val copiesLeft: String =
52-
lcpLicense?.charactersToCopyLeft
50+
lcpLicense.charactersToCopyLeft
5351
?.let { "$it characters" }
5452
?: super.copiesLeft
5553

5654
override val printsLeft: String =
57-
lcpLicense?.pagesToPrintLeft
55+
lcpLicense.pagesToPrintLeft
5856
?.let { "$it pages" }
5957
?: super.printsLeft
6058

61-
override val canRenewLoan: Boolean = lcpLicense?.canRenewLoan ?: false
59+
override val canRenewLoan: Boolean = lcpLicense.canRenewLoan
6260

63-
override suspend fun renewLoan(end: DateTime?): Try<Unit, Exception> {
64-
val lcpLicense = lcpLicense ?: return super.renewLoan(end)
61+
override suspend fun renewLoan(): Try<Date?, Exception> =
62+
lcpLicense.renewLoan(renewListener)
6563

66-
suspend fun urlPresenter(url: URL): Unit = suspendCoroutine { cont ->
67-
val intent = CustomTabsIntent.Builder().build().intent.apply {
68-
data = Uri.parse(url.toString())
69-
}
70-
71-
val launcher = activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
72-
cont.resume(Unit)
73-
}
74-
launcher.launch(intent)
75-
}
76-
77-
return lcpLicense.renewLoan(end, ::urlPresenter)
78-
}
64+
private val renewListener = MaterialRenewListener(
65+
license = lcpLicense,
66+
caller = activity,
67+
fragmentManager = activity.supportFragmentManager
68+
)
7969

8070
override val canReturnPublication: Boolean
81-
get() = lcpLicense?.canReturnPublication ?: false
71+
get() = lcpLicense.canReturnPublication
8272

8373
override suspend fun returnPublication(): Try<Unit, Exception> =
84-
lcpLicense?.returnPublication() ?: super.returnPublication()
74+
lcpLicense.returnPublication()
75+
8576
}

r2-testapp/src/main/java/org/readium/r2/testapp/epub/EpubActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ import org.readium.r2.shared.publication.presentation.presentation
5858
import org.readium.r2.shared.publication.services.isProtected
5959
import org.readium.r2.shared.publication.services.positions
6060
import org.readium.r2.testapp.BuildConfig.DEBUG
61-
import org.readium.r2.testapp.DRMManagementActivity
6261
import org.readium.r2.testapp.R
6362
import org.readium.r2.testapp.db.*
63+
import org.readium.r2.testapp.drm.DRMManagementActivity
6464
import org.readium.r2.testapp.library.LibraryActivity
6565
import org.readium.r2.testapp.library.activitiesLaunched
6666
import org.readium.r2.testapp.outline.R2OutlineActivity

r2-testapp/src/main/res/values/styles.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<resources>
1212

1313
<!-- Base application theme. -->
14-
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
14+
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
1515
<!-- Customize your theme here. -->
1616
<item name="colorPrimary">@color/colorPrimary</item>
1717
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

0 commit comments

Comments
 (0)