-
-
Notifications
You must be signed in to change notification settings - Fork 42
Device orientation support #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jgillich
wants to merge
5
commits into
maplibre:main
Choose a base branch
from
jgillich:sensors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...androidMain/kotlin/org/maplibre/compose/location/AndroidSensorEnhancedLocationProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package org.maplibre.compose.location | ||
|
|
||
| import android.Manifest | ||
| import android.content.Context | ||
| import android.content.Context.SENSOR_SERVICE | ||
| import android.hardware.Sensor | ||
| import android.hardware.SensorEvent | ||
| import android.hardware.SensorEventListener | ||
| import android.hardware.SensorManager | ||
| import androidx.annotation.RequiresPermission | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberCoroutineScope | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.channels.awaitClose | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.SharingStarted | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.callbackFlow | ||
| import kotlinx.coroutines.flow.combine | ||
| import kotlinx.coroutines.flow.stateIn | ||
|
|
||
| /** | ||
| * A [LocationProvider] that enhances an existing [LocationProvider] with bearing information | ||
| * derived from the device's rotation vector sensor. | ||
| * | ||
| * This class listens to the `Sensor.TYPE_ROTATION_VECTOR` to get the device's orientation. It then | ||
| * combines this sensor-derived bearing with the location data from the wrapped [locationProvider]. | ||
| * | ||
| * The sensor-based bearing is only used if its accuracy is better than the bearing accuracy | ||
| * provided by the original location provider. | ||
| * | ||
| * @param context The application context, used to access the `SensorManager`. | ||
| * @param locationProvider The underlying [LocationProvider] to be enhanced. | ||
| * @param coroutineScope The [CoroutineScope] in which the location and bearing flows are combined. | ||
| * @param sharingStarted The strategy for starting and stopping the collection of the location flow. | ||
| * Defaults to [SharingStarted.WhileSubscribed] with a 1-second stop timeout. | ||
| * @throws IllegalStateException if the rotation vector sensor is not available on the device. | ||
| */ | ||
| public class AndroidSensorEnhancedLocationProvider( | ||
| context: Context, | ||
| locationProvider: LocationProvider, | ||
| coroutineScope: CoroutineScope, | ||
| sharingStarted: SharingStarted = SharingStarted.WhileSubscribed(stopTimeoutMillis = 1000), | ||
| ) : LocationProvider { | ||
| private val sensorManager = context.getSystemService(SENSOR_SERVICE) as SensorManager | ||
|
|
||
| private fun accuracyToDegrees(accuracy: Int): Double = | ||
| when (accuracy) { | ||
| SensorManager.SENSOR_STATUS_ACCURACY_HIGH -> 5.0 | ||
| SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM -> 15.0 | ||
| SensorManager.SENSOR_STATUS_ACCURACY_LOW -> 45.0 | ||
| SensorManager.SENSOR_STATUS_UNRELIABLE -> 180.0 | ||
| else -> 180.0 | ||
| } | ||
|
|
||
| private val bearing: Flow<Pair<Double, Double>> = callbackFlow { | ||
| val rotationMatrix = FloatArray(9) | ||
| val orientationAngles = FloatArray(3) | ||
| var accuracyDegrees = accuracyToDegrees(SensorManager.SENSOR_STATUS_UNRELIABLE) | ||
|
|
||
| val listener = | ||
| object : SensorEventListener { | ||
| override fun onSensorChanged(event: SensorEvent?) { | ||
| if (event?.sensor?.type == Sensor.TYPE_ROTATION_VECTOR) { | ||
| SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values) | ||
| SensorManager.getOrientation(rotationMatrix, orientationAngles) | ||
|
|
||
| trySend(Math.toDegrees(orientationAngles[0].toDouble()) to accuracyDegrees) | ||
| } | ||
| } | ||
|
|
||
| override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) { | ||
| accuracyDegrees = accuracyToDegrees(accuracy) | ||
| } | ||
| } | ||
|
|
||
| val sensor = | ||
| sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) | ||
| ?: throw IllegalStateException("Rotation vector sensor is not available") | ||
|
|
||
| sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL) | ||
|
|
||
| awaitClose { sensorManager.unregisterListener(listener) } | ||
| } | ||
|
|
||
| override val location: StateFlow<Location?> = | ||
| locationProvider.location | ||
| .combine(bearing) { location, (sensorBearing, sensorAccuracy) -> | ||
| val bearingAccuracy = location?.bearingAccuracy | ||
| if (bearingAccuracy != null && bearingAccuracy > sensorAccuracy) { | ||
jgillich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| location.copy(bearing = sensorBearing, accuracy = sensorAccuracy) | ||
| } else { | ||
| location | ||
| } | ||
| } | ||
| .stateIn(coroutineScope, sharingStarted, null) | ||
| } | ||
|
|
||
| @Composable | ||
| @RequiresPermission( | ||
| anyOf = [Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION] | ||
| ) | ||
| public actual fun rememberSensorEnhancedLocationProvider( | ||
| locationProvider: LocationProvider | ||
| ): LocationProvider { | ||
| return rememberAndroidSensorEnhancedLocationProvider(locationProvider = locationProvider) | ||
| } | ||
|
|
||
| /** | ||
| * Create and remember an [AndroidSensorEnhancedLocationProvider], a [LocationProvider] that | ||
| * enhances an existing [LocationProvider] with bearing information derived from the device's | ||
| * rotation vector sensor. | ||
| */ | ||
| @Composable | ||
| @RequiresPermission( | ||
| anyOf = [Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION] | ||
| ) | ||
| public fun rememberAndroidSensorEnhancedLocationProvider( | ||
| locationProvider: LocationProvider, | ||
| context: Context = LocalContext.current, | ||
| coroutineScope: CoroutineScope = rememberCoroutineScope(), | ||
| sharingStarted: SharingStarted = SharingStarted.WhileSubscribed(stopTimeoutMillis = 1000), | ||
| ): AndroidSensorEnhancedLocationProvider { | ||
| return remember(context, locationProvider, coroutineScope, sharingStarted) { | ||
| AndroidSensorEnhancedLocationProvider( | ||
| context = context, | ||
| locationProvider = locationProvider, | ||
| coroutineScope = coroutineScope, | ||
| sharingStarted = sharingStarted, | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.