1
0
Fork 0

reverted the lazy loading hack for initializing Voice Input because it causes ANRs on multiple devices

This commit is contained in:
sspanak 2025-06-13 13:04:52 +03:00 committed by Dimo Karaivanov
parent 6edf4e18ab
commit 80640daa05
2 changed files with 2 additions and 28 deletions

View file

@ -15,7 +15,7 @@ import java.util.List;
import java.util.concurrent.Executors;
import io.github.sspanak.tt9.languages.Language;
import io.github.sspanak.tt9.util.sys.DeviceInfo;
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
class SpeechRecognizerSupportModern extends SpeechRecognizerSupportLegacy implements RecognitionSupportCallback {
@ -45,7 +45,6 @@ class SpeechRecognizerSupportModern extends SpeechRecognizerSupportLegacy implem
void checkOfflineSupport(@NonNull Runnable onSupportChecked) {
if (
locale == null
|| !DeviceInfo.AT_LEAST_ANDROID_13
|| !isOnDeviceRecognitionAvailable
|| missingOfflineLanguages.contains(locale)
|| availableOfflineLanguages.contains(locale)

View file

@ -10,7 +10,6 @@ import android.speech.SpeechRecognizer;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import io.github.sspanak.tt9.R;
@ -39,7 +38,7 @@ public class VoiceInputOps {
ConsumerCompat<VoiceInputError> onError
) {
listener = new VoiceListener(ims, onStart, this::onStop, this::onError);
recognizerSupport = lazyLoadSupportClass(ims);
recognizerSupport = DeviceInfo.AT_LEAST_ANDROID_13 ? new SpeechRecognizerSupportModern(ims) : new SpeechRecognizerSupportLegacy(ims);
onStopListening = onStop != null ? onStop : result -> {};
onListeningError = onError != null ? onError : error -> {};
@ -48,30 +47,6 @@ public class VoiceInputOps {
}
/**
* Android normally loads all classes even if they are not used on specific version. This here
* prevents the following error on older devices like Sonim XP3800:
* java.lang.NoClassDefFoundError: Failed resolution of: Landroid/speech/RecognitionSupportCallback;
* Caused by: java.lang.ClassNotFoundException: Didn't find class "android.speech.RecognitionSupportCallback"
*
*/
private SpeechRecognizerSupportLegacy lazyLoadSupportClass(Context context) {
if (DeviceInfo.AT_LEAST_ANDROID_13) {
try {
Package voicePackage = SpeechRecognizerSupportLegacy.class.getPackage();
String className = voicePackage != null ? voicePackage.getName() + ".SpeechRecognizerSupportModern" : "";
Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getDeclaredConstructor(Context.class);
return (SpeechRecognizerSupportLegacy) ctor.newInstance(context);
} catch (Exception e) {
return new SpeechRecognizerSupportLegacy(context);
}
}
return new SpeechRecognizerSupportLegacy(context);
}
static String getLocale(@NonNull Language lang) {
return lang.getLocale().toString().replace("_", "-");
}