diff --git a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaLibrary.java b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaLibrary.java index dd140fda..97f0830b 100644 --- a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaLibrary.java +++ b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaLibrary.java @@ -360,6 +360,7 @@ public class MediaLibrary { ContentValues v = new ContentValues(); v.put(MediaLibrary.PlaylistColumns._ID, hash63(name)); v.put(MediaLibrary.PlaylistColumns.NAME, name); + v.put(MediaLibrary.PlaylistColumns.NAME_SORT, keyFor(name)); long id = getBackend(context).insert(MediaLibrary.TABLE_PLAYLISTS, null, v); if (id != -1) @@ -747,6 +748,10 @@ public class MediaLibrary { * The name of this playlist */ String NAME = "name"; + /** + * Sortable column for name + */ + String NAME_SORT = "name_sort"; } // Song <-> Playlist mapping diff --git a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java index 7a99f3fa..9b54ef8f 100644 --- a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java +++ b/app/src/main/java/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 = 20180305; + private static final int DATABASE_VERSION = 20180416; /** * on-disk file to store the database */ diff --git a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaMigrations.java b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaMigrations.java new file mode 100644 index 00000000..c77e273b --- /dev/null +++ b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaMigrations.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2018 Adrian Ulrich + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package ch.blinkenlights.android.medialibrary; + +import android.content.ContentValues; +import android.database.sqlite.SQLiteDatabase; +import android.database.Cursor; +import android.util.Log; + + +public class MediaMigrations { + /** + * Migrate to 20180416 + * That is: populate NAME_SORT in the new source database + * + * @param dbh the database to work on + * @param fromDb the name of the source database + * @param toDb the name of the target database + **/ + static void migrate_to_20180416(SQLiteDatabase dbh, String fromDb, String toDb) { + Cursor cursor = dbh.query(fromDb, new String[]{MediaLibrary.PlaylistColumns._ID, MediaLibrary.PlaylistColumns.NAME}, null, null, null, null, null); + while (cursor.moveToNext()) { + long id = cursor.getLong(0); + String name = cursor.getString(1); + String key = MediaLibrary.keyFor(name); + + Log.v("VanillaMusic", "migrate_to_20180416 -> id="+id+", name="+name+" -> key = "+key); + ContentValues v = new ContentValues(); + v.put(MediaLibrary.PlaylistColumns._ID, id); + v.put(MediaLibrary.PlaylistColumns.NAME, name); + v.put(MediaLibrary.PlaylistColumns.NAME_SORT, key); + dbh.insert(toDb, null, v); + } + cursor.close(); + } + +} diff --git a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaSchema.java b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaSchema.java index e7159367..dcc0a95e 100644 --- a/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaSchema.java +++ b/app/src/main/java/ch/blinkenlights/android/medialibrary/MediaSchema.java @@ -104,8 +104,9 @@ public class MediaSchema { * SQL Schema for the playlists table */ private static final String DATABASE_CREATE_PLAYLISTS = "CREATE TABLE "+ MediaLibrary.TABLE_PLAYLISTS +" (" - + MediaLibrary.PlaylistColumns._ID +" INTEGER PRIMARY KEY, " - + MediaLibrary.PlaylistColumns.NAME +" TEXT NOT NULL " + + MediaLibrary.PlaylistColumns._ID +" INTEGER PRIMARY KEY, " + + MediaLibrary.PlaylistColumns.NAME +" TEXT NOT NULL, " + + MediaLibrary.PlaylistColumns.NAME_SORT +" TEXT NOT NULL " + ");"; /** @@ -331,6 +332,14 @@ public class MediaSchema { dbh.execSQL("ALTER TABLE "+MediaLibrary.TABLE_SONGS+" ADD COLUMN "+MediaLibrary.SongColumns.FLAGS+" INTEGER NOT NULL DEFAULT 0 "); } + if (oldVersion < 20180416) { + // This adds NAME_SORT, so we need to pre-populate all keys. + dbh.execSQL("ALTER TABLE "+MediaLibrary.TABLE_PLAYLISTS+" RENAME TO _migrate"); + dbh.execSQL(MediaSchema.DATABASE_CREATE_PLAYLISTS); + MediaMigrations.migrate_to_20180416(dbh, "_migrate", MediaLibrary.TABLE_PLAYLISTS); + dbh.execSQL("DROP TABLE _migrate"); + } + } } diff --git a/app/src/main/java/ch/blinkenlights/android/vanilla/MediaAdapter.java b/app/src/main/java/ch/blinkenlights/android/vanilla/MediaAdapter.java index 9cd239eb..7a8a5098 100644 --- a/app/src/main/java/ch/blinkenlights/android/vanilla/MediaAdapter.java +++ b/app/src/main/java/ch/blinkenlights/android/vanilla/MediaAdapter.java @@ -35,6 +35,7 @@ import android.provider.MediaStore; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.style.ForegroundColorSpan; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -222,7 +223,7 @@ public class MediaAdapter case MediaUtils.TYPE_PLAYLIST: mSource = MediaLibrary.TABLE_PLAYLISTS; mFields = new String[] { MediaLibrary.PlaylistColumns.NAME }; - mFieldKeys = null; + mFieldKeys = new String[] { MediaLibrary.PlaylistColumns.NAME_SORT }; mSortEntries = new int[] { R.string.title, R.string.date_added }; mAdapterSortValues = new String[] { MediaLibrary.PlaylistColumns.NAME+" %1$s", MediaLibrary.PlaylistColumns._ID+" %1$s" }; mExpandable = true; @@ -318,19 +319,10 @@ public class MediaAdapter // include the constraint (aka: search string) if any if (constraint != null && constraint.length() != 0) { - String[] needles; - String[] keySource; - - if (mFieldKeys != null) { - String colKey = MediaLibrary.keyFor(constraint); - String spaceColKey = DatabaseUtils.getCollationKey(" "); - needles = colKey.split(spaceColKey); - keySource = mFieldKeys; - } else { - // only used for playlists, maybe we should just update the schema ? - needles = SPACE_SPLIT.split(constraint); - keySource = mFields; - } + String colKey = MediaLibrary.keyFor(constraint); + String spaceColKey = DatabaseUtils.getCollationKey(" "); + String[] needles = colKey.split(spaceColKey); + String[] keySource = mFieldKeys; int size = needles.length; selectionArgs = new String[size];