From b8831f71f2b80c84fbe528790fc25f1905e6efa2 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Sat, 21 Jan 2017 14:05:01 +0100 Subject: [PATCH] use media database to store preferences instead of the shared pref provider --- .../android/medialibrary/MediaLibrary.java | 13 +++++++++ .../medialibrary/MediaLibraryBackend.java | 29 ++++++++++++++++++- .../android/medialibrary/MediaScanner.java | 11 +------ .../android/medialibrary/MediaSchema.java | 24 +++++++++++++++ 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java b/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java index 084966ff..4ba3620d 100644 --- a/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java +++ b/src/ch/blinkenlights/android/medialibrary/MediaLibrary.java @@ -37,6 +37,7 @@ public class MediaLibrary { public static final String TABLE_GENRES_SONGS = "genres_songs"; public static final String TABLE_PLAYLISTS = "playlists"; public static final String TABLE_PLAYLISTS_SONGS = "playlists_songs"; + public static final String TABLE_PREFERENCES = "preferences"; public static final String VIEW_ARTISTS = "_artists"; public static final String VIEW_ALBUMS_ARTISTS = "_albums_artists"; public static final String VIEW_SONGS_ALBUMS_ARTISTS = "_songs_albums_artists"; @@ -594,4 +595,16 @@ public class MediaLibrary { */ String POSITION = "position"; } + + // Preference keys + public interface PreferenceColumns { + /** + * The key of this preference + */ + String KEY = "key"; + /** + * The value of this preference + */ + String VALUE = "value"; + } } diff --git a/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java b/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java index bdd59ee6..32fe5e8a 100644 --- a/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java +++ b/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java @@ -35,7 +35,7 @@ public class MediaLibraryBackend extends SQLiteOpenHelper { /** * The database version we are using */ - private static final int DATABASE_VERSION = 20170102; + private static final int DATABASE_VERSION = 20170120; /** * on-disk file to store the database */ @@ -96,6 +96,33 @@ public class MediaLibraryBackend extends SQLiteOpenHelper { return mtime; } + /** + * Simple interface to set and get preference values + * + * @param stringKey the key to use + * @param newVal the value to set + * + * Note: The new value will only be set if it is >= 0 + * Lookup failures will return 0 + */ + int getSetPreference(String stringKey, int newVal) { + int oldVal = 0; // this is returned if we found nothing + int key = Math.abs(stringKey.hashCode()); + SQLiteDatabase dbh = getWritableDatabase(); + + Cursor cursor = dbh.query(MediaLibrary.TABLE_PREFERENCES, new String[] { MediaLibrary.PreferenceColumns.VALUE }, MediaLibrary.PreferenceColumns.KEY+"="+key, null, null, null, null, null); + if (cursor.moveToFirst()) { + oldVal = cursor.getInt(0); + } + cursor.close(); + + if (newVal >= 0 && newVal != oldVal) { + dbh.execSQL("INSERT OR REPLACE INTO "+MediaLibrary.TABLE_PREFERENCES+" ("+MediaLibrary.PreferenceColumns.KEY+", "+MediaLibrary.PreferenceColumns.VALUE+") " + +" VALUES("+key+", "+newVal+")"); + } + return oldVal; + } + /** * Wrapper for SQLiteDatabse.delete() function * diff --git a/src/ch/blinkenlights/android/medialibrary/MediaScanner.java b/src/ch/blinkenlights/android/medialibrary/MediaScanner.java index 6b4024f6..e251a882 100644 --- a/src/ch/blinkenlights/android/medialibrary/MediaScanner.java +++ b/src/ch/blinkenlights/android/medialibrary/MediaScanner.java @@ -532,16 +532,7 @@ public class MediaScanner implements Handler.Callback { * @return the value previously set, or 0 as a default */ private int getSetPreference(String prefKey, int newVal) { - SharedPreferences sharedPref = mContext.getSharedPreferences("scanner_preferences", Context.MODE_PRIVATE); - int oldVal = sharedPref.getInt(prefKey, 0); - - if (newVal >= 0 && newVal != oldVal) { - SharedPreferences.Editor editor = sharedPref.edit(); - editor.putInt(prefKey, newVal); - editor.apply(); - } - - return oldVal; + return mBackend.getSetPreference(prefKey, newVal); } diff --git a/src/ch/blinkenlights/android/medialibrary/MediaSchema.java b/src/ch/blinkenlights/android/medialibrary/MediaSchema.java index 9489de72..9aeb074a 100644 --- a/src/ch/blinkenlights/android/medialibrary/MediaSchema.java +++ b/src/ch/blinkenlights/android/medialibrary/MediaSchema.java @@ -116,6 +116,13 @@ public class MediaSchema { + MediaLibrary.PlaylistSongColumns.POSITION +" INTEGER NOT NULL " + ");"; + /** + * SQL schema for our preferences + */ + private static final String DATABASE_CREATE_PREFERENCES = "CREATE TABLE "+ MediaLibrary.TABLE_PREFERENCES + " (" + + MediaLibrary.PreferenceColumns.KEY +" INTEGER PRIMARY KEY, " + + MediaLibrary.PreferenceColumns.VALUE +" INTEGER NOT NULL " + + ");"; /** * Index to select a playlist quickly */ @@ -200,6 +207,7 @@ public class MediaSchema { dbh.execSQL(VIEW_CREATE_ALBUMS_ARTISTS); dbh.execSQL(VIEW_CREATE_ARTISTS); dbh.execSQL(VIEW_CREATE_PLAYLIST_SONGS); + dbh.execSQL(DATABASE_CREATE_PREFERENCES); } /** @@ -221,6 +229,22 @@ public class MediaSchema { if (oldVersion < 20170102) { dbh.execSQL("UPDATE songs SET disc_num=1 WHERE disc_num IS null"); } + + if (oldVersion < 20170120) { + dbh.execSQL(DATABASE_CREATE_PREFERENCES); + triggerFullMediaScan(dbh); + } + } + + /** + * Changes the mtime of all songs and flushes the scanner progress / preferences + * This triggers a full rebuild of the library on startup + * + * @param dbh the writeable dbh to use + */ + private static void triggerFullMediaScan(SQLiteDatabase dbh) { + dbh.execSQL("UPDATE "+MediaLibrary.TABLE_SONGS+" SET "+MediaLibrary.SongColumns.MTIME+"=0"); + dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_PREFERENCES); } }