summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobrun <tobrun.van.nuland@gmail.com>2017-08-10 13:18:46 +0200
committerTobrun <tobrun@mapbox.com>2017-08-10 15:08:32 +0200
commit9238dd46b4bb1f3dfdc87848f97aa66629a392af (patch)
treeaa9e0cb62eeea800a0f8d25f71022e2f90ecae8b
parenta5eecd6f11b62f6c69ec6d321c4f5a73dd8b15e5 (diff)
downloadqtlocation-mapboxgl-9238dd46b4bb1f3dfdc87848f97aa66629a392af.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.java30
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 4e696fb56b..22e92872a5 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
@@ -810,6 +810,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
@@ -846,9 +849,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]);
@@ -865,6 +867,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);