Merge pull request 'transfer-progress' (#8) from transfer-progress into main

Reviewed-on: https://patrice.asgardius.company/gitea/asgardius/s3manager/pulls/8
This commit is contained in:
Page Asgardius 2022-09-24 17:53:24 -07:00
commit a0c3cc6dc7
6 changed files with 259 additions and 172 deletions

View file

@ -5,7 +5,6 @@ import static android.content.ContentValues.TAG;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity; import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
@ -17,9 +16,7 @@ import android.provider.MediaStore;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSCredentials;
@ -29,11 +26,7 @@ import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions; import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3Object;
import com.google.android.material.snackbar.Snackbar;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -48,8 +41,12 @@ public class Downloader extends AppCompatActivity {
ProgressBar simpleProgressBar; ProgressBar simpleProgressBar;
Intent intent; Intent intent;
Button fileDownload; Button fileDownload;
Thread downloadFile; Thread downloadFile, downloadProgress;
S3Object object; S3Object object;
boolean started = false;
boolean cancel = false;
long filesize = 0;
long transfered = 0;
private WifiManager.WifiLock mWifiLock; private WifiManager.WifiLock mWifiLock;
private PowerManager.WakeLock mWakeLock; private PowerManager.WakeLock mWakeLock;
private PowerManager powerManager; private PowerManager powerManager;
@ -85,9 +82,15 @@ public class Downloader extends AppCompatActivity {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
//buttonaction //buttonaction
if (started) {
cancel = true;
downloadFile.interrupt();
//simpleProgressBar.setVisibility(View.INVISIBLE);
} else {
started = true;
simpleProgressBar.setVisibility(View.VISIBLE); simpleProgressBar.setVisibility(View.VISIBLE);
fileDownload.setEnabled(false); //fileDownload.setEnabled(false);
fileDownload.setText(getResources().getString(R.string.download_in_progress)); fileDownload.setText(getResources().getString(R.string.cancel_download));
//Acquiring WakeLock and WifiLock if not held //Acquiring WakeLock and WifiLock if not held
if (!mWifiLock.isHeld()) { if (!mWifiLock.isHeld()) {
mWifiLock.acquire(); mWifiLock.acquire();
@ -107,6 +110,7 @@ public class Downloader extends AppCompatActivity {
//s3client.createBucket(bucket, location); //s3client.createBucket(bucket, location);
//System.out.println(fkey); //System.out.println(fkey);
object = s3client.getObject(bucket, prefix+filename); object = s3client.getObject(bucket, prefix+filename);
filesize = (object.getObjectMetadata().getContentLength())/1024;
writeContentToFile(fileuri); writeContentToFile(fileuri);
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
@ -121,9 +125,9 @@ public class Downloader extends AppCompatActivity {
mWakeLock.release(); mWakeLock.release();
//System.out.println("WakeLock released"); //System.out.println("WakeLock released");
} }
simpleProgressBar.setVisibility(View.INVISIBLE); simpleProgressBar.setProgress(100);
fileDownload.setText(getResources().getString(R.string.download_success)); fileDownload.setText(getResources().getString(R.string.download_success));
Toast.makeText(getApplicationContext(),getResources().getString(R.string.download_success), Toast.LENGTH_SHORT).show(); fileDownload.setEnabled(false);
//simpleProgressBar.setVisibility(View.INVISIBLE); //simpleProgressBar.setVisibility(View.INVISIBLE);
} }
}); });
@ -145,9 +149,12 @@ public class Downloader extends AppCompatActivity {
mWakeLock.release(); mWakeLock.release();
//System.out.println("WakeLock released"); //System.out.println("WakeLock released");
} }
simpleProgressBar.setVisibility(View.INVISIBLE); if (cancel) {
fileDownload.setText(getResources().getString(R.string.download_canceled));
} else {
fileDownload.setText(getResources().getString(R.string.download_failed)); fileDownload.setText(getResources().getString(R.string.download_failed));
Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); }
fileDownload.setEnabled(false);
} }
}); });
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
@ -155,7 +162,33 @@ public class Downloader extends AppCompatActivity {
} }
} }
}); });
downloadProgress = new Thread(new Runnable() {
@Override
public void run() {
try {
//Your code goes here
while (fileDownload.isEnabled()) {
try {
if (filesize != 0) {
simpleProgressBar.setProgress((int)((transfered*100)/filesize));
}
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
//finish();
}
}
});
downloadFile.start(); downloadFile.start();
downloadProgress.start();
}
} }
}); });
@ -208,6 +241,7 @@ public class Downloader extends AppCompatActivity {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
for (int len; (len = in.read(buffer)) != -1; ) { for (int len; (len = in.read(buffer)) != -1; ) {
out.write(buffer, 0, len); out.write(buffer, 0, len);
transfered ++;
} }
} }
} }

View file

