[Android] Use queue and lock system for playlist updates

Geoffrey Métais git at videolan.org
Thu Jan 12 17:15:44 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan 12 17:04:00 2017 +0100| [b8e04eeeb1b06068b8995329979c3c3c11ad3931] | committer: Geoffrey Métais

Use queue and lock system for playlist updates

This is aimed to prevent race conditions in case of successive updates
triggered from playback service

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

 .../org/videolan/vlc/gui/audio/PlaylistAdapter.java   | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java
index ec92c05..24ed701 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/PlaylistAdapter.java
@@ -119,10 +119,21 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
         mDataSet.addAll(playList);
     }
 
+    private boolean mDispatching = false;
     public void dispatchUpdate(final List<MediaWrapper> newList) {
-        VLCApplication.runBackground(new Runnable() {
+        VLCApplication.queueBackground(new Runnable() {
             @Override
             public void run() {
+                synchronized (PlaylistAdapter.this) {
+                    if (mDispatching) {
+                        try {
+                            PlaylistAdapter.this.wait(1000);
+                        } catch (InterruptedException ignored) {
+                            return;
+                        }
+                    }
+                    mDispatching = true;
+                }
                 final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(mDataSet, newList));
                 mHandler.post(new Runnable() {
                     @Override
@@ -132,10 +143,14 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
                         result.dispatchUpdatesTo(PlaylistAdapter.this);
                         if (mService != null)
                             setCurrentIndex(mService.getCurrentMediaPosition());
+                        synchronized (PlaylistAdapter.this) {
+                            mDispatching = false;
+                            PlaylistAdapter.this.notify();
+                        }
                     }
                 });
             }
-        });
+        }, true);
     }
 
     @MainThread



More information about the Android mailing list