-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
AlbumArtContentProvider.kt
95 lines (77 loc) · 2.98 KB
/
AlbumArtContentProvider.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright 2019 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.uamp.media.library
import android.content.ContentProvider
import android.content.ContentResolver
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import android.os.ParcelFileDescriptor
import com.bumptech.glide.Glide
import java.io.File
import java.io.FileNotFoundException
import java.util.concurrent.TimeUnit
// The amount of time to wait for the album art file to download before timing out.
const val DOWNLOAD_TIMEOUT_SECONDS = 30L
class AlbumArtContentProvider : ContentProvider() {
companion object {
private val uriMap = mutableMapOf<Uri, Uri>()
fun mapUri(uri: Uri): Uri {
val path = uri.encodedPath?.substring(1)?.replace('/', ':') ?: return Uri.EMPTY
val contentUri = Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority("com.example.android.uamp")
.path(path)
.build()
uriMap[contentUri] = uri
return contentUri
}
}
override fun onCreate() = true
override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? {
val context = this.context ?: return null
val remoteUri = uriMap[uri] ?: throw FileNotFoundException(uri.path)
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(DOWNLOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS)
// 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
override fun query(
uri: Uri,
projection: Array<String>?,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?
): Cursor? = null
override fun update(
uri: Uri,
values: ContentValues?,
selection: String?,
selectionArgs: Array<String>?
) = 0
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?) = 0
override fun getType(uri: Uri): String? = null
}