From a5c60bceb88c3b180504c96570977485d5ba68bf Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 2 Oct 2021 11:13:07 +0200 Subject: [PATCH] add handling for deleting files on Android 11 --- app/build.gradle | 2 +- .../musicplayer/activities/SimpleActivity.kt | 38 +++++++++---------- .../musicplayer/extensions/Activity.kt | 29 +++++++++++++- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0359183d..0d918d72 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleActivity.kt index 2f7963c0..4dc5ddf4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleActivity.kt @@ -5,25 +5,25 @@ import com.simplemobiletools.musicplayer.R open class SimpleActivity : BaseSimpleActivity() { override fun getAppIconIDs() = arrayListOf( - R.mipmap.ic_launcher_red, - R.mipmap.ic_launcher_pink, - R.mipmap.ic_launcher_purple, - R.mipmap.ic_launcher_deep_purple, - R.mipmap.ic_launcher_indigo, - R.mipmap.ic_launcher_blue, - R.mipmap.ic_launcher_light_blue, - R.mipmap.ic_launcher_cyan, - R.mipmap.ic_launcher_teal, - R.mipmap.ic_launcher_green, - R.mipmap.ic_launcher_light_green, - R.mipmap.ic_launcher_lime, - R.mipmap.ic_launcher_yellow, - R.mipmap.ic_launcher_amber, - R.mipmap.ic_launcher, - R.mipmap.ic_launcher_deep_orange, - R.mipmap.ic_launcher_brown, - R.mipmap.ic_launcher_blue_grey, - R.mipmap.ic_launcher_grey_black + R.mipmap.ic_launcher_red, + R.mipmap.ic_launcher_pink, + R.mipmap.ic_launcher_purple, + R.mipmap.ic_launcher_deep_purple, + R.mipmap.ic_launcher_indigo, + R.mipmap.ic_launcher_blue, + R.mipmap.ic_launcher_light_blue, + R.mipmap.ic_launcher_cyan, + R.mipmap.ic_launcher_teal, + R.mipmap.ic_launcher_green, + R.mipmap.ic_launcher_light_green, + R.mipmap.ic_launcher_lime, + R.mipmap.ic_launcher_yellow, + R.mipmap.ic_launcher_amber, + R.mipmap.ic_launcher, + R.mipmap.ic_launcher_deep_orange, + R.mipmap.ic_launcher_brown, + R.mipmap.ic_launcher_blue_grey, + R.mipmap.ic_launcher_grey_black ) override fun getAppLauncherName() = getString(R.string.app_launcher_name) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Activity.kt index 28a03c7e..e84ce7a5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Activity.kt @@ -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, callback: () -> Unit) { } } -fun Activity.deleteTracks(tracks: List, callback: () -> Unit) { +fun BaseSimpleActivity.deleteTracks(tracks: List, callback: () -> Unit) { + val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI + if (isRPlus()) { + val uris = arrayListOf() + 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) {