Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added persistence to seektime for playing songs #923

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e99be33
Added persistence to seektime for playing songs
newhinton Feb 25, 2019
07b9ad8
added database queries for album-last-played-track persistence
newhinton Feb 25, 2019
2bf31b5
added context-menu action for album to play last song played from album
newhinton Feb 25, 2019
80bfa93
Added album state for songtimestampusage
newhinton Feb 28, 2019
7f54c73
added userinterface for timestamp-persistence
newhinton Feb 28, 2019
50e062c
refactored stringnamming
newhinton Feb 28, 2019
be5a2dd
Removed logging
newhinton Feb 28, 2019
e5b70d7
Added "Continue album" functionality
newhinton Feb 28, 2019
3245b9c
added migration for missing database entries
newhinton Feb 28, 2019
d904c6a
Directly continue album if default is set to continue
newhinton Feb 28, 2019
b4d0394
updated strings
newhinton Feb 28, 2019
5461863
Added better playback handling in regards of sql calls
newhinton Mar 3, 2019
3953fe0
Added better playback handling in regards of sql calls(Add restart on…
newhinton Mar 3, 2019
f478daf
catch nullpointer on timestamphandler if music was started from widget
newhinton Mar 9, 2019
e098569
Updated codestyle and fixed bug where milliseconds and seconds were m…
newhinton Mar 9, 2019
0b4b07a
Initial Timestamp now applies after song start
newhinton Mar 9, 2019
a2bf284
Only queue album, not every element by artist
newhinton Mar 9, 2019
f731002
Reset the last songtimestamp to 0 after the song finished
newhinton Mar 16, 2019
5d3e33e
process Song from widget
newhinton May 10, 2019
ddaa152
fix playback on some more occasions
newhinton May 10, 2019
bad2d22
fix nullpointer on empty song in timestamp handler
newhinton May 10, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,115 @@ public static void updateSongPlayCounts(Context context, long id, boolean played
getBackend(context).execSQL("UPDATE "+MediaLibrary.TABLE_SONGS+" SET "+column+"="+column+"+1 WHERE "+selection);
}

/**
* Updates the timestamp of a played song
*
* @param context the context to use
* @param id the song id to update
* @param timestamp the timestamp
*/
public static void updateSongTimestamp(Context context, long id, int timestamp) {
String selection = MediaLibrary.SongColumns._ID+"="+id;
getBackend(context).execSQL("UPDATE "+MediaLibrary.TABLE_SONGS+" SET "+SongColumns.TIMESTAMP_LAST_PLAY+"="+timestamp+" WHERE "+selection);
}

/**
* Get the timestamp of a played song
*
* @param context the context to use
* @param id the song id to update
* @return the timestamp
*/
public static int getSongTimestamp(Context context, long id) {

String[] projection = {SongColumns._ID, SongColumns.TIMESTAMP_LAST_PLAY};
String selection = SongColumns._ID + " = ?";
String[] selectionArgs = { String.valueOf(id) };

Cursor cursor = MediaLibrary.queryLibrary(context, MediaLibrary.TABLE_SONGS, projection, selection, selectionArgs, null);

int timestamp=-1;
if (cursor != null) {
if (cursor.moveToFirst()) {
timestamp = cursor.getInt(1);
}
cursor.close();
}
return timestamp;
}

/**
* Updates the timestamp of a played song
*
* @param context the context to use
* @param songId the song id
* @param albumId the album id to update
*/
public static void updateAlbumLastSong(Context context, long songId, long albumId) {
String selection = MediaLibrary.AlbumColumns._ID+"="+albumId;
getBackend(context).execSQL("UPDATE "+MediaLibrary.TABLE_ALBUMS+" SET "+AlbumColumns.LAST_TRACK_PLAYED+"="+songId+" WHERE "+selection);
}

/**
* Get the last played song of an album
*
* @param context the context to use
* @param id the song id to update
* @return the songid
*/
public static long getAlbumLastSong(Context context, long id) {

String[] projection = {AlbumColumns._ID, AlbumColumns.LAST_TRACK_PLAYED};
String selection = AlbumColumns._ID + " = ?";
String[] selectionArgs = { String.valueOf(id) };

Cursor cursor = MediaLibrary.queryLibrary(context, MediaLibrary.TABLE_ALBUMS, projection, selection, selectionArgs, null);

long timestamp=-1;
if (cursor != null) {
if (cursor.moveToFirst()) {
timestamp = cursor.getLong(1);
}
cursor.close();
}
return timestamp;
}

/**
* Sets the state if the album should use the last played timestamp on playstart
*
* @param context the context to use
* @param albumId the album id to update
* @param startAtTimestamp the album id to update
*/
public static void updateAlbumUseSongTimestamp(Context context, long albumId, boolean startAtTimestamp) {
int state = 0;
if(startAtTimestamp){
state = 1;
}

String selection = MediaLibrary.AlbumColumns._ID+"="+albumId;
getBackend(context).execSQL("UPDATE "+MediaLibrary.TABLE_ALBUMS+" SET "+AlbumColumns.START_AT_TRACKTIMESTAMP+"="+state+" WHERE "+selection);
}

public static int getAlbumUseSongTimestamp(Context context, long id) {

String[] projection = {AlbumColumns._ID, AlbumColumns.START_AT_TRACKTIMESTAMP};
String selection = AlbumColumns._ID + " = ?";
String[] selectionArgs = { String.valueOf(id) };

Cursor cursor = MediaLibrary.queryLibrary(context, MediaLibrary.TABLE_ALBUMS, projection, selection, selectionArgs, null);

int state=-1;
if (cursor != null) {
if (cursor.moveToFirst()) {
state = cursor.getInt(1);
}
cursor.close();
}
return state;
}

/**
* Creates a new empty playlist
*
Expand Down Expand Up @@ -653,6 +762,11 @@ public interface SongColumns {
* Various flags of this entry, see SONG_FLAG...
*/
String FLAGS = "_flags";
/**
* Contains the last known timestamp of the song.
* This is important for audiobooks/podcasts
*/
String TIMESTAMP_LAST_PLAY = "timestamp_last_play";
}

