Skip to content

Commit

Permalink
Fix fetching vector drawables in the Android Studio preview. (#2713)
Browse files Browse the repository at this point in the history
* Fix fetching vector drawables in the Android Studio preview.

* Increase image difference threshold.

* Increase image size.

* Fix order.
  • Loading branch information
colinrtwhite authored Nov 25, 2024
1 parent 70a23cb commit c98ac13
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ internal class ResourceUriFetcher(

override suspend fun fetch(): FetchResult {
// Expected format: android.resource://example.package.name/12345678
val packageName =
data.authority?.takeIf { it.isNotBlank() } ?: throwInvalidUriException(data)
val packageName = data.authority?.takeIf { it.isNotBlank() } ?: throwInvalidUriException(data)
val resId = data.pathSegments.lastOrNull()?.toIntOrNull() ?: throwInvalidUriException(data)

val context = options.context
Expand All @@ -40,8 +39,7 @@ internal class ResourceUriFetcher(
context.packageManager.getResourcesForApplication(packageName)
}
val path = TypedValue().apply { resources.getValue(resId, this, true) }.string
val entryName = path.substring(path.lastIndexOf('/'))
val mimeType = MimeTypeMap.getMimeTypeFromUrl(entryName)
val mimeType = MimeTypeMap.getMimeTypeFromUrl(path.toString())

return if (mimeType == MIME_TYPE_XML) {
// getDrawableCompat can only load resources that are in the current package.
Expand Down
29 changes: 18 additions & 11 deletions coil-core/src/commonMain/kotlin/coil3/util/mimeTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ object MimeTypeMap {
}

val lowerExtension = extension.lowercase()
return extensionFromMimeTypeMap(lowerExtension) ?: mimeTypeData[lowerExtension]
return mimeTypeData[lowerExtension] ?: extensionFromMimeTypeMap(lowerExtension)
}
}

internal expect fun extensionFromMimeTypeMap(extension: String): String?

// https://mimetype.io/all-types
// https://developer.mozilla.org/en-US/docs/Web/HTTP/MIME_types/Common_types
private val mimeTypeData = buildMap {
put("bin", "application/octet-stream")
put("gz", "application/gzip")
put("pdf", "application/pdf")
put("yaml", "application/yaml")

put("avif", "image/avif")
put("avifs", "image/avif")
put("bmp", "image/bmp")
Expand All @@ -48,14 +54,6 @@ private val mimeTypeData = buildMap {
put("jfif", "image/jpeg")
put("jfif-tbnl", "image/jpeg")
put("jif", "image/jpeg")
put("jpe", "image/pjpeg")
put("jpeg", "image/pjpeg")
put("jpg", "image/pjpeg")
put("pjpg", "image/pjpeg")
put("jfi", "image/pjpeg")
put("jfif", "image/pjpeg")
put("jfif-tbnl", "image/pjpeg")
put("jif", "image/pjpeg")
put("png", "image/png")
put("btif", "image/prs.btif")
put("svg", "image/svg+xml")
Expand Down Expand Up @@ -118,6 +116,17 @@ private val mimeTypeData = buildMap {
put("xpm", "image/x-xpixmap")
put("xwd", "image/x-xwindowdump")

put("css", "text/css")
put("csv", "text/csv")
put("htm", "text/html")
put("html", "text/html")
put("ics", "text/calendar")
put("js", "text/javascript")
put("mjs", "text/javascript")
put("md", "text/markdown")
put("txt", "text/plain")
put("xml", "text/xml")

put("3gp", "video/3gpp")
put("3g2", "video/3gpp2")
put("h261", "video/h261")
Expand Down Expand Up @@ -160,6 +169,4 @@ private val mimeTypeData = buildMap {
put("wvx", "video/x-ms-wvx")
put("avi", "video/x-msvideo")
put("movie", "video/x-sgi-movie")

put("pdf", "application/pdf")
}
5 changes: 5 additions & 0 deletions internal/test-compose-screenshot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ androidLibrary(name = "coil3.test.composescreenshot") {
buildFeatures {
compose = true
}
testOptions {
screenshotTests {
imageDifferenceThreshold = 0.01f
}
}
experimentalProperties["android.experimental.enableScreenshotTest"] = true
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF0000"
android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z"/>
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package coil3.composescreenshot

import android.graphics.Color
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
Expand All @@ -18,6 +20,7 @@ import coil3.compose.AsyncImagePreviewHandler
import coil3.compose.LocalAsyncImagePreviewHandler
import coil3.compose.rememberAsyncImagePainter
import coil3.test.FakeImage
import coil3.test.composescreenshot.R

@OptIn(ExperimentalCoilApi::class)
class PreviewScreenshots {
Expand Down Expand Up @@ -66,4 +69,27 @@ class PreviewScreenshots {
)
}
}

/** Regression test: https://github.com/coil-kt/coil/issues/2489 */
@Preview(
device = Devices.PIXEL,
showBackground = true,
)
@Composable
fun vector() {
val context = LocalContext.current
val imageLoader = remember { ImageLoader(context) }

Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxSize(),
) {
AsyncImage(
model = R.drawable.ic_tinted_vector,
contentDescription = null,
imageLoader = imageLoader,
modifier = Modifier.fillMaxSize(0.5f),
)
}
}
}

0 comments on commit c98ac13

Please sign in to comment.