Permalink
Browse files

changed the architecture of devicepicker to allow win32 to include bo…

…th legacy and uwp apis
  • Loading branch information...
peterfoot committed Jun 2, 2018
1 parent 173136e commit 47e2762f044de950fda4add8597182bdad4a1f8f
@@ -35,7 +35,9 @@ public sealed partial class DevicePicker
private Popup _popup;
#endif
#if !WINDOWS_UWP
#if WINDOWS_UWP || WIN32
private IDevicePicker _picker;
#else
private DevicePickerFilter _filter = new DevicePickerFilter();
#endif
@@ -45,8 +47,17 @@ public sealed partial class DevicePicker
/// </summary>
public DevicePicker()
{
#if WINDOWS_UWP
//Initialize();
#if WIN32
if (Type.GetType("Windows.Devices.Enumeration.DevicePicker, Windows, ContentType=WindowsRuntime") != null)
{
_picker = new DevicePickerUap();
}
else
{
_picker = new DevicePickerWin32();
}
#elif WINDOWS_UWP
_picker = new DevicePickerUap();
#endif
}
@@ -65,7 +76,7 @@ public DevicePicker()
//{
// DevicePickerDismissed?.Invoke(this, null);
//}
// <summary>
// Indicates that the user selected a device in the picker.
// </summary>
@@ -77,6 +88,7 @@ public DevicePicker()
// DeviceSelected?.Invoke(this, new DeviceSelectedEventArgs() { SelectedDevice = device });
//}
/*
#if !UNITY
#if !WINDOWS_UWP
private DevicePickerAppearance _appearance = new DevicePickerAppearance();
@@ -98,16 +110,17 @@ public DevicePickerAppearance Appearance
{
get
{
#if WINDOWS_UWP
return GetAppearance();
#if WINDOWS_UWP || WIN32
return _picker.Appearance;
#else
return _appearance;
#endif
}
}
#endif
*/
/// <summary>
/// Gets the filter used to choose what devices to show in the picker.
/// </summary>
@@ -124,8 +137,8 @@ public DevicePickerFilter Filter
{
get
{
#if WINDOWS_UWP
return GetFilter();
#if WINDOWS_UWP || WIN32
return _picker.Filter;
#else
return _filter;
#endif
@@ -165,7 +178,9 @@ public Task<DeviceInformation> PickSingleDeviceAsync(Rect selection)
#if !UNITY
public async Task<DeviceInformation> PickSingleDeviceAsync()
{
#if __ANDROID__ || __IOS__ || WIN32 || WINDOWS_UWP
#if WINDOWS_UWP || WIN32
return await _picker.PickSingleDeviceAsync();
#elif __ANDROID__ || __IOS__
return await DoPickSingleDeviceAsync();
#elif WINDOWS_PHONE_APP
@@ -187,6 +202,11 @@ public async Task<DeviceInformation> PickSingleDeviceAsync()
return null;
#endif
}
#endif
#else
public DeviceInformation PickSingleDevice()
{
return _picker.PickSingleDevice();
}
#endif
}
}
@@ -0,0 +1,28 @@
//-----------------------------------------------------------------------
// <copyright file="IDevicePicker.cs" company="In The Hand Ltd">
// Copyright (c) 2018 In The Hand Ltd, All rights reserved.
// This source code is licensed under the MIT License - see License.txt
// </copyright>
//-----------------------------------------------------------------------
using System;
#if !UNITY
using System.Threading.Tasks;
#endif
namespace InTheHand.Devices.Enumeration
{
internal interface IDevicePicker
{
//DevicePickerAppearance Appearance { get; }
DevicePickerFilter Filter { get; }
#if WIN32
DeviceInformation PickSingleDevice();
#endif
#if !UNITY
Task<DeviceInformation> PickSingleDeviceAsync();
#endif
}
}
@@ -78,6 +78,7 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Compile Include="Platforms\netstandard\RfcommServiceId.portable.cs" />
<Compile Include="Platforms\uap\DevicePickerUap.cs" />
<Compile Include="Platforms\win32\*.cs" />
</ItemGroup>
@@ -1,38 +1,38 @@
//-----------------------------------------------------------------------
// <copyright file="DevicePicker.uwp.cs" company="In The Hand Ltd">
// Copyright (c) 2017 In The Hand Ltd, All rights reserved.
// Copyright (c) 2017-18 In The Hand Ltd, All rights reserved.
// This source code is licensed under the MIT License - see License.txt
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using InTheHand.Devices.Bluetooth;
using System.Diagnostics;
using InTheHand.Foundation;
using InTheHand.UI.Popups;
namespace InTheHand.Devices.Enumeration
{
partial class DevicePicker
internal class DevicePickerUap : IDevicePicker
{
private Windows.Devices.Enumeration.DevicePicker _devicePicker = new Windows.Devices.Enumeration.DevicePicker();
private Windows.Devices.Enumeration.DevicePicker _devicePicker;
internal DevicePickerUap()
{
_devicePicker = new Windows.Devices.Enumeration.DevicePicker();
}
private DevicePicker(Windows.Devices.Enumeration.DevicePicker devicePicker)
private DevicePickerUap(Windows.Devices.Enumeration.DevicePicker devicePicker)
{
_devicePicker = devicePicker;
}
public static implicit operator Windows.Devices.Enumeration.DevicePicker(DevicePicker devicePicker)
public static implicit operator Windows.Devices.Enumeration.DevicePicker(DevicePickerUap devicePicker)
{
return devicePicker._devicePicker;
}
public static implicit operator DevicePicker(Windows.Devices.Enumeration.DevicePicker devicePicker)
public static implicit operator DevicePickerUap(Windows.Devices.Enumeration.DevicePicker devicePicker)
{
return new DevicePicker(devicePicker);
return new DevicePickerUap(devicePicker);
}
private void Initialize()
@@ -51,20 +51,69 @@ private void _picker_DevicePickerDismissed(Windows.Devices.Enumeration.DevicePic
OnDevicePickerDismissed();
}*/
private DevicePickerAppearance GetAppearance()
/*public DevicePickerAppearance Appearance
{
get
{
return _devicePicker.Appearance;
}
}*/
public DevicePickerFilter Filter
{
return _devicePicker.Appearance;
get
{
#if WIN32
var filter = new DevicePickerFilter();
//_devicePicker.Filter.SupportedDeviceSelectors.CopyTo(filter.SupportedDeviceSelectors,0);
return filter;
#else
return _devicePicker.Filter;
#endif
}
}
private DevicePickerFilter GetFilter()
public DeviceInformation PickSingleDevice()
{
return _devicePicker.Filter;
Task<DeviceInformation> t = Task.Run<DeviceInformation>(async () =>
{
var d = await _devicePicker.PickSingleDeviceAsync(Windows.Foundation.Rect.Empty);
#if WIN32
if (d != null)
{
Bluetooth.BLUETOOTH_DEVICE_INFO info = new Bluetooth.BLUETOOTH_DEVICE_INFO();
info.Address = 0;
return new DeviceInformation(info);
}
return null;
#else
return d == null ? null : d;
#endif
});
// TODO: this compiles but does it behave?
t.Wait();
return t.Result;
}
private async Task<DeviceInformation> DoPickSingleDeviceAsync()
public async Task<DeviceInformation> PickSingleDeviceAsync()
{
var d = await _devicePicker.PickSingleDeviceAsync(Windows.Foundation.Rect.Empty);
#if WIN32
if(d != null)
{
Bluetooth.BLUETOOTH_DEVICE_INFO info = new Bluetooth.BLUETOOTH_DEVICE_INFO();
info.Address = 0;
return new DeviceInformation(info);
}
return null;
#else
return d == null ? null : d;
#endif
}
@@ -17,9 +17,11 @@
namespace InTheHand.Devices.Enumeration
{
partial class DevicePicker
internal class DevicePickerWin32 : IDevicePicker
{
//private NativeMethods.PFN_DEVICE_CALLBACK _callback;
//public DevicePickerAppearance Appearance { get; } = new DevicePickerAppearance();
public DevicePickerFilter Filter { get; } = new DevicePickerFilter();
public DeviceInformation PickSingleDevice()
{
@@ -28,10 +30,10 @@ public DeviceInformation PickSingleDevice()
sdp.hwndParent = Process.GetCurrentProcess().MainWindowHandle;
sdp.numDevices = 1;
#if !UNITY
if (!string.IsNullOrEmpty(Appearance.Title))
/*if (!string.IsNullOrEmpty(Appearance.Title))
{
sdp.info = Appearance.Title;
}
}*/
#endif
//defaults
sdp.fShowAuthenticated = true;
@@ -137,16 +139,16 @@ public DeviceInformation PickSingleDevice()
}
#if !UNITY
private async Task<DeviceInformation> DoPickSingleDeviceAsync()
public async Task<DeviceInformation> PickSingleDeviceAsync()
{
NativeMethods.BLUETOOTH_SELECT_DEVICE_PARAMS sdp = new NativeMethods.BLUETOOTH_SELECT_DEVICE_PARAMS();
sdp.dwSize = Marshal.SizeOf(sdp);
sdp.hwndParent = Process.GetCurrentProcess().MainWindowHandle;
sdp.numDevices = 1;
if(!string.IsNullOrEmpty(Appearance.Title))
/*if(!string.IsNullOrEmpty(Appearance.Title))
{
sdp.info = Appearance.Title;
}
}*/
//defaults
sdp.fShowAuthenticated = true;

0 comments on commit 47e2762

Please sign in to comment.