This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
LibraryService.java
executable file
·835 lines (732 loc) · 31.9 KB
/
LibraryService.java
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
package v.blade.library;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Looper;
import android.util.Log;
import v.blade.ui.settings.SettingsActivity;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
/*
* LibraryService is the class that handles library caching/restore and synchronization
* It was a background service, but it works actually better as a static class that just calls a Thread to do async work
*/
public class LibraryService
{
private static final boolean LOG_REGISTER_SONG = false;
private static final int BETTER_SOURCES_MAX = 20; //stop better sources after 20 registered songs (avoid query limit)
static final String CACHE_SEPARATOR = "##";
/* user preferences */
public static boolean configured = false;
public static boolean SAVE_PLAYLISTS_TO_LIBRARY;
public static boolean REGISTER_SONGS_BETTER_SOURCES;
public static Uri TREE_URI;
public static boolean ENABLE_SONG_CHANGE_ANIM;
/* library */
private static final List<Artist> artists = Collections.synchronizedList(new ArrayList<Artist>());
private static final List<Album> albums = Collections.synchronizedList(new ArrayList<Album>());
private static final List<Song> songs = Collections.synchronizedList(new ArrayList<Song>());
private static final List<Playlist> playlists = Collections.synchronizedList(new ArrayList<Playlist>());
//song handles, that are not part of library but playable (from web sources)
private static final List<Song> handles = Collections.synchronizedList(new ArrayList<Song>());
private static final List<Album> albumHandles = Collections.synchronizedList(new ArrayList<Album>());
private static final List<Artist> artistHandles = Collections.synchronizedList(new ArrayList<Artist>());
static HashMap<String, ArrayList<Song>> songsByName = new HashMap<>();
//song linkss
public static final HashMap<Song, List<Song>> songLinks = new HashMap<>();
/* list callbacks */
public interface UserLibraryCallback{void onLibraryChange();}
public static UserLibraryCallback currentCallback;
private static File artCacheDir;
private static File betterSourceFile;
private static File songLinksFile;
public static List<Artist> getArtists() {return artists;}
public static List<Album> getAlbums() {return albums;}
public static List<Song> getSongs() {return songs;}
public static List<Playlist> getPlaylists() {return playlists;}
static Context appContext;
/* synchronization notification management */
public static volatile boolean synchronization;
public static volatile boolean loadingDone;
public static Thread syncThread;
public static void registerInit()
{
new Thread()
{
public void run()
{
Looper.prepare();
registerCachedSongs();
sortLibrary();
}
}.start();
}
/*
* Register local songs and web libraries that were previously cached
*/
private static void registerCachedSongs()
{
if(!configured) return;
//restore sources cache
for(Source s : Source.SOURCES) s.registerCachedSongs();
//restore song better source cache
if(REGISTER_SONGS_BETTER_SOURCES)
{
try
{
if(betterSourceFile.exists())
{
RandomAccessFile raf = new RandomAccessFile(betterSourceFile.getAbsolutePath(), "r");
//better sources
Source bestSource = Source.SOURCE_DEEZER.getPriority() > Source.SOURCE_SPOTIFY.getPriority() ? Source.SOURCE_DEEZER : Source.SOURCE_SPOTIFY;
while(raf.getFilePointer() < raf.length())
{
String[] tp = raf.readUTF().split(CACHE_SEPARATOR);
Song song = bestSource == Source.SOURCE_DEEZER ?
getSongHandle(tp[0], tp[1], tp[2], Long.parseLong(tp[5]),
new SongSources.SongSource(Long.parseLong(tp[6]), bestSource), Integer.parseInt(tp[4]), 0) :
getSongHandle(tp[0], tp[1], tp[2], Long.parseLong(tp[5]),
new SongSources.SongSource(tp[6], bestSource), Integer.parseInt(tp[4]), 0);
song.setFormat(tp[3]);
}
raf.close();
}
}
catch(IOException e)
{
Log.println(Log.ERROR, "[BLADE-CACHE]", "Cache restore : IOException");
e.printStackTrace();
}
}
registerSongLinks();
loadingDone = true;
new Thread()
{
public void run()
{
for(Source s : Source.SOURCES)
{
s.loadCachedArts();
}
}
}.start();
}
/*
* Configure the APIs, the preferences, ...
*/
public static void configureLibrary(Context appContext)
{
if(configured) return;
LibraryService.appContext = appContext;
//init cache dirs
artCacheDir = new File(appContext.getCacheDir().getAbsolutePath() + "/arts");
if(!artCacheDir.exists()) artCacheDir.mkdir();
betterSourceFile = new File(appContext.getCacheDir().getAbsolutePath() + "/betterSources.cached");
songLinksFile = new File(appContext.getCacheDir().getAbsolutePath() + "/songLinks.cached");
//get preferences
SharedPreferences accountsPrefs = appContext.getSharedPreferences(SettingsActivity.PREFERENCES_ACCOUNT_FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences generalPrefs = appContext.getSharedPreferences(SettingsActivity.PREFERENCES_GENERAL_FILE_NAME, Context.MODE_PRIVATE);
String treeUri = generalPrefs.getString("sdcard_uri", null);
if(treeUri != null) TREE_URI = Uri.parse(treeUri);
ENABLE_SONG_CHANGE_ANIM = generalPrefs.getBoolean("anim_0", true);
SAVE_PLAYLISTS_TO_LIBRARY = generalPrefs.getBoolean("save_playlist_to_library", false);
REGISTER_SONGS_BETTER_SOURCES = generalPrefs.getBoolean("register_better_sources", true);
//setup each source
for(Source s : Source.SOURCES) s.initConfig(accountsPrefs);
configured = true;
}
/*
* Registers a song in user library
*/
public static Song registerSong(String artist, String album, int albumTrack, int year, long duration, String name,
SongSources.SongSource source)
{
//REGISTER : this song is in the library of this source
source.setLibrary(true);
//check if the song is already registered
ArrayList<Song> snames = songsByName.get(name.toLowerCase());
if(snames != null)
{
for(Song s : snames)
{
if(s.getArtist().getName().equalsIgnoreCase(artist) && s.getAlbum().getName().equalsIgnoreCase(album))
{
if(LOG_REGISTER_SONG) System.out.println("[REGISTER] Found song " + s.getTitle() + " - " + s.getAlbum().getName() + " - " + s.getArtist().getName() + " SOURCE " + source.getSource());
s.getSources().addSource(source);
s.getAlbum().getSources().addSource(source);
s.getArtist().getSources().addSource(source);
return s;
}
}
}
//check if the song is already handled
synchronized(handles)
{
for(Song s : handles)
{
if(s.getTitle().equalsIgnoreCase(name) && s.getArtist().getName().equalsIgnoreCase(artist) && s.getAlbum().getName().equalsIgnoreCase(album))
{
s.getSources().addSource(source);
if(s.getAlbum().isHandled()) {albums.add(s.getAlbum()); s.getAlbum().setHandled(false); s.getArtist().addAlbum(s.getAlbum()); albumHandles.remove(s.getAlbum());}
if(s.getArtist().isHandled()) {artists.add(s.getArtist());s.getArtist().setHandled(false);artistHandles.remove(s.getArtist());}
s.getAlbum().addSong(s);
s.setHandled(false);
handles.remove(s);
songs.add(s);
//register song by name
if(snames != null) snames.add(s);
else {ArrayList<Song> sn = new ArrayList<>(); sn.add(s); songsByName.put(name.toLowerCase(), sn);}
if(LOG_REGISTER_SONG) System.out.println("[REGISTER] Found handled song " + s.getTitle() + " - " + s.getAlbum().getName() + " - " + s.getArtist().getName() + " SOURCE " + source.getSource());
return s;
}
}
}
Artist songArtist = null;
synchronized (artists)
{for (Artist art : artists) if (art.getName().equalsIgnoreCase(artist)) songArtist = art;}
if(songArtist == null)
{
songArtist = new Artist(artist);
artists.add(songArtist);
}
songArtist.getSources().addSource(source);
Album songAlbum = null;
synchronized (songArtist.getAlbums())
{
for(int i = 0;i<songArtist.getAlbums().size();i++)
if(songArtist.getAlbums().get(i).getName().equalsIgnoreCase(album))
songAlbum = songArtist.getAlbums().get(i);
}
if(songAlbum == null)
{
songAlbum = new Album(album, songArtist);
albums.add(songAlbum);
songArtist.addAlbum(songAlbum);
}
songAlbum.getSources().addSource(source);
Song song = new Song(name, songArtist, songAlbum, albumTrack, duration, year);
song.getSources().addSource(source);
songAlbum.addSong(song);
songs.add(song);
if(currentCallback != null) currentCallback.onLibraryChange();
//register song by name
if(snames != null) snames.add(song);
else {ArrayList<Song> sn = new ArrayList<>(); sn.add(song); songsByName.put(name.toLowerCase(), sn);}
if(LOG_REGISTER_SONG) System.out.println("[REGISTER] Registered : " + name + " - " + songAlbum.getName() + " - " + songArtist.getName() + " - SOURCE " + source.getSource());
return song;
}
public static void unregisterSong(Song song, SongSources.SongSource toUnregister)
{
//remove songsource
SongSources sources = song.getSources();
sources.removeSource(toUnregister);
boolean isInLibrary = false;
for(SongSources.SongSource s : sources.sources) if(s != null) if(s.getLibrary()) {isInLibrary = true; break;}
if(!isInLibrary)
{
//remove from library
LibraryService.getSongs().remove(song);
if(LibraryService.currentCallback != null) LibraryService.currentCallback.onLibraryChange();
//remove from album and set handled + remove album if last one song
song.getAlbum().getSongs().remove(song);
song.setHandled(true);
LibraryService.handles.add(song);
boolean albumHandled = true;
for(Song s : song.getAlbum().getSongs()) if(!s.isHandled()) albumHandled = false;
if(albumHandled)
{
//remove from library
LibraryService.getSongs().remove(song);
if(LibraryService.currentCallback != null) LibraryService.currentCallback.onLibraryChange();
//remove from artist and set handled + remove artist if last one album
song.getArtist().getAlbums().remove(song.getAlbum());
song.getAlbum().setHandled(true);
LibraryService.albumHandles.add(song.getAlbum());
boolean artistHandled = true;
for(Album a : song.getArtist().getAlbums()) if(!a.isHandled()) artistHandled = false;
if(artistHandled)
{
//remove from library
LibraryService.getArtists().remove(song.getArtist());
if(LibraryService.currentCallback != null) LibraryService.currentCallback.onLibraryChange();
//set handled
song.getArtist().setHandled(true);
artistHandles.add(song.getArtist());
}
}
}
}
/*
* Link a song to another (to say they are the same)
* source gets removed and songsources are merged
*/
public static void linkSong(Song source, Song destination, boolean save)
{
if(source == destination) return;
for(SongSources.SongSource src : source.getSources().sources)
{
if(src != null)
{
destination.getSources().addSource(src); //addSource is checking for double-add
unregisterSong(source, src);
}
}
if(source.isHandled()) handles.remove(source);
//replace song in playlists
synchronized (playlists)
{
for(Playlist p : playlists)
{
int index = p.getContent().indexOf(source);
if(index != -1)
{
p.getContent().remove(index);
p.getContent().add(index, destination);
}
}
}
if(currentCallback != null) currentCallback.onLibraryChange();
List<Song> list = songLinks.get(destination);
if(list == null) {list = new ArrayList<>(); songLinks.put(destination, list);}
list.add(source);
//save songlinks to cache (by rewriting hashmap)
if(save) writeLinks();
}
/*
* Rewrite song links on disk
*/
public static void writeLinks()
{
try
{
songLinksFile.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(songLinksFile));
for(Song s : songLinks.keySet())
{
List<Song> links = songLinks.get(s);
writer.write(s.getArtist() + CACHE_SEPARATOR + s.getAlbum() + CACHE_SEPARATOR + s.getTitle() + CACHE_SEPARATOR +
links.size() + CACHE_SEPARATOR);
for(Song linked : links)
writer.write(linked.getArtist() + CACHE_SEPARATOR + linked.getAlbum() + CACHE_SEPARATOR + linked.getTitle() + CACHE_SEPARATOR);
writer.newLine();
}
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/*
* Synchronize local/cached library with local/web library
* This method is asynchronous
*/
public static final int ERROR_LOADING_NOT_DONE = 1;
public interface SynchronizeCallback
{
void synchronizeDone();
void synchronizeFail(int error);
}
public static void synchronizeLibrary(SynchronizeCallback callback)
{
if(!loadingDone) {callback.synchronizeFail(ERROR_LOADING_NOT_DONE); return;}
synchronization = true;
syncThread = new Thread()
{
public void run()
{
Looper.prepare();
//copy all songs in library to HANDLES so that they don't need complete resync (better sources search, img load)
synchronized (songs)
{
for(Song s : songs) {handles.add(s); s.setHandled(true);}
}
synchronized (albums)
{
for(Album a : albums) {albumHandles.add(a); a.setHandled(true); a.getSongs().clear();}
}
synchronized (artists)
{
for(Artist a : artists) {artistHandles.add(a); a.setHandled(true); a.getAlbums().clear();}
}
for(Source s : Source.SOURCES) s.registerSongs();
registerSongLinks();
registerSongBetterSources();
sortLibrary();
synchronization = false;
callback.synchronizeDone();
}
};
syncThread.setName("SYNC_THREAD");
syncThread.setDaemon(true);
syncThread.start();
}
/*
* If the option is enabled, Blade will try to find a better source for all the songs added by WebService
* Example : you added a spotify album, but deezer prior > spotify ; Blade will load that album from Deezer
*/
private static void registerSongBetterSources()
{
if(!configured) return;
if(!REGISTER_SONGS_BETTER_SOURCES) return;
Source bestSource = Source.SOURCE_DEEZER.getPriority() > Source.SOURCE_SPOTIFY.getPriority() ? Source.SOURCE_DEEZER : Source.SOURCE_SPOTIFY;
if(!bestSource.isAvailable()) return;
int addedSongs = 0;
//search all songs on best source
ArrayList<Song> bestSourceSongs = new ArrayList<>();
synchronized (songs)
{
for(Song s : songs)
{
if(s.getSources().getSourceByPriority(0).getSource() != bestSource && s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_LOCAL_LIB)
{
//query bestSource for this song
try
{
if(bestSource.searchForSong(s))
{
bestSourceSongs.add(s);
}
addedSongs++;
if(addedSongs >= BETTER_SOURCES_MAX) break;
}
catch (Exception error)
{
//TODO : handle error
continue;
}
}
}
}
//also search for songs in playlist
if(!SAVE_PLAYLISTS_TO_LIBRARY)
{
for(Playlist p : playlists)
{
for(Song s : p.getContent())
{
if(s.getSources().getSourceByPriority(0).getSource() != bestSource && s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_LOCAL_LIB)
{
if(bestSource.searchForSong(s))
{
bestSourceSongs.add(s);
}
addedSongs++;
if(addedSongs >= BETTER_SOURCES_MAX) break;
}
}
}
}
//cache theses
try
{
//TODO : for now we keep betterSources forever, find a way to get rid of unused ones
if(betterSourceFile.exists()) betterSourceFile.createNewFile();
RandomAccessFile randomAccessFile = new RandomAccessFile(betterSourceFile.getAbsolutePath(), "rw");
randomAccessFile.seek(randomAccessFile.length());
for(Song song : bestSourceSongs)
{
randomAccessFile.writeUTF(song.getTitle() + CACHE_SEPARATOR + song.getAlbum().getName() + CACHE_SEPARATOR + song.getArtist().getName() + CACHE_SEPARATOR
+ song.getFormat() + CACHE_SEPARATOR + song.getTrackNumber() + CACHE_SEPARATOR + song.getDuration() + CACHE_SEPARATOR + song.getSources().getSourceByPriority(0).getId()
+ CACHE_SEPARATOR + "\n");
}
randomAccessFile.close();
}
catch(IOException e) {e.printStackTrace();}
if(currentCallback != null) currentCallback.onLibraryChange();
}
/*
* Register cached user-defined song links
*/
private static void registerSongLinks()
{
if(!configured) return;
if(!songLinksFile.exists()) return;
try
{
BufferedReader reader = new BufferedReader(new FileReader(songLinksFile));
while (reader.ready())
{
String[] line = reader.readLine().split(CACHE_SEPARATOR);
//System.out.println(Arrays.toString(line));
Song song = getSongHandle(line[2], line[1], line[0], 0, null, 0, 0);
if(song == null) continue;
int size = Integer.parseInt(line[3]);
for(int i = 0;i<size;i++)
{
Song toLink = getSongHandle(line[6+i], line[5+i], line[4+i], 0, null, 0, 0);
if(toLink == null) continue;
linkSong(toLink, song, false);
}
}
reader.close();
}
catch (IOException e)
{
System.err.println("registerSongLinks() encoutered IOException");
e.printStackTrace();
}
}
public static void sortLibrary()
{
/* sort collection by alphabetical order */
synchronized (songs)
{Collections.sort(songs, new Comparator<Song>(){
public int compare(Song a, Song b){ if(a.getTitle() != null && b.getTitle() != null) return a.getTitle().toLowerCase().compareTo(b.getTitle().toLowerCase()); else return 0;}
});}
synchronized (albums)
{Collections.sort(albums, new Comparator<Album>(){
public int compare(Album a, Album b){ if(a.getName() != null && b.getName() != null) return a.getName().toLowerCase().compareTo(b.getName().toLowerCase()); else return 0;
}
});}
synchronized (artists)
{Collections.sort(artists, new Comparator<Artist>(){
public int compare(Artist a, Artist b){ if(a.getName() != null && b.getName() != null) return a.getName().toLowerCase().compareTo(b.getName().toLowerCase()); else return 0;
}
});}
synchronized (playlists)
{Collections.sort(playlists, new Comparator<Playlist>(){
public int compare(Playlist a, Playlist b){ if(a.getName() != null && b.getName() != null) return a.getName().toLowerCase().compareTo(b.getName().toLowerCase()); else return 0;
}
});}
if(currentCallback != null) currentCallback.onLibraryChange();
/* sort each album per tracks */
synchronized (albums)
{
for(Album alb : albums)
{
synchronized (alb.getSongs())
{Collections.sort(alb.getSongs(), new Comparator<Song>() {
@Override
public int compare(Song o1, Song o2) {return o1.getTrackNumber() - o2.getTrackNumber();}
});}
}
}
if(currentCallback != null) currentCallback.onLibraryChange();
}
/*
* Query the library for songs
*/
public static ArrayList<Song> querySongs(String s)
{
ArrayList<Song> tr = new ArrayList<>();
String q = s.toLowerCase();
synchronized(songs)
{
for(Song song : songs)
if(song.getTitle().toLowerCase().contains(q))
tr.add(song);
}
return tr;
}
/*
* Query the library for objects
*/
public static ArrayList<LibraryObject> query(String s)
{
ArrayList<LibraryObject> tr = new ArrayList<>();
String q = s.toLowerCase();
synchronized(songs)
{
for(Song song : songs)
if(song.getTitle().toLowerCase().contains(q))
tr.add(song);
}
synchronized(albums)
{
for(Album alb : albums)
if(alb.getName().toLowerCase().contains(q))
tr.add(alb);
}
synchronized(artists)
{
for(Artist artist : artists)
if(artist.getName().toLowerCase().contains(q))
tr.add(artist);
}
synchronized(playlists)
{
for(Playlist playlist : playlists)
if(playlist.getName() != null)
if(playlist.getName().toLowerCase().contains(q))
tr.add(playlist);
}
return tr;
}
/*
* Query all web sources + local library for objects
*/
public static ArrayList<LibraryObject> queryWeb(String s)
{
ArrayList<LibraryObject> tr = new ArrayList<>();
String q = s.toLowerCase();
for(Source src : Source.SOURCES) tr.addAll(src.query(s));
// add results from local query
tr.addAll(query(s));
//remove doublons (oh my god n*m)
ArrayList<LibraryObject> trfinal = new ArrayList<>();
for(LibraryObject libraryObject : tr)
{
if(!trfinal.contains(libraryObject)) trfinal.add(libraryObject);
}
//sort (songs first, then albums, then artists)
Collections.sort(trfinal, new Comparator<LibraryObject>()
{
@Override
public int compare(LibraryObject o1, LibraryObject o2)
{
if(o1 instanceof Song && o2 instanceof Song)
return o2.getSources().getSourceByPriority(0).getSource().getPriority() - o1.getSources().getSourceByPriority(0).getSource().getPriority();
else if(o1 instanceof Song && o2 instanceof Album) return -2;
else if(o1 instanceof Song && o2 instanceof Artist) return -3;
else if(o1 instanceof Album && o2 instanceof Song) return 2;
else if(o1 instanceof Artist && o2 instanceof Song) return 3;
else if(o1 instanceof Album && o2 instanceof Artist) return -2;
else if(o1 instanceof Artist && o2 instanceof Album) return 2;
return 0;
}
});
return trfinal;
}
static Song getSongHandle(String name, String album, String artist, long duration, SongSources.SongSource source, int track, int year)
{
//if song is already registered, return song from library
ArrayList<Song> snames = songsByName.get(name.toLowerCase()); //TODO : fix sync problems
if(snames != null)
{
//check if the song is already registered
for(Song s : snames)
{
if(s.getArtist().getName().equalsIgnoreCase(artist) && s.getAlbum().getName().equalsIgnoreCase(album))
{
if(source != null)
{
s.getSources().addSource(source);
s.getAlbum().getSources().addSource(source);
s.getArtist().getSources().addSource(source);
}
if(LOG_REGISTER_SONG) System.out.println("[HANDLE] Found registered song : " + s.getTitle() + " - " + s.getAlbum().getName() + " - " + s.getArtist().getName() + " - SOURCE " + source.getSource());
return s;
}
}
}
for(Song s : handles)
if(s.getTitle().equalsIgnoreCase(name) && s.getArtist().getName().equalsIgnoreCase(artist) && s.getAlbum().getName().equalsIgnoreCase(album))
{
if(source != null)
{
s.getSources().addSource(source);
s.getAlbum().getSources().addSource(source);
s.getArtist().getSources().addSource(source);
}
if(LOG_REGISTER_SONG) System.out.println("[HANDLE] Found handled song : " + s.getTitle() + " - " + s.getAlbum().getName() + " - " + s.getArtist().getName() + " - SOURCE " + source.getSource());
return s;
}
if(source == null) return null;
//else create song object
Artist songArtist = null;
synchronized (artists)
{for(Artist art : artists) if(art.getName().equalsIgnoreCase(artist)) songArtist = art;}
if(songArtist == null) for(Artist art : artistHandles) if(art.getName().equalsIgnoreCase(artist)) songArtist = art;
if(songArtist == null) {songArtist = new Artist(artist); songArtist.setHandled(true); artistHandles.add(songArtist);}
songArtist.getSources().addSource(source);
Album songAlbum = null;
synchronized (albums)
{
for(Album alb : albums)
if(alb.getName().equalsIgnoreCase(album) && alb.getArtist().getName().equalsIgnoreCase(songArtist.getName()))
songAlbum = alb;
}
if(songAlbum == null)
synchronized (albumHandles)
{
for (Album alb : albumHandles)
if (alb.getName().equalsIgnoreCase(album) && alb.getArtist().getName().equalsIgnoreCase(songArtist.getName()))
songAlbum = alb;
}
if(songAlbum == null) {songAlbum = new Album(album, songArtist); songAlbum.setHandled(true); albumHandles.add(songAlbum);}
songAlbum.getSources().addSource(source);
Song s = new Song(name, songArtist, songAlbum, track, duration, year);
s.getSources().addSource(source);
s.setHandled(true);
handles.add(s);
if(LOG_REGISTER_SONG) System.out.println("[HANDLE] Handled : " + name + " - " + songAlbum.getName() + " - " + songArtist.getName() + " SOURCE " + source.getSource());
return s;
}
static void loadArt(LibraryObject obj, String path, boolean local)
{
if(local)
{
BitmapFactory.Options options = new BitmapFactory.Options();
//decode file bounds
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
//calculate resize to do
int inSampleSize = calculateSampleSize(options, Album.minatureSize, Album.minatureSize);
//load miniature
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
Bitmap toSet = BitmapFactory.decodeFile(path, options);
if(toSet != null) obj.setArt(path, toSet);
}
else
{
String fileName = obj.getName();
if(obj instanceof Album) fileName += "." + ((Album) obj).getArtist().getName();
if(fileName.contains("/")) fileName = fileName.replaceAll("/", "#");
fileName += "." + obj.getType();
File toSave = new File(artCacheDir.getAbsolutePath() + "/" + fileName + ".png");
if(!toSave.exists())
{
try
{
URLConnection connection = new URL(path).openConnection();
connection.setUseCaches(true);
OutputStream fos = new FileOutputStream(toSave);
InputStream nis = connection.getInputStream();
//copy stream
byte[] buffer = new byte[4096];
while(true)
{
try
{
int count = nis.read(buffer, 0, 4096);
if(count == -1) break;
fos.write(buffer, 0, count);
}
catch(InterruptedIOException ex)
{
ex.printStackTrace();
}
}
connection.getInputStream().close();
}
catch(Exception e)
{
Log.println(Log.WARN, "[BLADE]", "Exception on decoding art for object " + obj.getName() + " : " + path);
e.printStackTrace();
return;
}
}
loadArt(obj, toSave.getPath(), true);
}
}
private static int calculateSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if(height > reqHeight || width > reqWidth)
{
final int halfHeight = height/2;
final int halfWidth = width/2;
while((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth)
inSampleSize*=2;
}
return inSampleSize;
}
}