1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
/*
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.dsub.service;
import java.io.File;
import java.util.ArrayList;
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.dsub.domain.Bookmark;
import github.daneren2005.dsub.domain.ChatMessage;
import github.daneren2005.dsub.domain.Genre;
import github.daneren2005.dsub.domain.Indexes;
import github.daneren2005.dsub.domain.RemoteStatus;
import github.daneren2005.dsub.domain.Lyrics;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.domain.MusicFolder;
import github.daneren2005.dsub.domain.Playlist;
import github.daneren2005.dsub.domain.PodcastChannel;
import github.daneren2005.dsub.domain.SearchCritera;
import github.daneren2005.dsub.domain.SearchResult;
import github.daneren2005.dsub.domain.Share;
import github.daneren2005.dsub.domain.Version;
import github.daneren2005.dsub.util.CancellableTask;
import github.daneren2005.dsub.util.ProgressListener;
import github.daneren2005.dsub.util.TimeLimitedCache;
import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.Util;
/**
* @author Sindre Mehus
*/
public class CachedMusicService implements MusicService {
private static final String TAG = CachedMusicService.class.getSimpleName();
private static final int MUSIC_DIR_CACHE_SIZE = 20;
private static final int TTL_MUSIC_DIR = 5 * 60; // Five minutes
private final RESTMusicService musicService;
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>>(3600, TimeUnit.SECONDS);
private final TimeLimitedCache<List<MusicFolder>> cachedMusicFolders = new TimeLimitedCache<List<MusicFolder>>(10 * 3600, TimeUnit.SECONDS);
private final TimeLimitedCache<List<PodcastChannel>> cachedPodcastChannels = new TimeLimitedCache<List<PodcastChannel>>(10 * 3600, TimeUnit.SECONDS);
private String restUrl;
private boolean isTagBrowsing = false;
public CachedMusicService(RESTMusicService musicService) {
this.musicService = musicService;
}
@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 = FileUtil.deserialize(context, getCacheName(context, "license"), Boolean.class);
if(result == null) {
result = musicService.isLicenseValid(context, progressListener);
// Only save a copy license is valid
if(result == true) {
FileUtil.serialize(context, (Boolean) result, getCacheName(context, "license"));
}
}
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) {
if(!refresh) {
result = FileUtil.deserialize(context, getCacheName(context, "musicFolders"), ArrayList.class);
}
if(result == null) {
result = musicService.getMusicFolders(refresh, context, progressListener);
FileUtil.serialize(context, new ArrayList<MusicFolder>(result), getCacheName(context, "musicFolders"));
}
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();
}
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, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
if(!refresh) {
dir = FileUtil.deserialize(context, getCacheName(context, "directory", id), MusicDirectory.class);
}
if(dir == null) {
dir = musicService.getMusicDirectory(id, name, refresh, context, progressListener);
FileUtil.serialize(context, dir, getCacheName(context, "directory", id));
}
return dir;
}
@Override
public MusicDirectory getArtist(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
if(!refresh) {
dir = FileUtil.deserialize(context, getCacheName(context, "artist", id), MusicDirectory.class);
}
if(dir == null) {
dir = musicService.getArtist(id, name, refresh, context, progressListener);
FileUtil.serialize(context, dir, getCacheName(context, "artist", id));
}
return dir;
}
@Override
public MusicDirectory getAlbum(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
if(!refresh) {
dir = FileUtil.deserialize(context, getCacheName(context, "album", id), MusicDirectory.class);
}
if(dir == null) {
dir = musicService.getAlbum(id, name, refresh, context, progressListener);
FileUtil.serialize(context, dir, getCacheName(context, "album", id));
}
return dir;
}
@Override
public SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception {
return musicService.search(criteria, context, progressListener);
}
@Override
public MusicDirectory getPlaylist(boolean refresh, String id, String name, Context context, ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
if(!refresh) {
dir = FileUtil.deserialize(context, getCacheName(context, "playlist", id), MusicDirectory.class);
}
if(dir == null) {
dir = musicService.getPlaylist(refresh, id, name, context, progressListener);
FileUtil.serialize(context, dir, getCacheName(context, "playlist", id));
}
return dir;
}
@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) {
if(!refresh) {
result = FileUtil.deserialize(context, getCacheName(context, "playlist"), ArrayList.class);
}
if(result == null) {
result = musicService.getPlaylists(refresh, context, progressListener);
FileUtil.serialize(context, new ArrayList<Playlist>(result), getCacheName(context, "playlist"));
}
cachedPlaylists.set(result);
}
return result;
}
@Override
public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception {
cachedPlaylists.clear();
Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist")));
musicService.createPlaylist(id, name, entries, context, progressListener);
}
@Override
public void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception {
Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist")));
musicService.deletePlaylist(id, context, progressListener);
}
@Override
public void addToPlaylist(String id, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist", id)));
musicService.addToPlaylist(id, toAdd, context, progressListener);
}
@Override
public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception {
Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist", id)));
musicService.removeFromPlaylist(id, toRemove, context, progressListener);
}
@Override
public void overwritePlaylist(String id, String name, int toRemove, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist", id)));
musicService.overwritePlaylist(id, name, toRemove, toAdd, context, progressListener);
}
@Override
public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception {
Util.delete(new File(context.getCacheDir(), getCacheName(context, "playlist", id)));
musicService.updatePlaylist(id, name, comment, pub, 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 getAlbumList(String type, String extra, int size, int offset, Context context, ProgressListener progressListener) throws Exception {
return musicService.getAlbumList(type, extra, size, offset, context, progressListener);
}
@Override
public MusicDirectory getStarredList(Context context, ProgressListener progressListener) throws Exception {
return musicService.getStarredList(context, progressListener);
}
@Override
public MusicDirectory getRandomSongs(int size, String folder, String genre, String startYear, String endYear, Context context, ProgressListener progressListener) throws Exception {
return musicService.getRandomSongs(size, folder, genre, startYear, endYear, context, progressListener);
}
@Override
public String getCoverArtUrl(Context context, MusicDirectory.Entry entry) throws Exception {
return musicService.getCoverArtUrl(context, entry);
}
@Override
public Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, ProgressListener progressListener) throws Exception {
return musicService.getCoverArt(context, entry, size, 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 String getMusicUrl(Context context, MusicDirectory.Entry song, int maxBitrate) throws Exception {
return musicService.getMusicUrl(context, song, maxBitrate);
}
@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(int maxBitrate, Context context, String id) {
return musicService.getVideoUrl(maxBitrate, context, id);
}
@Override
public String getVideoStreamUrl(String format, int maxBitrate, Context context, String id) throws Exception {
return musicService.getVideoStreamUrl(format, maxBitrate, context, id);
}
@Override
public String getHlsUrl(String id, int bitRate, Context context) throws Exception {
return musicService.getHlsUrl(id, bitRate, context);
}
@Override
public RemoteStatus updateJukeboxPlaylist(List<String> ids, Context context, ProgressListener progressListener) throws Exception {
return musicService.updateJukeboxPlaylist(ids, context, progressListener);
}
@Override
public RemoteStatus skipJukebox(int index, int offsetSeconds, Context context, ProgressListener progressListener) throws Exception {
return musicService.skipJukebox(index, offsetSeconds, context, progressListener);
}
@Override
public RemoteStatus stopJukebox(Context context, ProgressListener progressListener) throws Exception {
return musicService.stopJukebox(context, progressListener);
}
@Override
public RemoteStatus startJukebox(Context context, ProgressListener progressListener) throws Exception {
return musicService.startJukebox(context, progressListener);
}
@Override
public RemoteStatus getJukeboxStatus(Context context, ProgressListener progressListener) throws Exception {
return musicService.getJukeboxStatus(context, progressListener);
}
@Override
public RemoteStatus setJukeboxGain(float gain, Context context, ProgressListener progressListener) throws Exception {
return musicService.setJukeboxGain(gain, context, progressListener);
}
@Override
public void setStarred(List<String> id, List<String> artistId, List<String> albumId, boolean starred, Context context, ProgressListener progressListener) throws Exception {
musicService.setStarred(id, artistId, albumId, starred, context, progressListener);
}
@Override
public List<Share> getShares(Context context, ProgressListener progressListener) throws Exception {
return musicService.getShares(context, progressListener);
}
@Override
public List<Share> createShare(List<String> ids, String description, Long expires, Context context, ProgressListener progressListener) throws Exception {
return musicService.createShare(ids, description, expires, context, progressListener);
}
@Override
public void deleteShare(String id, Context context, ProgressListener progressListener) throws Exception {
musicService.deleteShare(id, context, progressListener);
}
@Override
public void updateShare(String id, String description, Long expires, Context context, ProgressListener progressListener) throws Exception {
musicService.updateShare(id, description, expires, context, progressListener);
}
@Override
public List<ChatMessage> getChatMessages(Long since, Context context, ProgressListener progressListener) throws Exception {
return musicService.getChatMessages(since, context, progressListener);
}
@Override
public void addChatMessage(String message, Context context, ProgressListener progressListener) throws Exception {
musicService.addChatMessage(message, context, progressListener);
}
@Override
public List<Genre> getGenres(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
List<Genre> result = null;
if(!refresh) {
result = FileUtil.deserialize(context, getCacheName(context, "genre"), ArrayList.class);
}
if(result == null) {
result = musicService.getGenres(refresh, context, progressListener);
FileUtil.serialize(context, new ArrayList<Genre>(result), getCacheName(context, "genre"));
}
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);
}
@Override
public List<PodcastChannel> getPodcastChannels(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
checkSettingsChanged(context);
List<PodcastChannel> result = refresh ? null : cachedPodcastChannels.get();
if (result == null) {
if(!refresh) {
result = FileUtil.deserialize(context, getCacheName(context, "podcast"), ArrayList.class);
}
if(result == null) {
result = musicService.getPodcastChannels(refresh, context, progressListener);
FileUtil.serialize(context, new ArrayList<PodcastChannel>(result), getCacheName(context, "podcast"));
}
cachedPodcastChannels.set(result);
}
return result;
}
@Override
public MusicDirectory getPodcastEpisodes(boolean refresh, String id, Context context, ProgressListener progressListener) throws Exception {
String altId = "p-" + id;
MusicDirectory result = null;
if(!refresh) {
result = FileUtil.deserialize(context, getCacheName(context, "directory", altId), MusicDirectory.class, 10);
}
if(result == null) {
result = musicService.getPodcastEpisodes(refresh, id, context, progressListener);
FileUtil.serialize(context, result, getCacheName(context, "directory", altId));
}
return result;
}
@Override
public void refreshPodcasts(Context context, ProgressListener progressListener) throws Exception {
musicService.refreshPodcasts(context, progressListener);
}
@Override
public void createPodcastChannel(String url, Context context, ProgressListener progressListener) throws Exception{
Util.delete(new File(context.getCacheDir(), getCacheName(context, "podcast")));
cachedPodcastChannels.clear();
musicService.createPodcastChannel(url, context, progressListener);
}
@Override
public void deletePodcastChannel(String id, Context context, ProgressListener progressListener) throws Exception{
Util.delete(new File(context.getCacheDir(), getCacheName(context, "podcast")));
cachedPodcastChannels.clear();
musicService.deletePodcastChannel(id, context, progressListener);
}
@Override
public void downloadPodcastEpisode(String id, Context context, ProgressListener progressListener) throws Exception{
musicService.downloadPodcastEpisode(id, context, progressListener);
}
@Override
public void deletePodcastEpisode(String id, Context context, ProgressListener progressListener) throws Exception{
musicService.deletePodcastEpisode(id, context, progressListener);
}
@Override
public void setRating(String id, int rating, Context context, ProgressListener progressListener) throws Exception {
musicService.setRating(id, rating, context, progressListener);
}
@Override
public List<Bookmark> getBookmarks(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
return musicService.getBookmarks(refresh, context, progressListener);
}
@Override
public void createBookmark(String id, int position, String comment, Context context, ProgressListener progressListener) throws Exception {
musicService.createBookmark(id, position, comment, context, progressListener);
}
@Override
public void deleteBookmark(String id, Context context, ProgressListener progressListener) throws Exception {
musicService.deleteBookmark(id, context, progressListener);
}
@Override
public int processOfflineSyncs(final Context context, final ProgressListener progressListener) throws Exception{
return musicService.processOfflineSyncs(context, progressListener);
}
@Override
public void setInstance(Integer instance) throws Exception {
musicService.setInstance(instance);
}
private String getCacheName(Context context, String name, String id) {
String s = musicService.getRestUrl(context, null, false) + id;
return name + "-" + s.hashCode() + ".ser";
}
private String getCacheName(Context context, String name) {
String s = musicService.getRestUrl(context, null, false);
return name + "-" + s.hashCode() + ".ser";
}
private void checkSettingsChanged(Context context) {
String newUrl = musicService.getRestUrl(context, null, false);
boolean newIsTagBrowsing = Util.isTagBrowsing(context);
if (!Util.equals(newUrl, restUrl) || isTagBrowsing != newIsTagBrowsing) {
cachedMusicFolders.clear();
cachedLicenseValid.clear();
cachedIndexes.clear();
cachedPlaylists.clear();
cachedPodcastChannels.clear();
restUrl = newUrl;
isTagBrowsing = newIsTagBrowsing;
}
}
}
|