@ -12,7 +12,6 @@ import android.net.Uri;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.storage.StorageManager;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
@ -58,7 +57,9 @@ public class Uploader extends AppCompatActivity {
File ufile; File ufile;
Intent intent; Intent intent;
Button fileUpload; Button fileUpload;
Thread uploadFile; Thread uploadFile, uploadProgress;
boolean started = false;
long transfered = 0;
private static final long MAX_SINGLE_PART_UPLOAD_BYTES = 5 * 1024 * 1024; private static final long MAX_SINGLE_PART_UPLOAD_BYTES = 5 * 1024 * 1024;
private WifiManager.WifiLock mWifiLock; private WifiManager.WifiLock mWifiLock;
private PowerManager.WakeLock mWakeLock; private PowerManager.WakeLock mWakeLock;
@ -97,6 +98,15 @@ public class Uploader extends AppCompatActivity {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
//buttonaction //buttonaction
if (started) {
started = false;
uploadFile.interrupt();
//simpleProgressBar.setVisibility(View.INVISIBLE);
} else {
started = true;
transfered = 0;
//simpleProgressBar.setProgress(0);
simpleProgressBar.setVisibility(View.VISIBLE);
if (fileuri == null && folder == null) { if (fileuri == null && folder == null) {
Toast.makeText(Uploader.this, getResources().getString(R.string.no_file_selected), Toast.LENGTH_SHORT).show(); Toast.makeText(Uploader.this, getResources().getString(R.string.no_file_selected), Toast.LENGTH_SHORT).show();
} else { } else {
@ -109,9 +119,8 @@ public class Uploader extends AppCompatActivity {
mWakeLock.acquire(); mWakeLock.acquire();
//System.out.println("WakeLock acquired"); //System.out.println("WakeLock acquired");
} }
simpleProgressBar.setVisibility(View.VISIBLE); //eUpload.setEnabled(false);
fileUpload.setEnabled(false); fileUpload.setText(getResources().getString(R.string.cancel_upload));
fileUpload.setText(getResources().getString(R.string.upload_in_progress));
uploadFile = new Thread(new Runnable() { uploadFile = new Thread(new Runnable() {
@Override @Override
@ -146,10 +155,12 @@ public class Uploader extends AppCompatActivity {
mWakeLock.release(); mWakeLock.release();
//System.out.println("WakeLock released"); //System.out.println("WakeLock released");
} }
//simpleProgressBar.setProgress(100); simpleProgressBar.setProgress(100);
simpleProgressBar.setVisibility(View.INVISIBLE); //simpleProgressBar.setVisibility(View.INVISIBLE);
fileUpload.setText(getResources().getString(R.string.upload_success)); fileUpload.setText(getResources().getString(R.string.upload_success));
Toast.makeText(getApplicationContext(),getResources().getString(R.string.upload_success), Toast.LENGTH_SHORT).show(); started = false;
fileUpload.setEnabled(false);
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.upload_success), Toast.LENGTH_SHORT).show();
//simpleProgressBar.setVisibility(View.INVISIBLE); //simpleProgressBar.setVisibility(View.INVISIBLE);
} }
}); });
@ -171,10 +182,11 @@ public class Uploader extends AppCompatActivity {
mWakeLock.release(); mWakeLock.release();
//System.out.println("WakeLock released"); //System.out.println("WakeLock released");
} }
simpleProgressBar.setVisibility(View.INVISIBLE); started = false;
fileUpload.setEnabled(true); //simpleProgressBar.setVisibility(View.INVISIBLE);
//fileUpload.setEnabled(true);
fileUpload.setText(getResources().getString(R.string.retry)); fileUpload.setText(getResources().getString(R.string.retry));
Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
} }
}); });
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
@ -182,8 +194,43 @@ public class Uploader extends AppCompatActivity {
} }
} }
}); });
uploadProgress = new Thread(new Runnable() {
@Override
public void run() {
try {
//Your code goes here
while (started) {
try {
if (filesize != 0) {
//simpleProgressBar.setProgress((int)((transfered*100)/filesize));
simpleProgressBar.setProgress((int)((transfered*100)/filesize));
}
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//simpleProgressBar.setProgress(100);
}
});
//System.out.println("tree "+treelevel);
//System.out.println("prefix "+prefix);
} catch (Exception e) {
e.printStackTrace();
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
//finish();
}
}
});
//simpleProgressBar.setVisibility(View.VISIBLE); //simpleProgressBar.setVisibility(View.VISIBLE);
uploadFile.start(); uploadFile.start();
uploadProgress.start();
/*while (progress <= 99) { /*while (progress <= 99) {
System.out.println("Upload in progress"); System.out.println("Upload in progress");
if (upload != null) { if (upload != null) {
@ -202,6 +249,7 @@ public class Uploader extends AppCompatActivity {
}*/ }*/
} }
} }
}
}); });
} }
@ -319,6 +367,7 @@ public class Uploader extends AppCompatActivity {
partETags.add(uploadResult.getPartETag()); partETags.add(uploadResult.getPartETag());
fileOffset += partSize; fileOffset += partSize;
transfered = fileOffset;
} }
// Complete the multipart upload. // Complete the multipart upload.

