aboutsummaryrefslogtreecommitdiff
path: root/subsonic-android/src/github/daneren2005/subphonic/service/CachedMusicService.java
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2012-07-07 08:29:52 -0700
committerScott Jackson <daneren2005@gmail.com>2012-07-07 08:29:52 -0700
commit6ebae86dfbb7fa79d81e6b2485f395eeab7267ef (patch)
treebc26b39df3c6a666bcac960042f2ac8cb06ad202 /subsonic-android/src/github/daneren2005/subphonic/service/CachedMusicService.java
parent8a7bb33f73d4fab1e380adf972efc2f3a7ee8b3e (diff)
downloaddsub-6ebae86dfbb7fa79d81e6b2485f395eeab7267ef.tar.gz
dsub-6ebae86dfbb7fa79d81e6b2485f395eeab7267ef.tar.bz2
dsub-6ebae86dfbb7fa79d81e6b2485f395eeab7267ef.zip
Changed project package to github.daneren2005.subphonic
Diffstat (limited to 'subsonic-android/src/github/daneren2005/subphonic/service/CachedMusicService.java')
-rw-r--r--subsonic-android/src/github/daneren2005/subphonic/service/CachedMusicService.java237
1 files changed, 237 insertions, 0 deletions
diff --git a/subsonic-android/src/github/daneren2005/subphonic/service/CachedMusicService.java b/subsonic-android/src/github/daneren2005/subphonic/service/CachedMusicService.java
new file mode 100644
index 00000000..2d0ebf55
--- /dev/null
+++ b/subsonic-android/src/github/daneren2005/subphonic/service/CachedMusicService.java
@@ -0,0 +1,237 @@
+/*
+ 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 github.daneren2005.subphonic.service;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.http.HttpResponse;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import github.daneren2005.subphonic.domain.Indexes;
+import github.daneren2005.subphonic.domain.JukeboxStatus;
+import github.daneren2005.subphonic.domain.Lyrics;
+import github.daneren2005.subphonic.domain.MusicDirectory;
+import github.daneren2005.subphonic.domain.MusicFolder;
+import github.daneren2005.subphonic.domain.Playlist;
+import github.daneren2005.subphonic.domain.SearchCritera;
+import github.daneren2005.subphonic.domain.SearchResult;
+import github.daneren2005.subphonic.domain.Version;
+import github.daneren2005.subphonic.util.CancellableTask;
+import github.daneren2005.subphonic.util.LRUCache;
+import github.daneren2005.subphonic.util.ProgressListener;
+import github.daneren2005.subphonic.util.TimeLimitedCache;
+import github.daneren2005.subphonic.util.Util;
+
+/**
+ * @author Sindre Mehus
+ */
+public class CachedMusicService implements MusicService {
+
+ private static final int MUSIC_DIR_CACHE_SIZE = 20;
+ private static final int TTL_MUSIC_DIR = 5 * 60; // Five minutes
+
+ private final MusicService musicService;
+ private final LRUCache<String, TimeLimitedCache<MusicDirectory>> cachedMusicDirectories;
+ private final TimeLimitedCache<Boolean> cachedLicenseValid = new TimeLimitedCache<Boolean>(120, TimeUnit.SECONDS);
+ private final TimeLimitedCache<Indexes> cachedIndexes = new TimeLimitedCache<Indexes>(60 * 60, TimeUnit.SECONDS);
+ private final TimeLimitedCache<List<Playlist>> cachedPlaylists = new TimeLimitedCache<List<Playlist>>(60, TimeUnit.SECONDS);
+ private final TimeLimitedCache<List<MusicFolder>> cachedMusicFolders = new TimeLimitedCache<List<MusicFolder>>(10 * 3600, TimeUnit.SECONDS);
+ private String restUrl;
+
+ public CachedMusicService(MusicService musicService) {
+ this.musicService = musicService;
+ cachedMusicDirectories = new LRUCache<String, TimeLimitedCache<MusicDirectory>>(MUSIC_DIR_CACHE_SIZE);
+ }
+
+ @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
+ public List<MusicFolder> getMusicFolders(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
+ checkSettingsChanged(context);
+ if (refresh) {
+ cachedMusicFolders.clear();
+ }
+ List<MusicFolder> result = cachedMusicFolders.get();
+ if (result == null) {
+ result = musicService.getMusicFolders(refresh, context, progressListener);
+ 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;
+ }
+
+ @Override
+ public MusicDirectory getMusicDirectory(String id, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
+ checkSettingsChanged(context);
+ TimeLimitedCache<MusicDirectory> cache = refresh ? null : cachedMusicDirectories.get(id);
+ MusicDirectory dir = cache == null ? null : cache.get();
+ if (dir == null) {
+ dir = musicService.getMusicDirectory(id, refresh, context, progressListener);
+ cache = new TimeLimitedCache<MusicDirectory>(TTL_MUSIC_DIR, TimeUnit.SECONDS);
+ cache.set(dir);
+ cachedMusicDirectories.put(id, cache);
+ }
+ return dir;
+ }
+
+ @Override
+ public SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception {
+ return musicService.search(criteria, context, progressListener);
+ }
+
+ @Override
+ public MusicDirectory getPlaylist(String id, String name, Context context, ProgressListener progressListener) throws Exception {
+ return musicService.getPlaylist(id, name, context, progressListener);
+ }
+
+ @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 {
+ musicService.createPlaylist(id, name, entries, context, progressListener);
+ }
+
+ @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);
+ }
+
+ @Override
+ public MusicDirectory getRandomSongs(int size, Context context, ProgressListener progressListener) throws Exception {
+ return musicService.getRandomSongs(size, context, progressListener);
+ }
+
+ @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
+ public String getVideoUrl(Context context, String id) {
+ return musicService.getVideoUrl(context, id);
+ }
+
+ @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;
+ }
+ }
+}