Skip to content

Commit

Permalink
Merge pull request #282 from hotwired/accept-jpegs
Browse files Browse the repository at this point in the history
Accept "image/jpeg" mime type for image captures
  • Loading branch information
jayohms committed Aug 29, 2023
2 parents c450cec + 38dd74b commit f2023c7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ internal class TurboCameraCaptureDelegate(val context: Context) {
}

private fun FileChooserParams.allowsCameraCapture(): Boolean {
val accept = defaultAcceptType()
val acceptsAny = accept == "*/*"
val acceptsImages = accept == "image/*" || accept == "image/jpg"
val acceptsImages = defaultAcceptType() == "image/*" ||
acceptTypes.contains("image/jpeg") ||
acceptTypes.contains("image/jpg")

return isCaptureEnabled && (acceptsAny || acceptsImages)
return isCaptureEnabled && (acceptsAny() || acceptsImages)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ internal fun FileChooserParams.allowsMultiple(): Boolean {
return mode == FileChooserParams.MODE_OPEN_MULTIPLE
}

internal fun FileChooserParams.acceptsAny(): Boolean {
return defaultAcceptType() == "*/*"
}

internal fun FileChooserParams.defaultAcceptType(): String {
return when {
acceptTypes.isEmpty() -> "*/*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,43 @@ class TurboCameraCaptureDelegateTest : BaseUnitTest() {
}

@Test
fun buildIntent() {
val intent = delegate.buildIntent(params())
val uri = intent?.getParcelableExtra<Uri>(MediaStore.EXTRA_OUTPUT).toString()
fun buildIntentAcceptTypesValid() {
val acceptTypes = arrayOf(
"*/*",
"image/*",
"image/jpg",
"image/jpeg"
)

assertThat(intent?.action).isEqualTo(MediaStore.ACTION_IMAGE_CAPTURE)
assertThat(uri).startsWith("content://dev.hotwire.turbo.test.turbo.fileprovider/shared")
assertThat(uri).contains("/Capture_")
assertThat(uri).endsWith(".jpg")
}
acceptTypes.forEach {
val intent = delegate.buildIntent(params(acceptTypes = arrayOf(it)))
val uri = intent?.getParcelableExtra<Uri>(MediaStore.EXTRA_OUTPUT).toString()

@Test
fun buildIntentCaptureDisabled() {
val intent = delegate.buildIntent(params(captureEnabled = false))
assertThat(intent).isNull()
assertThat(intent).isNotNull()
assertThat(intent?.action).isEqualTo(MediaStore.ACTION_IMAGE_CAPTURE)
assertThat(uri).startsWith("content://dev.hotwire.turbo.test.turbo.fileprovider/shared")
assertThat(uri).contains("/Capture_")
assertThat(uri).endsWith(".jpg")
}
}

@Test
fun buildIntentAcceptTypeInvalidImage() {
val intent = delegate.buildIntent(params(acceptTypes = arrayOf("image/png")))
assertThat(intent).isNull()
fun buildIntentAcceptTypesInvalid() {
val acceptTypes = arrayOf(
"image/png",
"image/webp",
"video/*"
)

acceptTypes.forEach {
val intent = delegate.buildIntent(params(acceptTypes = arrayOf(it)))
assertThat(intent).isNull()
}
}

@Test
fun buildIntentAcceptTypeInvalid() {
val intent = delegate.buildIntent(params(acceptTypes = arrayOf("video/*")))
fun buildIntentCaptureDisabled() {
val intent = delegate.buildIntent(params(captureEnabled = false))
assertThat(intent).isNull()
}

Expand Down

0 comments on commit f2023c7

Please sign in to comment.