Merge pull request #451 from tzugen/remove-mediastore

Remove MediaStoreService, reinstate Util.scanMedia
This commit is contained in:
Nite 2021-05-06 11:52:19 +02:00 committed by GitHub
commit 162bb18078
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 119 deletions

View File

@ -188,6 +188,7 @@ public class Downloader
DownloadFile downloadFile = backgroundDownloadList.get(i);
if (downloadFile.isWorkDone() && (!downloadFile.shouldSave() || downloadFile.isSaved()))
{
Util.scanMedia(context, downloadFile.getCompleteFile());
// Don't need to keep list like active song list
backgroundDownloadList.remove(i);

View File

@ -33,6 +33,7 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
@ -1323,6 +1324,12 @@ public class Util
return preferences.getBoolean(Constants.PREFERENCES_KEY_SHOW_ALL_SONGS_BY_ARTIST, false);
}
public static void scanMedia(Context context, File file)
{
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
context.sendBroadcast(scanFileIntent);
}
public static int getImageLoaderConcurrency(Context context)
{

View File

@ -42,7 +42,6 @@ class DownloadFile(
val partialFile: File
val completeFile: File
private val saveFile: File = FileUtil.getSongFile(context, song)
private val mediaStoreService: MediaStoreService
private var downloadTask: CancellableTask? = null
var isFailed = false
private var retryCount = MAX_RETRIES
@ -65,7 +64,6 @@ class DownloadFile(
init {
partialFile = File(saveFile.parent, FileUtil.getPartialFile(saveFile.name))
completeFile = File(saveFile.parent, FileUtil.getCompleteFile(saveFile.name))
mediaStoreService = MediaStoreService(context)
}
/**
@ -137,7 +135,8 @@ class DownloadFile(
Util.delete(partialFile)
Util.delete(completeFile)
Util.delete(saveFile)
mediaStoreService.deleteFromMediaStore(this)
Util.scanMedia(context, saveFile)
}
fun unpin() {
@ -185,7 +184,7 @@ class DownloadFile(
} else if (completeWhenDone) {
if (save) {
Util.renameFile(partialFile, saveFile)
mediaStoreService.saveInMediaStore(this@DownloadFile)
Util.scanMedia(context, saveFile)
} else {
Util.renameFile(partialFile, completeFile)
}
@ -282,7 +281,7 @@ class DownloadFile(
} else {
if (save) {
Util.renameFile(partialFile, saveFile)
mediaStoreService.saveInMediaStore(this@DownloadFile)
Util.scanMedia(context, saveFile)
} else {
Util.renameFile(partialFile, completeFile)
}

View File

@ -1,114 +0,0 @@
/*
* MediaStoreService.kt
* Copyright (C) 2009-2021 Ultrasonic developers
*
* Distributed under terms of the GNU GPLv3 license.
*/
package org.moire.ultrasonic.service
import android.content.ContentValues
import android.content.Context
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import org.moire.ultrasonic.util.FileUtil
import timber.log.Timber
/**
* By adding UltraSonics media files to the Android MediaStore
* they become available in the stock music apps
*
* @author Sindre Mehus
*/
class MediaStoreService(private val context: Context) {
// Find the audio collection on the primary external storage device.
val collection: Uri by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
MediaStore.Audio.Media.getContentUri(
MediaStore.VOLUME_EXTERNAL_PRIMARY
)
} else {
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
}
}
val albumArtCollection: Uri by lazy {
// This path is not well documented
// https://android.googlesource.com/platform/packages/providers/
// MediaProvider/+/refs/tags/android-platform-11.0.0_r5/
// src/com/android/providers/media/MediaProvider.java#7596
Uri.parse(collection.toString().replaceAfterLast("/", "albumart"))
}
fun saveInMediaStore(downloadFile: DownloadFile) {
val song = downloadFile.song
val songFile = downloadFile.completeFile
// Delete existing row in case the song has been downloaded before.
deleteFromMediaStore(downloadFile)
val contentResolver = context.contentResolver
val values = ContentValues()
values.put(MediaStore.MediaColumns.TITLE, song.title)
values.put(MediaStore.Audio.AudioColumns.ARTIST, song.artist)
values.put(MediaStore.Audio.AudioColumns.ALBUM, song.album)
values.put(MediaStore.Audio.AudioColumns.TRACK, song.track)
values.put(MediaStore.Audio.AudioColumns.YEAR, song.year)
values.put(MediaStore.MediaColumns.DATA, songFile.absolutePath)
values.put(MediaStore.MediaColumns.MIME_TYPE, song.contentType)
values.put(MediaStore.Audio.AudioColumns.IS_MUSIC, 1)
val uri = contentResolver.insert(collection, values)
if (uri != null) {
// Look up album, and add cover art if found.
val cursor = contentResolver.query(
uri, arrayOf(MediaStore.Audio.AudioColumns.ALBUM_ID),
null, null, null
)
if (cursor != null && cursor.moveToFirst()) {
val albumId = cursor.getInt(0)
insertAlbumArt(albumId, downloadFile)
cursor.close()
}
}
}
fun deleteFromMediaStore(downloadFile: DownloadFile) {
val contentResolver = context.contentResolver
val song = downloadFile.song
val file = downloadFile.completeFile
val selection = MediaStore.Audio.AudioColumns.TITLE_KEY + "=? AND " +
MediaStore.MediaColumns.DATA + "=?"
val selectionArgs = arrayOf(MediaStore.Audio.keyFor(song.title), file.absolutePath)
val res = contentResolver.delete(collection, selection, selectionArgs)
if (res > 0) {
Timber.i("Deleting media store row for %s", song)
}
}
private fun insertAlbumArt(albumId: Int, downloadFile: DownloadFile) {
val contentResolver = context.contentResolver
val uri = Uri.withAppendedPath(albumArtCollection, albumId.toString())
?: return
val cursor = contentResolver.query(uri, null, null, null, null)
if (cursor != null && !cursor.moveToFirst()) {
// No album art found, add it.
val albumArtFile = FileUtil.getAlbumArtFile(context, downloadFile.song)
if (albumArtFile.exists()) {
val values = ContentValues()
values.put(MediaStore.Audio.AlbumColumns.ALBUM_ID, albumId)
// values.put(MediaStore.MediaColumns.DATA, albumArtFile.path)
contentResolver.insert(albumArtCollection, values)
Timber.i("Added album art: %s", albumArtFile)
}
cursor.close()
}
}
}