Clover Coverage Report - Subsonic-Android Coverage Report
Coverage timestamp: ven dic 19 2014 17:57:13 EST
../../../../../img/srcFileCovDistChart9.png 32% of files have more coverage
67   234   37   2,58
22   181   0,55   26
26     1,42  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  CachedMusicService       Line # 46 67 37 82,6% 0.82608694
 
No Tests
 
1    /*
2    This file is part of Subsonic.
3   
4    Subsonic is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8   
9    Subsonic is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12    GNU General Public License for more details.
13   
14    You should have received a copy of the GNU General Public License
15    along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
16   
17    Copyright 2009 (C) Sindre Mehus
18    */
19    package net.sourceforge.subsonic.androidapp.service;
20   
21    import java.util.List;
22    import java.util.concurrent.TimeUnit;
23   
24    import org.apache.http.HttpResponse;
25   
26    import android.content.Context;
27    import android.graphics.Bitmap;
28    import net.sourceforge.subsonic.androidapp.domain.Indexes;
29    import net.sourceforge.subsonic.androidapp.domain.JukeboxStatus;
30    import net.sourceforge.subsonic.androidapp.domain.Lyrics;
31    import net.sourceforge.subsonic.androidapp.domain.MusicDirectory;
32    import net.sourceforge.subsonic.androidapp.domain.MusicFolder;
33    import net.sourceforge.subsonic.androidapp.domain.Playlist;
34    import net.sourceforge.subsonic.androidapp.domain.SearchCritera;
35    import net.sourceforge.subsonic.androidapp.domain.SearchResult;
36    import net.sourceforge.subsonic.androidapp.domain.Version;
37    import net.sourceforge.subsonic.androidapp.util.CancellableTask;
38    import net.sourceforge.subsonic.androidapp.util.LRUCache;
39    import net.sourceforge.subsonic.androidapp.util.ProgressListener;
40    import net.sourceforge.subsonic.androidapp.util.TimeLimitedCache;
41    import net.sourceforge.subsonic.androidapp.util.Util;
42   
43    /**
44    * @author Sindre Mehus
45    */
 
46    public class CachedMusicService implements MusicService {
47   
48    private static final int MUSIC_DIR_CACHE_SIZE = 20;
49    private static final int TTL_MUSIC_DIR = 5 * 60; // Five minutes
50   
51    private final MusicService musicService;
52    private final LRUCache<String, TimeLimitedCache<MusicDirectory>> cachedMusicDirectories;
53    private final TimeLimitedCache<Boolean> cachedLicenseValid = new TimeLimitedCache<Boolean>(120, TimeUnit.SECONDS);
54    private final TimeLimitedCache<Indexes> cachedIndexes = new TimeLimitedCache<Indexes>(60 * 60, TimeUnit.SECONDS);
55    private final TimeLimitedCache<List<Playlist>> cachedPlaylists = new TimeLimitedCache<List<Playlist>>(60, TimeUnit.SECONDS);
56    private final TimeLimitedCache<List<MusicFolder>> cachedMusicFolders = new TimeLimitedCache<List<MusicFolder>>(10 * 3600, TimeUnit.SECONDS);
57    private String restUrl;
58   
 
59  1 toggle public CachedMusicService(MusicService musicService) {
60  1 this.musicService = musicService;
61  1 cachedMusicDirectories = new LRUCache<String, TimeLimitedCache<MusicDirectory>>(MUSIC_DIR_CACHE_SIZE);
62    }
63   
 
64  2 toggle @Override
65    public void ping(Context context, ProgressListener progressListener) throws Exception {
66  2 checkSettingsChanged(context);
67  2 musicService.ping(context, progressListener);
68    }
69   
 
70  12 toggle @Override
71    public boolean isLicenseValid(Context context, ProgressListener progressListener) throws Exception {
72  12 checkSettingsChanged(context);
73  12 Boolean result = cachedLicenseValid.get();
74  12 if (result == null) {
75  1 result = musicService.isLicenseValid(context, progressListener);
76  1 cachedLicenseValid.set(result, result ? 30L * 60L : 2L * 60L, TimeUnit.SECONDS);
77    }
78  12 return result;
79    }
80   
 
81  3 toggle @Override
82    public List<MusicFolder> getMusicFolders(Context context, ProgressListener progressListener) throws Exception {
83  3 checkSettingsChanged(context);
84  3 List<MusicFolder> result = cachedMusicFolders.get();
85  3 if (result == null) {
86  2 result = musicService.getMusicFolders(context, progressListener);
87  2 cachedMusicFolders.set(result);
88    }
89  3 return result;
90    }
91   
 
92  3 toggle @Override
93    public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
94  3 checkSettingsChanged(context);
95  3 if (refresh) {
96  1 cachedIndexes.clear();
97  1 cachedMusicFolders.clear();
98  1 cachedMusicDirectories.clear();
99    }
100  3 Indexes result = cachedIndexes.get();
101  3 if (result == null) {
102  3 result = musicService.getIndexes(musicFolderId, refresh, context, progressListener);
103  3 cachedIndexes.set(result);
104    }
105  3 return result;
106    }
107   
 
