Skip to content

Commit

Permalink
WIP adding stubs to make downlaod page
Browse files Browse the repository at this point in the history
  • Loading branch information
SubhamTyagi committed Oct 3, 2024
1 parent daaa91c commit a381ae5
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected void onCreate(Bundle savedInstanceState) {
mProgressMessage = findViewById(R.id.progress_message);
mDownloadLayout=findViewById(R.id.download_layout);

executorService = Executors.newFixedThreadPool(4);
executorService = Executors.newFixedThreadPool(1);
handler = new Handler(Looper.getMainLooper());

initDirectories();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@

package io.github.subhamtyagi.ocr.downloader;

//include all imports

public class LanguageActivity extends AppCompatActivity implements LanguageAdapter.OnLanguageItemClickListener {

private RecyclerView recyclerView;
private LanguageAdapter adapter;
private List<LanguageItem> languageList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_language);

// Initialize the RecyclerView and data
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

// Example data
languageList = new ArrayList<>();
languageList.add(new LanguageItem("English", false, false));
languageList.add(new LanguageItem("Spanish", true, false));
// Add more items...

adapter = new LanguageAdapter(languageList, this);
recyclerView.setAdapter(adapter);
}
@Override
public void onDownloadClicked(int position) {
// Start download and show progress
LanguageItem item = languageList.get(position);
item.setDownloading(true);
adapter.notifyItemChanged(position);

// download progress
//TODO: download codes goes here

runOnUiThread(() -> {
item.setDownloadProgress(finalProgress);
adapter.notifyItemChanged(position);
});
}

// After download is complete
runOnUiThread(() -> {
item.setDownloading(false);
item.setDownloaded(true);
adapter.notifyItemChanged(position);
});

}

@Override
public void onDeleteClicked(int position) {
// Handle delete logic
LanguageItem item = languageList.get(position);
item.setDownloaded(false);
adapter.notifyItemChanged(position);
}



@Override
public void onLanguageSelected(int position, boolean isSelected) {
// Handle selection logic here
LanguageItem item = languageList.get(position);
item.setSelected(isSelected);
}

