use media database to store preferences instead of the shared pref provider

This commit is contained in:
Adrian Ulrich committed 2017-01-21 14:05:01 +01:00
1 parent 1982b0f969
commit b8831f71f2
4 files changed
+66 -11

No files matched your search

@@ -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";
}
}
@@ -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
*
@@ -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);
}
@@ -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);
}
}