Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: Avoid directly implementing ISensorEventListener on SimpleOrientationSensor on Android #12224

Merged
Show file tree
Hide file tree
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
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