Skip to content

Commit

Permalink
refactor: move Sound.kt out of libanki
Browse files Browse the repository at this point in the history
The equivalent to `sound.py` at Anki Desktop is actually SoundKt.kt, so there is no reason to keep `Sound.kt` at libanki
  • Loading branch information
BrayanDSO committed Dec 8, 2023
1 parent 1747ac9 commit 292490d
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import com.ichi2.anim.ActivityTransitionAnimation
import com.ichi2.anim.ActivityTransitionAnimation.getInverseTransition
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.Sound.OnErrorListener.ErrorHandling
import com.ichi2.anki.UIUtils.showThemedToast
import com.ichi2.anki.cardviewer.*
import com.ichi2.anki.cardviewer.CardHtml.Companion.legacyGetTtsTags
Expand Down Expand Up @@ -87,9 +88,6 @@ import com.ichi2.compat.ResolveInfoFlagsCompat
import com.ichi2.libanki.*
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts.BUTTON_TYPE
import com.ichi2.libanki.Sound.OnErrorListener.ErrorHandling
import com.ichi2.libanki.Sound.SingleSoundSide
import com.ichi2.libanki.Sound.SoundSide
import com.ichi2.themes.Themes
import com.ichi2.themes.Themes.getResFromAttr
import com.ichi2.ui.FixedEditText
Expand Down
1 change: 0 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/MetaDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.annotation.WorkerThread
import com.ichi2.anki.model.WhiteboardPenColor
import com.ichi2.anki.model.WhiteboardPenColor.Companion.default
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.Sound.SoundSide
import com.ichi2.utils.KotlinCleanup
import com.ichi2.widget.SmallWidgetStatus
import timber.log.Timber
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/ReadText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import android.speech.tts.UtteranceProgressListener
import android.view.WindowManager.BadTokenException
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import com.ichi2.anki.Sound.SoundSide
import com.ichi2.anki.UIUtils.showThemedToast
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.Sound.SoundSide
import com.ichi2.libanki.TTSTag
import com.ichi2.utils.HandlerUtils.postDelayedOnNewHandler
import com.ichi2.utils.message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/

package com.ichi2.libanki
package com.ichi2.anki

import android.content.Context
import android.media.*
Expand All @@ -26,10 +26,8 @@ import androidx.annotation.CheckResult
import androidx.annotation.VisibleForTesting
import androidx.media.AudioFocusRequestCompat
import androidx.media.AudioManagerCompat
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.ReadText
import com.ichi2.libanki.Sound.OnErrorListener.ErrorHandling.CONTINUE_AUDIO
import com.ichi2.libanki.Sound.SoundSide.*
import com.ichi2.libanki.SoundOrVideoTag
import com.ichi2.libanki.addPlayIcons
import timber.log.Timber
import java.util.*
import java.util.regex.Pattern
Expand Down Expand Up @@ -80,8 +78,8 @@ class Sound(private val soundPlayer: SoundPlayer, private val soundDir: String)
}

private fun getSoundList(side: SoundSide): List<SoundPath> {
if (side == QUESTION_AND_ANSWER) {
return getSoundList(QUESTION) + getSoundList(ANSWER)
if (side == SoundSide.QUESTION_AND_ANSWER) {
return getSoundList(SoundSide.QUESTION) + getSoundList(SoundSide.ANSWER)
}
return soundPaths[side] ?: emptyList()
}
Expand Down Expand Up @@ -178,9 +176,9 @@ class Sound(private val soundPlayer: SoundPlayer, private val soundDir: String)
ReadText.stopTts() // TODO: Reconsider design
}

fun hasQuestion(): Boolean = getSounds(QUESTION) != null
fun hasQuestion(): Boolean = getSounds(SoundSide.QUESTION) != null

fun hasAnswer(): Boolean = getSounds(ANSWER) != null
fun hasAnswer(): Boolean = getSounds(SoundSide.ANSWER) != null

