Skip to content

Commit

Permalink
Fix file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
kairusds committed Oct 17, 2024
1 parent 2ca33c2 commit 9893f5d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 34 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ android{
dependencies{
implementation "androidx.appcompat:appcompat:1.6.1"
implementation "org.videolan.android:libvlc-all:3.6.0-eap12"
implementation "org.apache.tika:tika-core:2.9.2"
}
104 changes: 70 additions & 34 deletions app/src/main/java/github/kairusds/libvlcandroidtest/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.view.View;
import android.widget.ArrayAdapter;
Expand All @@ -24,15 +26,22 @@
import static org.videolan.libvlc.MediaPlayer.Event;
import org.videolan.libvlc.util.VLCVideoLayout;

// import java.io.IOException;
import org.apache.tika.Tika;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {
private static final boolean USE_TEXTURE_VIEW = false;
private static final boolean ENABLE_SUBTITLES = false;
private static final int FILE_ACCESS_REQUEST = 1;

private final Handler handler = new Handler(Looper.getMainLooper());
private final ExecutorService executor;

private VLCVideoLayout videoLayout = null;

private LibVLC libVLC = null;
Expand Down Expand Up @@ -71,6 +80,7 @@ protected void onCreate(Bundle savedInstanceState){
play();
}
}else{
executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
showFilePickerDialog(Environment.getExternalStorageDirectory());
}
});
Expand Down Expand Up @@ -150,13 +160,13 @@ private void play(){
mediaPlayer.play();
}

private void setVLCMedia(Uri uri){
private void setVLCMedia(String path){
if(libVLCAvailable()) return;
toast("setVLCMedia");
initVLC();
mediaPlayer.attachViews(videoLayout, null, ENABLE_SUBTITLES, USE_TEXTURE_VIEW);
try{
final Media media = new Media(libVLC, uri);
final Media media = new Media(libVLC, path);
media.setHWDecoderEnabled(true, true); // full hardware decoding
mediaPlayer.setMedia(media);
media.release();
Expand All @@ -181,40 +191,66 @@ private void setVLCMedia(Uri uri){
}

private void showFilePickerDialog(File dir){
File[] files = dir.listFiles();
if(files == null){
toast("Unable to access directory");
return;
}

ArrayList<String> fileList = new ArrayList<>();
fileList.add("..");
for(File file : files){
fileList.add(file.getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.select_dialog_item, fileList);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose a file or directory");
builder.setAdapter(adapter, (dialog, which) -> {
if(which == 0){
File parentDir = dir.getParentFile();
if(parentDir != null){
showFilePickerDialog(parentDir);
}else{
toast("No parent directory");
}
}else{
File selectedFile = files[which - 1];
if(selectedFile.isDirectory()){
showFilePickerDialog(selectedFile);
}else{
setVLCMedia(selectedFile.getAbsolutePath());
executor.execute(() -> {
File[] files = dir.listFiles();
ArrayList<String> fileList = new ArrayList<>();
HashMap<String, String> paths = new HashMap<>();
fileList.add("..");

if(files != null){
Tika tika = new Tika();
for(File file : files){
if(file.isDirectory()){
fileList.add(file.getName());
}else{
String mimetype = tika.detect(file.getName());
if(mimetype.startsWith("video/")){
fileList.add(file.getName());
paths.put(file.getName(), file.getAbsolutePath());
}
}
}
}
dialog.dismiss();

handler.post(() -> {
if(files == null){
toast("Unable to access directory");
executor.shutdown();
executor = null;
return;
}

ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.select_dialog_item, fileList);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Choose a file or directory");
builder.setAdapter(adapter, (dialog, which) -> {
if(which == 0){
File parentDir = dir.getParentFile();
if(parentDir != null){
dialog.dismiss();
showFilePickerDialog(parentDir);
}else{
toast("No parent directory");
}
}else{
String filename = fileList.get(which);
String filepath = paths.get(filename);
File selectedFile = new File(filepath);
if(selectedFile.isDirectory()){
dialog.dismiss();
showFilePickerDialog(selectedFile);
}else{
setVLCMedia(filepath);
dialog.dismiss();
executor.shutdown();
executor = null;
}
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
});
});
builder.setNegativeButton("Cancel", null);
builder.show();
}

@Override
Expand Down

0 comments on commit 9893f5d

Please sign in to comment.