Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions geocoding_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.1.1

- Runs the deprecated pre-API 33 `Geocoder.getFromLocation` and `Geocoder.getFromLocationName` lookups on a background thread instead of the platform main thread, preventing ANRs on Android 12 and lower when the geocoding service is slow.

## 5.1.0

- Updates minimum supported SDK version to Flutter 3.44/Dart 3.12.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,6 @@ abstract class AndroidGeocoderLibraryPigeonProxyApiRegistrar(val binaryMessenger
*/
abstract fun getPigeonApiAddress(): PigeonApiAddress

/**
* An implementation of [PigeonApiGeographicBounds] used to add a new Dart instance of
* `GeographicBounds` to the Dart `InstanceManager`.
*/
abstract fun getPigeonApiGeographicBounds(): PigeonApiGeographicBounds

/**
* An implementation of [PigeonApiBuild] used to add a new Dart instance of
* `Build` to the Dart `InstanceManager`.
Expand All @@ -426,6 +420,12 @@ abstract class AndroidGeocoderLibraryPigeonProxyApiRegistrar(val binaryMessenger
*/
abstract fun getPigeonApiGeocoder(): PigeonApiGeocoder

/**
* An implementation of [PigeonApiGeographicBounds] used to add a new Dart instance of
* `GeographicBounds` to the Dart `InstanceManager`.
*/
abstract fun getPigeonApiGeographicBounds(): PigeonApiGeographicBounds

/**
* An implementation of [PigeonApiLocale] used to add a new Dart instance of
* `Locale` to the Dart `InstanceManager`.
Expand All @@ -435,19 +435,19 @@ abstract class AndroidGeocoderLibraryPigeonProxyApiRegistrar(val binaryMessenger
fun setUp() {
AndroidGeocoderLibraryPigeonInstanceManagerApi.setUpMessageHandlers(binaryMessenger, instanceManager)
PigeonApiAddress.setUpMessageHandlers(binaryMessenger, getPigeonApiAddress())
PigeonApiGeographicBounds.setUpMessageHandlers(binaryMessenger, getPigeonApiGeographicBounds())
PigeonApiBuild.setUpMessageHandlers(binaryMessenger, getPigeonApiBuild())
PigeonApiGeocodeListener.setUpMessageHandlers(binaryMessenger, getPigeonApiGeocodeListener())
PigeonApiGeocoder.setUpMessageHandlers(binaryMessenger, getPigeonApiGeocoder())
PigeonApiGeographicBounds.setUpMessageHandlers(binaryMessenger, getPigeonApiGeographicBounds())
PigeonApiLocale.setUpMessageHandlers(binaryMessenger, getPigeonApiLocale())
}
fun tearDown() {
AndroidGeocoderLibraryPigeonInstanceManagerApi.setUpMessageHandlers(binaryMessenger, null)
PigeonApiAddress.setUpMessageHandlers(binaryMessenger, null)
PigeonApiGeographicBounds.setUpMessageHandlers(binaryMessenger, null)
PigeonApiBuild.setUpMessageHandlers(binaryMessenger, null)
PigeonApiGeocodeListener.setUpMessageHandlers(binaryMessenger, null)
PigeonApiGeocoder.setUpMessageHandlers(binaryMessenger, null)
PigeonApiGeographicBounds.setUpMessageHandlers(binaryMessenger, null)
PigeonApiLocale.setUpMessageHandlers(binaryMessenger, null)
}
}
Expand Down Expand Up @@ -488,13 +488,6 @@ private class AndroidGeocoderLibraryPigeonProxyApiBaseCodec(val registrar: Andro
logNewInstanceFailure("Address", value, it.exceptionOrNull())
}
}
}
else if (value is GeographicBounds) {
registrar.getPigeonApiGeographicBounds().pigeon_newInstance(value) {
if (it.isFailure) {
logNewInstanceFailure("GeographicBounds", value, it.exceptionOrNull())
}
}
}
else if (value is android.os.Build) {
registrar.getPigeonApiBuild().pigeon_newInstance(value) {
Expand All @@ -516,6 +509,13 @@ private class AndroidGeocoderLibraryPigeonProxyApiBaseCodec(val registrar: Andro
logNewInstanceFailure("Geocoder", value, it.exceptionOrNull())
}
}
}
else if (value is GeographicBounds) {
registrar.getPigeonApiGeographicBounds().pigeon_newInstance(value) {
if (it.isFailure) {
logNewInstanceFailure("GeographicBounds", value, it.exceptionOrNull())
}
}
}
else if (value is java.util.Locale) {
registrar.getPigeonApiLocale().pigeon_newInstance(value) {
Expand Down Expand Up @@ -1025,83 +1025,6 @@ abstract class PigeonApiAddress(open val pigeonRegistrar: AndroidGeocoderLibrary
}
}

}
/** Geographical bounds describing a geographic box. */
@Suppress("UNCHECKED_CAST")
abstract class PigeonApiGeographicBounds(open val pigeonRegistrar: AndroidGeocoderLibraryPigeonProxyApiRegistrar) {
abstract fun pigeon_defaultConstructor(lowerLeftLatitude: Double, lowerLeftLongitude: Double, upperRightLatitude: Double, upperRightLongitude: Double): GeographicBounds

abstract fun lowerLeftLatitude(pigeon_instance: GeographicBounds): Double

abstract fun lowerLeftLongitude(pigeon_instance: GeographicBounds): Double

abstract fun upperRightLatitude(pigeon_instance: GeographicBounds): Double

abstract fun upperRightLongitude(pigeon_instance: GeographicBounds): Double

companion object {
@Suppress("LocalVariableName")
fun setUpMessageHandlers(binaryMessenger: BinaryMessenger, api: PigeonApiGeographicBounds?) {
val codec = api?.pigeonRegistrar?.codec ?: AndroidGeocoderLibraryPigeonCodec()
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.geocoding_android.GeographicBounds.pigeon_defaultConstructor", codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val pigeon_identifierArg = args[0] as Long
val lowerLeftLatitudeArg = args[1] as Double
val lowerLeftLongitudeArg = args[2] as Double
val upperRightLatitudeArg = args[3] as Double
val upperRightLongitudeArg = args[4] as Double
val wrapped: List<Any?> = try {
api.pigeonRegistrar.instanceManager.addDartCreatedInstance(api.pigeon_defaultConstructor(lowerLeftLatitudeArg,lowerLeftLongitudeArg,upperRightLatitudeArg,upperRightLongitudeArg), pigeon_identifierArg)
listOf(null)
} catch (exception: Throwable) {
AndroidGeocoderLibraryPigeonUtils.wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
}
}

@Suppress("LocalVariableName", "FunctionName")
/** Creates a Dart instance of GeographicBounds and attaches it to [pigeon_instanceArg]. */
fun pigeon_newInstance(pigeon_instanceArg: GeographicBounds, callback: (Result<Unit>) -> Unit)
{
if (pigeonRegistrar.ignoreCallsToDart) {
callback(
Result.failure(
AndroidGeocoderError("ignore-calls-error", "Calls to Dart are being ignored.", "")))
} else if (pigeonRegistrar.instanceManager.containsInstance(pigeon_instanceArg)) {
callback(Result.success(Unit))
} else {
val pigeon_identifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance(pigeon_instanceArg)
val lowerLeftLatitudeArg = lowerLeftLatitude(pigeon_instanceArg)
val lowerLeftLongitudeArg = lowerLeftLongitude(pigeon_instanceArg)
val upperRightLatitudeArg = upperRightLatitude(pigeon_instanceArg)
val upperRightLongitudeArg = upperRightLongitude(pigeon_instanceArg)
val binaryMessenger = pigeonRegistrar.binaryMessenger
val codec = pigeonRegistrar.codec
val channelName = "dev.flutter.pigeon.geocoding_android.GeographicBounds.pigeon_newInstance"
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
channel.send(listOf(pigeon_identifierArg, lowerLeftLatitudeArg, lowerLeftLongitudeArg, upperRightLatitudeArg, upperRightLongitudeArg)) {
if (it is List<*>) {
if (it.size > 1) {
callback(Result.failure(AndroidGeocoderError(it[0] as String, it[1] as String, it[2] as String?)))
} else {
callback(Result.success(Unit))
}
} else {
callback(Result.failure(AndroidGeocoderLibraryPigeonUtils.createConnectionError(channelName)))
}
}
}
}

}
/** Information about the current Android build, extracted from system properties. */
@Suppress("UNCHECKED_CAST")
Expand Down Expand Up @@ -1313,7 +1236,7 @@ abstract class PigeonApiGeocoder(open val pigeonRegistrar: AndroidGeocoderLibrar
* in blocking the main thread. On Android devices running API 33 or higher
* please use the [Geocoder.getFromLocation] method.
*/
abstract fun getFromLocationPreAndroidApi33(pigeon_instance: android.location.Geocoder, latitude: Double, longitude: Double, maxResults: Long): List<android.location.Address?>?
abstract fun getFromLocationPreAndroidApi33(pigeon_instance: android.location.Geocoder, latitude: Double, longitude: Double, maxResults: Long, callback: (Result<List<android.location.Address?>?>) -> Unit)

/**
* Returns an array of Addresses that attempt to describe the named location,
Expand All @@ -1340,7 +1263,7 @@ abstract class PigeonApiGeocoder(open val pigeonRegistrar: AndroidGeocoderLibrar
* in blocking the main thread. On Android devices running API 33 or higher
* please use the [Geocoder.getFromLocation] method.
*/
abstract fun getFromLocationNamePreAndroidApi33(pigeon_instance: android.location.Geocoder, locationName: String, maxResults: Long, bounds: GeographicBounds?): List<android.location.Address?>?
abstract fun getFromLocationNamePreAndroidApi33(pigeon_instance: android.location.Geocoder, locationName: String, maxResults: Long, bounds: GeographicBounds?, callback: (Result<List<android.location.Address?>?>) -> Unit)

/**
* Returns true if there is a geocoder implementation present on the device
Expand Down Expand Up @@ -1419,12 +1342,15 @@ abstract class PigeonApiGeocoder(open val pigeonRegistrar: AndroidGeocoderLibrar
val latitudeArg = args[1] as Double
val longitudeArg = args[2] as Double
val maxResultsArg = args[3] as Long
val wrapped: List<Any?> = try {
listOf(api.getFromLocationPreAndroidApi33(pigeon_instanceArg, latitudeArg, longitudeArg, maxResultsArg))
} catch (exception: Throwable) {
AndroidGeocoderLibraryPigeonUtils.wrapError(exception)
api.getFromLocationPreAndroidApi33(pigeon_instanceArg, latitudeArg, longitudeArg, maxResultsArg) { result: Result<List<android.location.Address?>?> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(AndroidGeocoderLibraryPigeonUtils.wrapError(error))
} else {
val data = result.getOrNull()
reply.reply(AndroidGeocoderLibraryPigeonUtils.wrapResult(data))
}
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
Expand Down Expand Up @@ -1478,12 +1404,15 @@ abstract class PigeonApiGeocoder(open val pigeonRegistrar: AndroidGeocoderLibrar
val locationNameArg = args[1] as String
val maxResultsArg = args[2] as Long
val boundsArg = args[3] as GeographicBounds?
val wrapped: List<Any?> = try {
listOf(api.getFromLocationNamePreAndroidApi33(pigeon_instanceArg, locationNameArg, maxResultsArg, boundsArg))
} catch (exception: Throwable) {
AndroidGeocoderLibraryPigeonUtils.wrapError(exception)
api.getFromLocationNamePreAndroidApi33(pigeon_instanceArg, locationNameArg, maxResultsArg, boundsArg) { result: Result<List<android.location.Address?>?> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(AndroidGeocoderLibraryPigeonUtils.wrapError(error))
} else {
val data = result.getOrNull()
reply.reply(AndroidGeocoderLibraryPigeonUtils.wrapResult(data))
}
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
Expand Down Expand Up @@ -1537,6 +1466,83 @@ abstract class PigeonApiGeocoder(open val pigeonRegistrar: AndroidGeocoderLibrar
}
}

}
/** Geographical bounds describing a geographic box. */
@Suppress("UNCHECKED_CAST")
abstract class PigeonApiGeographicBounds(open val pigeonRegistrar: AndroidGeocoderLibraryPigeonProxyApiRegistrar) {
abstract fun pigeon_defaultConstructor(lowerLeftLatitude: Double, lowerLeftLongitude: Double, upperRightLatitude: Double, upperRightLongitude: Double): GeographicBounds

abstract fun lowerLeftLatitude(pigeon_instance: GeographicBounds): Double

abstract fun lowerLeftLongitude(pigeon_instance: GeographicBounds): Double

abstract fun upperRightLatitude(pigeon_instance: GeographicBounds): Double

abstract fun upperRightLongitude(pigeon_instance: GeographicBounds): Double

companion object {
@Suppress("LocalVariableName")
fun setUpMessageHandlers(binaryMessenger: BinaryMessenger, api: PigeonApiGeographicBounds?) {
val codec = api?.pigeonRegistrar?.codec ?: AndroidGeocoderLibraryPigeonCodec()
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.geocoding_android.GeographicBounds.pigeon_defaultConstructor", codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val pigeon_identifierArg = args[0] as Long
val lowerLeftLatitudeArg = args[1] as Double
val lowerLeftLongitudeArg = args[2] as Double
val upperRightLatitudeArg = args[3] as Double
val upperRightLongitudeArg = args[4] as Double
val wrapped: List<Any?> = try {
api.pigeonRegistrar.instanceManager.addDartCreatedInstance(api.pigeon_defaultConstructor(lowerLeftLatitudeArg,lowerLeftLongitudeArg,upperRightLatitudeArg,upperRightLongitudeArg), pigeon_identifierArg)
listOf(null)
} catch (exception: Throwable) {
AndroidGeocoderLibraryPigeonUtils.wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
}
}

@Suppress("LocalVariableName", "FunctionName")
/** Creates a Dart instance of GeographicBounds and attaches it to [pigeon_instanceArg]. */
fun pigeon_newInstance(pigeon_instanceArg: GeographicBounds, callback: (Result<Unit>) -> Unit)
{
if (pigeonRegistrar.ignoreCallsToDart) {
callback(
Result.failure(
AndroidGeocoderError("ignore-calls-error", "Calls to Dart are being ignored.", "")))
} else if (pigeonRegistrar.instanceManager.containsInstance(pigeon_instanceArg)) {
callback(Result.success(Unit))
} else {
val pigeon_identifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance(pigeon_instanceArg)
val lowerLeftLatitudeArg = lowerLeftLatitude(pigeon_instanceArg)
val lowerLeftLongitudeArg = lowerLeftLongitude(pigeon_instanceArg)
val upperRightLatitudeArg = upperRightLatitude(pigeon_instanceArg)
val upperRightLongitudeArg = upperRightLongitude(pigeon_instanceArg)
val binaryMessenger = pigeonRegistrar.binaryMessenger
val codec = pigeonRegistrar.codec
val channelName = "dev.flutter.pigeon.geocoding_android.GeographicBounds.pigeon_newInstance"
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
channel.send(listOf(pigeon_identifierArg, lowerLeftLatitudeArg, lowerLeftLongitudeArg, upperRightLatitudeArg, upperRightLongitudeArg)) {
if (it is List<*>) {
if (it.size > 1) {
callback(Result.failure(AndroidGeocoderError(it[0] as String, it[1] as String, it[2] as String?)))
} else {
callback(Result.success(Unit))
}
} else {
callback(Result.failure(AndroidGeocoderLibraryPigeonUtils.createConnectionError(channelName)))
}
}
}
}

}
/**
* A `Locale` object represents a specific geographical, political, or cultural
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.baseflow.geocoding.GeographicBounds
import com.baseflow.geocoding.PigeonApiGeocoder
import com.baseflow.geocoding.ProxyApiRegistrar
import java.util.Locale
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

/**
* ProxyApi implementation for {@link Geocoder}. This class may handle instantiating native
Expand All @@ -18,6 +20,10 @@ import java.util.Locale
*/
class GeocoderProxyApi(pigeonRegistrar: AndroidGeocoderLibraryPigeonProxyApiRegistrar) :
PigeonApiGeocoder(pigeonRegistrar) {
companion object {
private val executor: ExecutorService = Executors.newSingleThreadExecutor()
}

override fun pigeon_defaultConstructor(locale: Locale?): Geocoder {
check(pigeonRegistrar is ProxyApiRegistrar) { "The pigeonRegistrar is not an instance of ProxyApiRegistrar." }

Expand All @@ -43,9 +49,12 @@ class GeocoderProxyApi(pigeonRegistrar: AndroidGeocoderLibraryPigeonProxyApiRegi
pigeon_instance: Geocoder,
latitude: Double,
longitude: Double,
maxResults: Long
): List<Address?>? {
return pigeon_instance.getFromLocation(latitude, longitude, maxResults.toInt());
maxResults: Long,
callback: (Result<List<Address?>?>) -> Unit
) {
runInBackground(callback) {
pigeon_instance.getFromLocation(latitude, longitude, maxResults.toInt())
}
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
Expand All @@ -70,13 +79,33 @@ class GeocoderProxyApi(pigeonRegistrar: AndroidGeocoderLibraryPigeonProxyApiRegi
pigeon_instance: Geocoder,
locationName: String,
maxResults: Long,
bounds: GeographicBounds?
): List<Address?>? {
if (bounds != null) {
return pigeon_instance.getFromLocationName(locationName, maxResults.toInt(), bounds.lowerLeftLatitude, bounds.lowerLeftLongitude, bounds.upperRightLatitude, bounds.upperRightLongitude)
bounds: GeographicBounds?,
callback: (Result<List<Address?>?>) -> Unit
) {
runInBackground(callback) {
if (bounds != null) {
pigeon_instance.getFromLocationName(locationName, maxResults.toInt(), bounds.lowerLeftLatitude, bounds.lowerLeftLongitude, bounds.upperRightLatitude, bounds.upperRightLongitude)
} else {
pigeon_instance.getFromLocationName(locationName, maxResults.toInt())
}
}
}

/**
* Runs a blocking [Geocoder] lookup on a background thread and delivers the result on the main
* thread, so that the pre-API 33 synchronous methods never block the UI thread.
*/
private fun runInBackground(
callback: (Result<List<Address?>?>) -> Unit,
lookup: () -> List<Address?>?
) {
check(pigeonRegistrar is ProxyApiRegistrar) { "The pigeonRegistrar is not an instance of ProxyApiRegistrar." }
val registrar = pigeonRegistrar as ProxyApiRegistrar

return pigeon_instance.getFromLocationName(locationName, maxResults.toInt())
executor.execute {
val result = runCatching(lookup)
registrar.runOnMainThread { callback(result) }
}
}

override fun isPresent(): Boolean {
Expand Down
Loading