From e6994031b767a043f4ca6af82b3799b28c19d770 Mon Sep 17 00:00:00 2001 From: Tuomas Airaksinen Date: Thu, 1 Feb 2024 17:07:26 +0200 Subject: [PATCH] Fix #3191 (Crash: NoSuchKeyException: No entry for '06635 ' in StrongsHebrewRU) --- .../bible/android/control/link/LinkControl.kt | 2 +- .../activity/page/BibleJavascriptInterface.kt | 23 ++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/net/bible/android/control/link/LinkControl.kt b/app/src/main/java/net/bible/android/control/link/LinkControl.kt index c999082a2c..c9299f3d4f 100644 --- a/app/src/main/java/net/bible/android/control/link/LinkControl.kt +++ b/app/src/main/java/net/bible/android/control/link/LinkControl.kt @@ -233,7 +233,7 @@ class LinkControl @Inject constructor( enum class StrongsKeyType {HEBREW, GREEK} - private fun getStrongsKey(book: Book, key: String): BookAndKey? { + fun getStrongsKey(book: Book, key: String): BookAndKey? { val match = Regex("^([GH])(0*)([0-9]+).*").find(key) val category = match?.groups?.get(1)?.value ?: if(book.isHebrewDef) "H" diff --git a/app/src/main/java/net/bible/android/view/activity/page/BibleJavascriptInterface.kt b/app/src/main/java/net/bible/android/view/activity/page/BibleJavascriptInterface.kt index 84dd3641ca..0638ef056f 100644 --- a/app/src/main/java/net/bible/android/view/activity/page/BibleJavascriptInterface.kt +++ b/app/src/main/java/net/bible/android/view/activity/page/BibleJavascriptInterface.kt @@ -69,6 +69,7 @@ import org.crosswire.jsword.book.Books import org.crosswire.jsword.book.sword.SwordBook import org.crosswire.jsword.book.sword.SwordGenBook import org.crosswire.jsword.passage.KeyUtil +import org.crosswire.jsword.passage.NoSuchKeyException import org.crosswire.jsword.passage.RangedPassage import org.crosswire.jsword.passage.Verse import org.crosswire.jsword.passage.VerseFactory @@ -82,6 +83,7 @@ class BibleJavascriptInterface( private val bibleView: BibleView ) { private val currentPageManager: CurrentPageManager get() = bibleView.window.pageManager + val linkControl get() = bibleView.linkControl val bookmarkControl get() = bibleView.bookmarkControl val downloadControl get() = bibleView.downloadControl @@ -223,7 +225,7 @@ class BibleJavascriptInterface( val backend = book.backend as EpubBackend val key = backend.getKey(toKeyStr, toId) ?: return scope.launch(Dispatchers.Main) { - bibleView.linkControl.showLink(book, BookAndKey(key, book, htmlId = toId)) + linkControl.showLink(book, BookAndKey(key, book, htmlId = toId)) } } @@ -238,7 +240,7 @@ class BibleJavascriptInterface( val lnk = "${bibleBook.osis} $rest" val bibleLink = BibleView.BibleLink("content", target=lnk) scope.launch(Dispatchers.Main) { - bibleView.linkControl.loadApplicationUrl(bibleLink) + linkControl.loadApplicationUrl(bibleLink) } } link.startsWith("S:") -> { @@ -246,7 +248,7 @@ class BibleJavascriptInterface( val (prefix, rest) = link.split(":", limit=2) val bibleLink = BibleView.BibleLink("strong", target=rest) scope.launch(Dispatchers.Main) { - bibleView.linkControl.loadApplicationUrl(bibleLink) + linkControl.loadApplicationUrl(bibleLink) } } link.startsWith("#b") -> { @@ -257,7 +259,7 @@ class BibleJavascriptInterface( val lnk = "${bibleBook.osis}.$chapInt.$verInt" val bibleLink = BibleView.BibleLink("content", target=lnk) scope.launch(Dispatchers.Main) { - bibleView.linkControl.loadApplicationUrl(bibleLink) + linkControl.loadApplicationUrl(bibleLink) } } link.startsWith("#s") || link.startsWith("#d") -> { @@ -265,7 +267,7 @@ class BibleJavascriptInterface( val rest = link.substring(2) val bibleLink = BibleView.BibleLink("strong", target=rest) scope.launch(Dispatchers.Main) { - bibleView.linkControl.loadApplicationUrl(bibleLink) + linkControl.loadApplicationUrl(bibleLink) } } else -> { @@ -401,14 +403,14 @@ class BibleJavascriptInterface( @JavascriptInterface fun openStudyPad(labelId: String, bookmarkId: String) { scope.launch(Dispatchers.Main) { - bibleView.linkControl.openStudyPad(IdType(labelId), IdType(bookmarkId)) + linkControl.openStudyPad(IdType(labelId), IdType(bookmarkId)) } } @JavascriptInterface fun openMyNotes(v11n: String, ordinal: Int) { scope.launch(Dispatchers.Main) { - bibleView.linkControl.openMyNotes(v11n, ordinal) + linkControl.openMyNotes(v11n, ordinal) } } @@ -429,7 +431,12 @@ class BibleJavascriptInterface( fun speakGeneric(bookInitials: String, osisRef: String, ordinal: Int, endOrdinal: Int) { scope.launch(Dispatchers.Main) { val book = Books.installed().getBook(bookInitials) - val origKey = book.getKey(osisRef) + val origKey = try { + book.getKey(osisRef) + } catch (e: NoSuchKeyException) { + val bookAndKey = linkControl.getStrongsKey(book, osisRef) + bookAndKey?.key ?: return@launch + } val key = (origKey as? RangedPassage)?.toVerseRange ?: try {KeyUtil.getVerse(origKey)} catch (e: ClassCastException) {origKey} val ordinalRange = OrdinalRange(ordinal, positiveOrNull(endOrdinal)) val bookAndKey = BookAndKey(key, book, ordinalRange)