2013-04-06 21:47:24 +02:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
package com.thejoshwa.ultrasonic.androidapp.service;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
2013-04-20 23:58:59 +02:00
|
|
|
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Genre;
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Indexes;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.JukeboxStatus;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Lyrics;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.MusicDirectory;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.MusicFolder;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Playlist;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.SearchCritera;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.SearchResult;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.Version;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.CancellableTask;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.LRUCache;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.ProgressListener;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.TimeLimitedCache;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.Util;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Sindre Mehus
|
|
|
|
*/
|
|
|
|
public class CachedMusicService implements MusicService {
|
|
|
|
|
|
|
|
private static final int MUSIC_DIR_CACHE_SIZE = 50;
|
|
|
|
private static final int TTL_MUSIC_DIR = 5 * 60; // Five minutes
|
|
|
|
|
|
|
|
private final MusicService musicService;
|
|
|
|
private final LRUCache<String, TimeLimitedCache<MusicDirectory>> cachedMusicDirectories;
|
2013-05-18 12:50:54 +02:00
|
|
|
private final LRUCache<String, TimeLimitedCache<MusicDirectory>> cachedArtist;
|
|
|
|
private final LRUCache<String, TimeLimitedCache<MusicDirectory>> cachedAlbum;
|
2013-04-06 21:47:24 +02:00
|
|
|
private final TimeLimitedCache<Boolean> cachedLicenseValid = new TimeLimitedCache<Boolean>(120, TimeUnit.SECONDS);
|
|
|
|
private final TimeLimitedCache<Indexes> cachedIndexes = new TimeLimitedCache<Indexes>(60 * 60, TimeUnit.SECONDS);
|
2013-05-18 12:50:54 +02:00
|
|
|
private final TimeLimitedCache<Indexes> cachedArtists = new TimeLimitedCache<Indexes>(60 * 60, TimeUnit.SECONDS);
|
2013-05-16 09:59:55 +02:00
|
|
|
private final TimeLimitedCache<List<Playlist>> cachedPlaylists = new TimeLimitedCache<List<Playlist>>(3600, TimeUnit.SECONDS);
|
2013-04-06 21:47:24 +02:00
|
|
|
private final TimeLimitedCache<List<MusicFolder>> cachedMusicFolders = new TimeLimitedCache<List<MusicFolder>>(10 * 3600, TimeUnit.SECONDS);
|
2013-04-20 23:58:59 +02:00
|
|
|
private final TimeLimitedCache<List<Genre>> cachedGenres = new TimeLimitedCache<List<Genre>>(10 * 3600, TimeUnit.SECONDS);
|
2013-04-06 21:47:24 +02:00
|
|
|
private String restUrl;
|
|
|
|
|
|
|
|
public CachedMusicService(MusicService musicService) {
|
|
|
|
this.musicService = musicService;
|
|
|
|
cachedMusicDirectories = new LRUCache<String, TimeLimitedCache<MusicDirectory>>(MUSIC_DIR_CACHE_SIZE);
|
2013-05-18 12:50:54 +02:00
|
|
|
cachedArtist = new LRUCache<String, TimeLimitedCache<MusicDirectory>>(MUSIC_DIR_CACHE_SIZE);
|
|
|
|
cachedAlbum = new LRUCache<String, TimeLimitedCache<MusicDirectory>>(MUSIC_DIR_CACHE_SIZE);
|
2013-04-06 21:47:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void ping(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
checkSettingsChanged(context);
|
|
|
|
musicService.ping(context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isLicenseValid(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
checkSettingsChanged(context);
|
|
|
|
Boolean result = cachedLicenseValid.get();
|
|
|
|
if (result == null) {
|
|
|
|
result = musicService.isLicenseValid(context, progressListener);
|
|
|
|
cachedLicenseValid.set(result, result ? 30L * 60L : 2L * 60L, TimeUnit.SECONDS);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-16 09:59:55 +02:00
|
|
|
public List<MusicFolder> getMusicFolders(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
2013-04-06 21:47:24 +02:00
|
|
|
checkSettingsChanged(context);
|
2013-05-16 09:59:55 +02:00
|
|
|
if (refresh) {
|
|
|
|
cachedMusicFolders.clear();
|
|
|
|
}
|
2013-04-06 21:47:24 +02:00
|
|
|
List<MusicFolder> result = cachedMusicFolders.get();
|
|
|
|
if (result == null) {
|
2013-05-16 09:59:55 +02:00
|
|
|
result = musicService.getMusicFolders(refresh, context, progressListener);
|
2013-04-06 21:47:24 +02:00
|
|
|
cachedMusicFolders.set(result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
checkSettingsChanged(context);
|
|
|
|
if (refresh) {
|
|
|
|
cachedIndexes.clear();
|
|
|
|
cachedMusicFolders.clear();
|
|
|
|
cachedMusicDirectories.clear();
|
|
|
|
}
|
|
|
|
Indexes result = cachedIndexes.get();
|
|
|
|
if (result == null) {
|
|
|
|
result = musicService.getIndexes(musicFolderId, refresh, context, progressListener);
|
|
|
|
cachedIndexes.set(result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2013-05-18 12:50:54 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Indexes getArtists(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
checkSettingsChanged(context);
|
|
|
|
if (refresh) {
|
|
|
|
cachedArtists.clear();
|
|
|
|
}
|
|
|
|
Indexes result = cachedArtists.get();
|
|
|
|
if (result == null) {
|
|
|
|
result = musicService.getArtists(refresh, context, progressListener);
|
|
|
|
cachedArtists.set(result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2013-04-06 21:47:24 +02:00
|
|
|
|
|
|
|
@Override
|
2013-05-16 09:59:55 +02:00
|
|
|
public MusicDirectory getMusicDirectory(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
2013-04-06 21:47:24 +02:00
|
|
|
checkSettingsChanged(context);
|
|
|
|
TimeLimitedCache<MusicDirectory> cache = refresh ? null : cachedMusicDirectories.get(id);
|
|
|
|
MusicDirectory dir = cache == null ? null : cache.get();
|
|
|
|
if (dir == null) {
|
2013-05-16 09:59:55 +02:00
|
|
|
dir = musicService.getMusicDirectory(id, name, refresh, context, progressListener);
|
2013-04-06 21:47:24 +02:00
|
|
|
cache = new TimeLimitedCache<MusicDirectory>(TTL_MUSIC_DIR, TimeUnit.SECONDS);
|
|
|
|
cache.set(dir);
|
|
|
|
cachedMusicDirectories.put(id, cache);
|
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
}
|
2013-05-18 12:50:54 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getArtist(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
checkSettingsChanged(context);
|
|
|
|
TimeLimitedCache<MusicDirectory> cache = refresh ? null : cachedArtist.get(id);
|
|
|
|
MusicDirectory dir = cache == null ? null : cache.get();
|
|
|
|
if (dir == null) {
|
|
|
|
dir = musicService.getArtist(id, name, refresh, context, progressListener);
|
|
|
|
cache = new TimeLimitedCache<MusicDirectory>(TTL_MUSIC_DIR, TimeUnit.SECONDS);
|
|
|
|
cache.set(dir);
|
|
|
|
cachedArtist.put(id, cache);
|
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getAlbum(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
checkSettingsChanged(context);
|
|
|
|
TimeLimitedCache<MusicDirectory> cache = refresh ? null : cachedAlbum.get(id);
|
|
|
|
MusicDirectory dir = cache == null ? null : cache.get();
|
|
|
|
if (dir == null) {
|
|
|
|
dir = musicService.getAlbum(id, name, refresh, context, progressListener);
|
|
|
|
cache = new TimeLimitedCache<MusicDirectory>(TTL_MUSIC_DIR, TimeUnit.SECONDS);
|
|
|
|
cache.set(dir);
|
|
|
|
cachedAlbum.put(id, cache);
|
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
}
|
2013-04-06 21:47:24 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.search(criteria, context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-16 09:59:55 +02:00
|
|
|
public MusicDirectory getPlaylist(String id, String name, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.getPlaylist(id, name, context, progressListener);
|
2013-04-06 21:47:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Playlist> getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
checkSettingsChanged(context);
|
|
|
|
List<Playlist> result = refresh ? null : cachedPlaylists.get();
|
|
|
|
if (result == null) {
|
|
|
|
result = musicService.getPlaylists(refresh, context, progressListener);
|
|
|
|
cachedPlaylists.set(result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception {
|
2013-05-16 09:59:55 +02:00
|
|
|
cachedPlaylists.clear();
|
2013-04-06 21:47:24 +02:00
|
|
|
musicService.createPlaylist(id, name, entries, context, progressListener);
|
|
|
|
}
|
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
@Override
|
|
|
|
public void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
musicService.deletePlaylist(id, context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-18 12:50:54 +02:00
|
|
|
public void updatePlaylist(String id, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
musicService.updatePlaylist(id, toAdd, context, progressListener);
|
2013-05-16 09:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
musicService.removeFromPlaylist(id, toRemove, context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
musicService.updatePlaylist(id, name, comment, pub, context, progressListener);
|
|
|
|
}
|
|
|
|
|
2013-04-06 21:47:24 +02:00
|
|
|
@Override
|
|
|
|
public Lyrics getLyrics(String artist, String title, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.getLyrics(artist, title, context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void scrobble(String id, boolean submission, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
musicService.scrobble(id, submission, context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getAlbumList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.getAlbumList(type, size, offset, context, progressListener);
|
|
|
|
}
|
2013-05-18 12:50:54 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getAlbumList2(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.getAlbumList2(type, size, offset, context, progressListener);
|
|
|
|
}
|
2013-04-06 21:47:24 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getRandomSongs(int size, Context context, ProgressListener progressListener) throws Exception {
|
2013-05-16 09:59:55 +02:00
|
|
|
return musicService.getRandomSongs(size, context, progressListener);
|
2013-04-06 21:47:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SearchResult getStarred(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.getStarred(context, progressListener);
|
|
|
|
}
|
2013-05-18 12:50:54 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public SearchResult getStarred2(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.getStarred2(context, progressListener);
|
|
|
|
}
|
2013-04-06 21:47:24 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, boolean saveToFile, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.getCoverArt(context, entry, size, saveToFile, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public HttpResponse getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, CancellableTask task) throws Exception {
|
|
|
|
return musicService.getDownloadInputStream(context, song, offset, maxBitrate, task);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Version getLocalVersion(Context context) throws Exception {
|
|
|
|
return musicService.getLocalVersion(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Version getLatestVersion(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.getLatestVersion(context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-16 09:59:55 +02:00
|
|
|
public String getVideoUrl(int maxBitrate, Context context, String id) {
|
|
|
|
return musicService.getVideoUrl(maxBitrate, context, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getVideoStreamUrl(int maxBitrate, Context context, String id) {
|
|
|
|
return musicService.getVideoStreamUrl(maxBitrate, context, id);
|
2013-04-06 21:47:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus updateJukeboxPlaylist(List<String> ids, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.updateJukeboxPlaylist(ids, context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus skipJukebox(int index, int offsetSeconds, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.skipJukebox(index, offsetSeconds, context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus stopJukebox(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.stopJukebox(context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus startJukebox(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.startJukebox(context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus getJukeboxStatus(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.getJukeboxStatus(context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JukeboxStatus setJukeboxGain(float gain, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.setJukeboxGain(gain, context, progressListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkSettingsChanged(Context context) {
|
|
|
|
String newUrl = Util.getRestUrl(context, null);
|
|
|
|
if (!Util.equals(newUrl, restUrl)) {
|
|
|
|
cachedMusicFolders.clear();
|
|
|
|
cachedMusicDirectories.clear();
|
|
|
|
cachedLicenseValid.clear();
|
|
|
|
cachedIndexes.clear();
|
|
|
|
cachedPlaylists.clear();
|
|
|
|
restUrl = newUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-18 12:50:54 +02:00
|
|
|
public void star(String id, String albumId, String artistId, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
musicService.star(id, albumId, artistId, context, progressListener);
|
2013-04-06 21:47:24 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-05-18 12:50:54 +02:00
|
|
|
public void unstar(String id, String albumId, String artistId, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
musicService.unstar(id, albumId, artistId, context, progressListener);
|
2013-04-06 21:47:24 +02:00
|
|
|
}
|
2013-04-20 23:58:59 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Genre> getGenres(Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
checkSettingsChanged(context);
|
|
|
|
List<Genre> result = cachedGenres.get();
|
|
|
|
if (result == null) {
|
|
|
|
result = musicService.getGenres(context, progressListener);
|
|
|
|
cachedGenres.set(result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception {
|
|
|
|
return musicService.getSongsByGenre(genre, count, offset, context, progressListener);
|
|
|
|
}
|
2013-04-06 21:47:24 +02:00
|
|
|
}
|