Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
7644a86
Add coil-video to support decoding video frames.
colinrtwhite Sep 22, 2019
13379ee
Documentation.
colinrtwhite Sep 23, 2019
d8d932e
Depend on updated Parameters API.
colinrtwhite Sep 26, 2019
60602e7
Add README.
colinrtwhite Oct 7, 2019
27e8608
Refactor extension functions.
colinrtwhite Oct 7, 2019
1724678
Guard.
colinrtwhite Oct 8, 2019
9367f36
Use regular Dokka plugin.
colinrtwhite Oct 12, 2019
38d5770
Update docs and sample.
colinrtwhite Oct 13, 2019
01d725b
Formatting.
colinrtwhite Oct 13, 2019
82b237d
Fix clashing name.
colinrtwhite Oct 13, 2019
e63e9c9
Close the source.
colinrtwhite Oct 13, 2019
a5d16e2
Optimize.
colinrtwhite Oct 13, 2019
92c8a5d
Correctly close the source.
colinrtwhite Oct 13, 2019
c42e457
Optimization.
colinrtwhite Oct 13, 2019
f896b3b
Remove operator.
colinrtwhite Oct 13, 2019
7a4467c
Move FakeBitmapPool out of sharedTest.
colinrtwhite Oct 13, 2019
bfbbbca
Move sharedTest to coil-test module.
colinrtwhite Oct 13, 2019
d370236
Add shell test class.
colinrtwhite Oct 17, 2019
89dfbd7
Add basic tests.
colinrtwhite Oct 18, 2019
f8ee21a
Support allowRgb565.
colinrtwhite Oct 18, 2019
06c563e
Support setting the video frame option.
colinrtwhite Oct 18, 2019
0bacb57
Fix check.
colinrtwhite Oct 18, 2019
202814d
Re-arrange.
colinrtwhite Oct 18, 2019
963bd48
Re-arrange.
colinrtwhite Oct 18, 2019
1f7f31f
WIP BufferedMediaDataSource rework.
colinrtwhite Oct 20, 2019
3062517
Convert VideoFrameDecoder to a Fetcher.
colinrtwhite Jan 15, 2020
9866f2f
Handle asset uris properly.
colinrtwhite Jan 15, 2020
50c0289
Use extension function.
colinrtwhite Jan 15, 2020
75261ff
Fix tests.
colinrtwhite Jan 15, 2020
c7b867b
Documentation.
colinrtwhite Jan 15, 2020
574a1ca
Documentation.
colinrtwhite Jan 15, 2020
9aa1dad
Fix imports.
colinrtwhite Jan 16, 2020
da13b6a
Add to sample.
colinrtwhite Jan 16, 2020
030d8da
WIP
colinrtwhite Jan 22, 2020
d3e3c93
Ensure bitmap attributes.
colinrtwhite Jan 22, 2020
383d1ea
First pass.
colinrtwhite Jan 26, 2020
5f609c3
Fix Dokka.
colinrtwhite Feb 3, 2020
578cded
Docs.
colinrtwhite Feb 3, 2020
8ba4feb
WIP.
colinrtwhite Feb 3, 2020
fbf5efc
Correctly calculate isSampled.
colinrtwhite Feb 3, 2020
1dea0d3
Clean up.
colinrtwhite Feb 3, 2020
b19fb96
Guard against hardware config.
colinrtwhite Feb 3, 2020
7fe9f66
Rename asset.
colinrtwhite Feb 4, 2020
bb28849
Docs.
colinrtwhite Feb 4, 2020
6280971
Rebase fix.
colinrtwhite Feb 7, 2020
731dc1c
Use extension function.
colinrtwhite Feb 7, 2020
aafb4fa
Re-arrange.
colinrtwhite Feb 7, 2020
ec9da75
Re-arrange.
colinrtwhite Feb 7, 2020
09c191c
Fix package.
colinrtwhite Feb 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions buildSrc/src/main/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.kotlin.dsl.kotlin
import org.gradle.kotlin.dsl.project
import kotlin.math.pow

val Project.minSdk: Int
Expand Down Expand Up @@ -59,7 +60,11 @@ fun DependencyHandler.androidTestImplementation(dependencyNotation: Any): Depend
return add("androidTestImplementation", dependencyNotation)
}

