From ca0556000c4603d69cc6f553863e9cdd8dca940c Mon Sep 17 00:00:00 2001 From: Corinna Kindlhofer Date: Thu, 12 Sep 2024 10:05:27 +0200 Subject: [PATCH] IDE-183 Fix testComputeDialogValue --- .../catrobat/catroid/utils/TextBlockUtil.kt | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/catroid/src/main/java/org/catrobat/catroid/utils/TextBlockUtil.kt b/catroid/src/main/java/org/catrobat/catroid/utils/TextBlockUtil.kt index ff55c5617db..cf71a55f787 100644 --- a/catroid/src/main/java/org/catrobat/catroid/utils/TextBlockUtil.kt +++ b/catroid/src/main/java/org/catrobat/catroid/utils/TextBlockUtil.kt @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team + * Copyright (C) 2010-2024 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -25,15 +25,18 @@ package org.catrobat.catroid.utils import android.graphics.Point import android.graphics.Rect import com.google.mlkit.nl.languageid.LanguageIdentification +import com.google.mlkit.nl.languageid.LanguageIdentifier import com.google.mlkit.vision.text.Text import com.huawei.hms.mlsdk.langdetect.MLLangDetectorFactory import com.huawei.hms.mlsdk.langdetect.local.MLLocalLangDetector import com.huawei.hms.mlsdk.langdetect.local.MLLocalLangDetectorSetting import com.huawei.hms.mlsdk.text.MLText +import org.catrobat.catroid.CatroidApplication import org.catrobat.catroid.ProjectManager import org.catrobat.catroid.camera.VisualDetectionHandler.coordinatesFromRelativePosition import org.catrobat.catroid.common.ScreenValues.currentScreenResolution import org.catrobat.catroid.stage.StageActivity +import org.koin.java.KoinJavaComponent.get import kotlin.math.roundToInt object TextBlockUtil { @@ -44,12 +47,25 @@ object TextBlockUtil { private var imageWidth = 0 private var imageHeight = 0 private const val MAX_TEXT_SIZE = 100 - private var languageIdentifierGoogle = LanguageIdentification.getClient() - private var languageDetectorFactoryHuawei: MLLangDetectorFactory = MLLangDetectorFactory.getInstance() - var languageDetectorSettingHuawei: MLLocalLangDetectorSetting = MLLocalLangDetectorSetting.Factory() - .setTrustedThreshold(TRUSTED_THRESHOLD) - .create() - var languageIdentifierHuawei: MLLocalLangDetector = languageDetectorFactoryHuawei.getLocalLangDetector(languageDetectorSettingHuawei) + private var languageIdentifierGoogle: LanguageIdentifier? = null + private var languageIdentifierHuawei: MLLocalLangDetector? = null + + init { + val context = CatroidApplication.getAppContext() + val mobileServiceAvailability = get(MobileServiceAvailability::class.java) + if (mobileServiceAvailability.isGmsAvailable(context)) { + languageIdentifierGoogle = LanguageIdentification.getClient() + } else if (mobileServiceAvailability.isHmsAvailable(context)) { + val languageDetectorFactoryHuawei: MLLangDetectorFactory = + MLLangDetectorFactory.getInstance() + val languageDetectorSettingHuawei: MLLocalLangDetectorSetting = + MLLocalLangDetectorSetting.Factory() + .setTrustedThreshold(TRUSTED_THRESHOLD) + .create() + languageIdentifierHuawei = + languageDetectorFactoryHuawei.getLocalLangDetector(languageDetectorSettingHuawei) + } + } fun setTextBlocksGoogle(text: List, width: Int, height: Int) { imageWidth = width @@ -57,13 +73,13 @@ object TextBlockUtil { textBlockLanguages.clear() textBlockBoundingBoxes.clear() - text.forEachIndexed { index, textBlock -> textBlock.text.let { textBlocks.add(index, it) } textBlock.boundingBox?.let { textBlockBoundingBoxes.add(index, it) } - languageIdentifierGoogle.identifyLanguage(textBlock.text).addOnSuccessListener { languageCode -> - textBlockLanguages[index] = languageCode - } + languageIdentifierGoogle?.identifyLanguage(textBlock.text) + ?.addOnSuccessListener { languageCode -> + textBlockLanguages[index] = languageCode + } } } @@ -77,8 +93,9 @@ object TextBlockUtil { text.forEachIndexed { index, textBlock -> textBlock.stringValue?.let { textBlocks.add(index, it) } textBlock.border?.let { textBlockBoundingBoxes.add(index, it) } - val firstBestDetectTask = languageIdentifierHuawei.firstBestDetect(textBlock.stringValue) - firstBestDetectTask.addOnSuccessListener { languageCode -> + val firstBestDetectTask = + languageIdentifierHuawei?.firstBestDetect(textBlock.stringValue) + firstBestDetectTask?.addOnSuccessListener { languageCode -> textBlockLanguages[index] = languageCode } }