Browse Source

Several Android TV compatibility improvements

xy-focus-and-tvos
Max Katz 2 years ago
parent
commit
42e3dad005
  1. 2
      samples/ControlCatalog.Android/ControlCatalog.Android.csproj
  2. 9
      samples/ControlCatalog.Android/MainActivity.cs
  3. 14
      src/Android/Avalonia.Android/Platform/Input/AndroidKeyboardDevice.cs
  4. 17
      src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidKeyboardEventsHelper.cs

2
samples/ControlCatalog.Android/ControlCatalog.Android.csproj

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-android</TargetFramework>
<TargetFramework>net8.0-android</TargetFramework>
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>

9
samples/ControlCatalog.Android/MainActivity.cs

@ -4,11 +4,16 @@ using Avalonia;
using Avalonia.Android;
using static Android.Content.Intent;
// leanback and touchscreen are required for the Android TV.
[assembly: UsesFeature("android.software.leanback", Required = false)]
[assembly: UsesFeature("android.hardware.touchscreen", Required = false)]
namespace ControlCatalog.Android
{
[Activity(Label = "ControlCatalog.Android", Theme = "@style/MyTheme.NoActionBar", Icon = "@drawable/icon", MainLauncher = true, Exported = true, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.UiMode)]
// IntentFilter are here to test IActivatableApplicationLifetime
[IntentFilter(new [] { ActionView }, Categories = new [] { CategoryDefault, CategoryBrowsable }, DataScheme = "avln" )]
// CategoryBrowsable and DataScheme are required for Protocol activation.
// CategoryLeanbackLauncher is required for Android TV.
[IntentFilter(new [] { ActionView }, Categories = new [] { CategoryDefault, CategoryBrowsable, CategoryLeanbackLauncher }, DataScheme = "avln" )]
public class MainActivity : AvaloniaMainActivity<App>
{
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)

14
src/Android/Avalonia.Android/Platform/Input/AndroidKeyboardDevice.cs

@ -31,10 +31,6 @@ namespace Avalonia.Android.Platform.Input
{ Keycode.PageDown, Key.PageDown },
{ Keycode.MoveEnd, Key.End },
{ Keycode.MoveHome, Key.Home },
{ Keycode.DpadLeft, Key.Left },
{ Keycode.DpadUp, Key.Up },
{ Keycode.DpadRight, Key.Right },
{ Keycode.DpadDown, Key.Down },
// { Keycode.ButtonSelect?, Key.Select },
// { Keycode.print?, Key.Print },
//{ Keycode.execute?, Key.Execute },
@ -209,7 +205,15 @@ namespace Avalonia.Android.Platform.Input
//{ Keycode.?, Key.DbeEnterDialogConversionMode }
//{ Keycode.?, Key.OemClear }
//{ Keycode.?, Key.DeadCharProcessed }
{ Keycode.Backslash, Key.OemBackslash }
{ Keycode.Backslash, Key.OemBackslash },
// Loosely mapping DPad keys to Avalonia keys
{ Keycode.Back, Key.Escape },
{ Keycode.DpadCenter, Key.Space },
{ Keycode.DpadLeft, Key.Left },
{ Keycode.DpadUp, Key.Up },
{ Keycode.DpadRight, Key.Right },
{ Keycode.DpadDown, Key.Down }
};
internal static Key ConvertKey(Keycode key)

17
src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidKeyboardEventsHelper.cs

@ -129,15 +129,20 @@ namespace Avalonia.Android.Platform.Specific.Helpers
private KeyDeviceType GetKeyDeviceType(KeyEvent e)
{
var source = e.Device?.Sources ?? InputSourceType.Unknown;
if (source is InputSourceType.Joystick or
InputSourceType.ClassJoystick or
InputSourceType.Gamepad)
return KeyDeviceType.Gamepad;
if (source == InputSourceType.Dpad && e.Device?.KeyboardType == InputKeyboardType.NonAlphabetic)
// Remote controller reports itself as "DPad | Keyboard", which is confusing,
// so we need to double-check KeyboardType as well.
if (source.HasAnyFlag(InputSourceType.Dpad)
&& e.Device?.KeyboardType == InputKeyboardType.NonAlphabetic)
return KeyDeviceType.Remote;
return KeyDeviceType.Keyboard;
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags - it IS flags enum under the hood.
if (source.HasAnyFlag(InputSourceType.Joystick | InputSourceType.Gamepad))
return KeyDeviceType.Gamepad;
// ReSharper restore BitwiseOperatorOnEnumWithoutFlags
return KeyDeviceType.Keyboard; // fallback to the keyboard, if unknown.
}
public void Dispose()

Loading…
Cancel
Save