From 8a906018df507ca68b3df23a971f30f49cb9f610 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 12 Dec 2015 15:46:30 -0800 Subject: Add songs to SongDBHandler in normal viewing --- .../daneren2005/dsub/domain/MusicDirectory.java | 9 ++++ .../dsub/service/CachedMusicService.java | 13 ++++++ .../daneren2005/dsub/util/SongDBHandler.java | 48 +++++++++++++++++----- 3 files changed, 60 insertions(+), 10 deletions(-) (limited to 'app/src/main/java') 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 getSongs() { + List result = new ArrayList(); + 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 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 entries) { + SQLiteDatabase db = this.getWritableDatabase(); + + List> 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> entries) { + addSongsImpl(db, Util.getRestUrlHash(context, instance), entries); + } + protected synchronized void addSongsImpl(SQLiteDatabase db, int serverKey, List> entries) { + for(Pair 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 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 pair) { + protected synchronized Long[] getLastPlayed(Pair 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 getOnlineSongId(MusicDirectory.Entry entry) { + public synchronized Pair getOnlineSongId(MusicDirectory.Entry entry) { return getOnlineSongId(new DownloadFile(context, entry, true)); } - public Pair getOnlineSongId(DownloadFile downloadFile) { + public synchronized Pair getOnlineSongId(DownloadFile downloadFile) { return getOnlineSongId(Util.getRestUrlHash(context), downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath(), Util.isOffline(context) ? false : true); } - public Pair getOnlineSongId(int serverKey, MusicDirectory.Entry entry) { + public synchronized Pair getOnlineSongId(int serverKey, MusicDirectory.Entry entry) { return getOnlineSongId(serverKey, new DownloadFile(context, entry, true)); } - public Pair getOnlineSongId(int serverKey, DownloadFile downloadFile) { + public synchronized Pair getOnlineSongId(int serverKey, DownloadFile downloadFile) { return getOnlineSongId(serverKey, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath(), true); } - public Pair getOnlineSongId(int serverKey, String id, String savePath, boolean requireServerKey) { + public synchronized Pair 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 getIdFromPath(String path) { + public synchronized Pair 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 getIdFromPath(int serverKey, String path) { + public synchronized Pair getIdFromPath(int serverKey, String path) { SQLiteDatabase db = this.getReadableDatabase(); String[] columns = {SONGS_SERVER_KEY, SONGS_SERVER_ID}; -- cgit v1.2.3