diff --git a/build/PackageDiffIgnore.xml b/build/PackageDiffIgnore.xml index f60baeb97810..be6a7c9064f0 100644 --- a/build/PackageDiffIgnore.xml +++ b/build/PackageDiffIgnore.xml @@ -994,7 +994,9 @@ - + + + diff --git a/src/Uno.UWP/Devices/Sensors/SimpleOrientationSensor.Android.cs b/src/Uno.UWP/Devices/Sensors/SimpleOrientationSensor.Android.cs index 9ae058809963..2f8904994d5f 100644 --- a/src/Uno.UWP/Devices/Sensors/SimpleOrientationSensor.Android.cs +++ b/src/Uno.UWP/Devices/Sensors/SimpleOrientationSensor.Android.cs @@ -18,7 +18,7 @@ namespace Windows.Devices.Sensors { - public partial class SimpleOrientationSensor : Java.Lang.Object, ISensorEventListener + public partial class SimpleOrientationSensor { #region Static @@ -88,7 +88,7 @@ partial void Initialize() // If the the device has a gyroscope we will use the SensorType.Gravity, if not we will use single angle orientation calculations instead if (gravitySensor != null && useGravitySensor) { - _sensorManager.RegisterListener(this, _sensorManager.GetDefaultSensor(_gravitySensorType), SensorDelay.Normal); + _sensorManager.RegisterListener(new OrientationListener(this), _sensorManager.GetDefaultSensor(_gravitySensorType), SensorDelay.Normal); } else { @@ -108,30 +108,6 @@ partial void Initialize() }, null); } - #region GraviySensorType Methods - - public void OnAccuracyChanged(Sensor? sensor, [GeneratedEnum] SensorStatus accuracy) - { - } - - public void OnSensorChanged(SensorEvent? e) - { - if (e?.Sensor?.Type != _gravitySensorType || e?.Values == null) - { - return; - } - - // All units are negatives compared to iOS : https://developer.android.com/reference/android/hardware/SensorEvent#values - var gravityX = -(double)e.Values[0]; - var gravityY = -(double)e.Values[1]; - var gravityZ = -(double)e.Values[2]; - - var simpleOrientation = ToSimpleOrientation(gravityX, gravityY, gravityZ, _threshold, _currentOrientation); - SetCurrentOrientation(simpleOrientation); - } - - #endregion - #region OrientationSensorType Methods and Classes private void OnOrientationChanged(int angle) @@ -256,6 +232,36 @@ public SimpleOrientationEventListener(Action orientationChanged) : base(Con public override void OnOrientationChanged(int orientation) => _orientationChanged(orientation); } + private sealed class OrientationListener : Java.Lang.Object, ISensorEventListener + { + private readonly SimpleOrientationSensor _simpleOrientationSensor; + + public OrientationListener(SimpleOrientationSensor simpleOrientationSensor) + { + _simpleOrientationSensor = simpleOrientationSensor; + } + + public void OnAccuracyChanged(Sensor? sensor, [GeneratedEnum] SensorStatus accuracy) + { + } + + public void OnSensorChanged(SensorEvent? e) + { + if (e?.Sensor?.Type != _gravitySensorType || e?.Values == null) + { + return; + } + + // All units are negatives compared to iOS : https://developer.android.com/reference/android/hardware/SensorEvent#values + var gravityX = -(double)e.Values[0]; + var gravityY = -(double)e.Values[1]; + var gravityZ = -(double)e.Values[2]; + + var simpleOrientation = ToSimpleOrientation(gravityX, gravityY, gravityZ, _threshold, _simpleOrientationSensor._currentOrientation); + _simpleOrientationSensor.SetCurrentOrientation(simpleOrientation); + } + } + #endregion } }