[Android] [PATCH 04/10] Scan directories to display their content

Geoffrey Métais geoffrey.metais at gmail.com
Mon Apr 20 10:49:50 CEST 2015


---
 vlc-android/res/values/strings.xml                 |  1 +
 .../vlc/gui/browser/BaseBrowserAdapter.java        |  7 +-
 .../vlc/gui/browser/BaseBrowserFragment.java       | 91 +++++++++++++++++++++-
 .../vlc/gui/browser/FileBrowserFragment.java       | 11 ++-
 4 files changed, 103 insertions(+), 7 deletions(-)

diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index f509e13..4399e0b 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -338,6 +338,7 @@
     <string name="confirm_resume">Resume from last position?</string>
     <string name="confirm_resume_title">Ask confirmation to resume</string>
     <string name="confirm_resume_summary">if activated you will be asked to confirm when a video can be resumed from last position</string>
+    <string name="directory_empty">Directory is empty</string>
 
     <!--Accessibility-->
     <string name="more_actions">More Actions</string>
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
index 03df2d5..97252dd 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -22,6 +22,7 @@
 package org.videolan.vlc.gui.browser;
 
 import android.support.v7.widget.RecyclerView;
+import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -77,7 +78,11 @@ public class BaseBrowserAdapter extends  RecyclerView.Adapter<RecyclerView.ViewH
                   media.getType() == MediaWrapper.TYPE_VIDEO ||
                   (media.getType() == MediaWrapper.TYPE_DIR && Util.canWrite(media.getLocation())));
             vh.title.setText(media.getTitle());
