-
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
base: main
Are you sure you want to change the base?
Changes from all commits
be4385b
9620caf
4cc83cf
dae13bf
a3de742
00d4ff2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,8 @@ class IONGLOCController internal constructor( | |
| ), | ||
| private val fallbackHelper: IONGLOCFallbackHelper = IONGLOCFallbackHelper( | ||
| locationManager, connectivityManager | ||
| ) | ||
| ), | ||
| private val sensorHandler: IONGLOCSensorHandler | ||
| ) { | ||
|
|
||
| constructor( | ||
|
|
@@ -65,7 +66,8 @@ class IONGLOCController internal constructor( | |
| fusedLocationClient = LocationServices.getFusedLocationProviderClient(context), | ||
| locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager, | ||
| connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager, | ||
| activityLauncher = activityLauncher | ||
| activityLauncher = activityLauncher, | ||
| sensorHandler = IONGLOCSensorHandler(context) | ||
| ) | ||
|
|
||
| private lateinit var resolveLocationSettingsResultFlow: MutableSharedFlow<Result<Unit>> | ||
|
|
@@ -97,7 +99,8 @@ class IONGLOCController internal constructor( | |
| } else { | ||
| googleServicesHelper.getCurrentLocation(options) | ||
| } | ||
| Result.success(location.toOSLocationResult()) | ||
| val result = location.toOSLocationResult() | ||
| Result.success(result) | ||
| } | ||
| } catch (exception: Exception) { | ||
| Log.d(LOG_TAG, "Error fetching location: ${exception.message}") | ||
|
|
@@ -217,21 +220,31 @@ class IONGLOCController internal constructor( | |
| ): Flow<Result<List<IONGLOCLocationResult>>> = callbackFlow { | ||
| fun onNewLocations(locations: List<Location>) { | ||
| if (checkWatchInBlackList(watchId)) return | ||
| val locationResultList = locations.map { it.toOSLocationResult() } | ||
| val locationResultList = locations.map { | ||
| it.toOSLocationResult( | ||
| magneticHeading = sensorHandler.magneticHeading, | ||
| trueHeading = sensorHandler.getTrueHeading(it), | ||
| headingAccuracy = sensorHandler.headingAccuracy | ||
| ) | ||
| } | ||
| trySend(Result.success(locationResultList)) | ||
| } | ||
|
|
||
| sensorHandler.start() | ||
OS-pedrogustavobilro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| requestLocationUpdates( | ||
| watchId, | ||
| options, | ||
| useFallback = useFallback | ||
| ) { onNewLocations(it) } | ||
| ) { | ||
| onNewLocations(it) | ||
| } | ||
| } catch (e: Exception) { | ||
| trySend(Result.failure(e)) | ||
| } | ||
|
|
||
| awaitClose { | ||
| sensorHandler.stop() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm how about if a user sets multiple watches? Would think this call would make more sense inside
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, that's fair, but we're not calling |
||
| Log.d(LOG_TAG, "channel closed") | ||
| } | ||
| } | ||
|
|
||
OS-pedrogustavobilro marked this conversation as resolved.
Show resolved
Hide resolved
|
| 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 | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.