use of org.moire.ultrasonic.service.DownloadService in project ultrasonic by ultrasonic.
the class SubsonicTabActivity method downloadRecursively.
protected void downloadRecursively(final String id, final String name, final boolean isShare, final boolean isDirectory, final boolean save, final boolean append, final boolean autoplay, final boolean shuffle, final boolean background, final boolean playNext, final boolean unpin, final boolean isArtist) {
ModalBackgroundTask<List<Entry>> task = new ModalBackgroundTask<List<Entry>>(this, false) {
private static final int MAX_SONGS = 500;
@Override
protected List<Entry> doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(SubsonicTabActivity.this);
List<Entry> songs = new LinkedList<Entry>();
MusicDirectory root;
if (!Util.isOffline(SubsonicTabActivity.this) && isArtist && Util.getShouldUseId3Tags(SubsonicTabActivity.this)) {
getSongsForArtist(id, songs);
} else {
if (isDirectory) {
root = !Util.isOffline(SubsonicTabActivity.this) && Util.getShouldUseId3Tags(SubsonicTabActivity.this) ? musicService.getAlbum(id, name, false, SubsonicTabActivity.this, this) : musicService.getMusicDirectory(id, name, false, SubsonicTabActivity.this, this);
} else if (isShare) {
root = new MusicDirectory();
List<Share> shares = musicService.getShares(true, SubsonicTabActivity.this, this);
for (Share share : shares) {
if (share.getId().equals(id)) {
for (Entry entry : share.getEntries()) {
root.addChild(entry);
}
break;
}
}
} else {
root = musicService.getPlaylist(id, name, SubsonicTabActivity.this, this);
}
getSongsRecursively(root, songs);
}
return songs;
}
private void getSongsRecursively(MusicDirectory parent, List<Entry> songs) throws Exception {
if (songs.size() > MAX_SONGS) {
return;
}
for (Entry song : parent.getChildren(false, true)) {
if (!song.isVideo()) {
songs.add(song);
}
}
MusicService musicService = MusicServiceFactory.getMusicService(SubsonicTabActivity.this);
for (Entry dir : parent.getChildren(true, false)) {
MusicDirectory root;
root = !Util.isOffline(SubsonicTabActivity.this) && Util.getShouldUseId3Tags(SubsonicTabActivity.this) ? musicService.getAlbum(dir.getId(), dir.getTitle(), false, SubsonicTabActivity.this, this) : musicService.getMusicDirectory(dir.getId(), dir.getTitle(), false, SubsonicTabActivity.this, this);
getSongsRecursively(root, songs);
}
}
private void getSongsForArtist(String id, Collection<Entry> songs) throws Exception {
if (songs.size() > MAX_SONGS) {
return;
}
MusicService musicService = MusicServiceFactory.getMusicService(SubsonicTabActivity.this);
MusicDirectory artist = musicService.getArtist(id, "", false, SubsonicTabActivity.this, this);
for (Entry album : artist.getChildren()) {
MusicDirectory albumDirectory = musicService.getAlbum(album.getId(), "", false, SubsonicTabActivity.this, this);
for (Entry song : albumDirectory.getChildren()) {
if (!song.isVideo()) {
songs.add(song);
}
}
}
}
@Override
protected void done(List<Entry> songs) {
if (Util.getShouldSortByDisc(SubsonicTabActivity.this)) {
Collections.sort(songs, new EntryByDiscAndTrackComparator());
}
DownloadService downloadService = getDownloadService();
if (!songs.isEmpty() && downloadService != null) {
if (!append && !playNext && !unpin && !background) {
downloadService.clear();
}
warnIfNetworkOrStorageUnavailable();
if (!background) {
if (unpin) {
downloadService.unpin(songs);
} else {
downloadService.download(songs, save, autoplay, playNext, shuffle, false);
if (!append && Util.getShouldTransitionOnPlaybackPreference(SubsonicTabActivity.this)) {
startActivityForResultWithoutTransition(SubsonicTabActivity.this, DownloadActivity.class);
}
}
} else {
if (unpin) {
downloadService.unpin(songs);
} else {
downloadService.downloadBackground(songs, save);
}
}
}
}
};
task.execute();
}
use of org.moire.ultrasonic.service.DownloadService in project ultrasonic by ultrasonic.
the class DownloadActivity method menuItemSelected.
private boolean menuItemSelected(final int menuItemId, final DownloadFile song) {
Entry entry = null;
if (song != null) {
entry = song.getSong();
}
switch(menuItemId) {
case R.id.menu_show_artist:
if (entry == null) {
return false;
}
if (Util.getShouldUseId3Tags(DownloadActivity.this)) {
Intent intent = new Intent(DownloadActivity.this, SelectAlbumActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ID, entry.getArtistId());
intent.putExtra(Constants.INTENT_EXTRA_NAME_NAME, entry.getArtist());
intent.putExtra(Constants.INTENT_EXTRA_NAME_PARENT_ID, entry.getArtistId());
intent.putExtra(Constants.INTENT_EXTRA_NAME_ARTIST, true);
startActivityForResultWithoutTransition(DownloadActivity.this, intent);
}
return true;
case R.id.menu_show_album:
if (entry == null) {
return false;
}
Intent intent = new Intent(this, SelectAlbumActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ID, entry.getParent());
intent.putExtra(Constants.INTENT_EXTRA_NAME_NAME, entry.getAlbum());
startActivityForResultWithoutTransition(this, intent);
return true;
case R.id.menu_lyrics:
if (entry == null) {
return false;
}
intent = new Intent(this, LyricsActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_NAME_ARTIST, entry.getArtist());
intent.putExtra(Constants.INTENT_EXTRA_NAME_TITLE, entry.getTitle());
startActivityForResultWithoutTransition(this, intent);
return true;
case R.id.menu_remove:
getDownloadService().remove(song);
onDownloadListChanged();
return true;
case R.id.menu_item_screen_on_off:
if (getDownloadService().getKeepScreenOn()) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getDownloadService().setKeepScreenOn(false);
} else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getDownloadService().setKeepScreenOn(true);
}
return true;
case R.id.menu_shuffle:
getDownloadService().shuffle();
Util.toast(this, R.string.download_menu_shuffle_notification);
return true;
case R.id.menu_item_equalizer:
startActivity(new Intent(DownloadActivity.this, EqualizerActivity.class));
return true;
case R.id.menu_item_visualizer:
final boolean active = !visualizerView.isActive();
visualizerView.setActive(active);
if (!visualizerView.isActive()) {
visualizerViewLayout.setVisibility(View.GONE);
} else {
visualizerViewLayout.setVisibility(View.VISIBLE);
}
getDownloadService().setShowVisualization(visualizerView.isActive());
Util.toast(DownloadActivity.this, active ? R.string.download_visualizer_on : R.string.download_visualizer_off);
return true;
case R.id.menu_item_jukebox:
final boolean jukeboxEnabled = !getDownloadService().isJukeboxEnabled();
getDownloadService().setJukeboxEnabled(jukeboxEnabled);
Util.toast(DownloadActivity.this, jukeboxEnabled ? R.string.download_jukebox_on : R.string.download_jukebox_off, false);
return true;
case R.id.menu_item_toggle_list:
toggleFullScreenAlbumArt();
return true;
case R.id.menu_item_clear_playlist:
getDownloadService().setShufflePlayEnabled(false);
getDownloadService().clear();
onDownloadListChanged();
return true;
case R.id.menu_item_save_playlist:
if (!getDownloadService().getSongs().isEmpty()) {
showDialog(DIALOG_SAVE_PLAYLIST);
}
return true;
case R.id.menu_item_star:
if (currentSong == null) {
return true;
}
final boolean isStarred = currentSong.getStarred();
final String id = currentSong.getId();
if (isStarred) {
starMenuItem.setIcon(Util.getDrawableFromAttribute(SubsonicTabActivity.getInstance(), R.attr.star_hollow));
currentSong.setStarred(false);
} else {
starMenuItem.setIcon(Util.getDrawableFromAttribute(SubsonicTabActivity.getInstance(), R.attr.star_full));
currentSong.setStarred(true);
}
new Thread(new Runnable() {
@Override
public void run() {
final MusicService musicService = MusicServiceFactory.getMusicService(DownloadActivity.this);
try {
if (isStarred) {
musicService.unstar(id, null, null, DownloadActivity.this, null);
} else {
musicService.star(id, null, null, DownloadActivity.this, null);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
}).start();
return true;
case R.id.menu_item_bookmark_set:
if (currentSong == null) {
return true;
}
final String songId = currentSong.getId();
final int playerPosition = getDownloadService().getPlayerPosition();
currentSong.setBookmarkPosition(playerPosition);
String bookmarkTime = Util.formatTotalDuration(playerPosition, true);
new Thread(new Runnable() {
@Override
public void run() {
final MusicService musicService = MusicServiceFactory.getMusicService(DownloadActivity.this);
try {
musicService.createBookmark(songId, playerPosition, DownloadActivity.this, null);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
}).start();
String msg = getResources().getString(R.string.download_bookmark_set_at_position, bookmarkTime);
Util.toast(DownloadActivity.this, msg);
return true;
case R.id.menu_item_bookmark_delete:
if (currentSong == null) {
return true;
}
final String bookmarkSongId = currentSong.getId();
currentSong.setBookmarkPosition(0);
new Thread(new Runnable() {
@Override
public void run() {
final MusicService musicService = MusicServiceFactory.getMusicService(DownloadActivity.this);
try {
musicService.deleteBookmark(bookmarkSongId, DownloadActivity.this, null);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
}).start();
Util.toast(DownloadActivity.this, R.string.download_bookmark_removed);
return true;
case R.id.menu_item_share:
DownloadService downloadService = getDownloadService();
List<Entry> entries = new ArrayList<Entry>();
if (downloadService != null) {
List<DownloadFile> downloadServiceSongs = downloadService.getSongs();
if (downloadServiceSongs != null) {
for (DownloadFile downloadFile : downloadServiceSongs) {
if (downloadFile != null) {
Entry playlistEntry = downloadFile.getSong();
if (playlistEntry != null) {
entries.add(playlistEntry);
}
}
}
}
}
createShare(entries);
return true;
default:
return false;
}
}
use of org.moire.ultrasonic.service.DownloadService in project ultrasonic by ultrasonic.
the class DownloadActivity method onResume.
@Override
protected void onResume() {
super.onResume();
final DownloadService downloadService = getDownloadService();
if (downloadService == null || downloadService.getCurrentPlaying() == null) {
playlistFlipper.setDisplayedChild(1);
}
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
update();
}
});
}
};
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(runnable, 0L, 250L, TimeUnit.MILLISECONDS);
if (downloadService != null && downloadService.getKeepScreenOn()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
if (visualizerView != null) {
visualizerView.setActive(downloadService != null && downloadService.getShowVisualization());
}
invalidateOptionsMenu();
}
use of org.moire.ultrasonic.service.DownloadService in project ultrasonic by ultrasonic.
the class DownloadActivity method onSliderProgressChanged.
private void onSliderProgressChanged() {
DownloadService downloadService = getDownloadService();
if (downloadService == null || onProgressChangedTask != null) {
return;
}
onProgressChangedTask = new SilentBackgroundTask<Void>(this) {
DownloadService downloadService;
boolean isJukeboxEnabled;
int millisPlayed;
Integer duration;
PlayerState playerState;
@Override
protected Void doInBackground() throws Throwable {
downloadService = getDownloadService();
isJukeboxEnabled = downloadService.isJukeboxEnabled();
millisPlayed = Math.max(0, downloadService.getPlayerPosition());
duration = downloadService.getPlayerDuration();
playerState = getDownloadService().getPlayerState();
return null;
}
@Override
protected void done(final Void result) {
if (currentPlaying != null) {
final int millisTotal = duration == null ? 0 : duration;
positionTextView.setText(Util.formatTotalDuration(millisPlayed, true));
durationTextView.setText(Util.formatTotalDuration(millisTotal, true));
// Work-around for apparent bug.
progressBar.setMax(millisTotal == 0 ? 100 : millisTotal);
progressBar.setProgress(millisPlayed);
progressBar.setEnabled(currentPlaying.isWorkDone() || isJukeboxEnabled);
} else {
positionTextView.setText(R.string.util_zero_time);
durationTextView.setText(R.string.util_no_time);
progressBar.setProgress(0);
progressBar.setMax(0);
progressBar.setEnabled(false);
}
switch(playerState) {
case DOWNLOADING:
final long bytes = currentPlaying != null ? currentPlaying.getPartialFile().length() : 0;
String downloadStatus = getResources().getString(R.string.download_playerstate_downloading, Util.formatLocalizedBytes(bytes, DownloadActivity.this));
setActionBarSubtitle(downloadStatus);
break;
case PREPARING:
setActionBarSubtitle(R.string.download_playerstate_buffering);
break;
case STARTED:
final DownloadService downloadService = getDownloadService();
if (downloadService != null && downloadService.isShufflePlayEnabled()) {
setActionBarSubtitle(R.string.download_playerstate_playing_shuffle);
} else {
setActionBarSubtitle(null);
}
break;
default:
setActionBarSubtitle(null);
break;
case IDLE:
break;
case PREPARED:
break;
case STOPPED:
break;
case PAUSED:
break;
case COMPLETED:
break;
}
switch(playerState) {
case STARTED:
pauseButton.setVisibility(View.VISIBLE);
stopButton.setVisibility(View.GONE);
startButton.setVisibility(View.GONE);
break;
case DOWNLOADING:
case PREPARING:
pauseButton.setVisibility(View.GONE);
stopButton.setVisibility(View.VISIBLE);
startButton.setVisibility(View.GONE);
break;
default:
pauseButton.setVisibility(View.GONE);
stopButton.setVisibility(View.GONE);
startButton.setVisibility(View.VISIBLE);
break;
}
onProgressChangedTask = null;
}
};
onProgressChangedTask.execute();
}
use of org.moire.ultrasonic.service.DownloadService in project ultrasonic by ultrasonic.
the class DownloadActivity method start.
private void start() {
final DownloadService service = getDownloadService();
final PlayerState state = service.getPlayerState();
if (state == PAUSED || state == COMPLETED || state == STOPPED) {
service.start();
} else if (state == IDLE) {
warnIfNetworkOrStorageUnavailable();
final int current = service.getCurrentPlayingIndex();
if (current == -1) {
service.play(0);
} else {
service.play(current);
}
}
}
Aggregations