Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Check if 4th value is present in orienation sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Mar 11, 2020
1 parent 9e74d1c commit b6fe59a
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions Xamarin.Essentials/OrientationSensor/OrientationSensor.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ void ISensorEventListener.OnAccuracyChanged(Sensor sensor, [GeneratedEnum] Senso

void ISensorEventListener.OnSensorChanged(SensorEvent e)
{
if ((e?.Values?.Count ?? 0) < 4)
var count = e?.Values?.Count ?? 0;
if (count < 3)
return;

var data = new OrientationSensorData(e.Values[0], e.Values[1], e.Values[2], e.Values[3]);
OrientationSensor.OnChanged(data);
OrientationSensorData? data;

// Docs: https://developer.android.com/reference/android/hardware/SensorEvent#sensor.type_rotation_vector-:
// values[3], originally optional, will always be present from SDK Level 18 onwards. values[4] is a new value that has been added in SDK Level 18.

if (count < 4)
data = new OrientationSensorData(e.Values[0], e.Values[1], e.Values[2], -1);
else
data = new OrientationSensorData(e.Values[0], e.Values[1], e.Values[2], e.Values[3]);

OrientationSensor.OnChanged(data.Value);
}
}
}

0 comments on commit b6fe59a

Please sign in to comment.