Skip to content

Commit

Permalink
Merge pull request #139 from SanojPunchihewa/auto-save-config
Browse files Browse the repository at this point in the history
Automate F5N client mode
  • Loading branch information
SanojPunchihewa authored Mar 17, 2020
2 parents 541796c + 9a2747a commit 9ce80cf
Show file tree
Hide file tree
Showing 15 changed files with 504 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.preference:preference:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

<application
android:name=".Application"
android:allowBackup="true"
android:icon="@mipmap/launcher"
android:label="@string/app_name"
android:name=".Application"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning"
android:usesCleartextTraffic="true">
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".activity.SettingsActivity"
android:label="@string/title_activity_settings"></activity>
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mobilegenomics.f5n.GUIConfiguration;
import com.mobilegenomics.f5n.R;
import com.mobilegenomics.f5n.core.AppMode;
import com.mobilegenomics.f5n.fragments.FragmentSettings;
import com.mobilegenomics.f5n.support.PermissionResultCallback;
import com.mobilegenomics.f5n.support.PermissionUtils;
import java.util.ArrayList;
Expand Down Expand Up @@ -149,6 +150,9 @@ public boolean onOptionsItemSelected(final MenuItem item) {
case R.id.action_help:
startActivity(new Intent(MainActivity.this, HelpActivity.class));
return true;
case R.id.action_settings:
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
break;
}
return super.onOptionsItemSelected(item);
}
Expand Down
181 changes: 172 additions & 9 deletions app/src/main/java/com/mobilegenomics/f5n/activity/MinITActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
Expand All @@ -19,14 +21,17 @@
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.core.Step;
import com.mobilegenomics.f5n.dto.State;
import com.mobilegenomics.f5n.dto.WrapperObject;
import com.mobilegenomics.f5n.support.PipelineState;
Expand All @@ -37,15 +42,21 @@
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;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.io.CopyStreamAdapter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Objects;

public class MinITActivity extends AppCompatActivity {

class FTPUploadTask extends AsyncTask<String, Long, Boolean> {
Expand Down Expand Up @@ -171,6 +182,11 @@ protected void onProgressUpdate(final Long... values) {

private ArrayList<String> fileList;

private String DEFAULT_STORAGE_PATH = Environment.getExternalStorageDirectory() + "/mobile-genomics/";
private String STORAGE_PATH = PreferenceUtil.getSharedPreferenceString(R.string.key_storage_preference, DEFAULT_STORAGE_PATH);
private static final String DATA_SET = "\\$DATA_SET/";
private static final String REFERENCE_GNOME = "\\$REF_GNOME/";

public static void logHandler(Handler handler) {
handler.post(new Runnable() {
@Override
Expand Down Expand Up @@ -267,14 +283,77 @@ public void onClick(View v) {
PreferenceUtil
.setSharedPreferenceObject(R.string.id_wrapper_obj, ServerConnectionUtils.getWrapperObject());

Intent intent = new Intent(MinITActivity.this, DownloadActivity.class);
configureStepFolderPath();
// TODO Fix the following
// Protocol, file server IP and Port
intent.putExtra("DATA_SET_URL", serverIP);
intent.putExtra("FILE_NAME", zipFileName);
startActivity(intent);
downloadDataSetFTP();
}
});
}

private void configureStepFolderPath() {
String dataSetFolderPath = STORAGE_PATH + zipFileName.substring(0, zipFileName.length() - 4) + "/";
String referenceGnomePath = PreferenceUtil.getSharedPreferenceString(R.string.key_reference_gnome, dataSetFolderPath);
for (Step step : GUIConfiguration.getSteps()) {
step.setCommandString(step.getCommandString().replaceAll(DATA_SET, dataSetFolderPath));
step.setCommandString(step.getCommandString().replaceAll(REFERENCE_GNOME, referenceGnomePath));
}
}

private void downloadDataSetFTP() {
new MinITActivity.FTPDownloadTask().execute(serverIP, zipFileName);
}

private void extractZip(String filePath) {

ZipManager zipManager = new ZipManager(MinITActivity.this, new ZipListener() {
@Override
public void onStarted(@NonNull final long totalBytes) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setMax(100);
statusTextView.setText("Unzip started");
connectionLogText.append("Extracting data set...\n");
}
});
}

@Override
public void onProgress(@NonNull final long bytesDone, @NonNull final long totalBytes) {
runOnUiThread(new Runnable() {
@Override
public void run() {
int perc = ZipManager.getZipPercentage(bytesDone, totalBytes);
progressBar.setProgress(perc);
statusTextView.setText("Unzipping: " + perc + "%");
}
});
}

@Override
public void onComplete(@NonNull final boolean success, @Nullable final Exception exception) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (success) {
statusTextView.setText("Unzip Successful");
connectionLogText.append("Extracting data set completed\n");

GUIConfiguration.setPipelineState(PipelineState.TO_BE_CONFIGURED);
Intent intent = new Intent(MinITActivity.this, TerminalActivity.class);
intent.putExtra("FOLDER_PATH", STORAGE_PATH);
startActivity(intent);
} else {
statusTextView.setText("Unzip Error");
connectionLogText.append("Extracting data set error\n");
}
}
});
}
});
Uri treeUri = PreferenceUtil.getSharedPreferenceUri(R.string.sdcard_uri);
zipManager.unzip(treeUri, filePath);
}

