1

I've written an app that sorts Poweramp playlists, and it works all right. The only issue I'm running into is performance, and I think that's because I call getContentResolver().update on each row. Ideally, I'd love to be able to use a SQLite transaction. applyBatch and bulkInsert won't work, as I need to have a WHERE clause with each row update--I essentially have to update an index table, which has multiple playlists and their songs.

From what I understand, I won't be able to access Poweramp's database directly through SQLite, as I've read I need to have the same app signature or something? Maybe someone can clarify that. It seems like my only hope is to maybe override ContentResolver's applyBatch method or add my own (from this thread)? Although, I'm not entirely sure how to override that... the code in that thread already seems to have overridden the class somehow. I also don't think it uses an outside DB.

Any hints as to where I should look, or if there's already an answer out there, that'd be excellent.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
incutonez
  • 3,241
  • 9
  • 43
  • 92
  • Why an `applyBatch` won't work? You can create `ContentProviderOperation` using `ContentProviderOperation.Builder` which support WHERE clauses – MatPag Jan 31 '19 at 08:32
  • Maybe I didn't see that part, but it also required an AUTHORITY, which I had no clue what should go there... I tried putting the package's name there but got some android application exception. – incutonez Jan 31 '19 at 14:15
  • The AUTHORITY it's something specific to ContentProviders, if you read the official documentation about them, you will probably find out what is used for – MatPag Jan 31 '19 at 14:44
  • I did read a little about it, and was still confused... I'm not using a DB in my package, so I've tried a combination of using Poweramp's package name, my package name, etc... nothing seemed to work. I guess I'll try to dig a little deeper with that. – incutonez Jan 31 '19 at 15:58

1 Answers1

0

What I ended up doing was going with ContentProviderOperation and getContentResolver().applyBatch (thanks to @MatPag giving the advice that it's possible)... this now runs a whole lot faster, and I don't get the Wait messages anymore. The key part to applyBatch is you must wrap it in a try..catch, which isn't obvious by the Android Studio error that it reports. Here's the final code:

Cursor songs = createPlaylistCursor(sort);
if (songs.moveToFirst()) {
    int i = 0;
    Uri playListUri = getPlaylistUri();
    ArrayList<ContentProviderOperation> cs = new ArrayList<ContentProviderOperation>();
    do {
        cs.add(ContentProviderOperation.newUpdate(playListUri)
          .withSelection("folder_file_id=" + songs.getInt(songs.getColumnIndexOrThrow("folder_files._id")), null)
          .withValue("sort", i++)
          .build());
    } while (songs.moveToNext());
    try {
        getContentResolver().applyBatch("com.maxmpz.audioplayer.data", cs);
    }
    catch (Exception e) {
    }
}
incutonez
  • 3,241
  • 9
  • 43
  • 92