[Android] Less lock and update every new item added

Geoffrey Métais git at videolan.org
Wed May 25 17:24:23 CEST 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed May 25 16:24:56 2016 +0200| [4ecbf89d1cca7b2655191dc0130e5361272eaf07] | committer: Geoffrey Métais

Less lock and update every new item added

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

 .../src/org/videolan/vlc/gui/tv/MainTvActivity.java    |  2 +-
 .../org/videolan/vlc/gui/video/VideoGridFragment.java  |  7 +++++--
 .../org/videolan/vlc/gui/video/VideoListAdapter.java   |  3 +++
 .../org/videolan/vlc/gui/video/VideoListHandler.java   |  3 +--
 .../src/org/videolan/vlc/media/MediaLibrary.java       | 18 +++++++++++++-----
 5 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
index a870cba..5541b60 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
@@ -327,7 +327,7 @@ public class MainTvActivity extends BaseTvActivity implements IVideoBrowser, OnI
 
     @Override
     public void setItemToUpdate(MediaWrapper item) {
-        mHandler.sendMessage(mHandler.obtainMessage(VideoListHandler.UPDATE_ITEM, item));
+        mHandler.sendMessage(mHandler.obtainMessage(MediaLibrary.UPDATE_ITEM, item));
     }
 
     public void updateItem(MediaWrapper item) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index 0eb0fcb..d42d5e5 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -174,7 +174,6 @@ public class VideoGridFragment extends MediaBrowserFragment implements ISortable
         if ((getActivity() instanceof MainActivity))
             mMainActivity = (MainActivity) getActivity();
         mMediaLibrary.setBrowser(this);
-        mMediaLibrary.addUpdateHandler(mHandler);
         final boolean refresh = mVideoAdapter.isEmpty();
         // We don't animate while medialib is scanning. Because gridview is being populated.
         // That would lead to graphical glitches
@@ -190,6 +189,7 @@ public class VideoGridFragment extends MediaBrowserFragment implements ISortable
         updateViewMode();
         if (animate)
             mAnimator.animate();
+        mMediaLibrary.addUpdateHandler(mHandler);
 
         /* Start the thumbnailer */
         if (mThumbnailer != null)
@@ -384,7 +384,10 @@ public class VideoGridFragment extends MediaBrowserFragment implements ISortable
     private Handler mHandler = new VideoListHandler(this);
 
     public void updateItem(MediaWrapper item) {
+        if (item.getType() != MediaWrapper.TYPE_VIDEO)
+            return;
         mVideoAdapter.update(item);
+        mViewNomedia.setVisibility(View.GONE);
     }
 
     public void updateList() {
@@ -466,7 +469,7 @@ public class VideoGridFragment extends MediaBrowserFragment implements ISortable
     }
 
     public void setItemToUpdate(MediaWrapper item) {
-        mHandler.sendMessage(mHandler.obtainMessage(VideoListHandler.UPDATE_ITEM, item));
+        mHandler.sendMessage(mHandler.obtainMessage(MediaLibrary.UPDATE_ITEM, item));
     }
 
     public void setGroup(String prefix) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index 73d2af3..9ce1ba8 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -240,6 +240,9 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
         int position = mVideos.indexOf(item);
         if (position != -1) {
             mVideos.set(position, item);
+        } else {
+            position = mVideos.size();
+            mVideos.add(item);
         }
         notifyItemChanged(position);
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListHandler.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListHandler.java
index be86a1e..effc537 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListHandler.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListHandler.java
@@ -8,7 +8,6 @@ import org.videolan.vlc.interfaces.IVideoBrowser;
 import org.videolan.vlc.util.WeakHandler;
 
 public class VideoListHandler extends WeakHandler<IVideoBrowser> {
-    public static final int UPDATE_ITEM = 0;
 
     public VideoListHandler(IVideoBrowser owner) {
         super(owner);
@@ -20,7 +19,7 @@ public class VideoListHandler extends WeakHandler<IVideoBrowser> {
         if(owner == null) return;
 
         switch (msg.what) {
-            case UPDATE_ITEM:
+            case MediaLibrary.UPDATE_ITEM:
                 owner.updateItem((MediaWrapper)msg.obj);
                 break;
             case MediaLibrary.MEDIA_ITEMS_UPDATED:
diff --git a/vlc-android/src/org/videolan/vlc/media/MediaLibrary.java b/vlc-android/src/org/videolan/vlc/media/MediaLibrary.java
index d617d69..920db94 100644
--- a/vlc-android/src/org/videolan/vlc/media/MediaLibrary.java
+++ b/vlc-android/src/org/videolan/vlc/media/MediaLibrary.java
@@ -58,6 +58,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
 public class MediaLibrary {
     public final static String TAG = "VLC/MediaLibrary";
 
+    public static final int UPDATE_ITEM = 0;
     public static final int MEDIA_ITEMS_UPDATED = 100;
 
     private static MediaLibrary mInstance;
@@ -188,14 +189,12 @@ public class MediaLibrary {
 
     public ArrayList<MediaWrapper> getPlaylistFilesItems() {
         ArrayList<MediaWrapper> playlistItems = new ArrayList<>();
-        mItemListLock.readLock().lock();
         for (int i = 0; i < mItemList.size(); i++) {
             MediaWrapper item = mItemList.get(i);
             if (item.getType() == MediaWrapper.TYPE_PLAYLIST) {
                 playlistItems.add(item);
             }
         }
-        mItemListLock.readLock().unlock();
         return playlistItems;
     }
 
@@ -369,27 +368,28 @@ public class MediaLibrary {
                             // get existing media item from database
                             mItemList.add(existingMedias.get(fileURI));
                             mItemListLock.writeLock().unlock();
+                            notifyMediaUpdated(existingMedias.get(fileURI));
                             addedLocations.add(fileURI);
                         }
                     } else {
-                        mItemListLock.writeLock().lock();
                         // create new media item
                         final Media media = new Media(libVlcInstance, Uri.parse(fileURI));
                         media.parse();
                         /* skip files with .mod extension and no duration */
                         if ((media.getDuration() == 0 || (media.getTrackCount() != 0 && TextUtils.isEmpty(media.getTrack(0).codec))) &&
                             fileURI.endsWith(".mod")) {
-                            mItemListLock.writeLock().unlock();
                             media.release();
                             continue;
                         }
                         MediaWrapper mw = new MediaWrapper(media);
                         media.release();
                         mw.setLastModified(file.lastModified());
+                        mItemListLock.writeLock().lock();
                         mItemList.add(mw);
+                        mItemListLock.writeLock().unlock();
+                        notifyMediaUpdated(mw);
                         // Add this item to database
                         mediaDatabase.addMedia(mw);
-                        mItemListLock.writeLock().unlock();
                     }
                     if (isStopping) {
                         Log.d(TAG, "Stopping scan");
@@ -431,6 +431,14 @@ public class MediaLibrary {
         }
     }
 
+    private void notifyMediaUpdated(MediaWrapper mw) {
+        // update the video and audio activities
+        for (int i = 0; i < mUpdateHandler.size(); i++) {
+            Handler h = mUpdateHandler.get(i);
+            h.obtainMessage(UPDATE_ITEM, mw).sendToTarget();
+        }
+    }
+
     private void notifyMediaUpdated() {
         // update the video and audio activities
         for (int i = 0; i < mUpdateHandler.size(); i++) {



More information about the Android mailing list