public void copyIPAddress(View view) {
Expand Down Expand Up @@ -313,7 +392,7 @@ public void onSuccess(final WrapperObject job) {
@Override
public void run() {
GUIConfiguration.configureSteps(job.getSteps());
zipFileName = job.getPrefix();
zipFileName = job.getPrefix() + ".zip";
btnProcessJob.setVisibility(View.VISIBLE);
}
});
Expand Down Expand Up @@ -489,4 +568,88 @@ private boolean validateIPAddress(final String ip) {
return ip.matches(PATTERN);
}

class FTPDownloadTask extends AsyncTask<String, Long, Boolean> {

long downloadStartTime;

long fileSize;

boolean status;

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

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

});

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

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

File dir = new File(STORAGE_PATH + urls[1]);

OutputStream out = new FileOutputStream(dir);
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");
connectionLogText.append("Download completed\n");
long downloadTime = System.currentTimeMillis() - downloadStartTime;
if (downloadSuccess) {
String time = TimeFormat.millisToShortDHMS(downloadTime);
statusTextView.setText("Download Completed in " + time);
extractZip(STORAGE_PATH + zipFileName);
} else {
statusTextView.setText("Download Error");
}
}

@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i(TAG, "Download Started");
connectionLogText.append("\nDownload Started for" + zipFileName + "\n");
downloadStartTime = System.currentTimeMillis();
statusTextView.setText("Download Started");
progressBar.setMax(100);
}

@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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mobilegenomics.f5n.GUIConfiguration;
import com.mobilegenomics.f5n.R;
import com.mobilegenomics.f5n.core.PipelineStep;
import com.mobilegenomics.f5n.support.PipelineState;
import com.mobilegenomics.f5n.support.PreferenceUtil;

import java.util.ArrayList;

public class PipelineActivity extends AppCompatActivity {
Expand All @@ -23,6 +28,8 @@ public class PipelineActivity extends AppCompatActivity {

private static final int MODE_TERMINAL = 1;

private static final int MODE_PREV_CONFIG = 2;

@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -39,7 +46,7 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
}

Button btnGUIMode = new Button(this);
btnGUIMode.setText("Use GUI Mode");
btnGUIMode.setText(getString(R.string.btn_use_gui_mode));
btnGUIMode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
Expand All @@ -49,14 +56,24 @@ public void onClick(final View v) {
linearLayout.addView(btnGUIMode);

Button btnTerminalMode = new Button(this);
btnTerminalMode.setText("Use Terminal Mode");
btnTerminalMode.setText(getString(R.string.btn_use_terminal_mode));
btnTerminalMode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
initPipelineSteps(MODE_TERMINAL);
}
});
linearLayout.addView(btnTerminalMode);

Button btnLoadPreviousConfig = new Button(this);
btnLoadPreviousConfig.setText(getString(R.string.btn_load_prev_config));
btnLoadPreviousConfig.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
initPipelineSteps(MODE_PREV_CONFIG);
}
});
linearLayout.addView(btnLoadPreviousConfig);
}

private void initPipelineSteps(int mode) {
Expand All @@ -76,10 +93,24 @@ private void initPipelineSteps(int mode) {
GUIConfiguration.configureSteps(PipelineActivity.this, null);
if (mode == MODE_GUI) {
startActivity(new Intent(PipelineActivity.this, StepActivity.class));
} else {
} else if (mode == MODE_TERMINAL) {
startActivity(new Intent(PipelineActivity.this, TerminalActivity.class));
} else {
loadPreviousConfig();
}
} else {
loadPreviousConfig();
}
}

private void loadPreviousConfig() {
if ((PreferenceUtil.getSharedPreferenceString(R.string.id_folder_path) != null) &&
(PreferenceUtil.getSharedPreferenceStepList(R.string.id_step_list) != null) &&
!(PreferenceUtil.getSharedPreferenceStepList(R.string.id_step_list).isEmpty())) {
GUIConfiguration.setPipelineState(PipelineState.PREV_CONFIG_LOAD);
startActivity(new Intent(PipelineActivity.this, TerminalActivity.class));
} else {
Toast.makeText(PipelineActivity.this, "No previous configs to load", Toast.LENGTH_SHORT).show();
}
}
}
Loading

0 comments on commit 9ce80cf

Please sign in to comment.