private class DownloadTraining implements Runnable {
private final String dataType;
private final String lang;
private String size;

public DownloadTraining(String dataType, String lang) {
this.dataType = dataType;
this.lang = lang;
}

@Override
public void run() {
handler.post(() -> {
mProgressTitle.setText(getString(R.string.downloading));
mProgressMessage.setText(getString(R.string.downloading_language));
mDownloadLayout.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
});

boolean success = downloadTrainingData(dataType, lang);

handler.post(() -> {
mDownloadLayout.setVisibility(View.GONE);
if (success) {
initializeOCR(Utils.getTrainingDataLanguages(MainActivity.this));
} else {
Toast.makeText(MainActivity.this, "Download failed", Toast.LENGTH_SHORT).show();
}
});
}

@SuppressLint("DefaultLocale")
private boolean downloadTrainingData(String dataType, String lang) {
String downloadURL = getDownloadUrl(dataType, lang);
if (downloadURL == null) {
return false;
}
try {
URL url = new URL(downloadURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(false);
downloadURL = followRedirects(conn, downloadURL);
conn = (HttpURLConnection) new URL(downloadURL).openConnection();
conn.connect();
int totalContentSize = conn.getContentLength();
if (totalContentSize <= 0) {
return false;
}
size = Utils.getSize(totalContentSize);

// Switch from indeterminate to determinate progress bar
handler.post(() -> {
mProgressBar.setVisibility(View.VISIBLE);
mProgressMessage.setText("0"+ getString(R.string.percentage_downloaded)+ size);
mProgressBar.setProgress(0); // Reset progress bar to 0
});

try (InputStream input = new BufferedInputStream(conn.getInputStream());
OutputStream output = new FileOutputStream(new File(currentDirectory, String.format(Constants.LANGUAGE_CODE, lang)))) {

byte[] data = new byte[6 * 1024];
int downloaded = 0;
int count;

while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
downloaded += count;
int percentage = (downloaded * 100) / totalContentSize;
handler.post(() -> {
mProgressBar.setProgress(percentage);
mProgressMessage.setText(percentage+getString(R.string.percentage_downloaded)+size+".");
});
}
output.flush();
}

return true;
} catch (IOException e) {
Log.e(TAG, "Download failed: " + e.getLocalizedMessage());
return false;
}
}

private String getDownloadUrl(String dataType, String lang) {
switch (dataType) {
case "best":
return lang.equals("akk") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_AKK_BEST :
lang.equals("eqo") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_EQU :
String.format(Constants.TESSERACT_DATA_DOWNLOAD_URL_BEST, lang);
case "standard":
return lang.equals("akk") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_AKK_STANDARD :
lang.equals("eqo") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_EQU :
String.format(Constants.TESSERACT_DATA_DOWNLOAD_URL_STANDARD, lang);
default: // Assuming "fast" is the default
return lang.equals("akk") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_AKK_FAST :
lang.equals("eqo") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_EQU :
String.format(Constants.TESSERACT_DATA_DOWNLOAD_URL_FAST, lang);
}
}

private String followRedirects(HttpURLConnection conn, String downloadURL) throws IOException {
while (true) {
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
String location = conn.getHeaderField("Location");
URL base = new URL(downloadURL);
downloadURL = new URL(base, location).toExternalForm(); // Handle relative URLs
conn = (HttpURLConnection) new URL(downloadURL).openConnection(); // Re-open connection
} else {
break; // No more redirects
}
}
return downloadURL;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.github.subhamtyagi.ocr.downloader;


public class LanguageAdapter extends RecyclerView.Adapter<LanguageAdapter.LanguageViewHolder> {

private List<LanguageItem> languageList;
private OnLanguageItemClickListener listener;

public interface OnLanguageItemClickListener {
void onDownloadClicked(int position);
void onDeleteClicked(int position);
void onLanguageSelected(int position, boolean isSelected);
}

public LanguageAdapter(List<LanguageItem> languageList, OnLanguageItemClickListener listener) {
this.languageList = languageList;
this.listener = listener;
}

@NonNull
@Override
public LanguageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_language, parent, false);
return new LanguageViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull LanguageViewHolder holder, int position) {
LanguageItem currentItem = languageList.get(position);

holder.languageNameTextView.setText(currentItem.getLanguageName());
holder.selectLanguageCheckBox.setChecked(currentItem.isSelected());

// Toggle visibility and color of icons based on download status
if (currentItem.isDownloaded()) {
holder.downloadImageView.setImageResource(R.drawable.ic_download_green); // Change icon if downloaded
holder.deleteImageView.setVisibility(View.VISIBLE); // Show delete icon
} else {
holder.downloadImageView.setImageResource(R.drawable.ic_download); // Regular download icon
holder.deleteImageView.setVisibility(View.GONE); // Hide delete icon
}

// Manage the ProgressBar visibility and progress
if (currentItem.isDownloading()) {
holder.progressBar.setVisibility(View.VISIBLE);
holder.progressBar.setProgress(currentItem.getDownloadProgress());
} else {
holder.progressBar.setVisibility(View.GONE);
}

// Handle click events for download and delete
holder.downloadImageView.setOnClickListener(v -> listener.onDownloadClicked(position));
holder.deleteImageView.setOnClickListener(v -> listener.onDeleteClicked(position));

// Handle checkbox selection
holder.selectLanguageCheckBox.setOnCheckedChangeListener((buttonView, isChecked) ->
listener.onLanguageSelected(position, isChecked)
);
}

public static class LanguageViewHolder extends RecyclerView.ViewHolder {
public TextView languageNameTextView;
public ImageView downloadImageView;
public ImageView deleteImageView;
public CheckBox selectLanguageCheckBox;
public ProgressBar progressBar; // New ProgressBar view

public LanguageViewHolder(@NonNull View itemView) {
super(itemView);
languageNameTextView = itemView.findViewById(R.id.tv_language_name);
downloadImageView = itemView.findViewById(R.id.iv_download_language);
deleteImageView = itemView.findViewById(R.id.iv_delete_language);
selectLanguageCheckBox = itemView.findViewById(R.id.cb_select_language);
progressBar = itemView.findViewById(R.id.pb_download_progress); // Initialize ProgressBar
}
}


@Override
public int getItemCount() {
return languageList.size();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.github.subhamtyagi.ocr.downloader;

public class LanguageItem {
private String languageName;
private boolean isDownloaded;
private boolean isSelected;
private boolean isDownloading; // track downloading status
private int downloadProgress; // track download progress

public LanguageItem(String languageName, boolean isDownloaded, boolean isSelected) {
this.languageName = languageName;
this.isDownloaded = isDownloaded;
this.isSelected = isSelected;
this.isDownloading = false;
this.downloadProgress = 0;
}

public String getLanguageName() {
return languageName;
}

public boolean isDownloaded() {
return isDownloaded;
}

public void setDownloaded(boolean downloaded) {
isDownloaded = downloaded;
}

public boolean isSelected() {
return isSelected;
}

public void setSelected(boolean selected) {
isSelected = selected;
}

public boolean isDownloading() {
return isDownloading;
}

public void setDownloading(boolean downloading) {
isDownloading = downloading;
}

public int getDownloadProgress() {
return downloadProgress;
}

public void setDownloadProgress(int downloadProgress) {
this.downloadProgress = downloadProgress;
}
}

12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_language.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Loading

0 comments on commit a381ae5

Please sign in to comment.