[Android] Add update to threadpool only when needed

Geoffrey Métais git at videolan.org
Mon Dec 12 16:37:11 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Dec 12 16:36:23 2016 +0100| [15376de4ff50b0f29f963c3f72e1a81bcfb22af4] | committer: Geoffrey Métais

Add update to threadpool only when needed

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

 .../src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java      | 9 +++++++--
 .../src/org/videolan/vlc/gui/video/VideoGridFragment.java        | 2 +-
 vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java | 9 +++++++--
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
index d0c3677..cde8463 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
@@ -3,6 +3,7 @@ package org.videolan.vlc.gui.audio;
 import android.app.Activity;
 import android.content.Context;
 import android.databinding.ViewDataBinding;
+import android.os.Looper;
 import android.support.annotation.MainThread;
 import android.support.v7.util.DiffUtil;
 import android.support.v7.widget.RecyclerView;
@@ -242,7 +243,7 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
     }
 
     void dispatchUpdate(final MediaLibraryItem[] items) {
-        VLCApplication.runBackground(new Runnable() {
+        Runnable runnable = new Runnable() {
             @Override
             public void run() {
                 final MediaLibraryItem[] newList = hasSections() ? generateList(items) : items;
@@ -256,7 +257,11 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
                     }
                 });
             }
-        });
+        };
+        if (Looper.myLooper() == Looper.getMainLooper())
+            VLCApplication.runBackground(runnable);
+        else
+            runnable.run();
     }
 
     List<MediaLibraryItem> getSelection() {
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 71f5625..0334f01 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -380,12 +380,12 @@ public class VideoGridFragment extends MediaBrowserFragment implements MediaUpda
                     for (MediaGroup item : MediaGroup.group(itemList))
                         displayList.add(item.getMedia());
                 }
+                mVideoAdapter.dispatchUpdate(displayList);
 
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {
                         stopRefresh();
-                        mVideoAdapter.dispatchUpdate(displayList);
                     }
                 });
             }
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 264d079..275455e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -488,6 +488,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
             dispatchUpdate((ArrayList<MediaWrapper>) filterResults.values);
         }
     }
+
     @BindingAdapter({"time", "resolution"})
     public static void setLayoutHeight(View view, String time, String resolution) {
         ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
@@ -498,7 +499,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
     }
 
     void dispatchUpdate(final ArrayList<MediaWrapper> items) {
-        VLCApplication.runBackground(new Runnable() {
+        Runnable runnable = new Runnable() {
             @Override
             public void run() {
                 final SortedList<MediaWrapper> newSortedList = new SortedList<>(MediaWrapper.class, mVideoComparator);
@@ -517,7 +518,11 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
                     }
                 });
             }
-        });
+        };
+        if (Looper.myLooper() == Looper.getMainLooper())
+            VLCApplication.runBackground(runnable);
+        else
+            runnable.run();
     }
 
     private class VideoItemDiffCallback extends MediaItemDiffCallback {



More information about the Android mailing list