fun DependencyHandler.addTestDependencies(kotlinVersion: String) {
fun DependencyHandler.addTestDependencies(kotlinVersion: String, includeTestProject: Boolean = true) {
if (includeTestProject) {
testImplementation(project(":coil-test"))
}

testImplementation(kotlin("test-junit", kotlinVersion))
testImplementation(Library.KOTLINX_COROUTINES_TEST)

Expand All @@ -72,7 +77,11 @@ fun DependencyHandler.addTestDependencies(kotlinVersion: String) {
testImplementation(Library.ROBOLECTRIC)
}

fun DependencyHandler.addAndroidTestDependencies(kotlinVersion: String) {
fun DependencyHandler.addAndroidTestDependencies(kotlinVersion: String, includeTestProject: Boolean = true) {
if (includeTestProject) {
androidTestImplementation(project(":coil-test"))
}

androidTestImplementation(kotlin("test-junit", kotlinVersion))

androidTestImplementation(Library.ANDROIDX_TEST_CORE)
Expand Down
10 changes: 0 additions & 10 deletions coil-base/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ android {
targetSdkVersion(project.targetSdk)
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
sourceSets {
getByName("test").apply {
assets.srcDirs("src/sharedTest/assets")
java.srcDirs("src/sharedTest/java")
}
getByName("androidTest").apply {
assets.srcDirs("src/sharedTest/assets")
java.srcDirs("src/sharedTest/java")
}
}
libraryVariants.all {
generateBuildConfigProvider?.configure { enabled = false }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import coil.util.Utils
import coil.util.createMockWebServer
import coil.util.createOptions
import coil.util.getDrawableCompat
import coil.util.size
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
Expand Down Expand Up @@ -303,7 +304,7 @@ class RealImageLoaderIntegrationTest {

val resultDrawable = result.drawable
assertTrue(resultDrawable is BitmapDrawable)
assertTrue(resultDrawable.bitmap.run { width == size.width && height == size.height })
assertEquals(resultDrawable.bitmap.size, size)
}

@Test
Expand Down Expand Up @@ -387,7 +388,7 @@ class RealImageLoaderIntegrationTest {

val drawable = imageView.drawable
assertTrue(drawable is BitmapDrawable)
assertEquals(expectedSize, drawable.bitmap.run { PixelSize(width, height) })
assertEquals(expectedSize, drawable.bitmap.size)
}

private fun testGet(data: Any, expectedSize: PixelSize = PixelSize(100, 125)) {
Expand All @@ -398,7 +399,7 @@ class RealImageLoaderIntegrationTest {
}

assertTrue(drawable is BitmapDrawable)
assertEquals(expectedSize, drawable.bitmap.run { PixelSize(width, height) })
assertEquals(expectedSize, drawable.bitmap.size)
}

private fun copyNormalImageAssetToCacheDir(): File {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import coil.size.Scale
import coil.size.Size
import coil.util.createOptions
import coil.util.isSimilarTo
import coil.util.size
import kotlinx.coroutines.runBlocking
import okio.buffer
import okio.source
Expand Down Expand Up @@ -49,7 +50,7 @@ class BitmapFactoryDecoderTest {
assertEquals("closed", exception.message)
assertTrue(isSampled)
assertTrue(drawable is BitmapDrawable)
assertEquals(PixelSize(100, 125), drawable.bitmap.run { PixelSize(width, height) })
assertEquals(PixelSize(100, 125), drawable.bitmap.size)
assertEquals(drawable.bitmap.config, Bitmap.Config.ARGB_8888)
}

Expand Down
21 changes: 5 additions & 16 deletions coil-base/src/androidTest/java/coil/fetch/FileFetcherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import android.content.Context
import androidx.test.core.app.ApplicationProvider
import coil.bitmappool.BitmapPool
import coil.size.PixelSize
import coil.util.copyAssetToFile
import coil.util.createOptions
import kotlinx.coroutines.runBlocking
import okio.buffer
import okio.sink
import okio.source
import org.junit.Before
import org.junit.Test
import java.io.File
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
Expand All @@ -32,16 +29,13 @@ class FileFetcherTest {

@Test
fun basic() {
// Copy the asset to filesDir.
val source = context.assets.open("normal.jpg").source()
val file = File(context.filesDir.absolutePath + File.separator + "normal.jpg")
val sink = file.sink().buffer()
source.use { sink.use { sink.writeAll(source) } }
val file = context.copyAssetToFile("normal.jpg")

assertTrue(fetcher.handles(file))

val size = PixelSize(100, 100)
val result = runBlocking {
fetcher.fetch(pool, file, PixelSize(100, 100), createOptions())
fetcher.fetch(pool, file, size, createOptions())
}

assertTrue(result is SourceResult)
Expand All @@ -51,12 +45,7 @@ class FileFetcherTest {

@Test
fun fileCacheKeyWithLastModified() {
val file = File(context.filesDir.absolutePath + File.separator + "file.jpg")

// Copy the asset to filesDir.
val source = context.assets.open("normal.jpg").source()
val sink = file.sink().buffer()
source.use { sink.use { sink.writeAll(source) } }
val file = context.copyAssetToFile("normal.jpg")

file.setLastModified(1234L)
val firstKey = fetcher.key(file)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package coil.transform

import android.content.Context
import android.graphics.BitmapFactory
import androidx.test.core.app.ApplicationProvider
import coil.bitmappool.BitmapPool
import coil.size.OriginalSize
import coil.util.decodeBitmapAsset
import coil.util.isSimilarTo
import kotlinx.coroutines.runBlocking
import org.junit.Before
Expand All @@ -26,8 +26,8 @@ class CircleCropTransformationTest {

@Test
fun basic() {
val input = BitmapFactory.decodeStream(context.assets.open("normal_small.jpg"))
val expected = BitmapFactory.decodeStream(context.assets.open("normal_small_circle.png"))
val input = context.decodeBitmapAsset("normal_small.jpg")
val expected = context.decodeBitmapAsset("normal_small_circle.png")

val actual = runBlocking {
transformation.transform(pool, input, OriginalSize)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package coil.transform

import android.content.Context
import android.graphics.BitmapFactory
import androidx.test.core.app.ApplicationProvider
import coil.bitmappool.BitmapPool
import coil.size.OriginalSize
import coil.util.decodeBitmapAsset
import coil.util.getPixels
import coil.util.isSimilarTo
import kotlinx.coroutines.runBlocking
Expand All @@ -27,8 +27,8 @@ class GrayscaleTransformationTest {

@Test
fun basic() {
val input = BitmapFactory.decodeStream(context.assets.open("normal.jpg"))
val expected = BitmapFactory.decodeStream(context.assets.open("normal_grayscale.jpg"))
val input = context.decodeBitmapAsset("normal.jpg")
val expected = context.decodeBitmapAsset("normal_grayscale.jpg")

val actual = runBlocking {
transformation.transform(pool, input, OriginalSize)
Expand Down
18 changes: 11 additions & 7 deletions coil-base/src/test/java/coil/RealImageLoaderBasicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import coil.size.SizeResolver
import coil.transform.Transformation
import coil.util.createBitmap
import coil.util.createGetRequest
import coil.util.createImageLoader
import coil.util.createLoadRequest
import coil.util.decodeBitmapAsset
import coil.util.error
import coil.util.toDrawable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient
import org.junit.After
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -64,11 +65,14 @@ class RealImageLoaderBasicTest {
bitmapPool = BitmapPool(Long.MAX_VALUE)
referenceCounter = BitmapReferenceCounter(bitmapPool)
memoryCache = MemoryCache(referenceCounter, Int.MAX_VALUE)
imageLoader = createImageLoader(
context = context,
bitmapPool = bitmapPool,
referenceCounter = referenceCounter,
memoryCache = memoryCache
imageLoader = RealImageLoader(
context,
DefaultRequestOptions(),
bitmapPool,
referenceCounter,
memoryCache,
OkHttpClient(),
ComponentRegistry()
)
}

Expand Down Expand Up @@ -483,7 +487,7 @@ class RealImageLoaderBasicTest {
@Suppress("SameParameterValue")
private fun decodeAssetAndAddToMemoryCache(key: String, fileName: String): Bitmap {
val options = BitmapFactory.Options().apply { inPreferredConfig = Bitmap.Config.HARDWARE }
val bitmap = checkNotNull(BitmapFactory.decodeStream(context.assets.open(fileName), null, options))
val bitmap = context.decodeBitmapAsset(fileName, options)
assertEquals(Bitmap.Config.HARDWARE, bitmap.config)
memoryCache.set(key, bitmap, false)
return bitmap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL
import android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
import android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
import android.graphics.Bitmap
import coil.bitmappool.strategy.FakeBitmapPoolStrategy
import coil.util.DEFAULT_BITMAP_SIZE
import coil.util.createBitmap
import org.junit.Before
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package coil.bitmappool
package coil.bitmappool.strategy

import android.graphics.Bitmap
import coil.bitmappool.strategy.BitmapPoolStrategy
import java.util.ArrayDeque

class FakeBitmapPoolStrategy : BitmapPoolStrategy {
Expand All @@ -16,7 +15,7 @@ class FakeBitmapPoolStrategy : BitmapPoolStrategy {
bitmaps += bitmap
}

override operator fun get(width: Int, height: Int, config: Bitmap.Config): Bitmap? {
override fun get(width: Int, height: Int, config: Bitmap.Config): Bitmap? {
return if (bitmaps.isEmpty()) null else bitmaps.removeLast()
}

Expand Down
2 changes: 1 addition & 1 deletion coil-base/src/test/java/coil/lifecycle/FakeLifecycle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FakeLifecycle : Lifecycle() {
}

override fun removeObserver(observer: LifecycleObserver) {
observers.remove(observer)
observers -= observer
}

override fun getCurrentState() = state
Expand Down
2 changes: 1 addition & 1 deletion coil-gif/src/main/java/coil/decode/ImageDecoderDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ImageDecoderDecoder : Decoder {
var isSampled = false

// Work around https://issuetracker.google.com/issues/139371066 by copying the source to a temp file.
source.use { tempFile.sink().use { source.readAll(it) } }
source.use { tempFile.sink().use(source::readAll) }
val decoderSource = ImageDecoder.createSource(tempFile)

val baseDrawable = decoderSource.decodeDrawable { info, _ ->
Expand Down
1 change: 1 addition & 0 deletions coil-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
implementation(project(":coil-default"))
implementation(project(":coil-gif"))
implementation(project(":coil-svg"))
implementation(project(":coil-video"))

implementation(kotlin("stdlib", KotlinCompilerVersion.VERSION))

Expand Down
Binary file added coil-sample/src/main/assets/video.mp4
Binary file not shown.
7 changes: 7 additions & 0 deletions coil-sample/src/main/java/coil/sample/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import coil.ImageLoader
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder
import coil.fetch.VideoFrameFileFetcher
import coil.fetch.VideoFrameUriFetcher
import coil.util.CoilLogger
import okhttp3.Cache
import okhttp3.OkHttpClient
Expand All @@ -28,6 +30,11 @@ class Application : MultiDexApplication() {
availableMemoryPercentage(0.5) // Use 50% of the application's available memory.
crossfade(true) // Show a short crossfade when loading images from network or disk into an ImageView.
componentRegistry {
// Fetchers
add(VideoFrameFileFetcher(applicationContext))
add(VideoFrameUriFetcher(applicationContext))

// Decoders
if (SDK_INT >= P) {
add(ImageDecoderDecoder())
} else {
Expand Down
3 changes: 2 additions & 1 deletion coil-sample/src/main/java/coil/sample/AssetType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ package coil.sample
enum class AssetType(val fileName: String) {
JPG("jpgs.json"),
GIF("gifs.json"),
SVG("svgs.json")
SVG("svgs.json"),
MP4("video.mp4")
}
8 changes: 8 additions & 0 deletions coil-sample/src/main/java/coil/sample/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ package coil.sample

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorInt
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import coil.size.PixelSize
import kotlin.random.Random

inline fun <reified V : View> ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): V {
return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot) as V
Expand All @@ -35,3 +38,8 @@ fun Context.getDisplaySize(): PixelSize {
fun Int.dp(context: Context): Float {
return this * context.resources.displayMetrics.density
}

@ColorInt
fun randomColor(): Int {
return Color.argb(128, Random.nextInt(256), Random.nextInt(256), Random.nextInt(256))
}
6 changes: 4 additions & 2 deletions coil-sample/src/main/java/coil/sample/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package coil.sample

import androidx.annotation.ColorInt
import androidx.annotation.Px
import coil.request.Parameters

data class Image(
val url: String,
val uri: String,
@ColorInt val color: Int,
@Px val width: Int,
@Px val height: Int
@Px val height: Int,
val parameters: Parameters = Parameters.EMPTY
)
5 changes: 3 additions & 2 deletions coil-sample/src/main/java/coil/sample/ImageListAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class ImageListAdapter(
width = columnWidth
}

load(item.url) {
load(item.uri) {
parameters(item.parameters)
placeholder(ColorDrawable(item.color))
}

Expand All @@ -53,7 +54,7 @@ class ImageListAdapter(
}

private object Callback : DiffUtil.ItemCallback<Image>() {
override fun areItemsTheSame(old: Image, new: Image) = old.url == new.url
override fun areItemsTheSame(old: Image, new: Image) = old.uri == new.uri
override fun areContentsTheSame(old: Image, new: Image) = old == new
}
}
4 changes: 3 additions & 1 deletion coil-sample/src/main/java/coil/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class MainActivity : AppCompatActivity() {
is Screen.Detail -> {
list.isVisible = false
detail.isVisible = true
detail.load(screen.image.url)
detail.load(screen.image.uri) {
parameters(screen.image.parameters)
}
}
}
}
Expand Down
Loading