mirror of
https://github.com/ultrasonic/ultrasonic
synced 2025-02-12 17:50:39 +01:00
Remove MediaStoreService, reinstate Util.scanMedia
This partially reverts commit 8fbc2a9fa3f0430303954536baa8f5992bd3f2c8.
This commit is contained in:
parent
1dd43d857d
commit
66a306152d
@ -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);
|
||||
|
@ -34,6 +34,7 @@ import android.graphics.drawable.Drawable;
|
||||
import android.media.AudioManager;
|
||||
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;
|
||||
@ -1339,6 +1340,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)
|
||||
{
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user