-
Notifications
You must be signed in to change notification settings - Fork 661
/
ResourceUriMapper.kt
34 lines (28 loc) · 1.21 KB
/
ResourceUriMapper.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
package coil3.map
import android.content.ContentResolver.SCHEME_ANDROID_RESOURCE
import coil3.Uri
import coil3.pathSegments
import coil3.request.Options
import coil3.toUri
/**
* Maps android.resource uris with resource names to uris containing their resources ID. i.e.:
*
* android.resource://example.package.name/drawable/image -> android.resource://example.package.name/12345678
*/
internal class ResourceUriMapper : Mapper<Uri, Uri> {
override fun map(data: Uri, options: Options): Uri? {
if (!isApplicable(data)) return null
val packageName = data.authority.orEmpty()
val resources = options.context.packageManager.getResourcesForApplication(packageName)
val (type, name) = data.pathSegments
//noinspection DiscouragedApi: Necessary to support resource URIs.
val id = resources.getIdentifier(name, type, packageName)
check(id != 0) { "Invalid $SCHEME_ANDROID_RESOURCE URI: $data" }
return "$SCHEME_ANDROID_RESOURCE://$packageName/$id".toUri()
}
private fun isApplicable(data: Uri): Boolean {
return data.scheme == SCHEME_ANDROID_RESOURCE &&
!data.authority.isNullOrBlank() &&
data.pathSegments.count() == 2
}
}