From 4a61d64a4d5f45b47e84f2f92ec115cfec4fa49b Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Thu, 22 Oct 2020 16:05:28 +0900 Subject: [PATCH] minor fixes --- .../{MethodResultWrapper.kt => Coresult.kt} | 19 ------------------- .../aves/channel/calls/StorageHandler.kt | 4 +++- .../channel/streams/ImageByteStreamHandler.kt | 4 +++- .../streams/MediaStoreStreamHandler.kt | 4 +++- .../aves/metadata/ExifInterfaceHelper.kt | 14 ++++++++------ 5 files changed, 17 insertions(+), 28 deletions(-) rename android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/{MethodResultWrapper.kt => Coresult.kt} (54%) diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/MethodResultWrapper.kt b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/Coresult.kt similarity index 54% rename from android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/MethodResultWrapper.kt rename to android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/Coresult.kt index 2603c9cfa..d2db62561 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/MethodResultWrapper.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/Coresult.kt @@ -1,29 +1,10 @@ package deckers.thibault.aves.channel.calls -import android.os.Handler -import android.os.Looper import io.flutter.plugin.common.MethodChannel import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -// ensure `result` methods are called on the main looper thread -class MethodResultWrapper internal constructor(private val methodResult: MethodChannel.Result) : MethodChannel.Result { - private val handler: Handler = Handler(Looper.getMainLooper()) - - override fun success(result: Any?) { - handler.post { methodResult.success(result) } - } - - override fun error(errorCode: String, errorMessage: String?, errorDetails: Any?) { - handler.post { methodResult.error(errorCode, errorMessage, errorDetails) } - } - - override fun notImplemented() { - handler.post { methodResult.notImplemented() } - } -} - // ensure `result` methods are called on the main looper thread class Coresult internal constructor(private val methodResult: MethodChannel.Result) : MethodChannel.Result { private val mainScope = CoroutineScope(Dispatchers.Main) diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/StorageHandler.kt b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/StorageHandler.kt index cca84b5dc..5115f7c70 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/StorageHandler.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/StorageHandler.kt @@ -11,6 +11,8 @@ import deckers.thibault.aves.utils.StorageUtils.getVolumePaths import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import java.io.File import java.util.* @@ -29,7 +31,7 @@ class StorageHandler(private val context: Context) : MethodCallHandler { "getGrantedDirectories" -> result.success(ArrayList(PermissionManager.getGrantedDirs(context))) "getInaccessibleDirectories" -> getInaccessibleDirectories(call, result) "revokeDirectoryAccess" -> revokeDirectoryAccess(call, result) - "scanFile" -> scanFile(call, MethodResultWrapper(result)) + "scanFile" -> GlobalScope.launch { scanFile(call, Coresult(result)) } else -> result.notImplemented() } } diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/channel/streams/ImageByteStreamHandler.kt b/android/app/src/main/kotlin/deckers/thibault/aves/channel/streams/ImageByteStreamHandler.kt index 4b6fe28e0..1a4da7f1c 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/channel/streams/ImageByteStreamHandler.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/channel/streams/ImageByteStreamHandler.kt @@ -18,6 +18,8 @@ import deckers.thibault.aves.utils.MimeTypes.needRotationAfterGlide import deckers.thibault.aves.utils.StorageUtils.openInputStream import io.flutter.plugin.common.EventChannel import io.flutter.plugin.common.EventChannel.EventSink +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import java.io.ByteArrayOutputStream import java.io.IOException import java.io.InputStream @@ -30,7 +32,7 @@ class ImageByteStreamHandler(private val activity: Activity, private val argumen this.eventSink = eventSink handler = Handler(Looper.getMainLooper()) - Thread { streamImage() }.start() + GlobalScope.launch { streamImage() } } override fun onCancel(o: Any) {} diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/channel/streams/MediaStoreStreamHandler.kt b/android/app/src/main/kotlin/deckers/thibault/aves/channel/streams/MediaStoreStreamHandler.kt index 0f6331f9b..62e542d9b 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/channel/streams/MediaStoreStreamHandler.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/channel/streams/MediaStoreStreamHandler.kt @@ -7,6 +7,8 @@ import deckers.thibault.aves.model.provider.FieldMap import deckers.thibault.aves.model.provider.MediaStoreImageProvider import io.flutter.plugin.common.EventChannel import io.flutter.plugin.common.EventChannel.EventSink +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch class MediaStoreStreamHandler(private val context: Context, arguments: Any?) : EventChannel.StreamHandler { private lateinit var eventSink: EventSink @@ -25,7 +27,7 @@ class MediaStoreStreamHandler(private val context: Context, arguments: Any?) : E this.eventSink = eventSink handler = Handler(Looper.getMainLooper()) - Thread { fetchAll() }.start() + GlobalScope.launch { fetchAll() } } override fun onCancel(arguments: Any?) {} diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/metadata/ExifInterfaceHelper.kt b/android/app/src/main/kotlin/deckers/thibault/aves/metadata/ExifInterfaceHelper.kt index b1abc91e0..2e324310a 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/metadata/ExifInterfaceHelper.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/metadata/ExifInterfaceHelper.kt @@ -25,6 +25,8 @@ object ExifInterfaceHelper { ExifInterface.TAG_ORIENTATION, ) + private fun isNeverNull(tag: String): Boolean = neverNullTags.contains(tag) + private val baseTags: Map = mapOf( ExifInterface.TAG_APERTURE_VALUE to TagMapper(ExifDirectoryBase.TAG_APERTURE, DirType.EXIF_IFD0, TagFormat.RATIONAL), ExifInterface.TAG_ARTIST to TagMapper(ExifDirectoryBase.TAG_ARTIST, DirType.EXIF_IFD0, TagFormat.ASCII), @@ -32,9 +34,9 @@ object ExifInterfaceHelper { ExifInterface.TAG_BODY_SERIAL_NUMBER to TagMapper(ExifDirectoryBase.TAG_BODY_SERIAL_NUMBER, DirType.EXIF_IFD0, TagFormat.ASCII), ExifInterface.TAG_BRIGHTNESS_VALUE to TagMapper(ExifDirectoryBase.TAG_BRIGHTNESS_VALUE, DirType.EXIF_IFD0, TagFormat.RATIONAL), ExifInterface.TAG_CAMERA_OWNER_NAME to TagMapper(ExifDirectoryBase.TAG_CAMERA_OWNER_NAME, DirType.EXIF_IFD0, TagFormat.ASCII), - ExifInterface.TAG_CFA_PATTERN to TagMapper(ExifDirectoryBase.TAG_CFA_PATTERN, DirType.EXIF_IFD0, TagFormat.UNDEFINED), + ExifInterface.TAG_CFA_PATTERN to TagMapper(ExifDirectoryBase.TAG_CFA_PATTERN, DirType.EXIF_IFD0, TagFormat.BYTE), // spec format: UNDEFINED, e.g. [Red,Green][Green,Blue] ExifInterface.TAG_COLOR_SPACE to TagMapper(ExifDirectoryBase.TAG_COLOR_SPACE, DirType.EXIF_IFD0, TagFormat.SHORT), - ExifInterface.TAG_COMPONENTS_CONFIGURATION to TagMapper(ExifDirectoryBase.TAG_COMPONENTS_CONFIGURATION, DirType.EXIF_IFD0, TagFormat.UNDEFINED), + ExifInterface.TAG_COMPONENTS_CONFIGURATION to TagMapper(ExifDirectoryBase.TAG_COMPONENTS_CONFIGURATION, DirType.EXIF_IFD0, TagFormat.BYTE), // spec format: UNDEFINED, e.g. [Y,Cb,Cr] ExifInterface.TAG_COMPRESSED_BITS_PER_PIXEL to TagMapper(ExifDirectoryBase.TAG_COMPRESSED_AVERAGE_BITS_PER_PIXEL, DirType.EXIF_IFD0, TagFormat.RATIONAL), ExifInterface.TAG_COMPRESSION to TagMapper(ExifDirectoryBase.TAG_COMPRESSION, DirType.EXIF_IFD0, TagFormat.SHORT), ExifInterface.TAG_CONTRAST to TagMapper(ExifDirectoryBase.TAG_CONTRAST, DirType.EXIF_IFD0, TagFormat.SHORT), @@ -51,7 +53,7 @@ object ExifInterfaceHelper { ExifInterface.TAG_EXPOSURE_MODE to TagMapper(ExifDirectoryBase.TAG_EXPOSURE_MODE, DirType.EXIF_IFD0, TagFormat.SHORT), ExifInterface.TAG_EXPOSURE_PROGRAM to TagMapper(ExifDirectoryBase.TAG_EXPOSURE_PROGRAM, DirType.EXIF_IFD0, TagFormat.SHORT), ExifInterface.TAG_EXPOSURE_TIME to TagMapper(ExifDirectoryBase.TAG_EXPOSURE_TIME, DirType.EXIF_IFD0, TagFormat.RATIONAL), - ExifInterface.TAG_FILE_SOURCE to TagMapper(ExifDirectoryBase.TAG_FILE_SOURCE, DirType.EXIF_IFD0, TagFormat.UNDEFINED), + ExifInterface.TAG_FILE_SOURCE to TagMapper(ExifDirectoryBase.TAG_FILE_SOURCE, DirType.EXIF_IFD0, TagFormat.SHORT), // spec format: UNDEFINED ExifInterface.TAG_FLASH to TagMapper(ExifDirectoryBase.TAG_FLASH, DirType.EXIF_IFD0, TagFormat.SHORT), ExifInterface.TAG_FLASHPIX_VERSION to TagMapper(ExifDirectoryBase.TAG_FLASHPIX_VERSION, DirType.EXIF_IFD0, TagFormat.UNDEFINED), ExifInterface.TAG_FLASH_ENERGY to TagMapper(ExifDirectoryBase.TAG_FLASH_ENERGY, DirType.EXIF_IFD0, TagFormat.RATIONAL), @@ -101,7 +103,7 @@ object ExifInterfaceHelper { ExifInterface.TAG_SAMPLES_PER_PIXEL to TagMapper(ExifDirectoryBase.TAG_SAMPLES_PER_PIXEL, DirType.EXIF_IFD0, TagFormat.SHORT), ExifInterface.TAG_SATURATION to TagMapper(ExifDirectoryBase.TAG_SATURATION, DirType.EXIF_IFD0, TagFormat.SHORT), ExifInterface.TAG_SCENE_CAPTURE_TYPE to TagMapper(ExifDirectoryBase.TAG_SCENE_CAPTURE_TYPE, DirType.EXIF_IFD0, TagFormat.SHORT), - ExifInterface.TAG_SCENE_TYPE to TagMapper(ExifDirectoryBase.TAG_SCENE_TYPE, DirType.EXIF_IFD0, TagFormat.UNDEFINED), + ExifInterface.TAG_SCENE_TYPE to TagMapper(ExifDirectoryBase.TAG_SCENE_TYPE, DirType.EXIF_IFD0, TagFormat.SHORT), // spec format: UNDEFINED ExifInterface.TAG_SENSING_METHOD to TagMapper(ExifDirectoryBase.TAG_SENSING_METHOD, DirType.EXIF_IFD0, TagFormat.SHORT), ExifInterface.TAG_SENSITIVITY_TYPE to TagMapper(ExifDirectoryBase.TAG_SENSITIVITY_TYPE, DirType.EXIF_IFD0, TagFormat.SHORT), ExifInterface.TAG_SHARPNESS to TagMapper(ExifDirectoryBase.TAG_SHARPNESS, DirType.EXIF_IFD0, TagFormat.SHORT), @@ -229,7 +231,7 @@ object ExifInterfaceHelper { for ((exifInterfaceTag, mapper) in tags) { if (exif.hasAttribute(exifInterfaceTag)) { val value: String? = exif.getAttribute(exifInterfaceTag) - if (value != null && (value != "0" || !neverNullTags.contains(exifInterfaceTag))) { + if (value != null && !(value == "0" && isNeverNull(exifInterfaceTag))) { if (mapper != null) { val dir = metadataExtractorDirs[mapper.dirType] ?: error("Directory type ${mapper.dirType} does not have a matching Directory instance") val type = mapper.type @@ -258,7 +260,7 @@ object ExifInterfaceHelper { if (value != null && (value != "0" || !neverNullTags.contains(exifInterfaceTag))) { val obj: Any? = when (mapper.format) { TagFormat.ASCII, TagFormat.COMMENT, TagFormat.UNDEFINED -> value - TagFormat.BYTE -> value.toByteArray() + TagFormat.BYTE -> exif.getAttributeBytes(exifInterfaceTag) TagFormat.SHORT -> value.toShortOrNull() TagFormat.LONG -> value.toLongOrNull() TagFormat.RATIONAL -> toRational(value)