Skip to content

Commit

Permalink
Merge pull request #122 from SanojPunchihewa/bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
SanojPunchihewa authored Feb 6, 2020
2 parents eb3b409 + 698b161 commit 4286231
Show file tree
Hide file tree
Showing 13 changed files with 418 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ public static ArrayList<Step> getSteps() {
return steps;
}

public static void setSteps(ArrayList<Step> newSteps) {
steps = newSteps;
}

public static void configureLikedFileArgument(String fileName, String value) {
linkedFileArguments.put(fileName, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.mobilegenomics.f5n.core.PipelineComponent;
import com.mobilegenomics.f5n.support.FileUtil;
import com.mobilegenomics.f5n.support.PipelineState;
import com.mobilegenomics.f5n.support.PreferenceUtil;
import com.mobilegenomics.f5n.support.TimeFormat;
import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -276,9 +277,17 @@ protected void onPostExecute(final String s) {
btnWriteLog.setVisibility(View.VISIBLE);
btnProceed.setEnabled(true);
mProgressBar.setVisibility(View.GONE);
PreferenceUtil
.setSharedPreferenceInt(R.string.id_app_mode, GUIConfiguration.getPipelineState().ordinal());

if (GUIConfiguration.getAppMode() == AppMode.SLAVE) {
mp.start();
mp.setLooping(true);
GUIConfiguration.setPipelineState(PipelineState.TO_BE_UPLOAD);
PreferenceUtil
.setSharedPreferenceInt(R.string.id_app_mode, GUIConfiguration.getPipelineState().ordinal());
PreferenceUtil
.setSharedPreferenceString(R.string.id_results_summary, resultsSummary);
btnSendResults.setVisibility(View.VISIBLE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mobilegenomics.f5n.core.PipelineStep;
import com.mobilegenomics.f5n.support.DownloadListener;
import com.mobilegenomics.f5n.support.FileUtil;
import com.mobilegenomics.f5n.support.PipelineState;
import com.mobilegenomics.f5n.support.PreferenceUtil;
import com.mobilegenomics.f5n.support.TimeFormat;
import com.mobilegenomics.f5n.support.ZipListener;
Expand Down Expand Up @@ -137,6 +138,7 @@ public void onClick(final View v) {
GUIConfiguration.addPipelineStep(step);
}
GUIConfiguration.printList();
GUIConfiguration.setPipelineState(PipelineState.CONFIGURED);
GUIConfiguration.configureSteps(DemoActivity.this,
Environment.getExternalStorageDirectory() + "/" + folderName + "/"
+ fileName.substring(0, fileName.lastIndexOf(".")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public void onComplete(@NonNull final EndCause cause, @Nullable final Exception

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

Expand Down
197 changes: 177 additions & 20 deletions app/src/main/java/com/mobilegenomics/f5n/activity/MinITActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,42 @@
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.liulishuo.okdownload.core.Util;
import com.mobilegenomics.f5n.BuildConfig;
import com.mobilegenomics.f5n.GUIConfiguration;
import com.mobilegenomics.f5n.R;
import com.mobilegenomics.f5n.dto.State;
import com.mobilegenomics.f5n.dto.WrapperObject;
import com.mobilegenomics.f5n.support.PipelineState;
import com.mobilegenomics.f5n.support.PreferenceUtil;
import com.mobilegenomics.f5n.support.ServerCallback;
import com.mobilegenomics.f5n.support.ServerConnectionUtils;
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.FileInputStream;
import java.util.ArrayList;
import java.util.Objects;
import net.gotev.uploadservice.UploadService;
import org.apache.commons.net.ftp.FTP;
Expand All @@ -48,6 +57,10 @@ class FTPUploadTask extends AsyncTask<String, Long, Boolean> {
protected Boolean doInBackground(String... urls) {
FTPClient con;
try {

Log.e(TAG, "Address = " + urls[0]);
Log.e(TAG, "File path = " + urls[1]);

con = new FTPClient();
con.setDefaultPort(8000);
con.connect(urls[0]);
Expand All @@ -70,7 +83,8 @@ public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, l
fileSize = fileIn.length();

FileInputStream in = new FileInputStream(fileIn);
boolean result = con.storeFile(new File(urls[1]).getName(), in);
String filePath = "outputs/" + new File(urls[1]).getName();
boolean result = con.storeFile(filePath, in);
in.close();
status = result;
con.logout();
Expand All @@ -94,6 +108,9 @@ protected void onPostExecute(final Boolean uploadSuccess) {
super.onPostExecute(uploadSuccess);
Log.i(TAG, "Upload Finished");
if (uploadSuccess) {
GUIConfiguration.setPipelineState(PipelineState.COMPLETED);
PreferenceUtil
.setSharedPreferenceInt(R.string.id_app_mode, GUIConfiguration.getPipelineState().ordinal());
statusTextView.setText("Upload Completed");
sendJobResults();
} else {
Expand Down Expand Up @@ -126,11 +143,17 @@ protected void onProgressUpdate(final Long... values) {

TextView statusTextView;

private Button btnSendResult;
private TableRow trSendResults;

private String folderPath;
private Button btnProcessJob;

private Button btnRequestJob;

private boolean ranPipeline = false;
private Button btnCompressFiles;

private Button btnSendResults;

private String folderPath;

private String resultsSummary;

Expand All @@ -140,6 +163,8 @@ protected void onProgressUpdate(final Long... values) {

private String zipFileName;

private ArrayList<String> fileList;

public static void logHandler(Handler handler) {
handler.post(new Runnable() {
@Override
Expand All @@ -157,6 +182,12 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_minit);

if (PreferenceUtil.getSharedPreferenceInt(R.string.id_app_mode) == PipelineState.MINIT_RUNNING.ordinal() &&
PreferenceUtil.getSharedPreferenceStepList(R.string.id_step_list) != null &&
PreferenceUtil.getSharedPreferenceObject(R.string.id_wrapper_obj) != null) {
showResumeMessage(PipelineState.MINIT_RUNNING);
}

statusTextView = findViewById(R.id.txt_status);
progressBar = findViewById(R.id.progress_upload_status);

Expand All @@ -165,19 +196,28 @@ protected void onCreate(Bundle savedInstanceState) {
serverAddressInput = findViewById(R.id.input_server_address);

connectionLogText = findViewById(R.id.text_conn_log);
final Button btnRquestJob = findViewById(R.id.btn_request_job);
btnSendResult = findViewById(R.id.btn_send_result);

trSendResults = findViewById(R.id.tr_select_files_send);

btnRequestJob = findViewById(R.id.btn_request_job);
btnProcessJob = findViewById(R.id.btn_process_job);
btnCompressFiles = findViewById(R.id.btn_select_files);
btnSendResults = findViewById(R.id.btn_send_result);

if (getIntent().getExtras() != null) {
resultsSummary = getIntent().getExtras().getString("PIPELINE_STATUS");
folderPath = getIntent().getExtras().getString("FOLDER_PATH");
if (resultsSummary != null && !TextUtils.isEmpty(resultsSummary)) {
ranPipeline = true;
btnRquestJob.setText("Send Results");
btnRequestJob.setVisibility(View.GONE);
trSendResults.setVisibility(View.VISIBLE);
}
} else {
if (PreferenceUtil.getSharedPreferenceInt(R.string.id_app_mode) == PipelineState.TO_BE_UPLOAD.ordinal()) {
showResumeMessage(PipelineState.TO_BE_UPLOAD);
}
}

btnRquestJob.setOnClickListener(new View.OnClickListener() {
btnRequestJob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Expand All @@ -186,21 +226,41 @@ public void onClick(View v) {
serverIP = serverAddressInput.getText().toString().trim();

ServerConnectionUtils.setServerAddress(serverIP);
if (ranPipeline) {
uploadDataSet();
} else {
requestJob();
}
requestJob();
} else {
Toast.makeText(MinITActivity.this, "Please input a server IP", Toast.LENGTH_SHORT).show();
}
}
});

btnCompressFiles.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
openFileManager(false, true);
}
});

btnSendResults.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
if (serverAddressInput.getText() != null && !TextUtils
.isEmpty(serverAddressInput.getText().toString().trim())) {
serverIP = serverAddressInput.getText().toString().trim();
ServerConnectionUtils.setServerAddress(serverIP);
openFileManager(false, false);
} else {
Toast.makeText(MinITActivity.this, "Please input a server IP", Toast.LENGTH_SHORT).show();
}
}
});

btnSendResult.setOnClickListener(new View.OnClickListener() {
btnProcessJob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

PreferenceUtil
.setSharedPreferenceObject(R.string.id_wrapper_obj, ServerConnectionUtils.getWrapperObject());

Intent intent = new Intent(MinITActivity.this, DownloadActivity.class);
// TODO Fix the following
// Protocol, file server IP and Port
Expand Down Expand Up @@ -248,7 +308,7 @@ public void onSuccess(final WrapperObject job) {
public void run() {
GUIConfiguration.configureSteps(job.getSteps());
zipFileName = job.getPrefix();
btnSendResult.setVisibility(View.VISIBLE);
btnProcessJob.setVisibility(View.VISIBLE);
}
});
}
Expand All @@ -275,8 +335,16 @@ public void run() {
});
}

private void uploadDataSet() {
private void uploadDataSet(String filePath) {
new FTPUploadTask().execute(serverIP, filePath);
}

private void compressDataSet() {
// TODO check wifi connectivity
folderPath = fileList.get(fileList.size() - 1);
String zipFileName = folderPath + "/" + folderPath.substring(folderPath.lastIndexOf("/") + 1)
+ ".out.zip";

ZipManager zipManager = new ZipManager(MinITActivity.this, new ZipListener() {
@Override
public void onStarted(@NonNull final long totalBytes) {
Expand Down Expand Up @@ -308,16 +376,105 @@ public void onComplete(@NonNull final boolean success, @Nullable final Exception
public void run() {
if (success) {
statusTextView.setText("Zip Successful");
String path = folderPath + ".out.zip";
new FTPUploadTask().execute(serverIP, path);
} else {
statusTextView.setText("Zip Error");
}
}
});
}
});
zipManager.zip(folderPath);

fileList.remove(fileList.size() - 1);
zipManager.zip(fileList, zipFileName);
}

private void openFileManager(boolean dirOnly, boolean toCompress) {
fileList = new ArrayList<>();

if (toCompress) {
new ChooserDialog(MinITActivity.this)
.withFilter(dirOnly, false)
.enableMultiple(true)
.withChosenListener(new ChooserDialog.Result() {
@Override
public void onChoosePath(String path, File pathFile) {
if (fileList.contains(path)) {
fileList.remove(path);
} else {
fileList.add(path);
}
}
})
// to handle the back key pressed or clicked outside the dialog:
.withOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
dialog.cancel();
}
})
.withOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(final DialogInterface dialog) {
compressDataSet();
}
})
.withResources(R.string.title_choose_any_file, R.string.title_choose, R.string.dialog_cancel)
.build()
.show();
} else {
new ChooserDialog(MinITActivity.this)
.withFilter(dirOnly, false)
// to handle the result(s)
.withChosenListener(new ChooserDialog.Result() {
@Override
public void onChoosePath(String path, File pathFile) {
uploadDataSet(path);
}
})
.build()
.show();
}
}

private void showResumeMessage(PipelineState state) {

String msg;

if (state == PipelineState.TO_BE_UPLOAD) {
msg = "You can upload the data from previous job";
} else {
msg = "You can re run the previous job";
}

new AlertDialog.Builder(this)
.setTitle("Resume App State")
.setMessage(msg)
.setPositiveButton("Resume", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ServerConnectionUtils.setWrapperObject(
(WrapperObject) PreferenceUtil
.getSharedPreferenceObject(R.string.id_wrapper_obj));
if (state == PipelineState.TO_BE_UPLOAD) {
btnRequestJob.setVisibility(View.GONE);
trSendResults.setVisibility(View.VISIBLE);
resultsSummary = PreferenceUtil.getSharedPreferenceString(R.string.id_results_summary);
} else {
GUIConfiguration.setPipelineState(PipelineState.CONFIGURED);
GUIConfiguration
.setSteps(PreferenceUtil.getSharedPreferenceStepList(R.string.id_step_list));
Intent intent = new Intent(MinITActivity.this, TerminalActivity.class);
startActivity(intent);
}
}
})
.setNegativeButton("Get new Job", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
dialog.dismiss();
}
})
.setCancelable(false)
.show();
}

private boolean validateIPAddress(final String ip) {
Expand Down
Loading

0 comments on commit 4286231

Please sign in to comment.