downloader template

This commit is contained in:
Page Asgardius 2022-09-15 12:01:14 -07:00
parent 6db815aa42
commit a994e0c2f3
7 changed files with 254 additions and 7 deletions

View file

@ -8,6 +8,7 @@
<entry key="app/src/main/res/layout/activity_account_add.xml" value="0.25" />
<entry key="app/src/main/res/layout/activity_bucket_select.xml" value="0.2" />
<entry key="app/src/main/res/layout/activity_create_bucket.xml" value="0.20260416666666667" />
<entry key="app/src/main/res/layout/activity_downloader.xml" value="0.2713541666666667" />
<entry key="app/src/main/res/layout/activity_file_share.xml" value="0.19610507246376813" />
<entry key="app/src/main/res/layout/activity_image_viewer.xml" value="0.19610507246376813" />
<entry key="app/src/main/res/layout/activity_list_item.xml" value="0.19610507246376813" />
@ -18,6 +19,7 @@
<entry key="app/src/main/res/layout/activity_video_player.xml" value="0.1" />
<entry key="app/src/main/res/layout/activity_web_view.xml" value="0.17119565217391305" />
<entry key="app/src/main/res/layout/list_buckets.xml" value="0.19610507246376813" />
<entry key="app/src/main/res/menu/account_menu.xml" value="0.2713541666666667" />
<entry key="app/src/main/res/menu/bucket_menu.xml" value="0.20260416666666667" />
<entry key="app/src/main/res/menu/folder_menu.xml" value="0.2713541666666667" />
<entry key="app/src/main/res/menu/object_menu.xml" value="0.20260416666666667" />

View file

@ -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();
}
}

View file

@ -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);
}
}

View file

@ -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");

View file

@ -1,9 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Downloader">
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/download_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="25sp" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="10dp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="20dp">
<Button
android:id="@+id/filedownload"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/download_file"
tools:ignore="MissingConstraints" />
</LinearLayout>
<ProgressBar
android:id="@+id/simpleProgressBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:indeterminate="true"
android:max="100"
android:padding="20dp"
android:progress="50"
android:visibility="invisible"
tools:ignore="MissingConstraints" />
</LinearLayout>

View file

@ -4,6 +4,11 @@
<item
android:id="@+id/upload_file"
android:title="@string/upload_file_here" />
<item
android:id="@+id/download_file"
android:title="@string/download_file" />
<item
android:id="@+id/sharefile"
android:title="@string/file_external" />

View file

@ -14,6 +14,9 @@
<string name="bucket_name">Bucket name</string>
<string name="bucket_name_empty">A bucket name is required</string>
<string name="file_external">Open in</string>
<string name="download_file">Download file</string>
<string name="download_in_progress">Download in progress</string>
<string name="download_success">File downloaded successfully</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="feature_not_supported">Your device is not compatible with this feature</string>