[Android] [PATCH] Queue audio lists updates

Geoffrey Métais geoffrey.metais at gmail.com
Sat Dec 27 15:24:18 CET 2014


Queue Artists/Albums/Songs/Genres lists refresh, and execute the currently diplayed list refresh in first
This should speed up AudioBrowser display
---
 .../vlc/gui/audio/AudioBrowserFragment.java        | 107 ++++++++++++++++-----
 1 file changed, 82 insertions(+), 25 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
index 42948df..9c0d5b0 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -68,6 +68,8 @@ import org.videolan.vlc.widget.HeaderScrollView;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 public class AudioBrowserFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, IBrowser {
     public final static String TAG = "VLC/AudioBrowserFragment";
@@ -80,10 +82,12 @@ public class AudioBrowserFragment extends Fragment implements SwipeRefreshLayout
     private AudioServiceController mAudioController;
     private MediaLibrary mMediaLibrary;
 
-    private AudioBrowserListAdapter mSongsAdapter;
+    List<Media> mAudioList;
     private AudioBrowserListAdapter mArtistsAdapter;
     private AudioBrowserListAdapter mAlbumsAdapter;
+    private AudioBrowserListAdapter mSongsAdapter;
     private AudioBrowserListAdapter mGenresAdapter;
+    private ArrayList<AudioBrowserListAdapter> mAdaptersToNotify = new ArrayList<AudioBrowserListAdapter>();
 
     private View mEmptyView;
 
@@ -482,16 +486,27 @@ public class AudioBrowserFragment extends Fragment implements SwipeRefreshLayout
     }
 
     @Override
-    public void display() {
+    public void display(){
+        display(null);
+    }
+
+    public void display(final List<AudioBrowserListAdapter> adapters) {
         mReadyToDisplay = true;
         if (getActivity() != null)
             getActivity().runOnUiThread(new Runnable() {
                 @Override
                 public void run() {
-                    mArtistsAdapter.notifyDataSetChanged();
-                    mSongsAdapter.notifyDataSetChanged();
-                    mAlbumsAdapter.notifyDataSetChanged();
-                    mGenresAdapter.notifyDataSetChanged();
+                    if (adapters == null) {
+                        mArtistsAdapter.notifyDataSetChanged();
+                        mSongsAdapter.notifyDataSetChanged();
+                        mAlbumsAdapter.notifyDataSetChanged();
+                        mGenresAdapter.notifyDataSetChanged();
+                    } else
+                    synchronized (mAdaptersToNotify) {
+                        for (AudioBrowserListAdapter adapter : adapters)
+                            adapter.notifyDataSetChanged();
+                        adapters.clear();
+                    }
 
                     // Refresh the fast scroll data, since SectionIndexer doesn't respect notifyDataSetChanged
                     int[] lists = {R.id.artists_list, R.id.albums_list, R.id.songs_list, R.id.genres_list};
@@ -536,8 +551,8 @@ public class AudioBrowserFragment extends Fragment implements SwipeRefreshLayout
         mArtistsAdapter.clear();
         mAlbumsAdapter.clear();
         mGenresAdapter.clear();
-        final List<Media> audioList = MediaLibrary.getInstance().getAudioItems();
-        if (audioList.isEmpty()){
+        mAudioList = MediaLibrary.getInstance().getAudioItems();
+        if (mAudioList.isEmpty()){
             mSwipeRefreshLayout.setRefreshing(false);
             mEmptyView.setVisibility(View.VISIBLE);
             focusHelper(true, R.id.artists_list);
@@ -545,28 +560,70 @@ public class AudioBrowserFragment extends Fragment implements SwipeRefreshLayout
             mEmptyView.setVisibility(View.GONE);
             mHandler.sendEmptyMessageDelayed(MSG_LOADING, 300);
 
-            new Thread(new Runnable() {
-                @Override
-                public void run() {
-
-                    Collections.sort(audioList, MediaComparators.byArtist);
-                    mArtistsAdapter.addAll(audioList, AudioBrowserListAdapter.TYPE_ARTISTS);
+            ExecutorService tpe = Executors.newSingleThreadExecutor();
+            ArrayList<Runnable> tasks = new ArrayList<Runnable>();
+            tasks.add(updateArtists);
+            tasks.add(updateAlbums);
+            tasks.add(updateSongs);
+            tasks.add(updateGenres);
+            //process the visible list first
+            tasks.add(0, tasks.remove(mFlingViewPosition));
+            for (Runnable task : tasks)
+                tpe.submit(task);
+        }
+    }
 
-                    Collections.sort(audioList, MediaComparators.byName);
-                    mSongsAdapter.addAll(audioList, AudioBrowserListAdapter.TYPE_SONGS);
+    Runnable updateArtists = new Runnable() {
+        @Override
+        public void run() {
+            Collections.sort(mAudioList, MediaComparators.byArtist);
+            mArtistsAdapter.addAll(mAudioList, AudioBrowserListAdapter.TYPE_ARTISTS);
+            synchronized (mAdaptersToNotify) {
+                mAdaptersToNotify.add(mArtistsAdapter);
+            }
+            if (mReadyToDisplay)
+                display(mAdaptersToNotify);
+        }
+    };
 
-                    Collections.sort(audioList, MediaComparators.byAlbum);
-                    mAlbumsAdapter.addAll(audioList, AudioBrowserListAdapter.TYPE_ALBUMS);
+    Runnable updateAlbums = new Runnable() {
+        @Override
+        public void run() {
+            Collections.sort(mAudioList, MediaComparators.byAlbum);
+            mAlbumsAdapter.addAll(mAudioList, AudioBrowserListAdapter.TYPE_ALBUMS);
+            synchronized (mAdaptersToNotify) {
+                mAdaptersToNotify.add(mAlbumsAdapter);
+            }
+            if (mReadyToDisplay)
+                display(mAdaptersToNotify);
+        }
+    };
 
-                    Collections.sort(audioList, MediaComparators.byGenre);
-                    mGenresAdapter.addAll(audioList, AudioBrowserListAdapter.TYPE_GENRES);
+    Runnable updateSongs = new Runnable() {
+        @Override
+        public void run() {
+            Collections.sort(mAudioList, MediaComparators.byName);
+            mSongsAdapter.addAll(mAudioList, AudioBrowserListAdapter.TYPE_SONGS);
+            synchronized (mAdaptersToNotify) {
+                mAdaptersToNotify.add(mSongsAdapter);
+            }
+            if (mReadyToDisplay)
+                display(mAdaptersToNotify);
+        }
+    };
 
-                    if (mReadyToDisplay)
-                        display();
-                }
-            }).start();
+    Runnable updateGenres = new Runnable() {
+        @Override
+        public void run() {
+            Collections.sort(mAudioList, MediaComparators.byGenre);
+            mGenresAdapter.addAll(mAudioList, AudioBrowserListAdapter.TYPE_GENRES);
+            synchronized (mAdaptersToNotify) {
+                mAdaptersToNotify.add(mGenresAdapter);
+            }
+            if (mReadyToDisplay)
+                display(mAdaptersToNotify);
         }
-    }
+    };
 
     AudioBrowserListAdapter.ContextPopupMenuListener mContextPopupMenuListener
         = new AudioBrowserListAdapter.ContextPopupMenuListener() {
-- 
2.1.0



More information about the Android mailing list