Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
refactoring + bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vhaudiquet committed Jun 16, 2018
1 parent 1b5771e commit 6137b72
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 103 deletions.
136 changes: 34 additions & 102 deletions app/src/main/java/v/blade/library/LibraryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.graphics.BitmapFactory;
import android.os.Looper;
import android.util.Log;
import retrofit.RetrofitError;
import v.blade.ui.settings.SettingsActivity;

import java.io.*;
Expand Down Expand Up @@ -430,132 +429,65 @@ private static void registerSongBetterSources()

int addedSongs = 0;

if(bestSource == Source.SOURCE_SPOTIFY)
//search all songs on best source
ArrayList<Song> bestSourceSongs = new ArrayList<>();
for(Song s : songs)
{
//search all songs on spotify
ArrayList<Song> spotifySongs = new ArrayList<>();
for(Song s : songs)
if(s.getSources().getSourceByPriority(0).getSource() != bestSource && s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_LOCAL_LIB)
{
if(s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_SPOTIFY && s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_LOCAL_LIB)
{
//query spotify for this song
try
{
if(Source.SOURCE_SPOTIFY.searchForSong(s))
{
spotifySongs.add(s);
}
addedSongs++;
if(addedSongs >= BETTER_SOURCES_MAX) break;
}
catch (RetrofitError error)
{
//TODO : handle error
continue;
}
}
}
//also search for songs in playlist
if(!SAVE_PLAYLISTS_TO_LIBRARY)
{
for(Playlist p : playlists)
//query bestSource for this song
try
{
for(Song s : p.getContent())
if(bestSource.searchForSong(s))
{
if(s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_SPOTIFY && s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_LOCAL_LIB)
{
if(Source.SOURCE_SPOTIFY.searchForSong(s))
{
spotifySongs.add(s);
}
addedSongs++;
if(addedSongs >= BETTER_SOURCES_MAX) break;
}
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 : spotifySongs)
catch (Exception error)
{
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");
//TODO : handle error
continue;
}
randomAccessFile.close();
}
catch(IOException e) {e.printStackTrace();}

}
else if(bestSource == Source.SOURCE_DEEZER)
//also search for songs in playlist
if(!SAVE_PLAYLISTS_TO_LIBRARY)
{
ArrayList<Song> deezerSongs = new ArrayList<>();

//search all songs on deezer
synchronized (songs)
for(Playlist p : playlists)
{
for (Song s : songs)
for(Song s : p.getContent())
{
if (s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_DEEZER && s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_LOCAL_LIB)
if(s.getSources().getSourceByPriority(0).getSource() != bestSource && s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_LOCAL_LIB)
{
if(Source.SOURCE_DEEZER.searchForSong(s))
if(bestSource.searchForSong(s))
{
deezerSongs.add(s);
bestSourceSongs.add(s);
}
addedSongs++;
if(addedSongs >= BETTER_SOURCES_MAX) break;
}
}
}
//also search for songs in playlist
if(!SAVE_PLAYLISTS_TO_LIBRARY)
{
synchronized (playlists)
{
for(Playlist p : playlists)
{
synchronized (p.getContent())
{
for(Song s : p.getContent())
{
if(s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_DEEZER && s.getSources().getSourceByPriority(0).getSource() != Source.SOURCE_LOCAL_LIB)
{
if(Source.SOURCE_DEEZER.searchForSong(s))
{
deezerSongs.add(s);
}
addedSongs++;
if(addedSongs >= BETTER_SOURCES_MAX) break;
}
}
}
}
}
}
}

//cache theses
try
//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)
{
//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 : deezerSongs)
{
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();
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");
}
catch(IOException e) {e.printStackTrace();}
randomAccessFile.close();
}
catch(IOException e) {e.printStackTrace();}

if(currentCallback != null) currentCallback.onLibraryChange();
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/v/blade/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ static void showAddToPlaylist(Activity context, LibraryObject object)
{Playlist p = list.get(i); if(!p.isMine() && !p.isCollaborative()) list.remove(i);}
list.add(0, new Playlist(context.getString(R.string.new_playlist), null));

LibraryObjectAdapter adapter = new LibraryObjectAdapter(context, list);
LibraryObjectAdapter adapter = new LibraryObjectAdapter(context, list, false);
adapter.setHideMore(true);
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.add_to_playlist))
Expand Down

0 comments on commit 6137b72

Please sign in to comment.