-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move imageChecks.kt out of the internal package
- Loading branch information
Showing
5 changed files
with
77 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 12 additions & 35 deletions
47
...ampling-image/src/main/kotlin/me/saket/telephoto/subsamplingimage/internal/imageChecks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,26 @@ | ||
package me.saket.telephoto.subsamplingimage.internal | ||
|
||
import android.content.Context | ||
import android.util.TypedValue | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import me.saket.telephoto.subsamplingimage.ResourceImageSource | ||
import me.saket.telephoto.subsamplingimage.SubSamplingImageSource | ||
import okio.Buffer | ||
import me.saket.telephoto.subsamplingimage.util.canBeSubSampled as canBeSubSampledV2 | ||
import me.saket.telephoto.subsamplingimage.util.exists as existsV2 | ||
|
||
/** | ||
* Check whether an image source can be sub-sampled and decoded using [AndroidImageRegionDecoder]. | ||
*/ | ||
@Deprecated( | ||
message = "Moved to another package", | ||
replaceWith = ReplaceWith("canBeSubSampled(context)", "me.saket.telephoto.subsamplingimage.util.canBeSubSampled") | ||
) | ||
suspend fun SubSamplingImageSource.canBeSubSampled(context: Context): Boolean { | ||
if (this is ResourceImageSource) { | ||
return !isVectorDrawable(context) | ||
} | ||
|
||
return withContext(Dispatchers.IO) { | ||
peek(context).use { | ||
// Check for GIFs as well because Android's ImageDecoder | ||
// can return a Bitmap for single-frame GIFs. | ||
!isSvg(it) && !isGif(it) | ||
} | ||
} | ||
return canBeSubSampledV2(context) | ||
} | ||
|
||
/** Check whether an image source exists and has non-zero bytes. */ | ||
@Deprecated( | ||
message = "Moved to another package", | ||
replaceWith = ReplaceWith("exists(context)", "me.saket.telephoto.subsamplingimage.util.exists") | ||
) | ||
suspend fun SubSamplingImageSource.exists(context: Context): Boolean { | ||
return withContext(Dispatchers.IO) { | ||
try { | ||
peek(context).read(Buffer(), byteCount = 1) != -1L | ||
} catch (e: okio.FileNotFoundException) { | ||
// This catch block currently makes an assumption that files are only read | ||
// using okio, which is true for SubSamplingImageSource.file(), but might | ||
// fail for SubSamplingImageSource.rawSource(). I could probably make exists() | ||
// a member function of SubSamplingImageSource to fix that. | ||
false | ||
} | ||
} | ||
} | ||
|
||
private suspend fun ResourceImageSource.isVectorDrawable(context: Context): Boolean { | ||
return withContext(Dispatchers.IO) { | ||
TypedValue().apply { | ||
context.resources.getValue(id, this, /* resolveRefs = */ true) | ||
}.string.endsWith(".xml") | ||
} | ||
return existsV2(context) | ||
} |
52 changes: 52 additions & 0 deletions
52
...ub-sampling-image/src/main/kotlin/me/saket/telephoto/subsamplingimage/util/imageChecks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package me.saket.telephoto.subsamplingimage.util | ||
|
||
import android.content.Context | ||
import android.util.TypedValue | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import me.saket.telephoto.subsamplingimage.ResourceImageSource | ||
import me.saket.telephoto.subsamplingimage.SubSamplingImageSource | ||
import me.saket.telephoto.subsamplingimage.internal.AndroidImageRegionDecoder | ||
import me.saket.telephoto.subsamplingimage.internal.isGif | ||
import me.saket.telephoto.subsamplingimage.internal.isSvg | ||
import okio.Buffer | ||
|
||
/** | ||
* Check whether an image source can be sub-sampled and decoded using [AndroidImageRegionDecoder]. | ||
*/ | ||
suspend fun SubSamplingImageSource.canBeSubSampled(context: Context): Boolean { | ||
if (this is ResourceImageSource) { | ||
return !isVectorDrawable(context) | ||
} | ||
|
||
return withContext(Dispatchers.IO) { | ||
peek(context).use { | ||
// Check for GIFs as well because Android's ImageDecoder | ||
// can return a Bitmap for single-frame GIFs. | ||
!isSvg(it) && !isGif(it) | ||
} | ||
} | ||
} | ||
|
||
/** Check whether an image source exists and has non-zero bytes. */ | ||
suspend fun SubSamplingImageSource.exists(context: Context): Boolean { | ||
return withContext(Dispatchers.IO) { | ||
try { | ||
peek(context).read(Buffer(), byteCount = 1) != -1L | ||
} catch (e: okio.FileNotFoundException) { | ||
// This catch block currently makes an assumption that files are only read | ||
// using okio, which is true for SubSamplingImageSource.file(), but might | ||
// fail for SubSamplingImageSource.rawSource(). I could probably make exists() | ||
// a member function of SubSamplingImageSource to fix that. | ||
false | ||
} | ||
} | ||
} | ||
|
||
private suspend fun ResourceImageSource.isVectorDrawable(context: Context): Boolean { | ||
return withContext(Dispatchers.IO) { | ||
TypedValue().apply { | ||
context.resources.getValue(id, this, /* resolveRefs = */ true) | ||
}.string.endsWith(".xml") | ||
} | ||
} |