Skip to content

Commit

Permalink
Merge pull request #12224 from Youssef1313/simple-orientation-sensor-…
Browse files Browse the repository at this point in the history
…interface

fix!: Avoid directly implementing `ISensorEventListener` on `SimpleOrientationSensor` on Android
  • Loading branch information
jeromelaban authored May 10, 2023
2 parents 66ca42b + 96d6a82 commit 803ce8e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
4 changes: 3 additions & 1 deletion build/PackageDiffIgnore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,9 @@
<Member fullName="System.Void Windows.UI.Xaml.Window.set_Visible(System.Boolean value)" reason="Does not exist in UWP and WinUI" />

<Member fullName="System.Object Uno.UI.Svg.GlobalStaticResources.FindResource(System.String name)" reason="API Alignment" />

<Member fullName="System.Void Windows.Devices.Sensors.SimpleOrientationSensor.OnAccuracyChanged(Android.Hardware.Sensor sensor, Android.Hardware.SensorStatus accuracy)" reason="Api alignments" />
<Member fullName="System.Void Windows.Devices.Sensors.SimpleOrientationSensor.OnSensorChanged(Android.Hardware.SensorEvent e)" reason="Api alignments" />

<!-- BEGIN Skia Adjustments -->
<Member fullName="System.Void Uno.UI.Runtime.Skia.GtkHost..ctor(System.Func`1&lt;Windows.UI.Xaml.Application&gt; appBuilder, System.String[] args)" reason="API Alignment" />
<Member fullName="System.Void Uno.UI.Runtime.Skia.FrameBufferHost..ctor(System.Func`1&lt;Windows.UI.Xaml.Application&gt; appBuilder, System.String[] args)" reason="API Alignment" />
Expand Down
58 changes: 32 additions & 26 deletions src/Uno.UWP/Devices/Sensors/SimpleOrientationSensor.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace Windows.Devices.Sensors
{
public partial class SimpleOrientationSensor : Java.Lang.Object, ISensorEventListener
public partial class SimpleOrientationSensor
{
#region Static

Expand Down Expand Up @@ -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
{
Expand All @@ -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)
Expand Down Expand Up @@ -256,6 +232,36 @@ public SimpleOrientationEventListener(Action<int> 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
}
}
Expand Down

0 comments on commit 803ce8e

Please sign in to comment.