diff --git a/app/build.gradle b/app/build.gradle index c8c7fac..6661ce1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" } diff --git a/app/src/main/java/github/kairusds/libvlcandroidtest/MainActivity.java b/app/src/main/java/github/kairusds/libvlcandroidtest/MainActivity.java index f6b5af7..19bfcf9 100644 --- a/app/src/main/java/github/kairusds/libvlcandroidtest/MainActivity.java +++ b/app/src/main/java/github/kairusds/libvlcandroidtest/MainActivity.java @@ -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; @@ -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; @@ -71,6 +80,7 @@ protected void onCreate(Bundle savedInstanceState){ play(); } }else{ + executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2); showFilePickerDialog(Environment.getExternalStorageDirectory()); } }); @@ -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(); @@ -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 fileList = new ArrayList<>(); - fileList.add(".."); - for(File file : files){ - fileList.add(file.getName()); - } - ArrayAdapter 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 fileList = new ArrayList<>(); + HashMap 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 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