Refactoring

pull/182/head
M66B 5 years ago
parent c338aeeae7
commit cb745e5d69

@ -15,4 +15,14 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
.setTitle(R.string.title_translate) .setTitle(R.string.title_translate)
.create(); .create();
} }
static void Translate(String text, String targetLanguage, ITranslate intf) {
// Stub
}
interface ITranslate {
void onTranslated(String text);
void onError(Throwable ex);
}
} }

@ -44,66 +44,18 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
String targetLanguage = map.get(items[which]); String targetLanguage = map.get(items[which]);
LanguageIdentifier languageIdentifier = LanguageIdentification.getClient( Translate(text, targetLanguage, new ITranslate() {
new LanguageIdentificationOptions.Builder() @Override
.setConfidenceThreshold(0.34f) public void onTranslated(String text) {
.build()); getArguments().putString("translated", text);
languageIdentifier.identifyLanguage(text) sendResult(RESULT_OK);
.addOnSuccessListener( }
new OnSuccessListener<String>() {
@Override @Override
public void onSuccess(@Nullable String sourceLanguage) { public void onError(Throwable ex) {
Log.i("Translate source=" + sourceLanguage); Log.unexpectedError(getParentFragmentManager(), ex);
if (sourceLanguage.equals("und")) }
sourceLanguage = TranslateLanguage.ENGLISH; });
TranslatorOptions options = new TranslatorOptions.Builder()
.setSourceLanguage(sourceLanguage)
.setTargetLanguage(targetLanguage)
.build();
Translator translator = Translation.getClient(options);
DownloadConditions conditions = new DownloadConditions.Builder()
.requireWifi()
.build();
translator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(
new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void v) {
translator.translate(text)
.addOnSuccessListener(
new OnSuccessListener<String>() {
@Override
public void onSuccess(@NonNull String translatedText) {
getArguments().putString("translated", translatedText);
sendResult(RESULT_OK);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
});
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
});
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
});
} }
}) })
.create(); .create();
@ -112,4 +64,77 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
@Override @Override
public void onDismiss(@NonNull DialogInterface dialog) { public void onDismiss(@NonNull DialogInterface dialog) {
} }
static void Translate(String text, String targetLanguage, ITranslate intf) {
LanguageIdentifier languageIdentifier = LanguageIdentification.getClient(
new LanguageIdentificationOptions.Builder()
.setConfidenceThreshold(0.34f)
.build());
languageIdentifier.identifyLanguage(text)
.addOnSuccessListener(
new OnSuccessListener<String>() {
@Override
public void onSuccess(@Nullable String sourceLanguage) {
Log.i("Translate source=" + sourceLanguage);
if (sourceLanguage.equals("und"))
sourceLanguage = TranslateLanguage.ENGLISH;
if (sourceLanguage.equals(targetLanguage)) {
intf.onTranslated(text);
return;
}
TranslatorOptions options = new TranslatorOptions.Builder()
.setSourceLanguage(sourceLanguage)
.setTargetLanguage(targetLanguage)
.build();
Translator translator = Translation.getClient(options);
DownloadConditions conditions = new DownloadConditions.Builder()
.requireWifi()
.build();
translator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(
new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void v) {
translator.translate(text)
.addOnSuccessListener(
new OnSuccessListener<String>() {
@Override
public void onSuccess(@NonNull String translatedText) {
intf.onTranslated(translatedText);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception ex) {
intf.onError(ex);
}
});
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception ex) {
intf.onError(ex);
}
});
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception ex) {
intf.onError(ex);
}
});
}
interface ITranslate {
void onTranslated(String text);
void onError(Throwable ex);
}
} }

Loading…
Cancel
Save