summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/KeyboardExtensions.cs
blob: 9a22e641162b1fd5b6a399cdb1abaf10f4ac041c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Android.Text;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Platform.Android
{
	public 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 | InputTypes.NumberFlagSigned;
			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;
		}
	}
}