From a994e0c2f37e92f262b95061c176e652e5603f1b Mon Sep 17 00:00:00 2001 From: Page Asgardius Date: Thu, 15 Sep 2022 12:01:14 -0700 Subject: [PATCH] downloader template --- .idea/misc.xml | 2 + .../asgardius/page/s3manager/Downloader.java | 181 ++++++++++++++++++ .../page/s3manager/ObjectSelect.java | 19 +- .../asgardius/page/s3manager/Uploader.java | 7 +- .../main/res/layout/activity_downloader.xml | 44 ++++- app/src/main/res/menu/object_menu.xml | 5 + app/src/main/res/values/strings.xml | 3 + 7 files changed, 254 insertions(+), 7 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index aab6f58..a3f0bb8 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,6 +8,7 @@ + @@ -18,6 +19,7 @@ + diff --git a/app/src/main/java/asgardius/page/s3manager/Downloader.java b/app/src/main/java/asgardius/page/s3manager/Downloader.java index e2f1d01..4f35a32 100644 --- a/app/src/main/java/asgardius/page/s3manager/Downloader.java +++ b/app/src/main/java/asgardius/page/s3manager/Downloader.java @@ -1,14 +1,195 @@ package asgardius.page.s3manager; +import static android.content.ContentValues.TAG; + import androidx.appcompat.app.AppCompatActivity; +import android.app.Activity; +import android.app.DownloadManager; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.net.Uri; import android.os.Bundle; +import android.provider.MediaStore; +import android.util.Log; +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 com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.regions.Region; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.S3ClientOptions; +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.InputStream; +import java.io.OutputStream; public class Downloader extends AppCompatActivity { + String username, password, endpoint, bucket, filekey, filename, prefix, location, fkey; + boolean isfolder; + int progress; + Uri fileuri, folder, uri; + EditText fprefix; + Region region; + S3ClientOptions s3ClientOptions; + AWSCredentials myCredentials; + AmazonS3 s3client; + ProgressBar simpleProgressBar; + File dfile; + Intent intent; + Button fileDownload; + Thread downloadFile; + DownloadManager downloadManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_downloader); + filekey = getIntent().getStringExtra("file_url"); + filename = getIntent().getStringExtra("file_name"); + endpoint = getIntent().getStringExtra("endpoint"); + username = getIntent().getStringExtra("username"); + password = getIntent().getStringExtra("password"); + bucket = getIntent().getStringExtra("bucket"); + location = getIntent().getStringExtra("region"); + simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar); + fileDownload = (Button)findViewById(R.id.filedownload); + region = Region.getRegion(location); + s3ClientOptions = S3ClientOptions.builder().build(); + if (!endpoint.contains(getResources().getString(R.string.aws_endpoint))) { + s3ClientOptions.setPathStyleAccess(true); + } + myCredentials = new BasicAWSCredentials(username, password); + s3client = new AmazonS3Client(myCredentials, region); + s3client.setEndpoint(endpoint); + s3client.setS3ClientOptions(s3ClientOptions); + performFileSearch("Select download location"); + fileDownload.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View view) { + //buttonaction + simpleProgressBar.setVisibility(View.VISIBLE); + fileDownload.setText(getResources().getString(R.string.download_in_progress)); + downloadFile = new Thread(new Runnable() { + + @Override + public void run() { + simpleProgressBar.setVisibility(View.VISIBLE); + try { + //Your code goes here + //s3client.createBucket(bucket, location); + //System.out.println(fkey); + runOnUiThread(new Runnable() { + @Override + public void run() { + //simpleProgressBar.setProgress(100); + simpleProgressBar.setVisibility(View.INVISIBLE); + fileDownload.setText(getResources().getString(R.string.download_success)); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.upload_success), Toast.LENGTH_SHORT).show(); + //simpleProgressBar.setVisibility(View.INVISIBLE); + } + }); + //System.out.println("tree "+treelevel); + //System.out.println("prefix "+prefix); + + } catch (Exception e) { + e.printStackTrace(); + runOnUiThread(new Runnable() { + + @Override + public void run() { + simpleProgressBar.setVisibility(View.INVISIBLE); + fileDownload.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(); + //finish(); + } + } + }); + downloadFile.start(); + } + + }); + } + + private void performFileSearch(String messageTitle) { + //uri = Uri.parse("content://com.android.externalstorage.documents/document/home"); + intent = new Intent(); + intent.setAction(Intent.ACTION_CREATE_DOCUMENT); + //intent.addCategory(Intent.CATEGORY_OPENABLE); + //intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); + //intent.putExtra("android.provider.extra.INITIAL_URI", uri); + intent.putExtra(Intent.EXTRA_TITLE, filename); + intent.setType("*/*"); + ((Activity) this).startActivityForResult(intent, 50); + } + + @Override + public void onActivityResult(int requestCode, int resultCode, final Intent resultData) { + // The ACTION_OPEN_DOCUMENT intent was sent with the request code OPEN_DIRECTORY_REQUEST_CODE. + // If the request code seen here doesn't match, it's the response to some other intent, + // and the below code shouldn't run at all. + super.onActivityResult(requestCode, resultCode, resultData); + if (requestCode == 50) { + if (resultCode == Activity.RESULT_OK) { + // The document selected by the user won't be returned in the intent. + // Instead, a URI to that document will be contained in the return intent + // provided to this method as a parameter. Pull that uri using "resultData.getData()" + if (resultData != null && resultData.getData() != null) { + fileuri = resultData.getData(); + System.out.println(fileuri.toString()); + //System.out.println("File selected successfully"); + //System.out.println("content://com.android.externalstorage.documents"+file.getPath()); + } else { + Toast.makeText(Downloader.this, getResources().getString(R.string.file_path_fail), Toast.LENGTH_SHORT).show(); + finish(); + } + } else { + //System.out.println("User cancelled file browsing {}"); + finish(); + } + } + } + + private File writeContentToFile(Uri uri) throws IOException { + final File file = new File(getCacheDir(), getDisplayName(uri)); + try ( + final InputStream in = new FileInputStream(file); + final OutputStream out = getContentResolver().openOutputStream(uri); + ) { + byte[] buffer = new byte[1024]; + for (int len; (len = in.read(buffer)) != -1; ) { + out.write(buffer, 0, len); + } + return file; + } + } + + private String getDisplayName(Uri uri) { + final String[] projection = { MediaStore.Images.Media.DISPLAY_NAME }; + try ( + Cursor cursor = getContentResolver().query(uri, projection, null, null, null); + ){ + int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME); + if (cursor.moveToFirst()) { + return cursor.getString(columnIndex); + } + } + // If the display name is not found for any reason, use the Uri path as a fallback. + Log.w(TAG, "Couldnt determine DISPLAY_NAME for Uri. Falling back to Uri path: " + uri.getPath()); + return uri.getPath(); } } \ No newline at end of file diff --git a/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java b/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java index 00acfc0..d870345 100644 --- a/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java +++ b/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java @@ -273,7 +273,12 @@ public class ObjectSelect extends AppCompatActivity { public boolean onMenuItemClick(MenuItem menuItem) { // Toast message on menu item clicked //Toast.makeText(MainActivity.this, "You Clicked " + menuItem.getTitle(), Toast.LENGTH_SHORT).show(); - if (menuItem.getTitle() == getResources().getString(R.string.upload_file_here)) { + if (menuItem.getTitle() == getResources().getString(R.string.download_file)) { + Toast.makeText(ObjectSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); + //GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, prefix + Name.get(position).toString()); + //URL objectURL = s3client.generatePresignedUrl(request); + //download(objectURL.toString(), Name.get(position).toString()); + } else if (menuItem.getTitle() == getResources().getString(R.string.upload_file_here)) { //Toast.makeText(ObjectSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); upload(false); } else if (menuItem.getTitle() == getResources().getString(R.string.file_external)) { @@ -470,4 +475,16 @@ public class ObjectSelect extends AppCompatActivity { intent.putExtra("isfolder", isfolder); startActivity(intent); } + + private void download(String url, String filename) { + + Intent intent = new Intent(this, Downloader.class); + intent.putExtra("file_url", url); + intent.putExtra("file_name", filename); + intent.putExtra("endpoint", endpoint); + intent.putExtra("username", username); + intent.putExtra("password", password); + intent.putExtra("bucket", bucket); + startActivity(intent); + } } \ No newline at end of file diff --git a/app/src/main/java/asgardius/page/s3manager/Uploader.java b/app/src/main/java/asgardius/page/s3manager/Uploader.java index 82a5670..25e4cd0 100644 --- a/app/src/main/java/asgardius/page/s3manager/Uploader.java +++ b/app/src/main/java/asgardius/page/s3manager/Uploader.java @@ -43,7 +43,7 @@ import java.util.List; public class Uploader extends AppCompatActivity { String username, password, endpoint, bucket, prefix, location, fkey; - boolean isfolder; + //boolean isfolder; int progress; Uri fileuri, folder, uri; EditText fprefix; @@ -55,6 +55,7 @@ public class Uploader extends AppCompatActivity { long filesize; File ufile; Intent intent; + Button fileUpload; private static final long MAX_SINGLE_PART_UPLOAD_BYTES = 5 * 1024 * 1024; @Override @@ -67,7 +68,7 @@ public class Uploader extends AppCompatActivity { bucket = getIntent().getStringExtra("bucket"); location = getIntent().getStringExtra("region"); prefix = getIntent().getStringExtra("prefix"); - isfolder = getIntent().getBooleanExtra("isfolder", false); + //isfolder = getIntent().getBooleanExtra("isfolder", false); fprefix = (EditText)findViewById(R.id.fprefix); region = Region.getRegion(location); s3ClientOptions = S3ClientOptions.builder().build(); @@ -78,7 +79,7 @@ public class Uploader extends AppCompatActivity { s3client = new AmazonS3Client(myCredentials, region); s3client.setEndpoint(endpoint); s3client.setS3ClientOptions(s3ClientOptions); - Button fileUpload = (Button)findViewById(R.id.fileupload); + fileUpload = (Button)findViewById(R.id.fileupload); simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar); //Toast.makeText(Uploader.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); performFileSearch("Select file to upload"); diff --git a/app/src/main/res/layout/activity_downloader.xml b/app/src/main/res/layout/activity_downloader.xml index e152f71..dba4eb9 100644 --- a/app/src/main/res/layout/activity_downloader.xml +++ b/app/src/main/res/layout/activity_downloader.xml @@ -1,9 +1,47 @@ - - \ No newline at end of file + + + + +