ultrasonic-app-subsonic-and.../src/com/thejoshwa/ultrasonic/androidapp/activity/SelectAlbumActivity.java

873 lines
35 KiB
Java
Raw Normal View History

/*
This file is part of Subsonic.
Subsonic 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 3 of the License, or
(at your option) any later version.
Subsonic 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 Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2009 (C) Sindre Mehus
*/
2013-04-06 21:47:24 +02:00
package com.thejoshwa.ultrasonic.androidapp.activity;
import android.content.Intent;
2013-04-27 11:52:25 +02:00
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
2013-04-27 11:52:25 +02:00
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
2013-04-06 21:47:24 +02:00
import com.thejoshwa.ultrasonic.androidapp.R;
import com.thejoshwa.ultrasonic.androidapp.domain.MusicDirectory;
import com.thejoshwa.ultrasonic.androidapp.service.DownloadFile;
import com.thejoshwa.ultrasonic.androidapp.service.MusicService;
import com.thejoshwa.ultrasonic.androidapp.service.MusicServiceFactory;
import com.thejoshwa.ultrasonic.androidapp.util.AlbumHeader;
2013-04-06 21:47:24 +02:00
import com.thejoshwa.ultrasonic.androidapp.util.Constants;
import com.thejoshwa.ultrasonic.androidapp.util.EntryByDiscAndTrackComparator;
2013-04-06 21:47:24 +02:00
import com.thejoshwa.ultrasonic.androidapp.util.Pair;
import com.thejoshwa.ultrasonic.androidapp.util.TabActivityBackgroundTask;
import com.thejoshwa.ultrasonic.androidapp.util.Util;
import com.thejoshwa.ultrasonic.androidapp.view.EntryAdapter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
2013-04-27 11:52:25 +02:00
public class SelectAlbumActivity extends SubsonicTabActivity {
2013-04-27 11:52:25 +02:00
private PullToRefreshListView refreshAlbumListView;
private ListView albumListView;
private View header;
2013-04-27 11:52:25 +02:00
private View albumButtons;
private View emptyView;
2013-04-27 11:52:25 +02:00
private ImageView selectButton;
private ImageView playNowButton;
private ImageView playNextButton;
2013-04-27 11:52:25 +02:00
private ImageView playLastButton;
private ImageView pinButton;
private ImageView unpinButton;
2013-05-26 08:46:37 +02:00
private ImageView downloadButton;
2013-04-27 11:52:25 +02:00
private ImageView deleteButton;
private ImageView moreButton;
private boolean playAllButtonVisible;
private MenuItem playAllButton;
private boolean showHeader = true;
private Random random = new Random();
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_album);
2013-04-27 11:52:25 +02:00
albumButtons = findViewById(R.id.menu_album);
refreshAlbumListView = (PullToRefreshListView) findViewById(R.id.select_album_entries);
albumListView = refreshAlbumListView.getRefreshableView();
refreshAlbumListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
new GetDataTask().execute();
}
});
2013-04-27 11:52:25 +02:00
header = LayoutInflater.from(this).inflate(R.layout.select_album_header, albumListView, false);
2013-04-27 11:52:25 +02:00
albumListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
albumListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position >= 0) {
MusicDirectory.Entry entry = (MusicDirectory.Entry) parent.getItemAtPosition(position);
if (entry != null && entry.isDirectory()) {
Intent intent = new Intent(SelectAlbumActivity.this, SelectAlbumActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ID, entry.getId());
intent.putExtra(Constants.INTENT_EXTRA_NAME_IS_ALBUM, entry.isDirectory());
intent.putExtra(Constants.INTENT_EXTRA_NAME_NAME, entry.getTitle());
Util.startActivityWithoutTransition(SelectAlbumActivity.this, intent);
} else if (entry != null && entry.isVideo()) {
playVideo(entry);
} else {
enableButtons();
}
}
}
});
2013-04-27 11:52:25 +02:00
selectButton = (ImageView) findViewById(R.id.select_album_select);
playNowButton = (ImageView) findViewById(R.id.select_album_play_now);
playNextButton = (ImageView) findViewById(R.id.select_album_play_next);
2013-04-27 11:52:25 +02:00
playLastButton = (ImageView) findViewById(R.id.select_album_play_last);
pinButton = (ImageView) findViewById(R.id.select_album_pin);
unpinButton = (ImageView) findViewById(R.id.select_album_unpin);
2013-05-26 08:46:37 +02:00
downloadButton = (ImageView) findViewById(R.id.select_album_download);
2013-04-27 11:52:25 +02:00
deleteButton = (ImageView) findViewById(R.id.select_album_delete);
moreButton = (ImageView) findViewById(R.id.select_album_more);
emptyView = findViewById(R.id.select_album_empty);
selectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectAllOrNone();
}
});
playNowButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playNow(false, false);
}
});
playNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
download(true, false, false, true, false, getSelectedSongs(albumListView));
selectAll(false, false);
}
});
playLastButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playNow(false, true);
}
});
pinButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
downloadBackground(true);
selectAll(false, false);
}
});
unpinButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
unpin();
selectAll(false, false);
}
});
2013-05-26 08:46:37 +02:00
downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
downloadBackground(false);
selectAll(false, false);
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
delete();
selectAll(false, false);
}
});
2013-04-27 11:52:25 +02:00
registerForContextMenu(albumListView);
enableButtons();
String id = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_ID);
boolean isAlbum = getIntent().getBooleanExtra(Constants.INTENT_EXTRA_NAME_IS_ALBUM, false);
boolean isArtist = getIntent().getBooleanExtra(Constants.INTENT_EXTRA_NAME_ARTIST, false);
String name = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_NAME);
String playlistId = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID);
String playlistName = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME);
String albumListType = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE);
2013-04-20 23:58:59 +02:00
String genreName = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_GENRE_NAME);
int albumListTitle = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TITLE, 0);
int getStarredTracks = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_STARRED, 0);
int getVideos = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_VIDEOS, 0);
2013-04-20 23:58:59 +02:00
int getRandomTracks = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_RANDOM, 0);
int albumListSize = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0);
int albumListOffset = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0);
2013-04-27 11:52:25 +02:00
View browseMenuItem = findViewById(R.id.menu_browse);
menuDrawer.setActiveView(browseMenuItem);
if (playlistId != null) {
getPlaylist(playlistId, playlistName);
} else if (albumListType != null) {
2013-04-20 23:58:59 +02:00
getAlbumList(albumListType, albumListTitle, albumListSize, albumListOffset);
} else if (genreName != null) {
getSongsForGenre(genreName, albumListSize, albumListOffset);
} else if (getStarredTracks != 0) {
getStarred();
} else if (getVideos != 0) {
getVideos();
2013-04-20 23:58:59 +02:00
} else if (getRandomTracks != 0) {
getRandom(albumListSize);
} else {
if (!Util.isOffline(SelectAlbumActivity.this) && Util.getShouldUseId3Tags(SelectAlbumActivity.this)) {
if (isAlbum) {
getAlbum(id, name);
} else {
getArtist(id, name);
}
} else {
getMusicDirectory(id, name);
}
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
playAllButton = menu.findItem(R.id.select_album_play_all);
if (playAllButton != null) {
playAllButton.setVisible(playAllButtonVisible);
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.select_album, menu);
super.onCreateOptionsMenu(menu);
return true;
}
private void playNow(final boolean shuffle, final boolean append) {
2013-06-07 10:47:57 +02:00
List<MusicDirectory.Entry> selectedSongs = getSelectedSongs(albumListView);
if (selectedSongs.size() > 0) {
download(append, false, !append, false, shuffle, selectedSongs);
selectAll(false, false);
}
else {
playAll(shuffle, append);
}
}
private void playAll() {
playAll(false, false);
}
private void playAll(final boolean shuffle, final boolean append) {
boolean hasSubFolders = false;
2013-04-27 11:52:25 +02:00
for (int i = 0; i < albumListView.getCount(); i++) {
MusicDirectory.Entry entry = (MusicDirectory.Entry) albumListView.getItemAtPosition(i);
if (entry != null && entry.isDirectory()) {
hasSubFolders = true;
break;
}
}
boolean isArtist = getIntent().getBooleanExtra(Constants.INTENT_EXTRA_NAME_ARTIST, false);
String id = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_ID);
if (hasSubFolders && id != null) {
downloadRecursively(id, false, append, !append, shuffle, false, false, false, isArtist);
} else {
selectAll(true, false);
download(append, false, !append, false, shuffle, getSelectedSongs(albumListView));
selectAll(false, false);
}
}
private List<MusicDirectory.Entry> getSelectedSongs(ListView albumListView) {
List<MusicDirectory.Entry> songs = new ArrayList<MusicDirectory.Entry>(10);
if (albumListView != null) {
int count = albumListView.getCount();
for (int i = 0; i < count; i++) {
if (albumListView.isItemChecked(i)) {
songs.add((MusicDirectory.Entry) albumListView.getItemAtPosition(i));
}
}
}
return songs;
}
private void refresh() {
finish();
Intent intent = getIntent();
intent.putExtra(Constants.INTENT_EXTRA_NAME_REFRESH, true);
Util.startActivityWithoutTransition(this, intent);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
2013-04-27 11:52:25 +02:00
MusicDirectory.Entry entry = (MusicDirectory.Entry) albumListView.getItemAtPosition(info.position);
if (entry != null && entry.isDirectory()) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.select_album_context, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem menuItem) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo();
if (info == null) {
return true;
}
2013-04-27 11:52:25 +02:00
MusicDirectory.Entry entry = (MusicDirectory.Entry) albumListView.getItemAtPosition(info.position);
if (entry == null) {
return true;
}
switch (menuItem.getItemId()) {
case R.id.album_menu_play_now:
2013-07-18 08:01:05 +02:00
downloadRecursively(entry.getId(), false, false, true, false, false, false, false, false);
break;
case R.id.album_menu_play_next:
2013-07-18 08:01:05 +02:00
downloadRecursively(entry.getId(), false, false, false, false, false, true, false, false);
break;
case R.id.album_menu_play_last:
2013-07-18 08:01:05 +02:00
downloadRecursively(entry.getId(), false, true, false, false, false, false, false, false);
break;
case R.id.album_menu_pin:
2013-07-18 08:01:05 +02:00
downloadRecursively(entry.getId(), true, true, false, false, false, false, false, false);
break;
case R.id.album_menu_unpin:
2013-07-18 08:01:05 +02:00
downloadRecursively(entry.getId(), false, false, false, false, false, false, true, false);
2013-05-26 08:46:37 +02:00
break;
case R.id.album_menu_download:
2013-07-18 08:01:05 +02:00
downloadRecursively(entry.getId(), false, false, false, false, true, false, false, false);
2013-05-26 08:46:37 +02:00
break;
case R.id.select_album_play_all:
playAll();
break;
default:
return super.onContextItemSelected(menuItem);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
menuDrawer.toggleMenu();
return true;
case R.id.main_shuffle:
Intent intent1 = new Intent(this, DownloadActivity.class);
intent1.putExtra(Constants.INTENT_EXTRA_NAME_SHUFFLE, true);
Util.startActivityWithoutTransition(this, intent1);
return true;
case R.id.select_album_play_all:
playAll();
return true;
}
return false;
}
private void getMusicDirectory(final String id, final String name) {
setActionBarSubtitle(name);
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
boolean refresh = getIntent().getBooleanExtra(Constants.INTENT_EXTRA_NAME_REFRESH, false);
return service.getMusicDirectory(id, name, refresh, SelectAlbumActivity.this, this);
}
}.execute();
}
private void getArtist(final String id, final String name) {
setActionBarSubtitle(name);
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
boolean refresh = getIntent().getBooleanExtra(Constants.INTENT_EXTRA_NAME_REFRESH, false);
return service.getArtist(id, name, refresh, SelectAlbumActivity.this, this);
}
}.execute();
}
private void getAlbum(final String id, final String name) {
setActionBarSubtitle(name);
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
boolean refresh = getIntent().getBooleanExtra(Constants.INTENT_EXTRA_NAME_REFRESH, false);
return service.getAlbum(id, name, refresh, SelectAlbumActivity.this, this);
}
}.execute();
}
2013-04-20 23:58:59 +02:00
private void getSongsForGenre(final String genre, final int count, final int offset) {
setActionBarSubtitle(genre);
2013-04-20 23:58:59 +02:00
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
return service.getSongsByGenre(genre, count, offset, SelectAlbumActivity.this, this);
}
@Override
protected void done(Pair<MusicDirectory, Boolean> result) {
// Hide more button when results are less than album list size
if (result.getFirst().getChildren().size() < getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0)) {
moreButton.setVisibility(View.GONE);
} else {
moreButton.setVisibility(View.VISIBLE);
}
moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SelectAlbumActivity.this, SelectAlbumActivity.class);
String genre = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_GENRE_NAME);
int size = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0);
int offset = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0) + size;
intent.putExtra(Constants.INTENT_EXTRA_NAME_GENRE_NAME, genre);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, size);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, offset);
Util.startActivityWithoutTransition(SelectAlbumActivity.this, intent);
}
});
super.done(result);
}
}.execute();
}
private void getStarred() {
setActionBarSubtitle(R.string.main_songs_starred);
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
if (Util.getShouldUseId3Tags(SelectAlbumActivity.this)) {
return Util.getSongsFromSearchResult(service.getStarred2(SelectAlbumActivity.this, this));
} else {
return Util.getSongsFromSearchResult(service.getStarred(SelectAlbumActivity.this, this));
}
}
}.execute();
}
2013-04-20 23:58:59 +02:00
private void getVideos() {
showHeader = false;
setActionBarSubtitle(R.string.main_videos);
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
2013-06-04 10:13:47 +02:00
boolean refresh = getIntent().getBooleanExtra(Constants.INTENT_EXTRA_NAME_REFRESH, false);
return service.getVideos(refresh, SelectAlbumActivity.this, this);
}
}.execute();
}
2013-04-20 23:58:59 +02:00
private void getRandom(final int size) {
setActionBarSubtitle(R.string.main_songs_random);
2013-04-20 23:58:59 +02:00
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
return service.getRandomSongs(size, SelectAlbumActivity.this, this);
}
}.execute();
}
private void getPlaylist(final String playlistId, final String playlistName) {
setActionBarSubtitle(playlistName);
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
return service.getPlaylist(playlistId, playlistName, SelectAlbumActivity.this, this);
}
}.execute();
}
2013-04-20 23:58:59 +02:00
private void getAlbumList(final String albumListType, final int albumListTitle, final int size, final int offset) {
showHeader = false;
setActionBarSubtitle(albumListTitle);
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
if (Util.getShouldUseId3Tags(SelectAlbumActivity.this)) {
return service.getAlbumList2(albumListType, size, offset, SelectAlbumActivity.this, this);
} else {
return service.getAlbumList(albumListType, size, offset, SelectAlbumActivity.this, this);
}
}
@Override
protected void done(Pair<MusicDirectory, Boolean> result) {
if (!result.getFirst().getChildren().isEmpty()) {
pinButton.setVisibility(View.GONE);
unpinButton.setVisibility(View.GONE);
2013-05-26 08:46:37 +02:00
downloadButton.setVisibility(View.GONE);
deleteButton.setVisibility(View.GONE);
// Hide more button when results are less than album list size
if (result.getFirst().getChildren().size() < getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0)) {
moreButton.setVisibility(View.GONE);
} else {
moreButton.setVisibility(View.VISIBLE);
moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SelectAlbumActivity.this, SelectAlbumActivity.class);
int albumListTitle = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TITLE, 0);
String type = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE);
int size = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0);
int offset = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0) + size;
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TITLE, albumListTitle);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE, type);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, size);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, offset);
Util.startActivityWithoutTransition(SelectAlbumActivity.this, intent);
}
});
}
2013-04-27 11:52:25 +02:00
} else {
moreButton.setVisibility(View.GONE);
}
super.done(result);
}
}.execute();
}
private void selectAllOrNone() {
boolean someUnselected = false;
2013-04-27 11:52:25 +02:00
int count = albumListView.getCount();
for (int i = 0; i < count; i++) {
2013-04-27 11:52:25 +02:00
if (!albumListView.isItemChecked(i) && albumListView.getItemAtPosition(i) instanceof MusicDirectory.Entry) {
someUnselected = true;
break;
}
}
selectAll(someUnselected, true);
}
private void selectAll(boolean selected, boolean toast) {
2013-04-27 11:52:25 +02:00
int count = albumListView.getCount();
int selectedCount = 0;
for (int i = 0; i < count; i++) {
2013-04-27 11:52:25 +02:00
MusicDirectory.Entry entry = (MusicDirectory.Entry) albumListView.getItemAtPosition(i);
if (entry != null && !entry.isDirectory() && !entry.isVideo()) {
2013-04-27 11:52:25 +02:00
albumListView.setItemChecked(i, selected);
selectedCount++;
}
}
// Display toast: N tracks selected / N tracks unselected
if (toast) {
2013-04-27 11:52:25 +02:00
int toastResId = selected ? R.string.select_album_n_selected : R.string.select_album_n_unselected;
Util.toast(this, getString(toastResId, selectedCount));
}
enableButtons();
}
private void enableButtons() {
if (getDownloadService() == null) {
return;
}
List<MusicDirectory.Entry> selection = getSelectedSongs(albumListView);
boolean enabled = !selection.isEmpty();
boolean unpinEnabled = false;
boolean deleteEnabled = false;
2013-04-28 01:01:34 +02:00
int pinnedCount = 0;
2013-05-26 08:46:37 +02:00
for (MusicDirectory.Entry song : selection) {
DownloadFile downloadFile = getDownloadService().forSong(song);
if (downloadFile.isWorkDone()) {
deleteEnabled = true;
}
2013-05-26 08:46:37 +02:00
if (downloadFile.isSaved()) {
2013-04-28 01:01:34 +02:00
pinnedCount++;
unpinEnabled = true;
}
}
2013-04-28 01:01:34 +02:00
playNowButton.setVisibility(enabled ? View.VISIBLE : View.GONE);
playNextButton.setVisibility(enabled ? View.VISIBLE : View.GONE);
2013-04-28 01:01:34 +02:00
playLastButton.setVisibility(enabled ? View.VISIBLE : View.GONE);
pinButton.setVisibility((enabled && !Util.isOffline(this) && selection.size() > pinnedCount) ? View.VISIBLE : View.GONE);
2013-05-26 08:46:37 +02:00
unpinButton.setVisibility(enabled && unpinEnabled ? View.VISIBLE : View.GONE);
downloadButton.setVisibility(enabled && !deleteEnabled && !Util.isOffline(this) ? View.VISIBLE : View.GONE);
deleteButton.setVisibility(enabled && deleteEnabled ? View.VISIBLE : View.GONE);
}
private void downloadBackground(final boolean save) {
List<MusicDirectory.Entry> songs = getSelectedSongs(albumListView);
if (songs.isEmpty()) {
selectAll(true, false);
songs = getSelectedSongs(albumListView);
}
downloadBackground(save, songs);
}
private void downloadBackground(final boolean save, final List<MusicDirectory.Entry> songs) {
if (getDownloadService() == null) {
return;
}
Runnable onValid = new Runnable() {
@Override
public void run() {
warnIfNetworkOrStorageUnavailable();
getDownloadService().downloadBackground(songs, save);
2013-05-26 08:46:37 +02:00
if (save) {
Util.toast(SelectAlbumActivity.this, getResources().getQuantityString(R.plurals.select_album_n_songs_pinned, songs.size(), songs.size()));
} else {
Util.toast(SelectAlbumActivity.this, getResources().getQuantityString(R.plurals.select_album_n_songs_downloaded, songs.size(), songs.size()));
}
}
};
checkLicenseAndTrialPeriod(onValid);
}
private void delete() {
List<MusicDirectory.Entry> songs = getSelectedSongs(albumListView);
if (songs.isEmpty()) {
selectAll(true, false);
songs = getSelectedSongs(albumListView);
}
if (getDownloadService() != null) {
getDownloadService().delete(songs);
}
}
private void unpin() {
if (getDownloadService() != null) {
List<MusicDirectory.Entry> songs = getSelectedSongs(albumListView);
Util.toast(SelectAlbumActivity.this, getResources().getQuantityString(R.plurals.select_album_n_songs_unpinned, songs.size(), songs.size()));
getDownloadService().unpin(songs);
}
}
private abstract class LoadTask extends TabActivityBackgroundTask<Pair<MusicDirectory, Boolean>> {
public LoadTask() {
2013-05-24 10:16:48 +02:00
super(SelectAlbumActivity.this, true);
}
protected abstract MusicDirectory load(MusicService service) throws Exception;
@Override
protected Pair<MusicDirectory, Boolean> doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(SelectAlbumActivity.this);
MusicDirectory dir = load(musicService);
boolean valid = musicService.isLicenseValid(SelectAlbumActivity.this, this);
return new Pair<MusicDirectory, Boolean>(dir, valid);
}
@Override
protected void done(Pair<MusicDirectory, Boolean> result) {
MusicDirectory musicDirectory = result.getFirst();
List<MusicDirectory.Entry> entries = musicDirectory.getChildren();
if (Util.getShouldSortByDisc(SelectAlbumActivity.this)){
Collections.sort(entries, new EntryByDiscAndTrackComparator());
}
String directoryName = musicDirectory.getName();
boolean allVideos = true;
int songCount = 0;
for (MusicDirectory.Entry entry : entries) {
if (!entry.isVideo()) {
allVideos = false;
}
if (!entry.isDirectory()) {
songCount++;
}
}
final int listSize = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0);
2013-04-27 11:52:25 +02:00
if (songCount > 0) {
if(showHeader) {
View header = createHeader(entries, directoryName, songCount);
if (header != null) {
albumListView.addHeaderView(header, null, false);
}
}
2013-04-27 11:52:25 +02:00
pinButton.setVisibility(View.VISIBLE);
unpinButton.setVisibility(View.VISIBLE);
2013-05-26 08:46:37 +02:00
downloadButton.setVisibility(View.VISIBLE);
2013-04-27 11:52:25 +02:00
deleteButton.setVisibility(View.VISIBLE);
selectButton.setVisibility(allVideos ? View.GONE : View.VISIBLE);
playNowButton.setVisibility(View.VISIBLE);
playNextButton.setVisibility(View.VISIBLE);
playLastButton.setVisibility(View.VISIBLE);
2013-04-27 11:52:25 +02:00
if (listSize == 0 || songCount < listSize) {
moreButton.setVisibility(View.GONE);
} else {
moreButton.setVisibility(View.VISIBLE);
if (getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_RANDOM, 0) > 0) {
moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SelectAlbumActivity.this, SelectAlbumActivity.class);
int offset = getIntent().getIntExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0) + listSize;
intent.putExtra(Constants.INTENT_EXTRA_NAME_RANDOM, 1);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, listSize);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, offset);
Util.startActivityWithoutTransition(SelectAlbumActivity.this, intent);
}
});
}
2013-04-27 11:52:25 +02:00
}
} else {
pinButton.setVisibility(View.GONE);
unpinButton.setVisibility(View.GONE);
2013-05-26 08:46:37 +02:00
downloadButton.setVisibility(View.GONE);
2013-04-27 11:52:25 +02:00
deleteButton.setVisibility(View.GONE);
selectButton.setVisibility(View.GONE);
playNowButton.setVisibility(View.GONE);
playNextButton.setVisibility(View.GONE);
2013-04-27 11:52:25 +02:00
playLastButton.setVisibility(View.GONE);
if (listSize == 0 || result.getFirst().getChildren().size() < listSize) {
albumButtons.setVisibility(View.GONE);
} else {
moreButton.setVisibility(View.VISIBLE);
}
}
2013-04-28 01:01:34 +02:00
enableButtons();
boolean isAlbumList = getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE);
playAllButtonVisible = !(isAlbumList || entries.isEmpty()) && !allVideos;
emptyView.setVisibility(entries.isEmpty() ? View.VISIBLE : View.GONE);
if (playAllButton != null) {
playAllButton.setVisible(playAllButtonVisible);
}
2013-04-27 11:52:25 +02:00
albumListView.setAdapter(new EntryAdapter(SelectAlbumActivity.this, getImageLoader(), entries, true));
licenseValid = result.getSecond();
boolean playAll = getIntent().getBooleanExtra(Constants.INTENT_EXTRA_NAME_AUTOPLAY, false);
if (playAll && songCount > 0) {
playAll(getIntent().getBooleanExtra(Constants.INTENT_EXTRA_NAME_SHUFFLE, false), false);
}
}
protected View createHeader(List<MusicDirectory.Entry> entries, String name, int songCount) {
ImageView coverArtView = (ImageView) header.findViewById(R.id.select_album_art);
int artworkSelection = random.nextInt(entries.size());
getImageLoader().loadImage(coverArtView, entries.get(artworkSelection), true, Util.getAlbumImageSize(SelectAlbumActivity.this), false, true);
TextView titleView = (TextView) header.findViewById(R.id.select_album_title);
titleView.setText(name != null ? name : getActionBarSubtitle());
AlbumHeader albumHeader = AlbumHeader.processEntries(SelectAlbumActivity.this, entries);
// Don't show a header if all entries are videos
if (albumHeader.getIsAllVideo()) {
return null;
}
TextView artistView = (TextView) header.findViewById(R.id.select_album_artist);
String artist;
if (albumHeader.getArtists().size() == 1) {
artist = albumHeader.getArtists().iterator().next();
} else if (albumHeader.getGrandParents().size() == 1) {
artist = albumHeader.getGrandParents().iterator().next();
} else {
artist = getResources().getString(R.string.common_various_artists);
}
artistView.setText(artist);
TextView genreView = (TextView) header.findViewById(R.id.select_album_genre);
String genre;
if (albumHeader.getGenres().size() == 1) {
genre = albumHeader.getGenres().iterator().next();
} else {
genre = getResources().getString(R.string.common_multiple_genres);
}
genreView.setText(genre);
TextView songCountView = (TextView) header.findViewById(R.id.select_album_song_count);
String songs = getResources().getQuantityString(R.plurals.select_album_n_songs, songCount, songCount);
songCountView.setText(songs);
String duration = Util.formatTotalDuration(albumHeader.getTotalDuration());
TextView durationView = (TextView) header.findViewById(R.id.select_album_duration);
durationView.setText(duration);
return header;
}
}
2013-04-27 11:52:25 +02:00
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
@Override
protected void onPostExecute(String[] result) {
refreshAlbumListView.onRefreshComplete();
super.onPostExecute(result);
}
@Override
protected String[] doInBackground(Void... params) {
refresh();
return null;
}
}
}