108  5 toggle @Override
109    public MusicDirectory getMusicDirectory(String id, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
110  5 checkSettingsChanged(context);
111  5 TimeLimitedCache<MusicDirectory> cache = refresh ? null : cachedMusicDirectories.get(id);
112  5 MusicDirectory dir = cache == null ? null : cache.get();
113  5 if (dir == null) {
114  5 dir = musicService.getMusicDirectory(id, refresh, context, progressListener);
115  5 cache = new TimeLimitedCache<MusicDirectory>(TTL_MUSIC_DIR, TimeUnit.SECONDS);
116  5 cache.set(dir);
117  5 cachedMusicDirectories.put(id, cache);
118    }
119  5 return dir;
120    }
121   
 
122  1 toggle @Override
123    public SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception {
124  1 return musicService.search(criteria, context, progressListener);
125    }
126   
 
127  2 toggle @Override
128    public MusicDirectory getPlaylist(String id, Context context, ProgressListener progressListener) throws Exception {
129  2 return musicService.getPlaylist(id, context, progressListener);
130    }
131   
 
132  4 toggle @Override
133    public List<Playlist> getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
134  4 checkSettingsChanged(context);
135  4 List<Playlist> result = refresh ? null : cachedPlaylists.get();
136  4 if (result == null) {
137  2 result = musicService.getPlaylists(refresh, context, progressListener);
138  2 cachedPlaylists.set(result);
139    }
140  4 return result;
141    }
142   
 
143  1 toggle @Override
144    public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception {
145  1 musicService.createPlaylist(id, name, entries, context, progressListener);
146    }
147   
 
148  1 toggle @Override
149    public Lyrics getLyrics(String artist, String title, Context context, ProgressListener progressListener) throws Exception {
150  1 return musicService.getLyrics(artist, title, context, progressListener);
151    }
152   
 
153  4 toggle @Override
154    public void scrobble(String id, boolean submission, Context context, ProgressListener progressListener) throws Exception {
155  4 musicService.scrobble(id, submission, context, progressListener);
156    }
157   
 
158  3 toggle @Override
159    public MusicDirectory getAlbumList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception {
160  3 return musicService.getAlbumList(type, size, offset, context, progressListener);
161    }
162   
 
163  5 toggle @Override
164    public MusicDirectory getRandomSongs(int size, Context context, ProgressListener progressListener) throws Exception {
165  5 return musicService.getRandomSongs(size, context, progressListener);
166    }
167   
 
168  38 toggle @Override
169    public Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, boolean saveToFile, ProgressListener progressListener) throws Exception {
170  38 return musicService.getCoverArt(context, entry, size, saveToFile, progressListener);
171    }
172   
 
173  21 toggle @Override
174    public HttpResponse getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, CancellableTask task) throws Exception {
175  21 return musicService.getDownloadInputStream(context, song, offset, maxBitrate, task);
176    }
177   
 
178  0 toggle @Override
179    public Version getLocalVersion(Context context) throws Exception {
180  0 return musicService.getLocalVersion(context);
181    }
182   
 
183  0 toggle @Override
184    public Version getLatestVersion(Context context, ProgressListener progressListener) throws Exception {
185  0 return musicService.getLatestVersion(context, progressListener);
186    }
187   
 
188  0 toggle @Override
189    public String getVideoUrl(Context context, String id) {
190  0 return musicService.getVideoUrl(context, id);
191    }
192   
 
193  2 toggle @Override
194    public JukeboxStatus updateJukeboxPlaylist(List<String> ids, Context context, ProgressListener progressListener) throws Exception {
195  2 return musicService.updateJukeboxPlaylist(ids, context, progressListener);
196    }
197   
 
198  0 toggle @Override
199    public JukeboxStatus skipJukebox(int index, int offsetSeconds, Context context, ProgressListener progressListener) throws Exception {
200  0 return musicService.skipJukebox(index, offsetSeconds, context, progressListener);
201    }
202   
 
203  2 toggle @Override
204    public JukeboxStatus stopJukebox(Context context, ProgressListener progressListener) throws Exception {
205  2 return musicService.stopJukebox(context, progressListener);
206    }
207   
 
208  0 toggle @Override
209    public JukeboxStatus startJukebox(Context context, ProgressListener progressListener) throws Exception {
210  0 return musicService.startJukebox(context, progressListener);
211    }
212   
 
213  0 toggle @Override
214    public JukeboxStatus getJukeboxStatus(Context context, ProgressListener progressListener) throws Exception {
215  0 return musicService.getJukeboxStatus(context, progressListener);
216    }
217   
 
218  0 toggle @Override
219    public JukeboxStatus setJukeboxGain(float gain, Context context, ProgressListener progressListener) throws Exception {
220  0 return musicService.setJukeboxGain(gain, context, progressListener);
221    }
222   
 
223  29 toggle private void checkSettingsChanged(Context context) {
224  29 String newUrl = Util.getRestUrl(context, null);
225  29 if (!Util.equals(newUrl, restUrl)) {
226  1 cachedMusicFolders.clear();
227  1 cachedMusicDirectories.clear();
228  1 cachedLicenseValid.clear();
229  1 cachedIndexes.clear();
230  1 cachedPlaylists.clear();
231  1 restUrl = newUrl;
232    }
233    }
234    }