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/main/java/github') 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