diff options
Diffstat (limited to 'Xamarin.Forms.Platform.Android/Renderers/KeyboardExtensions.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Android/Renderers/KeyboardExtensions.cs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/Renderers/KeyboardExtensions.cs b/Xamarin.Forms.Platform.Android/Renderers/KeyboardExtensions.cs new file mode 100644 index 00000000..ab168b6d --- /dev/null +++ b/Xamarin.Forms.Platform.Android/Renderers/KeyboardExtensions.cs @@ -0,0 +1,77 @@ +using Android.Text; + +namespace Xamarin.Forms.Platform.Android +{ + internal static class KeyboardExtensions + { + public static InputTypes ToInputType(this Keyboard self) + { + var result = new InputTypes(); + + // ClassText: !autocaps, spellcheck, suggestions + // TextFlagNoSuggestions: !autocaps, !spellcheck, !suggestions + // InputTypes.ClassText | InputTypes.TextFlagCapSentences autocaps, spellcheck, suggestions + // InputTypes.ClassText | InputTypes.TextFlagCapSentences | InputTypes.TextFlagNoSuggestions; autocaps, !spellcheck, !suggestions + + if (self == Keyboard.Default) + result = InputTypes.ClassText | InputTypes.TextVariationNormal; + else if (self == Keyboard.Chat) + result = InputTypes.ClassText | InputTypes.TextFlagCapSentences | InputTypes.TextFlagNoSuggestions; + else if (self == Keyboard.Email) + result = InputTypes.ClassText | InputTypes.TextVariationEmailAddress; + else if (self == Keyboard.Numeric) + result = InputTypes.ClassNumber | InputTypes.NumberFlagDecimal; + else if (self == Keyboard.Telephone) + result = InputTypes.ClassPhone; + else if (self == Keyboard.Text) + result = InputTypes.ClassText | InputTypes.TextFlagCapSentences; + else if (self == Keyboard.Url) + result = InputTypes.ClassText | InputTypes.TextVariationUri; + else if (self is CustomKeyboard) + { + var custom = (CustomKeyboard)self; + bool capitalizedSentenceEnabled = (custom.Flags & KeyboardFlags.CapitalizeSentence) == KeyboardFlags.CapitalizeSentence; + bool spellcheckEnabled = (custom.Flags & KeyboardFlags.Spellcheck) == KeyboardFlags.Spellcheck; + bool suggestionsEnabled = (custom.Flags & KeyboardFlags.Suggestions) == KeyboardFlags.Suggestions; + + if (!capitalizedSentenceEnabled && !spellcheckEnabled && !suggestionsEnabled) + result = InputTypes.ClassText | InputTypes.TextFlagNoSuggestions; + + if (!capitalizedSentenceEnabled && !spellcheckEnabled && suggestionsEnabled) + { + // Due to the nature of android, TextFlagAutoCorrect includes Spellcheck + Log.Warning(null, "On Android, KeyboardFlags.Suggestions enables KeyboardFlags.Spellcheck as well due to a platform limitation."); + result = InputTypes.ClassText | InputTypes.TextFlagAutoCorrect; + } + + if (!capitalizedSentenceEnabled && spellcheckEnabled && !suggestionsEnabled) + result = InputTypes.ClassText | InputTypes.TextFlagAutoComplete; + + if (!capitalizedSentenceEnabled && spellcheckEnabled && suggestionsEnabled) + result = InputTypes.ClassText | InputTypes.TextFlagAutoCorrect; + + if (capitalizedSentenceEnabled && !spellcheckEnabled && !suggestionsEnabled) + result = InputTypes.ClassText | InputTypes.TextFlagCapSentences | InputTypes.TextFlagNoSuggestions; + + if (capitalizedSentenceEnabled && !spellcheckEnabled && suggestionsEnabled) + { + // Due to the nature of android, TextFlagAutoCorrect includes Spellcheck + Log.Warning(null, "On Android, KeyboardFlags.Suggestions enables KeyboardFlags.Spellcheck as well due to a platform limitation."); + result = InputTypes.ClassText | InputTypes.TextFlagCapSentences | InputTypes.TextFlagAutoCorrect; + } + + if (capitalizedSentenceEnabled && spellcheckEnabled && !suggestionsEnabled) + result = InputTypes.ClassText | InputTypes.TextFlagCapSentences | InputTypes.TextFlagAutoComplete; + + if (capitalizedSentenceEnabled && spellcheckEnabled && suggestionsEnabled) + result = InputTypes.ClassText | InputTypes.TextFlagCapSentences | InputTypes.TextFlagAutoCorrect; + } + else + { + // Should never happens + result = InputTypes.TextVariationNormal; + } + return result; + } + } +}
\ No newline at end of file |