fun interface OnErrorListener {
fun onError(mp: MediaPlayer?, which: Int, extra: Int, path: String?): ErrorHandling
Expand Down Expand Up @@ -294,7 +292,7 @@ open class SoundPlayer {
val errorHandler = errorListener
?: Sound.OnErrorListener { _: MediaPlayer?, what: Int, extra: Int, _: String? ->
Timber.w("Media Error: (%d, %d). Calling OnCompletionListener", what, extra)
CONTINUE_AUDIO
Sound.OnErrorListener.ErrorHandling.CONTINUE_AUDIO
}
playSoundInternal(soundPath, completionListener, errorHandler)
}
Expand Down Expand Up @@ -333,7 +331,7 @@ open class SoundPlayer {
)
// returning false calls onComplete()
return@setOnErrorListener when (errorHandling) {
CONTINUE_AUDIO -> false
Sound.OnErrorListener.ErrorHandling.CONTINUE_AUDIO -> false
Sound.OnErrorListener.ErrorHandling.RETRY_AUDIO -> {
playMedia()
true
Expand Down Expand Up @@ -370,7 +368,7 @@ open class SoundPlayer {
soundPath
)
) {
CONTINUE_AUDIO -> {
Sound.OnErrorListener.ErrorHandling.CONTINUE_AUDIO -> {
Timber.d("Force playing next sound.")
completionListener.onCompletion(mMediaPlayer)
}
Expand Down
7 changes: 4 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/CardHtml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package com.ichi2.anki.cardviewer

import android.content.Context
import com.ichi2.anki.R
import com.ichi2.anki.Sound
import com.ichi2.anki.Sound.SingleSoundSide
import com.ichi2.anki.Sound.SingleSoundSide.ANSWER
import com.ichi2.anki.Sound.SingleSoundSide.QUESTION
import com.ichi2.anki.TtsParser
import com.ichi2.anki.cardviewer.CardAppearance.Companion.hasUserDefinedNightMode
import com.ichi2.libanki.*
import com.ichi2.libanki.Sound.SingleSoundSide
import com.ichi2.libanki.Sound.SingleSoundSide.ANSWER
import com.ichi2.libanki.Sound.SingleSoundSide.QUESTION
import com.ichi2.libanki.template.MathJax
import com.ichi2.themes.HtmlColors
import com.ichi2.themes.Themes.currentTheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import android.content.Context
import android.content.res.Resources
import androidx.annotation.CheckResult
import anki.config.ConfigKey
import com.ichi2.anki.Sound
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.reviewer.ReviewerCustomFonts
import com.ichi2.libanki.Card
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Sound
import com.ichi2.libanki.stripAvRefs
import timber.log.Timber
import java.io.BufferedReader
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/TTS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import android.content.Context
import com.ichi2.anki.CardUtils
import com.ichi2.anki.R
import com.ichi2.anki.ReadText
import com.ichi2.anki.Sound.SoundSide
import com.ichi2.libanki.Card
import com.ichi2.libanki.Sound.SoundSide
import com.ichi2.libanki.TTSTag
import com.ichi2.libanki.Utils
import com.ichi2.libanki.template.TemplateFilters
Expand Down
3 changes: 2 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/libanki/TemplateManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

package com.ichi2.libanki

import com.ichi2.libanki.Sound.Companion.SOUND_RE
import com.ichi2.anki.Sound.Companion.SOUND_RE
import com.ichi2.anki.VIDEO_EXTENSIONS
import com.ichi2.libanki.TemplateManager.PartiallyRenderedCard.Companion.avTagsToNative
import com.ichi2.libanki.backend.BackendUtils
import com.ichi2.libanki.backend.model.toBackendNote
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.ichi2.anki

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.anki.cardviewer.ViewerCommand
import com.ichi2.libanki.Sound
import net.ankiweb.rsdroid.RustCleanup
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import com.ichi2.anki.servicelayer.NoteService
import com.ichi2.anki.servicelayer.NoteService.getFieldsAsBundleForPreview
import com.ichi2.libanki.Card
import com.ichi2.libanki.NotetypeJson
import com.ichi2.libanki.Sound
import com.ichi2.utils.KotlinCleanup
import com.ichi2.utils.stringIterable
import org.hamcrest.CoreMatchers.*
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/test/java/com/ichi2/anki/ReadTextTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.ichi2.anki.ReadText.closeForTests
import com.ichi2.anki.ReadText.initializeTts
import com.ichi2.anki.ReadText.releaseTts
import com.ichi2.anki.ReadText.textToSpeech
import com.ichi2.libanki.Sound.SoundSide.*
import com.ichi2.anki.Sound.SoundSide.*
import org.hamcrest.MatcherAssert.*
import org.hamcrest.Matchers.*
import org.junit.Before
Expand Down

0 comments on commit 292490d

Please sign in to comment.