Skip to content

Commit

Permalink
Merge pull request #157 from firemaples/fix/fix-issues-on-3.0.1
Browse files Browse the repository at this point in the history
Fix/fix issues on 3.0.1
  • Loading branch information
firemaples authored Oct 27, 2021
2 parents 78cad7d + cee774d commit cdc0b6a
Show file tree
Hide file tree
Showing 23 changed files with 221 additions and 38 deletions.
4 changes: 4 additions & 0 deletions main/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

<queries>
<package android:name="com.google.android.apps.translate" />

<intent>
<action android:name="android.intent.action.PROCESS_TEXT" />
</intent>
</queries>

<application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import tw.firemaples.onscreenocr.R
import tw.firemaples.onscreenocr.floatings.manager.FloatingStateManager
import tw.firemaples.onscreenocr.pages.launch.LaunchActivity
import tw.firemaples.onscreenocr.pages.setting.SettingManager
import tw.firemaples.onscreenocr.remoteconfig.RemoteConfigManager
import tw.firemaples.onscreenocr.screenshot.ScreenExtractor
Expand Down Expand Up @@ -100,11 +101,15 @@ class ViewHolderService : Service() {
}

private fun showViews() {
FloatingStateManager.showMainBar()
if (ScreenExtractor.isGranted) {
FloatingStateManager.showMainBar()
} else {
startActivity(LaunchActivity.getLaunchIntent(this))
}
}

private fun hideViews() {
FloatingStateManager.hideMainBar()
FloatingStateManager.detachAllViews()
}

private fun exit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ import kotlin.coroutines.CoroutineContext

