From 392fbee686936c6cec5e01977931a3385cd26671 Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Tue, 5 Jun 2018 16:37:04 +0200 Subject: [PATCH] GH-240 Add IsMainThread detection API (#277) * Add IsMainThread detection API * Add platform tests for main thread * Update docs and add more platform tests --- .../DeviceTests.Shared.projitems | 1 + .../DeviceTests.Shared/Platform_Tests.cs | 27 +++++ .../Platform/Platform.android.cs | 17 +++ Xamarin.Essentials/Platform/Platform.ios.cs | 5 +- .../Platform/Platform.netstandard.cs | 3 + .../Platform/Platform.shared.cs | 7 +- Xamarin.Essentials/Platform/Platform.uwp.cs | 3 + .../xamarin-essentials-android.xml | 14 +++ .../xamarin-essentials-ios.xml | 14 +++ .../xamarin-essentials-uwp.xml | 14 +++ .../en/FrameworksIndex/xamarin-essentials.xml | 14 +++ .../Xamarin.Essentials/OrientationSensor.xml | 105 ++++++++++++++++++ .../OrientationSensorChangedEventArgs.xml | 41 +++++++ .../OrientationSensorChangedEventHandler.xml | 25 +++++ .../OrientationSensorData.xml | 41 +++++++ docs/en/Xamarin.Essentials/Platform.xml | 28 ++++- docs/en/index.xml | 4 + 17 files changed, 357 insertions(+), 6 deletions(-) create mode 100644 DeviceTests/DeviceTests.Shared/Platform_Tests.cs create mode 100644 docs/en/Xamarin.Essentials/OrientationSensor.xml create mode 100644 docs/en/Xamarin.Essentials/OrientationSensorChangedEventArgs.xml create mode 100644 docs/en/Xamarin.Essentials/OrientationSensorChangedEventHandler.xml create mode 100644 docs/en/Xamarin.Essentials/OrientationSensorData.xml diff --git a/DeviceTests/DeviceTests.Shared/DeviceTests.Shared.projitems b/DeviceTests/DeviceTests.Shared/DeviceTests.Shared.projitems index c4db537bc4a..cd9deefd823 100644 --- a/DeviceTests/DeviceTests.Shared/DeviceTests.Shared.projitems +++ b/DeviceTests/DeviceTests.Shared/DeviceTests.Shared.projitems @@ -23,6 +23,7 @@ + diff --git a/DeviceTests/DeviceTests.Shared/Platform_Tests.cs b/DeviceTests/DeviceTests.Shared/Platform_Tests.cs new file mode 100644 index 00000000000..965f0d28fe3 --- /dev/null +++ b/DeviceTests/DeviceTests.Shared/Platform_Tests.cs @@ -0,0 +1,27 @@ +using System.Threading.Tasks; +using Xamarin.Essentials; +using Xunit; + +namespace DeviceTests +{ + public class Platform_Tests + { + [Fact] + public Task IsOnMainThread() + { + return Utils.OnMainThread(() => + { + Assert.True(Platform.IsMainThread); + }); + } + + [Fact] + public Task IsNotOnMainThread() + { + return Task.Run(() => + { + Assert.False(Platform.IsMainThread); + }); + } + } +} diff --git a/Xamarin.Essentials/Platform/Platform.android.cs b/Xamarin.Essentials/Platform/Platform.android.cs index c492e7b2b4d..ba405ea2ef8 100644 --- a/Xamarin.Essentials/Platform/Platform.android.cs +++ b/Xamarin.Essentials/Platform/Platform.android.cs @@ -56,8 +56,25 @@ internal static bool IsIntentSupported(Intent intent) internal static bool HasApiLevel(BuildVersionCodes versionCode) => (int)Build.VERSION.SdkInt >= (int)versionCode; + static bool PlatformIsMainThread + { + get + { + if (HasApiLevel(BuildVersionCodes.M)) + return Looper.MainLooper.IsCurrentThread; + + return Looper.MyLooper() == Looper.MainLooper; + } + } + static void PlatformBeginInvokeOnMainThread(Action action) { + if (IsMainThread) + { + action(); + return; + } + if (handler?.Looper != Looper.MainLooper) handler = new Handler(Looper.MainLooper); diff --git a/Xamarin.Essentials/Platform/Platform.ios.cs b/Xamarin.Essentials/Platform/Platform.ios.cs index 50b3e175d19..0aa6497740b 100644 --- a/Xamarin.Essentials/Platform/Platform.ios.cs +++ b/Xamarin.Essentials/Platform/Platform.ios.cs @@ -8,9 +8,12 @@ namespace Xamarin.Essentials { public static partial class Platform { + static bool PlatformIsMainThread => + NSThread.Current.IsMainThread; + static void PlatformBeginInvokeOnMainThread(Action action) { - if (NSThread.Current.IsMainThread) + if (IsMainThread) { action(); return; diff --git a/Xamarin.Essentials/Platform/Platform.netstandard.cs b/Xamarin.Essentials/Platform/Platform.netstandard.cs index 25ee81337ea..caef600670d 100644 --- a/Xamarin.Essentials/Platform/Platform.netstandard.cs +++ b/Xamarin.Essentials/Platform/Platform.netstandard.cs @@ -6,5 +6,8 @@ public static partial class Platform { static void PlatformBeginInvokeOnMainThread(Action action) => throw new NotImplementedInReferenceAssemblyException(); + + static bool PlatformIsMainThread => + throw new NotImplementedInReferenceAssemblyException(); } } diff --git a/Xamarin.Essentials/Platform/Platform.shared.cs b/Xamarin.Essentials/Platform/Platform.shared.cs index 6a70a73506f..42360a86214 100644 --- a/Xamarin.Essentials/Platform/Platform.shared.cs +++ b/Xamarin.Essentials/Platform/Platform.shared.cs @@ -5,8 +5,11 @@ namespace Xamarin.Essentials { public static partial class Platform { - public static void BeginInvokeOnMainThread(Action action) - => PlatformBeginInvokeOnMainThread(action); + public static bool IsMainThread => + PlatformIsMainThread; + + public static void BeginInvokeOnMainThread(Action action) => + PlatformBeginInvokeOnMainThread(action); internal static Task InvokeOnMainThread(Action action) { diff --git a/Xamarin.Essentials/Platform/Platform.uwp.cs b/Xamarin.Essentials/Platform/Platform.uwp.cs index 11cd1d35e96..d606bc58ce8 100644 --- a/Xamarin.Essentials/Platform/Platform.uwp.cs +++ b/Xamarin.Essentials/Platform/Platform.uwp.cs @@ -6,6 +6,9 @@ namespace Xamarin.Essentials { public static partial class Platform { + static bool PlatformIsMainThread => + CoreApplication.MainView.CoreWindow.Dispatcher == null; + static void PlatformBeginInvokeOnMainThread(Action action) { var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher; diff --git a/docs/en/FrameworksIndex/xamarin-essentials-android.xml b/docs/en/FrameworksIndex/xamarin-essentials-android.xml index 7ca06ecece7..08bab4bdbd1 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-android.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-android.xml @@ -251,6 +251,19 @@ + + + + + + + + + + + + + @@ -277,6 +290,7 @@ + diff --git a/docs/en/FrameworksIndex/xamarin-essentials-ios.xml b/docs/en/FrameworksIndex/xamarin-essentials-ios.xml index dc12119285b..39085333b73 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-ios.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-ios.xml @@ -251,6 +251,19 @@ + + + + + + + + + + + + + @@ -274,6 +287,7 @@ + diff --git a/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml b/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml index ad0531f5735..989ce1971b5 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml @@ -251,6 +251,19 @@ + + + + + + + + + + + + + @@ -274,6 +287,7 @@ + diff --git a/docs/en/FrameworksIndex/xamarin-essentials.xml b/docs/en/FrameworksIndex/xamarin-essentials.xml index 5ac3bea0590..39835563e24 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials.xml @@ -251,6 +251,19 @@ + + + + + + + + + + + + + @@ -274,6 +287,7 @@ + diff --git a/docs/en/Xamarin.Essentials/OrientationSensor.xml b/docs/en/Xamarin.Essentials/OrientationSensor.xml new file mode 100644 index 00000000000..841b59a729e --- /dev/null +++ b/docs/en/Xamarin.Essentials/OrientationSensor.xml @@ -0,0 +1,105 @@ + + + + + + Xamarin.Essentials + 1.0.0.0 + + + System.Object + + + + Device orientation (quaternion) relative to magnetic fields. + + + + + + + + + + Property + + Xamarin.Essentials + 1.0.0.0 + + + System.Boolean + + + Gets of currently monitoring the sensor. + If monitoring. + + + + + + + + + + Event + + Xamarin.Essentials + 1.0.0.0 + + + Xamarin.Essentials.OrientationSensorChangedEventHandler + + + + Event triggered when reading of sensor changes. + + + + + + + + + + + Method + + Xamarin.Essentials + 1.0.0.0 + + + System.Void + + + + + + Sensor speed to use. + Starts monitoring orientation sensor with specific speed. + + + + + + + + + + Method + + Xamarin.Essentials + 1.0.0.0 + + + System.Void + + + + Stops monitoring. + + + + + + + diff --git a/docs/en/Xamarin.Essentials/OrientationSensorChangedEventArgs.xml b/docs/en/Xamarin.Essentials/OrientationSensorChangedEventArgs.xml new file mode 100644 index 00000000000..60de9f558da --- /dev/null +++ b/docs/en/Xamarin.Essentials/OrientationSensorChangedEventArgs.xml @@ -0,0 +1,41 @@ + + + + + + Xamarin.Essentials + 1.0.0.0 + + + System.EventArgs + + + + Orientation event args when reading changes. + + + + + + + + + + Property + + Xamarin.Essentials + 1.0.0.0 + + + Xamarin.Essentials.OrientationSensorData + + + Gets the reading when it changes. + The current Reading + + + + + + + diff --git a/docs/en/Xamarin.Essentials/OrientationSensorChangedEventHandler.xml b/docs/en/Xamarin.Essentials/OrientationSensorChangedEventHandler.xml new file mode 100644 index 00000000000..ef04a5f0354 --- /dev/null +++ b/docs/en/Xamarin.Essentials/OrientationSensorChangedEventHandler.xml @@ -0,0 +1,25 @@ + + + + + + Xamarin.Essentials + 1.0.0.0 + + + System.Delegate + + + + + + System.Void + + + Event args. + Event handler with reading. + + + + + diff --git a/docs/en/Xamarin.Essentials/OrientationSensorData.xml b/docs/en/Xamarin.Essentials/OrientationSensorData.xml new file mode 100644 index 00000000000..40e86b6d4a5 --- /dev/null +++ b/docs/en/Xamarin.Essentials/OrientationSensorData.xml @@ -0,0 +1,41 @@ + + + + + + Xamarin.Essentials + 1.0.0.0 + + + System.ValueType + + + + Sensor data for orientation. + + + + + + + + + + Property + + Xamarin.Essentials + 1.0.0.0 + + + System.Numerics.Quaternion + + + Gets the current orientation that represents a Quaternion. + Gets the current orientation + + + + + + + diff --git a/docs/en/Xamarin.Essentials/Platform.xml b/docs/en/Xamarin.Essentials/Platform.xml index 0a07f3b049f..8c37990a2ec 100644 --- a/docs/en/Xamarin.Essentials/Platform.xml +++ b/docs/en/Xamarin.Essentials/Platform.xml @@ -36,7 +36,7 @@ Action to execute. Invokes an action on the main thread of the application. - + @@ -59,7 +59,7 @@ Application to initialize with. Initialize Xamarin.Essentials with Android's application class. - + @@ -83,6 +83,26 @@ Activity to use for initialization. Bundle of the activity. Initialize Xamarin.Essentials with Android's activity and bundle. + + + + + + + + + + Property + + Xamarin.Essentials + 1.0.0.0 + + + System.Boolean + + + Gets if it is the current main UI thread. + If main thread. @@ -110,7 +130,9 @@ The permissions from the corresponding overridden method in an activity. The grantResults from the corresponding overridden method in an activity. Pass permission request results from an activity's overridden method to the library for handling internal permission requests. - + + + diff --git a/docs/en/index.xml b/docs/en/index.xml index 3970c932285..18a8572f3e9 100644 --- a/docs/en/index.xml +++ b/docs/en/index.xml @@ -111,6 +111,10 @@ + + + +