-            vh.text.setVisibility(View.GONE);
+            if (media.getDescription() != null && !TextUtils.isEmpty(media.getDescription())) {
+                vh.text.setVisibility(View.VISIBLE);
+                vh.text.setText(media.getDescription());
+            } else
+                vh.text.setVisibility(View.INVISIBLE);
             vh.icon.setImageResource(getIconResId(media));
             vh.more.setVisibility(hasContextMenu ? View.VISIBLE : View.GONE);
             vh.itemView.setOnClickListener(new View.OnClickListener() {
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
index 9a9840f..54b17f7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
@@ -32,6 +32,8 @@ import android.support.v4.app.FragmentTransaction;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.PopupMenu;
 import android.support.v7.widget.RecyclerView;
+import android.text.TextUtils;
+import android.util.SparseArray;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
@@ -46,6 +48,7 @@ import org.videolan.libvlc.util.MediaBrowser;
 import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.R;
+import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.audio.AudioServiceController;
 import org.videolan.vlc.gui.CommonDialogs;
 import org.videolan.vlc.gui.DividerItemDecoration;
@@ -62,6 +65,8 @@ import org.videolan.vlc.widget.SwipeRefreshLayout;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
 
 
 public abstract class BaseBrowserFragment extends MediaBrowserFragment implements IRefreshable, MediaBrowser.EventListener, SwipeRefreshLayout.OnRefreshListener {
@@ -84,6 +89,11 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
     protected boolean mRoot;
     protected LibVLC mLibVLC;
 
+    private static final String FILES_LIST = "files_list";
+
+    private SparseArray<List<MediaWrapper>> mMediaLists;
+    public int mCurrentParsedPosition = 0;
+
     protected abstract Fragment createFragment();
     protected abstract void browseRoot();
     protected abstract String getCategoryTitle();
@@ -187,6 +197,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
             mLayoutManager.scrollToPositionWithOffset(mSavedPosition, 0);
             mSavedPosition = 0;
         }
+        parseSubDirectories();
         focusHelper(mAdapter.isEmpty());
     }
 
@@ -217,7 +228,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
      */
     protected boolean updateEmptyView(){
         if (mAdapter.isEmpty()){
-            mEmptyView.setText("Directory empty");
+            mEmptyView.setText(getString(R.string.directory_empty));
             mEmptyView.setVisibility(View.VISIBLE);
             mRecyclerView.setVisibility(View.GONE);
             mSwipeRefreshLayout.setEnabled(false);
@@ -239,10 +250,13 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
     @Override
     public void refresh() {
         mAdapter.clear();
-        if (mRoot)
+        if (mRoot) {
             browseRoot();
-        else
+            parseSubDirectories();
+        } else {
+            mMediaBrowser.changeEventListener(this);
             mMediaBrowser.browse(mMrl);
+        }
         mHandler.sendEmptyMessageDelayed(BrowserFragmentHandler.MSG_SHOW_LOADING, 300);
     }
 
@@ -375,4 +389,75 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
             ft.commit();
         MediaLibrary.getInstance().loadMediaItems();
     }
+
+    private void parseSubDirectories() {
+        if (mAdapter.isEmpty())
+            return;
+        mMediaLists = new SparseArray<List<MediaWrapper>>();
+        mMediaBrowser.changeEventListener(mFoldersBrowserListener);
+        mCurrentParsedPosition = 0;
+        mMediaBrowser.browse(((MediaWrapper) mAdapter.getItem(0)).getLocation()); //TODO manage non MW
+    }
+
+    private MediaBrowser.EventListener mFoldersBrowserListener = new MediaBrowser.EventListener(){
+        LinkedList<MediaWrapper> directories = new LinkedList<MediaWrapper>();
+        LinkedList<MediaWrapper> files = new LinkedList<MediaWrapper>();
+
+        @Override
+        public void onMediaAdded(int index, Media media) {
+            int type = media.getType();
+            if (type == Media.Type.Directory)
+                directories.add(new MediaWrapper(media));
+            else if (type == Media.Type.File)
+                files.add(new MediaWrapper(media));
+        }
+
+        @Override
+        public void onMediaRemoved(int index, Media media) {}
+
+        @Override
+        public void onBrowseEnd() {
+            String holderText = getDescription(directories.size(), files.size());
+            MediaWrapper mw = (MediaWrapper) mAdapter.getItem(mCurrentParsedPosition);
+
+            if (!TextUtils.equals(holderText, "")) {
+                mw.setDescription(holderText);
+                mAdapter.notifyItemChanged(mCurrentParsedPosition);
+                directories.addAll(files);
+                mMediaLists.append(mCurrentParsedPosition, directories);
+            }
+            while (++mCurrentParsedPosition < mAdapter.getItemCount()-1){ //skip media that are not browsable
+                if (mAdapter.getItem(mCurrentParsedPosition) instanceof MediaWrapper) {
+                    mw = (MediaWrapper) mAdapter.getItem(mCurrentParsedPosition);
+                    if (mw.getType() == MediaWrapper.TYPE_DIR || mw.getType() == MediaWrapper.TYPE_PLAYLIST)
+                        break;
+                }
+            }
+
+            if (mCurrentParsedPosition < mAdapter.getItemCount()) {
+                mMediaBrowser.browse(((MediaWrapper) mAdapter.getItem(mCurrentParsedPosition)).getLocation());
+                directories = new LinkedList<MediaWrapper>();
+                files = new LinkedList<MediaWrapper>();
+            } else
+                mMediaBrowser.release();
+        }
+
+        private String getDescription(int folderCount, int mediaFileCount) {
+            String holderText = "";
+            if (folderCount > 0) {
+                holderText += VLCApplication.getAppResources().getQuantityString(
+                        R.plurals.subfolders_quantity, folderCount, folderCount
+                );
+                if (mediaFileCount > 0)
+                    holderText += ", ";
+            }
+            if (mediaFileCount > 0)
+                holderText += VLCApplication.getAppResources().getQuantityString(
+                        R.plurals.mediafiles_quantity, mediaFileCount,
+                        mediaFileCount);
+            else if (folderCount == 0 && mediaFileCount == 0)
+                holderText = getString(R.string.directory_empty);
+            return holderText;
+        }
+    };
 }
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 c833922..822ea92 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -30,17 +30,23 @@ import android.os.Bundle;
 import android.os.Environment;
 import android.support.v4.app.Fragment;
 import android.support.v7.internal.widget.AdapterViewCompat;
-import android.util.Log;
+import android.text.TextUtils;
+import android.util.SparseArray;
 import android.view.ContextMenu;
 import android.view.MenuInflater;
 import android.view.View;
 
+import org.videolan.libvlc.Media;
+import org.videolan.libvlc.util.MediaBrowser;
 import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.R;
+import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Util;
 
 import java.io.File;
+import java.util.LinkedList;
+import java.util.List;
 
 public class FileBrowserFragment extends BaseBrowserFragment {
 
@@ -53,7 +59,7 @@ public class FileBrowserFragment extends BaseBrowserFragment {
     public void onCreate(Bundle bundle) {
         super.onCreate(bundle);
         mRoot = mMrl == null;
-        Log.d(TAG, "file root ? "+mRoot);
+        //TODO get already parsed list
     }
 
     @Override
@@ -86,7 +92,6 @@ public class FileBrowserFragment extends BaseBrowserFragment {
 
     @Override
     protected void browseRoot() {
-        Log.d(TAG, "file browse root");
         String storages[] = AndroidDevices.getMediaDirectories();
         MediaWrapper mw;
         for (String storage : storages) {
-- 
2.1.0



More information about the Android mailing list