diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-12-12 15:46:30 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-12-12 15:46:30 -0800 |
commit | 8a906018df507ca68b3df23a971f30f49cb9f610 (patch) | |
tree | da991aa37f96bd3debbc321cb50e590ff5cc7af0 /app/src/main/java | |
parent | 9debb55ea2e35f0759009af036c93ff489516107 (diff) | |
download | dsub-8a906018df507ca68b3df23a971f30f49cb9f610.tar.gz dsub-8a906018df507ca68b3df23a971f30f49cb9f610.tar.bz2 dsub-8a906018df507ca68b3df23a971f30f49cb9f610.zip |
Add songs to SongDBHandler in normal viewing
Diffstat (limited to 'app/src/main/java')
3 files changed, 60 insertions, 10 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java b/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java index 42c97b9e..6dc56bb3 100644 --- a/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java +++ b/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java @@ -107,6 +107,15 @@ public class MusicDirectory implements Serializable { } return result; } + public List<Entry> getSongs() { + List<Entry> result = new ArrayList<Entry>(); + for (Entry child : children) { + if (child != null && !child.isDirectory() && !child.isVideo()) { + result.add(child); + } + } + return result; + } public int getChildrenSize() { return children.size(); diff --git a/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java b/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java index 68ca36a7..e188f97f 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java @@ -53,6 +53,7 @@ import github.daneren2005.dsub.domain.Share; import github.daneren2005.dsub.domain.User; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.util.ProgressListener; +import github.daneren2005.dsub.util.SongDBHandler; import github.daneren2005.dsub.util.SyncUtil; import github.daneren2005.dsub.util.TimeLimitedCache; import github.daneren2005.dsub.util.FileUtil; @@ -171,6 +172,7 @@ public class CachedMusicService implements MusicService { protected Void doInBackground() throws Throwable { Util.sleepQuietly(2000L); MusicDirectory refreshed = musicService.getMusicDirectory(id, name, true, context, null); + updateAllSongs(context, refreshed); cached.updateDifferences(context, musicService.getInstance(context), refreshed); FileUtil.serialize(context, refreshed, getCacheName(context, "directory", id)); return null; @@ -193,6 +195,7 @@ public class CachedMusicService implements MusicService { if(dir == null) { dir = musicService.getMusicDirectory(id, name, refresh, context, progressListener); + updateAllSongs(context, dir); FileUtil.serialize(context, dir, getCacheName(context, "directory", id)); // If a cached copy exists to check against, look for removes @@ -231,6 +234,7 @@ public class CachedMusicService implements MusicService { if(dir == null) { dir = musicService.getAlbum(id, name, refresh, context, progressListener); + updateAllSongs(context, dir); FileUtil.serialize(context, dir, getCacheName(context, "album", id)); // If a cached copy exists to check against, look for removes @@ -254,6 +258,8 @@ public class CachedMusicService implements MusicService { } if(dir == null) { dir = musicService.getPlaylist(refresh, id, name, context, progressListener); + // With large playlists this takes too long + // updateAllSongs(context, dir); FileUtil.serialize(context, dir, getCacheName(context, "playlist", id)); File playlistFile = FileUtil.getPlaylistFile(context, Util.getServerName(context, musicService.getInstance(context)), dir.getName()); @@ -1500,6 +1506,13 @@ public class CachedMusicService implements MusicService { } } + protected void updateAllSongs(Context context, MusicDirectory dir) { + List<Entry> songs = dir.getSongs(); + if(!songs.isEmpty()) { + SongDBHandler.getHandler(context).addSongs(musicService.getInstance(context), songs); + } + } + private void checkSettingsChanged(Context context) { int instance = musicService.getInstance(context); String newUrl = musicService.getRestUrl(context, null, false); diff --git a/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java b/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java index 63de6d48..eb52de9c 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java +++ b/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java @@ -22,6 +22,9 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import java.util.ArrayList; +import java.util.List; + import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.service.DownloadFile; @@ -95,6 +98,31 @@ public class SongDBHandler extends SQLiteOpenHelper { db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_IGNORE); } + public synchronized void addSongs(int instance, List<MusicDirectory.Entry> entries) { + SQLiteDatabase db = this.getWritableDatabase(); + + List<Pair<String, String>> pairs = new ArrayList<>(); + for(MusicDirectory.Entry entry: entries) { + pairs.add(new Pair<>(entry.getId(), FileUtil.getSongFile(context, entry).getAbsolutePath())); + } + addSongs(db, instance, pairs); + + db.close(); + } + public synchronized void addSongs(SQLiteDatabase db, int instance, List<Pair<String, String>> entries) { + addSongsImpl(db, Util.getRestUrlHash(context, instance), entries); + } + protected synchronized void addSongsImpl(SQLiteDatabase db, int serverKey, List<Pair<String, String>> entries) { + for(Pair<String, String> entry: entries) { + ContentValues values = new ContentValues(); + values.put(SONGS_SERVER_KEY, serverKey); + values.put(SONGS_SERVER_ID, entry.getFirst()); + values.put(SONGS_COMPLETE_PATH, entry.getSecond()); + + db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_IGNORE); + } + } + public synchronized void setSongPlayed(DownloadFile downloadFile, boolean submission) { // TODO: In case of offline want to update all matches Pair<Integer, String> pair = getOnlineSongId(downloadFile); @@ -115,13 +143,13 @@ public class SongDBHandler extends SQLiteOpenHelper { db.close(); } - public Long[] getLastPlayed(MusicDirectory.Entry entry) { + public synchronized Long[] getLastPlayed(MusicDirectory.Entry entry) { return getLastPlayed(getOnlineSongId(entry)); } - protected Long[] getLastPlayed(Pair<Integer, String> pair) { + protected synchronized Long[] getLastPlayed(Pair<Integer, String> pair) { return getLastPlayed(pair.getFirst(), pair.getSecond()); } - public Long[] getLastPlayed(int serverKey, String id) { + public synchronized Long[] getLastPlayed(int serverKey, String id) { SQLiteDatabase db = this.getReadableDatabase(); String[] columns = {SONGS_LAST_PLAYED, SONGS_LAST_COMPLETED}; @@ -139,20 +167,20 @@ public class SongDBHandler extends SQLiteOpenHelper { return null; } - public Pair<Integer, String> getOnlineSongId(MusicDirectory.Entry entry) { + public synchronized Pair<Integer, String> getOnlineSongId(MusicDirectory.Entry entry) { return getOnlineSongId(new DownloadFile(context, entry, true)); } - public Pair<Integer, String> getOnlineSongId(DownloadFile downloadFile) { + public synchronized Pair<Integer, String> getOnlineSongId(DownloadFile downloadFile) { return getOnlineSongId(Util.getRestUrlHash(context), downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath(), Util.isOffline(context) ? false : true); } - public Pair<Integer, String> getOnlineSongId(int serverKey, MusicDirectory.Entry entry) { + public synchronized Pair<Integer, String> getOnlineSongId(int serverKey, MusicDirectory.Entry entry) { return getOnlineSongId(serverKey, new DownloadFile(context, entry, true)); } - public Pair<Integer, String> getOnlineSongId(int serverKey, DownloadFile downloadFile) { + public synchronized Pair<Integer, String> getOnlineSongId(int serverKey, DownloadFile downloadFile) { return getOnlineSongId(serverKey, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath(), true); } - public Pair<Integer, String> getOnlineSongId(int serverKey, String id, String savePath, boolean requireServerKey) { + public synchronized Pair<Integer, String> getOnlineSongId(int serverKey, String id, String savePath, boolean requireServerKey) { SharedPreferences prefs = Util.getPreferences(context); String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null); if(cacheLocn != null && id.indexOf(cacheLocn) != -1) { @@ -166,7 +194,7 @@ public class SongDBHandler extends SQLiteOpenHelper { } } - public Pair<Integer, String> getIdFromPath(String path) { + public synchronized Pair<Integer, String> getIdFromPath(String path) { SQLiteDatabase db = this.getReadableDatabase(); String[] columns = {SONGS_SERVER_KEY, SONGS_SERVER_ID}; @@ -179,7 +207,7 @@ public class SongDBHandler extends SQLiteOpenHelper { return null; } - public Pair<Integer, String> getIdFromPath(int serverKey, String path) { + public synchronized Pair<Integer, String> getIdFromPath(int serverKey, String path) { SQLiteDatabase db = this.getReadableDatabase(); String[] columns = {SONGS_SERVER_KEY, SONGS_SERVER_ID}; |