[Android] File Picker provider

Geoffrey Métais git at videolan.org
Wed Apr 4 18:34:50 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Mar  2 10:41:03 2018 +0100| [79c2808646b2d3260f6bad9081e2c23db763bb3c] | committer: Geoffrey Métais

File Picker provider

> https://code.videolan.org/videolan/vlc-android/commit/79c2808646b2d3260f6bad9081e2c23db763bb3c
---

 vlc-android/res/layout/directory_browser.xml       |  1 -
 vlc-android/res/layout/file_picker_activity.xml    |  3 +-
 .../vlc/gui/browser/FileBrowserFragment.java       |  2 -
 .../vlc/gui/browser/FilePickerAdapter.java         |  2 +-
 .../vlc/gui/browser/FilePickerFragment.java        | 48 +++++++++-------------
 .../org/videolan/vlc/viewmodels/BrowserProvider.kt |  2 +-
 .../videolan/vlc/viewmodels/FilePickerProvider.kt  | 24 +++++++++++
 7 files changed, 47 insertions(+), 35 deletions(-)

diff --git a/vlc-android/res/layout/directory_browser.xml b/vlc-android/res/layout/directory_browser.xml
index f8e0c8112..434003a9a 100644
--- a/vlc-android/res/layout/directory_browser.xml
+++ b/vlc-android/res/layout/directory_browser.xml
@@ -68,7 +68,6 @@
                 android:gravity="center"
                 android:text="@string/loading" />
 
-            <include layout="@layout/shadow_top"/>
         </android.support.constraint.ConstraintLayout></LinearLayout>
     </org.videolan.vlc.gui.view.SwipeRefreshLayout>
 </layout>
diff --git a/vlc-android/res/layout/file_picker_activity.xml b/vlc-android/res/layout/file_picker_activity.xml
index 9d02cfb49..c8bace40c 100644
--- a/vlc-android/res/layout/file_picker_activity.xml
+++ b/vlc-android/res/layout/file_picker_activity.xml
@@ -2,7 +2,8 @@
 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
-    android:orientation="vertical" android:layout_width="@dimen/file_picker_width"
+    android:orientation="vertical"
+    android:layout_width="@dimen/default_content_width"
     android:layout_height="@dimen/file_picker_height">
     <ImageView
         android:id="@+id/button_home"
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
index 1e05eac89..243659a5f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -51,13 +51,11 @@ import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.CustomDirectories;
 import org.videolan.vlc.util.FileUtils;
 import org.videolan.vlc.util.Strings;
