Skip to content

Commit

Permalink
Merge pull request #72 from SanojPunchihewa/minIt-android-implementation
Browse files Browse the repository at this point in the history
Add file upload functionality
  • Loading branch information
SanojPunchihewa authored Nov 4, 2019
2 parents d4556a7 + 1d05739 commit 14b2abb
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ 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 "net.gotev:uploadservice:3.5.2"
implementation 'com.liulishuo.okdownload:okdownload:1.0.5'
implementation 'com.liulishuo.okdownload:sqlite:1.0.5'
implementation 'com.liulishuo.okdownload:okhttp:1.0.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
Expand Down Expand Up @@ -50,6 +52,8 @@ public class ConfirmationActivity extends AppCompatActivity {

private boolean logWrittenToFile = false;

private String folderPath;

TextView txtLogs;

NestedScrollView scrollView;
Expand All @@ -64,6 +68,8 @@ public class ConfirmationActivity extends AppCompatActivity {

ProgressBar mProgressBar;

MediaPlayer mp;

@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -80,6 +86,10 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
linearLayout.addView(txtCommand);
}

if (getIntent().getExtras() != null) {
folderPath = getIntent().getExtras().getString("FOLDER_PATH");
}

btnProceed = new Button(this);
btnProceed.setText("Run the Pipeline");
btnProceed.setOnClickListener(new OnClickListener() {
Expand Down Expand Up @@ -125,6 +135,8 @@ public void onClick(final View v) {
separator2.setBackgroundColor(Color.parseColor("#000000"));
linearLayout.addView(separator2);

mp = MediaPlayer.create(this, R.raw.alarm);

btnWriteLog = new Button(this);
btnWriteLog.setText("Write Log to File");
btnWriteLog.setOnClickListener(new OnClickListener() {
Expand All @@ -143,8 +155,10 @@ public void onClick(final View v) {
btnSendResults.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
mp.stop();
Intent intent = new Intent(ConfirmationActivity.this, MinITActivity.class);
intent.putExtra("PIPELINE_STATUS", resultsSummary);
intent.putExtra("FOLDER_PATH", folderPath);
startActivity(intent);
}
});
Expand Down Expand Up @@ -191,6 +205,8 @@ protected void onPostExecute(final String s) {
btnProceed.setEnabled(true);
mProgressBar.setVisibility(View.GONE);
if (GUIConfiguration.getAppMode() == AppMode.SLAVE) {
mp.start();
mp.setLooping(true);
btnSendResults.setVisibility(View.VISIBLE);
}
}
Expand Down
104 changes: 99 additions & 5 deletions app/src/main/java/com/mobilegenomics/f5n/activity/MinITActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mobilegenomics.f5n.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -8,19 +9,33 @@
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

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.ServerCallback;
import com.mobilegenomics.f5n.support.ServerConnectionUtils;

public class MinITActivity extends AppCompatActivity {
import com.mobilegenomics.f5n.support.ZipManager;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import net.gotev.uploadservice.BinaryUploadRequest;
import net.gotev.uploadservice.ServerResponse;
import net.gotev.uploadservice.UploadInfo;
import net.gotev.uploadservice.UploadNotificationConfig;
import net.gotev.uploadservice.UploadService;
import net.gotev.uploadservice.UploadServiceSingleBroadcastReceiver;
import net.gotev.uploadservice.UploadStatusDelegate;

public class MinITActivity extends AppCompatActivity implements UploadStatusDelegate {

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

private static TextView connectionLogText;

Expand All @@ -34,6 +49,14 @@ public class MinITActivity extends AppCompatActivity {

private String resultsSummary;

private String folderPath;

TextView statusTextView;

ProgressBar progressBar;

private UploadServiceSingleBroadcastReceiver uploadReceiver;

public static void logHandler(Handler handler) {
handler.post(new Runnable() {
@Override
Expand All @@ -51,13 +74,21 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_minit);

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

UploadService.NAMESPACE = BuildConfig.APPLICATION_ID;

uploadReceiver = new UploadServiceSingleBroadcastReceiver(this);

final EditText 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);

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");
Expand All @@ -74,7 +105,7 @@ public void onClick(View v) {

ServerConnectionUtils.setServerAddress(serverIP);
if (ranPipeline) {
sendJobResults();
uploadDataSet();
} else {
requestJob();
}
Expand Down Expand Up @@ -139,4 +170,67 @@ public void onError(final WrapperObject job) {
}
});
}

private void uploadDataSet() {
// TODO check wifi connectivity
ZipManager zipManager = new ZipManager(MinITActivity.this);
zipManager.zip(folderPath);
String path = folderPath + ".zip";
try {
String uploadId =
new BinaryUploadRequest(this, "http://" + serverIP + ":8000/")
.setFileToUpload(path)
.setMethod("POST")
.addHeader("file-name", new File(path).getName())
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload();
// More info about receivers https://github.com/gotev/android-upload-service/wiki/Monitoring-upload-status
uploadReceiver.setUploadID(uploadId);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

@Override
public void onProgress(final Context context, final UploadInfo uploadInfo) {
String totalBytes = Util.humanReadableBytes(uploadInfo.getTotalBytes(), true);
String uploadedBytes = Util.humanReadableBytes(uploadInfo.getUploadedBytes(), true);
String status = "Uploading: " + uploadedBytes + "/" + totalBytes;
statusTextView.setText(status);
progressBar.setProgress(uploadInfo.getProgressPercent());
}

@Override
public void onError(final Context context, final UploadInfo uploadInfo, final ServerResponse serverResponse,
final Exception exception) {
statusTextView.setText("Result Upload failed: " + serverResponse.getHttpCode());
Log.e(TAG, "Upload Failed: " + serverResponse.getBodyAsString());
}

@Override
public void onCompleted(final Context context, final UploadInfo uploadInfo, final ServerResponse serverResponse) {
statusTextView.setText("Result Upload completed: " + serverResponse.getHttpCode());
sendJobResults();
}

@Override
public void onCancelled(final Context context, final UploadInfo uploadInfo) {
statusTextView.setText("Result Upload cancelled");
}

@Override
protected void onResume() {
super.onResume();
uploadReceiver.register(this);
}

@Override
protected void onPause() {
super.onPause();
uploadReceiver.unregister(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ public void onClick(final View v) {
}

GUIConfiguration.createPipeline();
startActivity(new Intent(TerminalActivity.this, ConfirmationActivity.class));
Intent intent = new Intent(TerminalActivity.this, ConfirmationActivity.class);
intent.putExtra("FOLDER_PATH", folderPath);
startActivity(intent);
}
});

Expand Down
22 changes: 20 additions & 2 deletions app/src/main/res/layout/activity_minit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,28 @@
android:text="@string/btn_connect" />
</TableRow>

<TextView
android:id="@+id/txt_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tr_send_message"
android:layout_marginLeft="10dp" />

<ProgressBar
android:id="@+id/progress_upload_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:progressTint="@android:color/black"
android:layout_below="@id/txt_status"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/tr_send_message"
android:layout_below="@+id/progress_upload_status"
android:layout_above="@id/btn_send_result"
android:layout_margin="10dp"
android:id="@+id/scroll_view">
Expand All @@ -47,6 +64,7 @@
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>

<Button
android:id="@+id/btn_send_result"
android:layout_width="wrap_content"
Expand All @@ -55,5 +73,5 @@
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:visibility="gone"
android:text="@string/btn_process_job"/>
android:text="@string/btn_process_job" />
</RelativeLayout>
Binary file added app/src/main/res/raw/alarm.mp3
Binary file not shown.

0 comments on commit 14b2abb

Please sign in to comment.