[Android] [PATCH 05/10] Files prefetching in browser

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


---
 .../vlc/gui/browser/BaseBrowserAdapter.java        |  9 ++++++-
 .../vlc/gui/browser/BaseBrowserFragment.java       | 30 +++++++++++++++-------
 .../vlc/gui/browser/FileBrowserFragment.java       |  8 ------
 .../vlc/gui/browser/NetworkBrowserFragment.java    |  4 +++
 4 files changed, 33 insertions(+), 18 deletions(-)

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 97252dd..79bbd8a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -21,6 +21,7 @@
  */
 package org.videolan.vlc.gui.browser;
 
+import android.os.Parcelable;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
 import android.view.LayoutInflater;
@@ -90,7 +91,7 @@ public class BaseBrowserAdapter extends  RecyclerView.Adapter<RecyclerView.ViewH
                 public void onClick(View v) {
                     MediaWrapper mw = (MediaWrapper) getItem(holder.getPosition());
                     if (mw.getType() == MediaWrapper.TYPE_DIR)
-                        fragment.browse(mw);
+                        fragment.browse(mw, holder.getPosition());
                     else
                         Util.openMedia(v.getContext(), mw);
                 }
@@ -171,6 +172,12 @@ public class BaseBrowserAdapter extends  RecyclerView.Adapter<RecyclerView.ViewH
         notifyItemInserted(position);
     }
 
+    public void addAll(ArrayList<MediaWrapper> mediaList){
+        mMediaList.clear();
+        for (MediaWrapper mw : mediaList)
+            mMediaList.add(mw);
+    }
+
     public void removeItem(int position){
         mMediaList.remove(position);
         notifyItemRemoved(position);
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 54b17f7..3decbd5 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
@@ -65,6 +65,7 @@ import org.videolan.vlc.widget.SwipeRefreshLayout;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -91,7 +92,8 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
 
     private static final String FILES_LIST = "files_list";
 
-    private SparseArray<List<MediaWrapper>> mMediaLists;
+    private SparseArray<ArrayList<MediaWrapper>> mMediaLists;
+    private ArrayList<MediaWrapper> mediaList;
     public int mCurrentParsedPosition = 0;
 
     protected abstract Fragment createFragment();
@@ -110,6 +112,9 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
         if (bundle == null)
             bundle = getArguments();
         if (bundle != null){
+            mediaList = bundle.getParcelableArrayList(FILES_LIST);
+            if (mediaList != null)
+                mAdapter.addAll(mediaList);
             mCurrentMedia = bundle.getParcelable(KEY_MEDIA);
             if (mCurrentMedia != null)
                 mMrl = mCurrentMedia.getLocation();
@@ -164,10 +169,13 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
         getActivity().getSupportFragmentManager().popBackStack();
     }
 
-    public void browse (MediaWrapper media){
+    public void browse (MediaWrapper media, int position){
         FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
         Fragment next = createFragment();
         Bundle args = new Bundle();
+        ArrayList<MediaWrapper> list = mMediaLists != null ? mMediaLists.get(position) : null;
+        if(list != null && !list.isEmpty())
+            args.putParcelableArrayList(FILES_LIST, list);
         args.putParcelable(KEY_MEDIA, media);
         next.setArguments(args);
         ft.replace(R.id.fragment_placeholder, next, media.getLocation());
@@ -243,8 +251,12 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
     protected void updateDisplay(){
         if (mMediaBrowser == null)
             mMediaBrowser = new MediaBrowser(mLibVLC, this);
-        if (mAdapter.isEmpty())
+        if (mAdapter.isEmpty()) {
             refresh();
+        } else {
+            mAdapter.notifyDataSetChanged();
+            parseSubDirectories();
+        }
     }
 
     @Override
@@ -390,18 +402,18 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
         MediaLibrary.getInstance().loadMediaItems();
     }
 
-    private void parseSubDirectories() {
+    protected void parseSubDirectories() {
         if (mAdapter.isEmpty())
             return;
-        mMediaLists = new SparseArray<List<MediaWrapper>>();
+        mMediaLists = new SparseArray<ArrayList<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>();
+        ArrayList<MediaWrapper> directories = new ArrayList<MediaWrapper>();
+        ArrayList<MediaWrapper> files = new ArrayList<MediaWrapper>();
 
         @Override
         public void onMediaAdded(int index, Media media) {
@@ -436,8 +448,8 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
 
             if (mCurrentParsedPosition < mAdapter.getItemCount()) {
                 mMediaBrowser.browse(((MediaWrapper) mAdapter.getItem(mCurrentParsedPosition)).getLocation());
-                directories = new LinkedList<MediaWrapper>();
-                files = new LinkedList<MediaWrapper>();
+                directories = new ArrayList<MediaWrapper>();
+                files = new ArrayList<MediaWrapper>();
             } else
                 mMediaBrowser.release();
         }
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 822ea92..38b86e2 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -30,23 +30,16 @@ import android.os.Bundle;
 import android.os.Environment;
 import android.support.v4.app.Fragment;
 import android.support.v7.internal.widget.AdapterViewCompat;
-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 {
 
@@ -59,7 +52,6 @@ public class FileBrowserFragment extends BaseBrowserFragment {
     public void onCreate(Bundle bundle) {
         super.onCreate(bundle);
         mRoot = mMrl == null;
-        //TODO get already parsed list
     }
 
     @Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java
index e5e3dea..45bf69d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java
@@ -81,6 +81,10 @@ public class NetworkBrowserFragment extends BaseBrowserFragment {
             refresh();
         else if (mRoot)
             updateFavorites();
+        else {
+            mAdapter.notifyDataSetChanged();
+            parseSubDirectories();
+        }
     }
 
     @Override
-- 
2.1.0



More information about the Android mailing list