Code refactor
This commit is contained in:
parent
6d0898a49a
commit
5ebeb9c587
20 changed files with 199 additions and 243 deletions
|
@ -4,11 +4,13 @@ import android.content.ContentUris
|
|||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.os.Environment
|
||||
import android.provider.BaseColumns
|
||||
import android.provider.MediaStore
|
||||
import android.text.TextUtils
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.core.content.FileProvider
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
|
@ -20,6 +22,7 @@ import code.name.monkey.retromusic.model.Playlist
|
|||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.model.lyrics.AbsSynchronizedLyrics
|
||||
import code.name.monkey.retromusic.repository.RealPlaylistRepository
|
||||
import code.name.monkey.retromusic.repository.RealSongRepository
|
||||
import code.name.monkey.retromusic.repository.SongRepository
|
||||
import code.name.monkey.retromusic.service.MusicService
|
||||
import org.jaudiotagger.audio.AudioFileIO
|
||||
|
@ -421,6 +424,78 @@ object MusicUtil : KoinComponent {
|
|||
.show()
|
||||
callback?.run()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteTracks(context: Context, songs: List<Song>) {
|
||||
val projection = arrayOf(
|
||||
BaseColumns._ID, MediaStore.MediaColumns.DATA
|
||||
)
|
||||
val selection = StringBuilder()
|
||||
selection.append(BaseColumns._ID + " IN (")
|
||||
for (i in songs.indices) {
|
||||
selection.append(songs[i].id)
|
||||
if (i < songs.size - 1) {
|
||||
selection.append(",")
|
||||
}
|
||||
}
|
||||
selection.append(")")
|
||||
var deletedCount = 0
|
||||
try {
|
||||
val cursor: Cursor? = context.contentResolver.query(
|
||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection.toString(),
|
||||
null, null
|
||||
)
|
||||
if (cursor != null) {
|
||||
// Step 1: Remove selected tracks from the current playlist, as well
|
||||
// as from the album art cache
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
val id: Int = cursor.getInt(0)
|
||||
val song: Song = RealSongRepository(context).song(id)
|
||||
removeFromQueue(song)
|
||||
cursor.moveToNext()
|
||||
}
|
||||
|
||||
|
||||
// Step 2: Remove files from card
|
||||
cursor.moveToFirst()
|
||||
while (!cursor.isAfterLast) {
|
||||
val id: Int = cursor.getInt(0)
|
||||
val name: String = cursor.getString(1)
|
||||
try { // File.delete can throw a security exception
|
||||
val f = File(name)
|
||||
if (f.delete()) {
|
||||
// Step 3: Remove selected track from the database
|
||||
context.contentResolver.delete(
|
||||
ContentUris.withAppendedId(
|
||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
id.toLong()
|
||||
), null, null
|
||||
)
|
||||
deletedCount++
|
||||
} else {
|
||||
// I'm not sure if we'd ever get here (deletion would
|
||||
// have to fail, but no exception thrown)
|
||||
Log.e("MusicUtils", "Failed to delete file $name")
|
||||
}
|
||||
cursor.moveToNext()
|
||||
} catch (ex: SecurityException) {
|
||||
cursor.moveToNext()
|
||||
} catch (e: NullPointerException) {
|
||||
Log.e("MusicUtils", "Failed to find file $name")
|
||||
}
|
||||
}
|
||||
cursor.close()
|
||||
}
|
||||
Toast.makeText(
|
||||
context,
|
||||
context.getString(R.string.deleted_x_songs, deletedCount),
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
} catch (ignored: SecurityException) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -77,7 +77,7 @@ class RingtoneManager(val context: Context) {
|
|||
return MaterialAlertDialogBuilder(context)
|
||||
.setTitle(R.string.dialog_title_set_ringtone)
|
||||
.setMessage(R.string.dialog_message_set_ringtone)
|
||||
.setPositiveButton(R.string.ok) { _, _ ->
|
||||
.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
|
||||
intent.data = Uri.parse("package:" + context.applicationContext.packageName)
|
||||
context.startActivity(intent)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue