20 changed files with 597 additions and 199 deletions
@ -0,0 +1,137 @@ |
|||
/* |
|||
* ************************************************************************* |
|||
* FileBrowserFragment.java |
|||
* ************************************************************************** |
|||
* Copyright © 2015 VLC authors and VideoLAN |
|||
* |
|||
* 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.browser; |
|||
|
|||
import android.content.BroadcastReceiver; |
|||
import android.content.Context; |
|||
import android.content.Intent; |
|||
import android.content.IntentFilter; |
|||
import android.os.Bundle; |
|||
import android.os.Environment; |
|||
import android.support.v4.app.Fragment; |
|||
import android.support.v7.internal.widget.AdapterViewCompat; |
|||
import android.util.Log; |
|||
import android.view.ContextMenu; |
|||
import android.view.MenuInflater; |
|||
import android.view.View; |
|||
|
|||
import org.videolan.vlc.MediaWrapper; |
|||
import org.videolan.vlc.R; |
|||
import org.videolan.vlc.util.AndroidDevices; |
|||
import org.videolan.vlc.util.Util; |
|||
|
|||
import java.io.File; |
|||
|
|||
public class FileBrowserFragment extends BaseBrowserFragment { |
|||
|
|||
public FileBrowserFragment() { |
|||
super(); |
|||
ROOT = Environment.getExternalStorageDirectory().getPath(); |
|||
} |
|||
|
|||
@Override |
|||
public void onCreate(Bundle bundle) { |
|||
super.onCreate(bundle); |
|||
mRoot = mMrl == null; |
|||
Log.d(TAG, "file root ? "+mRoot); |
|||
} |
|||
|
|||
@Override |
|||
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { |
|||
int position = ((AdapterViewCompat.AdapterContextMenuInfo)menuInfo).position; |
|||
MenuInflater menuInflater = getActivity().getMenuInflater(); |
|||
if (mAdapter.getItem(position) instanceof MediaWrapper) { |
|||
MediaWrapper mw = (MediaWrapper) mAdapter.getItem(position); |
|||
if (mw.getType() == MediaWrapper.TYPE_DIR) { |
|||
if (Util.canWrite(mw.getLocation())) { |
|||
menuInflater.inflate(R.menu.directory_view_dir, menu); |
|||
boolean nomedia = new File(mw.getLocation() + "/.nomedia").exists(); |
|||
menu.findItem(R.id.directory_view_hide_media).setVisible(!nomedia); |
|||
menu.findItem(R.id.directory_view_show_media).setVisible(nomedia); |
|||
} |
|||
} else if (mw.getType() == MediaWrapper.TYPE_AUDIO || mw.getType() == MediaWrapper.TYPE_VIDEO) |
|||
menuInflater.inflate(R.menu.directory_view_file, menu); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
protected Fragment createFragment() { |
|||
return new FileBrowserFragment(); |
|||
} |
|||
|
|||
@Override |
|||
protected String getCategoryTitle() { |
|||
return getString(R.string.directories); |
|||
} |
|||
|
|||
@Override |
|||
protected void browseRoot() { |
|||
Log.d(TAG, "file browse root"); |
|||
String storages[] = AndroidDevices.getMediaDirectories(); |
|||
MediaWrapper mw; |
|||
for (String storage : storages) { |
|||
mw = new MediaWrapper(storage); |
|||
mw.setTitle(AndroidDevices.getStorageTitle(storage)); |
|||
mw.setType(MediaWrapper.TYPE_DIR); |
|||
mAdapter.addItem(mw, false, false); |
|||
} |
|||
mHandler.sendEmptyMessage(BrowserFragmentHandler.MSG_HIDE_LOADING); |
|||
updateEmptyView(); |
|||
mAdapter.notifyDataSetChanged(); |
|||
} |
|||
|
|||
public void onStart(){ |
|||
super.onStart(); |
|||
|
|||
//Handle network connection state
|
|||
IntentFilter filter = new IntentFilter(); |
|||
filter.addAction(Intent.ACTION_MEDIA_MOUNTED); |
|||
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); |
|||
filter.addAction(Intent.ACTION_MEDIA_REMOVED); |
|||
filter.addAction(Intent.ACTION_MEDIA_EJECT); |
|||
getActivity().registerReceiver(storageReceiver, filter); |
|||
if (updateEmptyView()) |
|||
updateDisplay(); |
|||
} |
|||
|
|||
@Override |
|||
public void onStop() { |
|||
super.onStop(); |
|||
getActivity().unregisterReceiver(storageReceiver); |
|||
} |
|||
|
|||
private final BroadcastReceiver storageReceiver = new BroadcastReceiver() { |
|||
@Override |
|||
public void onReceive(Context context, Intent intent) { |
|||
String action = intent.getAction(); |
|||
|
|||
if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_MOUNTED) || |
|||
action.equalsIgnoreCase(Intent.ACTION_MEDIA_UNMOUNTED) || |
|||
action.equalsIgnoreCase(Intent.ACTION_MEDIA_REMOVED) || |
|||
action.equalsIgnoreCase(Intent.ACTION_MEDIA_EJECT)) { |
|||
if (updateEmptyView()) |
|||
updateDisplay(); |
|||
} |
|||
} |
|||
}; |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
/* |
|||
* ************************************************************************* |
|||
* NetworkBrowserAdapter.java |
|||
* ************************************************************************** |
|||
* Copyright © 2015 VLC authors and VideoLAN |
|||
* |
|||
* 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.browser; |
|||
|
|||
import org.videolan.vlc.R; |
|||
|
|||
public class NetworkBrowserAdapter extends BaseBrowserAdapter{ |
|||
|
|||
public NetworkBrowserAdapter(BaseBrowserFragment fragment){ |
|||
super(fragment); |
|||
FOLDER_RES_ID = R.drawable.ic_menu_network; |
|||
} |
|||
} |
|||
@ -0,0 +1,173 @@ |
|||
/* |
|||
* ************************************************************************* |
|||
* NetworkBrowserFragment.java |
|||
* ************************************************************************** |
|||
* Copyright © 2015 VLC authors and VideoLAN |
|||
* |
|||
* 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.browser; |
|||
|
|||
import android.content.BroadcastReceiver; |
|||
import android.content.Context; |
|||
import android.content.Intent; |
|||
import android.content.IntentFilter; |
|||
import android.net.ConnectivityManager; |
|||
import android.os.Bundle; |
|||
import android.support.v4.app.Fragment; |
|||
import android.view.View; |
|||
|
|||
import org.videolan.libvlc.util.MediaBrowser; |
|||
import org.videolan.vlc.MediaDatabase; |
|||
import org.videolan.vlc.MediaWrapper; |
|||
import org.videolan.vlc.R; |
|||
import org.videolan.vlc.util.AndroidDevices; |
|||
|
|||
import java.util.ArrayList; |
|||
|
|||
public class NetworkBrowserFragment extends BaseBrowserFragment { |
|||
|
|||
public NetworkBrowserFragment() { |
|||
ROOT = "smb"; |
|||
mHandler = new BrowserFragmentHandler(this); |
|||
mAdapter = new NetworkBrowserAdapter(this); |
|||
} |
|||
|
|||
@Override |
|||
public void onCreate(Bundle bundle) { |
|||
super.onCreate(bundle); |
|||
if (mMrl == null) |
|||
mMrl = ROOT; |
|||
mRoot = ROOT.equals(mMrl); |
|||
} |
|||
|
|||
public void onStart(){ |
|||
super.onStart(); |
|||
|
|||
//Handle network connection state
|
|||
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); |
|||
getActivity().registerReceiver(networkReceiver, filter); |
|||
} |
|||
|
|||
@Override |
|||
protected Fragment createFragment() { |
|||
return new NetworkBrowserFragment(); |
|||
} |
|||
|
|||
@Override |
|||
public void onStop() { |
|||
super.onStop(); |
|||
getActivity().unregisterReceiver(networkReceiver); |
|||
} |
|||
|
|||
protected void updateDisplay(){ |
|||
if (mMediaBrowser == null) |
|||
mMediaBrowser = new MediaBrowser(mLibVLC, this); |
|||
if (mAdapter.isEmpty()) |
|||
refresh(); |
|||
else if (mRoot) |
|||
updateFavorites(); |
|||
} |
|||
|
|||
@Override |
|||
protected void browseRoot() { |
|||
ArrayList<MediaWrapper> favs = MediaDatabase.getInstance().getAllNetworkFav(); |
|||
if (!favs.isEmpty()) { |
|||
mFavorites = favs.size(); |
|||
for (MediaWrapper fav : favs) { |
|||
mAdapter.addItem(fav, false, true); |
|||
} |
|||
mAdapter.addItem("Network favorites", false, true); |
|||
} |
|||
mMediaBrowser.discoverNetworkShares(); |
|||
} |
|||
|
|||
@Override |
|||
protected String getCategoryTitle() { |
|||
return getString(R.string.network_browsing); |
|||
} |
|||
|
|||
private void updateFavorites(){ |
|||
ArrayList<MediaWrapper> favs = MediaDatabase.getInstance().getAllNetworkFav(); |
|||
int newSize = favs.size(), totalSize = mAdapter.getItemCount(); |
|||
|
|||
if (newSize == 0 && mFavorites == 0) |
|||
return; |
|||
for (int i = 1 ; i <= mFavorites ; ++i){ //remove former favorites
|
|||
mAdapter.removeItem(totalSize-i); |
|||
} |
|||
if (newSize == 0) |
|||
mAdapter.removeItem(totalSize-mFavorites-1); //also remove separator if no more fav
|
|||
else { |
|||
if (mFavorites == 0) |
|||
mAdapter.addItem("Network favorites", false, false); //add header if needed
|
|||
for (MediaWrapper fav : favs) |
|||
mAdapter.addItem(fav, false, false); //add new favorites
|
|||
} |
|||
mFavorites = newSize; //update count
|
|||
} |
|||
|
|||
public void toggleFavorite() { |
|||
MediaDatabase db = MediaDatabase.getInstance(); |
|||
if (db.networkFavExists(mMrl)) |
|||
db.deleteNetworkFav(mMrl); |
|||
else |
|||
db.addNetworkFavItem(mMrl, mCurrentMedia.getTitle()); |
|||
getActivity().supportInvalidateOptionsMenu(); |
|||
} |
|||
|
|||
/** |
|||
* Update views visibility and emptiness info |
|||
* |
|||
* @return True if content needs can be refreshed |
|||
*/ |
|||
protected boolean updateEmptyView() { |
|||
if (AndroidDevices.hasLANConnection()) { |
|||
if (mAdapter.isEmpty()) { |
|||
mEmptyView.setText(mRoot ? R.string.network_shares_discovery : R.string.network_empty); |
|||
mEmptyView.setVisibility(View.VISIBLE); |
|||
mRecyclerView.setVisibility(View.GONE); |
|||
mSwipeRefreshLayout.setEnabled(false); |
|||
} else { |
|||
if (mEmptyView.getVisibility() == View.VISIBLE) { |
|||
mEmptyView.setVisibility(View.GONE); |
|||
mRecyclerView.setVisibility(View.VISIBLE); |
|||
mSwipeRefreshLayout.setEnabled(true); |
|||
} |
|||
} |
|||
return true; |
|||
} else { |
|||
if (mEmptyView.getVisibility() == View.GONE) { |
|||
mEmptyView.setText(R.string.network_connection_needed); |
|||
mEmptyView.setVisibility(View.VISIBLE); |
|||
mRecyclerView.setVisibility(View.GONE); |
|||
mSwipeRefreshLayout.setEnabled(false); |
|||
} |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
private final BroadcastReceiver networkReceiver = new BroadcastReceiver() { |
|||
@Override |
|||
public void onReceive(Context context, Intent intent) { |
|||
String action = intent.getAction(); |
|||
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) |
|||
if (updateEmptyView()) |
|||
updateDisplay(); |
|||
} |
|||
}; |
|||
} |
|||
Loading…
Reference in new issue