View file

@ -28,12 +28,13 @@
<ProgressBar <ProgressBar
android:id="@+id/simpleProgressBar" android:id="@+id/simpleProgressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:indeterminate="true" android:indeterminate="false"
android:max="100" android:max="100"
android:padding="20dp" android:padding="20dp"
android:progress="50" android:progress="0"
android:visibility="invisible" android:visibility="invisible"
tools:ignore="MissingConstraints" /> tools:ignore="MissingConstraints" />

View file

@ -46,12 +46,13 @@
<ProgressBar <ProgressBar
android:id="@+id/simpleProgressBar" android:id="@+id/simpleProgressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:indeterminate="true" android:indeterminate="false"
android:max="100" android:max="100"
android:padding="20dp" android:padding="20dp"
android:progress="50" android:progress="0"
android:visibility="invisible" android:visibility="invisible"
tools:ignore="MissingConstraints" /> tools:ignore="MissingConstraints" />

View file

@ -11,7 +11,7 @@
<string name="bucket_name_empty">Se requiere un nombre para el bucket</string> <string name="bucket_name_empty">Se requiere un nombre para el bucket</string>
<string name="file_share">Compartir archivo</string> <string name="file_share">Compartir archivo</string>
<string name="download_file">Descargar archivo</string> <string name="download_file">Descargar archivo</string>
<string name="download_in_progress">Descarga en proceso</string> <string name="cancel_download">Cancelar descarga</string>
<string name="download_success">Archivo descargado exitosamente</string> <string name="download_success">Archivo descargado exitosamente</string>
<string name="upload_file_tobucket">Subir archivo en este bucket</string> <string name="upload_file_tobucket">Subir archivo en este bucket</string>
<string name="upload_folder_tobucket">Subir carpeta en este bucket</string> <string name="upload_folder_tobucket">Subir carpeta en este bucket</string>
@ -21,7 +21,7 @@
<string name="upload_prefix">Prefijo del objeto</string> <string name="upload_prefix">Prefijo del objeto</string>
<string name="no_file_selected">Favor de seleccionar un archivo</string> <string name="no_file_selected">Favor de seleccionar un archivo</string>
<string name="upload_success">Archivo subido exitosamente</string> <string name="upload_success">Archivo subido exitosamente</string>
<string name="upload_in_progress">Subida en proceso</string> <string name="cancel_upload">Cancelar subida</string>
<string name="retry">Reintentar</string> <string name="retry">Reintentar</string>
<string name="download_failed">Descarga fallida</string> <string name="download_failed">Descarga fallida</string>
<string name="success">Éxito</string> <string name="success">Éxito</string>
@ -84,4 +84,5 @@
<string name="bucket_size">Tamaño del bucket</string> <string name="bucket_size">Tamaño del bucket</string>
<string name="file_count">Archivos</string> <string name="file_count">Archivos</string>
<string name="null_expiration_date">Debe establecer una fecha de expiración para compartir archivos</string> <string name="null_expiration_date">Debe establecer una fecha de expiración para compartir archivos</string>
<string name="download_canceled">Descarga cancelada</string>
</resources> </resources>

View file

@ -27,7 +27,7 @@
<string name="file_share">Share file</string> <string name="file_share">Share file</string>
<string name="file_external">Open in</string> <string name="file_external">Open in</string>
<string name="download_file">Download file</string> <string name="download_file">Download file</string>
<string name="download_in_progress">Download in progress</string> <string name="cancel_download">Cancel download</string>
<string name="download_success">File downloaded successfully</string> <string name="download_success">File downloaded successfully</string>
<string name="upload_file_tobucket">Upload file to this bucket</string> <string name="upload_file_tobucket">Upload file to this bucket</string>
<string name="upload_folder_tobucket">Upload folder to this bucket</string> <string name="upload_folder_tobucket">Upload folder to this bucket</string>
@ -37,9 +37,10 @@
<string name="upload_prefix">Object prefix</string> <string name="upload_prefix">Object prefix</string>
<string name="no_file_selected">Please select a file</string> <string name="no_file_selected">Please select a file</string>
<string name="upload_success">File uploaded successfully</string> <string name="upload_success">File uploaded successfully</string>
<string name="upload_in_progress">Upload in progress</string> <string name="cancel_upload">Cancel Upload</string>
<string name="retry">Retry</string> <string name="retry">Retry</string>
<string name="download_failed">Download failed</string> <string name="download_failed">Download failed</string>
<string name="download_canceled">Download canceled</string>
<string name="success">Success</string> <string name="success">Success</string>
<string name="object_info">Properties</string> <string name="object_info">Properties</string>
<string name="file_size">File Size</string> <string name="file_size">File Size</string>