diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2017-08-10 13:18:46 +0200 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2017-08-19 11:29:38 +0200 |
commit | b4cb719ceba57b5b2879d54473531c3404a20c77 (patch) | |
tree | 0db73f255cb1b1042ee5381a8146c957788ce81c | |
parent | 05cbd4e31d40921acea40ad935ceb06a0ea08324 (diff) | |
download | qtlocation-mapboxgl-b4cb719ceba57b5b2879d54473531c3404a20c77.tar.gz |
[android] - provide compability with Samsung devices forcing 3-4 array matrix length
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java index 24da59bb7e..61b0efe656 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java @@ -797,6 +797,9 @@ public class MyLocationView extends View { private Sensor rotationVectorSensor; private float[] matrix = new float[9]; + private float[] rotationVectorValue; + private float[] truncatedRotationVectorValue = new float[4]; + private float[] orientation = new float[3]; private boolean reportMissingSensor = true; // Compass data @@ -833,9 +836,8 @@ public class MyLocationView extends View { } if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) { - - // calculate the rotation matrix - SensorManager.getRotationMatrixFromVector(matrix, event.values); + rotationVectorValue = getRotationVectorFromSensorEvent(event); + SensorManager.getRotationMatrixFromVector(matrix, rotationVectorValue); SensorManager.getOrientation(matrix, orientation); magneticHeading = (float) Math.toDegrees(SensorManager.getOrientation(matrix, orientation)[0]); @@ -852,6 +854,28 @@ public class MyLocationView extends View { } } + /** + * Pulls out the rotation vector from a SensorEvent, with a maximum length + * vector of four elements to avoid potential compatibility issues. + * + * @param event the sensor event + * @return the events rotation vector, potentially truncated + */ + @NonNull + float[] getRotationVectorFromSensorEvent(@NonNull SensorEvent event) { + if (event.values.length > 4) { + // On some Samsung devices SensorManager.getRotationMatrixFromVector + // appears to throw an exception if rotation vector has length > 4. + // For the purposes of this class the first 4 values of the + // rotation vector are sufficient (see crbug.com/335298 for details). + // Only affects Android 4.3 + System.arraycopy(event.values, 0, truncatedRotationVectorValue, 0, 4); + return truncatedRotationVectorValue; + } else { + return event.values; + } + } + private void rotateCamera(float rotation) { CameraPosition.Builder builder = new CameraPosition.Builder(); builder.bearing(rotation); |