add handling for deleting files on Android 11

This commit is contained in:
tibbi 2021-10-02 11:13:07 +02:00
parent 3eeccc0ad7
commit a5c60bceb8
3 changed files with 47 additions and 22 deletions

View file

@ -62,7 +62,7 @@ android {
}
dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:a74ec200da'
implementation 'com.github.SimpleMobileTools:Simple-Commons:3e78714205'
implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'androidx.media:media:1.3.1'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

View file

@ -1,8 +1,14 @@
package com.simplemobiletools.musicplayer.extensions
import android.app.Activity
import android.content.ContentUris
import android.net.Uri
import android.provider.MediaStore
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isRPlus
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.dialogs.SelectPlaylistDialog
import com.simplemobiletools.musicplayer.helpers.RoomHelper
import com.simplemobiletools.musicplayer.models.Events
@ -43,12 +49,31 @@ fun Activity.addTracksToQueue(tracks: List<Track>, callback: () -> Unit) {
}
}
fun Activity.deleteTracks(tracks: List<Track>, callback: () -> Unit) {
fun BaseSimpleActivity.deleteTracks(tracks: List<Track>, callback: () -> Unit) {
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
if (isRPlus()) {
val uris = arrayListOf<Uri>()
tracks.forEach { track ->
val newUri = ContentUris.withAppendedId(uri, track.mediaStoreId)
uris.add(newUri)
}
deleteSDK30Uris(uris) { success ->
if (success) {
removeQueueItems(tracks) {}
EventBus.getDefault().post(Events.TrackDeleted())
callback()
} else {
toast(R.string.unknown_error_occurred)
}
}
return
}
tracks.forEach { track ->
try {
val where = "${MediaStore.Audio.Media._ID} = ?"
val args = arrayOf(track.mediaStoreId.toString())
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
contentResolver.delete(uri, where, args)
tracksDAO.removeTrack(track.mediaStoreId)
} catch (ignored: Exception) {