summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/LocalizedDigitsKeyListener.cs
blob: 009cffbba1e65f8294e12dafc84c70808142d5b7 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System.Collections.Generic;
using Android.Text;
using Android.Text.Method;
using Java.Lang;
using Java.Text;

namespace Xamarin.Forms.Platform.Android
{
	internal class LocalizedDigitsKeyListener : NumberKeyListener
	{
		readonly char _decimalSeparator;

		// I'm not aware of a situation/locale where this would need to be something different, 
		// but we'll make it easy to localize the sign in the future just in case
		const char SignCharacter = '-';

		static Dictionary<char, LocalizedDigitsKeyListener> s_unsignedCache;
		static Dictionary<char, LocalizedDigitsKeyListener> s_signedCache;

		static char GetDecimalSeparator()
		{
			var format = NumberFormat.Instance as DecimalFormat;
			if (format == null)
			{
				return '.';
			}

			DecimalFormatSymbols sym = format.DecimalFormatSymbols;
			return sym.DecimalSeparator;
		}

		public static NumberKeyListener Create(InputTypes inputTypes)
		{
			if ((inputTypes & InputTypes.NumberFlagDecimal) == 0)
			{
				// If decimal isn't allowed, we can just use the Android version
				return DigitsKeyListener.GetInstance(inputTypes.HasFlag(InputTypes.NumberFlagSigned), false);
			}

			// Figure out what the decimal separator is for the current locale
			char decimalSeparator = GetDecimalSeparator();

			if (decimalSeparator == '.')
			{
				// If it's '.', then we can just use the default Android version
				return DigitsKeyListener.GetInstance(inputTypes.HasFlag(InputTypes.NumberFlagSigned), true);
			}

			// If decimals are enabled and the locale's decimal separator is not '.'
			// (which is hard-coded in the Android DigitKeyListener), then use 
			// our custom one with a configurable decimal separator
			return GetInstance(inputTypes, decimalSeparator);
		}

		public static LocalizedDigitsKeyListener GetInstance(InputTypes inputTypes, char decimalSeparator)
		{
			if ((inputTypes & InputTypes.NumberFlagSigned) != 0)
			{
				return GetInstance(inputTypes, decimalSeparator, ref s_signedCache);
			}

			return GetInstance(inputTypes, decimalSeparator, ref s_unsignedCache);
		}

		static LocalizedDigitsKeyListener GetInstance(InputTypes inputTypes, char decimalSeparator, ref Dictionary<char, LocalizedDigitsKeyListener> cache)
		{
			if (cache == null)
			{
				cache = new Dictionary<char, LocalizedDigitsKeyListener>(1);
			}

			if (!cache.ContainsKey(decimalSeparator))
			{
				cache.Add(decimalSeparator, new LocalizedDigitsKeyListener(inputTypes, decimalSeparator));
			}

			return cache[decimalSeparator];
		}

		protected LocalizedDigitsKeyListener(InputTypes inputTypes, char decimalSeparator)
		{
			_decimalSeparator = decimalSeparator;
			InputType = inputTypes;
		}

		public override InputTypes InputType { get; }

		char[] _acceptedChars;

		protected override char[] GetAcceptedChars()
		{
			if ((InputType & InputTypes.NumberFlagSigned) == 0)
			{
				return _acceptedChars ??
				       (_acceptedChars = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', _decimalSeparator });
			}

			return _acceptedChars ??
			       (_acceptedChars = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', SignCharacter, _decimalSeparator });
		}

		static bool IsSignChar(char c)
		{
			return c == SignCharacter;
		}

		bool IsDecimalPointChar(char c)
		{
			return c == _decimalSeparator;
		}

		public override ICharSequence FilterFormatted(ICharSequence source, int start, int end, ISpanned dest, int dstart,
			int dend)
		{
			// Borrowed heavily from the Android source
			ICharSequence filterFormatted = base.FilterFormatted(source, start, end, dest, dstart, dend);

			if (filterFormatted != null)
			{
				source = filterFormatted;
				start = 0;
				end = filterFormatted.Length();
			}

			int sign = -1;
			int dec = -1;
			int dlen = dest.Length();

			// Find out if the existing text has a sign or decimal point characters.
			for (var i = 0; i < dstart; i++)
			{
				char c = dest.CharAt(i);
				if (IsSignChar(c))
				{
					sign = i;
				}
				else if (IsDecimalPointChar(c))
				{
					dec = i;
				}
			}

			for (int i = dend; i < dlen; i++)
			{
				char c = dest.CharAt(i);
				if (IsSignChar(c))
				{
					return new String(""); // Nothing can be inserted in front of a sign character.
				}

				if (IsDecimalPointChar(c))
				{
					dec = i;
				}
			}

			// If it does, we must strip them out from the source.
			// In addition, a sign character must be the very first character,
			// and nothing can be inserted before an existing sign character.
			// Go in reverse order so the offsets are stable.
			SpannableStringBuilder stripped = null;
			for (int i = end - 1; i >= start; i--)
			{
				char c = source.CharAt(i);
				var strip = false;

				if (IsSignChar(c))
				{
					if (i != start || dstart != 0)
					{
						strip = true;
					}
					else if (sign >= 0)
					{
						strip = true;
					}
					else
					{
						sign = i;
					}
				}
				else if (IsDecimalPointChar(c))
				{
					if (dec >= 0)
					{
						strip = true;
					}
					else
					{
						dec = i;
					}
				}

				if (strip)
				{
					if (end == start + 1)
					{
						return new String(""); // Only one character, and it was stripped.
					}
					if (stripped == null)
					{
						stripped = new SpannableStringBuilder(source, start, end);
					}
					stripped.Delete(i - start, i + 1 - start);
				}
			}

			return stripped ?? filterFormatted;
		}
	}
}