Skip to content

Commit

Permalink
Fix crash after continuing generic tts
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Aug 22, 2023
1 parent 83b9529 commit cfe57dc
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions app/src/main/java/net/bible/android/control/speak/SpeakControl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import org.crosswire.jsword.book.BookCategory
import org.crosswire.jsword.book.Books
import org.crosswire.jsword.book.sword.SwordBook
import org.crosswire.jsword.passage.Key
import org.crosswire.jsword.passage.KeyUtil
import org.crosswire.jsword.passage.NoSuchKeyException
import org.crosswire.jsword.passage.RangedPassage
import org.crosswire.jsword.passage.Verse
Expand All @@ -61,6 +60,7 @@ import net.bible.android.database.bookmarks.SpeakSettings
import net.bible.service.common.AdvancedSpeakSettings
import net.bible.service.device.speak.MediaButtonHandler
import net.bible.service.sword.BookAndKey
import net.bible.service.sword.BookAndKeySerialized

/**
* @author Martin Denham [mjdenham at gmail dot com]
Expand Down Expand Up @@ -130,7 +130,7 @@ class SpeakControl @Inject constructor(
private val currentlyPlayingBook: Book?
get() = if (!booksAvailable || !ttsInitialized) null else ttsServiceManager.currentlyPlayingBook

private val currentlyPlayingVerse: Key?
private val currentlyPlayingKey: Key?
get() = if (!booksAvailable || !ttsInitialized) null else ttsServiceManager.currentlyPlayingKey

init {
Expand Down Expand Up @@ -299,12 +299,16 @@ class SpeakControl @Inject constructor(
speakBible(currentBook as SwordBook, verse)
}

private fun speakBible(bookRef: String, osisRef: String) {
val book = Books.installed().getBook(bookRef) as SwordBook

private fun speakAny(bookRef: String, osisRef: String) {
val book = Books.installed().getBook(bookRef)
try {
val verse = (book.getKey(osisRef) as RangedPassage).getVerseAt(0)
speakBible(book, verse)
if(book is SwordBook) {
val verse = (book.getKey(osisRef) as RangedPassage).getVerseAt(0)
speakBible(book, verse)
} else {
val key = BookAndKeySerialized.fromJSON(osisRef).bookAndKey
speakGeneric(key)
}
} catch (e: NoSuchKeyException) {
Log.e(TAG, "Key not found $osisRef in $currentBook")
}
Expand Down Expand Up @@ -370,10 +374,15 @@ class SpeakControl @Inject constructor(

private fun saveCurrentPosition() {
val bookRef = currentlyPlayingBook?.initials
val osisRef = currentlyPlayingVerse?.osisRef
val key = currentlyPlayingKey
val osisRef = if(key is BookAndKey) {
key.serialized
} else {
key?.osisRef
}
if(bookRef != null && osisRef != null) {
CommonUtils.settings.setString("lastSpeakBook",bookRef);
CommonUtils.settings.setString("lastSpeakRef",osisRef);
CommonUtils.settings.setString("lastSpeakRef", osisRef);
} else {
CommonUtils.settings.removeString("lastSpeakBook");
CommonUtils.settings.removeString("lastSpeakRef");
Expand All @@ -388,7 +397,7 @@ class SpeakControl @Inject constructor(
val bookRef = CommonUtils.settings.getString("lastSpeakBook")
val osisRef = CommonUtils.settings.getString("lastSpeakRef")
Log.i(TAG, "continueLastPosition $bookRef $osisRef")
if(bookRef != null && osisRef != null) speakBible(bookRef, osisRef)
if(bookRef != null && osisRef != null) speakAny(bookRef, osisRef)
else startSpeakingFromDefault()
}

Expand Down

0 comments on commit cfe57dc

Please sign in to comment.