Skip to content

Commit

Permalink
fix download issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SubhamTyagi committed Jan 31, 2025
1 parent 2e47cdc commit 73b845e
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions app/src/main/java/io/github/subhamtyagi/ocr/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected void onResume() {
mLanguageName.setText(Utils.getTrainingDataLanguages(this).stream().map(Language::getName).collect(Collectors.joining(", ")));
}

/* public void startOCRFromShareMenu(Uri imageUri, Set<Language> languages) {
/* public void startOCRFromShareMenu(Uri imageUri, Set<Language> languages) {
initializeOCR(languages);
Utils.setLastUsedLanguage(this, languages);
// Log.d("radio", "showLanguageSelectionDialog: " + mLanguage);
Expand Down Expand Up @@ -236,8 +236,6 @@ private void initDirectories() {
}




/**
* initialize the OCR i.e tesseract api
* if there is no training data in directory than it will ask for download
Expand Down Expand Up @@ -296,27 +294,23 @@ private void handleReaderException(Set<Language> languages) {
private void downloadLanguageData() {
Set<Language> missingLanguage = new HashSet<>();
Set<Language> languages =Utils.getTrainingDataLanguages(this);
if (!isNoLanguagesDataMissingFromSet()) {
if (!Utils.isNetworkAvailable(getApplication())) {
Toast.makeText(this, getString(R.string.you_are_not_connected_to_internet), Toast.LENGTH_SHORT).show();
return;
if (!Utils.isNetworkAvailable(getApplication())) {
Toast.makeText(this, getString(R.string.you_are_not_connected_to_internet), Toast.LENGTH_SHORT).show();
return;
}
for (Language l : languages) {
if (isLanguageDataMissing(mTrainingDataType, l)) {
missingLanguage.add(l);

}
for (Language l : languages) {
if (isLanguageDataMissing(mTrainingDataType, l)) {
missingLanguage.add(l);
}
String missingLangName = missingLanguage.stream().map(Language::getName).collect(Collectors.joining(", "));
String msg = String.format(getString(R.string.download_description), missingLangName);
dialog = new AlertDialog.Builder(this).setTitle(R.string.training_data_missing).setCancelable(false).setMessage(msg).setPositiveButton(R.string.yes, (dialog, which) -> {
dialog.cancel();
for (Language language : missingLanguage) {
executorService.submit(new DownloadTraining(mTrainingDataType, language.getCode()));
}
}).setNegativeButton(R.string.no, (dialog, which) -> dialog.cancel()).create();
dialog.show();
}
String missingLangName = missingLanguage.stream().map(Language::getName).collect(Collectors.joining(", "));
String msg = String.format(getString(R.string.download_description), missingLangName);
dialog = new AlertDialog.Builder(this).setTitle(R.string.training_data_missing).setCancelable(false).setMessage(msg).setPositiveButton(R.string.yes, (dialog, which) -> {
dialog.cancel();
executorService.submit(new DownloadTraining(mTrainingDataType, missingLanguage));
}).setNegativeButton(R.string.no, (dialog, which) -> dialog.cancel()).create();
dialog.show();

}

private boolean isNoLanguagesDataMissingFromSet() {
Expand Down Expand Up @@ -503,12 +497,12 @@ private void updateImageView() {

private class DownloadTraining implements Runnable {
private final String dataType;
private final String lang;
private final Set<Language> languages;
private String size;

public DownloadTraining(String dataType, String lang) {
public DownloadTraining(String dataType, Set<Language> langs) {
this.dataType = dataType;
this.lang = lang;
this.languages = langs;
}

@Override
Expand All @@ -519,8 +513,10 @@ public void run() {
mProgressBar.setVisibility(View.GONE);
});

boolean success = downloadTrainingData(dataType, lang);

boolean success=true;
for (Language lang : languages) {
success = success && downloadTrainingData(dataType, lang.getCode());
}
handler.post(() -> {
mDownloadLayout.setVisibility(View.GONE);
if (success) {
Expand Down

0 comments on commit 73b845e

Please sign in to comment.