Skip to content

Commit

Permalink
fix: crash when epub file cannot be opened
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Apr 16, 2022
1 parent 8cf4ce3 commit bd018e4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 33 deletions.
23 changes: 13 additions & 10 deletions app/src/main/java/de/baumann/browser/activity/BrowserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -697,16 +697,19 @@ open class BrowserActivity : ComponentActivity(), BrowserController, OnClickList
rawHtml,
bookName,
chapterName,
ninjaWebView.url ?: "") { savedBookName ->
progressDialog.dismiss()
HelperUnit.openEpubToLastChapter(this@BrowserActivity, fileUri)

// save epub file info to preference
val bookUri = fileUri.toString()
if (config.savedEpubFileInfos.none { it.uri == bookUri }) {
config.addSavedEpubFile(EpubFileInfo(savedBookName, bookUri))
}
}
ninjaWebView.url ?: "",
{ savedBookName ->
progressDialog.dismiss()
HelperUnit.openEpubToLastChapter(this@BrowserActivity, fileUri)

// save epub file info to preference
val bookUri = fileUri.toString()
if (config.savedEpubFileInfos.none { it.uri == bookUri }) {
config.addSavedEpubFile(EpubFileInfo(savedBookName, bookUri))
}
},
{ progressDialog.dismiss() }
)
}
}
}
Expand Down
43 changes: 32 additions & 11 deletions app/src/main/java/de/baumann/browser/epub/EpubManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import de.baumann.browser.Ninja.R
import de.baumann.browser.activity.BrowserActivity
import de.baumann.browser.activity.EpubReaderActivity
import de.baumann.browser.util.Constants
import de.baumann.browser.view.dialog.DialogManager
import de.baumann.browser.view.dialog.TextInputDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand All @@ -18,13 +19,15 @@ import nl.siegmann.epublib.epub.EpubReader
import nl.siegmann.epublib.epub.EpubWriter
import nl.siegmann.epublib.service.MediatypeService
import org.jsoup.Jsoup
import org.koin.core.component.KoinComponent
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL


class EpubManager(private val context: Context) {
class EpubManager(private val context: Context): KoinComponent {
private val dialogManager: DialogManager by lazy { DialogManager(context as Activity) }

suspend fun getChapterName(defaultTitle: String?): String? {
var chapterName = defaultTitle?: "no title"
Expand Down Expand Up @@ -62,30 +65,46 @@ class EpubManager(private val context: Context) {
bookName: String,
chapterName: String,
currentUrl: String,
doneAction: (String) -> Unit
doneAction: (String) -> Unit,
errorAction: () -> Unit
) {
val webUri = Uri.parse(currentUrl)
val domain = webUri.host ?: "EinkBro"
var hasSavedSuccess = false

withContext(Dispatchers.IO) {
val book = if (isNew) createBook(domain, bookName) else openBook(fileUri)
if (book != null) {

val chapterIndex = book.tableOfContents.allUniqueResources.size + 1
val chapterFileName = "chapter$chapterIndex.html"
val chapterIndex = book.tableOfContents.allUniqueResources.size + 1
val chapterFileName = "chapter$chapterIndex.html"

val (processedHtml, imageMap) = processHtmlString(html, chapterIndex, "${webUri.scheme}://${webUri.host}/")
val (processedHtml, imageMap) = processHtmlString(html, chapterIndex, "${webUri.scheme}://${webUri.host}/")

book.addSection(chapterName, Resource(processedHtml.byteInputStream(), chapterFileName))
book.addSection(chapterName, Resource(processedHtml.byteInputStream(), chapterFileName))

saveImageResources(book, imageMap)
saveImageResources(book, imageMap)

saveBook(book, fileUri)
saveBook(book, fileUri)

doneAction.invoke(book.title)
doneAction.invoke(book.title)

hasSavedSuccess = true
}
}

if (!hasSavedSuccess) {
errorAction()
dialogManager.showOkCancelDialog(
messageResId = R.string.cannot_open_file,
okAction = {},
showInCenter = true,
showNegativeButton = false,
)
}
}

fun showEpubReader(uri: Uri) {
fun showEpubReader(uri: Uri) {
val intent = Intent(context, EpubReaderActivity::class.java).apply {
data = uri
}
Expand All @@ -97,7 +116,7 @@ class EpubManager(private val context: Context) {
metadata.addAuthor(Author(domain, "EinkBro App"))
}

private fun openBook(uri: Uri): Book {
private fun openBook(uri: Uri): Book? {
try {
val takeFlags: Int = (Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
context.contentResolver.takePersistableUriPermission(uri, takeFlags)
Expand All @@ -108,6 +127,8 @@ class EpubManager(private val context: Context) {
return EpubReader().readEpub(epubInputStream)
} catch (e: IOException) {
return createBook("", "EinkBro")
} catch (e: SecurityException) {
return null
}
}

Expand Down
28 changes: 16 additions & 12 deletions app/src/main/java/de/baumann/browser/view/dialog/DialogManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,24 @@ class DialogManager(
messageResId: Int? = null,
view: View? = null,
okAction: () -> Unit,
cancelAction: (() -> Unit)? = null
cancelAction: (() -> Unit)? = null,
showInCenter: Boolean = false,
showNegativeButton: Boolean = true
): Dialog {
val dialog = AlertDialog.Builder(activity, R.style.TouchAreaDialog)
.setPositiveButton(android.R.string.ok) { _, _ -> okAction() }
.setNegativeButton(android.R.string.cancel) { _, _ -> cancelAction?.invoke() }
.apply {
title?.let { title -> setTitle(title) }
view?.let { setView(it) }
messageResId?.let { setMessage(messageResId) }
}
.create().apply {
window?.setGravity(if (config.isToolbarOnTop) Gravity.CENTER else Gravity.BOTTOM)
window?.setBackgroundDrawableResource(R.drawable.background_with_border_margin)
}
.setPositiveButton(android.R.string.ok) { _, _ -> okAction() }
.apply {
title?.let { title -> setTitle(title) }
view?.let { setView(it) }
messageResId?.let { setMessage(messageResId) }
if (showNegativeButton) {
setNegativeButton(android.R.string.cancel) { _, _ -> cancelAction?.invoke() }
}
}
.create().apply {
window?.setGravity(if (config.isToolbarOnTop || showInCenter) Gravity.CENTER else Gravity.BOTTOM)
window?.setBackgroundDrawableResource(R.drawable.background_with_border_margin)
}
dialog.show()
return dialog
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,5 @@
<string name="setting_summary_vi_binding">启用VI键值绑定以简化键盘导航。</string>
<string name="edit_custom_font">选择自定义字体</string>
<string name="reload_font_change">刷新切换字体</string>
<string name="cannot_open_file">File can\'t be opened. Please re-open it from file picker.</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,5 @@
<string name="setting_summary_vi_binding">enable VI key bindings for eaiser keyboard navigation.</string>
<string name="edit_custom_font">設定自訂定型</string>
<string name="reload_font_change">重新載入網頁以更新字型</string>
<string name="cannot_open_file">無法開啟文件。請重新在文件管理器中選擇。</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,5 @@
<string name="setting_summary_vi_binding">enable VI key bindings for eaiser keyboard navigation.</string>
<string name="edit_custom_font">Edit Custom Font</string>
<string name="reload_font_change">Reload to take effect of font change</string>
<string name="cannot_open_file">File can\'t be opened. Please re-open it from file picker.</string>
</resources>

0 comments on commit bd018e4

Please sign in to comment.