Skip to content

Commit

Permalink
Merge pull request #118 from SanojPunchihewa/minIt-ftp
Browse files Browse the repository at this point in the history
Use FTP for download and upload in minIT mode
  • Loading branch information
SanojPunchihewa authored Jan 24, 2020
2 parents e9e2d39 + efb19a4 commit eb3b409
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 128 deletions.
6 changes: 6 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,5 @@ dependencies {
implementation 'com.liulishuo.okdownload:sqlite:1.0.5'
implementation 'com.liulishuo.okdownload:okhttp:1.0.5'
implementation 'com.squareup.okhttp3:okhttp:4.2.2'
implementation 'commons-net:commons-net:3.6'
}
178 changes: 139 additions & 39 deletions app/src/main/java/com/mobilegenomics/f5n/activity/DownloadActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
Expand All @@ -19,47 +21,135 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.liulishuo.okdownload.DownloadTask;
import com.liulishuo.okdownload.core.Util;
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.PipelineState;
import com.mobilegenomics.f5n.support.PreferenceUtil;
import com.mobilegenomics.f5n.support.ZipListener;
import com.mobilegenomics.f5n.support.ZipManager;
import com.obsez.android.lib.filechooser.ChooserDialog;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.io.CopyStreamAdapter;

public class DownloadActivity extends AppCompatActivity {

private static final String TAG = DownloadActivity.class.getSimpleName();
class FTPDownloadTask extends AsyncTask<String, Long, Boolean> {

private String folderPath;
long fileSize;

private static final String ecoliDataSetURL = "https://zanojmobiapps.com/_tmp/genome/ecoli/ecoli-data-set.zip";
boolean status;

LinearLayout linearLayout;
@Override
protected Boolean doInBackground(String... urls) {
FTPClient con;
try {
con = new FTPClient();
con.setDefaultPort(8000);
con.connect(urls[0]);

EditText urlInputPath;
con.setCopyStreamListener(new CopyStreamAdapter() {
@Override
public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) {
publishProgress(totalBytesTransferred);
}

EditText folderPathInput;
});

TextView statusTextView;
if (con.login("test", "test")) {
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
con.setBufferSize(1024000);
FTPFile[] ff = con.listFiles(urls[1]);

ProgressBar progressBar;
if (ff != null) {
fileSize = (ff[0].getSize());
}

OutputStream out = new FileOutputStream(new File(folderPath + "/" + urls[1]));
status = con.retrieveFile(urls[1], out);
out.close();
con.logout();
con.disconnect();
}
} catch (Exception e) {
Log.e(TAG, "Error: " + e);
status = false;
}
return status;
}

@Override
protected void onPostExecute(final Boolean downloadSuccess) {
super.onPostExecute(downloadSuccess);
Log.i(TAG, "Download Finished");
if (downloadSuccess) {
statusTextView.setText("Download Completed");
} else {
statusTextView.setText("Download Error");
}
enableButtons();
}

@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i(TAG, "Download Started");
statusTextView.setText("Download Started");
progressBar.setMax(100);
disableButtons();
}

@Override
protected void onProgressUpdate(final Long... values) {
super.onProgressUpdate(values);
String total = Util.humanReadableBytes(fileSize, true);
String downloaded = Util.humanReadableBytes(values[0], true);
statusTextView.setText("Downloading: " + downloaded + "/" + total);
float percent = (float) values[0] / fileSize;
progressBar.setProgress((int) percent * progressBar.getMax());
}
}

private static final String TAG = DownloadActivity.class.getSimpleName();

private static String folderPath;

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

Button btnDownload;

Button btnDownloadEcoli;

EditText filePathInput;
Button btnExtract;

Button btnRunPipeline;

Button btnSelectFilePath;

Button btnExtract;
EditText filePathInput;

Button btnRunPipeline;
EditText folderPathInput;

LinearLayout linearLayout;

ProgressBar progressBar;

