Skip to content

Commit

Permalink
Make ContentProvider block while downloading album art. Add placehold…
Browse files Browse the repository at this point in the history
…er image when downloading
  • Loading branch information
dturner committed Feb 9, 2021
1 parent 1982032 commit f488c5f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class MediaItemAdapter(

Glide.with(holder.albumArt)
.load(mediaItem.albumArtUri)
.placeholder(R.drawable.default_art)
.into(holder.albumArt)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import android.os.ParcelFileDescriptor
import android.util.Log
import com.bumptech.glide.Glide
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File
import java.io.FileNotFoundException

Expand All @@ -53,28 +49,22 @@ class AlbumArtContentProvider : ContentProvider() {
val context = this.context ?: return null
val remoteUri = uriMap[uri] ?: throw FileNotFoundException(uri.path)

val file = File(context.cacheDir, uri.path)
return if (!file.exists()) {
GlobalScope.launch(Dispatchers.IO) {
// Use Glide to download the album art
val cacheFile = Glide.with(context)
.asFile()
.load(remoteUri)
.submit()
.get()

// Rename the file Glide created to match our own scheme.
cacheFile.renameTo(file)

// Notify the caller that the artwork has been updated.
context.contentResolver.notifyChange(uri, null)
}

// Provide placeholder art until the correct art can be downloaded.
context.assets.openFd("default_art.png").parcelFileDescriptor
} else {
ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
var file = File(context.cacheDir, uri.path)

if (!file.exists()) {
// Use Glide to download the album art.
val cacheFile = Glide.with(context)
.asFile()
.load(remoteUri)
.submit()
.get()

// Rename the file Glide created to match our own scheme.
cacheFile.renameTo(file)

file = cacheFile
}
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
}

override fun insert(uri: Uri, values: ContentValues?): Uri? = null
Expand Down

0 comments on commit f488c5f

Please sign in to comment.