upload indicator

This commit is contained in:
Page Asgardius 2022-09-13 17:37:48 -07:00
parent a226cc9f70
commit 81690176a6
3 changed files with 38 additions and 17 deletions

View file

@ -23,6 +23,7 @@ import com.amazonaws.regions.Region;
import com.amazonaws.services.s3.AmazonS3; 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.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult; import com.amazonaws.services.s3.model.PutObjectResult;
import java.io.File; import java.io.File;
@ -35,6 +36,7 @@ import java.util.concurrent.TimeUnit;
public class Uploader extends AppCompatActivity { public class Uploader extends AppCompatActivity {
String username, password, endpoint, bucket, prefix, location, fkey; String username, password, endpoint, bucket, prefix, location, fkey;
boolean isfolder; boolean isfolder;
int progress;
Uri fileuri, folder; Uri fileuri, folder;
EditText fprefix; EditText fprefix;
Region region; Region region;
@ -43,6 +45,8 @@ public class Uploader extends AppCompatActivity {
AmazonS3 s3client; AmazonS3 s3client;
ProgressBar simpleProgressBar; ProgressBar simpleProgressBar;
String[] filename; String[] filename;
PutObjectResult upload;
long filesize;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -84,12 +88,17 @@ public class Uploader extends AppCompatActivity {
} else { } else {
//Toast.makeText(CreateBucket.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); //Toast.makeText(CreateBucket.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show();
//System.out.println(file.getPath()); //System.out.println(file.getPath());
simpleProgressBar.setVisibility(View.VISIBLE);
fileUpload.setEnabled(false);
fileUpload.setText(getResources().getString(R.string.wait));
if (fprefix.getText().toString().endsWith("/") || fprefix.getText().toString().equals("")) { if (fprefix.getText().toString().endsWith("/") || fprefix.getText().toString().equals("")) {
fkey = fprefix.getText().toString()+filename[filename.length-1]; fkey = fprefix.getText().toString()+filename[filename.length-1];
} else { } else {
fkey = fprefix.getText().toString()+"/"+filename[filename.length-1]; fkey = fprefix.getText().toString()+"/"+filename[filename.length-1];
} }
System.out.println(fkey); System.out.println(fkey);
progress = 0;
filesize = 0;
Thread uploadFile = new Thread(new Runnable() { Thread uploadFile = new Thread(new Runnable() {
@Override @Override
@ -99,25 +108,16 @@ public class Uploader extends AppCompatActivity {
//s3client.createBucket(bucket, location); //s3client.createBucket(bucket, location);
//System.out.println(fkey); //System.out.println(fkey);
File ufile = readContentToFile(fileuri); File ufile = readContentToFile(fileuri);
long filesize =ufile.length(); filesize = ufile.length();
PutObjectResult upload = s3client.putObject(bucket, fkey, ufile); PutObjectRequest request = new PutObjectRequest(bucket, fkey, ufile);
upload = s3client.putObject(request);
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
simpleProgressBar.setVisibility(View.VISIBLE); //simpleProgressBar.setProgress(100);
while (upload.getMetadata().getContentLength() <= filesize-1) { simpleProgressBar.setVisibility(View.INVISIBLE);
if(upload.getMetadata().getContentLength() != 0) { fileUpload.setText(getResources().getString(R.string.success));
simpleProgressBar.setProgress(((int)filesize / (int)upload.getMetadata().getContentLength())*100);
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
simpleProgressBar.setProgress(100);
// Sending reference and data to Adapter
Toast.makeText(getApplicationContext(),getResources().getString(R.string.upload_success), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(),getResources().getString(R.string.upload_success), Toast.LENGTH_SHORT).show();
//simpleProgressBar.setVisibility(View.INVISIBLE); //simpleProgressBar.setVisibility(View.INVISIBLE);
} }
@ -131,16 +131,34 @@ public class Uploader extends AppCompatActivity {
@Override @Override
public void run() { public void run() {
fileUpload.setEnabled(true);
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();
finish(); //finish();
} }
} }
}); });
//simpleProgressBar.setVisibility(View.VISIBLE); //simpleProgressBar.setVisibility(View.VISIBLE);
uploadFile.start(); uploadFile.start();
/*while (progress <= 99) {
System.out.println("Upload in progress");
if (upload != null) {
System.out.println(upload.getMetadata().getContentLength());
if(upload.getMetadata().getContentLength() != 0) {
progress = (int) (((int)filesize / (int)upload.getMetadata().getContentLength())*100);
simpleProgressBar.setProgress(progress);
}
}
System.out.println(filesize);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}*/
} }
} }

View file

@ -48,7 +48,7 @@
android:id="@+id/simpleProgressBar" android:id="@+id/simpleProgressBar"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:indeterminate="false" android:indeterminate="true"
android:max="100" android:max="100"
android:padding="20dp" android:padding="20dp"
android:progress="50" android:progress="50"

View file

@ -22,6 +22,9 @@
<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="wait">Wait</string>
<string name="retry">Retry</string>
<string name="success">Success</string>
<string name="create_bucket">Create new bucket</string> <string name="create_bucket">Create new bucket</string>
<string name="create_bucket_success">Bucket created successfully</string> <string name="create_bucket_success">Bucket created successfully</string>
<string name="upload_file_here">Upload file in current folder</string> <string name="upload_file_here">Upload file in current folder</string>