Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Compability for Samsung devices forcing 3-4 array vector length #9746

Merged
merged 1 commit into from
Aug 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,9 @@ private class CompassListener implements SensorEventListener {

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
Expand Down Expand Up @@ -846,9 +849,8 @@ public void onSensorChanged(SensorEvent event) {
}

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]);
Expand All @@ -865,6 +867,28 @@ public void onSensorChanged(SensorEvent event) {
}
}

/**
* 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);
Expand Down