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

[Android] Allow custom handler with key listener #22546

Merged
merged 5 commits into from
May 22, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#if ANDROID
using NUnit.Framework;
using NUnit.Framework.Legacy;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue21109 : _IssuesUITest
{
public Issue21109(TestDevice device) : base(device) { }

public override string Issue => "[Android] MAUI 8.0.3 -> 8.0.6 regression: custom handler with key listener no longer works";

[Test]
[Category(UITestCategories.Entry)]
public void EntryReturnTypeWorks()
{
App.WaitForElement("WaitForStubControl");

// Verify that ReturnType works as expected.
if (App.IsKeyboardShown())
App.DismissKeyboard();

var returnType1 = App.FindElement("ReturnTypeResult").GetText();
App.Tap("ReturnTypeEntry");
App.EnterText("ReturnTypeEntry", "a");
var returnType2 = App.FindElement("ReturnTypeResult").GetText();
ClassicAssert.AreNotEqual(returnType1, returnType2);
}
}
}
#endif
29 changes: 29 additions & 0 deletions src/Controls/tests/TestCases/Issues/Issue21109.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue21109"
xmlns:controls="clr-namespace:Maui.Controls.Sample.Issues">
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Label
AutomationId="WaitForStubControl"
Text="ReturnType (Search)" />
<Entry
AutomationId="SearchEntry"
ReturnType="Search" />
<Label
Text="Update the ReturnType just typing" />
<Entry
x:Name="ReturnTypeEntry"
AutomationId="ReturnTypeEntry"
TextChanged="OnReturnTypeEntryTextChanged"/>
<Label
x:Name="ReturnTypeResult"
AutomationId="ReturnTypeResult"/>
<Label
Text="Custom DecimalEntry" />
<controls:Issue21109DecimalEntry
AutomationId="CustomDecimalEntry"/>
</VerticalStackLayout>
</ContentPage>
65 changes: 65 additions & 0 deletions src/Controls/tests/TestCases/Issues/Issue21109.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Hosting;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 21109, "[Android] MAUI 8.0.3 -> 8.0.6 regression: custom handler with key listener no longer works", PlatformAffected.All)]
public partial class Issue21109 : ContentPage
{
public Issue21109()
{
InitializeComponent();

ReturnTypeResult.Text = $"ReturnType: {ReturnTypeEntry.ReturnType}";
}

void OnReturnTypeEntryTextChanged(object sender, TextChangedEventArgs e)
{
Random rnd = new Random();
var returnTypeCount = Enum.GetNames(typeof(ReturnType)).Length;

ReturnType returnType = ReturnType.Default;

do
{
returnType = (ReturnType)rnd.Next(0, returnTypeCount);
} while (returnType == ReturnTypeEntry.ReturnType);

ReturnTypeEntry.ReturnType = returnType;
ReturnTypeResult.Text = $"ReturnType: {returnType}";
}
}

public class Issue21109DecimalEntry : Entry
{
public Issue21109DecimalEntry()
{
Keyboard = Keyboard.Numeric;
}
}

public static class Issue21109Extensions
{
public static MauiAppBuilder Issue21109AddMappers(this MauiAppBuilder builder)
{
builder.ConfigureMauiHandlers(handlers =>
{
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping(nameof(Issue21109DecimalEntry), (handler, view) =>
{
if (view is Issue21109DecimalEntry)
{
#if ANDROID
handler.PlatformView.KeyListener = new Platform.Issue21109NumericKeyListener(handler.PlatformView.InputType);
#endif
}
});
});

return builder;
}
}
}
21 changes: 13 additions & 8 deletions src/Controls/tests/TestCases/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Maui.Controls.Sample.Issues;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Hosting;
Expand All @@ -8,18 +9,22 @@ namespace Maui.Controls.Sample
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp() =>
MauiApp
.CreateBuilder()
#if IOS || ANDROID
.UseMauiMaps()
#endif
.UseMauiApp<App>()
public static MauiApp CreateMauiApp()
{
var appBuilder = MauiApp.CreateBuilder();

#if IOS || ANDROID
appBuilder.UseMauiMaps();
#endif
appBuilder.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
})
.Build();
.Issue21109AddMappers();

return appBuilder.Build();
}
}

class App : Application
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Android.Text;
using Android.Text.Method;
using Android.Views;
using Microsoft.Maui.Controls;
using AView = Android.Views.View;

namespace Maui.Controls.Sample.Platform;

public class Issue21109NumericKeyListener : NumberKeyListener
{
public override InputTypes InputType { get; }

protected override char[] GetAcceptedChars() => "0123456789-,.".ToCharArray();

public Issue21109NumericKeyListener(InputTypes inputType)
{
InputType = inputType;
}

public override bool OnKeyDown(AView view, IEditable content, Keycode keyCode, KeyEvent e)
{
Application.Current.MainPage.DisplayAlert("OnKeyDown", string.Empty, "Ok");
return base.OnKeyDown(view, content, keyCode, e);
}
}
10 changes: 0 additions & 10 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public override void SetVirtualView(IView view)
// TODO: NET8 issoto - Change the return type to MauiAppCompatEditText
protected override void ConnectHandler(AppCompatEditText platformView)
{
platformView.ViewAttachedToWindow += OnViewAttachedToWindow;
platformView.TextChanged += OnTextChanged;
platformView.FocusChange += OnFocusedChange;
platformView.Touch += OnTouch;
Expand All @@ -54,7 +53,6 @@ protected override void DisconnectHandler(AppCompatEditText platformView)
{
_clearButtonDrawable = null;

platformView.ViewAttachedToWindow -= OnViewAttachedToWindow;
platformView.TextChanged -= OnTextChanged;
platformView.FocusChange -= OnFocusedChange;
platformView.Touch -= OnTouch;
Expand Down Expand Up @@ -148,14 +146,6 @@ static void MapFocus(IEntryHandler handler, IEntry entry, object? args)
handler.PlatformView.Focus(request);
}

void OnViewAttachedToWindow(object? sender, ViewAttachedToWindowEventArgs e)
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
{
if (PlatformView is null || VirtualView is null)
return;

PlatformView.UpdateReturnType(VirtualView);
}

void OnTextChanged(object? sender, TextChangedEventArgs e)
{
if (VirtualView == null)
Expand Down
6 changes: 5 additions & 1 deletion src/Core/src/Platform/Android/EditTextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using Android.Content;
using Android.Content.Res;
using Android.Graphics.Drawables;
using Android.Text;
Expand Down Expand Up @@ -203,6 +203,10 @@ public static void UpdateReturnType(this EditText editText, IEntry entry)
{
editText.SetInputType(entry);
editText.ImeOptions = entry.ReturnType.ToPlatform();

// Restart the input on the current focused EditText
InputMethodManager? imm = (InputMethodManager?)editText.Context?.GetSystemService(Context.InputMethodService);
imm?.RestartInput(editText);
}

// TODO: NET8 issoto - Revisit this, marking this method as `internal` to avoid breaking public API changes
Expand Down
Loading