From e01117fb553326d9e49215ef73bd5ae22b5e278a Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Tue, 3 Jul 2018 12:29:08 +0200 Subject: [PATCH] introduce playlist-observer metadata --- .../android/vanilla/PlaylistObserver.java | 190 +++++++++++++++--- 1 file changed, 157 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/ch/blinkenlights/android/vanilla/PlaylistObserver.java b/app/src/main/java/ch/blinkenlights/android/vanilla/PlaylistObserver.java index 6473c481..8fcb9141 100644 --- a/app/src/main/java/ch/blinkenlights/android/vanilla/PlaylistObserver.java +++ b/app/src/main/java/ch/blinkenlights/android/vanilla/PlaylistObserver.java @@ -20,8 +20,11 @@ package ch.blinkenlights.android.vanilla; import ch.blinkenlights.android.medialibrary.MediaLibrary; import ch.blinkenlights.android.medialibrary.LibraryObserver; -import android.database.Cursor; import android.content.Context; +import android.content.ContentValues; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; import android.os.Environment; import android.os.Handler; import android.os.HandlerThread; @@ -35,15 +38,23 @@ import java.io.IOException; import android.util.Log; -public class PlaylistObserver implements Handler.Callback { +public class PlaylistObserver extends SQLiteOpenHelper implements Handler.Callback { /** * Timeout to coalesce duplicate messages. */ private final static int COALESCE_EVENTS_DELAY_MS = 280; + /** + * Extension to use for M3U files + */ + private final static String M3U_EXT = ".m3u"; /** * Context to use. */ private Context mContext; + /** + * Thread used for execution. + */ + private HandlerThread mHandlerThread; /** * Handler thread used to perform playlist sync. */ @@ -53,13 +64,27 @@ public class PlaylistObserver implements Handler.Callback { */ private File mPlaylists = new File(Environment.getExternalStorageDirectory(), "Playlists"); + static class Database { + final static String TABLE_NAME = "playlist_metadata"; + final static String _ID = "_id"; + final static String NAME = "name"; + final static String MTIME = "mtime"; + final static String[] FILLED_PROJECTION = { + _ID, + NAME, + MTIME, + }; + } public PlaylistObserver(Context context) { + super(context, "playlistobserver.db", null, 1 /* version */); + mContext = context; - // Create thread which will be used for background work. - HandlerThread handlerThread = new HandlerThread("PlaylistWriter", Process.THREAD_PRIORITY_LOWEST); - handlerThread.start(); - mHandler = new Handler(handlerThread.getLooper(), this); + // Launch new thread for background execution + mHandlerThread= new HandlerThread("PlaylistWriter", Process.THREAD_PRIORITY_LOWEST); + mHandlerThread.start(); + mHandler = new Handler(mHandlerThread.getLooper(), this); + // Register to receive media library events. MediaLibrary.registerLibraryObserver(mObserver); } @@ -70,20 +95,28 @@ public class PlaylistObserver implements Handler.Callback { */ public void unregister() { MediaLibrary.unregisterLibraryObserver(mObserver); + + mHandlerThread.quitSafely(); + mHandlerThread = null; + mHandler = null; } - private final static int MSG_SYNC_M3U = 1; + private final static int MSG_DUMP_M3U = 1; + private final static int MSG_DUMP_ALL_M3U = 2; @Override public boolean handleMessage(Message message) { switch (message.what) { - case MSG_SYNC_M3U: + case MSG_DUMP_M3U: Long id = (Long)message.obj; - if (id == -1) { - syncAllM3u(); - } else { - syncM3uPlaylist(id); + if (!dumpM3uPlaylist(id)) { + // Dump of 'id' failed, so this playlist was likely deleted. + cleanupOrphanedM3u(); } break; + case MSG_DUMP_ALL_M3U: + dumpM3uPlaylists(); + cleanupOrphanedM3u(); + break; default: throw new IllegalArgumentException("Invalid message type received"); } @@ -93,26 +126,29 @@ public class PlaylistObserver implements Handler.Callback { /** * Exports a single playlist ad M3U(8). * - * @param id the playlist id to export + * @param id the playlist id to export. + * @return true if the playlist was dumped. */ - private void syncM3uPlaylist(long id) { - String name = Playlist.getPlaylist(mContext, id); + private boolean dumpM3uPlaylist(long id) { + final String name = Playlist.getPlaylist(mContext, id); - if (name == null) { - cleanupOrphanedM3u(); - return; - } + if (id < 0) + throw new IllegalArgumentException("Called with negative id!"); + + if (name == null) + return false; if (!mPlaylists.isDirectory()) mPlaylists.mkdir(); - Log.v("VanillaMusic", "Dumping "+getFileFromName(name)); + + Log.v("VanillaMusic", "Dumping "+getFileForName(mPlaylists, name)); PrintWriter pw = null; QueryTask query = MediaUtils.buildPlaylistQuery(id, Song.FILLED_PLAYLIST_PROJECTION); Cursor cursor = query.runQuery(mContext); try { if (cursor != null) { - pw = new PrintWriter(getFileFromName(name)); + pw = new PrintWriter(getFileForName(mPlaylists, name + M3U_EXT)); pw.println("#EXTM3U"); while (cursor.moveToNext()) { final String path = cursor.getString(1); @@ -123,27 +159,26 @@ public class PlaylistObserver implements Handler.Callback { pw.printf("#EXTINF:%d,%s - %s%n", (duration/1000), artist, title); pw.println(path); } - cursor.close(); + updatePlaylistMetadata(id, name); } } catch (IOException e) { Log.v("VanillaMusic", "IOException while writing:", e); } finally { + if (cursor != null) cursor.close(); if (pw != null) pw.close(); } + return true; } /** * Dumps all playlist to stable storage. */ - private void syncAllM3u() { - Log.v("VanillaMusic", "M3U: Dumping all playlists"); - cleanupOrphanedM3u(); - + private void dumpM3uPlaylists() { Cursor cursor = Playlist.queryPlaylists(mContext); if (cursor != null) { while(cursor.moveToNext()) { final long id = cursor.getLong(0); - syncM3uPlaylist(id); + sendUniqueMessage(MSG_DUMP_M3U, id); } cursor.close(); } @@ -154,21 +189,95 @@ public class PlaylistObserver implements Handler.Callback { * non-existing playlists and removes them. */ private void cleanupOrphanedM3u() { - Log.v("VanillaMusic", "Implement me!"); + SQLiteDatabase dbh = getReadableDatabase(); + Cursor cursor = dbh.query(Database.TABLE_NAME, Database.FILLED_PROJECTION, null, null, null, null, null); + if (cursor != null) { + while (cursor.moveToNext()) { + final long id = cursor.getLong(0); + final String name = cursor.getString(1); + final File src_m3u = getFileForName(mPlaylists, name + M3U_EXT); + + if (Playlist.getPlaylist(mContext, id) == null) { + // Native version of this playlist is gone, rename M3U variant: + File dst_m3u = getFileForName(mPlaylists, name + ".bak"); + src_m3u.renameTo(dst_m3u); + deletePlaylistMetadata(id); + Log.v("VanillaMusic", name+": Renamed old m3u"); + } else if (!src_m3u.exists()) { + Playlist.deletePlaylist(mContext, id); // Fixme: do we really want this? + deletePlaylistMetadata(id); + Log.v("VanillaMusic", name+": Killed native playlist"); + } + } + cursor.close(); + } } /** - * Returns a file object for given playlist name + * Returns a file object for given name * * @param name name of playlist to use. * @return file object for given name */ - private File getFileFromName(String name) { + private File getFileForName(File parent, String name) { //Fixme: check for m3u8 and remove invalid chars. - File f = new File(mPlaylists, name +".m3u8"); + name = name.replaceAll("/", "_"); + File f = new File(parent, name); return f; } + /** + * Updates the metadata of given playlist name + * + * @param id the id to update. + * @param name the name to register for this id. + */ + private void updatePlaylistMetadata(long id, String name) { + SQLiteDatabase dbh = getWritableDatabase(); + ContentValues values = new ContentValues(); + values.put(Database._ID, id); + values.put(Database.NAME, name); + values.put(Database.MTIME, System.currentTimeMillis()); + + deletePlaylistMetadata(id); + dbh.insert(Database.TABLE_NAME, null, values); + } + + /** + * Removes all known metadata of given id. + * + * @param id the id to purge. + */ + private void deletePlaylistMetadata(long id) { + SQLiteDatabase dbh = getWritableDatabase(); + dbh.delete(Database.TABLE_NAME, Database._ID+"=?", new String[] { new Long(id).toString() }); + } + + /** + * Returns true if the filename looks like an m3u file + * + * @param name the name to check + * @return true if file appears to be an M3U + */ + private boolean FIXME_isM3uFilename(String name) { + if (name.length() < M3U_EXT.length()) + return false; + final int offset = name.length() - M3U_EXT.length(); + return name.toLowerCase().substring(offset).equals(M3U_EXT.toLowerCase()); + } + + /** + * Adds a new message to the queue. Pending duplicate messages + * will be pruged. + * + * @param type the type of the message + * @param obj object payload of this message. + */ + private void sendUniqueMessage(int type, Long obj) { + mHandler.removeMessages(type, obj); + mHandler.sendMessageDelayed(mHandler.obtainMessage(type, obj), COALESCE_EVENTS_DELAY_MS); + } + /** * Library observer callback which notifies us about media library * events. @@ -179,11 +288,26 @@ public class PlaylistObserver implements Handler.Callback { if (type != LibraryObserver.Type.PLAYLIST || ongoing) return; - mHandler.removeMessages(MSG_SYNC_M3U, id); - mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SYNC_M3U, id), COALESCE_EVENTS_DELAY_MS); + // Dispatch this event but use different type if id was -1 as + // this indicates that multiple (unknown) playlists may have changed. + final int msg = (id < 0 ? MSG_DUMP_ALL_M3U : MSG_DUMP_M3U); + sendUniqueMessage(msg, id); } }; + @Override + public void onCreate(SQLiteDatabase dbh) { + dbh.execSQL("CREATE TABLE "+Database.TABLE_NAME+" ( " + + Database._ID + " INTEGER PRIMARY KEY, " + + Database.MTIME + " INTEGER NOT NULL, " + + Database.NAME + " TEXT NOT NULL )" + ); + } + + @Override + public void onUpgrade(SQLiteDatabase dbh, int oldVersion, int newVersion) { + // No updates so far. + } // TODO: // Use FileObserver to track playlist changes? // how do we check modifications? write a shadow-dir in private app storage with same mtimes?