// Columns of Album entries
Expand Down Expand Up @@ -681,6 +795,14 @@ public interface AlbumColumns {
* The mtime of this item
*/
String MTIME = "mtime";
/**
* The id of the last played song from this album
*/
String LAST_TRACK_PLAYED = "last_track_played";
/**
* The id of the last played song from this album
*/
String START_AT_TRACKTIMESTAMP = "start_at_tracktimestamp";
}

// Columns of Contributors entries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MediaLibraryBackend extends SQLiteOpenHelper {
/**
* The database version we are using
*/
private static final int DATABASE_VERSION = 20190210;
private static final int DATABASE_VERSION = 20190228;
/**
* on-disk file to store the database
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,19 @@ static void migrate_to_20180416(SQLiteDatabase dbh, String fromDb, String toDb)
cursor.close();
}

/**
* Migrate to 20190228
* Add fields for track-timestamp and albumposition
*
* @param dbh the database to work on
**/
static void migrate_to_20190228(SQLiteDatabase dbh) {
Log.v("VanillaMusic", "migrate_to_20190228");
dbh.execSQL("ALTER TABLE "+MediaLibrary.TABLE_ALBUMS+" ADD COLUMN "+MediaLibrary.AlbumColumns.LAST_TRACK_PLAYED+" INTEGER;");
dbh.execSQL("ALTER TABLE "+MediaLibrary.TABLE_ALBUMS+" ADD COLUMN "+MediaLibrary.AlbumColumns.START_AT_TRACKTIMESTAMP+" INTEGER DEFAULT -1;");
dbh.execSQL("ALTER TABLE "+MediaLibrary.TABLE_SONGS+" ADD COLUMN "+MediaLibrary.SongColumns.TIMESTAMP_LAST_PLAY+" INTEGER;");


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,34 @@ public class MediaSchema {
* SQL Schema of `songs' table
*/
private static final String DATABASE_CREATE_SONGS = "CREATE TABLE "+ MediaLibrary.TABLE_SONGS + " ("
+ MediaLibrary.SongColumns._ID +" INTEGER PRIMARY KEY, "
+ MediaLibrary.SongColumns.TITLE +" TEXT NOT NULL, "
+ MediaLibrary.SongColumns.TITLE_SORT +" VARCHAR(64) NOT NULL, "
+ MediaLibrary.SongColumns.SONG_NUMBER +" INTEGER, "
+ MediaLibrary.SongColumns.DISC_NUMBER +" INTEGER, "
+ MediaLibrary.SongColumns.YEAR +" INTEGER, "
+ MediaLibrary.SongColumns.ALBUM_ID +" INTEGER NOT NULL, "
+ MediaLibrary.SongColumns.PLAYCOUNT +" INTEGER NOT NULL DEFAULT 0, "
+ MediaLibrary.SongColumns.SKIPCOUNT +" INTEGER NOT NULL DEFAULT 0, "
+ MediaLibrary.SongColumns.MTIME +" TIMESTAMP DEFAULT (strftime('%s', CURRENT_TIMESTAMP)), "
+ MediaLibrary.SongColumns.DURATION +" INTEGER NOT NULL, "
+ MediaLibrary.SongColumns.PATH +" VARCHAR(4096) NOT NULL, "
+ MediaLibrary.SongColumns.FLAGS +" INTEGER NOT NULL DEFAULT 0 "
+ MediaLibrary.SongColumns._ID +" INTEGER PRIMARY KEY, "
+ MediaLibrary.SongColumns.TITLE +" TEXT NOT NULL, "
+ MediaLibrary.SongColumns.TITLE_SORT +" VARCHAR(64) NOT NULL, "
+ MediaLibrary.SongColumns.SONG_NUMBER +" INTEGER, "
+ MediaLibrary.SongColumns.DISC_NUMBER +" INTEGER, "
+ MediaLibrary.SongColumns.YEAR +" INTEGER, "
+ MediaLibrary.SongColumns.ALBUM_ID +" INTEGER NOT NULL, "
+ MediaLibrary.SongColumns.PLAYCOUNT +" INTEGER NOT NULL DEFAULT 0, "
+ MediaLibrary.SongColumns.SKIPCOUNT +" INTEGER NOT NULL DEFAULT 0, "
+ MediaLibrary.SongColumns.MTIME +" TIMESTAMP DEFAULT (strftime('%s', CURRENT_TIMESTAMP)), "
+ MediaLibrary.SongColumns.DURATION +" INTEGER NOT NULL, "
+ MediaLibrary.SongColumns.TIMESTAMP_LAST_PLAY+" INTEGER, "
+ MediaLibrary.SongColumns.PATH +" VARCHAR(4096) NOT NULL, "
+ MediaLibrary.SongColumns.FLAGS +" INTEGER NOT NULL DEFAULT 0 "
+ ");";

/**
* SQL Schema of `albums' table
*/
private static final String DATABASE_CREATE_ALBUMS = "CREATE TABLE "+ MediaLibrary.TABLE_ALBUMS + " ("
+ MediaLibrary.AlbumColumns._ID +" INTEGER PRIMARY KEY, "
+ MediaLibrary.AlbumColumns.ALBUM +" TEXT NOT NULL, "
+ MediaLibrary.AlbumColumns.ALBUM_SORT +" VARCHAR(64) NOT NULL, "
+ MediaLibrary.AlbumColumns.PRIMARY_ALBUM_YEAR+" INTEGER, "
+ MediaLibrary.AlbumColumns.PRIMARY_ARTIST_ID +" INTEGER NOT NULL DEFAULT 0, "
+ MediaLibrary.AlbumColumns.MTIME +" TIMESTAMP DEFAULT CURRENT_TIMESTAMP "
+ MediaLibrary.AlbumColumns._ID +" INTEGER PRIMARY KEY, "
+ MediaLibrary.AlbumColumns.ALBUM +" TEXT NOT NULL, "
+ MediaLibrary.AlbumColumns.ALBUM_SORT +" VARCHAR(64) NOT NULL, "
+ MediaLibrary.AlbumColumns.PRIMARY_ALBUM_YEAR +" INTEGER, "
+ MediaLibrary.AlbumColumns.PRIMARY_ARTIST_ID +" INTEGER NOT NULL DEFAULT 0, "
+ MediaLibrary.AlbumColumns.LAST_TRACK_PLAYED +" INTEGER, "
+ MediaLibrary.AlbumColumns.START_AT_TRACKTIMESTAMP +" INTEGER DEFAULT -1, "
+ MediaLibrary.AlbumColumns.MTIME +" TIMESTAMP DEFAULT CURRENT_TIMESTAMP "
+ ");";

/**
Expand Down Expand Up @@ -364,6 +367,10 @@ public static void upgradeDatabaseSchema(SQLiteDatabase dbh, int oldVersion) {
if (oldVersion < 20190210) {
dbh.execSQL(VIEW_CREATE_PLAYLISTS);
}

if(oldVersion < 20190228){
MediaMigrations.migrate_to_20190228(dbh);
}
}

}
Loading