TextView statusTextView;

EditText urlInputPath;

private DownloadTask downloadTask;

@SuppressLint("ClickableViewAccessibility")
@Override
Expand All @@ -75,8 +165,9 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {

if (getIntent().getExtras() != null) {
String path = getIntent().getExtras().getString("DATA_SET_URL");
String fileName = getIntent().getExtras().getString("FILE_NAME");
if (path != null && !TextUtils.isEmpty(path)) {
urlInputPath.setText(path);
urlInputPath.setText(path + "/" + fileName + ".zip");
}
}

Expand Down Expand Up @@ -114,7 +205,8 @@ public void onClick(final View v) {
@Override
public void onClick(final View v) {
if (urlInputPath.getText() != null && !TextUtils.isEmpty(urlInputPath.getText().toString().trim())) {
downloadDataSet(urlInputPath.getText().toString().trim());
//downloadDataSet(urlInputPath.getText().toString().trim());
downloadDatasetFTP(urlInputPath.getText().toString().trim());
} else {
Toast.makeText(DownloadActivity.this, "Please input a URL", Toast.LENGTH_SHORT).show();
}
Expand Down Expand Up @@ -187,6 +279,7 @@ public void onClick(final View v) {
btnRunPipeline.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
GUIConfiguration.setPipelineState(PipelineState.TO_BE_CONFIGURED);
Intent intent = new Intent(DownloadActivity.this, TerminalActivity.class);
intent.putExtra("FOLDER_PATH", folderPath.substring(0, folderPath.lastIndexOf(".")));
startActivity(intent);
Expand All @@ -196,26 +289,14 @@ public void onClick(final View v) {

}

private void openFileManager(boolean dirOnly) {
private void enableButtons() {
btnDownload.setEnabled(true);
btnDownloadEcoli.setEnabled(true);
}

new ChooserDialog(DownloadActivity.this)
.withFilter(dirOnly, false)
// to handle the result(s)
.withChosenListener(new ChooserDialog.Result() {
@Override
public void onChoosePath(String path, File pathFile) {
folderPath = path;
if (dirOnly) {
folderPathInput.setText(folderPath);
enableButtons();
} else {
filePathInput.setText(folderPath);
btnExtract.setEnabled(true);
}
}
})
.build()
.show();
private void disableButtons() {
btnDownload.setEnabled(false);
btnDownloadEcoli.setEnabled(false);
}

private void downloadDataSet(String url) {
Expand All @@ -234,6 +315,12 @@ public void onComplete(@NonNull final EndCause cause, @Nullable final Exception
downloadManager.download(DownloadActivity.this, treeUri);
}

private void downloadDatasetFTP(String url) {
String[] urlData = url.split("/");
Log.e(TAG, "URL=" + urlData[1]);
new FTPDownloadTask().execute(urlData[0], urlData[1]);
}

private void extractZip(String filepath) {

ZipManager zipManager = new ZipManager(DownloadActivity.this, new ZipListener() {
Expand Down Expand Up @@ -284,13 +371,26 @@ public void run() {
zipManager.unzip(treeUri, filepath);
}

private void enableButtons() {
btnDownload.setEnabled(true);
btnDownloadEcoli.setEnabled(true);
}
private void openFileManager(boolean dirOnly) {

private void disableButtons() {
btnDownload.setEnabled(false);
btnDownloadEcoli.setEnabled(false);
new ChooserDialog(DownloadActivity.this)
.withFilter(dirOnly, false)
// to handle the result(s)
.withChosenListener(new ChooserDialog.Result() {
@Override
public void onChoosePath(String path, File pathFile) {
folderPath = path;
if (dirOnly) {
folderPathInput.setText(folderPath);
enableButtons();
} else {
filePathInput.setText(folderPath);
btnExtract.setEnabled(true);
}
}
})
.build()
.show();
}

}
Loading

0 comments on commit eb3b409

Please sign in to comment.