Skip to content
Merged
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
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,12 @@ os-location-button {
border-color: #000000;
border-width: 1px;
border-radius: 16px;
--os-location-button-pressed-corner-radius: 12px;
--os-location-button-clickable-padding: 6px;
z-index: 2;
}
```

The native button supports opaque hexadecimal and RGB colors. Border width supports
`0px` through `3px`; both corner radii support `0px` through `68px`. Clickable
padding is clamped to the Android-supported range of `4px` through `8px`.
`0px` through `3px`; the corner radius supports `0px` through `68px`.

## API

Expand Down Expand Up @@ -290,3 +287,4 @@ The following table list all the plugin errors:
| OS-PLUG-GLOC-0016 | Android | Location settings error. |
| OS-PLUG-GLOC-0017 | Android | Unable to retrieve location because device has both Network and Location turned off. |
| OS-PLUG-GLOC-0018 | Android | Location permissions are not declared in manifest. Make sure at least ACCESS_COARSE_LOCATION is declared in AndroidManifest.xml, and optionally ACCESS_FINE_LOCATION if you require precise location access. |
| OS-PLUG-GLOC-0019 | Android | The location button reported an error. |
5 changes: 4 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ dependencies {
implementation project(':capacitor-android')
}

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'
compileOnly localRuntimeAars
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"

Expand Down
Binary file modified android/libs/IONGeolocationLib-debug.aar
Binary file not shown.
Binary file removed android/libs/IONGeolocationLibApi37-debug.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ object GeolocationErrors {
message = "Unable to retrieve location because device has both Network and Location turned off."
)

val LOCATION_BUTTON_ERROR = ErrorInfo(
code = formatErrorCode(19),
message = "The location button reported an error."
)

val LOCATION_MANIFEST_PERMISSIONS_MISSING = ErrorInfo(
code = formatErrorCode(18),
message = "Location permissions are not declared in manifest. Make sure at least ACCESS_COARSE_LOCATION is declared in AndroidManifest.xml, and optionally ACCESS_FINE_LOCATION if you require precise location access."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.capacitorjs.plugins.geolocation

import android.Manifest
import android.os.Build
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import com.getcapacitor.JSObject
import com.getcapacitor.PermissionState
Expand All @@ -19,7 +17,6 @@ import io.ionic.libs.iongeolocationlib.model.IONGLOCException
import io.ionic.libs.iongeolocationlib.model.IONGLOCLocationOptions
import io.ionic.libs.iongeolocationlib.model.IONGLOCLocationResult
import io.ionic.libs.iongeolocationlib.view.IONGLOCLocationButtonRegistry
import io.ionic.libs.iongeolocationlib.view.IONGLOCLocationButtonPermissionRequester
import io.ionic.libs.ionnativeislandslib.NativeIslandsController
import io.ionic.libs.ionnativeislandslib.NativeIslandsScrollChannel
import kotlinx.coroutines.CoroutineScope
Expand All @@ -42,19 +39,10 @@ class GeolocationPlugin : Plugin() {

private lateinit var controller: IONGLOCController
private lateinit var coroutineScope: CoroutineScope
private lateinit var locationButtonPermissionLauncher: ActivityResultLauncher<Array<String>>
private val nativeIslandsController = NativeIslandsController { event, payload ->
notifyListeners("nativeIslands:$event", JSObject.fromJSONObject(payload))
}
private val pendingLocationButtonPermissionResults = mutableListOf<(Boolean) -> Unit>()
private var locationButtonPermissionRequestInFlight = false
private val watchingCalls: MutableMap<String, PluginCall> = mutableMapOf()
private val locationButtonPermissionRequester =
IONGLOCLocationButtonPermissionRequester { callback ->
activity.runOnUiThread {
requestLocationButtonPermission(callback)
}
}

companion object {
const val LOCATION_ALIAS: String = "location"
Expand All @@ -74,17 +62,8 @@ class GeolocationPlugin : Plugin() {
}

this.controller = IONGLOCController(context, activityLauncher)
locationButtonPermissionLauncher = bridge.registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions(),
) { results ->
completeLocationButtonPermissionRequest(
results[Manifest.permission.ACCESS_FINE_LOCATION] == true,
)
}
IONGLOCLocationButtonRegistry.register(
activity,
controller,
locationButtonPermissionRequester,
::mapButtonErrorCode,
::mapButtonPosition,
)
Expand All @@ -106,38 +85,6 @@ class GeolocationPlugin : Plugin() {
}
}

private fun requestLocationButtonPermission(callback: (Boolean) -> Unit) {
if (
ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION,
) == android.content.pm.PackageManager.PERMISSION_GRANTED
) {
callback(true)
return
}
pendingLocationButtonPermissionResults += callback
if (locationButtonPermissionRequestInFlight) return
locationButtonPermissionRequestInFlight = true
try {
locationButtonPermissionLauncher.launch(
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
),
)
} catch (_: RuntimeException) {
completeLocationButtonPermissionRequest(false)
}
}

private fun completeLocationButtonPermissionRequest(granted: Boolean) {
locationButtonPermissionRequestInFlight = false
val callbacks = pendingLocationButtonPermissionResults.toList()
pendingLocationButtonPermissionResults.clear()
callbacks.forEach { callback -> callback(granted) }
}

/** Native Islands carrier methods hosted by Geolocation. */
@PluginMethod
fun nativeIslandsLocationButtonCapabilities(call: PluginCall) {
Expand Down Expand Up @@ -180,13 +127,7 @@ class GeolocationPlugin : Plugin() {
override fun handleOnDestroy() {
super.handleOnDestroy()
coroutineScope.cancel()
pendingLocationButtonPermissionResults.clear()
locationButtonPermissionRequestInFlight = false
if (::locationButtonPermissionLauncher.isInitialized) {
locationButtonPermissionLauncher.unregister()
}
nativeIslandsController.dispose()
IONGLOCLocationButtonRegistry.unregister(activity)
}

override fun handleOnResume() {
Expand Down Expand Up @@ -457,6 +398,7 @@ class GeolocationPlugin : Plugin() {
GeolocationErrors.GOOGLE_SERVICES_ERROR
}
is IONGLOCException.IONGLOCLocationRetrievalTimeoutException -> GeolocationErrors.GET_LOCATION_TIMEOUT
is IONGLOCException.IONGLOCLocationButtonException -> GeolocationErrors.LOCATION_BUTTON_ERROR
}

/**
Expand All @@ -466,7 +408,7 @@ class GeolocationPlugin : Plugin() {
* @param exception Throwable to map
*/
private fun mapButtonErrorCode(exception: Throwable): String? =
(exception as? IONGLOCException)?.let { mapToErrorInfo(it).code }
((exception as? IONGLOCException)?.let { mapToErrorInfo(it) } ?: GeolocationErrors.POSITION_UNAVAILABLE).code

/**
* Extension function to return a successful plugin result
Expand Down
16 changes: 8 additions & 8 deletions example-app-location-button/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 2 additions & 16 deletions example-app-location-button/src/js/capacitor-welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,13 @@ window.customElements.define(
</select>
</div>
<div class="field">
<label for="lb-corner-radius">Corner radius: <span id="lb-corner-radius-value">22</span>px</label>
<input type="range" id="lb-corner-radius" min="0" max="68" value="22">
</div>
<div class="field">
<label for="lb-pressed-corner-radius">Pressed corner radius: <span id="lb-pressed-corner-radius-value">12</span>px</label>
<input type="range" id="lb-pressed-corner-radius" min="0" max="68" value="12">
<label for="lb-corner-radius">Corner radius: <span id="lb-corner-radius-value">16</span>px</label>
<input type="range" id="lb-corner-radius" min="0" max="68" value="16">
</div>
<div class="field">
<label for="lb-stroke-width">Stroke width: <span id="lb-stroke-width-value">0</span>px (library max is 3)</label>
<input type="range" id="lb-stroke-width" min="0" max="3" value="0">
</div>
<div class="field">
<label for="lb-clickable-padding">Clickable padding: <span id="lb-clickable-padding-value">6</span>px</label>
<input type="range" id="lb-clickable-padding" min="4" max="8" value="6">
</div>
<div class="field field-inline">
<label for="lb-background-color">Background color</label>
<input type="color" id="lb-background-color" value="#0b57d0">
Expand Down Expand Up @@ -305,15 +297,9 @@ window.customElements.define(
bindRange('lb-corner-radius', 'lb-corner-radius-value', (value) => {
locationButton.style.borderRadius = `${value}px`;
});
bindRange('lb-pressed-corner-radius', 'lb-pressed-corner-radius-value', (value) => {
locationButton.style.setProperty('--os-location-button-pressed-corner-radius', `${value}px`);
});
bindRange('lb-stroke-width', 'lb-stroke-width-value', (value) => {
locationButton.style.borderTopWidth = `${value}px`;
});
bindRange('lb-clickable-padding', 'lb-clickable-padding-value', (value) => {
locationButton.style.setProperty('--os-location-button-clickable-padding', `${value}px`);
});

customizePanel.querySelector('#lb-text-type').addEventListener('change', (event) => {
locationButton.setAttribute('text-type', event.target.value);
Expand Down
27 changes: 2 additions & 25 deletions src/location-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ const STYLE_PROPERTIES = {
iconTint: '--os-location-button-icon-color',
strokeColor: 'border-top-color',
strokeWidth: 'border-top-width',
pressedCornerRadius: '--os-location-button-pressed-corner-radius',
clickablePadding: '--os-location-button-clickable-padding',
} as const;
const OBSERVED_ATTRIBUTES = ['text-type', 'maximum-age', 'timeout', 'enable-location-fallback'];
const OBSERVED_STYLES = [
Expand All @@ -52,8 +50,6 @@ const OBSERVED_STYLES = [
STYLE_PROPERTIES.iconTint,
STYLE_PROPERTIES.strokeColor,
STYLE_PROPERTIES.strokeWidth,
STYLE_PROPERTIES.pressedCornerRadius,
STYLE_PROPERTIES.clickablePadding,
'border-top-left-radius',
];

Expand Down Expand Up @@ -95,19 +91,6 @@ function pixelStyle(
return Number.isFinite(number) && number >= minimum && number <= maximum ? number : fallback;
}

function clampedPixelStyle(
style: CSSStyleDeclaration,
name: string,
minimum: number,
maximum: number,
fallback: number,
): number {
const value = style.getPropertyValue(name).trim();
if (!value.endsWith('px')) return fallback;
const number = Number.parseFloat(value);
return Number.isFinite(number) ? Math.min(maximum, Math.max(minimum, number)) : fallback;
}

function nonNegativeIntegerAttribute(element: HTMLElement, name: string, fallback: number, minimum = 0): number {
const value = element.getAttribute(name);
if (value === null) return fallback;
Expand Down Expand Up @@ -314,7 +297,7 @@ function installFallbackStyles(root: Document | ShadowRoot = document): void {
block-size: 100%;
min-inline-size: 3rem;
min-block-size: 3rem;
padding-inline: calc(1rem + clamp(4px, var(--os-location-button-clickable-padding, 6px), 8px));
padding-inline: 1rem;
border: inherit;
border-image-source: linear-gradient(transparent, transparent);
border-image-slice: 1;
Expand All @@ -329,7 +312,7 @@ function installFallbackStyles(root: Document | ShadowRoot = document): void {
.os-location-button-fallback::before {
content: '';
position: absolute;
inset: clamp(4px, var(--os-location-button-clickable-padding, 6px), 8px);
inset: 0;
z-index: 0;
box-sizing: border-box;
border-width: inherit;
Expand All @@ -339,10 +322,6 @@ function installFallbackStyles(root: Document | ShadowRoot = document): void {
background-color: inherit;
}

.os-location-button-fallback:active::before {
border-radius: clamp(0px, var(--os-location-button-pressed-corner-radius, 12px), 68px);
}

.os-location-button-fallback > * {
position: relative;
z-index: 1;
Expand Down Expand Up @@ -460,9 +439,7 @@ function registerLocationButton(geolocationPlugin: GeolocationPlugin, protectedS
iconTint: colorStyle(style, STYLE_PROPERTIES.iconTint, textColor),
strokeColor: colorStyle(style, STYLE_PROPERTIES.strokeColor, '#000000'),
cornerRadius,
pressedCornerRadius: pixelStyle(style, STYLE_PROPERTIES.pressedCornerRadius, 0, 68, 12),
strokeWidth: pixelStyle(style, STYLE_PROPERTIES.strokeWidth, 0, 3, 0),
clickablePadding: clampedPixelStyle(style, STYLE_PROPERTIES.clickablePadding, 4, 8, 6),
maximumAge: nonNegativeIntegerAttribute(element, 'maximum-age', 0),
timeout: nonNegativeIntegerAttribute(element, 'timeout', 10_000, 1),
enableLocationFallback: booleanAttribute(element, 'enable-location-fallback', true),
Expand Down
Binary file modified vendor/capacitor-native-islands-1.0.0.tgz
Binary file not shown.