Skip to content

Commit 4c07559

Browse files
committed
Resolve samsung keyboard issue
1 parent 395dca2 commit 4c07559

File tree

2 files changed

+99
-12
lines changed

2 files changed

+99
-12
lines changed

maui/src/NumericEntry/SfNumericEntry.Methods.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -707,12 +707,12 @@ void AndroidEntry_AfterTextChanged(object? sender, Android.Text.AfterTextChanged
707707
string decimalSeparator = GetNumberDecimalSeparator(GetNumberFormat());
708708
if (entry != null && (entry.Text == "-" || entry.Text == decimalSeparator))
709709
{
710-
if (!IsSamsungDevice() && _textBox != null)
710+
if (!_isSamsungWithSamsungKeyboard && _textBox != null)
711711
{
712712
_textBox.Text = AllowNull ? string.Empty : "0";
713713
}
714714
}
715-
if (IsSamsungDevice() && _textBox != null && _textBox.Text.Length > 1)
715+
if (_isSamsungWithSamsungKeyboard && _textBox != null && _textBox.Text.Length > 1)
716716
{
717717
if (_textBox.Text[0] == decimalSeparator[0] && _textBox.Text.LastIndexOf(decimalSeparator) == 0)
718718
{
@@ -732,13 +732,21 @@ void AndroidEntry_AfterTextChanged(object? sender, Android.Text.AfterTextChanged
732732
}
733733

734734
/// <summary>
735-
/// Validate the device is Samsung.
735+
/// Validate the device is Samsung and which keyboard is used.
736736
/// </summary>
737-
/// <returns>It returns <c>True</c> if the device is Samsung</returns>
738-
bool IsSamsungDevice()
739-
{
740-
return string.Equals(global::Android.OS.Build.Manufacturer, "samsung", StringComparison.OrdinalIgnoreCase);
741-
}
737+
void CheckDeviceAndKeyboard()
738+
{
739+
if (string.Equals(global::Android.OS.Build.Manufacturer, "samsung", StringComparison.OrdinalIgnoreCase))
740+
{
741+
var context = Android.App.Application.Context;
742+
string currentKeyboard = KeyboardChecker.GetCurrentKeyboard(context);
743+
_isSamsungWithSamsungKeyboard = currentKeyboard == "Samsung Keyboard";
744+
}
745+
else
746+
{
747+
_isSamsungWithSamsungKeyboard = false;
748+
}
749+
}
742750

743751
#endif
744752

@@ -1597,6 +1605,9 @@ void OnLoaded(object? sender, EventArgs e)
15971605
{
15981606
SetFlowDirection();
15991607
}
1608+
1609+
#elif ANDROID
1610+
CheckDeviceAndKeyboard();
16001611
#endif
16011612

16021613
}
@@ -1879,7 +1890,7 @@ void ProcessDisplayTextForPlatform(ref string displayText)
18791890

18801891
#if ANDROID
18811892
// Prefix zero if needed, except on Samsung devices
1882-
if (!IsSamsungDevice())
1893+
if (!_isSamsungWithSamsungKeyboard)
18831894
{
18841895
PrefixZeroIfNeeded(ref displayText, IsNegative(displayText, GetNumberFormat()), GetNumberFormat());
18851896
}
@@ -1992,7 +2003,7 @@ void UpdateCursorPosition(int decimalIndex, int caretPosition, string displayTex
19922003
return;
19932004
}
19942005
#if ANDROID
1995-
if (!IsSamsungDevice())
2006+
if (!_isSamsungWithSamsungKeyboard)
19962007
{
19972008
_cursorPosition = decimalIndex + 1;
19982009
}
@@ -2081,7 +2092,7 @@ void HandleNegativeKey(bool isNegative, int caretPosition)
20812092
#endif
20822093
}
20832094
#if ANDROID
2084-
else if (displayText.StartsWith('.') && IsSamsungDevice())
2095+
else if (displayText.StartsWith('.') && _isSamsungWithSamsungKeyboard)
20852096
{
20862097
string decimalSeparator = GetNumberDecimalSeparator(GetNumberFormat());
20872098
displayText = string.Concat("", displayText.AsSpan(1));

maui/src/NumericEntry/SfNumericEntry.cs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
using Syncfusion.Maui.Toolkit.EntryView;
99
#if ANDROID
1010
using Android.Text;
11+
using Android.Provider;
12+
using Android.Content;
13+
using Android.Views.InputMethods;
14+
using System.Linq;
1115
#elif MACCATALYST || IOS
1216
using UIKit;
1317
using Foundation;
@@ -266,6 +270,11 @@ public partial class SfNumericEntry : SfView, ITextElement, ITouchListener, IKey
266270
/// </summary>
267271
readonly CornerRadius _initialCornderRadius = 5;
268272
#elif ANDROID
273+
/// <summary>
274+
/// Indicates whether the samsung device use samsung keyboard or any other.
275+
/// </summary>
276+
bool _isSamsungWithSamsungKeyboard;
277+
269278
/// <summary>
270279
/// Holds the length of text currently selected by the user.
271280
/// </summary>
@@ -541,7 +550,7 @@ protected void OnGotFocus()
541550
protected void OnLostFocus()
542551
{
543552
#if ANDROID
544-
if (IsSamsungDevice() && _textBox != null)
553+
if (_isSamsungWithSamsungKeyboard && _textBox != null)
545554
{
546555
// Ensure _textBox.Text isn't just a decimal separator or a minus sign
547556
if (_textBox.Text == GetNumberDecimalSeparator(GetNumberFormat()) || _textBox.Text == "-")
@@ -995,4 +1004,71 @@ void UpdateFrames()
9951004
#endif
9961005
#endregion
9971006
}
1007+
1008+
#if ANDROID
1009+
1010+
/// <summary>
1011+
/// A utility class for checking the current keyboard in use on an Android device.
1012+
/// </summary>
1013+
internal static class KeyboardChecker
1014+
{
1015+
1016+
/// <summary>
1017+
/// A constant representing the package name for the Google Keyboard application.
1018+
/// </summary>
1019+
const string GboardPackage = "com.google.android.inputmethod.latin";
1020+
1021+
1022+
// Update the known package names for Samsung Keyboard and Gboard
1023+
static readonly string[] SamsungKeyboardPackages = ["com.samsung.android.keyboard", "com.sec.android.inputmethod", "com.samsung.android.honeyboard"];
1024+
1025+
/// <summary>
1026+
/// Gets the name of the current keyboard being used on the Android device.
1027+
/// </summary>
1028+
/// <param name="context">The context of the application, used to access system services.</param>
1029+
/// <returns>A string representing the name of the keyboard: "Samsung Keyboard", "Gboard", "Other Keyboard", or "Unknown Keyboard".</returns>
1030+
public static string GetCurrentKeyboard(Context context)
1031+
{
1032+
if (context.GetSystemService(Context.InputMethodService) is InputMethodManager inputMethodManager && inputMethodManager != null)
1033+
{
1034+
var defaultInputMethodId = Settings.Secure.GetString(
1035+
context.ContentResolver,
1036+
Settings.Secure.DefaultInputMethod
1037+
);
1038+
1039+
if (string.IsNullOrEmpty(defaultInputMethodId))
1040+
{
1041+
return "Unknown Keyboard";
1042+
}
1043+
1044+
var inputMethodList = inputMethodManager.InputMethodList;
1045+
foreach (var inputMethod in inputMethodList)
1046+
{
1047+
if (inputMethod?.Id != null && inputMethod.Id.Equals(defaultInputMethodId, StringComparison.Ordinal))
1048+
{
1049+
var packageName = inputMethod.PackageName;
1050+
if (!string.IsNullOrEmpty(packageName))
1051+
{
1052+
if (SamsungKeyboardPackages.Contains(packageName))
1053+
{
1054+
return "Samsung Keyboard";
1055+
}
1056+
else if (packageName.Equals(GboardPackage, StringComparison.OrdinalIgnoreCase))
1057+
{
1058+
return "Gboard";
1059+
}
1060+
else
1061+
{
1062+
return "Other Keyboard";
1063+
}
1064+
}
1065+
}
1066+
}
1067+
}
1068+
1069+
return "Unknown Keyboard";
1070+
}
1071+
}
1072+
#endif
1073+
9981074
}

0 commit comments

Comments
 (0)