Skip to content

Commit b0967f7

Browse files
authored
Display error details in the Test App (readium#444)
1 parent eb2f4e3 commit b0967f7

18 files changed

+224
-181
lines changed

test-app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
<!-- android:networkSecurityConfig is required for r2-lcp-kotlin and r2-navigator-kotlin -->
2525
<application
2626
android:name=".Application"
27-
android:allowBackup="false"
2827
android:icon="@mipmap/ic_launcher"
2928
android:label="Readium"
3029
android:supportsRtl="true"
3130
android:theme="@style/AppTheme"
3231
android:networkSecurityConfig="@xml/network_security_config"
33-
tools:replace="android:allowBackup"
3432
tools:targetApi="n">
3533
<activity android:name=".MainActivity"
3634
android:clearTaskOnLaunch="true"

test-app/src/main/java/org/readium/r2/testapp/MainActivity.kt

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import androidx.navigation.ui.setupActionBarWithNavController
1616
import androidx.navigation.ui.setupWithNavController
1717
import com.google.android.material.bottomnavigation.BottomNavigationView
1818
import com.google.android.material.snackbar.Snackbar
19-
import org.readium.r2.shared.util.toDebugDescription
20-
import timber.log.Timber
2119

2220
class MainActivity : AppCompatActivity() {
2321

@@ -51,20 +49,17 @@ class MainActivity : AppCompatActivity() {
5149
}
5250

5351
private fun handleEvent(event: MainViewModel.Event) {
54-
val message =
55-
when (event) {
56-
is MainViewModel.Event.ImportPublicationSuccess ->
57-
getString(R.string.import_publication_success)
52+
when (event) {
53+
is MainViewModel.Event.ImportPublicationSuccess ->
54+
Snackbar.make(
55+
findViewById(android.R.id.content),
56+
getString(R.string.import_publication_success),
57+
Snackbar.LENGTH_LONG
58+
).show()
5859

59-
is MainViewModel.Event.ImportPublicationError -> {
60-
Timber.e(event.error.toDebugDescription())
61-
event.error.toUserError().getUserMessage(this)
62-
}
60+
is MainViewModel.Event.ImportPublicationError -> {
61+
event.error.toUserError().show(this)
6362
}
64-
Snackbar.make(
65-
findViewById(android.R.id.content),
66-
message,
67-
Snackbar.LENGTH_LONG
68-
).show()
63+
}
6964
}
7065
}

test-app/src/main/java/org/readium/r2/testapp/bookshelf/BookshelfFragment.kt

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ import androidx.fragment.app.Fragment
2121
import androidx.fragment.app.activityViewModels
2222
import androidx.recyclerview.widget.RecyclerView
2323
import com.google.android.material.dialog.MaterialAlertDialogBuilder
24-
import com.google.android.material.snackbar.Snackbar
2524
import org.readium.r2.shared.util.AbsoluteUrl
26-
import org.readium.r2.shared.util.toDebugDescription
2725
import org.readium.r2.testapp.Application
2826
import org.readium.r2.testapp.R
2927
import org.readium.r2.testapp.data.model.Book
3028
import org.readium.r2.testapp.databinding.FragmentBookshelfBinding
3129
import org.readium.r2.testapp.opds.GridAutoFitLayoutManager
3230
import org.readium.r2.testapp.reader.ReaderActivityContract
3331
import org.readium.r2.testapp.utils.viewLifecycle
34-
import timber.log.Timber
3532

3633
class BookshelfFragment : Fragment() {
3734

@@ -154,28 +151,18 @@ class BookshelfFragment : Fragment() {
154151
}
155152

156153
private fun handleEvent(event: BookshelfViewModel.Event) {
157-
val message =
158-
when (event) {
159-
is BookshelfViewModel.Event.OpenPublicationError -> {
160-
Timber.e(event.error.toDebugDescription())
161-
(event.error).toUserError().getUserMessage(requireContext())
162-
}
154+
when (event) {
155+
is BookshelfViewModel.Event.OpenPublicationError -> {
156+
event.error.toUserError().show(requireActivity())
157+
}
163158

164-
is BookshelfViewModel.Event.LaunchReader -> {
165-
val intent = ReaderActivityContract().createIntent(
166-
requireContext(),
167-
event.arguments
168-
)
169-
startActivity(intent)
170-
null
171-
}
159+
is BookshelfViewModel.Event.LaunchReader -> {
160+
val intent = ReaderActivityContract().createIntent(
161+
requireContext(),
162+
event.arguments
163+
)
164+
startActivity(intent)
172165
}
173-
message?.let {
174-
Snackbar.make(
175-
requireView(),
176-
it,
177-
Snackbar.LENGTH_LONG
178-
).show()
179166
}
180167
}
181168

test-app/src/main/java/org/readium/r2/testapp/domain/ImportError.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ sealed class ImportError(
4747
ImportError(cause)
4848

4949
fun toUserError(): UserError = when (this) {
50-
is MissingLcpSupport -> UserError(R.string.missing_lcp_support)
51-
is Database -> UserError(R.string.import_publication_unable_add_pub_database)
52-
is DownloadFailed -> UserError(R.string.import_publication_download_failed)
50+
is MissingLcpSupport -> UserError(R.string.missing_lcp_support, cause = this)
51+
is Database -> UserError(R.string.import_publication_unable_add_pub_database, cause = this)
52+
is DownloadFailed -> UserError(R.string.import_publication_download_failed, cause = this)
5353
is LcpAcquisitionFailed -> cause.toUserError()
54-
is Opds -> UserError(R.string.import_publication_no_acquisition)
54+
is Opds -> UserError(R.string.import_publication_no_acquisition, cause = this)
5555
is Publication -> cause.toUserError()
5656
is FileSystem -> cause.toUserError()
5757
}

test-app/src/main/java/org/readium/r2/testapp/domain/LcpUserError.kt

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,101 +12,111 @@ import org.readium.r2.testapp.utils.UserError
1212

1313
fun LcpError.toUserError(): UserError = when (this) {
1414
LcpError.LicenseInteractionNotAvailable ->
15-
UserError(R.string.lcp_error_license_interaction_not_available)
15+
UserError(R.string.lcp_error_license_interaction_not_available, cause = this)
1616
LcpError.LicenseProfileNotSupported ->
17-
UserError(R.string.lcp_error_license_profile_not_supported)
17+
UserError(R.string.lcp_error_license_profile_not_supported, cause = this)
1818
LcpError.CrlFetching ->
19-
UserError(R.string.lcp_error_crl_fetching)
19+
UserError(R.string.lcp_error_crl_fetching, cause = this)
2020
is LcpError.Network ->
21-
UserError(R.string.lcp_error_network)
21+
UserError(R.string.lcp_error_network, cause = this)
2222

2323
is LcpError.Runtime ->
24-
UserError(R.string.lcp_error_runtime)
24+
UserError(R.string.lcp_error_runtime, cause = this)
2525
is LcpError.Unknown ->
26-
UserError(R.string.lcp_error_unknown)
26+
UserError(R.string.lcp_error_unknown, cause = this)
2727

2828
is LcpError.Container ->
2929
when (this) {
3030
is LcpError.Container.FileNotFound ->
31-
UserError(R.string.lcp_error_container_file_not_found)
31+
UserError(R.string.lcp_error_container_file_not_found, cause = this)
3232
LcpError.Container.OpenFailed ->
33-
UserError(R.string.lcp_error_container_open_failed)
33+
UserError(R.string.lcp_error_container_open_failed, cause = this)
3434
is LcpError.Container.ReadFailed ->
35-
UserError(R.string.lcp_error_container_read_failed)
35+
UserError(R.string.lcp_error_container_read_failed, cause = this)
3636
is LcpError.Container.WriteFailed ->
37-
UserError(R.string.lcp_error_container_write_failed)
37+
UserError(R.string.lcp_error_container_write_failed, cause = this)
3838
}
3939

4040
is LcpError.Decryption ->
4141
when (this) {
4242
LcpError.Decryption.ContentDecryptError ->
43-
UserError(R.string.lcp_error_decryption_content_decrypt_error)
43+
UserError(R.string.lcp_error_decryption_content_decrypt_error, cause = this)
4444
LcpError.Decryption.ContentKeyDecryptError ->
45-
UserError(R.string.lcp_error_decryption_content_key_decrypt_error)
45+
UserError(R.string.lcp_error_decryption_content_key_decrypt_error, cause = this)
4646
}
4747

4848
is LcpError.LicenseIntegrity ->
4949
when (this) {
5050
LcpError.LicenseIntegrity.CertificateRevoked ->
51-
UserError(R.string.lcp_error_license_integrity_certificate_revoked)
51+
UserError(R.string.lcp_error_license_integrity_certificate_revoked, cause = this)
5252
LcpError.LicenseIntegrity.InvalidCertificateSignature ->
53-
UserError(R.string.lcp_error_license_integrity_invalid_certificate_signature)
53+
UserError(
54+
R.string.lcp_error_license_integrity_invalid_certificate_signature,
55+
cause = this
56+
)
5457
LcpError.LicenseIntegrity.InvalidLicenseSignature ->
55-
UserError(R.string.lcp_error_license_integrity_invalid_license_signature)
58+
UserError(
59+
R.string.lcp_error_license_integrity_invalid_license_signature,
60+
cause = this
61+
)
5662
LcpError.LicenseIntegrity.InvalidLicenseSignatureDate ->
57-
UserError(R.string.lcp_error_license_integrity_invalid_license_signature_date)
63+
UserError(
64+
R.string.lcp_error_license_integrity_invalid_license_signature_date,
65+
cause = this
66+
)
5867
LcpError.LicenseIntegrity.InvalidUserKeyCheck ->
59-
UserError(R.string.lcp_error_license_integrity_invalid_user_key_check)
68+
UserError(R.string.lcp_error_license_integrity_invalid_user_key_check, cause = this)
6069
}
6170

6271
is LcpError.LicenseStatus ->
6372
when (this) {
6473
is LcpError.LicenseStatus.Cancelled ->
65-
UserError(R.string.lcp_error_license_status_cancelled, date)
74+
UserError(R.string.lcp_error_license_status_cancelled, date, cause = this)
6675
is LcpError.LicenseStatus.Expired ->
67-
UserError(R.string.lcp_error_license_status_expired, end)
76+
UserError(R.string.lcp_error_license_status_expired, end, cause = this)
6877
is LcpError.LicenseStatus.NotStarted ->
69-
UserError(R.string.lcp_error_license_status_not_started, start)
78+
UserError(R.string.lcp_error_license_status_not_started, start, cause = this)
7079
is LcpError.LicenseStatus.Returned ->
71-
UserError(R.string.lcp_error_license_status_returned, date)
80+
UserError(R.string.lcp_error_license_status_returned, date, cause = this)
7281
is LcpError.LicenseStatus.Revoked ->
7382
UserError(
7483
R.plurals.lcp_error_license_status_revoked,
7584
devicesCount,
7685
date,
77-
devicesCount
86+
devicesCount,
87+
cause = this
7888
)
7989
}
8090

8191
is LcpError.Parsing ->
8292
when (this) {
8393
LcpError.Parsing.LicenseDocument ->
84-
UserError(R.string.lcp_error_parsing_license_document)
94+
UserError(R.string.lcp_error_parsing_license_document, cause = this)
8595
LcpError.Parsing.MalformedJSON ->
86-
UserError(R.string.lcp_error_parsing_malformed_json)
96+
UserError(R.string.lcp_error_parsing_malformed_json, cause = this)
8797
LcpError.Parsing.StatusDocument ->
88-
UserError(R.string.lcp_error_parsing_license_document)
98+
UserError(R.string.lcp_error_parsing_license_document, cause = this)
8999
else ->
90-
UserError(R.string.lcp_error_parsing)
100+
UserError(R.string.lcp_error_parsing, cause = this)
91101
}
92102

93103
is LcpError.Renew ->
94104
when (this) {
95105
is LcpError.Renew.InvalidRenewalPeriod ->
96-
UserError(R.string.lcp_error_renew_invalid_renewal_period)
106+
UserError(R.string.lcp_error_renew_invalid_renewal_period, cause = this)
97107
LcpError.Renew.RenewFailed ->
98-
UserError(R.string.lcp_error_renew_renew_failed)
108+
UserError(R.string.lcp_error_renew_renew_failed, cause = this)
99109
LcpError.Renew.UnexpectedServerError ->
100-
UserError(R.string.lcp_error_renew_unexpected_server_error)
110+
UserError(R.string.lcp_error_renew_unexpected_server_error, cause = this)
101111
}
102112

103113
is LcpError.Return ->
104114
when (this) {
105115
LcpError.Return.AlreadyReturnedOrExpired ->
106-
UserError(R.string.lcp_error_return_already_returned_or_expired)
116+
UserError(R.string.lcp_error_return_already_returned_or_expired, cause = this)
107117
LcpError.Return.ReturnFailed ->
108-
UserError(R.string.lcp_error_return_return_failed)
118+
UserError(R.string.lcp_error_return_return_failed, cause = this)
109119
LcpError.Return.UnexpectedServerError ->
110-
UserError(R.string.lcp_error_return_unexpected_server_error)
120+
UserError(R.string.lcp_error_return_unexpected_server_error, cause = this)
111121
}
112122
}

test-app/src/main/java/org/readium/r2/testapp/domain/PublicationError.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ sealed class PublicationError(
3535
fun toUserError(): UserError =
3636
when (this) {
3737
is InvalidPublication ->
38-
UserError(R.string.publication_error_invalid_publication)
38+
UserError(R.string.publication_error_invalid_publication, cause = this)
3939
is Unexpected ->
40-
UserError(R.string.publication_error_unexpected)
40+
UserError(R.string.publication_error_unexpected, cause = this)
4141
is FormatNotSupported ->
42-
UserError(R.string.publication_error_unsupported_asset)
42+
UserError(R.string.publication_error_unsupported_asset, cause = this)
4343
is UnsupportedScheme ->
44-
UserError(R.string.publication_error_scheme_not_supported)
44+
UserError(R.string.publication_error_scheme_not_supported, cause = this)
4545
is ReadError ->
4646
cause.toUserError()
4747
}

test-app/src/main/java/org/readium/r2/testapp/domain/ReadUserError.kt

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,73 @@ fun ReadError.toUserError(): UserError = when (this) {
2020
is HttpError -> cause.toUserError()
2121
is FileSystemError -> cause.toUserError()
2222
is ContentResolverError -> cause.toUserError()
23-
else -> UserError(R.string.error_unexpected)
23+
else -> UserError(R.string.error_unexpected, cause = this)
2424
}
2525

26-
is ReadError.Decoding -> UserError(R.string.publication_error_invalid_publication)
27-
is ReadError.OutOfMemory -> UserError(R.string.publication_error_out_of_memory)
28-
is ReadError.UnsupportedOperation -> UserError(R.string.publication_error_unexpected)
26+
is ReadError.Decoding -> UserError(R.string.publication_error_invalid_publication, cause = this)
27+
is ReadError.OutOfMemory -> UserError(R.string.publication_error_out_of_memory, cause = this)
28+
is ReadError.UnsupportedOperation -> UserError(
29+
R.string.publication_error_unexpected,
30+
cause = this
31+
)
2932
}
3033

3134
fun HttpError.toUserError(): UserError = when (this) {
32-
is HttpError.IO -> UserError(R.string.publication_error_network_unexpected)
33-
is HttpError.MalformedResponse -> UserError(R.string.publication_error_network_unexpected)
34-
is HttpError.Redirection -> UserError(R.string.publication_error_network_unexpected)
35-
is HttpError.Timeout -> UserError(R.string.publication_error_network_timeout)
36-
is HttpError.Unreachable -> UserError(R.string.publication_error_network_unreachable)
37-
is HttpError.SslHandshake -> UserError(R.string.publication_error_network_ssl_handshake)
35+
is HttpError.IO -> UserError(R.string.publication_error_network_unexpected, cause = this)
36+
is HttpError.MalformedResponse -> UserError(
37+
R.string.publication_error_network_unexpected,
38+
cause = this
39+
)
40+
is HttpError.Redirection -> UserError(
41+
R.string.publication_error_network_unexpected,
42+
cause = this
43+
)
44+
is HttpError.Timeout -> UserError(R.string.publication_error_network_timeout, cause = this)
45+
is HttpError.Unreachable -> UserError(
46+
R.string.publication_error_network_unreachable,
47+
cause = this
48+
)
49+
is HttpError.SslHandshake -> UserError(
50+
R.string.publication_error_network_ssl_handshake,
51+
cause = this
52+
)
3853
is HttpError.ErrorResponse -> when (status) {
39-
HttpStatus.Forbidden -> UserError(R.string.publication_error_network_forbidden)
40-
HttpStatus.NotFound -> UserError(R.string.publication_error_network_not_found)
41-
else -> UserError(R.string.publication_error_network_unexpected)
54+
HttpStatus.Forbidden -> UserError(
55+
R.string.publication_error_network_forbidden,
56+
cause = this
57+
)
58+
HttpStatus.NotFound -> UserError(R.string.publication_error_network_not_found, cause = this)
59+
else -> UserError(R.string.publication_error_network_unexpected, cause = this)
4260
}
4361
}
4462

4563
fun FileSystemError.toUserError(): UserError = when (this) {
46-
is FileSystemError.Forbidden -> UserError(R.string.publication_error_filesystem_unexpected)
47-
is FileSystemError.IO -> UserError(R.string.publication_error_filesystem_unexpected)
64+
is FileSystemError.Forbidden -> UserError(
65+
R.string.publication_error_filesystem_unexpected,
66+
cause = this
67+
)
68+
is FileSystemError.IO -> UserError(
69+
R.string.publication_error_filesystem_unexpected,
70+
cause = this
71+
)
4872
is FileSystemError.InsufficientSpace -> UserError(
49-
R.string.publication_error_filesystem_insufficient_space
73+
R.string.publication_error_filesystem_insufficient_space,
74+
cause = this
75+
)
76+
is FileSystemError.FileNotFound -> UserError(
77+
R.string.publication_error_filesystem_not_found,
78+
cause = this
5079
)
51-
is FileSystemError.FileNotFound -> UserError(R.string.publication_error_filesystem_not_found)
5280
}
5381

5482
fun ContentResolverError.toUserError(): UserError = when (this) {
5583
is ContentResolverError.FileNotFound -> UserError(
56-
R.string.publication_error_filesystem_not_found
84+
R.string.publication_error_filesystem_not_found,
85+
cause = this
86+
)
87+
is ContentResolverError.IO -> UserError(
88+
R.string.publication_error_filesystem_unexpected,
89+
cause = this
5790
)
58-
is ContentResolverError.IO -> UserError(R.string.publication_error_filesystem_unexpected)
59-
is ContentResolverError.NotAvailable -> UserError(R.string.error_unexpected)
91+
is ContentResolverError.NotAvailable -> UserError(R.string.error_unexpected, cause = this)
6092
}

0 commit comments

Comments
 (0)