Folder navigation (#2)

This commit is contained in:
Page Asgardius 2022-09-03 19:09:26 -07:00
commit feec49a755
4 changed files with 112 additions and 16 deletions

View file

@ -28,7 +28,8 @@ public class BucketSelect extends AppCompatActivity {
ArrayList Name; ArrayList Name;
ArrayList Img; ArrayList Img;
RecyclerView recyclerView; RecyclerView recyclerView;
String username, password, endpoint; String username, password, endpoint, prefix;
int treelevel;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -36,6 +37,7 @@ public class BucketSelect extends AppCompatActivity {
endpoint = getIntent().getStringExtra("endpoint"); endpoint = getIntent().getStringExtra("endpoint");
username = getIntent().getStringExtra("username"); username = getIntent().getStringExtra("username");
password = getIntent().getStringExtra("password"); password = getIntent().getStringExtra("password");
prefix = "";
setContentView(R.layout.activity_bucket_select); setContentView(R.layout.activity_bucket_select);
Region region = Region.getRegion(US_EAST_1); Region region = Region.getRegion(US_EAST_1);
S3ClientOptions s3ClientOptions = S3ClientOptions.builder().build(); S3ClientOptions s3ClientOptions = S3ClientOptions.builder().build();
@ -118,10 +120,13 @@ public class BucketSelect extends AppCompatActivity {
private void explorer(String bucket) { private void explorer(String bucket) {
Intent intent = new Intent(this, ObjectSelect.class); Intent intent = new Intent(this, ObjectSelect.class);
treelevel = 0;
intent.putExtra("endpoint", endpoint); intent.putExtra("endpoint", endpoint);
intent.putExtra("username", username); intent.putExtra("username", username);
intent.putExtra("password", password); intent.putExtra("password", password);
intent.putExtra("bucket", bucket); intent.putExtra("bucket", bucket);
intent.putExtra("prefix", prefix);
intent.putExtra("treelevel", treelevel);
startActivity(intent); startActivity(intent);
} }

View file

@ -19,6 +19,8 @@ import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions; import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.Bucket; import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ListObjectsV2Request;
import com.amazonaws.services.s3.model.ListObjectsV2Result; import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3Object;
@ -26,14 +28,18 @@ import com.amazonaws.services.s3.model.S3ObjectSummary;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
public class ObjectSelect extends AppCompatActivity { public class ObjectSelect extends AppCompatActivity {
ArrayList Name; ArrayList Name;
ArrayList Img; ArrayList Img;
//ArrayList object;
RecyclerView recyclerView; RecyclerView recyclerView;
String username, password, endpoint, bucket; String username, password, endpoint, bucket, prefix;
int treelevel;
String[] filename, path;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -42,6 +48,8 @@ public class ObjectSelect extends AppCompatActivity {
username = getIntent().getStringExtra("username"); username = getIntent().getStringExtra("username");
password = getIntent().getStringExtra("password"); password = getIntent().getStringExtra("password");
bucket = getIntent().getStringExtra("bucket"); bucket = getIntent().getStringExtra("bucket");
prefix = getIntent().getStringExtra("prefix");
treelevel = getIntent().getIntExtra("treelevel", 0);
setContentView(R.layout.activity_object_select); setContentView(R.layout.activity_object_select);
Region region = Region.getRegion(US_EAST_1); Region region = Region.getRegion(US_EAST_1);
S3ClientOptions s3ClientOptions = S3ClientOptions.builder().build(); S3ClientOptions s3ClientOptions = S3ClientOptions.builder().build();
@ -62,28 +70,66 @@ public class ObjectSelect extends AppCompatActivity {
public void run() { public void run() {
try { try {
//Your code goes here //Your code goes here
List<Bucket> buckets = s3client.listBuckets(); //List<Bucket> buckets = s3client.listBuckets();
ListObjectsRequest orequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(prefix).withMaxKeys(2000);
//List<S3Object> objects = (List<S3Object>) s3client.listObjects(bucket, "/"); //List<S3Object> objects = (List<S3Object>) s3client.listObjects(bucket, "/");
ListObjectsV2Result result = s3client.listObjectsV2(bucket, "/"); ObjectListing result = s3client.listObjects(orequest);
//System.out.println(objects); //System.out.println(objects);
//This convert bucket list to an array list //This convert bucket list to an array list
Name = new ArrayList<String>();
Img = new ArrayList<String>(); Img = new ArrayList<String>();
LinkedHashSet<String> object = new LinkedHashSet<String>();
// Print bucket names // Print bucket names
//System.out.println("Buckets:"); //System.out.println("Buckets:");
int i=0; //int i=0;
List<S3ObjectSummary> objects = result.getObjectSummaries(); List<S3ObjectSummary> objects = result.getObjectSummaries();
boolean nextbatch = false;
while (result.isTruncated() || !nextbatch) {
if (nextbatch) {
result = s3client.listNextBatchOfObjects (result);
objects = result.getObjectSummaries();
} else {
nextbatch = true;
}
for (S3ObjectSummary os : objects) { for (S3ObjectSummary os : objects) {
Name.add(os.getKey()); filename = os.getKey().split("/");
if (os.getKey().endsWith(".opus") || os.getKey().endsWith(".ogg") if (filename.length == treelevel+1) {
|| os.getKey().endsWith(".oga") || os.getKey().endsWith(".mp3") object.add(filename[treelevel]);
|| os.getKey().endsWith(".m4a")) {
Img.add(R.drawable.audiofile);
} }
else { else {
object.add(filename[treelevel]+"/");
}
//i++;
}
}
Name = new ArrayList<String>(object);
object.clear();
//Img.add(R.drawable.unknownfile);
int i = 0;
while(i<Name.size()) {
//Img.add(R.drawable.unknownfile);
if (Name.get(i).toString().endsWith("/")) {
Img.add(R.drawable.folder);
}
else if (Name.get(i).toString().endsWith(".opus") || Name.get(i).toString().endsWith(".ogg")
|| Name.get(i).toString().endsWith(".oga") || Name.get(i).toString().endsWith(".mp3")
|| Name.get(i).toString().endsWith(".m4a") || Name.get(i).toString().endsWith(".flac")
|| Name.get(i).toString().endsWith(".mka")) {
Img.add(R.drawable.audiofile);
}
else if(Name.get(i).toString().endsWith(".mp4") || Name.get(i).toString().endsWith(".mkv")
|| Name.get(i).toString().endsWith(".webm") || Name.get(i).toString().endsWith(".m4v")) {
Img.add(R.drawable.videofile); Img.add(R.drawable.videofile);
} }
else {
Img.add(R.drawable.unknownfile);
} }
i++;
}
/*for (Bucket bucket : buckets) { /*for (Bucket bucket : buckets) {
//i++; //i++;
@ -105,6 +151,8 @@ public class ObjectSelect extends AppCompatActivity {
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
} }
}); });
//System.out.println("tree "+treelevel);
//System.out.println("prefix "+prefix);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -124,12 +172,19 @@ public class ObjectSelect extends AppCompatActivity {
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() { recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
@Override @Override
public void onClick(View view, int position) { public void onClick(View view, int position) {
System.out.println("Click on "+Name.get(position).toString()); //System.out.println("Click on "+Name.get(position).toString());
//explorer(Name.get(position).toString()); //explorer(Name.get(position).toString());
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, Name.get(position).toString()); if (Img.get(position).equals(R.drawable.folder)) {
//go to subfolder
explorer(Name.get(position).toString());
}
else {
//load media file
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, prefix + Name.get(position).toString());
URL objectURL = s3client.generatePresignedUrl(request); URL objectURL = s3client.generatePresignedUrl(request);
videoplayer(objectURL.toString()); videoplayer(objectURL.toString());
} }
}
@Override @Override
public void onLongClick(View view, int position) { public void onLongClick(View view, int position) {
@ -145,4 +200,40 @@ public class ObjectSelect extends AppCompatActivity {
startActivity(intent); startActivity(intent);
} }
private void explorer(String object) {
Intent intent = new Intent(this, ObjectSelect.class);
//treelevel ++;
intent.putExtra("endpoint", endpoint);
intent.putExtra("username", username);
intent.putExtra("password", password);
intent.putExtra("bucket", bucket);
intent.putExtra("prefix", prefix + object);
intent.putExtra("treelevel", treelevel+1);
startActivity(intent);
}
public void onBackPressed() {
if (treelevel >= 2) {
path = prefix.split("/");
prefix = "";
int i = 0;
//System.out.println("path "+i);
while(i <= path.length-2) {
prefix = prefix.concat(path[i]);
prefix = prefix.concat("/");
//System.out.println("position "+i);
i++;
}
}
else if (treelevel == 1) {
prefix = "";
}
treelevel --;
//System.out.println("tree "+treelevel);
//System.out.println("prefix "+prefix);
finish();
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB