Skip to content

Commit

Permalink
Merge pull request #70 from SanojPunchihewa/bug-fixes
Browse files Browse the repository at this point in the history
Change DownloadManager to okdownload manager
  • Loading branch information
SanojPunchihewa authored Oct 30, 2019
2 parents b1968a3 + f6f380b commit 6483130
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 103 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.github.hedzr:android-file-chooser:v1.2.0-final'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.liulishuo.okdownload:okdownload:1.0.5'
implementation 'com.liulishuo.okdownload:sqlite:1.0.5'
implementation 'com.liulishuo.okdownload:okhttp:1.0.5'
implementation 'com.squareup.okhttp3:okhttp:4.2.2'
}
89 changes: 37 additions & 52 deletions app/src/main/java/com/mobilegenomics/f5n/activity/DemoActivity.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package com.mobilegenomics.f5n.activity;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.liulishuo.okdownload.core.cause.EndCause;
import com.mobilegenomics.f5n.GUIConfiguration;
import com.mobilegenomics.f5n.R;
import com.mobilegenomics.f5n.core.PipelineStep;
import com.mobilegenomics.f5n.support.DownloadListener;
import com.mobilegenomics.f5n.support.TimeFormat;
import com.mobilegenomics.f5n.support.ZipManager;
import java.io.File;
Expand All @@ -37,7 +38,9 @@ public class DemoActivity extends AppCompatActivity {

private static final String fileName = "ecoli-data-set.zip";

private long downloadID;
private TextView statusTextView;

private ProgressBar progressBar;

private LinearLayout linearLayout;

Expand Down Expand Up @@ -94,6 +97,17 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
txtSkipDownload.setPadding(0, 10, 0, 0);
linearLayout.addView(txtSkipDownload);

statusTextView = new TextView(this);
linearLayout.addView(statusTextView);

progressBar = new ProgressBar(this,
null,
android.R.attr.progressBarStyleHorizontal);
progressBar.setProgress(0);
progressBar.setProgressTintList(ColorStateList
.valueOf(Color.BLACK));
linearLayout.addView(progressBar);

Button btnDownloadAndExtract = new Button(this);
btnDownloadAndExtract.setText("Download & Extract");
btnDownloadAndExtract.setOnClickListener(new OnClickListener() {
Expand Down Expand Up @@ -134,52 +148,23 @@ private void downloadDataSet(String url) {

writeToLogFile("Downloading data set...\n");

DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);

try {
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(fileName);
request.setDescription("Downloading");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
request.setDestinationUri(
Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/" + folderName + "/"
+ fileName));

downloadID = downloadmanager.enqueue(request);
} catch (Exception e) {
Toast.makeText(this, "Invalid URL", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Exception: " + e);
writeToLogFile("[Error] Downloading data set: " + e + "\n");
}
}

private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Fetching the download id received with the broadcast
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
//Checking if the received broadcast is for our enqueued download by matching download id
if (downloadID == id) {
Toast.makeText(DemoActivity.this, "Download Completed", Toast.LENGTH_SHORT).show();
writeToLogFile("Downloading data set completed\n");
extractZip(Environment.getExternalStorageDirectory() + "/" + folderName + "/"
+ fileName);
}
}
};

@Override
protected void onStart() {
super.onStart();
registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

@Override
protected void onPause() {
super.onPause();
unregisterReceiver(onDownloadComplete);
com.mobilegenomics.f5n.support.DownloadManager
downloadManager = new com.mobilegenomics.f5n.support.DownloadManager(url,
Environment.getExternalStorageDirectory() + "/" + folderName, statusTextView,
progressBar,
new DownloadListener() {
@Override
public void onComplete(@NonNull final EndCause cause, @Nullable final Exception realCause) {
if (cause == EndCause.COMPLETED) {
writeToLogFile("Downloading data set completed\n");
extractZip(Environment.getExternalStorageDirectory() + "/" + folderName + "/"
+ fileName);
} else {
writeToLogFile("Downloading data set error : " + realCause);
}
}
});
downloadManager.download();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package com.mobilegenomics.f5n.activity;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.liulishuo.okdownload.DownloadTask;
import com.liulishuo.okdownload.core.cause.EndCause;
import com.mobilegenomics.f5n.GUIConfiguration;
import com.mobilegenomics.f5n.R;
import com.mobilegenomics.f5n.core.AppMode;
import com.mobilegenomics.f5n.support.DownloadListener;
import com.mobilegenomics.f5n.support.DownloadManager;
import com.mobilegenomics.f5n.support.ZipManager;
import com.obsez.android.lib.filechooser.ChooserDialog;
import java.io.File;
Expand All @@ -35,14 +36,18 @@ public class DownloadActivity extends AppCompatActivity {

private static final String ecoliDataSetURL = "https://zanojmobiapps.com/_tmp/genome/ecoli/ecoli_2kb_region.zip";

private long downloadID;
private DownloadTask downloadTask;

LinearLayout linearLayout;

EditText urlInputPath;

EditText folderPathInput;

TextView statusTextView;

ProgressBar progressBar;

Button btnDownload;

Button btnDownloadEcoli;
Expand All @@ -61,8 +66,6 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_vertical);

registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

linearLayout = findViewById(R.id.vertical_linear_layout);

urlInputPath = new EditText(this);
Expand Down Expand Up @@ -90,6 +93,17 @@ public void onClick(final View v) {
});
linearLayout.addView(btnSetFolderPath);

statusTextView = new TextView(this);
linearLayout.addView(statusTextView);

progressBar = new ProgressBar(this,
null,
android.R.attr.progressBarStyleHorizontal);
progressBar.setProgress(0);
progressBar.setProgressTintList(ColorStateList
.valueOf(Color.BLACK));
linearLayout.addView(progressBar);

btnDownload = new Button(this);
btnDownload.setText("Download Data");
btnDownload.setEnabled(false);
Expand Down Expand Up @@ -197,42 +211,16 @@ private void downloadDataSet(String url) {

disableButtons();

DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);

String nameOfFile = URLUtil.guessFileName(url, null,
MimeTypeMap.getFileExtensionFromUrl(url));

try {
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(nameOfFile);
request.setDescription("Downloading");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
request.setDestinationUri(
Uri.parse("file://" + folderPath + "/" + nameOfFile));

downloadID = downloadmanager.enqueue(request);
} catch (Exception e) {
Toast.makeText(this, "Invalid URL", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Exception: " + e);
enableButtons();
}
DownloadManager downloadManager = new DownloadManager(url, folderPath, statusTextView, progressBar,
new DownloadListener() {
@Override
public void onComplete(@NonNull final EndCause cause, @Nullable final Exception realCause) {
enableButtons();
}
});
downloadManager.download();
}

private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Fetching the download id received with the broadcast
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
//Checking if the received broadcast is for our enqueued download by matching download id
if (downloadID == id) {
Toast.makeText(DownloadActivity.this, "Download Completed", Toast.LENGTH_SHORT).show();
enableButtons();
}
}
};

private void extractZip(String filepath) {
ZipManager zipManager = new ZipManager(DownloadActivity.this);
zipManager.unzip(filepath);
Expand All @@ -247,10 +235,4 @@ private void disableButtons() {
btnDownload.setEnabled(false);
btnDownloadEcoli.setEnabled(false);
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(onDownloadComplete);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mobilegenomics.f5n.support;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.liulishuo.okdownload.core.cause.EndCause;

public interface DownloadListener {

void onComplete(@NonNull final EndCause cause, @Nullable final Exception realCause);
}
Loading

0 comments on commit 6483130

Please sign in to comment.