-import org.videolan.vlc.util.Util;
 import org.videolan.vlc.viewmodels.FileBrowserProvider;
 
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.List;
 
 public class FileBrowserFragment extends BaseBrowserFragment {
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerAdapter.java
index 62777b473..933f1a485 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerAdapter.java
@@ -27,7 +27,7 @@ import org.videolan.medialibrary.media.MediaWrapper;
 
 public class FilePickerAdapter extends BaseBrowserAdapter {
 
-    public FilePickerAdapter(BaseBrowserFragment fragment) {
+    FilePickerAdapter(BaseBrowserFragment fragment) {
         super(fragment);
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerFragment.java
index 8abe85546..77d2bdf11 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FilePickerFragment.java
@@ -24,6 +24,7 @@
 package org.videolan.vlc.gui.browser;
 
 import android.app.Activity;
+import android.arch.lifecycle.ViewModelProviders;
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;
@@ -38,6 +39,7 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.FileUtils;
 import org.videolan.vlc.util.Strings;
+import org.videolan.vlc.viewmodels.FilePickerProvider;
 
 public class FilePickerFragment extends FileBrowserFragment {
 
@@ -56,22 +58,21 @@ public class FilePickerFragment extends FileBrowserFragment {
 
     @Override
     public void onCreate(Bundle bundle) {
-        if (getActivity().getIntent() != null) {
-            Uri uri = getActivity().getIntent().getData();
+        final Activity activity = getActivity();
+        if (activity != null && activity.getIntent() != null) {
+            final Uri uri = getActivity().getIntent().getData();
             if (uri == null || TextUtils.equals(uri.getScheme(), "http")) {
-                getActivity().setIntent(null);
+                activity.setIntent(null);
             }
         }
         super.onCreate(bundle);
         mAdapter = new FilePickerAdapter(this);
         mRoot = defineIsRoot();
-//        runOnBrowserThread(new Runnable() {
-//            @Override
-//            public void run() {
-//                initMediaBrowser(FilePickerFragment.this);
-//                mMediaBrowser.setIgnoreFileTypes("db,nfo,ini,jpg,jpeg,ljpg,gif,png,pgm,pgmyuv,pbm,pam,tga,bmp,pnm,xpm,xcf,pcx,tif,tiff,lbm,sfv");
-//            }
-//        });
+    }
+
+    @Override
+    protected void setupBrowser() {
+        browser = ViewModelProviders.of(this, new FilePickerProvider.Factory(mMrl)).get(FilePickerProvider.class);
     }
 
     @Override
@@ -89,10 +90,8 @@ public class FilePickerFragment extends FileBrowserFragment {
 
     public void onClick(View v, int position, MediaLibraryItem item) {
         final MediaWrapper media = (MediaWrapper) item;
-        if (media.getType() == MediaWrapper.TYPE_DIR)
-            browse(media, true);
-        else
-            pickFile(media);
+        if (media.getType() == MediaWrapper.TYPE_DIR) browse(media, true);
+        else pickFile(media);
 
     }
     void pickFile(MediaWrapper mw){
@@ -103,38 +102,29 @@ public class FilePickerFragment extends FileBrowserFragment {
     }
 
     public void browseUp() {
-        if (mRoot)
-            getActivity().finish();
+        if (mRoot) getActivity().finish();
         else if (TextUtils.equals(Strings.removeFileProtocole(mMrl), AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY)) {
             mMrl = null;
             mRoot = true;
-            mAdapter.clear();
-            browseRoot();
+            browser.browseRoot();
         } else if (mMrl != null) {
-            MediaWrapper mw = new MediaWrapper(Uri.parse(FileUtils.getParent(mMrl)));
+            final MediaWrapper mw = new MediaWrapper(Uri.parse(FileUtils.getParent(mMrl)));
             browse(mw, false);
         }
     }
 
     protected boolean defineIsRoot() {
-        if (mMrl == null)
-            return true;
+        if (mMrl == null) return true;
         if (mMrl.startsWith("file")) {
-            String path = Strings.removeFileProtocole(mMrl);
+            final String path = Strings.removeFileProtocole(mMrl);
             for (String directory : rootDirectories) {
                 if (path.startsWith(directory))
                     return false;
             }
             return true;
-        } else
-            return mMrl.length() < 7;
+        } else return mMrl.length() < 7;
     }
 
-//    @Override
-//    protected int getLayoutId(){
-//        return R.layout.file_picker_fragment;
-//    }
-
     @Override
     protected int getBrowserFlags() {
         return MediaBrowser.Flag.Interact|MediaBrowser.Flag.NoSlavesAutodetect;
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/viewmodels/BrowserProvider.kt
index d6ae78788..caedca882 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/BrowserProvider.kt
@@ -35,7 +35,7 @@ abstract class BrowserProvider(val url: String?) : BaseModel<MediaLibraryItem>()
     protected val browserContext by lazy { HandlerContext(browserHandler, "browser-context") }
     internal val medialibrary = Medialibrary.getInstance()
 
-    protected fun initBrowser(listener: EventListener = this) {
+    protected open fun initBrowser(listener: EventListener = this) {
         if (mediabrowser === null) mediabrowser = MediaBrowser(VLCInstance.get(), listener, browserHandler)
         else mediabrowser?.changeEventListener(listener)
     }
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/FilePickerProvider.kt b/vlc-android/src/org/videolan/vlc/viewmodels/FilePickerProvider.kt
new file mode 100644
index 000000000..4de5d334d
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/FilePickerProvider.kt
@@ -0,0 +1,24 @@
+package org.videolan.vlc.viewmodels
+
+import android.arch.lifecycle.ViewModel
+import android.arch.lifecycle.ViewModelProvider
+import org.videolan.libvlc.util.MediaBrowser
+
+
+class FilePickerProvider(url: String?) : FileBrowserProvider(url, true) {
+
+    override fun getFlags(): Int {
+        return MediaBrowser.Flag.Interact or MediaBrowser.Flag.NoSlavesAutodetect
+    }
+
+    override fun initBrowser(listener: MediaBrowser.EventListener) {
+        super.initBrowser(listener)
+        mediabrowser?.setIgnoreFileTypes("db,nfo,ini,jpg,jpeg,ljpg,gif,png,pgm,pgmyuv,pbm,pam,tga,bmp,pnm,xpm,xcf,pcx,tif,tiff,lbm,sfv");
+    }
+    class Factory(val url: String?): ViewModelProvider.NewInstanceFactory() {
+        override fun <T : ViewModel> create(modelClass: Class<T>): T {
+            @Suppress("UNCHECKED_CAST")
+            return FilePickerProvider(url) as T
+        }
+    }
+}
\ No newline at end of file



More information about the Android mailing list