use of android.provider.MediaStore in project Shuttle by timusus.
the class ShuttleApplication method cleanMostPlayedPlaylist.
/**
* Check items in the Most Played playlist and ensure their ids exist in the MediaStore.
* <p>
* If they don't, remove them from the playlist.
*/
@NonNull
private Completable cleanMostPlayedPlaylist() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return Completable.complete();
}
return Completable.fromAction(() -> {
List<Integer> playCountIds = new ArrayList<>();
Query query = new Query.Builder().uri(PlayCountTable.URI).projection(new String[] { PlayCountTable.COLUMN_ID }).build();
SqlUtils.createActionableQuery(this, cursor -> playCountIds.add(cursor.getInt(cursor.getColumnIndex(PlayCountTable.COLUMN_ID))), query);
List<Integer> songIds = new ArrayList<>();
query = new Query.Builder().uri(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI).projection(new String[] { MediaStore.Audio.Media._ID }).build();
SqlUtils.createActionableQuery(this, cursor -> songIds.add(cursor.getInt(cursor.getColumnIndex(PlayCountTable.COLUMN_ID))), query);
StringBuilder selection = new StringBuilder(PlayCountTable.COLUMN_ID + " IN (");
selection.append(TextUtils.join(",", Stream.of(playCountIds).filter(playCountId -> !songIds.contains(playCountId)).toList()));
selection.append(")");
try {
getContentResolver().delete(PlayCountTable.URI, selection.toString(), null);
} catch (IllegalArgumentException ignored) {
}
});
}
Aggregations