Search in sources :

Example 1 with ModalBackgroundTask

use of org.moire.ultrasonic.util.ModalBackgroundTask 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();
}
Also used : MusicDirectory(org.moire.ultrasonic.domain.MusicDirectory) MusicService(org.moire.ultrasonic.service.MusicService) EntryByDiscAndTrackComparator(org.moire.ultrasonic.util.EntryByDiscAndTrackComparator) ModalBackgroundTask(org.moire.ultrasonic.util.ModalBackgroundTask) LinkedList(java.util.LinkedList) DownloadService(org.moire.ultrasonic.service.DownloadService) Entry(org.moire.ultrasonic.domain.MusicDirectory.Entry) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Share(org.moire.ultrasonic.domain.Share)

Example 2 with ModalBackgroundTask

use of org.moire.ultrasonic.util.ModalBackgroundTask in project ultrasonic by ultrasonic.

the class ServerSettingsFragment method testConnection.

private void testConnection() {
    ModalBackgroundTask<Boolean> task = new ModalBackgroundTask<Boolean>(getActivity(), false) {

        private int previousInstance;

        @Override
        protected Boolean doInBackground() throws Throwable {
            updateProgress(R.string.settings_testing_connection);
            final Context context = getActivity();
            previousInstance = Util.getActiveServer(context);
            Util.setActiveServer(context, serverId);
            try {
                MusicService musicService = MusicServiceFactory.getMusicService(context);
                musicService.ping(context, this);
                return musicService.isLicenseValid(context, null);
            } finally {
                Util.setActiveServer(context, previousInstance);
            }
        }

        @Override
        protected void done(Boolean licenseValid) {
            if (licenseValid) {
                Util.toast(getActivity(), R.string.settings_testing_ok);
            } else {
                Util.toast(getActivity(), R.string.settings_testing_unlicensed);
            }
        }

        @Override
        protected void cancel() {
            super.cancel();
            Util.setActiveServer(getActivity(), previousInstance);
        }

        @Override
        protected void error(Throwable error) {
            Log.w(LOG_TAG, error.toString(), error);
            new ErrorDialog(getActivity(), String.format("%s %s", getResources().getString(R.string.settings_connection_failure), getErrorMessage(error)), false);
        }
    };
    task.execute();
}
Also used : Context(android.content.Context) MusicService(org.moire.ultrasonic.service.MusicService) ErrorDialog(org.moire.ultrasonic.util.ErrorDialog) ModalBackgroundTask(org.moire.ultrasonic.util.ModalBackgroundTask)

Aggregations

MusicService (org.moire.ultrasonic.service.MusicService)2 ModalBackgroundTask (org.moire.ultrasonic.util.ModalBackgroundTask)2 Context (android.content.Context)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 MusicDirectory (org.moire.ultrasonic.domain.MusicDirectory)1 Entry (org.moire.ultrasonic.domain.MusicDirectory.Entry)1 Share (org.moire.ultrasonic.domain.Share)1 DownloadService (org.moire.ultrasonic.service.DownloadService)1 EntryByDiscAndTrackComparator (org.moire.ultrasonic.util.EntryByDiscAndTrackComparator)1 ErrorDialog (org.moire.ultrasonic.util.ErrorDialog)1