[Android] Fix fast browsing with prefetch media
Geoffrey Métais
git at videolan.org
Wed Feb 22 15:18:26 CET 2017
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Feb 22 15:17:36 2017 +0100| [9a8c08ad051ec93f8d74e1b7d89792c6c5ad1b50] | committer: Geoffrey Métais
Fix fast browsing with prefetch media
> https://code.videolan.org/videolan/vlc-android/commit/9a8c08ad051ec93f8d74e1b7d89792c6c5ad1b50
---
.../vlc/gui/browser/BaseBrowserAdapter.java | 22 ++++++++++++----------
.../vlc/gui/browser/BaseBrowserFragment.java | 10 ++++------
.../vlc/gui/browser/FileBrowserFragment.java | 18 ++++++++++++++++--
.../vlc/gui/browser/NetworkBrowserFragment.java | 6 +++++-
.../vlc/gui/browser/StorageBrowserFragment.java | 2 +-
5 files changed, 38 insertions(+), 20 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
index 8dd081c..f6ffd28 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -215,7 +215,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
public void clear() {
if (!isEmpty())
- dispatchUpdate(new ArrayList<MediaLibraryItem>(0));
+ update(new ArrayList<MediaLibraryItem>(0));
}
public boolean isEmpty(){
@@ -245,7 +245,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
mMediaCount++;
list.add(position, item);
- dispatchUpdate(list);
+ update(list);
}
public void setTop (int top) {
@@ -253,14 +253,14 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
}
public void addAll(ArrayList<? extends MediaLibraryItem> mediaList){
- dispatchUpdate((ArrayList<MediaLibraryItem>) mediaList);
+ update((ArrayList<MediaLibraryItem>) mediaList);
}
void removeItem(int position) {
MediaLibraryItem item = mMediaList.get(position);
ArrayList<MediaLibraryItem> list = new ArrayList<>(mPendingUpdates.isEmpty() ? mMediaList : mPendingUpdates.peekLast());
list.remove(position);
- dispatchUpdate(list);
+ update(list);
if (item .getItemType() == TYPE_MEDIA && (((MediaWrapper) item).getType() == MediaWrapper.TYPE_VIDEO || ((MediaWrapper) item).getType() == MediaWrapper.TYPE_AUDIO))
mMediaCount--;
}
@@ -351,13 +351,15 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
}
private ArrayDeque<ArrayList<MediaLibraryItem>> mPendingUpdates = new ArrayDeque<>();
- void dispatchUpdate(final ArrayList<MediaLibraryItem> items) {
+
+ @MainThread
+ void update(final ArrayList<MediaLibraryItem> items) {
mPendingUpdates.add(items);
if (mPendingUpdates.size() == 1)
- update(items);
+ internalUpdate(items);
}
- private void update(final ArrayList<MediaLibraryItem> items) {
+ private void internalUpdate(final ArrayList<MediaLibraryItem> items) {
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
@@ -375,7 +377,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
result.dispatchUpdatesTo(BaseBrowserAdapter.this);
fragment.onUpdateFinished(null);
if (!mPendingUpdates.isEmpty())
- update(mPendingUpdates.peek());
+ internalUpdate(mPendingUpdates.peek());
}
});
}
@@ -384,7 +386,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
void restoreList() {
if (mOriginalData != null) {
- dispatchUpdate(new ArrayList<>(mOriginalData));
+ update(new ArrayList<>(mOriginalData));
mOriginalData = null;
}
}
@@ -400,7 +402,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
- dispatchUpdate((ArrayList<MediaLibraryItem>) filterResults.values);
+ update((ArrayList<MediaLibraryItem>) filterResults.values);
}
}
}
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
index f5ac334..56b4338 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
@@ -100,7 +100,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
boolean goBack = false;
private SparseArray<ArrayList<MediaWrapper>> mFoldersContentLists;
- private ArrayList<MediaWrapper> mediaList;
+ protected ArrayList<MediaWrapper> mediaList;
public int mCurrentParsedPosition = 0;
protected abstract Fragment createFragment();
@@ -121,10 +121,8 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
mFoldersContentLists = (SparseArray<ArrayList<MediaWrapper>>) VLCApplication.getData(KEY_CONTENT_LIST);
if (mFoldersContentLists == null)
mFoldersContentLists = new SparseArray<>();
- if (bundle != null){
+ if (bundle != null) {
mediaList = (ArrayList<MediaWrapper>) VLCApplication.getData(KEY_MEDIA_LIST);
- if (mediaList != null)
- mAdapter.addAll(mediaList);
mCurrentMedia = bundle.getParcelable(KEY_MEDIA);
if (mCurrentMedia != null)
mMrl = mCurrentMedia.getLocation();
@@ -162,7 +160,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
if (!mAdapter.isEmpty()) {
mAdapter.notifyItemRangeInserted(0, mAdapter.getItemCount());
onUpdateFinished(mAdapter);
- } else if (!(this instanceof NetworkBrowserFragment))
+ } else if (!(this instanceof NetworkBrowserFragment) && !(this instanceof FileBrowserFragment))
refresh();
}
@@ -749,8 +747,8 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
public void onUpdateFinished(RecyclerView.Adapter adapter) {
mHandler.sendEmptyMessage(BrowserFragmentHandler.MSG_HIDE_LOADING);
updateEmptyView();
- parseSubDirectories();
if (!mAdapter.isEmpty()) {
+ parseSubDirectories();
if (mSavedPosition > 0) {
mLayoutManager.scrollToPositionWithOffset(mSavedPosition, 0);
mSavedPosition = 0;
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
index 36bbb98..4d735d1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -66,6 +66,15 @@ public class FileBrowserFragment<T extends BaseBrowserAdapter> extends BaseBrows
}
@Override
+ public void onStart() {
+ super.onStart();
+ if (mediaList != null) {
+ mAdapter.addAll(mediaList);
+ } else
+ refresh();
+ }
+
+ @Override
protected Fragment createFragment() {
return new FileBrowserFragment();
}
@@ -98,7 +107,7 @@ public class FileBrowserFragment<T extends BaseBrowserAdapter> extends BaseBrows
public void run() {
String storages[] = AndroidDevices.getMediaDirectories();
MediaWrapper directory;
- ArrayList<MediaLibraryItem> devices = new ArrayList<>(storages.length);
+ final ArrayList<MediaLibraryItem> devices = new ArrayList<>(storages.length);
for (String mediaDirLocation : storages) {
if (!(new File(mediaDirLocation).exists()))
continue;
@@ -108,7 +117,12 @@ public class FileBrowserFragment<T extends BaseBrowserAdapter> extends BaseBrows
directory.setDisplayTitle(VLCApplication.getAppResources().getString(R.string.internal_memory));
devices.add(directory);
}
- mAdapter.dispatchUpdate(devices);
+ VLCApplication.runOnMainThread(new Runnable() {
+ @Override
+ public void run() {
+ mAdapter.update(devices);
+ }
+ });
mHandler.sendEmptyMessage(BrowserFragmentHandler.MSG_HIDE_LOADING);
}
});
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java
index e937d60..59f23d6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/NetworkBrowserFragment.java
@@ -67,7 +67,11 @@ public class NetworkBrowserFragment extends BaseBrowserFragment {
super.onStart();
//Handle network connection state
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
- mSkipRefresh = !mAdapter.isEmpty();
+
+ if (mediaList != null) {
+ mSkipRefresh = !mediaList.isEmpty();
+ mAdapter.addAll(mediaList);
+ }
getActivity().registerReceiver(networkReceiver, filter);
if (mSkipRefresh)
parseSubDirectories();
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.java
index fffd3b8..ad8183e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.java
@@ -140,7 +140,7 @@ public class StorageBrowserFragment extends FileBrowserFragment implements Entry
storage = new Storage(Uri.parse(customDir));
storagesList.add(storage);
}
- mAdapter.dispatchUpdate(storagesList);
+ mAdapter.update(storagesList);
mHandler.sendEmptyMessage(BrowserFragmentHandler.MSG_HIDE_LOADING);
}
More information about the Android
mailing list