Audinaut-subsonic-app-android/app/src/main/java/net/nullsum/audinaut/fragments/SelectDirectoryFragment.java

628 lines
24 KiB
Java
Raw Normal View History

2017-01-09 08:01:12 +01:00
package net.nullsum.audinaut.fragments;
2016-12-18 18:41:30 +01:00
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
2018-03-25 03:28:28 +02:00
2017-01-09 08:01:12 +01:00
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.adapter.AlphabeticalAlbumAdapter;
import net.nullsum.audinaut.adapter.EntryGridAdapter;
2018-03-25 03:28:28 +02:00
import net.nullsum.audinaut.adapter.EntryInfiniteGridAdapter;
2017-01-09 08:01:12 +01:00
import net.nullsum.audinaut.adapter.SectionAdapter;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.service.CachedMusicService;
import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
import net.nullsum.audinaut.service.OfflineException;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.LoadingTask;
import net.nullsum.audinaut.util.Pair;
import net.nullsum.audinaut.util.TabBackgroundTask;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.FastScroller;
import net.nullsum.audinaut.view.UpdateView;
2016-12-18 18:41:30 +01:00
2018-03-25 03:28:28 +02:00
import java.io.Serializable;
2016-12-18 18:41:30 +01:00
import java.util.ArrayList;
2018-03-25 03:28:28 +02:00
import java.util.Collections;
import java.util.List;
2016-12-18 18:41:30 +01:00
2017-01-09 08:01:12 +01:00
import static net.nullsum.audinaut.domain.MusicDirectory.Entry;
2016-12-18 18:41:30 +01:00
public class SelectDirectoryFragment extends SubsonicFragment implements SectionAdapter.OnItemClickedListener<Entry> {
2018-03-25 03:28:28 +02:00
private String id;
private String name;
private Entry directory;
private String playlistId;
private String playlistName;
private boolean playlistOwner;
private String albumListType;
private String albumListExtra;
private int albumListSize;
private boolean refreshListing = false;
private boolean restoredInstance = false;
private boolean lookupParent = false;
private boolean largeAlbums = false;
private boolean topTracks = false;
private String lookupEntry;
2018-03-24 20:25:12 +01:00
private RecyclerView recyclerView;
private FastScroller fastScroller;
private EntryGridAdapter entryGridAdapter;
private List<Entry> albums;
private List<Entry> entries;
private LoadTask currentTask;
public SelectDirectoryFragment() {
super();
}
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
2018-03-25 03:28:28 +02:00
if (bundle != null) {
2018-03-24 20:25:12 +01:00
entries = (List<Entry>) bundle.getSerializable(Constants.FRAGMENT_LIST);
albums = (List<Entry>) bundle.getSerializable(Constants.FRAGMENT_LIST2);
2018-03-25 03:28:28 +02:00
if (albums == null) {
2018-03-24 20:25:12 +01:00
albums = new ArrayList<>();
}
restoredInstance = true;
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(Constants.FRAGMENT_LIST, (Serializable) entries);
outState.putSerializable(Constants.FRAGMENT_LIST2, (Serializable) albums);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
Bundle args = getArguments();
2018-03-25 03:28:28 +02:00
if (args != null) {
2018-03-24 20:25:12 +01:00
id = args.getString(Constants.INTENT_EXTRA_NAME_ID);
name = args.getString(Constants.INTENT_EXTRA_NAME_NAME);
directory = (Entry) args.getSerializable(Constants.INTENT_EXTRA_NAME_DIRECTORY);
playlistId = args.getString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID);
playlistName = args.getString(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME);
playlistOwner = args.getBoolean(Constants.INTENT_EXTRA_NAME_PLAYLIST_OWNER, false);
albumListType = args.getString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE);
albumListExtra = args.getString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_EXTRA);
albumListSize = args.getInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0);
refreshListing = args.getBoolean(Constants.INTENT_EXTRA_REFRESH_LISTINGS);
artist = args.getBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, false);
lookupEntry = args.getString(Constants.INTENT_EXTRA_SEARCH_SONG);
topTracks = args.getBoolean(Constants.INTENT_EXTRA_TOP_TRACKS);
String childId = args.getString(Constants.INTENT_EXTRA_NAME_CHILD_ID);
2018-03-25 03:28:28 +02:00
if (childId != null) {
2018-03-24 20:25:12 +01:00
id = childId;
lookupParent = true;
}
2018-03-25 03:28:28 +02:00
if (entries == null) {
2018-03-24 20:25:12 +01:00
entries = (List<Entry>) args.getSerializable(Constants.FRAGMENT_LIST);
albums = (List<Entry>) args.getSerializable(Constants.FRAGMENT_LIST2);
2018-03-25 03:28:28 +02:00
if (albums == null) {
albums = new ArrayList<>();
2018-03-24 20:25:12 +01:00
}
}
}
rootView = inflater.inflate(R.layout.abstract_recycler_fragment, container, false);
2018-03-25 03:28:28 +02:00
refreshLayout = rootView.findViewById(R.id.refresh_layout);
2018-03-24 20:25:12 +01:00
refreshLayout.setOnRefreshListener(this);
2018-03-25 03:28:28 +02:00
if (Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_LARGE_ALBUM_ART, true)) {
2018-03-24 20:25:12 +01:00
largeAlbums = true;
}
2018-03-25 03:28:28 +02:00
recyclerView = rootView.findViewById(R.id.fragment_recycler);
2018-03-24 20:25:12 +01:00
recyclerView.setHasFixedSize(true);
2018-03-25 03:28:28 +02:00
fastScroller = rootView.findViewById(R.id.fragment_fast_scroller);
2018-03-24 20:25:12 +01:00
setupScrollList(recyclerView);
setupLayoutManager(recyclerView, largeAlbums);
2018-03-25 03:28:28 +02:00
if (entries == null) {
if (primaryFragment || secondaryFragment) {
2018-03-24 20:25:12 +01:00
load(false);
} else {
invalidated = true;
}
} else {
2016-12-18 18:41:30 +01:00
finishLoading();
2018-03-24 20:25:12 +01:00
}
2018-03-25 03:28:28 +02:00
if (name != null) {
2018-03-24 20:25:12 +01:00
setTitle(name);
}
return rootView;
}
@Override
public void setIsOnlyVisible(boolean isOnlyVisible) {
boolean update = this.isOnlyVisible != isOnlyVisible;
super.setIsOnlyVisible(isOnlyVisible);
2018-03-25 03:28:28 +02:00
if (update && entryGridAdapter != null) {
2018-03-24 20:25:12 +01:00
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
2018-03-25 03:28:28 +02:00
if (layoutManager instanceof GridLayoutManager) {
2018-03-24 20:25:12 +01:00
((GridLayoutManager) layoutManager).setSpanCount(getRecyclerColumnCount());
}
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
2018-03-25 03:28:28 +02:00
if (albumListType != null) {
2018-03-24 20:25:12 +01:00
menuInflater.inflate(R.menu.select_album_list, menu);
2018-03-25 03:28:28 +02:00
} else if (artist) {
2018-03-24 20:25:12 +01:00
menuInflater.inflate(R.menu.select_album, menu);
} else {
2018-03-25 03:28:28 +02:00
if (Util.isOffline(context)) {
2016-12-18 18:41:30 +01:00
menuInflater.inflate(R.menu.select_song_offline, menu);
2018-03-25 03:28:28 +02:00
} else {
2016-12-18 18:41:30 +01:00
menuInflater.inflate(R.menu.select_song, menu);
2018-03-25 03:28:28 +02:00
if (playlistId == null || !playlistOwner) {
2016-12-18 18:41:30 +01:00
menu.removeItem(R.id.menu_remove_playlist);
}
}
2018-03-24 20:25:12 +01:00
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_remove_playlist:
removeFromPlaylist(playlistId, playlistName, getSelectedIndexes());
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView updateView, Entry entry) {
onCreateContextMenuSupport(menu, menuInflater, updateView, entry);
2018-03-25 03:28:28 +02:00
if (!Util.isOffline(context) && (playlistId == null || !playlistOwner)) {
2018-03-24 20:25:12 +01:00
menu.removeItem(R.id.song_menu_remove_playlist);
}
recreateContextMenu(menu);
}
2018-03-25 03:28:28 +02:00
2018-03-24 20:25:12 +01:00
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Entry> updateView, Entry entry) {
2018-03-25 03:28:28 +02:00
if (onContextItemSelected(menuItem, entry)) {
2018-03-24 20:25:12 +01:00
return true;
}
switch (menuItem.getItemId()) {
case R.id.song_menu_remove_playlist:
2018-03-25 03:28:28 +02:00
removeFromPlaylist(playlistId, playlistName, Collections.singletonList(entries.indexOf(entry)));
2018-03-24 20:25:12 +01:00
break;
}
return true;
}
@Override
public void onItemClicked(UpdateView<Entry> updateView, Entry entry) {
if (entry.isDirectory()) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, entry.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, entry.getTitle());
args.putSerializable(Constants.INTENT_EXTRA_NAME_DIRECTORY, entry);
if ("newest".equals(albumListType)) {
args.putBoolean(Constants.INTENT_EXTRA_REFRESH_LISTINGS, true);
}
2018-03-25 03:28:28 +02:00
if (!entry.isAlbum()) {
2018-03-24 20:25:12 +01:00
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
}
fragment.setArguments(args);
2018-03-25 03:28:28 +02:00
replaceFragment(fragment);
2018-03-24 20:25:12 +01:00
} else {
onSongPress(entries, entry, albumListType == null);
}
}
@Override
protected void refresh(boolean refresh) {
load(refresh);
}
@Override
protected boolean isShowArtistEnabled() {
return albumListType != null;
}
private void load(boolean refresh) {
2018-03-25 03:28:28 +02:00
if (refreshListing) {
2018-03-24 20:25:12 +01:00
refresh = true;
}
2018-03-25 03:28:28 +02:00
if (currentTask != null) {
2018-03-24 20:25:12 +01:00
currentTask.cancel();
}
recyclerView.setVisibility(View.INVISIBLE);
if (playlistId != null) {
getPlaylist(playlistId, playlistName, refresh);
} else if (albumListType != null) {
getAlbumList(albumListType, albumListSize, refresh);
} else {
2016-12-18 18:41:30 +01:00
getMusicDirectory(id, name, refresh);
2018-03-24 20:25:12 +01:00
}
}
private void getMusicDirectory(final String id, final String name, final boolean refresh) {
setTitle(name);
2018-03-25 03:28:28 +02:00
new LoadTask() {
2018-03-24 20:25:12 +01:00
@Override
protected MusicDirectory load(MusicService service) throws Exception {
MusicDirectory dir = getMusicDirectory(id, name, refresh, service, this);
2018-03-25 03:28:28 +02:00
if (lookupParent && dir.getParent() != null) {
2018-03-24 20:25:12 +01:00
dir = getMusicDirectory(dir.getParent(), name, refresh, service, this);
// Update the fragment pointers so other stuff works correctly
SelectDirectoryFragment.this.id = dir.getId();
SelectDirectoryFragment.this.name = dir.getName();
2018-03-25 03:28:28 +02:00
} else if (id != null && directory == null && dir.getParent() != null && !artist) {
2018-03-24 20:25:12 +01:00
MusicDirectory parentDir = getMusicDirectory(dir.getParent(), name, refresh, true, service, this);
2018-03-25 03:28:28 +02:00
for (Entry child : parentDir.getChildren()) {
if (id.equals(child.getId())) {
2018-03-24 20:25:12 +01:00
directory = child;
break;
}
}
}
return dir;
}
@Override
protected void done(Pair<MusicDirectory, Boolean> result) {
SelectDirectoryFragment.this.name = result.getFirst().getName();
setTitle(SelectDirectoryFragment.this.name);
super.done(result);
}
}.execute();
}
private void getPlaylist(final String playlistId, final String playlistName, final boolean refresh) {
setTitle(playlistName);
2018-03-25 03:28:28 +02:00
new LoadTask() {
2018-03-24 20:25:12 +01:00
@Override
protected MusicDirectory load(MusicService service) throws Exception {
return service.getPlaylist(refresh, playlistId, playlistName, context, this);
}
}.execute();
}
private void getAlbumList(final String albumListType, final int size, final boolean refresh) {
if ("random".equals(albumListType)) {
setTitle(R.string.main_albums_random);
} else if ("recent".equals(albumListType)) {
setTitle(R.string.main_albums_recent);
} else if ("frequent".equals(albumListType)) {
setTitle(R.string.main_albums_frequent);
2018-03-25 03:28:28 +02:00
} else if ("genres".equals(albumListType) || "years".equals(albumListType)) {
2018-03-24 20:25:12 +01:00
setTitle(albumListExtra);
2018-03-25 03:28:28 +02:00
} else if ("alphabeticalByName".equals(albumListType)) {
2018-03-24 20:25:12 +01:00
setTitle(R.string.main_albums_alphabetical);
2018-03-25 03:28:28 +02:00
}
if (MainFragment.SONGS_NEWEST.equals(albumListType)) {
2018-03-24 20:25:12 +01:00
setTitle(R.string.main_songs_newest);
} else if (MainFragment.SONGS_TOP_PLAYED.equals(albumListType)) {
setTitle(R.string.main_songs_top_played);
} else if (MainFragment.SONGS_RECENT.equals(albumListType)) {
setTitle(R.string.main_songs_recent);
} else if (MainFragment.SONGS_FREQUENT.equals(albumListType)) {
setTitle(R.string.main_songs_frequent);
}
2018-03-25 03:28:28 +02:00
new LoadTask() {
2018-03-24 20:25:12 +01:00
@Override
protected MusicDirectory load(MusicService service) throws Exception {
MusicDirectory result;
2018-03-25 03:28:28 +02:00
if ("genres".equals(albumListType) || "years".equals(albumListType)) {
2018-03-24 20:25:12 +01:00
result = service.getAlbumList(albumListType, albumListExtra, size, 0, refresh, context, this);
2018-03-25 03:28:28 +02:00
if (result.getChildrenSize() == 0 && "genres".equals(albumListType)) {
2018-03-24 20:25:12 +01:00
SelectDirectoryFragment.this.albumListType = "genres-songs";
result = service.getSongsByGenre(albumListExtra, size, 0, context, this);
}
2018-03-25 03:28:28 +02:00
} else if ("genres".equals(albumListType) || "genres-songs".equals(albumListType)) {
2018-03-24 20:25:12 +01:00
result = service.getSongsByGenre(albumListExtra, size, 0, context, this);
2018-03-25 03:28:28 +02:00
} else if (albumListType.contains(MainFragment.SONGS_LIST_PREFIX)) {
2018-03-24 20:25:12 +01:00
result = service.getSongList(albumListType, size, 0, context, this);
} else {
result = service.getAlbumList(albumListType, size, 0, refresh, context, this);
}
return result;
}
}.execute();
}
@Override
public SectionAdapter<Entry> getCurrentAdapter() {
return entryGridAdapter;
}
@Override
public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final GridLayoutManager gridLayoutManager) {
return new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int viewType = entryGridAdapter.getItemViewType(position);
2018-03-25 03:28:28 +02:00
if (viewType == EntryGridAdapter.VIEW_TYPE_SONG || viewType == EntryGridAdapter.VIEW_TYPE_HEADER || viewType == EntryInfiniteGridAdapter.VIEW_TYPE_LOADING) {
2018-03-24 20:25:12 +01:00
return gridLayoutManager.getSpanCount();
} else {
return 1;
}
}
};
}
2016-12-18 18:41:30 +01:00
private void finishLoading() {
2018-03-24 20:25:12 +01:00
boolean validData = !entries.isEmpty() || !albums.isEmpty();
2018-03-25 03:28:28 +02:00
if (!validData) {
2018-03-24 20:25:12 +01:00
setEmpty(true);
}
2018-03-25 03:28:28 +02:00
if (validData) {
2018-03-24 20:25:12 +01:00
recyclerView.setVisibility(View.VISIBLE);
}
2018-03-25 03:28:28 +02:00
if (albumListType == null) {
2018-03-24 20:25:12 +01:00
entryGridAdapter = new EntryGridAdapter(context, entries, getImageLoader(), largeAlbums);
entryGridAdapter.setRemoveFromPlaylist(playlistId != null);
} else {
2018-03-25 03:28:28 +02:00
if ("alphabeticalByName".equals(albumListType)) {
2018-03-24 20:25:12 +01:00
entryGridAdapter = new AlphabeticalAlbumAdapter(context, entries, getImageLoader(), largeAlbums);
} else {
entryGridAdapter = new EntryInfiniteGridAdapter(context, entries, getImageLoader(), largeAlbums);
}
// Setup infinite loading based on scrolling
final EntryInfiniteGridAdapter infiniteGridAdapter = (EntryInfiniteGridAdapter) entryGridAdapter;
infiniteGridAdapter.setData(albumListType, albumListExtra, albumListSize);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
int totalItemCount = layoutManager.getItemCount();
int lastVisibleItem;
2018-03-25 03:28:28 +02:00
if (layoutManager instanceof GridLayoutManager) {
2018-03-24 20:25:12 +01:00
lastVisibleItem = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
2018-03-25 03:28:28 +02:00
} else if (layoutManager instanceof LinearLayoutManager) {
2018-03-24 20:25:12 +01:00
lastVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
} else {
return;
}
2018-03-25 03:28:28 +02:00
if (totalItemCount > 0 && lastVisibleItem >= totalItemCount - 2) {
2018-03-24 20:25:12 +01:00
infiniteGridAdapter.loadMore();
}
}
});
}
entryGridAdapter.setOnItemClickedListener(this);
// Always show artist if this is not a artist we are viewing
2018-03-25 03:28:28 +02:00
if (!artist) {
entryGridAdapter.setShowArtist();
2018-03-24 20:25:12 +01:00
}
2018-03-25 03:28:28 +02:00
if (topTracks) {
entryGridAdapter.setShowAlbum();
2018-03-24 20:25:12 +01:00
}
int scrollToPosition = -1;
2018-03-25 03:28:28 +02:00
if (lookupEntry != null) {
for (int i = 0; i < entries.size(); i++) {
if (lookupEntry.equals(entries.get(i).getTitle())) {
2018-03-24 20:25:12 +01:00
scrollToPosition = i;
entryGridAdapter.addSelected(entries.get(i));
lookupEntry = null;
break;
}
}
}
recyclerView.setAdapter(entryGridAdapter);
fastScroller.attachRecyclerView(recyclerView);
context.supportInvalidateOptionsMenu();
2018-03-25 03:28:28 +02:00
if (scrollToPosition != -1) {
2018-03-24 20:25:12 +01:00
recyclerView.scrollToPosition(scrollToPosition);
}
Bundle args = getArguments();
boolean playAll = args.getBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, false);
if (playAll && !restoredInstance) {
playAll(args.getBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE, false), false, false);
}
}
@Override
protected void playNow(final boolean shuffle, final boolean append, final boolean playNext) {
List<Entry> songs = getSelectedEntries();
2018-03-25 03:28:28 +02:00
if (!songs.isEmpty()) {
download(songs, append, !append, playNext, shuffle);
2018-03-24 20:25:12 +01:00
entryGridAdapter.clearSelected();
} else {
playAll(shuffle, append, playNext);
}
}
2018-03-25 03:28:28 +02:00
2018-03-24 20:25:12 +01:00
private void playAll(final boolean shuffle, final boolean append, final boolean playNext) {
boolean hasSubFolders = albums != null && !albums.isEmpty();
if (hasSubFolders && id != null) {
downloadRecursively(id, false, append, !append, shuffle, false, playNext);
2018-03-25 03:28:28 +02:00
} else if (hasSubFolders && albumListType != null) {
2018-03-24 20:25:12 +01:00
downloadRecursively(albums, shuffle, append, playNext);
} else {
2018-03-25 03:28:28 +02:00
download(entries, append, !append, playNext, shuffle);
2018-03-24 20:25:12 +01:00
}
}
private List<Integer> getSelectedIndexes() {
List<Entry> selected = entryGridAdapter.getSelected();
2018-03-25 03:28:28 +02:00
List<Integer> indexes = new ArrayList<>();
2018-03-24 20:25:12 +01:00
2018-03-25 03:28:28 +02:00
for (Entry entry : selected) {
2018-03-24 20:25:12 +01:00
indexes.add(entries.indexOf(entry));
}
return indexes;
}
@Override
protected void downloadBackground(final boolean save) {
List<Entry> songs = getSelectedEntries();
2018-03-25 03:28:28 +02:00
if (playlistId != null) {
2018-03-24 20:25:12 +01:00
songs = entries;
}
2018-03-25 03:28:28 +02:00
if (songs.isEmpty()) {
2018-03-24 20:25:12 +01:00
// Get both songs and albums
2018-03-25 03:28:28 +02:00
downloadRecursively(id, save, false, false, false, true, false);
2018-03-24 20:25:12 +01:00
} else {
downloadBackground(save, songs);
}
}
2018-03-25 03:28:28 +02:00
2018-03-24 20:25:12 +01:00
@Override
2018-03-25 03:28:28 +02:00
void downloadBackground(final boolean save, final List<Entry> entries) {
2018-03-24 20:25:12 +01:00
if (getDownloadService() == null) {
return;
}
warnIfStorageUnavailable();
2018-03-25 03:28:28 +02:00
new RecursiveLoader(context) {
2018-03-24 20:25:12 +01:00
@Override
protected Boolean doInBackground() throws Throwable {
2018-03-25 03:28:28 +02:00
getSongsRecursively(entries);
2018-03-24 20:25:12 +01:00
getDownloadService().downloadBackground(songs, save);
return null;
}
@Override
protected void done(Boolean result) {
Util.toast(context, context.getResources().getQuantityString(R.plurals.select_album_n_songs_downloading, songs.size(), songs.size()));
}
};
}
@Override
2018-03-25 03:28:28 +02:00
void download(List<Entry> entries, boolean append, boolean autoplay, boolean playNext, boolean shuffle) {
download(entries, append, autoplay, playNext, shuffle, playlistName, playlistId);
2018-03-24 20:25:12 +01:00
}
@Override
protected void delete() {
List<Entry> songs = getSelectedEntries();
2018-03-25 03:28:28 +02:00
if (songs.isEmpty()) {
for (Entry entry : entries) {
if (entry.isDirectory()) {
2018-03-24 20:25:12 +01:00
deleteRecursively(entry);
} else {
songs.add(entry);
}
}
}
if (getDownloadService() != null) {
getDownloadService().delete(songs);
}
}
2018-03-25 03:28:28 +02:00
private void removeFromPlaylist(final String id, final String name, final List<Integer> indexes) {
2018-03-24 20:25:12 +01:00
new LoadingTask<Void>(context, true) {
@Override
protected Void doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
musicService.removeFromPlaylist(id, indexes, context, null);
return null;
}
@Override
protected void done(Void result) {
2018-03-25 03:28:28 +02:00
for (Integer index : indexes) {
2018-03-24 20:25:12 +01:00
entryGridAdapter.removeAt(index);
}
Util.toast(context, context.getResources().getString(R.string.removed_playlist, indexes.size(), name));
}
@Override
protected void error(Throwable error) {
String msg;
if (error instanceof OfflineException) {
msg = getErrorMessage(error);
} else {
msg = context.getResources().getString(R.string.updated_playlist_error, name) + " " + getErrorMessage(error);
}
Util.toast(context, msg, false);
}
}.execute();
}
2018-03-25 03:28:28 +02:00
private abstract class LoadTask extends TabBackgroundTask<Pair<MusicDirectory, Boolean>> {
2018-03-24 20:25:12 +01:00
2018-03-25 03:28:28 +02:00
public LoadTask() {
super(SelectDirectoryFragment.this);
2018-03-24 20:25:12 +01:00
2018-03-25 03:28:28 +02:00
currentTask = this;
}
2018-03-24 20:25:12 +01:00
2018-03-25 03:28:28 +02:00
protected abstract MusicDirectory load(MusicService service) throws Exception;
2018-03-24 20:25:12 +01:00
2018-03-25 03:28:28 +02:00
@Override
protected Pair<MusicDirectory, Boolean> doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
MusicDirectory dir = load(musicService);
2018-03-24 20:25:12 +01:00
2018-03-25 03:28:28 +02:00
albums = dir.getChildren(true, false);
entries = dir.getChildren();
2018-03-24 20:25:12 +01:00
2018-03-25 03:28:28 +02:00
// This isn't really an artist if no albums on it!
if (albums.size() == 0) {
artist = false;
2018-03-24 20:25:12 +01:00
}
2018-03-25 03:28:28 +02:00
return new Pair<>(dir, true);
2018-03-24 20:25:12 +01:00
}
2018-03-25 03:28:28 +02:00
@Override
protected void done(Pair<MusicDirectory, Boolean> result) {
finishLoading();
currentTask = null;
2018-03-24 20:25:12 +01:00
}
2018-03-25 03:28:28 +02:00
@Override
public void updateCache(int changeCode) {
if (entryGridAdapter != null && changeCode == CachedMusicService.CACHE_UPDATE_LIST) {
entryGridAdapter.notifyDataSetChanged();
2018-03-24 20:25:12 +01:00
}
}
}
2016-12-18 18:41:30 +01:00
}