Commit 93b03bf0 authored by Geoffrey Métais's avatar Geoffrey Métais

Factorize a thread queue for adapters

Base adapter class with threadpool of max 1 thread to serialize diffutil
calculations in background
parent 1711794a
/*
* *************************************************************************
* ThreadQueueAdapter.java
* **************************************************************************
* Copyright © 2017 VLC authors and VideoLAN
* Author: Geoffrey Métais
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
* ***************************************************************************
*/
package org.videolan.vlc.gui;
import android.support.v7.widget.RecyclerView;
import org.videolan.vlc.VLCApplication;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public abstract class ThreadQueueAdapter<V extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<V> {
private ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(1, 1, 2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(), VLCApplication.THREAD_FACTORY);
protected void queueTask(Runnable runnable) {
mThreadPool.execute(runnable);
}
}
......@@ -44,6 +44,7 @@ import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.databinding.AudioBrowserItemBinding;
import org.videolan.vlc.databinding.AudioBrowserSeparatorBinding;
import org.videolan.vlc.gui.ThreadQueueAdapter;
import org.videolan.vlc.gui.helpers.AsyncImageLoader;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.gui.view.FastScroller;
......@@ -58,7 +59,7 @@ import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapter.ViewHolder> implements FastScroller.SeparatedAdapter, Filterable {
public class AudioBrowserAdapter extends ThreadQueueAdapter<AudioBrowserAdapter.ViewHolder> implements FastScroller.SeparatedAdapter, Filterable {
private static final String TAG = "VLC/AudioBrowserAdapter";
......@@ -279,7 +280,7 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
}
private void internalUpdate(final MediaLibraryItem[] items) {
new Thread(new Runnable() {
queueTask(new Runnable() {
@Override
public void run() {
final MediaLibraryItem[] newList = mOriginalDataSet == null && hasSections() ? generateList(items) : items;
......@@ -296,7 +297,7 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
}
});
}
}).start();
});
}
@MainThread
......
......@@ -42,6 +42,7 @@ import org.videolan.vlc.PlaybackService;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.databinding.PlaylistItemBinding;
import org.videolan.vlc.gui.ThreadQueueAdapter;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.interfaces.SwipeDragHelperAdapter;
import org.videolan.vlc.media.MediaUtils;
......@@ -52,17 +53,11 @@ import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHolder> implements SwipeDragHelperAdapter, Filterable {
public class PlaylistAdapter extends ThreadQueueAdapter<PlaylistAdapter.ViewHolder> implements SwipeDragHelperAdapter, Filterable {
private static final String TAG = "VLC/PlaylistAdapter";
private ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(1, 1, 2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(), VLCApplication.THREAD_FACTORY);
private ItemFilter mFilter = new ItemFilter();
private PlaybackService mService = null;
private IPlayer mAudioPlayer;
......@@ -143,7 +138,7 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
@MainThread
private void internalUpdate(final List<MediaWrapper> newList) {
mThreadPool.execute(new Runnable() {
queueTask(new Runnable() {
@Override
public void run() {
final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(mDataSet, newList), false);
......
......@@ -43,6 +43,7 @@ import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.databinding.BrowserItemBinding;
import org.videolan.vlc.databinding.BrowserItemSeparatorBinding;
import org.videolan.vlc.gui.ThreadQueueAdapter;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.util.MediaItemDiffCallback;
import org.videolan.vlc.util.MediaItemFilter;
......@@ -50,15 +51,12 @@ import org.videolan.vlc.util.MediaItemFilter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static org.videolan.medialibrary.media.MediaLibraryItem.FLAG_SELECTED;
import static org.videolan.medialibrary.media.MediaLibraryItem.TYPE_MEDIA;
import static org.videolan.medialibrary.media.MediaLibraryItem.TYPE_STORAGE;
public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.ViewHolder> implements Filterable {
public class BaseBrowserAdapter extends ThreadQueueAdapter<BaseBrowserAdapter.ViewHolder> implements Filterable {
protected static final String TAG = "VLC/BaseBrowserAdapter";
private static int FOLDER_RES_ID = R.drawable.ic_menu_folder;
......@@ -75,9 +73,6 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
private int mTop = 0, mMediaCount = 0, mSelectionCount = 0;
private ItemFilter mFilter = new ItemFilter();
private ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(1, 1, 2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(), VLCApplication.THREAD_FACTORY);
BaseBrowserAdapter(BaseBrowserFragment fragment){
this.fragment = fragment;
}
......@@ -365,7 +360,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
}
private void update(final ArrayList<MediaLibraryItem> items) {
mThreadPool.execute(new Runnable() {
queueTask(new Runnable() {
@Override
public void run() {
final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(mMediaList, items), false);
......
......@@ -46,6 +46,7 @@ import org.videolan.medialibrary.media.MediaWrapper;
import org.videolan.vlc.BR;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.gui.ThreadQueueAdapter;
import org.videolan.vlc.gui.helpers.AsyncImageLoader;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.interfaces.IEventsHandler;
......@@ -61,7 +62,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> implements Filterable {
public class VideoListAdapter extends ThreadQueueAdapter<VideoListAdapter.ViewHolder> implements Filterable {
public final static String TAG = "VLC/VideoListAdapter";
......@@ -495,7 +496,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
}
private void internalUpdate(final ArrayList<MediaWrapper> items, final boolean detectMoves) {
new Thread(new Runnable() {
queueTask(new Runnable() {
@Override
public void run() {
Collections.sort(items, mVideoComparator);
......@@ -512,7 +513,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
}
});
}
}).start();
});
}
private class VideoItemDiffCallback extends DiffUtil.Callback {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment