[Android] TV: Refactor network discoverer
Geoffrey Métais
git at videolan.org
Wed Apr 4 18:36:07 CEST 2018
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Mar 30 12:07:39 2018 +0200| [6b6050084390fcd3c815b1124ca784a9024410d2] | committer: Geoffrey Métais
TV: Refactor network discoverer
> https://code.videolan.org/videolan/vlc-android/commit/6b6050084390fcd3c815b1124ca784a9024410d2
---
.../vlc/gui/tv/browser/BrowserGridFragment.java | 147 ++++-----------------
.../vlc/viewmodels/browser/NetworkProvider.kt | 2 +-
2 files changed, 24 insertions(+), 125 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/BrowserGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/tv/browser/BrowserGridFragment.java
index 72106e609..3ab0260aa 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/BrowserGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/BrowserGridFragment.java
@@ -24,176 +24,75 @@
package org.videolan.vlc.gui.tv.browser;
import android.annotation.TargetApi;
+import android.arch.lifecycle.Observer;
+import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
-import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
-import android.os.Handler;
-import android.os.HandlerThread;
-import android.os.Looper;
-import android.os.Process;
+import android.support.annotation.Nullable;
import android.support.v17.leanback.widget.OnItemViewClickedListener;
import android.support.v17.leanback.widget.OnItemViewSelectedListener;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.RowPresenter;
import android.support.v7.preference.PreferenceManager;
-import android.text.TextUtils;
-import org.videolan.libvlc.Media;
-import org.videolan.libvlc.util.MediaBrowser;
+import org.videolan.medialibrary.media.MediaLibraryItem;
import org.videolan.medialibrary.media.MediaWrapper;
+import org.videolan.vlc.ExternalMonitor;
import org.videolan.vlc.VLCApplication;
-import org.videolan.vlc.gui.helpers.MediaComparators;
import org.videolan.vlc.gui.tv.DetailsActivity;
import org.videolan.vlc.gui.tv.MediaItemDetails;
import org.videolan.vlc.gui.tv.TvUtil;
import org.videolan.vlc.gui.tv.browser.interfaces.BrowserActivityInterface;
import org.videolan.vlc.gui.tv.browser.interfaces.DetailsFragment;
import org.videolan.vlc.util.Constants;
-import org.videolan.vlc.util.VLCInstance;
+import org.videolan.vlc.viewmodels.browser.NetworkProvider;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
-public class BrowserGridFragment extends GridFragment implements MediaBrowser.EventListener, OnItemViewSelectedListener, OnItemViewClickedListener, DetailsFragment {
+public class BrowserGridFragment extends GridFragment implements OnItemViewSelectedListener, OnItemViewClickedListener, DetailsFragment {
- private MediaBrowser mMediaBrowser;
- private Uri mUri;
- List<MediaWrapper> mMediaList = null;
private MediaWrapper mItemSelected;
private boolean mShowHiddenFiles = false;
-
- private Handler mBrowserHandler;
-
- protected void runOnBrowserThread(Runnable runnable) {
- if (mBrowserHandler == null) {
- HandlerThread handlerThread = new HandlerThread("vlc-mProvider", Process.THREAD_PRIORITY_DEFAULT+Process.THREAD_PRIORITY_LESS_FAVORABLE);
- handlerThread.start();
- mBrowserHandler = new Handler(handlerThread.getLooper());
- }
- if (Looper.myLooper() == mBrowserHandler.getLooper())
- runnable.run();
- else
- mBrowserHandler.post(runnable);
- }
+ private NetworkProvider provider;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setOnItemViewSelectedListener(this);
setOnItemViewClickedListener(this);
mShowHiddenFiles = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext()).getBoolean("browser_show_hidden_files", false);
- }
-
- public void onResume() {
- super.onResume();
- mMediaList = new ArrayList<>();
- if (mAdapter.size() == 0) runOnBrowserThread(new Runnable() {
- @Override
- public void run() {
- mMediaBrowser = new MediaBrowser(VLCInstance.get(), BrowserGridFragment.this);
- if (mUri != null) {
- int flags = MediaBrowser.Flag.Interact;
- if (mShowHiddenFiles) flags |= MediaBrowser.Flag.ShowHiddenFiles;
- mMediaBrowser.browse(mUri, flags);
- } else
- mMediaBrowser.discoverNetworkShares();
- ((BrowserActivityInterface)mContext).showProgress(true);
- }
- });
- }
-
- public void onPause(){
- super.onPause();
- runOnBrowserThread(new Runnable() {
- @Override
- public void run() {
- if (mMediaBrowser != null) mMediaBrowser.release();
- mMediaBrowser = null;
- }
- });
- ((BrowserActivityInterface)mContext).updateEmptyView(false);
- }
- @Override
- public void onMediaAdded(int index, Media media) {
- final MediaWrapper mw = new MediaWrapper(media);
- final int type = mw.getType();
- VLCApplication.runOnMainThread(new Runnable() {
+ provider = ViewModelProviders.of(this).get(NetworkProvider.class);
+ provider.getDataset().observe(this, new Observer<List<MediaLibraryItem>>() {
@Override
- public void run() {
- if (type == MediaWrapper.TYPE_AUDIO || type == MediaWrapper.TYPE_VIDEO || type == MediaWrapper.TYPE_DIR)
- mMediaList.add(mw);
- if (mUri == null) { // we are at root level
- mw.setDescription(mw.getUri().getScheme());
- mAdapter.add(mw);
- }
- ((BrowserActivityInterface)getActivity()).showProgress(false);
+ public void onChanged(@Nullable List<MediaLibraryItem> mediaLibraryItems) {
+ mAdapter.setItems(mediaLibraryItems, TvUtil.INSTANCE.getDiffCallback());
}
});
- }
-
- @Override
- public void onMediaRemoved(int index, Media media) {
- final String uri = media.getUri().toString();
- VLCApplication.runOnMainThread(new Runnable() {
+ ExternalMonitor.connected.observe(this, new Observer<Boolean>() {
@Override
- public void run() {
- int position = -1;
- for (int i = 0; i < mMediaList.size(); ++i) {
- if (TextUtils.equals(mMediaList.get(i).getUri().toString(), uri)) {
- position = i;
- break;
- }
- }
- if (position == -1) return;
- mAdapter.removeItems(position, 1);
+ public void onChanged(@Nullable Boolean connected) {
+ if (connected) provider.refresh();
+ //TODO empty/disconnected view
}
});
}
@Override
- public void onBrowseEnd() {
- if (mMediaBrowser != null) {
- mMediaBrowser.release();
- mMediaBrowser = null;
- }
- VLCApplication.runOnMainThread(new Runnable() {
- @Override
- public void run() {
- ((BrowserActivityInterface)getActivity()).showProgress(false);
- ((BrowserActivityInterface)getActivity()).updateEmptyView(mMediaList.isEmpty());
- sortList();
- }
- });
+ public void onStop() {
+ super.onStop();
+ provider.releaseBrowser();
}
- public void sortList(){
- List<MediaWrapper> files = new ArrayList<MediaWrapper>(), dirs = new ArrayList<MediaWrapper>();
- for (Object item : mMediaList){
- if (item instanceof MediaWrapper) {
- MediaWrapper media = (MediaWrapper) item;
- if (media.getType() == MediaWrapper.TYPE_DIR)
- dirs.add(media);
- else
- files.add(media);
- }
- }
- Collections.sort(dirs, MediaComparators.byName);
- Collections.sort(files, MediaComparators.byName);
- mMediaList.clear();
- mMediaList.addAll(dirs);
- mMediaList.addAll(files);
- mAdapter.clear();
- mAdapter.addAll(0, mMediaList);
- mAdapter.notifyArrayItemRangeChanged(0, mMediaList.size());
+ public void onPause() {
+ super.onPause();
+ ((BrowserActivityInterface)mContext).updateEmptyView(false);
}
public void showDetails() {
if (mItemSelected.getType() == MediaWrapper.TYPE_DIR) {
- Intent intent = new Intent(getActivity(),
- DetailsActivity.class);
+ final Intent intent = new Intent(getActivity(), DetailsActivity.class);
// pass the item information
intent.putExtra("media", mItemSelected);
intent.putExtra("item", new MediaItemDetails(mItemSelected.getTitle(),
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/browser/NetworkProvider.kt b/vlc-android/src/org/videolan/vlc/viewmodels/browser/NetworkProvider.kt
index a4a1a0a08..3dbbed79f 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/browser/NetworkProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/browser/NetworkProvider.kt
@@ -14,7 +14,7 @@ import org.videolan.vlc.ExternalMonitor
import org.videolan.vlc.media.MediaDatabase
import java.util.*
-class NetworkProvider(url: String?): BrowserProvider(url) {
+class NetworkProvider(url: String? = null): BrowserProvider(url) {
val favorites by lazy {
MutableLiveData<MutableList<MediaLibraryItem>>()
}
More information about the Android
mailing list