From cdd2d2a52adbc3e478d39dbd74bbcbac90973686 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 28 Nov 2015 20:01:24 -0800 Subject: Start of keeping track of played songs. Based loosly on https://github.com/popeen/Popeens-DSub/commit/07bf2c09a1f46efaaa805abee6c4282d143c045e --- .../dsub/fragments/SubsonicFragment.java | 11 +++ .../daneren2005/dsub/service/DownloadFile.java | 3 + .../github/daneren2005/dsub/service/Scrobbler.java | 44 +++++---- .../daneren2005/dsub/util/SongDBHandler.java | 109 +++++++++++++++++++++ .../java/github/daneren2005/dsub/util/Util.java | 3 + app/src/main/res/values/strings.xml | 1 + 6 files changed, 153 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java (limited to 'app/src') diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java index 6d25bc0b..d824a236 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java @@ -77,6 +77,7 @@ import github.daneren2005.dsub.util.MenuUtil; import github.daneren2005.dsub.util.ProgressListener; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.util.LoadingTask; +import github.daneren2005.dsub.util.SongDBHandler; import github.daneren2005.dsub.util.UpdateHelper; import github.daneren2005.dsub.util.UserUtil; import github.daneren2005.dsub.util.Util; @@ -1293,6 +1294,16 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR headers.add(R.string.details_starred); details.add(Util.formatBoolean(context, song.isStarred())); + try { + Long[] dates = SongDBHandler.getHandler(context).getLastPlayed(song.getId()); + if(dates != null) { + headers.add(R.string.details_last_played); + details.add(Util.formatDate(dates[0])); + } + } catch(Exception e) { + Log.e(TAG, "Failed to get last played", e); + } + if(song instanceof PodcastEpisode) { headers.add(R.string.details_description); details.add(song.getAlbum()); diff --git a/app/src/main/java/github/daneren2005/dsub/service/DownloadFile.java b/app/src/main/java/github/daneren2005/dsub/service/DownloadFile.java index 505e4a6d..cd36c56c 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadFile.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadFile.java @@ -206,6 +206,9 @@ public class DownloadFile implements BufferFile { return saveFile; } + public File getSaveFile() { + return saveFile; + } public File getPartialFile() { return partialFile; diff --git a/app/src/main/java/github/daneren2005/dsub/service/Scrobbler.java b/app/src/main/java/github/daneren2005/dsub/service/Scrobbler.java index 1f8538c9..17a3a90d 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/Scrobbler.java +++ b/app/src/main/java/github/daneren2005/dsub/service/Scrobbler.java @@ -5,6 +5,7 @@ import android.util.Log; import github.daneren2005.dsub.domain.PodcastEpisode; import github.daneren2005.dsub.util.SilentBackgroundTask; +import github.daneren2005.dsub.util.SongDBHandler; import github.daneren2005.dsub.util.Util; /** @@ -32,28 +33,11 @@ public class Scrobbler { } public void scrobble(final Context context, final DownloadFile song, final boolean submission) { - if (song == null || !Util.isScrobblingEnabled(context)) { - return; - } - - // Ignore if online with no network access - if(!Util.isOffline(context) && !Util.isNetworkConnected(context)) { - return; - } - - // Ignore podcasts - if(song.getSong() instanceof PodcastEpisode) { - return; - } - - // Ignore songs which are under 30 seconds per Last.FM guidelines - Integer duration = song.getSong().getDuration(); - if(duration != null && duration > 0 && duration < 30) { + if(song == null) { return; } final String id = song.getSong().getId(); - // Avoid duplicate registrations. if (submission && id.equals(lastSubmission)) { return; @@ -71,6 +55,30 @@ public class Scrobbler { new SilentBackgroundTask(context) { @Override protected Void doInBackground() { + // TODO: Need to move to register plays of offline songs as well + if(!Util.isOffline(context)) { + SongDBHandler.getHandler(context).setSongPlayed(song, submission); + } + + // Scrobbling disabled + if (!Util.isScrobblingEnabled(context)) { + return null; + } + // Ignore if online with no network access + else if(!Util.isOffline(context) && !Util.isNetworkConnected(context)) { + return null; + } + // Ignore podcasts + else if(song.getSong() instanceof PodcastEpisode) { + return null; + } + + // Ignore songs which are under 30 seconds per Last.FM guidelines + Integer duration = song.getSong().getDuration(); + if(duration != null && duration > 0 && duration < 30) { + return null; + } + MusicService service = MusicServiceFactory.getMusicService(context); try { service.scrobble(id, submission, context, null); diff --git a/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java b/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java new file mode 100644 index 00000000..f123521f --- /dev/null +++ b/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java @@ -0,0 +1,109 @@ +/* + 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 . + Copyright 2015 (C) Scott Jackson +*/ + +package github.daneren2005.dsub.util; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +import java.util.Date; + +import github.daneren2005.dsub.domain.MusicDirectory; +import github.daneren2005.dsub.service.DownloadFile; + +public class SongDBHandler extends SQLiteOpenHelper { + private static final String TAG = SongDBHandler.class.getSimpleName(); + private static SongDBHandler dbHandler; + + private static final int DATABASE_VERSION = 1; + public static final String DATABASE_NAME = "SongsDB"; + + public static final String TABLE_SONGS = "RegisteredSongs"; + public static final String SONGS_ID = "id"; + public static final String SONGS_SERVER_ID = "serverId"; + public static final String SONGS_COMPLETE_PATH = "completePath"; + public static final String SONGS_LAST_PLAYED = "lastPlayed"; + public static final String SONGS_LAST_COMPLETED = "lastCompleted"; + + private SongDBHandler(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL("CREATE TABLE " + TABLE_SONGS + " ( " + SONGS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SONGS_SERVER_ID + " TEXT NOT NULL UNIQUE, " + SONGS_COMPLETE_PATH + " TEXT NOT NULL, " + SONGS_LAST_PLAYED + " INTEGER, " + SONGS_LAST_COMPLETED + " INTEGER )"); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + db.execSQL("DROP TABLE IF EXISTS " + TABLE_SONGS); + this.onCreate(db); + } + + public synchronized void addSong(DownloadFile downloadFile) { + SQLiteDatabase db = this.getWritableDatabase(); + addSong(db, downloadFile); + db.close(); + } + protected synchronized void addSong(SQLiteDatabase db, DownloadFile downloadFile) { + ContentValues values = new ContentValues(); + values.put(SONGS_SERVER_ID, downloadFile.getSong().getId()); + values.put(SONGS_COMPLETE_PATH, downloadFile.getSaveFile().getAbsolutePath()); + + db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_REPLACE); + } + + public synchronized void setSongPlayed(DownloadFile downloadFile, boolean submission) { + // Open and make sure song is in db + SQLiteDatabase db = this.getWritableDatabase(); + addSong(db, downloadFile); + + // Update song's last played + ContentValues values = new ContentValues(); + values.put(submission ? SONGS_LAST_COMPLETED : SONGS_LAST_PLAYED, System.currentTimeMillis()); + db.update(TABLE_SONGS, values, SONGS_SERVER_ID + " = ?", new String[]{downloadFile.getSong().getId()}); + db.close(); + } + + public Long[] getLastPlayed(String id) { + SQLiteDatabase db = this.getReadableDatabase(); + + String[] columns = {SONGS_LAST_PLAYED, SONGS_LAST_COMPLETED}; + Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_ID + " = ?", new String[] { id }, null, null, null, null); + + try { + cursor.moveToFirst(); + + Long[] dates = new Long[2]; + dates[0] = cursor.getLong(0); + dates[1] = cursor.getLong(1); + return dates; + } catch(Exception e) {} + + return null; + } + + public static SongDBHandler getHandler(Context context) { + if(dbHandler == null) { + dbHandler = new SongDBHandler(context); + } + + return dbHandler; + } +} diff --git a/app/src/main/java/github/daneren2005/dsub/util/Util.java b/app/src/main/java/github/daneren2005/dsub/util/Util.java index 490e922b..df5e512e 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/Util.java +++ b/app/src/main/java/github/daneren2005/dsub/util/Util.java @@ -908,6 +908,9 @@ public final class Util { } } } + public static String formatDate(long millis) { + return formatDate(new Date(millis)); + } public static String formatBoolean(Context context, boolean value) { return context.getResources().getString(value ? R.string.common_true : R.string.common_false); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 303a8e0e..3acbff3e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -642,6 +642,7 @@ Position Updated Starred + Last Played No songs -- cgit v1.2.3 From 9d1830a481abfb118241884a42889e50d12594cd Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Mon, 30 Nov 2015 13:09:33 -0800 Subject: Keep track of played songs in Offline mode if we have a id registered for it --- .../dsub/fragments/SubsonicFragment.java | 2 +- .../daneren2005/dsub/service/DownloadFile.java | 4 ++ .../github/daneren2005/dsub/service/Scrobbler.java | 5 +- .../daneren2005/dsub/util/SongDBHandler.java | 58 ++++++++++++++++++---- 4 files changed, 55 insertions(+), 14 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java index d824a236..ec8474fb 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java @@ -1295,7 +1295,7 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR details.add(Util.formatBoolean(context, song.isStarred())); try { - Long[] dates = SongDBHandler.getHandler(context).getLastPlayed(song.getId()); + Long[] dates = SongDBHandler.getHandler(context).getLastPlayed(context, song); if(dates != null) { headers.add(R.string.details_last_played); details.add(Util.formatDate(dates[0])); diff --git a/app/src/main/java/github/daneren2005/dsub/service/DownloadFile.java b/app/src/main/java/github/daneren2005/dsub/service/DownloadFile.java index cd36c56c..3febfaea 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadFile.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadFile.java @@ -85,6 +85,10 @@ public class DownloadFile implements BufferFile { return song; } + public Context getContext() { + return context; + } + /** * Returns the effective bit rate. */ diff --git a/app/src/main/java/github/daneren2005/dsub/service/Scrobbler.java b/app/src/main/java/github/daneren2005/dsub/service/Scrobbler.java index 17a3a90d..1d9fecef 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/Scrobbler.java +++ b/app/src/main/java/github/daneren2005/dsub/service/Scrobbler.java @@ -55,10 +55,7 @@ public class Scrobbler { new SilentBackgroundTask(context) { @Override protected Void doInBackground() { - // TODO: Need to move to register plays of offline songs as well - if(!Util.isOffline(context)) { - SongDBHandler.getHandler(context).setSongPlayed(song, submission); - } + SongDBHandler.getHandler(context).setSongPlayed(song, submission); // Scrobbling disabled if (!Util.isScrobblingEnabled(context)) { 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 f123521f..0b60884f 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java +++ b/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java @@ -17,12 +17,10 @@ package github.daneren2005.dsub.util; import android.content.ContentValues; import android.content.Context; +import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; -import android.util.Log; - -import java.util.Date; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.service.DownloadFile; @@ -47,6 +45,7 @@ public class SongDBHandler extends SQLiteOpenHelper { @Override public void onCreate(SQLiteDatabase db) { + // TODO: Need to handle multiple servers db.execSQL("CREATE TABLE " + TABLE_SONGS + " ( " + SONGS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SONGS_SERVER_ID + " TEXT NOT NULL UNIQUE, " + SONGS_COMPLETE_PATH + " TEXT NOT NULL, " + SONGS_LAST_PLAYED + " INTEGER, " + SONGS_LAST_COMPLETED + " INTEGER )"); } @@ -62,30 +61,41 @@ public class SongDBHandler extends SQLiteOpenHelper { db.close(); } protected synchronized void addSong(SQLiteDatabase db, DownloadFile downloadFile) { + addSong(db, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath()); + } + protected synchronized void addSong(SQLiteDatabase db, String id, String absolutePath) { ContentValues values = new ContentValues(); - values.put(SONGS_SERVER_ID, downloadFile.getSong().getId()); - values.put(SONGS_COMPLETE_PATH, downloadFile.getSaveFile().getAbsolutePath()); + values.put(SONGS_SERVER_ID, id); + values.put(SONGS_COMPLETE_PATH, absolutePath); - db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_REPLACE); + db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_IGNORE); } public synchronized void setSongPlayed(DownloadFile downloadFile, boolean submission) { + String id = getOnlineSongId(downloadFile); + if(id == null) { + return; + } + // Open and make sure song is in db SQLiteDatabase db = this.getWritableDatabase(); - addSong(db, downloadFile); + addSong(db, id, downloadFile.getSaveFile().getAbsolutePath()); // Update song's last played ContentValues values = new ContentValues(); values.put(submission ? SONGS_LAST_COMPLETED : SONGS_LAST_PLAYED, System.currentTimeMillis()); - db.update(TABLE_SONGS, values, SONGS_SERVER_ID + " = ?", new String[]{downloadFile.getSong().getId()}); + db.update(TABLE_SONGS, values, SONGS_SERVER_ID + " = ?", new String[]{id}); db.close(); } + public Long[] getLastPlayed(Context context, MusicDirectory.Entry entry) { + return getLastPlayed(getOnlineSongId(context, entry)); + } public Long[] getLastPlayed(String id) { SQLiteDatabase db = this.getReadableDatabase(); String[] columns = {SONGS_LAST_PLAYED, SONGS_LAST_COMPLETED}; - Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_ID + " = ?", new String[] { id }, null, null, null, null); + Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_ID + " = ?", new String[]{id}, null, null, null, null); try { cursor.moveToFirst(); @@ -99,6 +109,36 @@ public class SongDBHandler extends SQLiteOpenHelper { return null; } + public String getOnlineSongId(Context context, MusicDirectory.Entry entry) { + return getOnlineSongId(new DownloadFile(context, entry, true)); + } + public String getOnlineSongId(DownloadFile downloadFile) { + return getOnlineSongId(downloadFile.getContext(), downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath()); + } + protected String getOnlineSongId(Context context, String id, String savePath) { + SharedPreferences prefs = Util.getPreferences(context); + String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null); + if(cacheLocn != null && id.indexOf(cacheLocn) != -1) { + id = getIdFromPath(savePath); + } + + return id; + } + + public String getIdFromPath(String path) { + SQLiteDatabase db = this.getReadableDatabase(); + + String[] columns = {SONGS_SERVER_ID}; + Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_COMPLETE_PATH + " = ?", new String[] { path }, null, null, null, null); + + try { + cursor.moveToFirst(); + return cursor.getString(0); + } catch(Exception e) {} + + return null; + } + public static SongDBHandler getHandler(Context context) { if(dbHandler == null) { dbHandler = new SongDBHandler(context); -- cgit v1.2.3 From 7e288e34ad29154da0c03ce05fb92956ae19cb78 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Fri, 11 Dec 2015 17:58:28 -0800 Subject: Add server instance to RegisteredSongs --- .../dsub/fragments/SubsonicFragment.java | 2 +- .../daneren2005/dsub/util/SongDBHandler.java | 101 ++++++++++++++++----- .../java/github/daneren2005/dsub/util/Util.java | 16 ++++ 3 files changed, 95 insertions(+), 24 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java index ec8474fb..715263ff 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java @@ -1295,7 +1295,7 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR details.add(Util.formatBoolean(context, song.isStarred())); try { - Long[] dates = SongDBHandler.getHandler(context).getLastPlayed(context, song); + Long[] dates = SongDBHandler.getHandler(context).getLastPlayed(song); if(dates != null) { headers.add(R.string.details_last_played); details.add(Util.formatDate(dates[0])); 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 0b60884f..e7716b49 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java +++ b/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java @@ -29,24 +29,34 @@ public class SongDBHandler extends SQLiteOpenHelper { private static final String TAG = SongDBHandler.class.getSimpleName(); private static SongDBHandler dbHandler; - private static final int DATABASE_VERSION = 1; + private static final int DATABASE_VERSION = 2; public static final String DATABASE_NAME = "SongsDB"; public static final String TABLE_SONGS = "RegisteredSongs"; public static final String SONGS_ID = "id"; + public static final String SONGS_SERVER_KEY = "serverKey"; public static final String SONGS_SERVER_ID = "serverId"; public static final String SONGS_COMPLETE_PATH = "completePath"; public static final String SONGS_LAST_PLAYED = "lastPlayed"; public static final String SONGS_LAST_COMPLETED = "lastCompleted"; + private Context context; + private SongDBHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); + this.context = context; } @Override public void onCreate(SQLiteDatabase db) { - // TODO: Need to handle multiple servers - db.execSQL("CREATE TABLE " + TABLE_SONGS + " ( " + SONGS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SONGS_SERVER_ID + " TEXT NOT NULL UNIQUE, " + SONGS_COMPLETE_PATH + " TEXT NOT NULL, " + SONGS_LAST_PLAYED + " INTEGER, " + SONGS_LAST_COMPLETED + " INTEGER )"); + db.execSQL("CREATE TABLE " + TABLE_SONGS + " ( " + + SONGS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + + SONGS_SERVER_KEY + " INTEGER NOT NULL, " + + SONGS_SERVER_ID + " TEXT NOT NULL, " + + SONGS_COMPLETE_PATH + " TEXT NOT NULL, " + + SONGS_LAST_PLAYED + " INTEGER, " + + SONGS_LAST_COMPLETED + " INTEGER, " + + "UNIQUE(" + SONGS_SERVER_KEY + ", " + SONGS_SERVER_ID + "))"); } @Override @@ -56,15 +66,29 @@ public class SongDBHandler extends SQLiteOpenHelper { } public synchronized void addSong(DownloadFile downloadFile) { + addSong(Util.getMostRecentActiveServer(context), downloadFile); + } + public synchronized void addSong(int instance, DownloadFile downloadFile) { SQLiteDatabase db = this.getWritableDatabase(); - addSong(db, downloadFile); + addSong(db, instance, downloadFile); db.close(); } protected synchronized void addSong(SQLiteDatabase db, DownloadFile downloadFile) { - addSong(db, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath()); + addSong(db, Util.getMostRecentActiveServer(context), downloadFile); + } + protected synchronized void addSong(SQLiteDatabase db, int instance, DownloadFile downloadFile) { + addSong(db, instance, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath()); } + protected synchronized void addSong(SQLiteDatabase db, String id, String absolutePath) { + addSong(db, Util.getMostRecentActiveServer(context), id, absolutePath); + } + protected synchronized void addSong(SQLiteDatabase db, int instance, String id, String absolutePath) { + addSongImpl(db, Util.getRestUrlHash(context, instance), id, absolutePath); + } + protected synchronized void addSongImpl(SQLiteDatabase db, int serverKey, String id, String absolutePath) { ContentValues values = new ContentValues(); + values.put(SONGS_SERVER_KEY, serverKey); values.put(SONGS_SERVER_ID, id); values.put(SONGS_COMPLETE_PATH, absolutePath); @@ -72,30 +96,37 @@ public class SongDBHandler extends SQLiteOpenHelper { } public synchronized void setSongPlayed(DownloadFile downloadFile, boolean submission) { - String id = getOnlineSongId(downloadFile); - if(id == null) { + // TODO: In case of offline want to update all matches + Pair pair = getOnlineSongId(downloadFile); + if(pair == null) { return; } + int serverKey = pair.getFirst(); + String id = pair.getSecond(); // Open and make sure song is in db SQLiteDatabase db = this.getWritableDatabase(); - addSong(db, id, downloadFile.getSaveFile().getAbsolutePath()); + addSongImpl(db, serverKey, id, downloadFile.getSaveFile().getAbsolutePath()); // Update song's last played ContentValues values = new ContentValues(); values.put(submission ? SONGS_LAST_COMPLETED : SONGS_LAST_PLAYED, System.currentTimeMillis()); - db.update(TABLE_SONGS, values, SONGS_SERVER_ID + " = ?", new String[]{id}); + db.update(TABLE_SONGS, values, SONGS_SERVER_KEY + " = ? AND " + SONGS_SERVER_ID + " = ?", new String[]{Integer.toString(serverKey), id}); db.close(); } - public Long[] getLastPlayed(Context context, MusicDirectory.Entry entry) { - return getLastPlayed(getOnlineSongId(context, entry)); + public Long[] getLastPlayed(MusicDirectory.Entry entry) { + // TODO: In case of offline want to get most recent played match + return getLastPlayed(getOnlineSongId(entry)); + } + protected Long[] getLastPlayed(Pair pair) { + return getLastPlayed(pair.getFirst(), pair.getSecond()); } - public Long[] getLastPlayed(String id) { + public Long[] getLastPlayed(int serverKey, String id) { SQLiteDatabase db = this.getReadableDatabase(); String[] columns = {SONGS_LAST_PLAYED, SONGS_LAST_COMPLETED}; - Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_ID + " = ?", new String[]{id}, null, null, null, null); + Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_KEY + " = ? AND " + SONGS_SERVER_ID + " = ?", new String[]{Integer.toString(serverKey), id}, null, null, null, null); try { cursor.moveToFirst(); @@ -109,31 +140,55 @@ public class SongDBHandler extends SQLiteOpenHelper { return null; } - public String getOnlineSongId(Context context, MusicDirectory.Entry entry) { + public Pair getOnlineSongId(MusicDirectory.Entry entry) { return getOnlineSongId(new DownloadFile(context, entry, true)); } - public String getOnlineSongId(DownloadFile downloadFile) { - return getOnlineSongId(downloadFile.getContext(), downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath()); + public 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) { + return getOnlineSongId(serverKey, new DownloadFile(context, entry, true)); + } + public Pair getOnlineSongId(int serverKey, DownloadFile downloadFile) { + return getOnlineSongId(serverKey, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath(), true); } - protected String getOnlineSongId(Context context, String id, String savePath) { + protected 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) { - id = getIdFromPath(savePath); + if(requireServerKey) { + return getIdFromPath(serverKey, savePath); + } else { + return getIdFromPath(savePath); + } + } else { + return new Pair<>(serverKey, id); } - - return id; } - public String getIdFromPath(String path) { + public Pair getIdFromPath(String path) { SQLiteDatabase db = this.getReadableDatabase(); - String[] columns = {SONGS_SERVER_ID}; + String[] columns = {SONGS_SERVER_KEY, SONGS_SERVER_ID}; Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_COMPLETE_PATH + " = ?", new String[] { path }, null, null, null, null); try { cursor.moveToFirst(); - return cursor.getString(0); + return new Pair(cursor.getInt(0), cursor.getString(1)); + } catch(Exception e) {} + + return null; + } + public Pair getIdFromPath(int serverKey, String path) { + SQLiteDatabase db = this.getReadableDatabase(); + + String[] columns = {SONGS_SERVER_KEY, SONGS_SERVER_ID}; + Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_KEY + " = ? AND " + SONGS_COMPLETE_PATH + " = ?", new String[] {Integer.toString(serverKey), path }, null, null, null, null); + + try { + cursor.moveToFirst(); + return new Pair(cursor.getInt(0), cursor.getString(1)); } catch(Exception e) {} return null; diff --git a/app/src/main/java/github/daneren2005/dsub/util/Util.java b/app/src/main/java/github/daneren2005/dsub/util/Util.java index df5e512e..bf4af20d 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/Util.java +++ b/app/src/main/java/github/daneren2005/dsub/util/Util.java @@ -176,6 +176,10 @@ public final class Util { // Don't allow the SERVER_INSTANCE to ever be 0 return prefs.getBoolean(Constants.PREFERENCES_KEY_OFFLINE, false) ? 0 : Math.max(1, prefs.getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1)); } + public static int getMostRecentActiveServer(Context context) { + SharedPreferences prefs = getPreferences(context); + return Math.max(1, prefs.getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1)); + } public static int getServerCount(Context context) { SharedPreferences prefs = getPreferences(context); @@ -450,6 +454,18 @@ public final class Util { return builder.toString(); } + public static int getRestUrlHash(Context context) { + return getRestUrlHash(context, Util.getMostRecentActiveServer(context)); + } + public static int getRestUrlHash(Context context, int instance) { + StringBuilder builder = new StringBuilder(); + + SharedPreferences prefs = Util.getPreferences(context); + builder.append(prefs.getString(Constants.PREFERENCES_KEY_SERVER_URL + instance, null)); + builder.append(prefs.getString(Constants.PREFERENCES_KEY_USERNAME + instance, null)); + + return builder.toString().hashCode(); + } public static String replaceInternalUrl(Context context, String url) { // Only change to internal when using https -- cgit v1.2.3 From 8bd8ba0244436a809cfecde8122a7b2a1c525145 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Fri, 11 Dec 2015 18:02:45 -0800 Subject: If case of multiple identical paths offline, show last played as most recent one --- app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'app/src') 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 e7716b49..ba0851c2 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java +++ b/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java @@ -116,7 +116,6 @@ public class SongDBHandler extends SQLiteOpenHelper { } public Long[] getLastPlayed(MusicDirectory.Entry entry) { - // TODO: In case of offline want to get most recent played match return getLastPlayed(getOnlineSongId(entry)); } protected Long[] getLastPlayed(Pair pair) { @@ -171,7 +170,7 @@ public class SongDBHandler extends SQLiteOpenHelper { SQLiteDatabase db = this.getReadableDatabase(); String[] columns = {SONGS_SERVER_KEY, SONGS_SERVER_ID}; - Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_COMPLETE_PATH + " = ?", new String[] { path }, null, null, null, null); + Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_COMPLETE_PATH + " = ?", new String[] { path }, null, null, SONGS_LAST_PLAYED + " DESC", null); try { cursor.moveToFirst(); -- cgit v1.2.3 From 9debb55ea2e35f0759009af036c93ff489516107 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 12 Dec 2015 15:22:33 -0800 Subject: Try to lookup songs in SongDBHandler for offline scrobbling/stars --- .../daneren2005/dsub/service/OfflineMusicService.java | 14 +++++++++++--- .../daneren2005/dsub/service/RESTMusicService.java | 18 +++++++++++++----- .../github/daneren2005/dsub/util/SongDBHandler.java | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/github/daneren2005/dsub/service/OfflineMusicService.java b/app/src/main/java/github/daneren2005/dsub/service/OfflineMusicService.java index c8253c91..11a723ce 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/OfflineMusicService.java @@ -56,8 +56,10 @@ import github.daneren2005.dsub.domain.Share; import github.daneren2005.dsub.domain.User; import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.FileUtil; +import github.daneren2005.dsub.util.Pair; import github.daneren2005.dsub.util.ProgressListener; import github.daneren2005.dsub.util.SilentBackgroundTask; +import github.daneren2005.dsub.util.SongDBHandler; import github.daneren2005.dsub.util.Util; import java.io.*; import java.util.Comparator; @@ -556,9 +558,15 @@ public class OfflineMusicService implements MusicService { SharedPreferences.Editor offlineEditor = offline.edit(); if(id.indexOf(cacheLocn) != -1) { - String scrobbleSearchCriteria = Util.parseOfflineIDSearch(context, id, cacheLocn); - offlineEditor.putString(Constants.OFFLINE_SCROBBLE_SEARCH + scrobbles, scrobbleSearchCriteria); - offlineEditor.remove(Constants.OFFLINE_SCROBBLE_ID + scrobbles); + Pair cachedSongId = SongDBHandler.getHandler(context).getIdFromPath(id); + if(cachedSongId != null) { + offlineEditor.putString(Constants.OFFLINE_SCROBBLE_ID + scrobbles, cachedSongId.getSecond()); + offlineEditor.remove(Constants.OFFLINE_SCROBBLE_SEARCH + scrobbles); + } else { + String scrobbleSearchCriteria = Util.parseOfflineIDSearch(context, id, cacheLocn); + offlineEditor.putString(Constants.OFFLINE_SCROBBLE_SEARCH + scrobbles, scrobbleSearchCriteria); + offlineEditor.remove(Constants.OFFLINE_SCROBBLE_ID + scrobbles); + } } else { offlineEditor.putString(Constants.OFFLINE_SCROBBLE_ID + scrobbles, id); offlineEditor.remove(Constants.OFFLINE_SCROBBLE_SEARCH + scrobbles); diff --git a/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java b/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java index 9ec77897..9af512e3 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java @@ -69,6 +69,7 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Looper; import android.util.Log; + import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.*; import github.daneren2005.dsub.service.parser.AlbumListParser; @@ -99,10 +100,12 @@ import github.daneren2005.dsub.service.parser.VideosParser; import github.daneren2005.dsub.service.ssl.SSLSocketFactory; import github.daneren2005.dsub.service.ssl.TrustSelfSignedStrategy; import github.daneren2005.dsub.util.BackgroundTask; +import github.daneren2005.dsub.util.Pair; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.FileUtil; import github.daneren2005.dsub.util.ProgressListener; +import github.daneren2005.dsub.util.SongDBHandler; import github.daneren2005.dsub.util.Util; import java.io.*; import java.util.zip.GZIPInputStream; @@ -1796,11 +1799,16 @@ public class RESTMusicService implements MusicService { SharedPreferences prefs = Util.getPreferences(context); String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null); if(cacheLocn != null && id.indexOf(cacheLocn) != -1) { - String searchCriteria = Util.parseOfflineIDSearch(context, id, cacheLocn); - SearchCritera critera = new SearchCritera(searchCriteria, 0, 0, 1); - SearchResult result = searchNew(critera, context, progressListener); - if(result.getSongs().size() == 1){ - id = result.getSongs().get(0).getId(); + Pair cachedSongId = SongDBHandler.getHandler(context).getIdFromPath(Util.getRestUrlHash(context, getInstance(context)), id); + if(cachedSongId != null) { + id = cachedSongId.getSecond(); + } else { + String searchCriteria = Util.parseOfflineIDSearch(context, id, cacheLocn); + SearchCritera critera = new SearchCritera(searchCriteria, 0, 0, 1); + SearchResult result = searchNew(critera, context, progressListener); + if (result.getSongs().size() == 1) { + id = result.getSongs().get(0).getId(); + } } } 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 ba0851c2..63de6d48 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java +++ b/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java @@ -152,7 +152,7 @@ public class SongDBHandler extends SQLiteOpenHelper { public Pair getOnlineSongId(int serverKey, DownloadFile downloadFile) { return getOnlineSongId(serverKey, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath(), true); } - protected Pair getOnlineSongId(int serverKey, String id, String savePath, boolean requireServerKey) { + public 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) { -- cgit v1.2.3 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') 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 From 5d5b110ca0f8d7d5e599a401a7931a5cd78e9ffc Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 12 Dec 2015 15:55:50 -0800 Subject: Fix songs with a db entry but never been played showing as epoc --- .../main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/src') diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java index 715263ff..88c8363b 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java @@ -1296,7 +1296,7 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR try { Long[] dates = SongDBHandler.getHandler(context).getLastPlayed(song); - if(dates != null) { + if(dates != null && dates[0] != null && dates[0] > 0) { headers.add(R.string.details_last_played); details.add(Util.formatDate(dates[0])); } -- cgit v1.2.3 From 26873c657849a1351618950a1533fd59b3422ba8 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 12 Dec 2015 15:56:26 -0800 Subject: Speed up addSongsImpl enough we can load for playlists as well --- .../dsub/service/CachedMusicService.java | 3 +-- .../github/daneren2005/dsub/util/SongDBHandler.java | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) (limited to 'app/src') 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 e188f97f..c0851009 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java @@ -258,8 +258,7 @@ 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); + updateAllSongs(context, dir); FileUtil.serialize(context, dir, getCacheName(context, "playlist", id)); File playlistFile = FileUtil.getPlaylistFile(context, Util.getServerName(context, musicService.getInstance(context)), dir.getName()); 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 eb52de9c..08f6f461 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java +++ b/app/src/main/java/github/daneren2005/dsub/util/SongDBHandler.java @@ -113,14 +113,21 @@ public class SongDBHandler extends SQLiteOpenHelper { 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.beginTransaction(); + try { + 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); - } + db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_IGNORE); + } + + db.setTransactionSuccessful(); + } catch(Exception e) {} + + db.endTransaction(); } public synchronized void setSongPlayed(DownloadFile downloadFile, boolean submission) { -- cgit v1.2.3