Commit 50930bfb authored by Geoffrey Métais's avatar Geoffrey Métais

Thread browser updates

parent b661b0db
......@@ -47,8 +47,12 @@ import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.util.MediaItemDiffCallback;
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;
......@@ -71,6 +75,9 @@ 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;
}
......@@ -221,20 +228,21 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
return mMediaList.isEmpty();
}
public void addItem(MediaLibraryItem item, boolean notify, boolean top){
addItem(item, notify, top, -1);
public void addItem(MediaLibraryItem item, boolean top){
addItem(item, top, -1);
}
void addItem(MediaLibraryItem item, boolean notify, int position){
addItem(item, notify, false, position);
void addItem(MediaLibraryItem item, int position){
addItem(item, false, position);
}
void addItem(MediaLibraryItem item, boolean notify, boolean top, int positionTo){
void addItem(MediaLibraryItem item, boolean top, int positionTo){
int position;
ArrayList<MediaLibraryItem> list = new ArrayList<>(mPendingUpdates.isEmpty() ? mMediaList : mPendingUpdates.peekLast());
if (positionTo != -1)
position = positionTo;
else
position = top ? mTop : mMediaList.size();
position = top ? mTop : list.size();
if (item .getItemType() == TYPE_MEDIA && item.getTitle().startsWith("."))
return;
......@@ -242,50 +250,39 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
if (item .getItemType() == TYPE_MEDIA && (((MediaWrapper) item).getType() == MediaWrapper.TYPE_VIDEO || ((MediaWrapper) item).getType() == MediaWrapper.TYPE_AUDIO))
mMediaCount++;
mMediaList.add(position, item);
if (notify)
notifyItemInserted(position);
list.add(position, item);
dispatchUpdate(list);
}
public void setTop (int top) {
mTop = top;
}
public void addAll(ArrayList<MediaWrapper> mediaList){
mMediaList.clear();
boolean isHoneyComb = AndroidUtil.isHoneycombOrLater();
for (MediaWrapper mw : mediaList) {
mMediaList.add(mw);
if (mw.getType() == MediaWrapper.TYPE_AUDIO || (isHoneyComb && mw.getType() == MediaWrapper.TYPE_VIDEO))
mMediaCount++;
}
public void addAll(ArrayList<? extends MediaLibraryItem> mediaList){
dispatchUpdate((ArrayList<MediaLibraryItem>) mediaList);
}
void removeItem(int position, boolean notify){
MediaLibraryItem item = null;
item = mMediaList.get(position);
mMediaList.remove(position);
if (notify)
notifyItemRemoved(position);
void removeItem(int position) {
MediaLibraryItem item = mMediaList.get(position);
ArrayList<MediaLibraryItem> list = new ArrayList<>(mPendingUpdates.isEmpty() ? mMediaList : mPendingUpdates.peekLast());
list.remove(position);
dispatchUpdate(list);
if (item .getItemType() == TYPE_MEDIA && (((MediaWrapper) item).getType() == MediaWrapper.TYPE_VIDEO || ((MediaWrapper) item).getType() == MediaWrapper.TYPE_AUDIO))
mMediaCount--;
}
void removeItem(String path, boolean notify) {
void removeItem(String path) {
int position = -1;
for (int i = 0; i< getItemCount(); ++i) {
MediaLibraryItem item = mMediaList.get(i);
if (item .getItemType() == TYPE_MEDIA && TextUtils.equals(path, ((MediaWrapper) item).getUri().toString())) {
position = i;
if (((MediaWrapper) item).getType() == MediaWrapper.TYPE_VIDEO || ((MediaWrapper) item).getType() == MediaWrapper.TYPE_AUDIO)
mMediaCount--;
break;
}
}
if (position == -1)
return;
mMediaList.remove(position);
if (notify)
notifyItemRemoved(position);
removeItem(position);
}
public ArrayList<MediaLibraryItem> getAll(){
......@@ -359,14 +356,35 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
return mFilter;
}
boolean isHoneyComb = AndroidUtil.isHoneycombOrLater();
private ArrayDeque<ArrayList<MediaLibraryItem>> mPendingUpdates = new ArrayDeque<>();
void dispatchUpdate(final ArrayList<MediaLibraryItem> items) {
VLCApplication.runOnMainThread(new Runnable() {
mPendingUpdates.add(items);
if (mPendingUpdates.size() == 1)
update(items);
}
private void update(final ArrayList<MediaLibraryItem> items) {
mThreadPool.execute(new Runnable() {
@Override
public void run() {
final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(mMediaList, items), false);
mMediaList = items;
result.dispatchUpdatesTo(BaseBrowserAdapter.this);
fragment.onUpdateFinished(null);
for (MediaLibraryItem item : items) {
if (item.getItemType() == MediaLibraryItem.TYPE_MEDIA
&& (((MediaWrapper)item).getType() == MediaWrapper.TYPE_AUDIO|| (isHoneyComb && ((MediaWrapper)item).getType() == MediaWrapper.TYPE_VIDEO)))
mMediaCount++;
}
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
mPendingUpdates.remove();
mMediaList = items;
result.dispatchUpdatesTo(BaseBrowserAdapter.this);
fragment.onUpdateFinished(null);
if (!mPendingUpdates.isEmpty())
update(mPendingUpdates.peek());
}
});
}
});
}
......
......@@ -255,7 +255,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
@Override
public void onMediaAdded(int index, Media media) {
boolean wasEmtpy = mAdapter.isEmpty();
mAdapter.addItem(new MediaWrapper(media), true, false);
mAdapter.addItem(new MediaWrapper(media), false);
if (!wasEmtpy)
return;
updateEmptyView();
......@@ -265,7 +265,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
@Override
public void onMediaRemoved(int index, Media media) {
mAdapter.removeItem(media.getUri().toString(), mReadyToDisplay);
mAdapter.removeItem(media.getUri().toString());
}
@Override
......@@ -434,7 +434,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
return true;
}
case R.id.directory_view_delete:
mAdapter.removeItem(position, true);
mAdapter.removeItem(position);
UiTools.snackerWithCancel(getView(), getString(R.string.file_deleted), new Runnable() {
@Override
public void run() {
......@@ -565,7 +565,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
@Override
public void onBrowseEnd() {
if (mAdapter.isEmpty()) {
if (mAdapter.isEmpty() || getActivity() == null) {
mCurrentParsedPosition = -1;
releaseBrowser();
return;
......
......@@ -160,7 +160,7 @@ public class FileBrowserFragment<T extends BaseBrowserAdapter> extends BaseBrows
Storage storage = (Storage) mAdapter.getItem(position);
MediaDatabase.getInstance().recursiveRemoveDir(storage.getUri().getPath());
CustomDirectories.removeCustomDirectory(storage.getUri().getPath());
mAdapter.removeItem(position, true);
mAdapter.removeItem(position);
((AudioPlayerContainerActivity)getActivity()).updateLib();
return true;
} else
......
......@@ -34,11 +34,11 @@ public class FilePickerAdapter extends BaseBrowserAdapter {
super(fragment);
}
public void addItem(MediaLibraryItem media, boolean notify, boolean top){
public void addItem(MediaLibraryItem media, boolean top){
if (media.getItemType() != TYPE_MEDIA)
return;
if (filter((MediaWrapper) media))
super.addItem(media, notify, top);
super.addItem(media, top);
}
public void onBindViewHolder(final ViewHolder holder, int position) {
......
......@@ -127,7 +127,7 @@ public class NetworkBrowserFragment extends BaseBrowserFragment {
mMediaBrowser.discoverNetworkShares();
else {
if (!mAdapter.isEmpty())
mAdapter.removeItem(mAdapter.getItemCount() - 1, true);
mAdapter.removeItem(mAdapter.getItemCount() - 1);
mHandler.sendEmptyMessage(BrowserFragmentHandler.MSG_HIDE_LOADING);
}
}
......@@ -175,20 +175,19 @@ public class NetworkBrowserFragment extends BaseBrowserFragment {
}
if (mFavorites != 0 && !mAdapter.isEmpty())
for (int i = 1 ; i <= mFavorites ; ++i) //remove former favorites
mAdapter.removeItem(1, mReadyToDisplay);
mAdapter.removeItem(1);
if (newSize == 0 && !mAdapter.isEmpty()) {
mAdapter.removeItem(0, mReadyToDisplay); //also remove separator if no more fav
mAdapter.removeItem(0, mReadyToDisplay); //also remove separator if no more fav
mAdapter.removeItem(0); //also remove separator if no more fav
mAdapter.removeItem(0); //also remove separator if no more fav
} else {
boolean isEmpty = mAdapter.isEmpty();
if (mFavorites == 0 || isEmpty)
mAdapter.addItem(new DummyItem(getString(R.string.network_favorites)), false, false,0); //add header if needed
mAdapter.addItem(new DummyItem(getString(R.string.network_favorites)), false,0); //add header if needed
for (int i = 0 ; i < newSize ; )
mAdapter.addItem(favs.get(i), false, false, ++i); //add new favorites
mAdapter.addItem(favs.get(i), false, ++i); //add new favorites
if (mFavorites == 0 || isEmpty)
mAdapter.addItem(new DummyItem(getString(R.string.network_shared_folders)), false, false, newSize + 1); //add header if needed
mAdapter.notifyItemRangeChanged(0, newSize+1);
mAdapter.addItem(new DummyItem(getString(R.string.network_shared_folders)), false, newSize + 1); //add header if needed
}
mFavorites = newSize; //update count
if (newSize != 0)
......
......@@ -64,10 +64,10 @@ class StorageBrowserAdapter extends BaseBrowserAdapter {
vh.setContextMenuListener();
}
public void addItem(MediaLibraryItem item, boolean notify, boolean top) {
public void addItem(MediaLibraryItem item, boolean top) {
if (item.getItemType() == MediaLibraryItem.TYPE_MEDIA)
item = new Storage(((MediaWrapper)item).getUri());
super.addItem(item, notify, top);
super.addItem(item, top);
}
private void removeDir(final String path) {
......
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