-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add support for heading in addWatch
#10
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
Open
ItsChaceD
wants to merge
9
commits into
main
Choose a base branch
from
feat/RMET-4897/heading-support
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
be4385b
feat: add support for heading
ItsChaceD 9620caf
remove heading from getCurrentPosition
ItsChaceD 4cc83cf
fix tests
ItsChaceD dae13bf
fix tests
ItsChaceD a3de742
refactor sensor handler
ItsChaceD 00d4ff2
refactor true heading
ItsChaceD 7116bc2
stop sensor on clear
ItsChaceD 4d49acc
move sensor handler stop to clearWatch
ItsChaceD 9975e47
remove watch channels
ItsChaceD 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
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
121 changes: 121 additions & 0 deletions
121
src/main/kotlin/io/ionic/libs/iongeolocationlib/controller/IONGLOCSensorHandler.kt
OS-pedrogustavobilro marked this conversation as resolved.
Show resolved
Hide resolved
|
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,121 @@ | ||
| package io.ionic.libs.iongeolocationlib.controller | ||
|
|
||
| import android.content.Context | ||
| import android.hardware.Sensor | ||
| import android.hardware.SensorEvent | ||
| import android.hardware.SensorEventListener | ||
| import android.hardware.SensorManager | ||
| import android.location.Location | ||
| import android.hardware.GeomagneticField | ||
|
|
||
| /** | ||
| * Handler for device sensors to calculate heading. | ||
| */ | ||
| internal class IONGLOCSensorHandler(context: Context) : SensorEventListener { | ||
OS-pedrogustavobilro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager | ||
| private val accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) | ||
| private val magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) | ||
|
|
||
| private var gravity: FloatArray? = null | ||
| private var geomagnetic: FloatArray? = null | ||
|
|
||
| @Volatile | ||
| var magneticHeading: Float? = null | ||
| private set | ||
|
|
||
| @Volatile | ||
| var headingAccuracy: Float? = null | ||
| private set | ||
|
|
||
| private var watcherCount = 0 | ||
|
|
||
| @Synchronized | ||
| fun start() { | ||
| if (watcherCount == 0) { | ||
| accelerometer?.let { | ||
| sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_UI) | ||
| } | ||
| magnetometer?.let { | ||
| sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_UI) | ||
| } | ||
| } | ||
| watcherCount++ | ||
| } | ||
|
|
||
| @Synchronized | ||
| fun stop() { | ||
| watcherCount-- | ||
| if (watcherCount <= 0) { | ||
| watcherCount = 0 | ||
| sensorManager.unregisterListener(this) | ||
| } | ||
| } | ||
|
|
||
| override fun onSensorChanged(event: SensorEvent) { | ||
| if (event.sensor.type == Sensor.TYPE_ACCELEROMETER) { | ||
| gravity = event.values | ||
| } | ||
| if (event.sensor.type == Sensor.TYPE_MAGNETIC_FIELD) { | ||
| geomagnetic = event.values | ||
| // Using magnetometer accuracy as heading accuracy proxy | ||
| headingAccuracy = getHeadingAccuracy(event.accuracy) | ||
| } | ||
|
|
||
| updateHeadings() | ||
| } | ||
|
|
||
| private fun updateHeadings() { | ||
| val g = gravity ?: return | ||
| val m = geomagnetic ?: return | ||
|
|
||
| val r = FloatArray(9) | ||
| val i = FloatArray(9) | ||
|
|
||
| if (SensorManager.getRotationMatrix(r, i, g, m)) { | ||
| val orientation = FloatArray(3) | ||
| SensorManager.getOrientation(r, orientation) | ||
|
|
||
| // Azimuth is orientation[0], in radians. | ||
| // Convert to degrees and normalize to 0-360. | ||
| val azimuthInRadians = orientation[0] | ||
| val azimuthInDegrees = Math.toDegrees(azimuthInRadians.toDouble()).toFloat() | ||
| magneticHeading = (azimuthInDegrees + 360) % 360 | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Calculates the true heading on the fly based on a given location. | ||
| * @param location the location to use for calculating the geomagnetic declination | ||
| * @return the calculated true heading or null if magnetic heading is not yet available | ||
| */ | ||
| fun getTrueHeading(location: Location): Float? { | ||
| return magneticHeading?.let { mh -> | ||
| val geoField = GeomagneticField( | ||
| location.latitude.toFloat(), | ||
| location.longitude.toFloat(), | ||
| location.altitude.toFloat(), | ||
| location.time | ||
| ) | ||
| (mh + geoField.declination + 360) % 360 | ||
| } | ||
| } | ||
|
|
||
| override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) { | ||
| if (sensor.type == Sensor.TYPE_MAGNETIC_FIELD) { | ||
OS-pedrogustavobilro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| headingAccuracy = getHeadingAccuracy(accuracy) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Uses SensorManager accuracy status as a heuristic proxy for heading accuracy, | ||
| * as Android does not provide direct heading accuracy in degrees for the magnetometer. | ||
| */ | ||
| private fun getHeadingAccuracy(accuracy: Int): Float { | ||
| return when (accuracy) { | ||
| SensorManager.SENSOR_STATUS_ACCURACY_HIGH -> 10f | ||
| SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM -> 20f | ||
| SensorManager.SENSOR_STATUS_ACCURACY_LOW -> 30f | ||
| else -> 45f | ||
| } | ||
| } | ||
| } | ||
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.