diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/AnalysisHandler.kt b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/AnalysisHandler.kt index f0f020287..e44d0e5b8 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/AnalysisHandler.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/AnalysisHandler.kt @@ -1,6 +1,5 @@ package deckers.thibault.aves.channel.calls -import android.annotation.SuppressLint import android.app.Activity import android.content.ComponentName import android.content.Context @@ -32,7 +31,6 @@ class AnalysisHandler(private val activity: Activity, private val onAnalysisComp } } - @SuppressLint("CommitPrefEdits") private fun registerCallback(call: MethodCall, result: MethodChannel.Result) { val callbackHandle = call.argument("callbackHandle")?.toLong() if (callbackHandle == null) { diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/GeocodingHandler.kt b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/GeocodingHandler.kt index 7fac5c664..6af0642e2 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/GeocodingHandler.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/GeocodingHandler.kt @@ -3,8 +3,8 @@ package deckers.thibault.aves.channel.calls import android.content.Context import android.location.Address import android.location.Geocoder -import android.os.Build import deckers.thibault.aves.channel.calls.Coresult.Companion.safe +import deckers.thibault.aves.utils.getFromLocationCompat import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler @@ -12,8 +12,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch -import java.io.IOException -import java.util.* +import java.util.Locale // as of 2021/03/10, geocoding packages exist but: // - `geocoder` is unmaintained @@ -76,26 +75,9 @@ class GeocodingHandler(private val context: Context) : MethodCallHandler { } } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - geocoder!!.getFromLocation(latitude, longitude, maxResults, object : Geocoder.GeocodeListener { - override fun onGeocode(addresses: List) = processAddresses(addresses.filterNotNull()) - - override fun onError(errorMessage: String?) { - result.error("getAddress-asyncerror", "failed to get address", errorMessage) - } - }) - } else { - try { - @Suppress("deprecation") - val addresses = geocoder!!.getFromLocation(latitude, longitude, maxResults) ?: ArrayList() - processAddresses(addresses) - } catch (e: IOException) { - // `grpc failed`, etc. - result.error("getAddress-network", "failed to get address because of network issues", e.message) - } catch (e: Exception) { - result.error("getAddress-exception", "failed to get address", e.message) - } - } + geocoder!!.getFromLocationCompat( + latitude, longitude, maxResults, ::processAddresses, + ) { code, message, details -> result.error(code, message, details) } } companion object { diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/GlobalSearchHandler.kt b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/GlobalSearchHandler.kt index d0d7767e9..c614ab6ea 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/GlobalSearchHandler.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/GlobalSearchHandler.kt @@ -1,13 +1,15 @@ package deckers.thibault.aves.channel.calls -import android.annotation.SuppressLint import android.content.Context import deckers.thibault.aves.SearchSuggestionsProvider import deckers.thibault.aves.channel.calls.Coresult.Companion.safe import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler -import kotlinx.coroutines.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch class GlobalSearchHandler(private val context: Context) : MethodCallHandler { private val ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) @@ -19,7 +21,6 @@ class GlobalSearchHandler(private val context: Context) : MethodCallHandler { } } - @SuppressLint("CommitPrefEdits") private fun registerCallback(call: MethodCall, result: MethodChannel.Result) { val callbackHandle = call.argument("callbackHandle")?.toLong() if (callbackHandle == null) { diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/fetchers/RegionFetcher.kt b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/fetchers/RegionFetcher.kt index a022bfd8d..60276c136 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/fetchers/RegionFetcher.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/fetchers/RegionFetcher.kt @@ -6,12 +6,12 @@ import android.graphics.BitmapFactory import android.graphics.BitmapRegionDecoder import android.graphics.Rect import android.net.Uri -import android.os.Build import com.bumptech.glide.Glide import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.request.RequestOptions import deckers.thibault.aves.decoder.MultiTrackImage +import deckers.thibault.aves.utils.BitmapRegionDecoderCompat import deckers.thibault.aves.utils.BitmapUtils.getBytes import deckers.thibault.aves.utils.MimeTypes import deckers.thibault.aves.utils.StorageUtils @@ -68,13 +68,7 @@ class RegionFetcher internal constructor( try { if (currentDecoderRef == null) { val newDecoder = StorageUtils.openInputStream(context, uri)?.use { input -> - @Suppress("BlockingMethodInNonBlockingContext") - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - BitmapRegionDecoder.newInstance(input) - } else { - @Suppress("deprecation") - BitmapRegionDecoder.newInstance(input, false) - } + BitmapRegionDecoderCompat.newInstance(input) } if (newDecoder == null) { result.error("getRegion-read-null", "failed to open file for mimeType=$mimeType uri=$uri regionRect=$regionRect", null) diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/utils/Compat.kt b/android/app/src/main/kotlin/deckers/thibault/aves/utils/Compat.kt index 4aa362a1b..fb75f47ff 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/utils/Compat.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/utils/Compat.kt @@ -5,9 +5,14 @@ import android.content.Intent import android.content.pm.ApplicationInfo import android.content.pm.PackageManager import android.content.pm.ResolveInfo +import android.graphics.BitmapRegionDecoder +import android.location.Address +import android.location.Geocoder import android.os.Build import android.os.Parcelable import android.view.Display +import java.io.IOException +import java.io.InputStream inline fun Intent.getParcelableExtraCompat(name: String): T? { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { @@ -44,3 +49,44 @@ fun PackageManager.queryIntentActivitiesCompat(intent: Intent, flags: Int): List queryIntentActivities(intent, flags) } } + +fun Geocoder.getFromLocationCompat( + latitude: Double, + longitude: Double, + maxResults: Int, + processAddresses: (addresses: List
) -> Unit, + onError: (errorCode: String, errorMessage: String?, errorDetails: Any?) -> Unit, +) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + getFromLocation(latitude, longitude, maxResults, object : Geocoder.GeocodeListener { + override fun onGeocode(addresses: List) = processAddresses(addresses.filterNotNull()) + + override fun onError(errorMessage: String?) { + onError("getAddress-asyncerror", "failed to get address", errorMessage) + } + }) + } else { + try { + @Suppress("deprecation") + val addresses = getFromLocation(latitude, longitude, maxResults) ?: ArrayList() + processAddresses(addresses) + } catch (e: IOException) { + // `grpc failed`, etc. + onError("getAddress-network", "failed to get address because of network issues", e.message) + } catch (e: Exception) { + onError("getAddress-exception", "failed to get address", e.message) + } + } +} + +object BitmapRegionDecoderCompat { + @Throws(IOException::class) + fun newInstance(input: InputStream): BitmapRegionDecoder? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + BitmapRegionDecoder.newInstance(input) + } else { + @Suppress("deprecation") + BitmapRegionDecoder.newInstance(input, false) + } + } +}