abstract class FloatingView(protected val context: Context) {

companion object {
private val attachedFloatingViews: MutableList<FloatingView> = mutableListOf()

fun detachAllFloatingViews() {
attachedFloatingViews.toList().forEach { it.detachFromScreen() }
}
}

private val logger: Logger by lazy { Logger(this::class) }

private val windowManager: WindowManager by lazy { context.getSystemService(Context.WINDOW_SERVICE) as WindowManager }
Expand Down Expand Up @@ -109,6 +117,8 @@ abstract class FloatingView(protected val context: Context) {

lifecycleOwner.onStateChanged(Lifecycle.State.RESUMED)

attachedFloatingViews.add(this)

attached = true
}

Expand All @@ -130,6 +140,8 @@ abstract class FloatingView(protected val context: Context) {

lifecycleOwner.onStateChanged(Lifecycle.State.CREATED)

attachedFloatingViews.remove(this)

attached = false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ abstract class MovableFloatingView(context: Context) : FloatingView(context) {
}

//region Moving to edge
fun moveToEdgeIfEnabled() {
rootView.post { if (moveToEdgeAfterMoved) moveToEdge() }
}

private fun moveToEdge() {
val params = params

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,27 @@ class MainBar(context: Context) : MovableFloatingView(context) {

viewModel.languageText.observe(lifecycleOwner) {
tvLang.text = it
moveToEdgeIfEnabled()
}

viewModel.displayGoogleTranslateIcon.observe(lifecycleOwner) {
ivGoogleTranslator.showOrHide(it)
moveToEdgeIfEnabled()
}

viewModel.displaySelectButton.observe(lifecycleOwner) {
btSelect.showOrHide(it)
moveToEdgeIfEnabled()
}

viewModel.displayTranslateButton.observe(lifecycleOwner) {
btTranslate.showOrHide(it)
moveToEdgeIfEnabled()
}

viewModel.displayCloseButton.observe(lifecycleOwner) {
btClose.showOrHide(it)
moveToEdgeIfEnabled()
}

viewModel.displayMenuItems.observe(lifecycleOwner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import tw.firemaples.onscreenocr.R
import tw.firemaples.onscreenocr.floatings.base.FloatingView
import tw.firemaples.onscreenocr.floatings.dialog.DialogView
import tw.firemaples.onscreenocr.floatings.main.MainBar
import tw.firemaples.onscreenocr.floatings.result.ResultView
Expand All @@ -21,6 +22,7 @@ import tw.firemaples.onscreenocr.screenshot.ScreenExtractor
import tw.firemaples.onscreenocr.translator.TranslationProviderType
import tw.firemaples.onscreenocr.translator.TranslationResult
import tw.firemaples.onscreenocr.translator.Translator
import tw.firemaples.onscreenocr.utils.Constants
import tw.firemaples.onscreenocr.utils.Logger
import tw.firemaples.onscreenocr.utils.Utils
import kotlin.reflect.KClass
Expand Down Expand Up @@ -62,19 +64,16 @@ object FloatingStateManager {
private var selectedRect: Rect? = null
private var croppedBitmap: Bitmap? = null

fun toggleMainBar() {
if (isMainBarAttached) hideMainBar()
else showMainBar()
}

fun showMainBar() {
if (isMainBarAttached) return
mainBar.attachToScreen()
scope.launch {
_showingStateChangedFlow.emit(true)
}
}

fun hideMainBar() {
if (!isMainBarAttached) return
mainBar.detachFromScreen()
scope.launch {
_showingStateChangedFlow.emit(false)
Expand All @@ -86,6 +85,14 @@ object FloatingStateManager {
mainBar.attachToScreen()
}

fun detachAllViews() {
backToIdle()
scope.launch {
hideMainBar()
FloatingView.detachAllFloatingViews()
}
}

fun startScreenCircling() = stateIn(State.Idle::class) {
if (!Translator.getTranslator().checkEnvironment(scope)) {
return@stateIn
Expand Down Expand Up @@ -160,11 +167,15 @@ object FloatingStateManager {
resultView.textRecognized(result, parent, selected)
startTranslation(result)
} catch (e: Exception) {
val error =
if (e.message?.contains(Constants.errorInputImageIsTooSmall) == true) {
context.getString(R.string.error_selected_area_too_small)
} else
e.message
?: context.getString(R.string.error_an_unknown_error_found_while_recognition_text)

logger.warn(t = e)
showError(
e.message
?: context.getString(R.string.error_an_unknown_error_found_while_recognition_text)
)
showError(error)
FirebaseEvent.logOCRFailed(TextRecognizer.getRecognizer().name, e)
}
}
Expand Down Expand Up @@ -192,6 +203,18 @@ object FloatingStateManager {
FirebaseEvent.logTranslationTextFinished(translator)
backToIdle()
}
is TranslationResult.SourceLangNotSupport -> {
FirebaseEvent.logTranslationSourceLangNotSupport(
translator, recognitionResult.langCode,
)
showResult(
Result.SourceLangNotSupport(
ocrText = recognitionResult.result,
boundingBoxes = recognitionResult.boundingBoxes,
providerType = translationResult.type,
)
)
}
TranslationResult.OCROnlyResult -> {
FirebaseEvent.logTranslationTextFinished(translator)
showResult(
Expand Down Expand Up @@ -249,14 +272,11 @@ object FloatingStateManager {
}
}

private fun backToIdle() =
stateIn(
State.TextTranslating::class,
State.ResultDisplaying::class,
State.ErrorDisplaying::class
) {
changeState(State.Idle)
fun backToIdle() =
scope.launch {
if (currentState != State.Idle) changeState(State.Idle)
resultView.backToIdle()
showMainBar()
}

private fun stateIn(
Expand All @@ -274,9 +294,13 @@ object FloatingStateManager {
State.ScreenCircling -> arrayOf(State.Idle::class, State.ScreenCircled::class)
State.ScreenCircled -> arrayOf(State.Idle::class, State.ScreenCapturing::class)
State.ScreenCapturing ->
arrayOf(State.TextRecognizing::class, State.ErrorDisplaying::class)
arrayOf(
State.Idle::class, State.TextRecognizing::class, State.ErrorDisplaying::class
)
State.TextRecognizing ->
arrayOf(State.TextTranslating::class, State.ErrorDisplaying::class)
arrayOf(
State.Idle::class, State.TextTranslating::class, State.ErrorDisplaying::class
)
State.TextTranslating ->
arrayOf(
State.ResultDisplaying::class, State.ErrorDisplaying::class, State.Idle::class
Expand Down Expand Up @@ -320,6 +344,12 @@ sealed class Result(
val providerType: TranslationProviderType,
) : Result(ocrText, boundingBoxes)

data class SourceLangNotSupport(
override val ocrText: String,
override val boundingBoxes: List<Rect>,
val providerType: TranslationProviderType,
) : Result(ocrText, boundingBoxes)

data class OCROnly(
override val ocrText: String,
override val boundingBoxes: List<Rect>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.widget.EditText
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.constraintlayout.widget.Group
import androidx.core.content.ContextCompat
import tw.firemaples.onscreenocr.R
import tw.firemaples.onscreenocr.floatings.base.FloatingView
import tw.firemaples.onscreenocr.floatings.dialog.DialogView
Expand Down Expand Up @@ -87,7 +88,15 @@ class ResultView(context: Context) : FloatingView(context) {
tvOCRText.text = it
}
viewModel.translatedText.observe(lifecycleOwner) {
tvTranslatedText.text = it
if (it == null) {
tvTranslatedText.text = null
} else {
val (text, color) = it
tvTranslatedText.text = text
tvTranslatedText.setTextColor(ContextCompat.getColor(context, color))
}

reposition()
}

viewModel.displayRecognitionBlock.observe(lifecycleOwner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ResultViewModel(viewScope: CoroutineScope) : FloatingViewModel(viewScope)
private val _ocrText = MutableLiveData<String?>()
val ocrText: LiveData<String?> = _ocrText

private val _translatedText = MutableLiveData<String?>()
val translatedText: LiveData<String?> = _translatedText
private val _translatedText = MutableLiveData<Pair<String, Int>?>()
val translatedText: LiveData<Pair<String, Int>?> = _translatedText

private val _displayRecognitionBlock = MutableLiveData<Boolean>()
val displayRecognitionBlock: LiveData<Boolean> = _displayRecognitionBlock
Expand Down Expand Up @@ -135,12 +135,16 @@ class ResultViewModel(viewScope: CoroutineScope) : FloatingViewModel(viewScope)

when (result) {
is Result.Translated -> {
_translatedText.value = result.translatedText
_translatedText.value = result.translatedText to R.color.foregroundSecond

if (repo.hideRecognizedTextAfterTranslated().first()) {
_displayRecognitionBlock.value = false
}
}
is Result.SourceLangNotSupport -> {
_translatedText.value =
context.getString(R.string.msg_translator_provider_does_not_support_the_ocr_lang) to R.color.alert
}
is Result.OCROnly -> {
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package tw.firemaples.onscreenocr.floatings.translationSelectPanel

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import tw.firemaples.onscreenocr.R
import tw.firemaples.onscreenocr.floatings.base.FloatingViewModel
import tw.firemaples.onscreenocr.repo.OCRRepository
import tw.firemaples.onscreenocr.repo.TranslationRepository
import tw.firemaples.onscreenocr.translator.TranslationProvider
import tw.firemaples.onscreenocr.utils.Utils
import tw.firemaples.onscreenocr.utils.firstPart

class TranslationSelectPanelViewModel(viewScope: CoroutineScope) :
FloatingViewModel(viewScope) {
Expand All @@ -29,6 +33,8 @@ class TranslationSelectPanelViewModel(viewScope: CoroutineScope) :
private val _displayTranslationHint = MutableLiveData<String?>()
val displayTranslationHint: LiveData<String?> = _displayTranslationHint

private val context: Context by lazy { Utils.context }

private val ocrRepo = OCRRepository()
private val translationRepo = TranslationRepository()

Expand All @@ -49,13 +55,24 @@ class TranslationSelectPanelViewModel(viewScope: CoroutineScope) :
private suspend fun loadTranslationLanguageList(providerKey: String) {
val translationLanguages =
translationRepo.getTranslationLanguageList(providerKey).first()

if (translationLanguages.isEmpty()) {
_displayTranslationHint.value = translationRepo.getTranslationHint(providerKey).first()
_translationLangList.value = emptyList()
} else {
_displayTranslationHint.value = null
_translationLangList.value = translationLanguages
.map { LangItem(it.code, it.displayName, it.selected) }
val selectedOCRLang = ocrRepo.selectedOCRLangFlow.first()

if (translationLanguages.any {
it.code.firstPart() == selectedOCRLang.firstPart()
}) {
_displayTranslationHint.value = null
_translationLangList.value = translationLanguages
.map { LangItem(it.code, it.displayName, it.selected) }
} else {
_displayTranslationHint.value =
context.getString(R.string.msg_translator_provider_does_not_support_the_ocr_lang)
_translationLangList.value = emptyList()
}
}
}

Expand All @@ -66,6 +83,8 @@ class TranslationSelectPanelViewModel(viewScope: CoroutineScope) :
_ocrLanguageList.value = ocrLangList.map {
it.copy(selected = it.code == langCode)
}

loadTranslationLanguageList(translationRepo.selectedProviderTypeFlow.first().key)
}
}

Expand Down
11 changes: 11 additions & 0 deletions main/src/main/java/tw/firemaples/onscreenocr/log/FirebaseEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private const val EVENT_OCR_FINISHED = "ocr_finished"
private const val EVENT_START_TRANSLATION_TEXT = "start_translation_text"
private const val EVENT_TRANSLATION_TEXT_FINISHED = "translation_text_finished"
private const val EVENT_TRANSLATION_TEXT_FAILED = "translation_text_failed"
private const val EVENT_TRANSLATION_SOURCE_LANG_NOT_SUPPORT = "translation_source_lang_not_support"

private const val EVENT_SHOW_RESULT_WINDOW = "show_result_window"
private const val EVENT_SHOW_GOOGLE_TRANSLATE_WINDOW = "show_google_translate_window"
Expand Down Expand Up @@ -212,6 +213,16 @@ object FirebaseEvent {
})
}

fun logTranslationSourceLangNotSupport(translator: Translator, fromLang: String) {
PerformanceTracer.stopTracing(TRACE_TRANSLATE_TEXT, false)

val serviceName = translator.type.name
logEvent(EVENT_TRANSLATION_SOURCE_LANG_NOT_SUPPORT, Bundle().apply {
putString("translate_service", serviceName)
putString("translate_from", fromLang)
})
}

fun logTranslationTextFailed(translator: Translator) {
PerformanceTracer.stopTracing(TRACE_TRANSLATE_TEXT, false)

Expand Down
Loading

0 comments on commit cdc0b6a

Please sign in to comment.