From 7ee944ad88ea5bd25890b545320158434950b508 Mon Sep 17 00:00:00 2001 From: OS-pedrogustavobilro Date: Wed, 18 Nov 2026 15:31:54 +0000 Subject: [PATCH] feat: Use compose version of Location Button --- CHANGELOG.md | 6 + README.md | 22 ++- build.gradle | 11 +- .../view/IONGLOCLocationButtonIsland.kt | 149 ++++++++++-------- .../view/IONGLOCLocationButtonRegistry.kt | 4 +- 5 files changed, 117 insertions(+), 75 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6f6023..f665499 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Feature: Depend on `androidx.core.locationbutton:locationbutton:1.0.0-alpha01`, which is still an alpha release (the latest published), so its API may change incompatibly. The `android.permission.USE_LOCATION_BUTTON` permission is now added to the merged manifest. - Chore: Update build tooling (Gradle 9.4.1, AGP 9.2.1, nexus publish plugin 2.0.0, SonarQube plugin 7.6.0). The publishing script no longer uses the `-b` Gradle flag, which Gradle 9 removed. +### 2026-11-18 + +- Fix: The Location Button is now the Jetpack Compose `LocationButton` (`androidx.core.locationbutton:locationbutton-compose:1.0.0-alpha01`, still an alpha release), hosted in a `ComposeView`. The Views `LocationButton` could show Google's compat button next to the system button on Android 17 ([issue tracker](https://issuetracker.google.com/issues/567953859)). The `androidx.core.locationbutton:locationbutton` dependency is no longer declared directly. +- BREAKING CHANGE: The host Activity must be a `ComponentActivity` (`AppCompatActivity` is one), as the Compose button finds its Activity and permission launcher through it. `IONGLOCLocationButtonIsland` no longer takes a `Context` in its constructor. +- Fix: The Location Button's layout listener is now removed when the island is destroyed. + ## [2.2.2] ### 2026-08-04 diff --git a/README.md b/README.md index 2bdd4fd..470c283 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,11 @@ In your app-level gradle file, import the `ion-android-geolocation` library like `IONGLOCLocationButtonRegistry.register(controller, ...)` registers the `os.locationButton` native component. The host plugin exposes the corresponding web -component. The permission flow is handled by the `androidx.core.locationbutton` -`LocationButton` itself: the system button on Android 17+, and the library's own locally -rendered button with its default permission flow on older versions (the host Activity must -be an `ActivityResultRegistryOwner`, which `AppCompatActivity` is). +component. The button is the Jetpack Compose `LocationButton` from +`androidx.core.locationbutton:locationbutton-compose`, hosted in a `ComposeView`. The permission +flow is handled by that `LocationButton` itself: the system button on Android 17+, and a locally +rendered button with its default permission flow on older versions (see the Activity requirement +below). The component emits `grant`, `position`, and `buttonError` events. @@ -55,10 +56,15 @@ Since version 3.0.0, the library ships as a single AAR that includes the Locatio - Android Gradle Plugin 9.1 or higher and Kotlin 2.1 or higher in the consuming build, because `androidx.core.locationbutton` requires them. - `minSdk` 26 or higher. - -`androidx.core.locationbutton:locationbutton` is still an **alpha** release (`1.0.0-alpha01`, the -latest published at the time of writing) and its API may change incompatibly. The version is pinned, -not a range. +- The host Activity must be a `ComponentActivity` (`AppCompatActivity` is one), because the + Compose button finds its Activity and the permission launcher (an `ActivityResultRegistryOwner`) + through it. + +`androidx.core.locationbutton:locationbutton-compose` is still an **alpha** release +(`1.0.0-alpha01`, the latest published at the time of writing) and its API may change +incompatibly. The version is pinned, not a range. Expect +roughly 1 MB more in an APK built with R8, and several MB more without it. The `core.locationbutton` library should +be reviewed regularly and updated when newer (more stable) ones are published. ## Methods diff --git a/build.gradle b/build.gradle index ae1975a..c29bcfc 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,7 @@ buildscript { classpath("io.github.gradle-nexus:publish-plugin:2.0.0") } classpath 'com.android.tools.build:gradle:9.2.1' + classpath 'org.jetbrains.kotlin:compose-compiler-gradle-plugin:2.2.10' classpath "org.jacoco:org.jacoco.core:$jacocoVersion" } } @@ -35,11 +36,16 @@ if (System.getenv("SHOULD_PUBLISH") == "true") { apply plugin: "com.android.library" apply plugin: "jacoco" +apply plugin: "org.jetbrains.kotlin.plugin.compose" android { namespace "io.ionic.libs.iongeolocationlib" compileSdk 37 + buildFeatures { + compose true + } + defaultConfig { minSdk 26 targetSdk 37 @@ -111,7 +117,10 @@ repositories { dependencies { implementation 'androidx.core:core-ktx:1.16.0' - implementation 'androidx.core.locationbutton:locationbutton:1.0.0-alpha01' + // Using compose androidx library instead of views because Views has UI issues + // See https://issuetracker.google.com/issues/567953859 + implementation 'androidx.core.locationbutton:locationbutton-compose:1.0.0-alpha01' + implementation 'androidx.compose.foundation:foundation:1.12.1' if (System.getenv("SHOULD_PUBLISH") == "true") { api 'io.ionic.libs:ionnativeislands-android:1.0.0' } else { diff --git a/src/main/kotlin/io/ionic/libs/iongeolocationlib/view/IONGLOCLocationButtonIsland.kt b/src/main/kotlin/io/ionic/libs/iongeolocationlib/view/IONGLOCLocationButtonIsland.kt index 831d41c..a5a23ea 100644 --- a/src/main/kotlin/io/ionic/libs/iongeolocationlib/view/IONGLOCLocationButtonIsland.kt +++ b/src/main/kotlin/io/ionic/libs/iongeolocationlib/view/IONGLOCLocationButtonIsland.kt @@ -1,6 +1,5 @@ package io.ionic.libs.iongeolocationlib.view -import android.content.Context import android.app.Activity import android.graphics.Color import android.graphics.Rect @@ -8,10 +7,20 @@ import android.os.Build import android.view.SurfaceView import android.view.View import android.view.ViewGroup +import android.view.ViewTreeObserver import android.widget.FrameLayout import androidx.annotation.ChecksSdkIntAtLeast +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color as ComposeColor +import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.unit.dp import androidx.core.graphics.toColorInt -import androidx.core.locationbutton.LocationButton +import androidx.core.locationbutton.compose.LocationButton +import androidx.core.locationbutton.compose.LocationButtonTextType import io.ionic.libs.iongeolocationlib.controller.IONGLOCController import io.ionic.libs.iongeolocationlib.model.IONGLOCException import io.ionic.libs.iongeolocationlib.model.IONGLOCLocationResult @@ -20,8 +29,7 @@ import io.ionic.libs.ionnativeislandslib.NativeIslandEventEmitting import io.ionic.libs.ionnativeislandslib.NativeIslandSurfaceClip class IONGLOCLocationButtonIsland( - private val context: Context, - activity: Activity, + private val activity: Activity, controller: IONGLOCController, errorCodeMapper: ((Throwable) -> String?)? = null, positionMapper: ((IONGLOCLocationResult) -> Map)? = null, @@ -35,7 +43,6 @@ class IONGLOCLocationButtonIsland( override var eventSink: ((String, Map) -> Unit)? = null - private val density = context.resources.displayMetrics.density private val delegate = IONGLOCLocationButtonDelegate( activity, controller, @@ -45,34 +52,48 @@ class IONGLOCLocationButtonIsland( eventSink?.invoke(name, payload) } - private var textType = "precise-location" - private var backgroundColor = Color.rgb(11, 87, 208) - private var textColor = Color.WHITE - private var iconTint = Color.WHITE - private var strokeColor = Color.BLACK - private var cornerRadius = 16f * density - private var strokeWidth = 0f + // Sizes are in dp, which equals CSS px: the runtime lays the host out from CSS px. + private data class ButtonConfig( + val textType: String = "precise-location", + val backgroundColor: Int = Color.rgb(11, 87, 208), + val textColor: Int = Color.WHITE, + val iconTint: Int = Color.WHITE, + val strokeColor: Int = Color.BLACK, + val cornerRadius: Float = 16f, + val strokeWidth: Float = 0f, + ) + + private var config by mutableStateOf(ButtonConfig()) private var visibleRect: Rect? = null - - private val button = LocationButton(context).apply { - id = View.generateViewId() - parentActivity = activity - minimumWidth = minimumTouchTarget - minimumHeight = minimumTouchTarget - setOnPermissionResultListener { granted -> - delegate.permissionGranted(granted) - } - setOnErrorListener { error -> - delegate.emitButtonError( - IONGLOCException.IONGLOCLocationButtonException( - error.message ?: "location button session failed", - error, - ), + private var clipObserver: ViewTreeObserver.OnGlobalLayoutListener? = null + + private val button = ComposeView(activity).apply { + setContent { + val current = config + LocationButton( + modifier = Modifier.fillMaxSize(), + backgroundColor = ComposeColor(current.backgroundColor), + strokeColor = ComposeColor(current.strokeColor), + strokeWidth = current.strokeWidth.dp, + cornerRadius = current.cornerRadius.dp, + pressedCornerRadius = current.cornerRadius.dp, + iconTint = ComposeColor(current.iconTint), + textType = LOCATION_BUTTON_TEXT_TYPES.getValue(current.textType), + textColor = ComposeColor(current.textColor), + onError = { error -> + delegate.emitButtonError( + IONGLOCException.IONGLOCLocationButtonException( + error.message ?: "location button session failed", + error, + ), + ) + }, + onPermissionResult = { granted -> delegate.permissionGranted(granted) }, ) } } - private val buttonHost = object : FrameLayout(context) { + private val buttonHost = object : FrameLayout(activity) { private var attachPending = false override fun onAttachedToWindow() { @@ -120,17 +141,15 @@ class IONGLOCLocationButtonIsland( LayoutParams.MATCH_PARENT, ), ) - button.viewTreeObserver.addOnGlobalLayoutListener { applySurfaceClip() } + // The SurfaceView is created by the composition, after the first layout. + clipObserver = ViewTreeObserver.OnGlobalLayoutListener { applySurfaceClip() } + .also { button.viewTreeObserver.addOnGlobalLayoutListener(it) } applySurfaceClip() } } } } - init { - renderButton() - } - override val view: View get() = buttonHost override fun setVisibleRect(rect: Rect?) { @@ -141,7 +160,7 @@ class IONGLOCLocationButtonIsland( private fun applySurfaceClip() { if (button.clipBounds != visibleRect) button.clipBounds = visibleRect - val surface = (button as ViewGroup).getChildAt(0) as? SurfaceView + val surface = button.findSurfaceView() if (surface != null && surface.clipBounds != visibleRect) surface.clipBounds = visibleRect } @@ -155,9 +174,11 @@ class IONGLOCLocationButtonIsland( override fun onDestroy() { delegate.onDestroy() - button.setOnPermissionResultListener(null) - button.setOnErrorListener(null) - button.parentActivity = null + clipObserver?.let { observer -> + if (button.viewTreeObserver.isAlive) button.viewTreeObserver.removeOnGlobalLayoutListener(observer) + } + clipObserver = null + button.disposeComposition() buttonHost.removeAllViews() } @@ -166,29 +187,20 @@ class IONGLOCLocationButtonIsland( require(it in LOCATION_BUTTON_TEXT_TYPES) { "unsupported Location Button text type" } - textType = it } - params.color("backgroundColor")?.let { backgroundColor = it } - params.color("textColor")?.let { textColor = it } - params.color("iconTint")?.let { iconTint = it } - params.color("strokeColor")?.let { strokeColor = it } - params.dimension("cornerRadius", 0.0, 68.0)?.let { cornerRadius = it } - params.dimension("strokeWidth", 0.0, 3.0)?.let { strokeWidth = it } + // Validate everything before applying, so an invalid update changes nothing. + config = config.copy( + textType = params.string("textType") ?: config.textType, + backgroundColor = params.color("backgroundColor") ?: config.backgroundColor, + textColor = params.color("textColor") ?: config.textColor, + iconTint = params.color("iconTint") ?: config.iconTint, + strokeColor = params.color("strokeColor") ?: config.strokeColor, + cornerRadius = params.dimension("cornerRadius", 0.0, 68.0) ?: config.cornerRadius, + strokeWidth = params.dimension("strokeWidth", 0.0, 3.0) ?: config.strokeWidth, + ) params.long("timeout", minimum = 1)?.let { delegate.timeout = it } params.long("maximumAge", minimum = 0)?.let { delegate.maximumAge = it } params.boolean("enableLocationFallback")?.let { delegate.enableLocationManagerFallback = it } - renderButton() - } - - private fun renderButton() { - button.setTextType(LOCATION_BUTTON_TEXT_TYPES.getValue(textType)) - button.setBackgroundColor(backgroundColor) - button.setTextColor(textColor) - button.setIconTint(iconTint) - button.setStrokeColor(strokeColor) - button.setCornerRadius(cornerRadius) - button.setPressedCornerRadius(cornerRadius) - button.setStrokeWidth(strokeWidth.toInt()) } private fun Map.string(name: String): String? { @@ -214,19 +226,28 @@ class IONGLOCLocationButtonIsland( require(cssPixels.isFinite() && cssPixels in minimum..maximum) { "$name must be between $minimum and $maximum CSS pixels" } - return (cssPixels * density).toFloat() + return cssPixels.toFloat() } - private val minimumTouchTarget: Int get() = (48f * density).toInt() + private val minimumTouchTarget: Int + get() = (48f * button.resources.displayMetrics.density).toInt() + + private fun View.findSurfaceView(): SurfaceView? { + if (this is SurfaceView) return this + if (this is ViewGroup) { + for (i in 0 until childCount) getChildAt(i).findSurfaceView()?.let { return it } + } + return null + } } private val LOCATION_BUTTON_TEXT_TYPES = mapOf( - "precise-location" to LocationButton.TEXT_TYPE_PRECISE_LOCATION, - "use-precise-location" to LocationButton.TEXT_TYPE_USE_PRECISE_LOCATION, - "share-precise-location" to LocationButton.TEXT_TYPE_SHARE_PRECISE_LOCATION, - "near-my-precise-location" to LocationButton.TEXT_TYPE_NEAR_MY_PRECISE_LOCATION, - "near-your-precise-location" to LocationButton.TEXT_TYPE_NEAR_YOUR_PRECISE_LOCATION, - "none" to LocationButton.TEXT_TYPE_NONE, + "precise-location" to LocationButtonTextType.PreciseLocation, + "use-precise-location" to LocationButtonTextType.UsePreciseLocation, + "share-precise-location" to LocationButtonTextType.SharePreciseLocation, + "near-my-precise-location" to LocationButtonTextType.NearMyPreciseLocation, + "near-your-precise-location" to LocationButtonTextType.NearYourPreciseLocation, + "none" to LocationButtonTextType.None, ) private val HEX_COLOR = Regex("^#[0-9A-Fa-f]{6}$") diff --git a/src/main/kotlin/io/ionic/libs/iongeolocationlib/view/IONGLOCLocationButtonRegistry.kt b/src/main/kotlin/io/ionic/libs/iongeolocationlib/view/IONGLOCLocationButtonRegistry.kt index 55c819a..dff7569 100644 --- a/src/main/kotlin/io/ionic/libs/iongeolocationlib/view/IONGLOCLocationButtonRegistry.kt +++ b/src/main/kotlin/io/ionic/libs/iongeolocationlib/view/IONGLOCLocationButtonRegistry.kt @@ -17,8 +17,8 @@ object IONGLOCLocationButtonRegistry { componentName = "os.locationButton", accessibility = NativeIslandAccessibility.NATIVE, requiresUnobscuredSurface = requiresUnobscuredSurface(), - factory = { context, activity -> - IONGLOCLocationButtonIsland(context, activity, controller, errorCodeMapper, positionMapper) + factory = { _, activity -> + IONGLOCLocationButtonIsland(activity, controller, errorCodeMapper, positionMapper) }, ) }